-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.php
98 lines (80 loc) · 2.61 KB
/
vars.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
require_once('exception.php');
class vars
{
const names_flat = '\w+';
const names_objective = '[\w:]+\w';
function __wakeup()
{
$this->initialize();
}
function initialize()
{
$top = array
(
'user:agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
'user:ip' => $_SERVER['REMOTE_ADDR']
);
foreach($_GET as $name => $value)
{
$top["get:$name"] = $value;
}
if(isset($_SESSION))
{
foreach($_SESSION as $name => $value)
{
$top["session:$name"] = $value;
}
}
$this->push($top);
}
function get($name)
{
$top = &$this->vars[count($this->vars) - 1];
isset($top[$name]) or runtime_error('Variable not found: ' . $name);
return $top[$name];
}
function insert($name, $value)
{
$top = &$this->vars[count($this->vars) - 1];
preg_match('/\A[\w:]+\w\Z/', $name) or runtime_error('Invalid characters in variable name: ' . $name);
!isset($top[$name]) or runtime_error('Duplicate variable name: ' . $name);
$top[$name] = $this->apply($value);
}
function push($args)
{
$this->vars[] = end($this->vars);
foreach($args as $name => $value)
{
$this->insert($name, $value);
}
}
function pop()
{
array_pop($this->vars);
}
function apply($string, $quotes = false, $names = vars::names_objective)
{
$self = $this;
return preg_replace_callback('/\$(' . $names . ')/', function($matches) use($self, $quotes)
{
return $self->replace($matches[1], $quotes);
}, $string);
//return preg_replace('/\$(' . $names . ')/e', "\$this->replace('\\1', \$quotes)", $string);
}
static function apply_assoc($string, $vars, $quotes = false, $names = vars::names_objective)
{
return preg_replace('/\$(' . $names . ')/e', "self::replace_assoc('\\1', \$vars, \$quotes)", $string);
}
private function replace($name, $quotes)
{
$top = &$this->vars[count($this->vars) - 1];
return isset($top[$name]) ? ($quotes ? args::quote($top[$name]) : $top[$name]) : '$' . $name;
}
private static function replace_assoc($name, $vars, $quotes)
{
return isset($vars[$name]) ? ($quotes ? args::quote($vars[$name]) : $vars[$name]) : '$' . $name;
}
private $vars = array(array());
}
?>