| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * PHP versions 5 |
|---|
| 4 | * |
|---|
| 5 | * phTagr : Tag, Browse, and Share Your Photos. |
|---|
| 6 | * Copyright 2006-2012, Sebastian Felis (sebastian@phtagr.org) |
|---|
| 7 | * |
|---|
| 8 | * Licensed under The GPL-2.0 License |
|---|
| 9 | * Redistributions of files must retain the above copyright notice. |
|---|
| 10 | * |
|---|
| 11 | * @copyright Copyright 2006-2012, Sebastian Felis (sebastian@phtagr.org) |
|---|
| 12 | * @link http://www.phtagr.org phTagr |
|---|
| 13 | * @package Phtagr |
|---|
| 14 | * @since phTagr 2.2b3 |
|---|
| 15 | * @license GPL-2.0 (http://www.opensource.org/licenses/GPL-2.0) |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | class Search extends Object |
|---|
| 19 | { |
|---|
| 20 | |
|---|
| 21 | var $_data; |
|---|
| 22 | |
|---|
| 23 | /** List of chars to escape on setParam() */ |
|---|
| 24 | var $escapeChars = '=,/'; |
|---|
| 25 | |
|---|
| 26 | /** Clears all parameters and set them to the defaults*/ |
|---|
| 27 | function clear() { |
|---|
| 28 | $this->_data = array(); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | /** Returns all parameters |
|---|
| 32 | @return Parameter array */ |
|---|
| 33 | function getParams() { |
|---|
| 34 | return $this->_data; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | /** Set all parameters |
|---|
| 38 | @param data Parameter array |
|---|
| 39 | @note The parameters are not validated! */ |
|---|
| 40 | function setParams($data = array()) { |
|---|
| 41 | $this->_data = $data; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** Validate parameter value. |
|---|
| 45 | @note Overwritten by inherited classes */ |
|---|
| 46 | function validate($name, $value) { |
|---|
| 47 | return true; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** Returns parameter |
|---|
| 51 | @param name Name of parameter |
|---|
| 52 | @param default Default value, if the parameter does not exists. Default |
|---|
| 53 | value is null */ |
|---|
| 54 | function getParam($name, $default = null) { |
|---|
| 55 | if (!empty($this->_data[$name])) { |
|---|
| 56 | return $this->_data[$name]; |
|---|
| 57 | } else { |
|---|
| 58 | return $default; |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** Set a singular parameter |
|---|
| 63 | @param name Parameter name |
|---|
| 64 | @param value Parameter value |
|---|
| 65 | @param validate Optional parameter to validate the parameter. Default is |
|---|
| 66 | true |
|---|
| 67 | @return True on success */ |
|---|
| 68 | function setParam($name, $value, $validate = true) { |
|---|
| 69 | if ($validate === false || $this->validate($name, $value)) { |
|---|
| 70 | $this->_data[$name] = $value; |
|---|
| 71 | return true; |
|---|
| 72 | } else { |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** Add a parameter to an array. |
|---|
| 78 | @param name Parameter name. |
|---|
| 79 | @param value Parameter value (which will be pluralized) |
|---|
| 80 | @param validate Optional parameter to validate the parameter. Default is |
|---|
| 81 | true |
|---|
| 82 | @note The name will be pluralized. */ |
|---|
| 83 | function addParam($name, $value, $validate = true) { |
|---|
| 84 | $name = Inflector::pluralize($name); |
|---|
| 85 | if (is_array($value)) { |
|---|
| 86 | foreach ($value as $v) { |
|---|
| 87 | $this->addParam($name, $v, $validate); |
|---|
| 88 | } |
|---|
| 89 | return; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | if ((!isset($this->_data[$name]) || !in_array($value, $this->_data[$name])) && |
|---|
| 93 | ($validate === false || $this->validate($name, $value))) { |
|---|
| 94 | $this->_data[$name][] = $value; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | function delParam($name, $value = false) { |
|---|
| 99 | if (!isset($this->_data[$name])) { |
|---|
| 100 | return; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | if (!empty($value)) { |
|---|
| 104 | if (is_array($value)) { |
|---|
| 105 | foreach ($value as $v) { |
|---|
| 106 | $this->delParam($name, $v); |
|---|
| 107 | } |
|---|
| 108 | return; |
|---|
| 109 | } |
|---|
| 110 | // handle array |
|---|
| 111 | $key = array_search($value, $this->_data[$name]); |
|---|
| 112 | if ($key !== false) { |
|---|
| 113 | unset($this->_data[$name][$key]); |
|---|
| 114 | } |
|---|
| 115 | if (count($this->_data[$name]) == 0) { |
|---|
| 116 | unset($this->_data[$name]); |
|---|
| 117 | } |
|---|
| 118 | } else { |
|---|
| 119 | // handle single value |
|---|
| 120 | unset($this->_data[$name]); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | function __call($name, $args) { |
|---|
| 125 | if (!preg_match('/^(get|set|add|del|delete)(.*)$/', $name, $matches)) { |
|---|
| 126 | $this->log("Search: Undefined function $name"); |
|---|
| 127 | return; |
|---|
| 128 | } |
|---|
| 129 | $name = Inflector::underscore($matches[2]); |
|---|
| 130 | switch ($matches[1]) { |
|---|
| 131 | case 'get': |
|---|
| 132 | if (count($args) > 0) { |
|---|
| 133 | return $this->getParam($name, $args[0]); |
|---|
| 134 | } else { |
|---|
| 135 | return $this->getParam($name); |
|---|
| 136 | } |
|---|
| 137 | break; |
|---|
| 138 | case 'set': |
|---|
| 139 | if (count($args) == 1) { |
|---|
| 140 | return $this->setParam($name, $args[0]); |
|---|
| 141 | } elseif (count($args) == 2) { |
|---|
| 142 | return $this->setParam($name, $args[0], $args[1]); |
|---|
| 143 | } |
|---|
| 144 | break; |
|---|
| 145 | case 'add': |
|---|
| 146 | if (count($args) == 1) { |
|---|
| 147 | return $this->addParam($name, $args[0]); |
|---|
| 148 | } elseif (count($args) == 2) { |
|---|
| 149 | return $this->addParam($name, $args[0], $args[1]); |
|---|
| 150 | } |
|---|
| 151 | break; |
|---|
| 152 | case 'del': |
|---|
| 153 | case 'delete': |
|---|
| 154 | if (count($args) == 1) { |
|---|
| 155 | if (!isset($this->_data[$name])) { |
|---|
| 156 | $plural = Inflector::pluralize($name); |
|---|
| 157 | if (isset($this->_data[$plural])) { |
|---|
| 158 | $name = $plural; |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | $this->delParam($name, $args[0]); |
|---|
| 162 | } else { |
|---|
| 163 | $this->delParam($name); |
|---|
| 164 | } |
|---|
| 165 | break; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | function encode($input) { |
|---|
| 170 | $out = ''; |
|---|
| 171 | $input = (string)$input; |
|---|
| 172 | $len = strlen($input); |
|---|
| 173 | for ($i = 0; $i < $len; $i++) { |
|---|
| 174 | $c = substr($input, $i, 1); |
|---|
| 175 | if (strpos($this->escapeChars, $c) !== false) { |
|---|
| 176 | $c = '=' . dechex(ord($c)); |
|---|
| 177 | } |
|---|
| 178 | $out = $out . $c; |
|---|
| 179 | } |
|---|
| 180 | return $out; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | function _c2h($c) { |
|---|
| 184 | $d = ord($c); |
|---|
| 185 | if ($d >= 48 && $d <= 57) { |
|---|
| 186 | return $d - 48; |
|---|
| 187 | } elseif ($d >= 65 && $d <= 70) { |
|---|
| 188 | return $d - 55; |
|---|
| 189 | } elseif ($d >= 97 && $d <= 102) { |
|---|
| 190 | return $d - 87; |
|---|
| 191 | } else { |
|---|
| 192 | return false; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | function _dechex($c1, $c2) { |
|---|
| 197 | $d1 = $this->_c2h($c1); |
|---|
| 198 | $d2 = $this->_c2h($c2); |
|---|
| 199 | if ($d1 === false || $d2 === false) { |
|---|
| 200 | return false; |
|---|
| 201 | } else { |
|---|
| 202 | return chr($d1 * 16 + $d2); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | function decode($input) { |
|---|
| 207 | $out = ''; |
|---|
| 208 | $input = (string)$input; |
|---|
| 209 | $len = strlen($input); |
|---|
| 210 | for ($i = 0; $i < $len; $i++) { |
|---|
| 211 | $c = substr($input, $i, 1); |
|---|
| 212 | if ($c == '=') { |
|---|
| 213 | if ($i + 2 >= $len) { |
|---|
| 214 | break; |
|---|
| 215 | } |
|---|
| 216 | $c1 = substr($input, $i + 1, 1); |
|---|
| 217 | $c2 = substr($input, $i + 2, 1); |
|---|
| 218 | $c = $this->_dechex($c1, $c2); |
|---|
| 219 | if ($c !== false) { |
|---|
| 220 | $out .= $c; |
|---|
| 221 | } |
|---|
| 222 | $i += 2; |
|---|
| 223 | } else { |
|---|
| 224 | $out .= $c; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | return $out; |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | ?> |
|---|