|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
This file is part of WideImage. |
|
4
|
|
|
|
|
5
|
|
|
WideImage is free software; you can redistribute it and/or modify |
|
6
|
|
|
it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
the Free Software Foundation; either version 2.1 of the License, or |
|
8
|
|
|
(at your option) any later version. |
|
9
|
|
|
|
|
10
|
|
|
WideImage is distributed in the hope that it will be useful, |
|
11
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
GNU Lesser General Public License for more details. |
|
14
|
|
|
|
|
15
|
|
|
You should have received a copy of the GNU Lesser General Public License |
|
16
|
|
|
along with WideImage; if not, write to the Free Software |
|
17
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
18
|
|
|
**/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @package Demos |
|
22
|
|
|
*/ |
|
23
|
|
|
class Request |
|
24
|
|
|
{ |
|
25
|
|
|
protected $vars = array(); |
|
26
|
|
|
|
|
27
|
|
|
protected static $instance; |
|
28
|
|
|
static function getInstance() |
|
29
|
|
|
{ |
|
30
|
|
|
if (self::$instance === null) |
|
31
|
|
|
self::$instance = new Request; |
|
32
|
|
|
return self::$instance; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->vars = $_GET; |
|
38
|
|
|
|
|
39
|
|
|
/* |
|
40
|
|
|
// have to rely on parsing QUERY_STRING, thanks to PHP |
|
41
|
|
|
// http://bugs.php.net/bug.php?id=39078 |
|
42
|
|
|
// http://bugs.php.net/bug.php?id=45149 |
|
43
|
|
|
$all_vars = explode('&', $_SERVER['QUERY_STRING']); |
|
44
|
|
|
foreach ($all_vars as $keyval) |
|
45
|
|
|
{ |
|
46
|
|
|
if (strlen($keyval) == 0) |
|
47
|
|
|
continue; |
|
48
|
|
|
|
|
49
|
|
|
if (strpos($keyval, '=') === false) |
|
50
|
|
|
{ |
|
51
|
|
|
$key = $keyval; |
|
52
|
|
|
$value = true; |
|
53
|
|
|
} |
|
54
|
|
|
else |
|
55
|
|
|
{ |
|
56
|
|
|
list($key, $value) = explode('=', $keyval); |
|
57
|
|
|
#$value = str_replace('%2B', '[[PLUS]]', $value); |
|
58
|
|
|
$value = urldecode($value); |
|
59
|
|
|
#$value = str_replace('[[PLUS]]', '+', $value); |
|
60
|
|
|
} |
|
61
|
|
|
$this->vars[$key] = $value; |
|
62
|
|
|
} |
|
63
|
|
|
*/ |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
function get($key, $default = null) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
if (isset($this->vars[$key])) |
|
69
|
|
|
return $this->vars[$key]; |
|
70
|
|
|
else |
|
71
|
|
|
return $default; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function set($key, $value) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$this->vars[$key] = $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
function getInt($key, $default = 0) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$value = self::get($key); |
|
|
|
|
|
|
82
|
|
|
if (strlen($value) > 0) |
|
|
|
|
|
|
83
|
|
|
return intval($value); |
|
84
|
|
|
else |
|
85
|
|
|
return $default; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
function getFloat($key, $default = 0) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$value = self::get($key); |
|
|
|
|
|
|
91
|
|
|
if (strlen($value) > 0) |
|
|
|
|
|
|
92
|
|
|
return floatval($value); |
|
93
|
|
|
else |
|
94
|
|
|
return $default; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
function getCoordinate($key, $default = 0) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$v = self::get($key); |
|
|
|
|
|
|
100
|
|
|
if (strlen($v) > 0 && \WideImage\Coordinate::parse($v) !== null) |
|
|
|
|
|
|
101
|
|
|
return self::get($key); |
|
102
|
|
|
else |
|
103
|
|
|
return $default; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
function getOption($key, $valid = array(), $default = null) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$value = self::get($key); |
|
|
|
|
|
|
109
|
|
|
if ($value !== null && in_array($value, $valid)) |
|
110
|
|
|
return strval($value); |
|
111
|
|
|
else |
|
112
|
|
|
return $default; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
function getColor($key, $default = '000000') |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$value = self::get($key); |
|
|
|
|
|
|
118
|
|
|
if (substr($value, 0, 1) == '#') |
|
|
|
|
|
|
119
|
|
|
$value = substr($value, 1); |
|
120
|
|
|
|
|
121
|
|
|
if ($value === '' || preg_match('~^[0-9a-f]{1,6}$~i', $value)) |
|
122
|
|
|
return $value; |
|
123
|
|
|
else |
|
124
|
|
|
return $default; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
function getRegex($key, $regex, $default = null) |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$value = self::get($key); |
|
|
|
|
|
|
130
|
|
|
if ($value !== null && preg_match($regex, $value)) |
|
131
|
|
|
return $value; |
|
132
|
|
|
else |
|
133
|
|
|
return $default; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.