@@ -8,18 +8,18 @@ |
||
8 | 8 | class HTMLPurifier_VarParser_Native extends HTMLPurifier_VarParser |
9 | 9 | { |
10 | 10 | |
11 | - protected function parseImplementation($var, $type, $allow_null) { |
|
12 | - return $this->evalExpression($var); |
|
13 | - } |
|
11 | + protected function parseImplementation($var, $type, $allow_null) { |
|
12 | + return $this->evalExpression($var); |
|
13 | + } |
|
14 | 14 | |
15 | - protected function evalExpression($expr) { |
|
16 | - $var = null; |
|
17 | - $result = eval("\$var = $expr;"); |
|
18 | - if ($result === false) { |
|
19 | - throw new HTMLPurifier_VarParserException("Fatal error in evaluated code"); |
|
20 | - } |
|
21 | - return $var; |
|
22 | - } |
|
15 | + protected function evalExpression($expr) { |
|
16 | + $var = null; |
|
17 | + $result = eval("\$var = $expr;"); |
|
18 | + if ($result === false) { |
|
19 | + throw new HTMLPurifier_VarParserException("Fatal error in evaluated code"); |
|
20 | + } |
|
21 | + return $var; |
|
22 | + } |
|
23 | 23 | |
24 | 24 | } |
25 | 25 |
@@ -60,8 +60,7 @@ |
||
60 | 60 | { |
61 | 61 | $body .= $this->_makexml($arrNode); |
62 | 62 | } |
63 | - } |
|
64 | - else if(is_object($value)) |
|
63 | + } else if(is_object($value)) |
|
65 | 64 | { |
66 | 65 | $body = $this->_makexml($value); |
67 | 66 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | { |
20 | 20 | $buff = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; |
21 | 21 | |
22 | - foreach($xml as $nodeName => $nodeItem) |
|
22 | + foreach ($xml as $nodeName => $nodeItem) |
|
23 | 23 | { |
24 | 24 | $buff .= $this->_makexml($nodeItem); |
25 | 25 | } |
@@ -34,16 +34,16 @@ discard block |
||
34 | 34 | function _makexml($node) |
35 | 35 | { |
36 | 36 | $body = ''; |
37 | - foreach($node as $key => $value) |
|
37 | + foreach ($node as $key => $value) |
|
38 | 38 | { |
39 | - switch($key) |
|
39 | + switch ($key) |
|
40 | 40 | { |
41 | 41 | case 'node_name' : break; |
42 | 42 | case 'attrs' : { |
43 | 43 | $attrs = ''; |
44 | - if(isset($value)) |
|
44 | + if (isset($value)) |
|
45 | 45 | { |
46 | - foreach($value as $attrName => $attrValue) |
|
46 | + foreach ($value as $attrName => $attrValue) |
|
47 | 47 | { |
48 | 48 | $attrs .= sprintf(' %s="%s"', $attrName, htmlspecialchars($attrValue, ENT_COMPAT | ENT_HTML401, 'UTF-8', false)); |
49 | 49 | } |
@@ -54,21 +54,21 @@ discard block |
||
54 | 54 | $body = $value; |
55 | 55 | break; |
56 | 56 | default : { |
57 | - if(is_array($value)) |
|
57 | + if (is_array($value)) |
|
58 | 58 | { |
59 | - foreach($value as $idx => $arrNode) |
|
59 | + foreach ($value as $idx => $arrNode) |
|
60 | 60 | { |
61 | 61 | $body .= $this->_makexml($arrNode); |
62 | 62 | } |
63 | 63 | } |
64 | - else if(is_object($value)) |
|
64 | + else if (is_object($value)) |
|
65 | 65 | { |
66 | 66 | $body = $this->_makexml($value); |
67 | 67 | } |
68 | 68 | } |
69 | 69 | } |
70 | 70 | } |
71 | - return sprintf('<%s%s>%s</%s>' . "\n", $node->node_name, $attrs, $body, $node->node_name); |
|
71 | + return sprintf('<%s%s>%s</%s>'."\n", $node->node_name, $attrs, $body, $node->node_name); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | } |
@@ -102,8 +102,7 @@ |
||
102 | 102 | if(!file_exists($this->js_file)) |
103 | 103 | { |
104 | 104 | $this->_compile(); |
105 | - } |
|
106 | - else if(filemtime($this->xml_file) > filemtime($this->js_file)) |
|
105 | + } else if(filemtime($this->xml_file) > filemtime($this->js_file)) |
|
107 | 106 | { |
108 | 107 | $this->_compile(); |
109 | 108 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | |
82 | 82 | function __construct($path, $xml_file) |
83 | 83 | { |
84 | - if(substr($path, -1) !== '/') |
|
84 | + if (substr($path, -1) !== '/') |
|
85 | 85 | { |
86 | 86 | $path .= '/'; |
87 | 87 | } |
@@ -95,15 +95,15 @@ discard block |
||
95 | 95 | */ |
96 | 96 | function compile() |
97 | 97 | { |
98 | - if(!file_exists($this->xml_file)) |
|
98 | + if (!file_exists($this->xml_file)) |
|
99 | 99 | { |
100 | 100 | return; |
101 | 101 | } |
102 | - if(!file_exists($this->js_file)) |
|
102 | + if (!file_exists($this->js_file)) |
|
103 | 103 | { |
104 | 104 | $this->_compile(); |
105 | 105 | } |
106 | - else if(filemtime($this->xml_file) > filemtime($this->js_file)) |
|
106 | + else if (filemtime($this->xml_file) > filemtime($this->js_file)) |
|
107 | 107 | { |
108 | 108 | $this->_compile(); |
109 | 109 | } |
@@ -136,25 +136,25 @@ discard block |
||
136 | 136 | |
137 | 137 | |
138 | 138 | $field_node = $xml_obj->filter->form->node; |
139 | - if($field_node && !is_array($field_node)) |
|
139 | + if ($field_node && !is_array($field_node)) |
|
140 | 140 | { |
141 | 141 | $field_node = array($field_node); |
142 | 142 | } |
143 | 143 | |
144 | 144 | $parameter_param = $xml_obj->filter->parameter->param; |
145 | - if($parameter_param && !is_array($parameter_param)) |
|
145 | + if ($parameter_param && !is_array($parameter_param)) |
|
146 | 146 | { |
147 | 147 | $parameter_param = array($parameter_param); |
148 | 148 | } |
149 | 149 | |
150 | 150 | $response_tag = $xml_obj->filter->response->tag; |
151 | - if($response_tag && !is_array($response_tag)) |
|
151 | + if ($response_tag && !is_array($response_tag)) |
|
152 | 152 | { |
153 | 153 | $response_tag = array($response_tag); |
154 | 154 | } |
155 | 155 | |
156 | 156 | // If extend_filter exists, result returned by calling the method |
157 | - if($extend_filter) |
|
157 | + if ($extend_filter) |
|
158 | 158 | { |
159 | 159 | // If extend_filter exists, it changes the name of cache not to use cache |
160 | 160 | $this->js_file .= '.nocache.js'; |
@@ -163,24 +163,24 @@ discard block |
||
163 | 163 | list($module_name, $method) = explode('.', $extend_filter); |
164 | 164 | |
165 | 165 | // contibue if both module_name and methos exist. |
166 | - if($module_name && $method) |
|
166 | + if ($module_name && $method) |
|
167 | 167 | { |
168 | 168 | // get model object of the module |
169 | 169 | $oExtendFilter = getModel($module_name); |
170 | 170 | |
171 | 171 | // execute if method exists |
172 | - if(method_exists($oExtendFilter, $method)) |
|
172 | + if (method_exists($oExtendFilter, $method)) |
|
173 | 173 | { |
174 | 174 | // get the result |
175 | 175 | $extend_filter_list = $oExtendFilter->{$method}(TRUE); |
176 | 176 | $extend_filter_count = count($extend_filter_list); |
177 | 177 | |
178 | 178 | // apply lang_value from the result to the variable |
179 | - for($i = 0; $i < $extend_filter_count; $i++) |
|
179 | + for ($i = 0; $i < $extend_filter_count; $i++) |
|
180 | 180 | { |
181 | 181 | $name = $extend_filter_list[$i]->name; |
182 | 182 | $lang_value = $extend_filter_list[$i]->lang; |
183 | - if($lang_value) |
|
183 | + if ($lang_value) |
|
184 | 184 | { |
185 | 185 | $lang->{$name} = $lang_value; |
186 | 186 | } |
@@ -200,15 +200,15 @@ discard block |
||
200 | 200 | $fields = array(); |
201 | 201 | |
202 | 202 | // create custom rule |
203 | - if($rules && $rules->rule) |
|
203 | + if ($rules && $rules->rule) |
|
204 | 204 | { |
205 | - if(!is_array($rules->rule)) |
|
205 | + if (!is_array($rules->rule)) |
|
206 | 206 | { |
207 | 207 | $rules->rule = array($rules->rule); |
208 | 208 | } |
209 | - foreach($rules->rule as $r) |
|
209 | + foreach ($rules->rule as $r) |
|
210 | 210 | { |
211 | - if($r->attrs->type == 'regex') |
|
211 | + if ($r->attrs->type == 'regex') |
|
212 | 212 | { |
213 | 213 | $js_rules[] = "v.cast('ADD_RULE', ['{$r->attrs->name}', {$r->body}]);"; |
214 | 214 | } |
@@ -217,14 +217,14 @@ discard block |
||
217 | 217 | |
218 | 218 | // generates a field, which is a script of the checked item |
219 | 219 | $node_count = count($field_node); |
220 | - if($node_count) |
|
220 | + if ($node_count) |
|
221 | 221 | { |
222 | - foreach($field_node as $key => $node) |
|
222 | + foreach ($field_node as $key => $node) |
|
223 | 223 | { |
224 | 224 | $attrs = $node->attrs; |
225 | 225 | $target = trim($attrs->target); |
226 | 226 | |
227 | - if(!$target) |
|
227 | + if (!$target) |
|
228 | 228 | { |
229 | 229 | continue; |
230 | 230 | } |
@@ -234,34 +234,34 @@ discard block |
||
234 | 234 | |
235 | 235 | $field = array(); |
236 | 236 | |
237 | - if($attrs->required == 'true') |
|
237 | + if ($attrs->required == 'true') |
|
238 | 238 | { |
239 | 239 | $field[] = 'required:true'; |
240 | 240 | } |
241 | - if($attrs->minlength > 0) |
|
241 | + if ($attrs->minlength > 0) |
|
242 | 242 | { |
243 | - $field[] = 'minlength:' . $attrs->minlength; |
|
243 | + $field[] = 'minlength:'.$attrs->minlength; |
|
244 | 244 | } |
245 | - if($attrs->maxlength > 0) |
|
245 | + if ($attrs->maxlength > 0) |
|
246 | 246 | { |
247 | - $field[] = 'maxlength:' . $attrs->maxlength; |
|
247 | + $field[] = 'maxlength:'.$attrs->maxlength; |
|
248 | 248 | } |
249 | - if($equalto) |
|
249 | + if ($equalto) |
|
250 | 250 | { |
251 | 251 | $field[] = "equalto:'{$attrs->equalto}'"; |
252 | 252 | } |
253 | - if($rule) |
|
253 | + if ($rule) |
|
254 | 254 | { |
255 | 255 | $field[] = "rule:'{$rule}'"; |
256 | 256 | } |
257 | 257 | |
258 | - $fields[] = "'{$target}': {" . implode(',', $field) . "}"; |
|
258 | + $fields[] = "'{$target}': {".implode(',', $field)."}"; |
|
259 | 259 | |
260 | - if(!in_array($target, $target_list)) |
|
260 | + if (!in_array($target, $target_list)) |
|
261 | 261 | { |
262 | 262 | $target_list[] = $target; |
263 | 263 | } |
264 | - if(!$target_type_list[$target]) |
|
264 | + if (!$target_type_list[$target]) |
|
265 | 265 | { |
266 | 266 | $target_type_list[$target] = $filter; |
267 | 267 | } |
@@ -271,12 +271,12 @@ discard block |
||
271 | 271 | // Check extend_filter_item |
272 | 272 | $rule_types = array('homepage' => 'homepage', 'email_address' => 'email'); |
273 | 273 | |
274 | - for($i = 0; $i < $extend_filter_count; $i++) |
|
274 | + for ($i = 0; $i < $extend_filter_count; $i++) |
|
275 | 275 | { |
276 | 276 | $filter_item = $extend_filter_list[$i]; |
277 | 277 | $target = trim($filter_item->name); |
278 | 278 | |
279 | - if(!$target) |
|
279 | + if (!$target) |
|
280 | 280 | { |
281 | 281 | continue; |
282 | 282 | } |
@@ -287,21 +287,21 @@ discard block |
||
287 | 287 | $required = ($filter_item->required == 'true'); |
288 | 288 | |
289 | 289 | $field = array(); |
290 | - if($required) |
|
290 | + if ($required) |
|
291 | 291 | { |
292 | 292 | $field[] = 'required:true'; |
293 | 293 | } |
294 | - if($rule) |
|
294 | + if ($rule) |
|
295 | 295 | { |
296 | 296 | $field[] = "rule:'{$rule}'"; |
297 | 297 | } |
298 | - $fields[] = "\t\t'{$target}' : {" . implode(',', $field) . "}"; |
|
298 | + $fields[] = "\t\t'{$target}' : {".implode(',', $field)."}"; |
|
299 | 299 | |
300 | - if(!in_array($target, $target_list)) |
|
300 | + if (!in_array($target, $target_list)) |
|
301 | 301 | { |
302 | 302 | $target_list[] = $target; |
303 | 303 | } |
304 | - if(!$target_type_list[$target]) |
|
304 | + if (!$target_type_list[$target]) |
|
305 | 305 | { |
306 | 306 | $target_type_list[$target] = $type; |
307 | 307 | } |
@@ -310,37 +310,37 @@ discard block |
||
310 | 310 | // generates parameter script to create dbata |
311 | 311 | $rename_params = array(); |
312 | 312 | $parameter_count = count($parameter_param); |
313 | - if($parameter_count) |
|
313 | + if ($parameter_count) |
|
314 | 314 | { |
315 | 315 | // contains parameter of the default filter contents |
316 | - foreach($parameter_param as $key => $param) |
|
316 | + foreach ($parameter_param as $key => $param) |
|
317 | 317 | { |
318 | 318 | $attrs = $param->attrs; |
319 | 319 | $name = trim($attrs->name); |
320 | 320 | $target = trim($attrs->target); |
321 | 321 | |
322 | 322 | //if($name && $target && ($name != $target)) $js_doc[] = "\t\tparams['{$name}'] = params['{$target}']; delete params['{$target}'];"; |
323 | - if($name && $target && ($name != $target)) |
|
323 | + if ($name && $target && ($name != $target)) |
|
324 | 324 | { |
325 | 325 | $rename_params[] = "'{$target}':'{$name}'"; |
326 | 326 | } |
327 | - if($name && !in_array($name, $target_list)) |
|
327 | + if ($name && !in_array($name, $target_list)) |
|
328 | 328 | { |
329 | 329 | $target_list[] = $name; |
330 | 330 | } |
331 | 331 | } |
332 | 332 | |
333 | 333 | // Check extend_filter_item |
334 | - for($i = 0; $i < $extend_filter_count; $i++) |
|
334 | + for ($i = 0; $i < $extend_filter_count; $i++) |
|
335 | 335 | { |
336 | 336 | $filter_item = $extend_filter_list[$i]; |
337 | 337 | $target = $name = trim($filter_item->name); |
338 | - if(!$name || !$target) |
|
338 | + if (!$name || !$target) |
|
339 | 339 | { |
340 | 340 | continue; |
341 | 341 | } |
342 | 342 | |
343 | - if(!in_array($name, $target_list)) |
|
343 | + if (!in_array($name, $target_list)) |
|
344 | 344 | { |
345 | 345 | $target_list[] = $name; |
346 | 346 | } |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | // generates the response script |
351 | 351 | $response_count = count($response_tag); |
352 | 352 | $responses = array(); |
353 | - for($i = 0; $i < $response_count; $i++) |
|
353 | + for ($i = 0; $i < $response_count; $i++) |
|
354 | 354 | { |
355 | 355 | $attrs = $response_tag[$i]->attrs; |
356 | 356 | $name = $attrs->name; |
@@ -359,10 +359,10 @@ discard block |
||
359 | 359 | |
360 | 360 | // writes lang values of the form field |
361 | 361 | $target_count = count($target_list); |
362 | - for($i = 0; $i < $target_count; $i++) |
|
362 | + for ($i = 0; $i < $target_count; $i++) |
|
363 | 363 | { |
364 | 364 | $target = $target_list[$i]; |
365 | - if(!$lang->{$target}) |
|
365 | + if (!$lang->{$target}) |
|
366 | 366 | { |
367 | 367 | $lang->{$target} = $target; |
368 | 368 | } |
@@ -381,9 +381,9 @@ discard block |
||
381 | 381 | */ |
382 | 382 | |
383 | 383 | // writes error messages |
384 | - foreach($lang->filter as $key => $val) |
|
384 | + foreach ($lang->filter as $key => $val) |
|
385 | 385 | { |
386 | - if(!$val) |
|
386 | + if (!$val) |
|
387 | 387 | { |
388 | 388 | $val = $key; |
389 | 389 | } |
@@ -392,24 +392,24 @@ discard block |
||
392 | 392 | } |
393 | 393 | |
394 | 394 | $callback_func = $xml_obj->filter->response->attrs->callback_func; |
395 | - if(!$callback_func) |
|
395 | + if (!$callback_func) |
|
396 | 396 | { |
397 | 397 | $callback_func = "filterAlertMessage"; |
398 | 398 | } |
399 | 399 | |
400 | 400 | $confirm_msg = ''; |
401 | - if($confirm_msg_code) |
|
401 | + if ($confirm_msg_code) |
|
402 | 402 | { |
403 | 403 | $confirm_msg = $lang->{$confirm_msg_code}; |
404 | 404 | } |
405 | 405 | |
406 | 406 | $jsdoc = array(); |
407 | - $jsdoc[] = "function {$filter_name}(form){ return legacy_filter('{$filter_name}', form, '{$module}', '{$act}', {$callback_func}, [" . implode(',', $responses) . "], '" . addslashes($confirm_msg) . "', {" . implode(',', $rename_params) . "}) };"; |
|
407 | + $jsdoc[] = "function {$filter_name}(form){ return legacy_filter('{$filter_name}', form, '{$module}', '{$act}', {$callback_func}, [".implode(',', $responses)."], '".addslashes($confirm_msg)."', {".implode(',', $rename_params)."}) };"; |
|
408 | 408 | $jsdoc[] = '(function($){'; |
409 | 409 | $jsdoc[] = "\tvar v=xe.getApp('validator')[0];if(!v)return false;"; |
410 | - $jsdoc[] = "\t" . 'v.cast("ADD_FILTER", ["' . $filter_name . '", {' . implode(',', $fields) . '}]);'; |
|
411 | - $jsdoc[] = "\t" . implode("\n\t", $js_rules); |
|
412 | - $jsdoc[] = "\t" . implode("\n\t", $js_messages); |
|
410 | + $jsdoc[] = "\t".'v.cast("ADD_FILTER", ["'.$filter_name.'", {'.implode(',', $fields).'}]);'; |
|
411 | + $jsdoc[] = "\t".implode("\n\t", $js_rules); |
|
412 | + $jsdoc[] = "\t".implode("\n\t", $js_messages); |
|
413 | 413 | $jsdoc[] = '})(jQuery);'; |
414 | 414 | $jsdoc = implode("\n", $jsdoc); |
415 | 415 | |
@@ -424,7 +424,7 @@ discard block |
||
424 | 424 | */ |
425 | 425 | function _getCompiledFileName($xml_file) |
426 | 426 | { |
427 | - return sprintf('%s%s.%s.compiled.js', $this->compiled_path, md5($this->version . $xml_file), Context::getLangType()); |
|
427 | + return sprintf('%s%s.%s.compiled.js', $this->compiled_path, md5($this->version.$xml_file), Context::getLangType()); |
|
428 | 428 | } |
429 | 429 | |
430 | 430 | } |
@@ -10,8 +10,7 @@ discard block |
||
10 | 10 | if(version_compare(PHP_VERSION, '5.4.0', '<')) |
11 | 11 | { |
12 | 12 | @error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING); |
13 | -} |
|
14 | -else |
|
13 | +} else |
|
15 | 14 | { |
16 | 15 | @error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING ^ E_STRICT); |
17 | 16 | } |
@@ -56,8 +55,7 @@ discard block |
||
56 | 55 | if(file_exists(_XE_PATH_ . 'config/package.inc.php')) |
57 | 56 | { |
58 | 57 | require _XE_PATH_ . 'config/package.inc.php'; |
59 | -} |
|
60 | -else |
|
58 | +} else |
|
61 | 59 | { |
62 | 60 | /** |
63 | 61 | * Package type |
@@ -398,12 +396,13 @@ discard block |
||
398 | 396 | if(isset($GLOBALS['__xe_autoload_file_map'][strtolower($class_name)])) |
399 | 397 | { |
400 | 398 | require _XE_PATH_ . $GLOBALS['__xe_autoload_file_map'][strtolower($class_name)]; |
401 | - } |
|
402 | - elseif(preg_match('/^([a-zA-Z0-9_]+?)(Admin)?(View|Controller|Model|Api|Wap|Mobile)?$/', $class_name, $matches)) |
|
399 | + } elseif(preg_match('/^([a-zA-Z0-9_]+?)(Admin)?(View|Controller|Model|Api|Wap|Mobile)?$/', $class_name, $matches)) |
|
403 | 400 | { |
404 | 401 | $candidate_filename = array(); |
405 | 402 | $candidate_filename[] = 'modules/' . $matches[1] . '/' . $matches[1]; |
406 | - if(isset($matches[2]) && $matches[2]) $candidate_filename[] = 'admin'; |
|
403 | + if(isset($matches[2]) && $matches[2]) { |
|
404 | + $candidate_filename[] = 'admin'; |
|
405 | + } |
|
407 | 406 | $candidate_filename[] = (isset($matches[3]) && $matches[3]) ? strtolower($matches[3]) : 'class'; |
408 | 407 | $candidate_filename[] = 'php'; |
409 | 408 |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | * @file config/config.inc.php |
8 | 8 | * @author XEHub ([email protected]) |
9 | 9 | */ |
10 | -if(version_compare(PHP_VERSION, '5.4.0', '<')) |
|
10 | +if (version_compare(PHP_VERSION, '5.4.0', '<')) |
|
11 | 11 | { |
12 | 12 | @error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING); |
13 | 13 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | @error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_WARNING ^ E_STRICT); |
17 | 17 | } |
18 | 18 | |
19 | -if(!defined('__XE__')) |
|
19 | +if (!defined('__XE__')) |
|
20 | 20 | { |
21 | 21 | exit(); |
22 | 22 | } |
@@ -52,9 +52,9 @@ discard block |
||
52 | 52 | // Set can use other method instead cookie to store session id(for file upload) |
53 | 53 | ini_set('session.use_only_cookies', 0); |
54 | 54 | |
55 | -if(file_exists(_XE_PATH_ . 'config/package.inc.php')) |
|
55 | +if (file_exists(_XE_PATH_.'config/package.inc.php')) |
|
56 | 56 | { |
57 | - require _XE_PATH_ . 'config/package.inc.php'; |
|
57 | + require _XE_PATH_.'config/package.inc.php'; |
|
58 | 58 | } |
59 | 59 | else |
60 | 60 | { |
@@ -96,12 +96,12 @@ discard block |
||
96 | 96 | * define('__ENABLE_PHPUNIT_TEST__', 0); |
97 | 97 | * define('__PROXY_SERVER__', 'http://domain:port/path'); |
98 | 98 | */ |
99 | -if(file_exists(_XE_PATH_ . 'config/config.user.inc.php')) |
|
99 | +if (file_exists(_XE_PATH_.'config/config.user.inc.php')) |
|
100 | 100 | { |
101 | - require _XE_PATH_ . 'config/config.user.inc.php'; |
|
101 | + require _XE_PATH_.'config/config.user.inc.php'; |
|
102 | 102 | } |
103 | 103 | |
104 | -if(!defined('__DEBUG__')) |
|
104 | +if (!defined('__DEBUG__')) |
|
105 | 105 | { |
106 | 106 | /** |
107 | 107 | * output debug message(bit value) |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | define('__DEBUG__', 0); |
117 | 117 | } |
118 | 118 | |
119 | -if(!defined('__DEBUG_OUTPUT__')) |
|
119 | +if (!defined('__DEBUG_OUTPUT__')) |
|
120 | 120 | { |
121 | 121 | /** |
122 | 122 | * output location of debug message |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | define('__DEBUG_OUTPUT__', 0); |
131 | 131 | } |
132 | 132 | |
133 | -if(!defined('__DEBUG_PROTECT__')) |
|
133 | +if (!defined('__DEBUG_PROTECT__')) |
|
134 | 134 | { |
135 | 135 | /** |
136 | 136 | * output comments of the firePHP console and browser |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | define('__DEBUG_PROTECT__', 1); |
144 | 144 | } |
145 | 145 | |
146 | -if(!defined('__DEBUG_PROTECT_IP__')) |
|
146 | +if (!defined('__DEBUG_PROTECT_IP__')) |
|
147 | 147 | { |
148 | 148 | /** |
149 | 149 | * Set a ip address to allow debug |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | define('__DEBUG_PROTECT_IP__', '127.0.0.1'); |
152 | 152 | } |
153 | 153 | |
154 | -if(!defined('__DEBUG_DB_OUTPUT__')) |
|
154 | +if (!defined('__DEBUG_DB_OUTPUT__')) |
|
155 | 155 | { |
156 | 156 | /** |
157 | 157 | * DB error message definition |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | define('__DEBUG_DB_OUTPUT__', 0); |
165 | 165 | } |
166 | 166 | |
167 | -if(!defined('__LOG_SLOW_QUERY__')) |
|
167 | +if (!defined('__LOG_SLOW_QUERY__')) |
|
168 | 168 | { |
169 | 169 | /** |
170 | 170 | * Query log for only timeout query among DB queries |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | define('__LOG_SLOW_QUERY__', 0); |
179 | 179 | } |
180 | 180 | |
181 | -if(!defined('__LOG_SLOW_TRIGGER__')) |
|
181 | +if (!defined('__LOG_SLOW_TRIGGER__')) |
|
182 | 182 | { |
183 | 183 | /** |
184 | 184 | * Trigger excute time log |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | define('__LOG_SLOW_TRIGGER__', 0); |
193 | 193 | } |
194 | 194 | |
195 | -if(!defined('__LOG_SLOW_ADDON__')) |
|
195 | +if (!defined('__LOG_SLOW_ADDON__')) |
|
196 | 196 | { |
197 | 197 | /** |
198 | 198 | * Addon excute time log |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | define('__LOG_SLOW_ADDON__', 0); |
207 | 207 | } |
208 | 208 | |
209 | -if(!defined('__LOG_SLOW_WIDGET__')) |
|
209 | +if (!defined('__LOG_SLOW_WIDGET__')) |
|
210 | 210 | { |
211 | 211 | /** |
212 | 212 | * Widget excute time log |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | define('__LOG_SLOW_WIDGET__', 0); |
221 | 221 | } |
222 | 222 | |
223 | -if(!defined('__DEBUG_QUERY__')) |
|
223 | +if (!defined('__DEBUG_QUERY__')) |
|
224 | 224 | { |
225 | 225 | /** |
226 | 226 | * Leave DB query information |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | define('__DEBUG_QUERY__', 0); |
234 | 234 | } |
235 | 235 | |
236 | -if(!defined('__OB_GZHANDLER_ENABLE__')) |
|
236 | +if (!defined('__OB_GZHANDLER_ENABLE__')) |
|
237 | 237 | { |
238 | 238 | /** |
239 | 239 | * option to enable/disable a compression feature using ob_gzhandler |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | define('__OB_GZHANDLER_ENABLE__', 1); |
248 | 248 | } |
249 | 249 | |
250 | -if(!defined('__ENABLE_PHPUNIT_TEST__')) |
|
250 | +if (!defined('__ENABLE_PHPUNIT_TEST__')) |
|
251 | 251 | { |
252 | 252 | /** |
253 | 253 | * decide to use/not use the php unit test (Path/tests/index.php) |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | define('__ENABLE_PHPUNIT_TEST__', 0); |
261 | 261 | } |
262 | 262 | |
263 | -if(!defined('__PROXY_SERVER__')) |
|
263 | +if (!defined('__PROXY_SERVER__')) |
|
264 | 264 | { |
265 | 265 | /** |
266 | 266 | * __PROXY_SERVER__ has server information to request to the external through the target server |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | define('__PROXY_SERVER__', NULL); |
270 | 270 | } |
271 | 271 | |
272 | -if(!defined('__ERROR_LOG__')) |
|
272 | +if (!defined('__ERROR_LOG__')) |
|
273 | 273 | { |
274 | 274 | /** |
275 | 275 | * __ERROR_LOG__ 는 PHP의 에러로그를 출력하는 기능입니다. 개발시 워닝에러이상의 에러부터 잡기 시작합니다. |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | define('__ERROR_LOG__', 0); |
281 | 281 | } |
282 | 282 | |
283 | -if(!defined('__DISABLE_DEFAULT_CSS__')) |
|
283 | +if (!defined('__DISABLE_DEFAULT_CSS__')) |
|
284 | 284 | { |
285 | 285 | /** |
286 | 286 | * XE의 기본 CSS 스타일을 로드하지 않도록 합니다. |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | define('__DISABLE_DEFAULT_CSS__', 0); |
296 | 296 | } |
297 | 297 | |
298 | -if(!defined('__AUTO_OPCACHE_INVALIDATE__')) |
|
298 | +if (!defined('__AUTO_OPCACHE_INVALIDATE__')) |
|
299 | 299 | { |
300 | 300 | /** |
301 | 301 | * 업데이트 시 주요 파일의 OPcache를 자동 초기화 옵션 |
@@ -310,13 +310,13 @@ discard block |
||
310 | 310 | } |
311 | 311 | |
312 | 312 | // Require specific files when using Firebug console output |
313 | -if((__DEBUG_OUTPUT__ == 2) && version_compare(PHP_VERSION, '6.0.0') === -1) |
|
313 | +if ((__DEBUG_OUTPUT__ == 2) && version_compare(PHP_VERSION, '6.0.0') === -1) |
|
314 | 314 | { |
315 | - require _XE_PATH_ . 'libs/FirePHPCore/FirePHP.class.php'; |
|
315 | + require _XE_PATH_.'libs/FirePHPCore/FirePHP.class.php'; |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | // Set Timezone as server time |
319 | -if(version_compare(PHP_VERSION, '5.3.0') >= 0) |
|
319 | +if (version_compare(PHP_VERSION, '5.3.0') >= 0) |
|
320 | 320 | { |
321 | 321 | date_default_timezone_set(@date_default_timezone_get()); |
322 | 322 | } |
@@ -422,8 +422,8 @@ discard block |
||
422 | 422 | * Invalidates a cached script of OPcache when version is changed. |
423 | 423 | * @see https://github.com/xpressengine/xe-core/issues/2189 |
424 | 424 | **/ |
425 | -$cache_path = _XE_PATH_ . 'files/cache/store/' . __XE_VERSION__; |
|
426 | -if( |
|
425 | +$cache_path = _XE_PATH_.'files/cache/store/'.__XE_VERSION__; |
|
426 | +if ( |
|
427 | 427 | __AUTO_OPCACHE_INVALIDATE__ === 1 |
428 | 428 | && !is_dir($cache_path) |
429 | 429 | && function_exists('opcache_get_status') |
@@ -433,62 +433,62 @@ discard block |
||
433 | 433 | @mkdir($cache_path, 0755, TRUE); |
434 | 434 | @chmod($cache_path, 0755); |
435 | 435 | |
436 | - foreach($GLOBALS['__xe_autoload_file_map'] as $script) { |
|
437 | - opcache_invalidate(_XE_PATH_ . $script, true); |
|
436 | + foreach ($GLOBALS['__xe_autoload_file_map'] as $script) { |
|
437 | + opcache_invalidate(_XE_PATH_.$script, true); |
|
438 | 438 | } |
439 | - opcache_invalidate(_XE_PATH_ . 'config/func.inc.php', true); |
|
439 | + opcache_invalidate(_XE_PATH_.'config/func.inc.php', true); |
|
440 | 440 | } |
441 | 441 | |
442 | 442 | // Require a function-defined-file for simple use |
443 | -require(_XE_PATH_ . 'config/func.inc.php'); |
|
443 | +require(_XE_PATH_.'config/func.inc.php'); |
|
444 | 444 | |
445 | -if(__DEBUG__) { |
|
445 | +if (__DEBUG__) { |
|
446 | 446 | define('__StartTime__', getMicroTime()); |
447 | 447 | } |
448 | 448 | |
449 | -if(__DEBUG__) { |
|
449 | +if (__DEBUG__) { |
|
450 | 450 | $GLOBALS['__elapsed_class_load__'] = 0; |
451 | 451 | } |
452 | 452 | |
453 | 453 | function __xe_autoload($class_name) |
454 | 454 | { |
455 | - if(__DEBUG__) { |
|
455 | + if (__DEBUG__) { |
|
456 | 456 | $time_at = getMicroTime(); |
457 | 457 | } |
458 | 458 | |
459 | - if(isset($GLOBALS['__xe_autoload_file_map'][strtolower($class_name)])) |
|
459 | + if (isset($GLOBALS['__xe_autoload_file_map'][strtolower($class_name)])) |
|
460 | 460 | { |
461 | - require _XE_PATH_ . $GLOBALS['__xe_autoload_file_map'][strtolower($class_name)]; |
|
461 | + require _XE_PATH_.$GLOBALS['__xe_autoload_file_map'][strtolower($class_name)]; |
|
462 | 462 | } |
463 | - elseif(preg_match('/^([a-zA-Z0-9_]+?)(Admin)?(View|Controller|Model|Api|Wap|Mobile)?$/', $class_name, $matches)) |
|
463 | + elseif (preg_match('/^([a-zA-Z0-9_]+?)(Admin)?(View|Controller|Model|Api|Wap|Mobile)?$/', $class_name, $matches)) |
|
464 | 464 | { |
465 | 465 | $candidate_filename = array(); |
466 | - $candidate_filename[] = 'modules/' . $matches[1] . '/' . $matches[1]; |
|
467 | - if(isset($matches[2]) && $matches[2]) $candidate_filename[] = 'admin'; |
|
466 | + $candidate_filename[] = 'modules/'.$matches[1].'/'.$matches[1]; |
|
467 | + if (isset($matches[2]) && $matches[2]) $candidate_filename[] = 'admin'; |
|
468 | 468 | $candidate_filename[] = (isset($matches[3]) && $matches[3]) ? strtolower($matches[3]) : 'class'; |
469 | 469 | $candidate_filename[] = 'php'; |
470 | 470 | |
471 | 471 | $candidate_filename = implode('.', $candidate_filename); |
472 | 472 | |
473 | - if(file_exists(_XE_PATH_ . $candidate_filename)) |
|
473 | + if (file_exists(_XE_PATH_.$candidate_filename)) |
|
474 | 474 | { |
475 | - require _XE_PATH_ . $candidate_filename; |
|
475 | + require _XE_PATH_.$candidate_filename; |
|
476 | 476 | } |
477 | 477 | } |
478 | 478 | |
479 | - if(__DEBUG__) { |
|
479 | + if (__DEBUG__) { |
|
480 | 480 | $GLOBALS['__elapsed_class_load__'] += getMicroTime() - $time_at; |
481 | 481 | } |
482 | 482 | } |
483 | 483 | spl_autoload_register('__xe_autoload'); |
484 | 484 | |
485 | -if(version_compare(PHP_VERSION, '7.2', '<')) |
|
485 | +if (version_compare(PHP_VERSION, '7.2', '<')) |
|
486 | 486 | { |
487 | 487 | class_alias('BaseObject', 'Object', true); |
488 | 488 | } |
489 | 489 | |
490 | -if(file_exists(_XE_PATH_ . '/vendor/autoload.php')) { |
|
491 | - require _XE_PATH_ . '/vendor/autoload.php'; |
|
490 | +if (file_exists(_XE_PATH_.'/vendor/autoload.php')) { |
|
491 | + require _XE_PATH_.'/vendor/autoload.php'; |
|
492 | 492 | } |
493 | 493 | /* End of file config.inc.php */ |
494 | 494 | /* Location: ./config/config.inc.php */ |
@@ -58,8 +58,7 @@ |
||
58 | 58 | { |
59 | 59 | $oModuleHandler->displayContent($oModuleHandler->procModule()); |
60 | 60 | } |
61 | - } |
|
62 | - catch(Exception $e) |
|
61 | + } catch(Exception $e) |
|
63 | 62 | { |
64 | 63 | htmlHeader(); |
65 | 64 | echo Context::getLang($e->getMessage()); |
@@ -32,11 +32,11 @@ discard block |
||
32 | 32 | /** |
33 | 33 | * @brief Declare constants for generic use and for checking to avoid a direct call from the Web |
34 | 34 | **/ |
35 | -define('__XE__', TRUE); |
|
35 | +define('__XE__', TRUE); |
|
36 | 36 | /** |
37 | 37 | * @brief Include the necessary configuration files |
38 | 38 | **/ |
39 | -require dirname(__FILE__) . '/config/config.inc.php'; |
|
39 | +require dirname(__FILE__).'/config/config.inc.php'; |
|
40 | 40 | |
41 | 41 | /** |
42 | 42 | * @brief Initialize by creating Context object |
@@ -48,18 +48,18 @@ discard block |
||
48 | 48 | /** |
49 | 49 | * @brief If default_url is set and it is different from the current url, attempt to redirect for SSO authentication and then process the module |
50 | 50 | **/ |
51 | -if($oContext->checkSSO()) |
|
51 | +if ($oContext->checkSSO()) |
|
52 | 52 | { |
53 | 53 | $oModuleHandler = new ModuleHandler(); |
54 | 54 | |
55 | 55 | try |
56 | 56 | { |
57 | - if($oModuleHandler->init()) |
|
57 | + if ($oModuleHandler->init()) |
|
58 | 58 | { |
59 | 59 | $oModuleHandler->displayContent($oModuleHandler->procModule()); |
60 | 60 | } |
61 | 61 | } |
62 | - catch(Exception $e) |
|
62 | + catch (Exception $e) |
|
63 | 63 | { |
64 | 64 | htmlHeader(); |
65 | 65 | echo Context::getLang($e->getMessage()); |
@@ -192,9 +192,9 @@ discard block |
||
192 | 192 | * @var array |
193 | 193 | */ |
194 | 194 | protected $options = array('maxObjectDepth' => 10, |
195 | - 'maxArrayDepth' => 20, |
|
196 | - 'useNativeJsonEncode' => true, |
|
197 | - 'includeLineNumbers' => true); |
|
195 | + 'maxArrayDepth' => 20, |
|
196 | + 'useNativeJsonEncode' => true, |
|
197 | + 'includeLineNumbers' => true); |
|
198 | 198 | |
199 | 199 | /** |
200 | 200 | * Filters used to exclude object members when encoding |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | * @return array |
230 | 230 | */ |
231 | 231 | public function __sleep() { |
232 | - return array('options','objectFilters','enabled'); |
|
232 | + return array('options','objectFilters','enabled'); |
|
233 | 233 | } |
234 | 234 | |
235 | 235 | /** |
@@ -239,10 +239,10 @@ discard block |
||
239 | 239 | * @return FirePHP |
240 | 240 | */ |
241 | 241 | public static function getInstance($AutoCreate=false) { |
242 | - if($AutoCreate===true && !self::$instance) { |
|
243 | - self::init(); |
|
244 | - } |
|
245 | - return self::$instance; |
|
242 | + if($AutoCreate===true && !self::$instance) { |
|
243 | + self::init(); |
|
244 | + } |
|
245 | + return self::$instance; |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | /** |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | * @return FirePHP |
252 | 252 | */ |
253 | 253 | public static function init() { |
254 | - return self::$instance = new self(); |
|
254 | + return self::$instance = new self(); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | /** |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | * @return void |
262 | 262 | */ |
263 | 263 | public function setEnabled($Enabled) { |
264 | - $this->enabled = $Enabled; |
|
264 | + $this->enabled = $Enabled; |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | /** |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | * @return boolean TRUE if enabled |
271 | 271 | */ |
272 | 272 | public function getEnabled() { |
273 | - return $this->enabled; |
|
273 | + return $this->enabled; |
|
274 | 274 | } |
275 | 275 | |
276 | 276 | /** |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | * @return void |
284 | 284 | */ |
285 | 285 | public function setObjectFilter($Class, $Filter) { |
286 | - $this->objectFilters[strtolower($Class)] = $Filter; |
|
286 | + $this->objectFilters[strtolower($Class)] = $Filter; |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | /** |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * @return void |
300 | 300 | */ |
301 | 301 | public function setOptions($Options) { |
302 | - $this->options = array_merge($this->options,$Options); |
|
302 | + $this->options = array_merge($this->options,$Options); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | /** |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | * @return array The currently set options |
309 | 309 | */ |
310 | 310 | public function getOptions() { |
311 | - return $this->options; |
|
311 | + return $this->options; |
|
312 | 312 | } |
313 | 313 | |
314 | 314 | /** |
@@ -320,14 +320,14 @@ discard block |
||
320 | 320 | */ |
321 | 321 | public function registerErrorHandler($throwErrorExceptions=true) |
322 | 322 | { |
323 | - //NOTE: The following errors will not be caught by this error handler: |
|
324 | - // E_ERROR, E_PARSE, E_CORE_ERROR, |
|
325 | - // E_CORE_WARNING, E_COMPILE_ERROR, |
|
326 | - // E_COMPILE_WARNING, E_STRICT |
|
323 | + //NOTE: The following errors will not be caught by this error handler: |
|
324 | + // E_ERROR, E_PARSE, E_CORE_ERROR, |
|
325 | + // E_CORE_WARNING, E_COMPILE_ERROR, |
|
326 | + // E_COMPILE_WARNING, E_STRICT |
|
327 | 327 | |
328 | - $this->throwErrorExceptions = $throwErrorExceptions; |
|
328 | + $this->throwErrorExceptions = $throwErrorExceptions; |
|
329 | 329 | |
330 | - return set_error_handler(array($this,'errorHandler')); |
|
330 | + return set_error_handler(array($this,'errorHandler')); |
|
331 | 331 | } |
332 | 332 | |
333 | 333 | /** |
@@ -343,20 +343,20 @@ discard block |
||
343 | 343 | */ |
344 | 344 | public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) |
345 | 345 | { |
346 | - // Don't throw exception if error reporting is switched off |
|
347 | - if (error_reporting() == 0) { |
|
348 | - return; |
|
349 | - } |
|
350 | - // Only throw exceptions for errors we are asking for |
|
351 | - if (error_reporting() & $errno) { |
|
352 | - |
|
353 | - $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline); |
|
354 | - if($this->throwErrorExceptions) { |
|
355 | - throw $exception; |
|
356 | - } else { |
|
357 | - $this->fb($exception); |
|
358 | - } |
|
359 | - } |
|
346 | + // Don't throw exception if error reporting is switched off |
|
347 | + if (error_reporting() == 0) { |
|
348 | + return; |
|
349 | + } |
|
350 | + // Only throw exceptions for errors we are asking for |
|
351 | + if (error_reporting() & $errno) { |
|
352 | + |
|
353 | + $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline); |
|
354 | + if($this->throwErrorExceptions) { |
|
355 | + throw $exception; |
|
356 | + } else { |
|
357 | + $this->fb($exception); |
|
358 | + } |
|
359 | + } |
|
360 | 360 | } |
361 | 361 | |
362 | 362 | /** |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | */ |
369 | 369 | public function registerExceptionHandler() |
370 | 370 | { |
371 | - return set_exception_handler(array($this,'exceptionHandler')); |
|
371 | + return set_exception_handler(array($this,'exceptionHandler')); |
|
372 | 372 | } |
373 | 373 | |
374 | 374 | /** |
@@ -381,13 +381,13 @@ discard block |
||
381 | 381 | */ |
382 | 382 | function exceptionHandler($Exception) { |
383 | 383 | |
384 | - $this->inExceptionHandler = true; |
|
384 | + $this->inExceptionHandler = true; |
|
385 | 385 | |
386 | - header('HTTP/1.1 500 Internal Server Error'); |
|
386 | + header('HTTP/1.1 500 Internal Server Error'); |
|
387 | 387 | |
388 | - $this->fb($Exception); |
|
388 | + $this->fb($Exception); |
|
389 | 389 | |
390 | - $this->inExceptionHandler = false; |
|
390 | + $this->inExceptionHandler = false; |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | /** |
@@ -399,14 +399,14 @@ discard block |
||
399 | 399 | */ |
400 | 400 | public function registerAssertionHandler($convertAssertionErrorsToExceptions=true, $throwAssertionExceptions=false) |
401 | 401 | { |
402 | - $this->convertAssertionErrorsToExceptions = $convertAssertionErrorsToExceptions; |
|
403 | - $this->throwAssertionExceptions = $throwAssertionExceptions; |
|
402 | + $this->convertAssertionErrorsToExceptions = $convertAssertionErrorsToExceptions; |
|
403 | + $this->throwAssertionExceptions = $throwAssertionExceptions; |
|
404 | 404 | |
405 | - if($throwAssertionExceptions && !$convertAssertionErrorsToExceptions) { |
|
406 | - throw $this->newException('Cannot throw assertion exceptions as assertion errors are not being converted to exceptions!'); |
|
407 | - } |
|
405 | + if($throwAssertionExceptions && !$convertAssertionErrorsToExceptions) { |
|
406 | + throw $this->newException('Cannot throw assertion exceptions as assertion errors are not being converted to exceptions!'); |
|
407 | + } |
|
408 | 408 | |
409 | - return assert_options(ASSERT_CALLBACK, array($this, 'assertionHandler')); |
|
409 | + return assert_options(ASSERT_CALLBACK, array($this, 'assertionHandler')); |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | /** |
@@ -421,21 +421,21 @@ discard block |
||
421 | 421 | public function assertionHandler($file, $line, $code) |
422 | 422 | { |
423 | 423 | |
424 | - if($this->convertAssertionErrorsToExceptions) { |
|
424 | + if($this->convertAssertionErrorsToExceptions) { |
|
425 | 425 | |
426 | - $exception = new ErrorException('Assertion Failed - Code[ '.$code.' ]', 0, null, $file, $line); |
|
426 | + $exception = new ErrorException('Assertion Failed - Code[ '.$code.' ]', 0, null, $file, $line); |
|
427 | 427 | |
428 | - if($this->throwAssertionExceptions) { |
|
429 | - throw $exception; |
|
430 | - } else { |
|
431 | - $this->fb($exception); |
|
432 | - } |
|
428 | + if($this->throwAssertionExceptions) { |
|
429 | + throw $exception; |
|
430 | + } else { |
|
431 | + $this->fb($exception); |
|
432 | + } |
|
433 | 433 | |
434 | - } else { |
|
434 | + } else { |
|
435 | 435 | |
436 | - $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File'=>$file,'Line'=>$line)); |
|
436 | + $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File'=>$file,'Line'=>$line)); |
|
437 | 437 | |
438 | - } |
|
438 | + } |
|
439 | 439 | } |
440 | 440 | |
441 | 441 | /** |
@@ -445,7 +445,7 @@ discard block |
||
445 | 445 | */ |
446 | 446 | public function setProcessorUrl($URL) |
447 | 447 | { |
448 | - $this->setHeader('X-FirePHP-ProcessorURL', $URL); |
|
448 | + $this->setHeader('X-FirePHP-ProcessorURL', $URL); |
|
449 | 449 | } |
450 | 450 | |
451 | 451 | /** |
@@ -455,7 +455,7 @@ discard block |
||
455 | 455 | */ |
456 | 456 | public function setRendererUrl($URL) |
457 | 457 | { |
458 | - $this->setHeader('X-FirePHP-RendererURL', $URL); |
|
458 | + $this->setHeader('X-FirePHP-RendererURL', $URL); |
|
459 | 459 | } |
460 | 460 | |
461 | 461 | /** |
@@ -472,20 +472,20 @@ discard block |
||
472 | 472 | */ |
473 | 473 | public function group($Name, $Options=null) { |
474 | 474 | |
475 | - if(!$Name) { |
|
476 | - throw $this->newException('You must specify a label for the group!'); |
|
477 | - } |
|
475 | + if(!$Name) { |
|
476 | + throw $this->newException('You must specify a label for the group!'); |
|
477 | + } |
|
478 | 478 | |
479 | - if($Options) { |
|
480 | - if(!is_array($Options)) { |
|
481 | - throw $this->newException('Options must be defined as an array!'); |
|
482 | - } |
|
483 | - if(array_key_exists('Collapsed', $Options)) { |
|
484 | - $Options['Collapsed'] = ($Options['Collapsed'])?'true':'false'; |
|
485 | - } |
|
486 | - } |
|
479 | + if($Options) { |
|
480 | + if(!is_array($Options)) { |
|
481 | + throw $this->newException('Options must be defined as an array!'); |
|
482 | + } |
|
483 | + if(array_key_exists('Collapsed', $Options)) { |
|
484 | + $Options['Collapsed'] = ($Options['Collapsed'])?'true':'false'; |
|
485 | + } |
|
486 | + } |
|
487 | 487 | |
488 | - return $this->fb(null, $Name, FirePHP::GROUP_START, $Options); |
|
488 | + return $this->fb(null, $Name, FirePHP::GROUP_START, $Options); |
|
489 | 489 | } |
490 | 490 | |
491 | 491 | /** |
@@ -495,7 +495,7 @@ discard block |
||
495 | 495 | * @throws Exception |
496 | 496 | */ |
497 | 497 | public function groupEnd() { |
498 | - return $this->fb(null, null, FirePHP::GROUP_END); |
|
498 | + return $this->fb(null, null, FirePHP::GROUP_END); |
|
499 | 499 | } |
500 | 500 | |
501 | 501 | /** |
@@ -508,7 +508,7 @@ discard block |
||
508 | 508 | * @throws Exception |
509 | 509 | */ |
510 | 510 | public function log($Object, $Label=null) { |
511 | - return $this->fb($Object, $Label, FirePHP::LOG); |
|
511 | + return $this->fb($Object, $Label, FirePHP::LOG); |
|
512 | 512 | } |
513 | 513 | |
514 | 514 | /** |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | * @throws Exception |
522 | 522 | */ |
523 | 523 | public function info($Object, $Label=null) { |
524 | - return $this->fb($Object, $Label, FirePHP::INFO); |
|
524 | + return $this->fb($Object, $Label, FirePHP::INFO); |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | /** |
@@ -534,7 +534,7 @@ discard block |
||
534 | 534 | * @throws Exception |
535 | 535 | */ |
536 | 536 | public function warn($Object, $Label=null) { |
537 | - return $this->fb($Object, $Label, FirePHP::WARN); |
|
537 | + return $this->fb($Object, $Label, FirePHP::WARN); |
|
538 | 538 | } |
539 | 539 | |
540 | 540 | /** |
@@ -547,7 +547,7 @@ discard block |
||
547 | 547 | * @throws Exception |
548 | 548 | */ |
549 | 549 | public function error($Object, $Label=null) { |
550 | - return $this->fb($Object, $Label, FirePHP::ERROR); |
|
550 | + return $this->fb($Object, $Label, FirePHP::ERROR); |
|
551 | 551 | } |
552 | 552 | |
553 | 553 | /** |
@@ -560,7 +560,7 @@ discard block |
||
560 | 560 | * @throws Exception |
561 | 561 | */ |
562 | 562 | public function dump($Key, $Variable) { |
563 | - return $this->fb($Variable, $Key, FirePHP::DUMP); |
|
563 | + return $this->fb($Variable, $Key, FirePHP::DUMP); |
|
564 | 564 | } |
565 | 565 | |
566 | 566 | /** |
@@ -572,7 +572,7 @@ discard block |
||
572 | 572 | * @throws Exception |
573 | 573 | */ |
574 | 574 | public function trace($Label) { |
575 | - return $this->fb($Label, FirePHP::TRACE); |
|
575 | + return $this->fb($Label, FirePHP::TRACE); |
|
576 | 576 | } |
577 | 577 | |
578 | 578 | /** |
@@ -585,7 +585,7 @@ discard block |
||
585 | 585 | * @throws Exception |
586 | 586 | */ |
587 | 587 | public function table($Label, $Table) { |
588 | - return $this->fb($Table, $Label, FirePHP::TABLE); |
|
588 | + return $this->fb($Table, $Label, FirePHP::TABLE); |
|
589 | 589 | } |
590 | 590 | |
591 | 591 | /** |
@@ -594,12 +594,12 @@ discard block |
||
594 | 594 | * @return boolean |
595 | 595 | */ |
596 | 596 | public function detectClientExtension() { |
597 | - /* Check if FirePHP is installed on client */ |
|
598 | - if(!@preg_match_all('/\sFirePHP\/([\.|\d]*)\s?/si',$this->getUserAgent(),$m) || |
|
599 | - !version_compare($m[1][0],'0.0.6','>=')) { |
|
600 | - return false; |
|
601 | - } |
|
602 | - return true; |
|
597 | + /* Check if FirePHP is installed on client */ |
|
598 | + if(!@preg_match_all('/\sFirePHP\/([\.|\d]*)\s?/si',$this->getUserAgent(),$m) || |
|
599 | + !version_compare($m[1][0],'0.0.6','>=')) { |
|
600 | + return false; |
|
601 | + } |
|
602 | + return true; |
|
603 | 603 | } |
604 | 604 | |
605 | 605 | /** |
@@ -612,265 +612,265 @@ discard block |
||
612 | 612 | */ |
613 | 613 | public function fb($Object) { |
614 | 614 | |
615 | - if(!$this->enabled) { |
|
616 | - return false; |
|
617 | - } |
|
615 | + if(!$this->enabled) { |
|
616 | + return false; |
|
617 | + } |
|
618 | 618 | |
619 | - if (headers_sent($filename, $linenum)) { |
|
620 | - // If we are logging from within the exception handler we cannot throw another exception |
|
621 | - if($this->inExceptionHandler) { |
|
622 | - // Simply echo the error out to the page |
|
623 | - echo '<div style="border: 2px solid red; font-family: Arial; font-size: 12px; background-color: lightgray; padding: 5px;"><span style="color: red; font-weight: bold;">FirePHP ERROR:</span> Headers already sent in <b>'.$filename.'</b> on line <b>'.$linenum.'</b>. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.</div>'; |
|
624 | - } else { |
|
625 | - throw $this->newException('Headers already sent in '.$filename.' on line '.$linenum.'. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.'); |
|
626 | - } |
|
627 | - } |
|
619 | + if (headers_sent($filename, $linenum)) { |
|
620 | + // If we are logging from within the exception handler we cannot throw another exception |
|
621 | + if($this->inExceptionHandler) { |
|
622 | + // Simply echo the error out to the page |
|
623 | + echo '<div style="border: 2px solid red; font-family: Arial; font-size: 12px; background-color: lightgray; padding: 5px;"><span style="color: red; font-weight: bold;">FirePHP ERROR:</span> Headers already sent in <b>'.$filename.'</b> on line <b>'.$linenum.'</b>. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.</div>'; |
|
624 | + } else { |
|
625 | + throw $this->newException('Headers already sent in '.$filename.' on line '.$linenum.'. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.'); |
|
626 | + } |
|
627 | + } |
|
628 | 628 | |
629 | - $Type = null; |
|
630 | - $Label = null; |
|
631 | - $Options = array(); |
|
629 | + $Type = null; |
|
630 | + $Label = null; |
|
631 | + $Options = array(); |
|
632 | 632 | |
633 | - if(func_num_args()==1) { |
|
634 | - } else |
|
635 | - if(func_num_args()==2) { |
|
636 | - switch(func_get_arg(1)) { |
|
637 | - case self::LOG: |
|
638 | - case self::INFO: |
|
639 | - case self::WARN: |
|
640 | - case self::ERROR: |
|
641 | - case self::DUMP: |
|
642 | - case self::TRACE: |
|
643 | - case self::EXCEPTION: |
|
644 | - case self::TABLE: |
|
645 | - case self::GROUP_START: |
|
646 | - case self::GROUP_END: |
|
647 | - $Type = func_get_arg(1); |
|
648 | - break; |
|
649 | - default: |
|
650 | - $Label = func_get_arg(1); |
|
651 | - break; |
|
652 | - } |
|
653 | - } else |
|
654 | - if(func_num_args()==3) { |
|
655 | - $Type = func_get_arg(2); |
|
656 | - $Label = func_get_arg(1); |
|
657 | - } else |
|
658 | - if(func_num_args()==4) { |
|
659 | - $Type = func_get_arg(2); |
|
660 | - $Label = func_get_arg(1); |
|
661 | - $Options = func_get_arg(3); |
|
662 | - } else { |
|
663 | - throw $this->newException('Wrong number of arguments to fb() function!'); |
|
664 | - } |
|
633 | + if(func_num_args()==1) { |
|
634 | + } else |
|
635 | + if(func_num_args()==2) { |
|
636 | + switch(func_get_arg(1)) { |
|
637 | + case self::LOG: |
|
638 | + case self::INFO: |
|
639 | + case self::WARN: |
|
640 | + case self::ERROR: |
|
641 | + case self::DUMP: |
|
642 | + case self::TRACE: |
|
643 | + case self::EXCEPTION: |
|
644 | + case self::TABLE: |
|
645 | + case self::GROUP_START: |
|
646 | + case self::GROUP_END: |
|
647 | + $Type = func_get_arg(1); |
|
648 | + break; |
|
649 | + default: |
|
650 | + $Label = func_get_arg(1); |
|
651 | + break; |
|
652 | + } |
|
653 | + } else |
|
654 | + if(func_num_args()==3) { |
|
655 | + $Type = func_get_arg(2); |
|
656 | + $Label = func_get_arg(1); |
|
657 | + } else |
|
658 | + if(func_num_args()==4) { |
|
659 | + $Type = func_get_arg(2); |
|
660 | + $Label = func_get_arg(1); |
|
661 | + $Options = func_get_arg(3); |
|
662 | + } else { |
|
663 | + throw $this->newException('Wrong number of arguments to fb() function!'); |
|
664 | + } |
|
665 | 665 | |
666 | 666 | |
667 | - if(!$this->detectClientExtension()) { |
|
668 | - return false; |
|
669 | - } |
|
667 | + if(!$this->detectClientExtension()) { |
|
668 | + return false; |
|
669 | + } |
|
670 | 670 | |
671 | - $meta = array(); |
|
672 | - $skipFinalObjectEncode = false; |
|
671 | + $meta = array(); |
|
672 | + $skipFinalObjectEncode = false; |
|
673 | 673 | |
674 | - if($Object instanceof Exception) { |
|
674 | + if($Object instanceof Exception) { |
|
675 | 675 | |
676 | - $meta['file'] = $this->_escapeTraceFile($Object->getFile()); |
|
677 | - $meta['line'] = $Object->getLine(); |
|
676 | + $meta['file'] = $this->_escapeTraceFile($Object->getFile()); |
|
677 | + $meta['line'] = $Object->getLine(); |
|
678 | 678 | |
679 | - $trace = $Object->getTrace(); |
|
680 | - if($Object instanceof ErrorException |
|
681 | - && isset($trace[0]['function']) |
|
682 | - && $trace[0]['function']=='errorHandler' |
|
683 | - && isset($trace[0]['class']) |
|
684 | - && $trace[0]['class']=='FirePHP') { |
|
679 | + $trace = $Object->getTrace(); |
|
680 | + if($Object instanceof ErrorException |
|
681 | + && isset($trace[0]['function']) |
|
682 | + && $trace[0]['function']=='errorHandler' |
|
683 | + && isset($trace[0]['class']) |
|
684 | + && $trace[0]['class']=='FirePHP') { |
|
685 | 685 | |
686 | - $severity = false; |
|
687 | - switch($Object->getSeverity()) { |
|
688 | - case E_WARNING: $severity = 'E_WARNING'; break; |
|
689 | - case E_NOTICE: $severity = 'E_NOTICE'; break; |
|
690 | - case E_USER_ERROR: $severity = 'E_USER_ERROR'; break; |
|
691 | - case E_USER_WARNING: $severity = 'E_USER_WARNING'; break; |
|
692 | - case E_USER_NOTICE: $severity = 'E_USER_NOTICE'; break; |
|
693 | - case E_STRICT: $severity = 'E_STRICT'; break; |
|
694 | - case E_RECOVERABLE_ERROR: $severity = 'E_RECOVERABLE_ERROR'; break; |
|
695 | - case E_DEPRECATED: $severity = 'E_DEPRECATED'; break; |
|
696 | - case E_USER_DEPRECATED: $severity = 'E_USER_DEPRECATED'; break; |
|
697 | - } |
|
686 | + $severity = false; |
|
687 | + switch($Object->getSeverity()) { |
|
688 | + case E_WARNING: $severity = 'E_WARNING'; break; |
|
689 | + case E_NOTICE: $severity = 'E_NOTICE'; break; |
|
690 | + case E_USER_ERROR: $severity = 'E_USER_ERROR'; break; |
|
691 | + case E_USER_WARNING: $severity = 'E_USER_WARNING'; break; |
|
692 | + case E_USER_NOTICE: $severity = 'E_USER_NOTICE'; break; |
|
693 | + case E_STRICT: $severity = 'E_STRICT'; break; |
|
694 | + case E_RECOVERABLE_ERROR: $severity = 'E_RECOVERABLE_ERROR'; break; |
|
695 | + case E_DEPRECATED: $severity = 'E_DEPRECATED'; break; |
|
696 | + case E_USER_DEPRECATED: $severity = 'E_USER_DEPRECATED'; break; |
|
697 | + } |
|
698 | 698 | |
699 | - $Object = array('Class'=>get_class($Object), |
|
700 | - 'Message'=>$severity.': '.$Object->getMessage(), |
|
701 | - 'File'=>$this->_escapeTraceFile($Object->getFile()), |
|
702 | - 'Line'=>$Object->getLine(), |
|
703 | - 'Type'=>'trigger', |
|
704 | - 'Trace'=>$this->_escapeTrace(array_splice($trace,2))); |
|
705 | - $skipFinalObjectEncode = true; |
|
706 | - } else { |
|
707 | - $Object = array('Class'=>get_class($Object), |
|
708 | - 'Message'=>$Object->getMessage(), |
|
709 | - 'File'=>$this->_escapeTraceFile($Object->getFile()), |
|
710 | - 'Line'=>$Object->getLine(), |
|
711 | - 'Type'=>'throw', |
|
712 | - 'Trace'=>$this->_escapeTrace($trace)); |
|
713 | - $skipFinalObjectEncode = true; |
|
714 | - } |
|
715 | - $Type = self::EXCEPTION; |
|
699 | + $Object = array('Class'=>get_class($Object), |
|
700 | + 'Message'=>$severity.': '.$Object->getMessage(), |
|
701 | + 'File'=>$this->_escapeTraceFile($Object->getFile()), |
|
702 | + 'Line'=>$Object->getLine(), |
|
703 | + 'Type'=>'trigger', |
|
704 | + 'Trace'=>$this->_escapeTrace(array_splice($trace,2))); |
|
705 | + $skipFinalObjectEncode = true; |
|
706 | + } else { |
|
707 | + $Object = array('Class'=>get_class($Object), |
|
708 | + 'Message'=>$Object->getMessage(), |
|
709 | + 'File'=>$this->_escapeTraceFile($Object->getFile()), |
|
710 | + 'Line'=>$Object->getLine(), |
|
711 | + 'Type'=>'throw', |
|
712 | + 'Trace'=>$this->_escapeTrace($trace)); |
|
713 | + $skipFinalObjectEncode = true; |
|
714 | + } |
|
715 | + $Type = self::EXCEPTION; |
|
716 | 716 | |
717 | - } else |
|
718 | - if($Type==self::TRACE) { |
|
717 | + } else |
|
718 | + if($Type==self::TRACE) { |
|
719 | 719 | |
720 | - $trace = debug_backtrace(); |
|
721 | - if(!$trace) return false; |
|
722 | - for( $i=0 ; $i<sizeof($trace) ; $i++ ) { |
|
723 | - |
|
724 | - if(isset($trace[$i]['class']) |
|
725 | - && isset($trace[$i]['file']) |
|
726 | - && ($trace[$i]['class']=='FirePHP' |
|
727 | - || $trace[$i]['class']=='FB') |
|
728 | - && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' |
|
729 | - || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { |
|
730 | - /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ |
|
731 | - } else |
|
732 | - if(isset($trace[$i]['class']) |
|
733 | - && isset($trace[$i+1]['file']) |
|
734 | - && $trace[$i]['class']=='FirePHP' |
|
735 | - && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
736 | - /* Skip fb() */ |
|
737 | - } else |
|
738 | - if($trace[$i]['function']=='fb' |
|
739 | - || $trace[$i]['function']=='trace' |
|
740 | - || $trace[$i]['function']=='send') { |
|
741 | - $Object = array('Class'=>isset($trace[$i]['class'])?$trace[$i]['class']:'', |
|
742 | - 'Type'=>isset($trace[$i]['type'])?$trace[$i]['type']:'', |
|
743 | - 'Function'=>isset($trace[$i]['function'])?$trace[$i]['function']:'', |
|
744 | - 'Message'=>$trace[$i]['args'][0], |
|
745 | - 'File'=>isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'', |
|
746 | - 'Line'=>isset($trace[$i]['line'])?$trace[$i]['line']:'', |
|
747 | - 'Args'=>isset($trace[$i]['args'])?$this->encodeObject($trace[$i]['args']):'', |
|
748 | - 'Trace'=>$this->_escapeTrace(array_splice($trace,$i+1))); |
|
749 | - |
|
750 | - $skipFinalObjectEncode = true; |
|
751 | - $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; |
|
752 | - $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; |
|
753 | - break; |
|
754 | - } |
|
755 | - } |
|
756 | - |
|
757 | - } else |
|
758 | - if($Type==self::TABLE) { |
|
720 | + $trace = debug_backtrace(); |
|
721 | + if(!$trace) return false; |
|
722 | + for( $i=0 ; $i<sizeof($trace) ; $i++ ) { |
|
723 | + |
|
724 | + if(isset($trace[$i]['class']) |
|
725 | + && isset($trace[$i]['file']) |
|
726 | + && ($trace[$i]['class']=='FirePHP' |
|
727 | + || $trace[$i]['class']=='FB') |
|
728 | + && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' |
|
729 | + || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { |
|
730 | + /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ |
|
731 | + } else |
|
732 | + if(isset($trace[$i]['class']) |
|
733 | + && isset($trace[$i+1]['file']) |
|
734 | + && $trace[$i]['class']=='FirePHP' |
|
735 | + && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
736 | + /* Skip fb() */ |
|
737 | + } else |
|
738 | + if($trace[$i]['function']=='fb' |
|
739 | + || $trace[$i]['function']=='trace' |
|
740 | + || $trace[$i]['function']=='send') { |
|
741 | + $Object = array('Class'=>isset($trace[$i]['class'])?$trace[$i]['class']:'', |
|
742 | + 'Type'=>isset($trace[$i]['type'])?$trace[$i]['type']:'', |
|
743 | + 'Function'=>isset($trace[$i]['function'])?$trace[$i]['function']:'', |
|
744 | + 'Message'=>$trace[$i]['args'][0], |
|
745 | + 'File'=>isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'', |
|
746 | + 'Line'=>isset($trace[$i]['line'])?$trace[$i]['line']:'', |
|
747 | + 'Args'=>isset($trace[$i]['args'])?$this->encodeObject($trace[$i]['args']):'', |
|
748 | + 'Trace'=>$this->_escapeTrace(array_splice($trace,$i+1))); |
|
749 | + |
|
750 | + $skipFinalObjectEncode = true; |
|
751 | + $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; |
|
752 | + $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; |
|
753 | + break; |
|
754 | + } |
|
755 | + } |
|
756 | + |
|
757 | + } else |
|
758 | + if($Type==self::TABLE) { |
|
759 | 759 | |
760 | - if(isset($Object[0]) && is_string($Object[0])) { |
|
761 | - $Object[1] = $this->encodeTable($Object[1]); |
|
762 | - } else { |
|
763 | - $Object = $this->encodeTable($Object); |
|
764 | - } |
|
760 | + if(isset($Object[0]) && is_string($Object[0])) { |
|
761 | + $Object[1] = $this->encodeTable($Object[1]); |
|
762 | + } else { |
|
763 | + $Object = $this->encodeTable($Object); |
|
764 | + } |
|
765 | 765 | |
766 | - $skipFinalObjectEncode = true; |
|
766 | + $skipFinalObjectEncode = true; |
|
767 | 767 | |
768 | - } else |
|
769 | - if($Type==self::GROUP_START) { |
|
768 | + } else |
|
769 | + if($Type==self::GROUP_START) { |
|
770 | 770 | |
771 | - if(!$Label) { |
|
772 | - throw $this->newException('You must specify a label for the group!'); |
|
773 | - } |
|
771 | + if(!$Label) { |
|
772 | + throw $this->newException('You must specify a label for the group!'); |
|
773 | + } |
|
774 | 774 | |
775 | - } else { |
|
776 | - if($Type===null) { |
|
777 | - $Type = self::LOG; |
|
778 | - } |
|
779 | - } |
|
775 | + } else { |
|
776 | + if($Type===null) { |
|
777 | + $Type = self::LOG; |
|
778 | + } |
|
779 | + } |
|
780 | 780 | |
781 | - if($this->options['includeLineNumbers']) { |
|
782 | - if(!isset($meta['file']) || !isset($meta['line'])) { |
|
781 | + if($this->options['includeLineNumbers']) { |
|
782 | + if(!isset($meta['file']) || !isset($meta['line'])) { |
|
783 | 783 | |
784 | - $trace = debug_backtrace(); |
|
785 | - for( $i=0 ; $trace && $i<sizeof($trace) ; $i++ ) { |
|
784 | + $trace = debug_backtrace(); |
|
785 | + for( $i=0 ; $trace && $i<sizeof($trace) ; $i++ ) { |
|
786 | 786 | |
787 | - if(isset($trace[$i]['class']) |
|
788 | - && isset($trace[$i]['file']) |
|
789 | - && ($trace[$i]['class']=='FirePHP' |
|
790 | - || $trace[$i]['class']=='FB') |
|
791 | - && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' |
|
792 | - || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { |
|
793 | - /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ |
|
794 | - } else |
|
795 | - if(isset($trace[$i]['class']) |
|
796 | - && isset($trace[$i+1]['file']) |
|
797 | - && $trace[$i]['class']=='FirePHP' |
|
798 | - && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
799 | - /* Skip fb() */ |
|
800 | - } else |
|
801 | - if(isset($trace[$i]['file']) |
|
802 | - && substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
803 | - /* Skip FB::fb() */ |
|
804 | - } else { |
|
805 | - $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; |
|
806 | - $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; |
|
807 | - break; |
|
808 | - } |
|
809 | - } |
|
787 | + if(isset($trace[$i]['class']) |
|
788 | + && isset($trace[$i]['file']) |
|
789 | + && ($trace[$i]['class']=='FirePHP' |
|
790 | + || $trace[$i]['class']=='FB') |
|
791 | + && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' |
|
792 | + || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { |
|
793 | + /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ |
|
794 | + } else |
|
795 | + if(isset($trace[$i]['class']) |
|
796 | + && isset($trace[$i+1]['file']) |
|
797 | + && $trace[$i]['class']=='FirePHP' |
|
798 | + && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
799 | + /* Skip fb() */ |
|
800 | + } else |
|
801 | + if(isset($trace[$i]['file']) |
|
802 | + && substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
803 | + /* Skip FB::fb() */ |
|
804 | + } else { |
|
805 | + $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; |
|
806 | + $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; |
|
807 | + break; |
|
808 | + } |
|
809 | + } |
|
810 | 810 | |
811 | - } |
|
812 | - } else { |
|
813 | - unset($meta['file']); |
|
814 | - unset($meta['line']); |
|
815 | - } |
|
811 | + } |
|
812 | + } else { |
|
813 | + unset($meta['file']); |
|
814 | + unset($meta['line']); |
|
815 | + } |
|
816 | 816 | |
817 | 817 | $this->setHeader('X-Wf-Protocol-1','http://meta.wildfirehq.org/Protocol/JsonStream/0.2'); |
818 | 818 | $this->setHeader('X-Wf-1-Plugin-1','http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/'.self::VERSION); |
819 | 819 | |
820 | - $structure_index = 1; |
|
821 | - if($Type==self::DUMP) { |
|
822 | - $structure_index = 2; |
|
823 | - $this->setHeader('X-Wf-1-Structure-2','http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1'); |
|
824 | - } else { |
|
825 | - $this->setHeader('X-Wf-1-Structure-1','http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1'); |
|
826 | - } |
|
820 | + $structure_index = 1; |
|
821 | + if($Type==self::DUMP) { |
|
822 | + $structure_index = 2; |
|
823 | + $this->setHeader('X-Wf-1-Structure-2','http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1'); |
|
824 | + } else { |
|
825 | + $this->setHeader('X-Wf-1-Structure-1','http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1'); |
|
826 | + } |
|
827 | 827 | |
828 | - if($Type==self::DUMP) { |
|
829 | - $msg = '{"'.$Label.'":'.$this->jsonEncode($Object, $skipFinalObjectEncode).'}'; |
|
830 | - } else { |
|
831 | - $msg_meta = $Options; |
|
832 | - $msg_meta['Type'] = $Type; |
|
833 | - if($Label!==null) { |
|
834 | - $msg_meta['Label'] = $Label; |
|
835 | - } |
|
836 | - if(isset($meta['file']) && !isset($msg_meta['File'])) { |
|
837 | - $msg_meta['File'] = $meta['file']; |
|
838 | - } |
|
839 | - if(isset($meta['line']) && !isset($msg_meta['Line'])) { |
|
840 | - $msg_meta['Line'] = $meta['line']; |
|
841 | - } |
|
842 | - $msg = '['.$this->jsonEncode($msg_meta).','.$this->jsonEncode($Object, $skipFinalObjectEncode).']'; |
|
843 | - } |
|
828 | + if($Type==self::DUMP) { |
|
829 | + $msg = '{"'.$Label.'":'.$this->jsonEncode($Object, $skipFinalObjectEncode).'}'; |
|
830 | + } else { |
|
831 | + $msg_meta = $Options; |
|
832 | + $msg_meta['Type'] = $Type; |
|
833 | + if($Label!==null) { |
|
834 | + $msg_meta['Label'] = $Label; |
|
835 | + } |
|
836 | + if(isset($meta['file']) && !isset($msg_meta['File'])) { |
|
837 | + $msg_meta['File'] = $meta['file']; |
|
838 | + } |
|
839 | + if(isset($meta['line']) && !isset($msg_meta['Line'])) { |
|
840 | + $msg_meta['Line'] = $meta['line']; |
|
841 | + } |
|
842 | + $msg = '['.$this->jsonEncode($msg_meta).','.$this->jsonEncode($Object, $skipFinalObjectEncode).']'; |
|
843 | + } |
|
844 | 844 | |
845 | - $parts = explode("\n",chunk_split($msg, 5000, "\n")); |
|
845 | + $parts = explode("\n",chunk_split($msg, 5000, "\n")); |
|
846 | 846 | |
847 | - for( $i=0 ; $i<count($parts) ; $i++) { |
|
847 | + for( $i=0 ; $i<count($parts) ; $i++) { |
|
848 | 848 | |
849 | - $part = $parts[$i]; |
|
850 | - if ($part) { |
|
849 | + $part = $parts[$i]; |
|
850 | + if ($part) { |
|
851 | 851 | |
852 | - if(count($parts)>2) { |
|
853 | - // Message needs to be split into multiple parts |
|
854 | - $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, |
|
855 | - (($i==0)?strlen($msg):'') |
|
856 | - . '|' . $part . '|' |
|
857 | - . (($i<count($parts)-2)?'\\':'')); |
|
858 | - } else { |
|
859 | - $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, |
|
860 | - strlen($part) . '|' . $part . '|'); |
|
861 | - } |
|
852 | + if(count($parts)>2) { |
|
853 | + // Message needs to be split into multiple parts |
|
854 | + $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, |
|
855 | + (($i==0)?strlen($msg):'') |
|
856 | + . '|' . $part . '|' |
|
857 | + . (($i<count($parts)-2)?'\\':'')); |
|
858 | + } else { |
|
859 | + $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, |
|
860 | + strlen($part) . '|' . $part . '|'); |
|
861 | + } |
|
862 | 862 | |
863 | - $this->messageIndex++; |
|
863 | + $this->messageIndex++; |
|
864 | 864 | |
865 | - if ($this->messageIndex > 99999) { |
|
866 | - throw $this->newException('Maximum number (99,999) of messages reached!'); |
|
867 | - } |
|
868 | - } |
|
869 | - } |
|
865 | + if ($this->messageIndex > 99999) { |
|
866 | + throw $this->newException('Maximum number (99,999) of messages reached!'); |
|
867 | + } |
|
868 | + } |
|
869 | + } |
|
870 | 870 | |
871 | 871 | $this->setHeader('X-Wf-1-Index',$this->messageIndex-1); |
872 | 872 | |
873 | - return true; |
|
873 | + return true; |
|
874 | 874 | } |
875 | 875 | |
876 | 876 | /** |
@@ -880,7 +880,7 @@ discard block |
||
880 | 880 | * @return string |
881 | 881 | */ |
882 | 882 | protected function _standardizePath($Path) { |
883 | - return preg_replace('/\\\\+/','/',$Path); |
|
883 | + return preg_replace('/\\\\+/','/',$Path); |
|
884 | 884 | } |
885 | 885 | |
886 | 886 | /** |
@@ -890,16 +890,16 @@ discard block |
||
890 | 890 | * @return array |
891 | 891 | */ |
892 | 892 | protected function _escapeTrace($Trace) { |
893 | - if(!$Trace) return $Trace; |
|
894 | - for( $i=0 ; $i<sizeof($Trace) ; $i++ ) { |
|
895 | - if(isset($Trace[$i]['file'])) { |
|
896 | - $Trace[$i]['file'] = $this->_escapeTraceFile($Trace[$i]['file']); |
|
897 | - } |
|
898 | - if(isset($Trace[$i]['args'])) { |
|
899 | - $Trace[$i]['args'] = $this->encodeObject($Trace[$i]['args']); |
|
900 | - } |
|
901 | - } |
|
902 | - return $Trace; |
|
893 | + if(!$Trace) return $Trace; |
|
894 | + for( $i=0 ; $i<sizeof($Trace) ; $i++ ) { |
|
895 | + if(isset($Trace[$i]['file'])) { |
|
896 | + $Trace[$i]['file'] = $this->_escapeTraceFile($Trace[$i]['file']); |
|
897 | + } |
|
898 | + if(isset($Trace[$i]['args'])) { |
|
899 | + $Trace[$i]['args'] = $this->encodeObject($Trace[$i]['args']); |
|
900 | + } |
|
901 | + } |
|
902 | + return $Trace; |
|
903 | 903 | } |
904 | 904 | |
905 | 905 | /** |
@@ -909,15 +909,15 @@ discard block |
||
909 | 909 | * @return string |
910 | 910 | */ |
911 | 911 | protected function _escapeTraceFile($File) { |
912 | - /* Check if we have a windows filepath */ |
|
913 | - if(strpos($File,'\\')) { |
|
914 | - /* First strip down to single \ */ |
|
912 | + /* Check if we have a windows filepath */ |
|
913 | + if(strpos($File,'\\')) { |
|
914 | + /* First strip down to single \ */ |
|
915 | 915 | |
916 | - $file = preg_replace('/\\\\+/','\\',$File); |
|
916 | + $file = preg_replace('/\\\\+/','\\',$File); |
|
917 | 917 | |
918 | - return $file; |
|
919 | - } |
|
920 | - return $File; |
|
918 | + return $file; |
|
919 | + } |
|
920 | + return $File; |
|
921 | 921 | } |
922 | 922 | |
923 | 923 | /** |
@@ -927,7 +927,7 @@ discard block |
||
927 | 927 | * @param string_type $Value |
928 | 928 | */ |
929 | 929 | protected function setHeader($Name, $Value) { |
930 | - return header($Name.': '.$Value); |
|
930 | + return header($Name.': '.$Value); |
|
931 | 931 | } |
932 | 932 | |
933 | 933 | /** |
@@ -936,8 +936,8 @@ discard block |
||
936 | 936 | * @return string|false |
937 | 937 | */ |
938 | 938 | protected function getUserAgent() { |
939 | - if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; |
|
940 | - return $_SERVER['HTTP_USER_AGENT']; |
|
939 | + if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; |
|
940 | + return $_SERVER['HTTP_USER_AGENT']; |
|
941 | 941 | } |
942 | 942 | |
943 | 943 | /** |
@@ -947,7 +947,7 @@ discard block |
||
947 | 947 | * @return Exception |
948 | 948 | */ |
949 | 949 | protected function newException($Message) { |
950 | - return new Exception($Message); |
|
950 | + return new Exception($Message); |
|
951 | 951 | } |
952 | 952 | |
953 | 953 | /** |
@@ -960,17 +960,17 @@ discard block |
||
960 | 960 | */ |
961 | 961 | public function jsonEncode($Object, $skipObjectEncode=false) |
962 | 962 | { |
963 | - if(!$skipObjectEncode) { |
|
964 | - $Object = $this->encodeObject($Object); |
|
965 | - } |
|
963 | + if(!$skipObjectEncode) { |
|
964 | + $Object = $this->encodeObject($Object); |
|
965 | + } |
|
966 | 966 | |
967 | - if(function_exists('json_encode') |
|
968 | - && $this->options['useNativeJsonEncode']!=false) { |
|
967 | + if(function_exists('json_encode') |
|
968 | + && $this->options['useNativeJsonEncode']!=false) { |
|
969 | 969 | |
970 | - return json_encode($Object); |
|
971 | - } else { |
|
972 | - return $this->json_encode($Object); |
|
973 | - } |
|
970 | + return json_encode($Object); |
|
971 | + } else { |
|
972 | + return $this->json_encode($Object); |
|
973 | + } |
|
974 | 974 | } |
975 | 975 | |
976 | 976 | /** |
@@ -981,23 +981,23 @@ discard block |
||
981 | 981 | */ |
982 | 982 | protected function encodeTable($Table) { |
983 | 983 | |
984 | - if(!$Table) return $Table; |
|
984 | + if(!$Table) return $Table; |
|
985 | 985 | |
986 | - $new_table = array(); |
|
987 | - foreach($Table as $row) { |
|
986 | + $new_table = array(); |
|
987 | + foreach($Table as $row) { |
|
988 | 988 | |
989 | - if(is_array($row)) { |
|
990 | - $new_row = array(); |
|
989 | + if(is_array($row)) { |
|
990 | + $new_row = array(); |
|
991 | 991 | |
992 | - foreach($row as $item) { |
|
993 | - $new_row[] = $this->encodeObject($item); |
|
994 | - } |
|
992 | + foreach($row as $item) { |
|
993 | + $new_row[] = $this->encodeObject($item); |
|
994 | + } |
|
995 | 995 | |
996 | - $new_table[] = $new_row; |
|
997 | - } |
|
998 | - } |
|
996 | + $new_table[] = $new_row; |
|
997 | + } |
|
998 | + } |
|
999 | 999 | |
1000 | - return $new_table; |
|
1000 | + return $new_table; |
|
1001 | 1001 | } |
1002 | 1002 | |
1003 | 1003 | /** |
@@ -1010,135 +1010,135 @@ discard block |
||
1010 | 1010 | */ |
1011 | 1011 | protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1) |
1012 | 1012 | { |
1013 | - $return = array(); |
|
1013 | + $return = array(); |
|
1014 | 1014 | |
1015 | - if (is_resource($Object)) { |
|
1015 | + if (is_resource($Object)) { |
|
1016 | 1016 | |
1017 | - return '** '.(string)$Object.' **'; |
|
1017 | + return '** '.(string)$Object.' **'; |
|
1018 | 1018 | |
1019 | - } else |
|
1020 | - if (is_object($Object)) { |
|
1019 | + } else |
|
1020 | + if (is_object($Object)) { |
|
1021 | 1021 | |
1022 | - if ($ObjectDepth > $this->options['maxObjectDepth']) { |
|
1023 | - return '** Max Object Depth ('.$this->options['maxObjectDepth'].') **'; |
|
1024 | - } |
|
1022 | + if ($ObjectDepth > $this->options['maxObjectDepth']) { |
|
1023 | + return '** Max Object Depth ('.$this->options['maxObjectDepth'].') **'; |
|
1024 | + } |
|
1025 | 1025 | |
1026 | - foreach ($this->objectStack as $refVal) { |
|
1027 | - if ($refVal === $Object) { |
|
1028 | - return '** Recursion ('.get_class($Object).') **'; |
|
1029 | - } |
|
1030 | - } |
|
1031 | - array_push($this->objectStack, $Object); |
|
1026 | + foreach ($this->objectStack as $refVal) { |
|
1027 | + if ($refVal === $Object) { |
|
1028 | + return '** Recursion ('.get_class($Object).') **'; |
|
1029 | + } |
|
1030 | + } |
|
1031 | + array_push($this->objectStack, $Object); |
|
1032 | 1032 | |
1033 | - $return['__className'] = $class = get_class($Object); |
|
1034 | - $class_lower = strtolower($class); |
|
1035 | - |
|
1036 | - $reflectionClass = new ReflectionClass($class); |
|
1037 | - $properties = array(); |
|
1038 | - foreach( $reflectionClass->getProperties() as $property) { |
|
1039 | - $properties[$property->getName()] = $property; |
|
1040 | - } |
|
1033 | + $return['__className'] = $class = get_class($Object); |
|
1034 | + $class_lower = strtolower($class); |
|
1035 | + |
|
1036 | + $reflectionClass = new ReflectionClass($class); |
|
1037 | + $properties = array(); |
|
1038 | + foreach( $reflectionClass->getProperties() as $property) { |
|
1039 | + $properties[$property->getName()] = $property; |
|
1040 | + } |
|
1041 | 1041 | |
1042 | - $members = (array)$Object; |
|
1042 | + $members = (array)$Object; |
|
1043 | 1043 | |
1044 | - foreach( $properties as $raw_name => $property ) { |
|
1044 | + foreach( $properties as $raw_name => $property ) { |
|
1045 | 1045 | |
1046 | - $name = $raw_name; |
|
1047 | - if($property->isStatic()) { |
|
1048 | - $name = 'static:'.$name; |
|
1049 | - } |
|
1050 | - if($property->isPublic()) { |
|
1051 | - $name = 'public:'.$name; |
|
1052 | - } else |
|
1053 | - if($property->isPrivate()) { |
|
1054 | - $name = 'private:'.$name; |
|
1055 | - $raw_name = "\0".$class."\0".$raw_name; |
|
1056 | - } else |
|
1057 | - if($property->isProtected()) { |
|
1058 | - $name = 'protected:'.$name; |
|
1059 | - $raw_name = "\0".'*'."\0".$raw_name; |
|
1060 | - } |
|
1046 | + $name = $raw_name; |
|
1047 | + if($property->isStatic()) { |
|
1048 | + $name = 'static:'.$name; |
|
1049 | + } |
|
1050 | + if($property->isPublic()) { |
|
1051 | + $name = 'public:'.$name; |
|
1052 | + } else |
|
1053 | + if($property->isPrivate()) { |
|
1054 | + $name = 'private:'.$name; |
|
1055 | + $raw_name = "\0".$class."\0".$raw_name; |
|
1056 | + } else |
|
1057 | + if($property->isProtected()) { |
|
1058 | + $name = 'protected:'.$name; |
|
1059 | + $raw_name = "\0".'*'."\0".$raw_name; |
|
1060 | + } |
|
1061 | 1061 | |
1062 | - if(!(isset($this->objectFilters[$class_lower]) |
|
1063 | - && is_array($this->objectFilters[$class_lower]) |
|
1064 | - && in_array($raw_name,$this->objectFilters[$class_lower]))) { |
|
1062 | + if(!(isset($this->objectFilters[$class_lower]) |
|
1063 | + && is_array($this->objectFilters[$class_lower]) |
|
1064 | + && in_array($raw_name,$this->objectFilters[$class_lower]))) { |
|
1065 | 1065 | |
1066 | - if(array_key_exists($raw_name,$members) |
|
1067 | - && !$property->isStatic()) { |
|
1066 | + if(array_key_exists($raw_name,$members) |
|
1067 | + && !$property->isStatic()) { |
|
1068 | 1068 | |
1069 | - $return[$name] = $this->encodeObject($members[$raw_name], $ObjectDepth + 1, 1); |
|
1069 | + $return[$name] = $this->encodeObject($members[$raw_name], $ObjectDepth + 1, 1); |
|
1070 | 1070 | |
1071 | - } else { |
|
1072 | - if(method_exists($property,'setAccessible')) { |
|
1073 | - $property->setAccessible(true); |
|
1074 | - $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1); |
|
1075 | - } else |
|
1076 | - if($property->isPublic()) { |
|
1077 | - $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1); |
|
1078 | - } else { |
|
1079 | - $return[$name] = '** Need PHP 5.3 to get value **'; |
|
1080 | - } |
|
1081 | - } |
|
1082 | - } else { |
|
1083 | - $return[$name] = '** Excluded by Filter **'; |
|
1084 | - } |
|
1085 | - } |
|
1071 | + } else { |
|
1072 | + if(method_exists($property,'setAccessible')) { |
|
1073 | + $property->setAccessible(true); |
|
1074 | + $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1); |
|
1075 | + } else |
|
1076 | + if($property->isPublic()) { |
|
1077 | + $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1); |
|
1078 | + } else { |
|
1079 | + $return[$name] = '** Need PHP 5.3 to get value **'; |
|
1080 | + } |
|
1081 | + } |
|
1082 | + } else { |
|
1083 | + $return[$name] = '** Excluded by Filter **'; |
|
1084 | + } |
|
1085 | + } |
|
1086 | 1086 | |
1087 | - // Include all members that are not defined in the class |
|
1088 | - // but exist in the object |
|
1089 | - foreach( $members as $raw_name => $value ) { |
|
1087 | + // Include all members that are not defined in the class |
|
1088 | + // but exist in the object |
|
1089 | + foreach( $members as $raw_name => $value ) { |
|
1090 | 1090 | |
1091 | - $name = $raw_name; |
|
1091 | + $name = $raw_name; |
|
1092 | 1092 | |
1093 | - if ($name{0} == "\0") { |
|
1094 | - $parts = explode("\0", $name); |
|
1095 | - $name = $parts[2]; |
|
1096 | - } |
|
1093 | + if ($name{0} == "\0") { |
|
1094 | + $parts = explode("\0", $name); |
|
1095 | + $name = $parts[2]; |
|
1096 | + } |
|
1097 | 1097 | |
1098 | - if(!isset($properties[$name])) { |
|
1099 | - $name = 'undeclared:'.$name; |
|
1098 | + if(!isset($properties[$name])) { |
|
1099 | + $name = 'undeclared:'.$name; |
|
1100 | 1100 | |
1101 | - if(!(isset($this->objectFilters[$class_lower]) |
|
1102 | - && is_array($this->objectFilters[$class_lower]) |
|
1103 | - && in_array($raw_name,$this->objectFilters[$class_lower]))) { |
|
1101 | + if(!(isset($this->objectFilters[$class_lower]) |
|
1102 | + && is_array($this->objectFilters[$class_lower]) |
|
1103 | + && in_array($raw_name,$this->objectFilters[$class_lower]))) { |
|
1104 | 1104 | |
1105 | - $return[$name] = $this->encodeObject($value, $ObjectDepth + 1, 1); |
|
1106 | - } else { |
|
1107 | - $return[$name] = '** Excluded by Filter **'; |
|
1108 | - } |
|
1109 | - } |
|
1110 | - } |
|
1105 | + $return[$name] = $this->encodeObject($value, $ObjectDepth + 1, 1); |
|
1106 | + } else { |
|
1107 | + $return[$name] = '** Excluded by Filter **'; |
|
1108 | + } |
|
1109 | + } |
|
1110 | + } |
|
1111 | 1111 | |
1112 | - array_pop($this->objectStack); |
|
1112 | + array_pop($this->objectStack); |
|
1113 | 1113 | |
1114 | - } elseif (is_array($Object)) { |
|
1114 | + } elseif (is_array($Object)) { |
|
1115 | 1115 | |
1116 | - if ($ArrayDepth > $this->options['maxArrayDepth']) { |
|
1117 | - return '** Max Array Depth ('.$this->options['maxArrayDepth'].') **'; |
|
1118 | - } |
|
1116 | + if ($ArrayDepth > $this->options['maxArrayDepth']) { |
|
1117 | + return '** Max Array Depth ('.$this->options['maxArrayDepth'].') **'; |
|
1118 | + } |
|
1119 | 1119 | |
1120 | - foreach ($Object as $key => $val) { |
|
1120 | + foreach ($Object as $key => $val) { |
|
1121 | 1121 | |
1122 | - // Encoding the $GLOBALS PHP array causes an infinite loop |
|
1123 | - // if the recursion is not reset here as it contains |
|
1124 | - // a reference to itself. This is the only way I have come up |
|
1125 | - // with to stop infinite recursion in this case. |
|
1126 | - if($key=='GLOBALS' |
|
1127 | - && is_array($val) |
|
1128 | - && array_key_exists('GLOBALS',$val)) { |
|
1129 | - $val['GLOBALS'] = '** Recursion (GLOBALS) **'; |
|
1130 | - } |
|
1122 | + // Encoding the $GLOBALS PHP array causes an infinite loop |
|
1123 | + // if the recursion is not reset here as it contains |
|
1124 | + // a reference to itself. This is the only way I have come up |
|
1125 | + // with to stop infinite recursion in this case. |
|
1126 | + if($key=='GLOBALS' |
|
1127 | + && is_array($val) |
|
1128 | + && array_key_exists('GLOBALS',$val)) { |
|
1129 | + $val['GLOBALS'] = '** Recursion (GLOBALS) **'; |
|
1130 | + } |
|
1131 | 1131 | |
1132 | - $return[$key] = $this->encodeObject($val, 1, $ArrayDepth + 1); |
|
1133 | - } |
|
1134 | - } else { |
|
1135 | - if(self::is_utf8($Object)) { |
|
1136 | - return $Object; |
|
1137 | - } else { |
|
1138 | - return utf8_encode($Object); |
|
1139 | - } |
|
1140 | - } |
|
1141 | - return $return; |
|
1132 | + $return[$key] = $this->encodeObject($val, 1, $ArrayDepth + 1); |
|
1133 | + } |
|
1134 | + } else { |
|
1135 | + if(self::is_utf8($Object)) { |
|
1136 | + return $Object; |
|
1137 | + } else { |
|
1138 | + return utf8_encode($Object); |
|
1139 | + } |
|
1140 | + } |
|
1141 | + return $return; |
|
1142 | 1142 | } |
1143 | 1143 | |
1144 | 1144 | /** |
@@ -1148,29 +1148,29 @@ discard block |
||
1148 | 1148 | * @return boolean |
1149 | 1149 | */ |
1150 | 1150 | protected static function is_utf8($str) { |
1151 | - $c=0; $b=0; |
|
1152 | - $bits=0; |
|
1153 | - $len=strlen($str); |
|
1154 | - for($i=0; $i<$len; $i++){ |
|
1155 | - $c=ord($str[$i]); |
|
1156 | - if($c > 128){ |
|
1157 | - if(($c >= 254)) return false; |
|
1158 | - elseif($c >= 252) $bits=6; |
|
1159 | - elseif($c >= 248) $bits=5; |
|
1160 | - elseif($c >= 240) $bits=4; |
|
1161 | - elseif($c >= 224) $bits=3; |
|
1162 | - elseif($c >= 192) $bits=2; |
|
1163 | - else return false; |
|
1164 | - if(($i+$bits) > $len) return false; |
|
1165 | - while($bits > 1){ |
|
1166 | - $i++; |
|
1167 | - $b=ord($str[$i]); |
|
1168 | - if($b < 128 || $b > 191) return false; |
|
1169 | - $bits--; |
|
1170 | - } |
|
1171 | - } |
|
1172 | - } |
|
1173 | - return true; |
|
1151 | + $c=0; $b=0; |
|
1152 | + $bits=0; |
|
1153 | + $len=strlen($str); |
|
1154 | + for($i=0; $i<$len; $i++){ |
|
1155 | + $c=ord($str[$i]); |
|
1156 | + if($c > 128){ |
|
1157 | + if(($c >= 254)) return false; |
|
1158 | + elseif($c >= 252) $bits=6; |
|
1159 | + elseif($c >= 248) $bits=5; |
|
1160 | + elseif($c >= 240) $bits=4; |
|
1161 | + elseif($c >= 224) $bits=3; |
|
1162 | + elseif($c >= 192) $bits=2; |
|
1163 | + else return false; |
|
1164 | + if(($i+$bits) > $len) return false; |
|
1165 | + while($bits > 1){ |
|
1166 | + $i++; |
|
1167 | + $b=ord($str[$i]); |
|
1168 | + if($b < 128 || $b > 191) return false; |
|
1169 | + $bits--; |
|
1170 | + } |
|
1171 | + } |
|
1172 | + } |
|
1173 | + return true; |
|
1174 | 1174 | } |
1175 | 1175 | |
1176 | 1176 | /** |
@@ -1249,35 +1249,35 @@ discard block |
||
1249 | 1249 | */ |
1250 | 1250 | private function json_utf82utf16($utf8) |
1251 | 1251 | { |
1252 | - // oh please oh please oh please oh please oh please |
|
1253 | - if(function_exists('mb_convert_encoding')) { |
|
1254 | - return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); |
|
1255 | - } |
|
1256 | - |
|
1257 | - switch(strlen($utf8)) { |
|
1258 | - case 1: |
|
1259 | - // this case should never be reached, because we are in ASCII range |
|
1260 | - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1261 | - return $utf8; |
|
1262 | - |
|
1263 | - case 2: |
|
1264 | - // return a UTF-16 character from a 2-byte UTF-8 char |
|
1265 | - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1266 | - return chr(0x07 & (ord($utf8{0}) >> 2)) |
|
1267 | - . chr((0xC0 & (ord($utf8{0}) << 6)) |
|
1268 | - | (0x3F & ord($utf8{1}))); |
|
1269 | - |
|
1270 | - case 3: |
|
1271 | - // return a UTF-16 character from a 3-byte UTF-8 char |
|
1272 | - // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1273 | - return chr((0xF0 & (ord($utf8{0}) << 4)) |
|
1274 | - | (0x0F & (ord($utf8{1}) >> 2))) |
|
1275 | - . chr((0xC0 & (ord($utf8{1}) << 6)) |
|
1276 | - | (0x7F & ord($utf8{2}))); |
|
1277 | - } |
|
1278 | - |
|
1279 | - // ignoring UTF-32 for now, sorry |
|
1280 | - return ''; |
|
1252 | + // oh please oh please oh please oh please oh please |
|
1253 | + if(function_exists('mb_convert_encoding')) { |
|
1254 | + return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); |
|
1255 | + } |
|
1256 | + |
|
1257 | + switch(strlen($utf8)) { |
|
1258 | + case 1: |
|
1259 | + // this case should never be reached, because we are in ASCII range |
|
1260 | + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1261 | + return $utf8; |
|
1262 | + |
|
1263 | + case 2: |
|
1264 | + // return a UTF-16 character from a 2-byte UTF-8 char |
|
1265 | + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1266 | + return chr(0x07 & (ord($utf8{0}) >> 2)) |
|
1267 | + . chr((0xC0 & (ord($utf8{0}) << 6)) |
|
1268 | + | (0x3F & ord($utf8{1}))); |
|
1269 | + |
|
1270 | + case 3: |
|
1271 | + // return a UTF-16 character from a 3-byte UTF-8 char |
|
1272 | + // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1273 | + return chr((0xF0 & (ord($utf8{0}) << 4)) |
|
1274 | + | (0x0F & (ord($utf8{1}) >> 2))) |
|
1275 | + . chr((0xC0 & (ord($utf8{1}) << 6)) |
|
1276 | + | (0x7F & ord($utf8{2}))); |
|
1277 | + } |
|
1278 | + |
|
1279 | + // ignoring UTF-32 for now, sorry |
|
1280 | + return ''; |
|
1281 | 1281 | } |
1282 | 1282 | |
1283 | 1283 | /** |
@@ -1294,133 +1294,133 @@ discard block |
||
1294 | 1294 | private function json_encode($var) |
1295 | 1295 | { |
1296 | 1296 | |
1297 | - if(is_object($var)) { |
|
1298 | - if(in_array($var,$this->json_objectStack)) { |
|
1299 | - return '"** Recursion **"'; |
|
1300 | - } |
|
1301 | - } |
|
1297 | + if(is_object($var)) { |
|
1298 | + if(in_array($var,$this->json_objectStack)) { |
|
1299 | + return '"** Recursion **"'; |
|
1300 | + } |
|
1301 | + } |
|
1302 | 1302 | |
1303 | - switch (gettype($var)) { |
|
1304 | - case 'boolean': |
|
1305 | - return $var ? 'true' : 'false'; |
|
1303 | + switch (gettype($var)) { |
|
1304 | + case 'boolean': |
|
1305 | + return $var ? 'true' : 'false'; |
|
1306 | 1306 | |
1307 | - case 'NULL': |
|
1308 | - return 'null'; |
|
1307 | + case 'NULL': |
|
1308 | + return 'null'; |
|
1309 | 1309 | |
1310 | - case 'integer': |
|
1311 | - return (int) $var; |
|
1310 | + case 'integer': |
|
1311 | + return (int) $var; |
|
1312 | 1312 | |
1313 | - case 'double': |
|
1314 | - case 'float': |
|
1315 | - return (float) $var; |
|
1313 | + case 'double': |
|
1314 | + case 'float': |
|
1315 | + return (float) $var; |
|
1316 | 1316 | |
1317 | - case 'string': |
|
1318 | - // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT |
|
1319 | - $ascii = ''; |
|
1320 | - $strlen_var = strlen($var); |
|
1317 | + case 'string': |
|
1318 | + // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT |
|
1319 | + $ascii = ''; |
|
1320 | + $strlen_var = strlen($var); |
|
1321 | 1321 | |
1322 | - /* |
|
1322 | + /* |
|
1323 | 1323 | * Iterate over every character in the string, |
1324 | 1324 | * escaping with a slash or encoding to UTF-8 where necessary |
1325 | 1325 | */ |
1326 | - for ($c = 0; $c < $strlen_var; ++$c) { |
|
1327 | - |
|
1328 | - $ord_var_c = ord($var{$c}); |
|
1329 | - |
|
1330 | - switch (true) { |
|
1331 | - case $ord_var_c == 0x08: |
|
1332 | - $ascii .= '\b'; |
|
1333 | - break; |
|
1334 | - case $ord_var_c == 0x09: |
|
1335 | - $ascii .= '\t'; |
|
1336 | - break; |
|
1337 | - case $ord_var_c == 0x0A: |
|
1338 | - $ascii .= '\n'; |
|
1339 | - break; |
|
1340 | - case $ord_var_c == 0x0C: |
|
1341 | - $ascii .= '\f'; |
|
1342 | - break; |
|
1343 | - case $ord_var_c == 0x0D: |
|
1344 | - $ascii .= '\r'; |
|
1345 | - break; |
|
1346 | - |
|
1347 | - case $ord_var_c == 0x22: |
|
1348 | - case $ord_var_c == 0x2F: |
|
1349 | - case $ord_var_c == 0x5C: |
|
1350 | - // double quote, slash, slosh |
|
1351 | - $ascii .= '\\'.$var{$c}; |
|
1352 | - break; |
|
1353 | - |
|
1354 | - case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): |
|
1355 | - // characters U-00000000 - U-0000007F (same as ASCII) |
|
1356 | - $ascii .= $var{$c}; |
|
1357 | - break; |
|
1358 | - |
|
1359 | - case (($ord_var_c & 0xE0) == 0xC0): |
|
1360 | - // characters U-00000080 - U-000007FF, mask 110XXXXX |
|
1361 | - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1362 | - $char = pack('C*', $ord_var_c, ord($var{$c + 1})); |
|
1363 | - $c += 1; |
|
1364 | - $utf16 = $this->json_utf82utf16($char); |
|
1365 | - $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1366 | - break; |
|
1367 | - |
|
1368 | - case (($ord_var_c & 0xF0) == 0xE0): |
|
1369 | - // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
|
1370 | - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1371 | - $char = pack('C*', $ord_var_c, |
|
1372 | - ord($var{$c + 1}), |
|
1373 | - ord($var{$c + 2})); |
|
1374 | - $c += 2; |
|
1375 | - $utf16 = $this->json_utf82utf16($char); |
|
1376 | - $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1377 | - break; |
|
1378 | - |
|
1379 | - case (($ord_var_c & 0xF8) == 0xF0): |
|
1380 | - // characters U-00010000 - U-001FFFFF, mask 11110XXX |
|
1381 | - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1382 | - $char = pack('C*', $ord_var_c, |
|
1383 | - ord($var{$c + 1}), |
|
1384 | - ord($var{$c + 2}), |
|
1385 | - ord($var{$c + 3})); |
|
1386 | - $c += 3; |
|
1387 | - $utf16 = $this->json_utf82utf16($char); |
|
1388 | - $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1389 | - break; |
|
1390 | - |
|
1391 | - case (($ord_var_c & 0xFC) == 0xF8): |
|
1392 | - // characters U-00200000 - U-03FFFFFF, mask 111110XX |
|
1393 | - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1394 | - $char = pack('C*', $ord_var_c, |
|
1395 | - ord($var{$c + 1}), |
|
1396 | - ord($var{$c + 2}), |
|
1397 | - ord($var{$c + 3}), |
|
1398 | - ord($var{$c + 4})); |
|
1399 | - $c += 4; |
|
1400 | - $utf16 = $this->json_utf82utf16($char); |
|
1401 | - $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1402 | - break; |
|
1403 | - |
|
1404 | - case (($ord_var_c & 0xFE) == 0xFC): |
|
1405 | - // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
|
1406 | - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1407 | - $char = pack('C*', $ord_var_c, |
|
1408 | - ord($var{$c + 1}), |
|
1409 | - ord($var{$c + 2}), |
|
1410 | - ord($var{$c + 3}), |
|
1411 | - ord($var{$c + 4}), |
|
1412 | - ord($var{$c + 5})); |
|
1413 | - $c += 5; |
|
1414 | - $utf16 = $this->json_utf82utf16($char); |
|
1415 | - $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1416 | - break; |
|
1417 | - } |
|
1418 | - } |
|
1419 | - |
|
1420 | - return '"'.$ascii.'"'; |
|
1421 | - |
|
1422 | - case 'array': |
|
1423 | - /* |
|
1326 | + for ($c = 0; $c < $strlen_var; ++$c) { |
|
1327 | + |
|
1328 | + $ord_var_c = ord($var{$c}); |
|
1329 | + |
|
1330 | + switch (true) { |
|
1331 | + case $ord_var_c == 0x08: |
|
1332 | + $ascii .= '\b'; |
|
1333 | + break; |
|
1334 | + case $ord_var_c == 0x09: |
|
1335 | + $ascii .= '\t'; |
|
1336 | + break; |
|
1337 | + case $ord_var_c == 0x0A: |
|
1338 | + $ascii .= '\n'; |
|
1339 | + break; |
|
1340 | + case $ord_var_c == 0x0C: |
|
1341 | + $ascii .= '\f'; |
|
1342 | + break; |
|
1343 | + case $ord_var_c == 0x0D: |
|
1344 | + $ascii .= '\r'; |
|
1345 | + break; |
|
1346 | + |
|
1347 | + case $ord_var_c == 0x22: |
|
1348 | + case $ord_var_c == 0x2F: |
|
1349 | + case $ord_var_c == 0x5C: |
|
1350 | + // double quote, slash, slosh |
|
1351 | + $ascii .= '\\'.$var{$c}; |
|
1352 | + break; |
|
1353 | + |
|
1354 | + case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): |
|
1355 | + // characters U-00000000 - U-0000007F (same as ASCII) |
|
1356 | + $ascii .= $var{$c}; |
|
1357 | + break; |
|
1358 | + |
|
1359 | + case (($ord_var_c & 0xE0) == 0xC0): |
|
1360 | + // characters U-00000080 - U-000007FF, mask 110XXXXX |
|
1361 | + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1362 | + $char = pack('C*', $ord_var_c, ord($var{$c + 1})); |
|
1363 | + $c += 1; |
|
1364 | + $utf16 = $this->json_utf82utf16($char); |
|
1365 | + $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1366 | + break; |
|
1367 | + |
|
1368 | + case (($ord_var_c & 0xF0) == 0xE0): |
|
1369 | + // characters U-00000800 - U-0000FFFF, mask 1110XXXX |
|
1370 | + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1371 | + $char = pack('C*', $ord_var_c, |
|
1372 | + ord($var{$c + 1}), |
|
1373 | + ord($var{$c + 2})); |
|
1374 | + $c += 2; |
|
1375 | + $utf16 = $this->json_utf82utf16($char); |
|
1376 | + $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1377 | + break; |
|
1378 | + |
|
1379 | + case (($ord_var_c & 0xF8) == 0xF0): |
|
1380 | + // characters U-00010000 - U-001FFFFF, mask 11110XXX |
|
1381 | + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1382 | + $char = pack('C*', $ord_var_c, |
|
1383 | + ord($var{$c + 1}), |
|
1384 | + ord($var{$c + 2}), |
|
1385 | + ord($var{$c + 3})); |
|
1386 | + $c += 3; |
|
1387 | + $utf16 = $this->json_utf82utf16($char); |
|
1388 | + $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1389 | + break; |
|
1390 | + |
|
1391 | + case (($ord_var_c & 0xFC) == 0xF8): |
|
1392 | + // characters U-00200000 - U-03FFFFFF, mask 111110XX |
|
1393 | + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1394 | + $char = pack('C*', $ord_var_c, |
|
1395 | + ord($var{$c + 1}), |
|
1396 | + ord($var{$c + 2}), |
|
1397 | + ord($var{$c + 3}), |
|
1398 | + ord($var{$c + 4})); |
|
1399 | + $c += 4; |
|
1400 | + $utf16 = $this->json_utf82utf16($char); |
|
1401 | + $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1402 | + break; |
|
1403 | + |
|
1404 | + case (($ord_var_c & 0xFE) == 0xFC): |
|
1405 | + // characters U-04000000 - U-7FFFFFFF, mask 1111110X |
|
1406 | + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
|
1407 | + $char = pack('C*', $ord_var_c, |
|
1408 | + ord($var{$c + 1}), |
|
1409 | + ord($var{$c + 2}), |
|
1410 | + ord($var{$c + 3}), |
|
1411 | + ord($var{$c + 4}), |
|
1412 | + ord($var{$c + 5})); |
|
1413 | + $c += 5; |
|
1414 | + $utf16 = $this->json_utf82utf16($char); |
|
1415 | + $ascii .= sprintf('\u%04s', bin2hex($utf16)); |
|
1416 | + break; |
|
1417 | + } |
|
1418 | + } |
|
1419 | + |
|
1420 | + return '"'.$ascii.'"'; |
|
1421 | + |
|
1422 | + case 'array': |
|
1423 | + /* |
|
1424 | 1424 | * As per JSON spec if any array key is not an integer |
1425 | 1425 | * we must treat the the whole array as an object. We |
1426 | 1426 | * also try to catch a sparsely populated associative |
@@ -1438,63 +1438,63 @@ discard block |
||
1438 | 1438 | * bracket notation. |
1439 | 1439 | */ |
1440 | 1440 | |
1441 | - // treat as a JSON object |
|
1442 | - if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) { |
|
1441 | + // treat as a JSON object |
|
1442 | + if (is_array($var) && count($var) && (array_keys($var) !== range(0, sizeof($var) - 1))) { |
|
1443 | 1443 | |
1444 | - $this->json_objectStack[] = $var; |
|
1444 | + $this->json_objectStack[] = $var; |
|
1445 | 1445 | |
1446 | - $properties = array_map(array($this, 'json_name_value'), |
|
1447 | - array_keys($var), |
|
1448 | - array_values($var)); |
|
1446 | + $properties = array_map(array($this, 'json_name_value'), |
|
1447 | + array_keys($var), |
|
1448 | + array_values($var)); |
|
1449 | 1449 | |
1450 | - array_pop($this->json_objectStack); |
|
1450 | + array_pop($this->json_objectStack); |
|
1451 | 1451 | |
1452 | - foreach($properties as $property) { |
|
1453 | - if($property instanceof Exception) { |
|
1454 | - return $property; |
|
1455 | - } |
|
1456 | - } |
|
1452 | + foreach($properties as $property) { |
|
1453 | + if($property instanceof Exception) { |
|
1454 | + return $property; |
|
1455 | + } |
|
1456 | + } |
|
1457 | 1457 | |
1458 | - return '{' . join(',', $properties) . '}'; |
|
1459 | - } |
|
1458 | + return '{' . join(',', $properties) . '}'; |
|
1459 | + } |
|
1460 | 1460 | |
1461 | - $this->json_objectStack[] = $var; |
|
1461 | + $this->json_objectStack[] = $var; |
|
1462 | 1462 | |
1463 | - // treat it like a regular array |
|
1464 | - $elements = array_map(array($this, 'json_encode'), $var); |
|
1463 | + // treat it like a regular array |
|
1464 | + $elements = array_map(array($this, 'json_encode'), $var); |
|
1465 | 1465 | |
1466 | - array_pop($this->json_objectStack); |
|
1466 | + array_pop($this->json_objectStack); |
|
1467 | 1467 | |
1468 | - foreach($elements as $element) { |
|
1469 | - if($element instanceof Exception) { |
|
1470 | - return $element; |
|
1471 | - } |
|
1472 | - } |
|
1468 | + foreach($elements as $element) { |
|
1469 | + if($element instanceof Exception) { |
|
1470 | + return $element; |
|
1471 | + } |
|
1472 | + } |
|
1473 | 1473 | |
1474 | - return '[' . join(',', $elements) . ']'; |
|
1474 | + return '[' . join(',', $elements) . ']'; |
|
1475 | 1475 | |
1476 | - case 'object': |
|
1477 | - $vars = self::encodeObject($var); |
|
1476 | + case 'object': |
|
1477 | + $vars = self::encodeObject($var); |
|
1478 | 1478 | |
1479 | - $this->json_objectStack[] = $var; |
|
1479 | + $this->json_objectStack[] = $var; |
|
1480 | 1480 | |
1481 | - $properties = array_map(array($this, 'json_name_value'), |
|
1482 | - array_keys($vars), |
|
1483 | - array_values($vars)); |
|
1481 | + $properties = array_map(array($this, 'json_name_value'), |
|
1482 | + array_keys($vars), |
|
1483 | + array_values($vars)); |
|
1484 | 1484 | |
1485 | - array_pop($this->json_objectStack); |
|
1485 | + array_pop($this->json_objectStack); |
|
1486 | 1486 | |
1487 | - foreach($properties as $property) { |
|
1488 | - if($property instanceof Exception) { |
|
1489 | - return $property; |
|
1490 | - } |
|
1491 | - } |
|
1487 | + foreach($properties as $property) { |
|
1488 | + if($property instanceof Exception) { |
|
1489 | + return $property; |
|
1490 | + } |
|
1491 | + } |
|
1492 | 1492 | |
1493 | - return '{' . join(',', $properties) . '}'; |
|
1493 | + return '{' . join(',', $properties) . '}'; |
|
1494 | 1494 | |
1495 | - default: |
|
1496 | - return null; |
|
1497 | - } |
|
1495 | + default: |
|
1496 | + return null; |
|
1497 | + } |
|
1498 | 1498 | } |
1499 | 1499 | |
1500 | 1500 | /** |
@@ -1508,22 +1508,22 @@ discard block |
||
1508 | 1508 | */ |
1509 | 1509 | private function json_name_value($name, $value) |
1510 | 1510 | { |
1511 | - // Encoding the $GLOBALS PHP array causes an infinite loop |
|
1512 | - // if the recursion is not reset here as it contains |
|
1513 | - // a reference to itself. This is the only way I have come up |
|
1514 | - // with to stop infinite recursion in this case. |
|
1515 | - if($name=='GLOBALS' |
|
1516 | - && is_array($value) |
|
1517 | - && array_key_exists('GLOBALS',$value)) { |
|
1518 | - $value['GLOBALS'] = '** Recursion **'; |
|
1519 | - } |
|
1511 | + // Encoding the $GLOBALS PHP array causes an infinite loop |
|
1512 | + // if the recursion is not reset here as it contains |
|
1513 | + // a reference to itself. This is the only way I have come up |
|
1514 | + // with to stop infinite recursion in this case. |
|
1515 | + if($name=='GLOBALS' |
|
1516 | + && is_array($value) |
|
1517 | + && array_key_exists('GLOBALS',$value)) { |
|
1518 | + $value['GLOBALS'] = '** Recursion **'; |
|
1519 | + } |
|
1520 | 1520 | |
1521 | - $encoded_value = $this->json_encode($value); |
|
1521 | + $encoded_value = $this->json_encode($value); |
|
1522 | 1522 | |
1523 | - if($encoded_value instanceof Exception) { |
|
1524 | - return $encoded_value; |
|
1525 | - } |
|
1523 | + if($encoded_value instanceof Exception) { |
|
1524 | + return $encoded_value; |
|
1525 | + } |
|
1526 | 1526 | |
1527 | - return $this->json_encode(strval($name)) . ':' . $encoded_value; |
|
1527 | + return $this->json_encode(strval($name)) . ':' . $encoded_value; |
|
1528 | 1528 | } |
1529 | 1529 | } |
@@ -718,7 +718,9 @@ discard block |
||
718 | 718 | if($Type==self::TRACE) { |
719 | 719 | |
720 | 720 | $trace = debug_backtrace(); |
721 | - if(!$trace) return false; |
|
721 | + if(!$trace) { |
|
722 | + return false; |
|
723 | + } |
|
722 | 724 | for( $i=0 ; $i<sizeof($trace) ; $i++ ) { |
723 | 725 | |
724 | 726 | if(isset($trace[$i]['class']) |
@@ -890,7 +892,9 @@ discard block |
||
890 | 892 | * @return array |
891 | 893 | */ |
892 | 894 | protected function _escapeTrace($Trace) { |
893 | - if(!$Trace) return $Trace; |
|
895 | + if(!$Trace) { |
|
896 | + return $Trace; |
|
897 | + } |
|
894 | 898 | for( $i=0 ; $i<sizeof($Trace) ; $i++ ) { |
895 | 899 | if(isset($Trace[$i]['file'])) { |
896 | 900 | $Trace[$i]['file'] = $this->_escapeTraceFile($Trace[$i]['file']); |
@@ -936,7 +940,9 @@ discard block |
||
936 | 940 | * @return string|false |
937 | 941 | */ |
938 | 942 | protected function getUserAgent() { |
939 | - if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; |
|
943 | + if(!isset($_SERVER['HTTP_USER_AGENT'])) { |
|
944 | + return false; |
|
945 | + } |
|
940 | 946 | return $_SERVER['HTTP_USER_AGENT']; |
941 | 947 | } |
942 | 948 | |
@@ -981,7 +987,9 @@ discard block |
||
981 | 987 | */ |
982 | 988 | protected function encodeTable($Table) { |
983 | 989 | |
984 | - if(!$Table) return $Table; |
|
990 | + if(!$Table) { |
|
991 | + return $Table; |
|
992 | + } |
|
985 | 993 | |
986 | 994 | $new_table = array(); |
987 | 995 | foreach($Table as $row) { |
@@ -1154,18 +1162,30 @@ discard block |
||
1154 | 1162 | for($i=0; $i<$len; $i++){ |
1155 | 1163 | $c=ord($str[$i]); |
1156 | 1164 | if($c > 128){ |
1157 | - if(($c >= 254)) return false; |
|
1158 | - elseif($c >= 252) $bits=6; |
|
1159 | - elseif($c >= 248) $bits=5; |
|
1160 | - elseif($c >= 240) $bits=4; |
|
1161 | - elseif($c >= 224) $bits=3; |
|
1162 | - elseif($c >= 192) $bits=2; |
|
1163 | - else return false; |
|
1164 | - if(($i+$bits) > $len) return false; |
|
1165 | + if(($c >= 254)) { |
|
1166 | + return false; |
|
1167 | + } elseif($c >= 252) { |
|
1168 | + $bits=6; |
|
1169 | + } elseif($c >= 248) { |
|
1170 | + $bits=5; |
|
1171 | + } elseif($c >= 240) { |
|
1172 | + $bits=4; |
|
1173 | + } elseif($c >= 224) { |
|
1174 | + $bits=3; |
|
1175 | + } elseif($c >= 192) { |
|
1176 | + $bits=2; |
|
1177 | + } else { |
|
1178 | + return false; |
|
1179 | + } |
|
1180 | + if(($i+$bits) > $len) { |
|
1181 | + return false; |
|
1182 | + } |
|
1165 | 1183 | while($bits > 1){ |
1166 | 1184 | $i++; |
1167 | 1185 | $b=ord($str[$i]); |
1168 | - if($b < 128 || $b > 191) return false; |
|
1186 | + if($b < 128 || $b > 191) { |
|
1187 | + return false; |
|
1188 | + } |
|
1169 | 1189 | $bits--; |
1170 | 1190 | } |
1171 | 1191 | } |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | * @return array |
230 | 230 | */ |
231 | 231 | public function __sleep() { |
232 | - return array('options','objectFilters','enabled'); |
|
232 | + return array('options', 'objectFilters', 'enabled'); |
|
233 | 233 | } |
234 | 234 | |
235 | 235 | /** |
@@ -238,8 +238,8 @@ discard block |
||
238 | 238 | * @param boolean $AutoCreate |
239 | 239 | * @return FirePHP |
240 | 240 | */ |
241 | - public static function getInstance($AutoCreate=false) { |
|
242 | - if($AutoCreate===true && !self::$instance) { |
|
241 | + public static function getInstance($AutoCreate = false) { |
|
242 | + if ($AutoCreate === true && !self::$instance) { |
|
243 | 243 | self::init(); |
244 | 244 | } |
245 | 245 | return self::$instance; |
@@ -299,7 +299,7 @@ discard block |
||
299 | 299 | * @return void |
300 | 300 | */ |
301 | 301 | public function setOptions($Options) { |
302 | - $this->options = array_merge($this->options,$Options); |
|
302 | + $this->options = array_merge($this->options, $Options); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | /** |
@@ -318,7 +318,7 @@ discard block |
||
318 | 318 | * |
319 | 319 | * @return mixed Returns a string containing the previously defined error handler (if any) |
320 | 320 | */ |
321 | - public function registerErrorHandler($throwErrorExceptions=true) |
|
321 | + public function registerErrorHandler($throwErrorExceptions = true) |
|
322 | 322 | { |
323 | 323 | //NOTE: The following errors will not be caught by this error handler: |
324 | 324 | // E_ERROR, E_PARSE, E_CORE_ERROR, |
@@ -327,7 +327,7 @@ discard block |
||
327 | 327 | |
328 | 328 | $this->throwErrorExceptions = $throwErrorExceptions; |
329 | 329 | |
330 | - return set_error_handler(array($this,'errorHandler')); |
|
330 | + return set_error_handler(array($this, 'errorHandler')); |
|
331 | 331 | } |
332 | 332 | |
333 | 333 | /** |
@@ -351,7 +351,7 @@ discard block |
||
351 | 351 | if (error_reporting() & $errno) { |
352 | 352 | |
353 | 353 | $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline); |
354 | - if($this->throwErrorExceptions) { |
|
354 | + if ($this->throwErrorExceptions) { |
|
355 | 355 | throw $exception; |
356 | 356 | } else { |
357 | 357 | $this->fb($exception); |
@@ -368,7 +368,7 @@ discard block |
||
368 | 368 | */ |
369 | 369 | public function registerExceptionHandler() |
370 | 370 | { |
371 | - return set_exception_handler(array($this,'exceptionHandler')); |
|
371 | + return set_exception_handler(array($this, 'exceptionHandler')); |
|
372 | 372 | } |
373 | 373 | |
374 | 374 | /** |
@@ -397,12 +397,12 @@ discard block |
||
397 | 397 | * @param boolean $throwAssertionExceptions |
398 | 398 | * @return mixed Returns the original setting or FALSE on errors |
399 | 399 | */ |
400 | - public function registerAssertionHandler($convertAssertionErrorsToExceptions=true, $throwAssertionExceptions=false) |
|
400 | + public function registerAssertionHandler($convertAssertionErrorsToExceptions = true, $throwAssertionExceptions = false) |
|
401 | 401 | { |
402 | 402 | $this->convertAssertionErrorsToExceptions = $convertAssertionErrorsToExceptions; |
403 | 403 | $this->throwAssertionExceptions = $throwAssertionExceptions; |
404 | 404 | |
405 | - if($throwAssertionExceptions && !$convertAssertionErrorsToExceptions) { |
|
405 | + if ($throwAssertionExceptions && !$convertAssertionErrorsToExceptions) { |
|
406 | 406 | throw $this->newException('Cannot throw assertion exceptions as assertion errors are not being converted to exceptions!'); |
407 | 407 | } |
408 | 408 | |
@@ -421,11 +421,11 @@ discard block |
||
421 | 421 | public function assertionHandler($file, $line, $code) |
422 | 422 | { |
423 | 423 | |
424 | - if($this->convertAssertionErrorsToExceptions) { |
|
424 | + if ($this->convertAssertionErrorsToExceptions) { |
|
425 | 425 | |
426 | 426 | $exception = new ErrorException('Assertion Failed - Code[ '.$code.' ]', 0, null, $file, $line); |
427 | 427 | |
428 | - if($this->throwAssertionExceptions) { |
|
428 | + if ($this->throwAssertionExceptions) { |
|
429 | 429 | throw $exception; |
430 | 430 | } else { |
431 | 431 | $this->fb($exception); |
@@ -433,7 +433,7 @@ discard block |
||
433 | 433 | |
434 | 434 | } else { |
435 | 435 | |
436 | - $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File'=>$file,'Line'=>$line)); |
|
436 | + $this->fb($code, 'Assertion Failed', FirePHP::ERROR, array('File'=>$file, 'Line'=>$line)); |
|
437 | 437 | |
438 | 438 | } |
439 | 439 | } |
@@ -470,18 +470,18 @@ discard block |
||
470 | 470 | * @return true |
471 | 471 | * @throws Exception |
472 | 472 | */ |
473 | - public function group($Name, $Options=null) { |
|
473 | + public function group($Name, $Options = null) { |
|
474 | 474 | |
475 | - if(!$Name) { |
|
475 | + if (!$Name) { |
|
476 | 476 | throw $this->newException('You must specify a label for the group!'); |
477 | 477 | } |
478 | 478 | |
479 | - if($Options) { |
|
480 | - if(!is_array($Options)) { |
|
479 | + if ($Options) { |
|
480 | + if (!is_array($Options)) { |
|
481 | 481 | throw $this->newException('Options must be defined as an array!'); |
482 | 482 | } |
483 | - if(array_key_exists('Collapsed', $Options)) { |
|
484 | - $Options['Collapsed'] = ($Options['Collapsed'])?'true':'false'; |
|
483 | + if (array_key_exists('Collapsed', $Options)) { |
|
484 | + $Options['Collapsed'] = ($Options['Collapsed']) ? 'true' : 'false'; |
|
485 | 485 | } |
486 | 486 | } |
487 | 487 | |
@@ -507,7 +507,7 @@ discard block |
||
507 | 507 | * @return true |
508 | 508 | * @throws Exception |
509 | 509 | */ |
510 | - public function log($Object, $Label=null) { |
|
510 | + public function log($Object, $Label = null) { |
|
511 | 511 | return $this->fb($Object, $Label, FirePHP::LOG); |
512 | 512 | } |
513 | 513 | |
@@ -520,7 +520,7 @@ discard block |
||
520 | 520 | * @return true |
521 | 521 | * @throws Exception |
522 | 522 | */ |
523 | - public function info($Object, $Label=null) { |
|
523 | + public function info($Object, $Label = null) { |
|
524 | 524 | return $this->fb($Object, $Label, FirePHP::INFO); |
525 | 525 | } |
526 | 526 | |
@@ -533,7 +533,7 @@ discard block |
||
533 | 533 | * @return true |
534 | 534 | * @throws Exception |
535 | 535 | */ |
536 | - public function warn($Object, $Label=null) { |
|
536 | + public function warn($Object, $Label = null) { |
|
537 | 537 | return $this->fb($Object, $Label, FirePHP::WARN); |
538 | 538 | } |
539 | 539 | |
@@ -546,7 +546,7 @@ discard block |
||
546 | 546 | * @return true |
547 | 547 | * @throws Exception |
548 | 548 | */ |
549 | - public function error($Object, $Label=null) { |
|
549 | + public function error($Object, $Label = null) { |
|
550 | 550 | return $this->fb($Object, $Label, FirePHP::ERROR); |
551 | 551 | } |
552 | 552 | |
@@ -595,8 +595,8 @@ discard block |
||
595 | 595 | */ |
596 | 596 | public function detectClientExtension() { |
597 | 597 | /* Check if FirePHP is installed on client */ |
598 | - if(!@preg_match_all('/\sFirePHP\/([\.|\d]*)\s?/si',$this->getUserAgent(),$m) || |
|
599 | - !version_compare($m[1][0],'0.0.6','>=')) { |
|
598 | + if (!@preg_match_all('/\sFirePHP\/([\.|\d]*)\s?/si', $this->getUserAgent(), $m) || |
|
599 | + !version_compare($m[1][0], '0.0.6', '>=')) { |
|
600 | 600 | return false; |
601 | 601 | } |
602 | 602 | return true; |
@@ -612,13 +612,13 @@ discard block |
||
612 | 612 | */ |
613 | 613 | public function fb($Object) { |
614 | 614 | |
615 | - if(!$this->enabled) { |
|
615 | + if (!$this->enabled) { |
|
616 | 616 | return false; |
617 | 617 | } |
618 | 618 | |
619 | 619 | if (headers_sent($filename, $linenum)) { |
620 | 620 | // If we are logging from within the exception handler we cannot throw another exception |
621 | - if($this->inExceptionHandler) { |
|
621 | + if ($this->inExceptionHandler) { |
|
622 | 622 | // Simply echo the error out to the page |
623 | 623 | echo '<div style="border: 2px solid red; font-family: Arial; font-size: 12px; background-color: lightgray; padding: 5px;"><span style="color: red; font-weight: bold;">FirePHP ERROR:</span> Headers already sent in <b>'.$filename.'</b> on line <b>'.$linenum.'</b>. Cannot send log data to FirePHP. You must have Output Buffering enabled via ob_start() or output_buffering ini directive.</div>'; |
624 | 624 | } else { |
@@ -630,10 +630,10 @@ discard block |
||
630 | 630 | $Label = null; |
631 | 631 | $Options = array(); |
632 | 632 | |
633 | - if(func_num_args()==1) { |
|
633 | + if (func_num_args() == 1) { |
|
634 | 634 | } else |
635 | - if(func_num_args()==2) { |
|
636 | - switch(func_get_arg(1)) { |
|
635 | + if (func_num_args() == 2) { |
|
636 | + switch (func_get_arg(1)) { |
|
637 | 637 | case self::LOG: |
638 | 638 | case self::INFO: |
639 | 639 | case self::WARN: |
@@ -651,11 +651,11 @@ discard block |
||
651 | 651 | break; |
652 | 652 | } |
653 | 653 | } else |
654 | - if(func_num_args()==3) { |
|
654 | + if (func_num_args() == 3) { |
|
655 | 655 | $Type = func_get_arg(2); |
656 | 656 | $Label = func_get_arg(1); |
657 | 657 | } else |
658 | - if(func_num_args()==4) { |
|
658 | + if (func_num_args() == 4) { |
|
659 | 659 | $Type = func_get_arg(2); |
660 | 660 | $Label = func_get_arg(1); |
661 | 661 | $Options = func_get_arg(3); |
@@ -664,27 +664,27 @@ discard block |
||
664 | 664 | } |
665 | 665 | |
666 | 666 | |
667 | - if(!$this->detectClientExtension()) { |
|
667 | + if (!$this->detectClientExtension()) { |
|
668 | 668 | return false; |
669 | 669 | } |
670 | 670 | |
671 | 671 | $meta = array(); |
672 | 672 | $skipFinalObjectEncode = false; |
673 | 673 | |
674 | - if($Object instanceof Exception) { |
|
674 | + if ($Object instanceof Exception) { |
|
675 | 675 | |
676 | 676 | $meta['file'] = $this->_escapeTraceFile($Object->getFile()); |
677 | 677 | $meta['line'] = $Object->getLine(); |
678 | 678 | |
679 | 679 | $trace = $Object->getTrace(); |
680 | - if($Object instanceof ErrorException |
|
680 | + if ($Object instanceof ErrorException |
|
681 | 681 | && isset($trace[0]['function']) |
682 | - && $trace[0]['function']=='errorHandler' |
|
682 | + && $trace[0]['function'] == 'errorHandler' |
|
683 | 683 | && isset($trace[0]['class']) |
684 | - && $trace[0]['class']=='FirePHP') { |
|
684 | + && $trace[0]['class'] == 'FirePHP') { |
|
685 | 685 | |
686 | 686 | $severity = false; |
687 | - switch($Object->getSeverity()) { |
|
687 | + switch ($Object->getSeverity()) { |
|
688 | 688 | case E_WARNING: $severity = 'E_WARNING'; break; |
689 | 689 | case E_NOTICE: $severity = 'E_NOTICE'; break; |
690 | 690 | case E_USER_ERROR: $severity = 'E_USER_ERROR'; break; |
@@ -701,7 +701,7 @@ discard block |
||
701 | 701 | 'File'=>$this->_escapeTraceFile($Object->getFile()), |
702 | 702 | 'Line'=>$Object->getLine(), |
703 | 703 | 'Type'=>'trigger', |
704 | - 'Trace'=>$this->_escapeTrace(array_splice($trace,2))); |
|
704 | + 'Trace'=>$this->_escapeTrace(array_splice($trace, 2))); |
|
705 | 705 | $skipFinalObjectEncode = true; |
706 | 706 | } else { |
707 | 707 | $Object = array('Class'=>get_class($Object), |
@@ -715,49 +715,49 @@ discard block |
||
715 | 715 | $Type = self::EXCEPTION; |
716 | 716 | |
717 | 717 | } else |
718 | - if($Type==self::TRACE) { |
|
718 | + if ($Type == self::TRACE) { |
|
719 | 719 | |
720 | 720 | $trace = debug_backtrace(); |
721 | - if(!$trace) return false; |
|
722 | - for( $i=0 ; $i<sizeof($trace) ; $i++ ) { |
|
721 | + if (!$trace) return false; |
|
722 | + for ($i = 0; $i < sizeof($trace); $i++) { |
|
723 | 723 | |
724 | - if(isset($trace[$i]['class']) |
|
724 | + if (isset($trace[$i]['class']) |
|
725 | 725 | && isset($trace[$i]['file']) |
726 | - && ($trace[$i]['class']=='FirePHP' |
|
727 | - || $trace[$i]['class']=='FB') |
|
728 | - && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' |
|
729 | - || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { |
|
726 | + && ($trace[$i]['class'] == 'FirePHP' |
|
727 | + || $trace[$i]['class'] == 'FB') |
|
728 | + && (substr($this->_standardizePath($trace[$i]['file']), -18, 18) == 'FirePHPCore/fb.php' |
|
729 | + || substr($this->_standardizePath($trace[$i]['file']), -29, 29) == 'FirePHPCore/FirePHP.class.php')) { |
|
730 | 730 | /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ |
731 | 731 | } else |
732 | - if(isset($trace[$i]['class']) |
|
733 | - && isset($trace[$i+1]['file']) |
|
734 | - && $trace[$i]['class']=='FirePHP' |
|
735 | - && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
732 | + if (isset($trace[$i]['class']) |
|
733 | + && isset($trace[$i + 1]['file']) |
|
734 | + && $trace[$i]['class'] == 'FirePHP' |
|
735 | + && substr($this->_standardizePath($trace[$i + 1]['file']), -18, 18) == 'FirePHPCore/fb.php') { |
|
736 | 736 | /* Skip fb() */ |
737 | 737 | } else |
738 | - if($trace[$i]['function']=='fb' |
|
739 | - || $trace[$i]['function']=='trace' |
|
740 | - || $trace[$i]['function']=='send') { |
|
741 | - $Object = array('Class'=>isset($trace[$i]['class'])?$trace[$i]['class']:'', |
|
742 | - 'Type'=>isset($trace[$i]['type'])?$trace[$i]['type']:'', |
|
743 | - 'Function'=>isset($trace[$i]['function'])?$trace[$i]['function']:'', |
|
738 | + if ($trace[$i]['function'] == 'fb' |
|
739 | + || $trace[$i]['function'] == 'trace' |
|
740 | + || $trace[$i]['function'] == 'send') { |
|
741 | + $Object = array('Class'=>isset($trace[$i]['class']) ? $trace[$i]['class'] : '', |
|
742 | + 'Type'=>isset($trace[$i]['type']) ? $trace[$i]['type'] : '', |
|
743 | + 'Function'=>isset($trace[$i]['function']) ? $trace[$i]['function'] : '', |
|
744 | 744 | 'Message'=>$trace[$i]['args'][0], |
745 | - 'File'=>isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):'', |
|
746 | - 'Line'=>isset($trace[$i]['line'])?$trace[$i]['line']:'', |
|
747 | - 'Args'=>isset($trace[$i]['args'])?$this->encodeObject($trace[$i]['args']):'', |
|
748 | - 'Trace'=>$this->_escapeTrace(array_splice($trace,$i+1))); |
|
745 | + 'File'=>isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : '', |
|
746 | + 'Line'=>isset($trace[$i]['line']) ? $trace[$i]['line'] : '', |
|
747 | + 'Args'=>isset($trace[$i]['args']) ? $this->encodeObject($trace[$i]['args']) : '', |
|
748 | + 'Trace'=>$this->_escapeTrace(array_splice($trace, $i + 1))); |
|
749 | 749 | |
750 | 750 | $skipFinalObjectEncode = true; |
751 | - $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; |
|
752 | - $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; |
|
751 | + $meta['file'] = isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : ''; |
|
752 | + $meta['line'] = isset($trace[$i]['line']) ? $trace[$i]['line'] : ''; |
|
753 | 753 | break; |
754 | 754 | } |
755 | 755 | } |
756 | 756 | |
757 | 757 | } else |
758 | - if($Type==self::TABLE) { |
|
758 | + if ($Type == self::TABLE) { |
|
759 | 759 | |
760 | - if(isset($Object[0]) && is_string($Object[0])) { |
|
760 | + if (isset($Object[0]) && is_string($Object[0])) { |
|
761 | 761 | $Object[1] = $this->encodeTable($Object[1]); |
762 | 762 | } else { |
763 | 763 | $Object = $this->encodeTable($Object); |
@@ -766,44 +766,44 @@ discard block |
||
766 | 766 | $skipFinalObjectEncode = true; |
767 | 767 | |
768 | 768 | } else |
769 | - if($Type==self::GROUP_START) { |
|
769 | + if ($Type == self::GROUP_START) { |
|
770 | 770 | |
771 | - if(!$Label) { |
|
771 | + if (!$Label) { |
|
772 | 772 | throw $this->newException('You must specify a label for the group!'); |
773 | 773 | } |
774 | 774 | |
775 | 775 | } else { |
776 | - if($Type===null) { |
|
776 | + if ($Type === null) { |
|
777 | 777 | $Type = self::LOG; |
778 | 778 | } |
779 | 779 | } |
780 | 780 | |
781 | - if($this->options['includeLineNumbers']) { |
|
782 | - if(!isset($meta['file']) || !isset($meta['line'])) { |
|
781 | + if ($this->options['includeLineNumbers']) { |
|
782 | + if (!isset($meta['file']) || !isset($meta['line'])) { |
|
783 | 783 | |
784 | 784 | $trace = debug_backtrace(); |
785 | - for( $i=0 ; $trace && $i<sizeof($trace) ; $i++ ) { |
|
785 | + for ($i = 0; $trace && $i < sizeof($trace); $i++) { |
|
786 | 786 | |
787 | - if(isset($trace[$i]['class']) |
|
787 | + if (isset($trace[$i]['class']) |
|
788 | 788 | && isset($trace[$i]['file']) |
789 | - && ($trace[$i]['class']=='FirePHP' |
|
790 | - || $trace[$i]['class']=='FB') |
|
791 | - && (substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php' |
|
792 | - || substr($this->_standardizePath($trace[$i]['file']),-29,29)=='FirePHPCore/FirePHP.class.php')) { |
|
789 | + && ($trace[$i]['class'] == 'FirePHP' |
|
790 | + || $trace[$i]['class'] == 'FB') |
|
791 | + && (substr($this->_standardizePath($trace[$i]['file']), -18, 18) == 'FirePHPCore/fb.php' |
|
792 | + || substr($this->_standardizePath($trace[$i]['file']), -29, 29) == 'FirePHPCore/FirePHP.class.php')) { |
|
793 | 793 | /* Skip - FB::trace(), FB::send(), $firephp->trace(), $firephp->fb() */ |
794 | 794 | } else |
795 | - if(isset($trace[$i]['class']) |
|
796 | - && isset($trace[$i+1]['file']) |
|
797 | - && $trace[$i]['class']=='FirePHP' |
|
798 | - && substr($this->_standardizePath($trace[$i+1]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
795 | + if (isset($trace[$i]['class']) |
|
796 | + && isset($trace[$i + 1]['file']) |
|
797 | + && $trace[$i]['class'] == 'FirePHP' |
|
798 | + && substr($this->_standardizePath($trace[$i + 1]['file']), -18, 18) == 'FirePHPCore/fb.php') { |
|
799 | 799 | /* Skip fb() */ |
800 | 800 | } else |
801 | - if(isset($trace[$i]['file']) |
|
802 | - && substr($this->_standardizePath($trace[$i]['file']),-18,18)=='FirePHPCore/fb.php') { |
|
801 | + if (isset($trace[$i]['file']) |
|
802 | + && substr($this->_standardizePath($trace[$i]['file']), -18, 18) == 'FirePHPCore/fb.php') { |
|
803 | 803 | /* Skip FB::fb() */ |
804 | 804 | } else { |
805 | - $meta['file'] = isset($trace[$i]['file'])?$this->_escapeTraceFile($trace[$i]['file']):''; |
|
806 | - $meta['line'] = isset($trace[$i]['line'])?$trace[$i]['line']:''; |
|
805 | + $meta['file'] = isset($trace[$i]['file']) ? $this->_escapeTraceFile($trace[$i]['file']) : ''; |
|
806 | + $meta['line'] = isset($trace[$i]['line']) ? $trace[$i]['line'] : ''; |
|
807 | 807 | break; |
808 | 808 | } |
809 | 809 | } |
@@ -814,50 +814,50 @@ discard block |
||
814 | 814 | unset($meta['line']); |
815 | 815 | } |
816 | 816 | |
817 | - $this->setHeader('X-Wf-Protocol-1','http://meta.wildfirehq.org/Protocol/JsonStream/0.2'); |
|
818 | - $this->setHeader('X-Wf-1-Plugin-1','http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/'.self::VERSION); |
|
817 | + $this->setHeader('X-Wf-Protocol-1', 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2'); |
|
818 | + $this->setHeader('X-Wf-1-Plugin-1', 'http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/'.self::VERSION); |
|
819 | 819 | |
820 | 820 | $structure_index = 1; |
821 | - if($Type==self::DUMP) { |
|
821 | + if ($Type == self::DUMP) { |
|
822 | 822 | $structure_index = 2; |
823 | - $this->setHeader('X-Wf-1-Structure-2','http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1'); |
|
823 | + $this->setHeader('X-Wf-1-Structure-2', 'http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1'); |
|
824 | 824 | } else { |
825 | - $this->setHeader('X-Wf-1-Structure-1','http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1'); |
|
825 | + $this->setHeader('X-Wf-1-Structure-1', 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1'); |
|
826 | 826 | } |
827 | 827 | |
828 | - if($Type==self::DUMP) { |
|
828 | + if ($Type == self::DUMP) { |
|
829 | 829 | $msg = '{"'.$Label.'":'.$this->jsonEncode($Object, $skipFinalObjectEncode).'}'; |
830 | 830 | } else { |
831 | 831 | $msg_meta = $Options; |
832 | 832 | $msg_meta['Type'] = $Type; |
833 | - if($Label!==null) { |
|
833 | + if ($Label !== null) { |
|
834 | 834 | $msg_meta['Label'] = $Label; |
835 | 835 | } |
836 | - if(isset($meta['file']) && !isset($msg_meta['File'])) { |
|
836 | + if (isset($meta['file']) && !isset($msg_meta['File'])) { |
|
837 | 837 | $msg_meta['File'] = $meta['file']; |
838 | 838 | } |
839 | - if(isset($meta['line']) && !isset($msg_meta['Line'])) { |
|
839 | + if (isset($meta['line']) && !isset($msg_meta['Line'])) { |
|
840 | 840 | $msg_meta['Line'] = $meta['line']; |
841 | 841 | } |
842 | 842 | $msg = '['.$this->jsonEncode($msg_meta).','.$this->jsonEncode($Object, $skipFinalObjectEncode).']'; |
843 | 843 | } |
844 | 844 | |
845 | - $parts = explode("\n",chunk_split($msg, 5000, "\n")); |
|
845 | + $parts = explode("\n", chunk_split($msg, 5000, "\n")); |
|
846 | 846 | |
847 | - for( $i=0 ; $i<count($parts) ; $i++) { |
|
847 | + for ($i = 0; $i < count($parts); $i++) { |
|
848 | 848 | |
849 | 849 | $part = $parts[$i]; |
850 | 850 | if ($part) { |
851 | 851 | |
852 | - if(count($parts)>2) { |
|
852 | + if (count($parts) > 2) { |
|
853 | 853 | // Message needs to be split into multiple parts |
854 | 854 | $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, |
855 | - (($i==0)?strlen($msg):'') |
|
856 | - . '|' . $part . '|' |
|
857 | - . (($i<count($parts)-2)?'\\':'')); |
|
855 | + (($i == 0) ?strlen($msg) : '') |
|
856 | + . '|'.$part.'|' |
|
857 | + . (($i < count($parts) - 2) ? '\\' : '')); |
|
858 | 858 | } else { |
859 | 859 | $this->setHeader('X-Wf-1-'.$structure_index.'-'.'1-'.$this->messageIndex, |
860 | - strlen($part) . '|' . $part . '|'); |
|
860 | + strlen($part).'|'.$part.'|'); |
|
861 | 861 | } |
862 | 862 | |
863 | 863 | $this->messageIndex++; |
@@ -868,7 +868,7 @@ discard block |
||
868 | 868 | } |
869 | 869 | } |
870 | 870 | |
871 | - $this->setHeader('X-Wf-1-Index',$this->messageIndex-1); |
|
871 | + $this->setHeader('X-Wf-1-Index', $this->messageIndex - 1); |
|
872 | 872 | |
873 | 873 | return true; |
874 | 874 | } |
@@ -880,7 +880,7 @@ discard block |
||
880 | 880 | * @return string |
881 | 881 | */ |
882 | 882 | protected function _standardizePath($Path) { |
883 | - return preg_replace('/\\\\+/','/',$Path); |
|
883 | + return preg_replace('/\\\\+/', '/', $Path); |
|
884 | 884 | } |
885 | 885 | |
886 | 886 | /** |
@@ -890,12 +890,12 @@ discard block |
||
890 | 890 | * @return array |
891 | 891 | */ |
892 | 892 | protected function _escapeTrace($Trace) { |
893 | - if(!$Trace) return $Trace; |
|
894 | - for( $i=0 ; $i<sizeof($Trace) ; $i++ ) { |
|
895 | - if(isset($Trace[$i]['file'])) { |
|
893 | + if (!$Trace) return $Trace; |
|
894 | + for ($i = 0; $i < sizeof($Trace); $i++) { |
|
895 | + if (isset($Trace[$i]['file'])) { |
|
896 | 896 | $Trace[$i]['file'] = $this->_escapeTraceFile($Trace[$i]['file']); |
897 | 897 | } |
898 | - if(isset($Trace[$i]['args'])) { |
|
898 | + if (isset($Trace[$i]['args'])) { |
|
899 | 899 | $Trace[$i]['args'] = $this->encodeObject($Trace[$i]['args']); |
900 | 900 | } |
901 | 901 | } |
@@ -910,10 +910,10 @@ discard block |
||
910 | 910 | */ |
911 | 911 | protected function _escapeTraceFile($File) { |
912 | 912 | /* Check if we have a windows filepath */ |
913 | - if(strpos($File,'\\')) { |
|
913 | + if (strpos($File, '\\')) { |
|
914 | 914 | /* First strip down to single \ */ |
915 | 915 | |
916 | - $file = preg_replace('/\\\\+/','\\',$File); |
|
916 | + $file = preg_replace('/\\\\+/', '\\', $File); |
|
917 | 917 | |
918 | 918 | return $file; |
919 | 919 | } |
@@ -936,7 +936,7 @@ discard block |
||
936 | 936 | * @return string|false |
937 | 937 | */ |
938 | 938 | protected function getUserAgent() { |
939 | - if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; |
|
939 | + if (!isset($_SERVER['HTTP_USER_AGENT'])) return false; |
|
940 | 940 | return $_SERVER['HTTP_USER_AGENT']; |
941 | 941 | } |
942 | 942 | |
@@ -958,14 +958,14 @@ discard block |
||
958 | 958 | * @param object $Object The object to be encoded |
959 | 959 | * @return string The JSON string |
960 | 960 | */ |
961 | - public function jsonEncode($Object, $skipObjectEncode=false) |
|
961 | + public function jsonEncode($Object, $skipObjectEncode = false) |
|
962 | 962 | { |
963 | - if(!$skipObjectEncode) { |
|
963 | + if (!$skipObjectEncode) { |
|
964 | 964 | $Object = $this->encodeObject($Object); |
965 | 965 | } |
966 | 966 | |
967 | - if(function_exists('json_encode') |
|
968 | - && $this->options['useNativeJsonEncode']!=false) { |
|
967 | + if (function_exists('json_encode') |
|
968 | + && $this->options['useNativeJsonEncode'] != false) { |
|
969 | 969 | |
970 | 970 | return json_encode($Object); |
971 | 971 | } else { |
@@ -981,15 +981,15 @@ discard block |
||
981 | 981 | */ |
982 | 982 | protected function encodeTable($Table) { |
983 | 983 | |
984 | - if(!$Table) return $Table; |
|
984 | + if (!$Table) return $Table; |
|
985 | 985 | |
986 | 986 | $new_table = array(); |
987 | - foreach($Table as $row) { |
|
987 | + foreach ($Table as $row) { |
|
988 | 988 | |
989 | - if(is_array($row)) { |
|
989 | + if (is_array($row)) { |
|
990 | 990 | $new_row = array(); |
991 | 991 | |
992 | - foreach($row as $item) { |
|
992 | + foreach ($row as $item) { |
|
993 | 993 | $new_row[] = $this->encodeObject($item); |
994 | 994 | } |
995 | 995 | |
@@ -1014,7 +1014,7 @@ discard block |
||
1014 | 1014 | |
1015 | 1015 | if (is_resource($Object)) { |
1016 | 1016 | |
1017 | - return '** '.(string)$Object.' **'; |
|
1017 | + return '** '.(string) $Object.' **'; |
|
1018 | 1018 | |
1019 | 1019 | } else |
1020 | 1020 | if (is_object($Object)) { |
@@ -1035,45 +1035,45 @@ discard block |
||
1035 | 1035 | |
1036 | 1036 | $reflectionClass = new ReflectionClass($class); |
1037 | 1037 | $properties = array(); |
1038 | - foreach( $reflectionClass->getProperties() as $property) { |
|
1038 | + foreach ($reflectionClass->getProperties() as $property) { |
|
1039 | 1039 | $properties[$property->getName()] = $property; |
1040 | 1040 | } |
1041 | 1041 | |
1042 | - $members = (array)$Object; |
|
1042 | + $members = (array) $Object; |
|
1043 | 1043 | |
1044 | - foreach( $properties as $raw_name => $property ) { |
|
1044 | + foreach ($properties as $raw_name => $property) { |
|
1045 | 1045 | |
1046 | 1046 | $name = $raw_name; |
1047 | - if($property->isStatic()) { |
|
1047 | + if ($property->isStatic()) { |
|
1048 | 1048 | $name = 'static:'.$name; |
1049 | 1049 | } |
1050 | - if($property->isPublic()) { |
|
1050 | + if ($property->isPublic()) { |
|
1051 | 1051 | $name = 'public:'.$name; |
1052 | 1052 | } else |
1053 | - if($property->isPrivate()) { |
|
1053 | + if ($property->isPrivate()) { |
|
1054 | 1054 | $name = 'private:'.$name; |
1055 | 1055 | $raw_name = "\0".$class."\0".$raw_name; |
1056 | 1056 | } else |
1057 | - if($property->isProtected()) { |
|
1057 | + if ($property->isProtected()) { |
|
1058 | 1058 | $name = 'protected:'.$name; |
1059 | 1059 | $raw_name = "\0".'*'."\0".$raw_name; |
1060 | 1060 | } |
1061 | 1061 | |
1062 | - if(!(isset($this->objectFilters[$class_lower]) |
|
1062 | + if (!(isset($this->objectFilters[$class_lower]) |
|
1063 | 1063 | && is_array($this->objectFilters[$class_lower]) |
1064 | - && in_array($raw_name,$this->objectFilters[$class_lower]))) { |
|
1064 | + && in_array($raw_name, $this->objectFilters[$class_lower]))) { |
|
1065 | 1065 | |
1066 | - if(array_key_exists($raw_name,$members) |
|
1066 | + if (array_key_exists($raw_name, $members) |
|
1067 | 1067 | && !$property->isStatic()) { |
1068 | 1068 | |
1069 | 1069 | $return[$name] = $this->encodeObject($members[$raw_name], $ObjectDepth + 1, 1); |
1070 | 1070 | |
1071 | 1071 | } else { |
1072 | - if(method_exists($property,'setAccessible')) { |
|
1072 | + if (method_exists($property, 'setAccessible')) { |
|
1073 | 1073 | $property->setAccessible(true); |
1074 | 1074 | $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1); |
1075 | 1075 | } else |
1076 | - if($property->isPublic()) { |
|
1076 | + if ($property->isPublic()) { |
|
1077 | 1077 | $return[$name] = $this->encodeObject($property->getValue($Object), $ObjectDepth + 1, 1); |
1078 | 1078 | } else { |
1079 | 1079 | $return[$name] = '** Need PHP 5.3 to get value **'; |
@@ -1086,7 +1086,7 @@ discard block |
||
1086 | 1086 | |
1087 | 1087 | // Include all members that are not defined in the class |
1088 | 1088 | // but exist in the object |
1089 | - foreach( $members as $raw_name => $value ) { |
|
1089 | + foreach ($members as $raw_name => $value) { |
|
1090 | 1090 | |
1091 | 1091 | $name = $raw_name; |
1092 | 1092 | |
@@ -1095,12 +1095,12 @@ discard block |
||
1095 | 1095 | $name = $parts[2]; |
1096 | 1096 | } |
1097 | 1097 | |
1098 | - if(!isset($properties[$name])) { |
|
1098 | + if (!isset($properties[$name])) { |
|
1099 | 1099 | $name = 'undeclared:'.$name; |
1100 | 1100 | |
1101 | - if(!(isset($this->objectFilters[$class_lower]) |
|
1101 | + if (!(isset($this->objectFilters[$class_lower]) |
|
1102 | 1102 | && is_array($this->objectFilters[$class_lower]) |
1103 | - && in_array($raw_name,$this->objectFilters[$class_lower]))) { |
|
1103 | + && in_array($raw_name, $this->objectFilters[$class_lower]))) { |
|
1104 | 1104 | |
1105 | 1105 | $return[$name] = $this->encodeObject($value, $ObjectDepth + 1, 1); |
1106 | 1106 | } else { |
@@ -1123,16 +1123,16 @@ discard block |
||
1123 | 1123 | // if the recursion is not reset here as it contains |
1124 | 1124 | // a reference to itself. This is the only way I have come up |
1125 | 1125 | // with to stop infinite recursion in this case. |
1126 | - if($key=='GLOBALS' |
|
1126 | + if ($key == 'GLOBALS' |
|
1127 | 1127 | && is_array($val) |
1128 | - && array_key_exists('GLOBALS',$val)) { |
|
1128 | + && array_key_exists('GLOBALS', $val)) { |
|
1129 | 1129 | $val['GLOBALS'] = '** Recursion (GLOBALS) **'; |
1130 | 1130 | } |
1131 | 1131 | |
1132 | 1132 | $return[$key] = $this->encodeObject($val, 1, $ArrayDepth + 1); |
1133 | 1133 | } |
1134 | 1134 | } else { |
1135 | - if(self::is_utf8($Object)) { |
|
1135 | + if (self::is_utf8($Object)) { |
|
1136 | 1136 | return $Object; |
1137 | 1137 | } else { |
1138 | 1138 | return utf8_encode($Object); |
@@ -1148,24 +1148,24 @@ discard block |
||
1148 | 1148 | * @return boolean |
1149 | 1149 | */ |
1150 | 1150 | protected static function is_utf8($str) { |
1151 | - $c=0; $b=0; |
|
1152 | - $bits=0; |
|
1153 | - $len=strlen($str); |
|
1154 | - for($i=0; $i<$len; $i++){ |
|
1155 | - $c=ord($str[$i]); |
|
1156 | - if($c > 128){ |
|
1157 | - if(($c >= 254)) return false; |
|
1158 | - elseif($c >= 252) $bits=6; |
|
1159 | - elseif($c >= 248) $bits=5; |
|
1160 | - elseif($c >= 240) $bits=4; |
|
1161 | - elseif($c >= 224) $bits=3; |
|
1162 | - elseif($c >= 192) $bits=2; |
|
1151 | + $c = 0; $b = 0; |
|
1152 | + $bits = 0; |
|
1153 | + $len = strlen($str); |
|
1154 | + for ($i = 0; $i < $len; $i++) { |
|
1155 | + $c = ord($str[$i]); |
|
1156 | + if ($c > 128) { |
|
1157 | + if (($c >= 254)) return false; |
|
1158 | + elseif ($c >= 252) $bits = 6; |
|
1159 | + elseif ($c >= 248) $bits = 5; |
|
1160 | + elseif ($c >= 240) $bits = 4; |
|
1161 | + elseif ($c >= 224) $bits = 3; |
|
1162 | + elseif ($c >= 192) $bits = 2; |
|
1163 | 1163 | else return false; |
1164 | - if(($i+$bits) > $len) return false; |
|
1165 | - while($bits > 1){ |
|
1164 | + if (($i + $bits) > $len) return false; |
|
1165 | + while ($bits > 1) { |
|
1166 | 1166 | $i++; |
1167 | - $b=ord($str[$i]); |
|
1168 | - if($b < 128 || $b > 191) return false; |
|
1167 | + $b = ord($str[$i]); |
|
1168 | + if ($b < 128 || $b > 191) return false; |
|
1169 | 1169 | $bits--; |
1170 | 1170 | } |
1171 | 1171 | } |
@@ -1250,11 +1250,11 @@ discard block |
||
1250 | 1250 | private function json_utf82utf16($utf8) |
1251 | 1251 | { |
1252 | 1252 | // oh please oh please oh please oh please oh please |
1253 | - if(function_exists('mb_convert_encoding')) { |
|
1253 | + if (function_exists('mb_convert_encoding')) { |
|
1254 | 1254 | return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); |
1255 | 1255 | } |
1256 | 1256 | |
1257 | - switch(strlen($utf8)) { |
|
1257 | + switch (strlen($utf8)) { |
|
1258 | 1258 | case 1: |
1259 | 1259 | // this case should never be reached, because we are in ASCII range |
1260 | 1260 | // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 |
@@ -1294,8 +1294,8 @@ discard block |
||
1294 | 1294 | private function json_encode($var) |
1295 | 1295 | { |
1296 | 1296 | |
1297 | - if(is_object($var)) { |
|
1298 | - if(in_array($var,$this->json_objectStack)) { |
|
1297 | + if (is_object($var)) { |
|
1298 | + if (in_array($var, $this->json_objectStack)) { |
|
1299 | 1299 | return '"** Recursion **"'; |
1300 | 1300 | } |
1301 | 1301 | } |
@@ -1449,13 +1449,13 @@ discard block |
||
1449 | 1449 | |
1450 | 1450 | array_pop($this->json_objectStack); |
1451 | 1451 | |
1452 | - foreach($properties as $property) { |
|
1453 | - if($property instanceof Exception) { |
|
1452 | + foreach ($properties as $property) { |
|
1453 | + if ($property instanceof Exception) { |
|
1454 | 1454 | return $property; |
1455 | 1455 | } |
1456 | 1456 | } |
1457 | 1457 | |
1458 | - return '{' . join(',', $properties) . '}'; |
|
1458 | + return '{'.join(',', $properties).'}'; |
|
1459 | 1459 | } |
1460 | 1460 | |
1461 | 1461 | $this->json_objectStack[] = $var; |
@@ -1465,13 +1465,13 @@ discard block |
||
1465 | 1465 | |
1466 | 1466 | array_pop($this->json_objectStack); |
1467 | 1467 | |
1468 | - foreach($elements as $element) { |
|
1469 | - if($element instanceof Exception) { |
|
1468 | + foreach ($elements as $element) { |
|
1469 | + if ($element instanceof Exception) { |
|
1470 | 1470 | return $element; |
1471 | 1471 | } |
1472 | 1472 | } |
1473 | 1473 | |
1474 | - return '[' . join(',', $elements) . ']'; |
|
1474 | + return '['.join(',', $elements).']'; |
|
1475 | 1475 | |
1476 | 1476 | case 'object': |
1477 | 1477 | $vars = self::encodeObject($var); |
@@ -1484,13 +1484,13 @@ discard block |
||
1484 | 1484 | |
1485 | 1485 | array_pop($this->json_objectStack); |
1486 | 1486 | |
1487 | - foreach($properties as $property) { |
|
1488 | - if($property instanceof Exception) { |
|
1487 | + foreach ($properties as $property) { |
|
1488 | + if ($property instanceof Exception) { |
|
1489 | 1489 | return $property; |
1490 | 1490 | } |
1491 | 1491 | } |
1492 | 1492 | |
1493 | - return '{' . join(',', $properties) . '}'; |
|
1493 | + return '{'.join(',', $properties).'}'; |
|
1494 | 1494 | |
1495 | 1495 | default: |
1496 | 1496 | return null; |
@@ -1512,18 +1512,18 @@ discard block |
||
1512 | 1512 | // if the recursion is not reset here as it contains |
1513 | 1513 | // a reference to itself. This is the only way I have come up |
1514 | 1514 | // with to stop infinite recursion in this case. |
1515 | - if($name=='GLOBALS' |
|
1515 | + if ($name == 'GLOBALS' |
|
1516 | 1516 | && is_array($value) |
1517 | - && array_key_exists('GLOBALS',$value)) { |
|
1517 | + && array_key_exists('GLOBALS', $value)) { |
|
1518 | 1518 | $value['GLOBALS'] = '** Recursion **'; |
1519 | 1519 | } |
1520 | 1520 | |
1521 | 1521 | $encoded_value = $this->json_encode($value); |
1522 | 1522 | |
1523 | - if($encoded_value instanceof Exception) { |
|
1523 | + if ($encoded_value instanceof Exception) { |
|
1524 | 1524 | return $encoded_value; |
1525 | 1525 | } |
1526 | 1526 | |
1527 | - return $this->json_encode(strval($name)) . ':' . $encoded_value; |
|
1527 | + return $this->json_encode(strval($name)).':'.$encoded_value; |
|
1528 | 1528 | } |
1529 | 1529 | } |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | /** |
227 | 227 | * When the object gets serialized only include specific object members. |
228 | 228 | * |
229 | - * @return array |
|
229 | + * @return string[] |
|
230 | 230 | */ |
231 | 231 | public function __sleep() { |
232 | 232 | return array('options','objectFilters','enabled'); |
@@ -316,7 +316,7 @@ discard block |
||
316 | 316 | * |
317 | 317 | * Will throw exceptions for each php error. |
318 | 318 | * |
319 | - * @return mixed Returns a string containing the previously defined error handler (if any) |
|
319 | + * @return null|callable Returns a string containing the previously defined error handler (if any) |
|
320 | 320 | */ |
321 | 321 | public function registerErrorHandler($throwErrorExceptions=true) |
322 | 322 | { |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | /** |
363 | 363 | * Register FirePHP as your exception handler |
364 | 364 | * |
365 | - * @return mixed Returns the name of the previously defined exception handler, |
|
365 | + * @return callable Returns the name of the previously defined exception handler, |
|
366 | 366 | * or NULL on error. |
367 | 367 | * If no previous handler was defined, NULL is also returned. |
368 | 368 | */ |
@@ -467,7 +467,7 @@ discard block |
||
467 | 467 | * |
468 | 468 | * @param string $Name |
469 | 469 | * @param array $Options OPTIONAL Instructions on how to log the group |
470 | - * @return true |
|
470 | + * @return boolean |
|
471 | 471 | * @throws Exception |
472 | 472 | */ |
473 | 473 | public function group($Name, $Options=null) { |
@@ -491,7 +491,7 @@ discard block |
||
491 | 491 | /** |
492 | 492 | * Ends a group you have started before |
493 | 493 | * |
494 | - * @return true |
|
494 | + * @return boolean |
|
495 | 495 | * @throws Exception |
496 | 496 | */ |
497 | 497 | public function groupEnd() { |
@@ -504,7 +504,7 @@ discard block |
||
504 | 504 | * @see FirePHP::LOG |
505 | 505 | * @param mixes $Object |
506 | 506 | * @param string $Label |
507 | - * @return true |
|
507 | + * @return boolean |
|
508 | 508 | * @throws Exception |
509 | 509 | */ |
510 | 510 | public function log($Object, $Label=null) { |
@@ -517,7 +517,7 @@ discard block |
||
517 | 517 | * @see FirePHP::INFO |
518 | 518 | * @param mixes $Object |
519 | 519 | * @param string $Label |
520 | - * @return true |
|
520 | + * @return boolean |
|
521 | 521 | * @throws Exception |
522 | 522 | */ |
523 | 523 | public function info($Object, $Label=null) { |
@@ -530,7 +530,7 @@ discard block |
||
530 | 530 | * @see FirePHP::WARN |
531 | 531 | * @param mixes $Object |
532 | 532 | * @param string $Label |
533 | - * @return true |
|
533 | + * @return boolean |
|
534 | 534 | * @throws Exception |
535 | 535 | */ |
536 | 536 | public function warn($Object, $Label=null) { |
@@ -543,7 +543,7 @@ discard block |
||
543 | 543 | * @see FirePHP::ERROR |
544 | 544 | * @param mixes $Object |
545 | 545 | * @param string $Label |
546 | - * @return true |
|
546 | + * @return boolean |
|
547 | 547 | * @throws Exception |
548 | 548 | */ |
549 | 549 | public function error($Object, $Label=null) { |
@@ -556,7 +556,7 @@ discard block |
||
556 | 556 | * @see FirePHP::DUMP |
557 | 557 | * @param string $Key |
558 | 558 | * @param mixed $Variable |
559 | - * @return true |
|
559 | + * @return boolean |
|
560 | 560 | * @throws Exception |
561 | 561 | */ |
562 | 562 | public function dump($Key, $Variable) { |
@@ -568,7 +568,7 @@ discard block |
||
568 | 568 | * |
569 | 569 | * @see FirePHP::TRACE |
570 | 570 | * @param string $Label |
571 | - * @return true |
|
571 | + * @return boolean |
|
572 | 572 | * @throws Exception |
573 | 573 | */ |
574 | 574 | public function trace($Label) { |
@@ -581,7 +581,7 @@ discard block |
||
581 | 581 | * @see FirePHP::TABLE |
582 | 582 | * @param string $Label |
583 | 583 | * @param string $Table |
584 | - * @return true |
|
584 | + * @return boolean |
|
585 | 585 | * @throws Exception |
586 | 586 | */ |
587 | 587 | public function table($Label, $Table) { |
@@ -607,7 +607,7 @@ discard block |
||
607 | 607 | * |
608 | 608 | * @see http://www.firephp.org/Wiki/Reference/Fb |
609 | 609 | * @param mixed $Object The variable to be logged |
610 | - * @return true Return TRUE if message was added to headers, FALSE otherwise |
|
610 | + * @return boolean Return TRUE if message was added to headers, FALSE otherwise |
|
611 | 611 | * @throws Exception |
612 | 612 | */ |
613 | 613 | public function fb($Object) { |
@@ -1005,7 +1005,6 @@ discard block |
||
1005 | 1005 | * protected and private visibility |
1006 | 1006 | * |
1007 | 1007 | * @param BaseObject $Object The object to be encoded |
1008 | - * @param int $Depth The current traversal depth |
|
1009 | 1008 | * @return array All members of the object |
1010 | 1009 | */ |
1011 | 1010 | protected function encodeObject($Object, $ObjectDepth = 1, $ArrayDepth = 1) |
@@ -18,28 +18,28 @@ |
||
18 | 18 | } |
19 | 19 | |
20 | 20 | public function getResponseCode() { |
21 | - if($this->response) |
|
21 | + if ($this->response) |
|
22 | 22 | { |
23 | 23 | return $this->response->getStatus(); |
24 | 24 | } |
25 | 25 | } |
26 | 26 | |
27 | 27 | public function getResponseHeader() { |
28 | - if($this->response) |
|
28 | + if ($this->response) |
|
29 | 29 | { |
30 | 30 | return $this->response->getHeader(); |
31 | 31 | } |
32 | 32 | } |
33 | 33 | |
34 | 34 | public function getResponseBody() { |
35 | - if($this->response) |
|
35 | + if ($this->response) |
|
36 | 36 | { |
37 | 37 | return $this->response->getBody(); |
38 | 38 | } |
39 | 39 | } |
40 | 40 | |
41 | 41 | public function getResponseCookies() { |
42 | - if($this->response) |
|
42 | + if ($this->response) |
|
43 | 43 | { |
44 | 44 | return $this->response->getCookies(); |
45 | 45 | } |
@@ -41,990 +41,990 @@ |
||
41 | 41 | */ |
42 | 42 | class HTTP_Request2 implements SplSubject |
43 | 43 | { |
44 | - /**#@+ |
|
44 | + /**#@+ |
|
45 | 45 | * Constants for HTTP request methods |
46 | 46 | * |
47 | 47 | * @link http://tools.ietf.org/html/rfc2616#section-5.1.1 |
48 | 48 | */ |
49 | - const METHOD_OPTIONS = 'OPTIONS'; |
|
50 | - const METHOD_GET = 'GET'; |
|
51 | - const METHOD_HEAD = 'HEAD'; |
|
52 | - const METHOD_POST = 'POST'; |
|
53 | - const METHOD_PUT = 'PUT'; |
|
54 | - const METHOD_DELETE = 'DELETE'; |
|
55 | - const METHOD_TRACE = 'TRACE'; |
|
56 | - const METHOD_CONNECT = 'CONNECT'; |
|
57 | - /**#@-*/ |
|
58 | - |
|
59 | - /**#@+ |
|
49 | + const METHOD_OPTIONS = 'OPTIONS'; |
|
50 | + const METHOD_GET = 'GET'; |
|
51 | + const METHOD_HEAD = 'HEAD'; |
|
52 | + const METHOD_POST = 'POST'; |
|
53 | + const METHOD_PUT = 'PUT'; |
|
54 | + const METHOD_DELETE = 'DELETE'; |
|
55 | + const METHOD_TRACE = 'TRACE'; |
|
56 | + const METHOD_CONNECT = 'CONNECT'; |
|
57 | + /**#@-*/ |
|
58 | + |
|
59 | + /**#@+ |
|
60 | 60 | * Constants for HTTP authentication schemes |
61 | 61 | * |
62 | 62 | * @link http://tools.ietf.org/html/rfc2617 |
63 | 63 | */ |
64 | - const AUTH_BASIC = 'basic'; |
|
65 | - const AUTH_DIGEST = 'digest'; |
|
66 | - /**#@-*/ |
|
67 | - |
|
68 | - /** |
|
69 | - * Regular expression used to check for invalid symbols in RFC 2616 tokens |
|
70 | - * @link http://pear.php.net/bugs/bug.php?id=15630 |
|
71 | - */ |
|
72 | - const REGEXP_INVALID_TOKEN = '![\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]!'; |
|
73 | - |
|
74 | - /** |
|
75 | - * Regular expression used to check for invalid symbols in cookie strings |
|
76 | - * @link http://pear.php.net/bugs/bug.php?id=15630 |
|
77 | - * @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html |
|
78 | - */ |
|
79 | - const REGEXP_INVALID_COOKIE = '/[\s,;]/'; |
|
80 | - |
|
81 | - /** |
|
82 | - * Fileinfo magic database resource |
|
83 | - * @var resource |
|
84 | - * @see detectMimeType() |
|
85 | - */ |
|
86 | - private static $_fileinfoDb; |
|
87 | - |
|
88 | - /** |
|
89 | - * Observers attached to the request (instances of SplObserver) |
|
90 | - * @var array |
|
91 | - */ |
|
92 | - protected $observers = array(); |
|
93 | - |
|
94 | - /** |
|
95 | - * Request URL |
|
96 | - * @var Net_URL2 |
|
97 | - */ |
|
98 | - protected $url; |
|
99 | - |
|
100 | - /** |
|
101 | - * Request method |
|
102 | - * @var string |
|
103 | - */ |
|
104 | - protected $method = self::METHOD_GET; |
|
105 | - |
|
106 | - /** |
|
107 | - * Authentication data |
|
108 | - * @var array |
|
109 | - * @see getAuth() |
|
110 | - */ |
|
111 | - protected $auth; |
|
112 | - |
|
113 | - /** |
|
114 | - * Request headers |
|
115 | - * @var array |
|
116 | - */ |
|
117 | - protected $headers = array(); |
|
118 | - |
|
119 | - /** |
|
120 | - * Configuration parameters |
|
121 | - * @var array |
|
122 | - * @see setConfig() |
|
123 | - */ |
|
124 | - protected $config = array( |
|
125 | - 'adapter' => 'HTTP_Request2_Adapter_Socket', |
|
126 | - 'connect_timeout' => 10, |
|
127 | - 'timeout' => 0, |
|
128 | - 'use_brackets' => true, |
|
129 | - 'protocol_version' => '1.1', |
|
130 | - 'buffer_size' => 16384, |
|
131 | - 'store_body' => true, |
|
132 | - 'local_ip' => null, |
|
133 | - |
|
134 | - 'proxy_host' => '', |
|
135 | - 'proxy_port' => '', |
|
136 | - 'proxy_user' => '', |
|
137 | - 'proxy_password' => '', |
|
138 | - 'proxy_auth_scheme' => self::AUTH_BASIC, |
|
139 | - 'proxy_type' => 'http', |
|
140 | - |
|
141 | - 'ssl_verify_peer' => true, |
|
142 | - 'ssl_verify_host' => true, |
|
143 | - 'ssl_cafile' => null, |
|
144 | - 'ssl_capath' => null, |
|
145 | - 'ssl_local_cert' => null, |
|
146 | - 'ssl_passphrase' => null, |
|
147 | - |
|
148 | - 'digest_compat_ie' => false, |
|
149 | - |
|
150 | - 'follow_redirects' => false, |
|
151 | - 'max_redirects' => 5, |
|
152 | - 'strict_redirects' => false |
|
153 | - ); |
|
154 | - |
|
155 | - /** |
|
156 | - * Last event in request / response handling, intended for observers |
|
157 | - * @var array |
|
158 | - * @see getLastEvent() |
|
159 | - */ |
|
160 | - protected $lastEvent = array( |
|
161 | - 'name' => 'start', |
|
162 | - 'data' => null |
|
163 | - ); |
|
164 | - |
|
165 | - /** |
|
166 | - * Request body |
|
167 | - * @var string|resource |
|
168 | - * @see setBody() |
|
169 | - */ |
|
170 | - protected $body = ''; |
|
171 | - |
|
172 | - /** |
|
173 | - * Array of POST parameters |
|
174 | - * @var array |
|
175 | - */ |
|
176 | - protected $postParams = array(); |
|
177 | - |
|
178 | - /** |
|
179 | - * Array of file uploads (for multipart/form-data POST requests) |
|
180 | - * @var array |
|
181 | - */ |
|
182 | - protected $uploads = array(); |
|
183 | - |
|
184 | - /** |
|
185 | - * Adapter used to perform actual HTTP request |
|
186 | - * @var HTTP_Request2_Adapter |
|
187 | - */ |
|
188 | - protected $adapter; |
|
189 | - |
|
190 | - /** |
|
191 | - * Cookie jar to persist cookies between requests |
|
192 | - * @var HTTP_Request2_CookieJar |
|
193 | - */ |
|
194 | - protected $cookieJar = null; |
|
195 | - |
|
196 | - /** |
|
197 | - * Constructor. Can set request URL, method and configuration array. |
|
198 | - * |
|
199 | - * Also sets a default value for User-Agent header. |
|
200 | - * |
|
201 | - * @param string|Net_Url2 $url Request URL |
|
202 | - * @param string $method Request method |
|
203 | - * @param array $config Configuration for this Request instance |
|
204 | - */ |
|
205 | - public function __construct( |
|
206 | - $url = null, $method = self::METHOD_GET, array $config = array() |
|
207 | - ) { |
|
208 | - $this->setConfig($config); |
|
209 | - if (!empty($url)) { |
|
210 | - $this->setUrl($url); |
|
211 | - } |
|
212 | - if (!empty($method)) { |
|
213 | - $this->setMethod($method); |
|
214 | - } |
|
215 | - $this->setHeader( |
|
216 | - 'user-agent', 'HTTP_Request2/2.2.1 ' . |
|
217 | - '(http://pear.php.net/package/http_request2) PHP/' . phpversion() |
|
218 | - ); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Sets the URL for this request |
|
223 | - * |
|
224 | - * If the URL has userinfo part (username & password) these will be removed |
|
225 | - * and converted to auth data. If the URL does not have a path component, |
|
226 | - * that will be set to '/'. |
|
227 | - * |
|
228 | - * @param string|Net_URL2 $url Request URL |
|
229 | - * |
|
230 | - * @return HTTP_Request2 |
|
231 | - * @throws HTTP_Request2_LogicException |
|
232 | - */ |
|
233 | - public function setUrl($url) |
|
234 | - { |
|
235 | - if (is_string($url)) { |
|
236 | - $url = new Net_URL2( |
|
237 | - $url, array(Net_URL2::OPTION_USE_BRACKETS => $this->config['use_brackets']) |
|
238 | - ); |
|
239 | - } |
|
240 | - if (!$url instanceof Net_URL2) { |
|
241 | - throw new HTTP_Request2_LogicException( |
|
242 | - 'Parameter is not a valid HTTP URL', |
|
243 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
244 | - ); |
|
245 | - } |
|
246 | - // URL contains username / password? |
|
247 | - if ($url->getUserinfo()) { |
|
248 | - $username = $url->getUser(); |
|
249 | - $password = $url->getPassword(); |
|
250 | - $this->setAuth(rawurldecode($username), $password? rawurldecode($password): ''); |
|
251 | - $url->setUserinfo(''); |
|
252 | - } |
|
253 | - if ('' == $url->getPath()) { |
|
254 | - $url->setPath('/'); |
|
255 | - } |
|
256 | - $this->url = $url; |
|
257 | - |
|
258 | - return $this; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Returns the request URL |
|
263 | - * |
|
264 | - * @return Net_URL2 |
|
265 | - */ |
|
266 | - public function getUrl() |
|
267 | - { |
|
268 | - return $this->url; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Sets the request method |
|
273 | - * |
|
274 | - * @param string $method one of the methods defined in RFC 2616 |
|
275 | - * |
|
276 | - * @return HTTP_Request2 |
|
277 | - * @throws HTTP_Request2_LogicException if the method name is invalid |
|
278 | - */ |
|
279 | - public function setMethod($method) |
|
280 | - { |
|
281 | - // Method name should be a token: http://tools.ietf.org/html/rfc2616#section-5.1.1 |
|
282 | - if (preg_match(self::REGEXP_INVALID_TOKEN, $method)) { |
|
283 | - throw new HTTP_Request2_LogicException( |
|
284 | - "Invalid request method '{$method}'", |
|
285 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
286 | - ); |
|
287 | - } |
|
288 | - $this->method = $method; |
|
289 | - |
|
290 | - return $this; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Returns the request method |
|
295 | - * |
|
296 | - * @return string |
|
297 | - */ |
|
298 | - public function getMethod() |
|
299 | - { |
|
300 | - return $this->method; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Sets the configuration parameter(s) |
|
305 | - * |
|
306 | - * The following parameters are available: |
|
307 | - * <ul> |
|
308 | - * <li> 'adapter' - adapter to use (string)</li> |
|
309 | - * <li> 'connect_timeout' - Connection timeout in seconds (integer)</li> |
|
310 | - * <li> 'timeout' - Total number of seconds a request can take. |
|
311 | - * Use 0 for no limit, should be greater than |
|
312 | - * 'connect_timeout' if set (integer)</li> |
|
313 | - * <li> 'use_brackets' - Whether to append [] to array variable names (bool)</li> |
|
314 | - * <li> 'protocol_version' - HTTP Version to use, '1.0' or '1.1' (string)</li> |
|
315 | - * <li> 'buffer_size' - Buffer size to use for reading and writing (int)</li> |
|
316 | - * <li> 'store_body' - Whether to store response body in response object. |
|
317 | - * Set to false if receiving a huge response and |
|
318 | - * using an Observer to save it (boolean)</li> |
|
319 | - * <li> 'local_ip' - Specifies the IP address that will be used for accessing |
|
320 | - * the network (string)</li> |
|
321 | - * <li> 'proxy_type' - Proxy type, 'http' or 'socks5' (string)</li> |
|
322 | - * <li> 'proxy_host' - Proxy server host (string)</li> |
|
323 | - * <li> 'proxy_port' - Proxy server port (integer)</li> |
|
324 | - * <li> 'proxy_user' - Proxy auth username (string)</li> |
|
325 | - * <li> 'proxy_password' - Proxy auth password (string)</li> |
|
326 | - * <li> 'proxy_auth_scheme' - Proxy auth scheme, one of HTTP_Request2::AUTH_* constants (string)</li> |
|
327 | - * <li> 'proxy' - Shorthand for proxy_* parameters, proxy given as URL, |
|
328 | - * e.g. 'socks5://localhost:1080/' (string)</li> |
|
329 | - * <li> 'ssl_verify_peer' - Whether to verify peer's SSL certificate (bool)</li> |
|
330 | - * <li> 'ssl_verify_host' - Whether to check that Common Name in SSL |
|
331 | - * certificate matches host name (bool)</li> |
|
332 | - * <li> 'ssl_cafile' - Cerificate Authority file to verify the peer |
|
333 | - * with (use with 'ssl_verify_peer') (string)</li> |
|
334 | - * <li> 'ssl_capath' - Directory holding multiple Certificate |
|
335 | - * Authority files (string)</li> |
|
336 | - * <li> 'ssl_local_cert' - Name of a file containing local cerificate (string)</li> |
|
337 | - * <li> 'ssl_passphrase' - Passphrase with which local certificate |
|
338 | - * was encoded (string)</li> |
|
339 | - * <li> 'digest_compat_ie' - Whether to imitate behaviour of MSIE 5 and 6 |
|
340 | - * in using URL without query string in digest |
|
341 | - * authentication (boolean)</li> |
|
342 | - * <li> 'follow_redirects' - Whether to automatically follow HTTP Redirects (boolean)</li> |
|
343 | - * <li> 'max_redirects' - Maximum number of redirects to follow (integer)</li> |
|
344 | - * <li> 'strict_redirects' - Whether to keep request method on redirects via status 301 and |
|
345 | - * 302 (true, needed for compatibility with RFC 2616) |
|
346 | - * or switch to GET (false, needed for compatibility with most |
|
347 | - * browsers) (boolean)</li> |
|
348 | - * </ul> |
|
349 | - * |
|
350 | - * @param string|array $nameOrConfig configuration parameter name or array |
|
351 | - * ('parameter name' => 'parameter value') |
|
352 | - * @param mixed $value parameter value if $nameOrConfig is not an array |
|
353 | - * |
|
354 | - * @return HTTP_Request2 |
|
355 | - * @throws HTTP_Request2_LogicException If the parameter is unknown |
|
356 | - */ |
|
357 | - public function setConfig($nameOrConfig, $value = null) |
|
358 | - { |
|
359 | - if (is_array($nameOrConfig)) { |
|
360 | - foreach ($nameOrConfig as $name => $value) { |
|
361 | - $this->setConfig($name, $value); |
|
362 | - } |
|
363 | - |
|
364 | - } elseif ('proxy' == $nameOrConfig) { |
|
365 | - $url = new Net_URL2($value); |
|
366 | - $this->setConfig(array( |
|
367 | - 'proxy_type' => $url->getScheme(), |
|
368 | - 'proxy_host' => $url->getHost(), |
|
369 | - 'proxy_port' => $url->getPort(), |
|
370 | - 'proxy_user' => rawurldecode($url->getUser()), |
|
371 | - 'proxy_password' => rawurldecode($url->getPassword()) |
|
372 | - )); |
|
373 | - |
|
374 | - } else { |
|
375 | - if (!array_key_exists($nameOrConfig, $this->config)) { |
|
376 | - throw new HTTP_Request2_LogicException( |
|
377 | - "Unknown configuration parameter '{$nameOrConfig}'", |
|
378 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
379 | - ); |
|
380 | - } |
|
381 | - $this->config[$nameOrConfig] = $value; |
|
382 | - } |
|
383 | - |
|
384 | - return $this; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Returns the value(s) of the configuration parameter(s) |
|
389 | - * |
|
390 | - * @param string $name parameter name |
|
391 | - * |
|
392 | - * @return mixed value of $name parameter, array of all configuration |
|
393 | - * parameters if $name is not given |
|
394 | - * @throws HTTP_Request2_LogicException If the parameter is unknown |
|
395 | - */ |
|
396 | - public function getConfig($name = null) |
|
397 | - { |
|
398 | - if (null === $name) { |
|
399 | - return $this->config; |
|
400 | - } elseif (!array_key_exists($name, $this->config)) { |
|
401 | - throw new HTTP_Request2_LogicException( |
|
402 | - "Unknown configuration parameter '{$name}'", |
|
403 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
404 | - ); |
|
405 | - } |
|
406 | - return $this->config[$name]; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Sets the autentification data |
|
411 | - * |
|
412 | - * @param string $user user name |
|
413 | - * @param string $password password |
|
414 | - * @param string $scheme authentication scheme |
|
415 | - * |
|
416 | - * @return HTTP_Request2 |
|
417 | - */ |
|
418 | - public function setAuth($user, $password = '', $scheme = self::AUTH_BASIC) |
|
419 | - { |
|
420 | - if (empty($user)) { |
|
421 | - $this->auth = null; |
|
422 | - } else { |
|
423 | - $this->auth = array( |
|
424 | - 'user' => (string)$user, |
|
425 | - 'password' => (string)$password, |
|
426 | - 'scheme' => $scheme |
|
427 | - ); |
|
428 | - } |
|
429 | - |
|
430 | - return $this; |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Returns the authentication data |
|
435 | - * |
|
436 | - * The array has the keys 'user', 'password' and 'scheme', where 'scheme' |
|
437 | - * is one of the HTTP_Request2::AUTH_* constants. |
|
438 | - * |
|
439 | - * @return array |
|
440 | - */ |
|
441 | - public function getAuth() |
|
442 | - { |
|
443 | - return $this->auth; |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Sets request header(s) |
|
448 | - * |
|
449 | - * The first parameter may be either a full header string 'header: value' or |
|
450 | - * header name. In the former case $value parameter is ignored, in the latter |
|
451 | - * the header's value will either be set to $value or the header will be |
|
452 | - * removed if $value is null. The first parameter can also be an array of |
|
453 | - * headers, in that case method will be called recursively. |
|
454 | - * |
|
455 | - * Note that headers are treated case insensitively as per RFC 2616. |
|
456 | - * |
|
457 | - * <code> |
|
458 | - * $req->setHeader('Foo: Bar'); // sets the value of 'Foo' header to 'Bar' |
|
459 | - * $req->setHeader('FoO', 'Baz'); // sets the value of 'Foo' header to 'Baz' |
|
460 | - * $req->setHeader(array('foo' => 'Quux')); // sets the value of 'Foo' header to 'Quux' |
|
461 | - * $req->setHeader('FOO'); // removes 'Foo' header from request |
|
462 | - * </code> |
|
463 | - * |
|
464 | - * @param string|array $name header name, header string ('Header: value') |
|
465 | - * or an array of headers |
|
466 | - * @param string|array|null $value header value if $name is not an array, |
|
467 | - * header will be removed if value is null |
|
468 | - * @param bool $replace whether to replace previous header with the |
|
469 | - * same name or append to its value |
|
470 | - * |
|
471 | - * @return HTTP_Request2 |
|
472 | - * @throws HTTP_Request2_LogicException |
|
473 | - */ |
|
474 | - public function setHeader($name, $value = null, $replace = true) |
|
475 | - { |
|
476 | - if (is_array($name)) { |
|
477 | - foreach ($name as $k => $v) { |
|
478 | - if (is_string($k)) { |
|
479 | - $this->setHeader($k, $v, $replace); |
|
480 | - } else { |
|
481 | - $this->setHeader($v, null, $replace); |
|
482 | - } |
|
483 | - } |
|
484 | - } else { |
|
485 | - if (null === $value && strpos($name, ':')) { |
|
486 | - list($name, $value) = array_map('trim', explode(':', $name, 2)); |
|
487 | - } |
|
488 | - // Header name should be a token: http://tools.ietf.org/html/rfc2616#section-4.2 |
|
489 | - if (preg_match(self::REGEXP_INVALID_TOKEN, $name)) { |
|
490 | - throw new HTTP_Request2_LogicException( |
|
491 | - "Invalid header name '{$name}'", |
|
492 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
493 | - ); |
|
494 | - } |
|
495 | - // Header names are case insensitive anyway |
|
496 | - $name = strtolower($name); |
|
497 | - if (null === $value) { |
|
498 | - unset($this->headers[$name]); |
|
499 | - |
|
500 | - } else { |
|
501 | - if (is_array($value)) { |
|
502 | - $value = implode(', ', array_map('trim', $value)); |
|
503 | - } elseif (is_string($value)) { |
|
504 | - $value = trim($value); |
|
505 | - } |
|
506 | - if (!isset($this->headers[$name]) || $replace) { |
|
507 | - $this->headers[$name] = $value; |
|
508 | - } else { |
|
509 | - $this->headers[$name] .= ', ' . $value; |
|
510 | - } |
|
511 | - } |
|
512 | - } |
|
513 | - |
|
514 | - return $this; |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Returns the request headers |
|
519 | - * |
|
520 | - * The array is of the form ('header name' => 'header value'), header names |
|
521 | - * are lowercased |
|
522 | - * |
|
523 | - * @return array |
|
524 | - */ |
|
525 | - public function getHeaders() |
|
526 | - { |
|
527 | - return $this->headers; |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Adds a cookie to the request |
|
532 | - * |
|
533 | - * If the request does not have a CookieJar object set, this method simply |
|
534 | - * appends a cookie to "Cookie:" header. |
|
535 | - * |
|
536 | - * If a CookieJar object is available, the cookie is stored in that object. |
|
537 | - * Data from request URL will be used for setting its 'domain' and 'path' |
|
538 | - * parameters, 'expires' and 'secure' will be set to null and false, |
|
539 | - * respectively. If you need further control, use CookieJar's methods. |
|
540 | - * |
|
541 | - * @param string $name cookie name |
|
542 | - * @param string $value cookie value |
|
543 | - * |
|
544 | - * @return HTTP_Request2 |
|
545 | - * @throws HTTP_Request2_LogicException |
|
546 | - * @see setCookieJar() |
|
547 | - */ |
|
548 | - public function addCookie($name, $value) |
|
549 | - { |
|
550 | - if (!empty($this->cookieJar)) { |
|
551 | - $this->cookieJar->store( |
|
552 | - array('name' => $name, 'value' => $value), $this->url |
|
553 | - ); |
|
554 | - |
|
555 | - } else { |
|
556 | - $cookie = $name . '=' . $value; |
|
557 | - if (preg_match(self::REGEXP_INVALID_COOKIE, $cookie)) { |
|
558 | - throw new HTTP_Request2_LogicException( |
|
559 | - "Invalid cookie: '{$cookie}'", |
|
560 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
561 | - ); |
|
562 | - } |
|
563 | - $cookies = empty($this->headers['cookie'])? '': $this->headers['cookie'] . '; '; |
|
564 | - $this->setHeader('cookie', $cookies . $cookie); |
|
565 | - } |
|
566 | - |
|
567 | - return $this; |
|
568 | - } |
|
569 | - |
|
570 | - /** |
|
571 | - * Sets the request body |
|
572 | - * |
|
573 | - * If you provide file pointer rather than file name, it should support |
|
574 | - * fstat() and rewind() operations. |
|
575 | - * |
|
576 | - * @param string|resource|HTTP_Request2_MultipartBody $body Either a |
|
577 | - * string with the body or filename containing body or |
|
578 | - * pointer to an open file or object with multipart body data |
|
579 | - * @param bool $isFilename Whether |
|
580 | - * first parameter is a filename |
|
581 | - * |
|
582 | - * @return HTTP_Request2 |
|
583 | - * @throws HTTP_Request2_LogicException |
|
584 | - */ |
|
585 | - public function setBody($body, $isFilename = false) |
|
586 | - { |
|
587 | - if (!$isFilename && !is_resource($body)) { |
|
588 | - if (!$body instanceof HTTP_Request2_MultipartBody) { |
|
589 | - $this->body = (string)$body; |
|
590 | - } else { |
|
591 | - $this->body = $body; |
|
592 | - } |
|
593 | - } else { |
|
594 | - $fileData = $this->fopenWrapper($body, empty($this->headers['content-type'])); |
|
595 | - $this->body = $fileData['fp']; |
|
596 | - if (empty($this->headers['content-type'])) { |
|
597 | - $this->setHeader('content-type', $fileData['type']); |
|
598 | - } |
|
599 | - } |
|
600 | - $this->postParams = $this->uploads = array(); |
|
601 | - |
|
602 | - return $this; |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Returns the request body |
|
607 | - * |
|
608 | - * @return string|resource|HTTP_Request2_MultipartBody |
|
609 | - */ |
|
610 | - public function getBody() |
|
611 | - { |
|
612 | - if (self::METHOD_POST == $this->method |
|
613 | - && (!empty($this->postParams) || !empty($this->uploads)) |
|
614 | - ) { |
|
615 | - if (0 === strpos($this->headers['content-type'], 'application/x-www-form-urlencoded')) { |
|
616 | - $body = http_build_query($this->postParams, '', '&'); |
|
617 | - if (!$this->getConfig('use_brackets')) { |
|
618 | - $body = preg_replace('/%5B\d+%5D=/', '=', $body); |
|
619 | - } |
|
620 | - // support RFC 3986 by not encoding '~' symbol (request #15368) |
|
621 | - return str_replace('%7E', '~', $body); |
|
622 | - |
|
623 | - } elseif (0 === strpos($this->headers['content-type'], 'multipart/form-data')) { |
|
624 | - require_once 'HTTP/Request2/MultipartBody.php'; |
|
625 | - return new HTTP_Request2_MultipartBody( |
|
626 | - $this->postParams, $this->uploads, $this->getConfig('use_brackets') |
|
627 | - ); |
|
628 | - } |
|
629 | - } |
|
630 | - return $this->body; |
|
631 | - } |
|
632 | - |
|
633 | - /** |
|
634 | - * Adds a file to form-based file upload |
|
635 | - * |
|
636 | - * Used to emulate file upload via a HTML form. The method also sets |
|
637 | - * Content-Type of HTTP request to 'multipart/form-data'. |
|
638 | - * |
|
639 | - * If you just want to send the contents of a file as the body of HTTP |
|
640 | - * request you should use setBody() method. |
|
641 | - * |
|
642 | - * If you provide file pointers rather than file names, they should support |
|
643 | - * fstat() and rewind() operations. |
|
644 | - * |
|
645 | - * @param string $fieldName name of file-upload field |
|
646 | - * @param string|resource|array $filename full name of local file, |
|
647 | - * pointer to open file or an array of files |
|
648 | - * @param string $sendFilename filename to send in the request |
|
649 | - * @param string $contentType content-type of file being uploaded |
|
650 | - * |
|
651 | - * @return HTTP_Request2 |
|
652 | - * @throws HTTP_Request2_LogicException |
|
653 | - */ |
|
654 | - public function addUpload( |
|
655 | - $fieldName, $filename, $sendFilename = null, $contentType = null |
|
656 | - ) { |
|
657 | - if (!is_array($filename)) { |
|
658 | - $fileData = $this->fopenWrapper($filename, empty($contentType)); |
|
659 | - $this->uploads[$fieldName] = array( |
|
660 | - 'fp' => $fileData['fp'], |
|
661 | - 'filename' => !empty($sendFilename)? $sendFilename |
|
662 | - :(is_string($filename)? basename($filename): 'anonymous.blob') , |
|
663 | - 'size' => $fileData['size'], |
|
664 | - 'type' => empty($contentType)? $fileData['type']: $contentType |
|
665 | - ); |
|
666 | - } else { |
|
667 | - $fps = $names = $sizes = $types = array(); |
|
668 | - foreach ($filename as $f) { |
|
669 | - if (!is_array($f)) { |
|
670 | - $f = array($f); |
|
671 | - } |
|
672 | - $fileData = $this->fopenWrapper($f[0], empty($f[2])); |
|
673 | - $fps[] = $fileData['fp']; |
|
674 | - $names[] = !empty($f[1])? $f[1] |
|
675 | - :(is_string($f[0])? basename($f[0]): 'anonymous.blob'); |
|
676 | - $sizes[] = $fileData['size']; |
|
677 | - $types[] = empty($f[2])? $fileData['type']: $f[2]; |
|
678 | - } |
|
679 | - $this->uploads[$fieldName] = array( |
|
680 | - 'fp' => $fps, 'filename' => $names, 'size' => $sizes, 'type' => $types |
|
681 | - ); |
|
682 | - } |
|
683 | - if (empty($this->headers['content-type']) |
|
684 | - || 'application/x-www-form-urlencoded' == $this->headers['content-type'] |
|
685 | - ) { |
|
686 | - $this->setHeader('content-type', 'multipart/form-data'); |
|
687 | - } |
|
688 | - |
|
689 | - return $this; |
|
690 | - } |
|
691 | - |
|
692 | - /** |
|
693 | - * Adds POST parameter(s) to the request. |
|
694 | - * |
|
695 | - * @param string|array $name parameter name or array ('name' => 'value') |
|
696 | - * @param mixed $value parameter value (can be an array) |
|
697 | - * |
|
698 | - * @return HTTP_Request2 |
|
699 | - */ |
|
700 | - public function addPostParameter($name, $value = null) |
|
701 | - { |
|
702 | - if (!is_array($name)) { |
|
703 | - $this->postParams[$name] = $value; |
|
704 | - } else { |
|
705 | - foreach ($name as $k => $v) { |
|
706 | - $this->addPostParameter($k, $v); |
|
707 | - } |
|
708 | - } |
|
709 | - if (empty($this->headers['content-type'])) { |
|
710 | - $this->setHeader('content-type', 'application/x-www-form-urlencoded'); |
|
711 | - } |
|
712 | - |
|
713 | - return $this; |
|
714 | - } |
|
715 | - |
|
716 | - /** |
|
717 | - * Attaches a new observer |
|
718 | - * |
|
719 | - * @param SplObserver $observer any object implementing SplObserver |
|
720 | - */ |
|
721 | - public function attach(SplObserver $observer) |
|
722 | - { |
|
723 | - foreach ($this->observers as $attached) { |
|
724 | - if ($attached === $observer) { |
|
725 | - return; |
|
726 | - } |
|
727 | - } |
|
728 | - $this->observers[] = $observer; |
|
729 | - } |
|
730 | - |
|
731 | - /** |
|
732 | - * Detaches an existing observer |
|
733 | - * |
|
734 | - * @param SplObserver $observer any object implementing SplObserver |
|
735 | - */ |
|
736 | - public function detach(SplObserver $observer) |
|
737 | - { |
|
738 | - foreach ($this->observers as $key => $attached) { |
|
739 | - if ($attached === $observer) { |
|
740 | - unset($this->observers[$key]); |
|
741 | - return; |
|
742 | - } |
|
743 | - } |
|
744 | - } |
|
745 | - |
|
746 | - /** |
|
747 | - * Notifies all observers |
|
748 | - */ |
|
749 | - public function notify() |
|
750 | - { |
|
751 | - foreach ($this->observers as $observer) { |
|
752 | - $observer->update($this); |
|
753 | - } |
|
754 | - } |
|
755 | - |
|
756 | - /** |
|
757 | - * Sets the last event |
|
758 | - * |
|
759 | - * Adapters should use this method to set the current state of the request |
|
760 | - * and notify the observers. |
|
761 | - * |
|
762 | - * @param string $name event name |
|
763 | - * @param mixed $data event data |
|
764 | - */ |
|
765 | - public function setLastEvent($name, $data = null) |
|
766 | - { |
|
767 | - $this->lastEvent = array( |
|
768 | - 'name' => $name, |
|
769 | - 'data' => $data |
|
770 | - ); |
|
771 | - $this->notify(); |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * Returns the last event |
|
776 | - * |
|
777 | - * Observers should use this method to access the last change in request. |
|
778 | - * The following event names are possible: |
|
779 | - * <ul> |
|
780 | - * <li>'connect' - after connection to remote server, |
|
781 | - * data is the destination (string)</li> |
|
782 | - * <li>'disconnect' - after disconnection from server</li> |
|
783 | - * <li>'sentHeaders' - after sending the request headers, |
|
784 | - * data is the headers sent (string)</li> |
|
785 | - * <li>'sentBodyPart' - after sending a part of the request body, |
|
786 | - * data is the length of that part (int)</li> |
|
787 | - * <li>'sentBody' - after sending the whole request body, |
|
788 | - * data is request body length (int)</li> |
|
789 | - * <li>'receivedHeaders' - after receiving the response headers, |
|
790 | - * data is HTTP_Request2_Response object</li> |
|
791 | - * <li>'receivedBodyPart' - after receiving a part of the response |
|
792 | - * body, data is that part (string)</li> |
|
793 | - * <li>'receivedEncodedBodyPart' - as 'receivedBodyPart', but data is still |
|
794 | - * encoded by Content-Encoding</li> |
|
795 | - * <li>'receivedBody' - after receiving the complete response |
|
796 | - * body, data is HTTP_Request2_Response object</li> |
|
797 | - * </ul> |
|
798 | - * Different adapters may not send all the event types. Mock adapter does |
|
799 | - * not send any events to the observers. |
|
800 | - * |
|
801 | - * @return array The array has two keys: 'name' and 'data' |
|
802 | - */ |
|
803 | - public function getLastEvent() |
|
804 | - { |
|
805 | - return $this->lastEvent; |
|
806 | - } |
|
807 | - |
|
808 | - /** |
|
809 | - * Sets the adapter used to actually perform the request |
|
810 | - * |
|
811 | - * You can pass either an instance of a class implementing HTTP_Request2_Adapter |
|
812 | - * or a class name. The method will only try to include a file if the class |
|
813 | - * name starts with HTTP_Request2_Adapter_, it will also try to prepend this |
|
814 | - * prefix to the class name if it doesn't contain any underscores, so that |
|
815 | - * <code> |
|
816 | - * $request->setAdapter('curl'); |
|
817 | - * </code> |
|
818 | - * will work. |
|
819 | - * |
|
820 | - * @param string|HTTP_Request2_Adapter $adapter Adapter to use |
|
821 | - * |
|
822 | - * @return HTTP_Request2 |
|
823 | - * @throws HTTP_Request2_LogicException |
|
824 | - */ |
|
825 | - public function setAdapter($adapter) |
|
826 | - { |
|
827 | - if (is_string($adapter)) { |
|
828 | - if (!class_exists($adapter, false)) { |
|
829 | - if (false === strpos($adapter, '_')) { |
|
830 | - $adapter = 'HTTP_Request2_Adapter_' . ucfirst($adapter); |
|
831 | - } |
|
832 | - if (!class_exists($adapter, false) |
|
833 | - && preg_match('/^HTTP_Request2_Adapter_([a-zA-Z0-9]+)$/', $adapter) |
|
834 | - ) { |
|
835 | - include_once str_replace('_', DIRECTORY_SEPARATOR, $adapter) . '.php'; |
|
836 | - } |
|
837 | - if (!class_exists($adapter, false)) { |
|
838 | - throw new HTTP_Request2_LogicException( |
|
839 | - "Class {$adapter} not found", |
|
840 | - HTTP_Request2_Exception::MISSING_VALUE |
|
841 | - ); |
|
842 | - } |
|
843 | - } |
|
844 | - $adapter = new $adapter; |
|
845 | - } |
|
846 | - if (!$adapter instanceof HTTP_Request2_Adapter) { |
|
847 | - throw new HTTP_Request2_LogicException( |
|
848 | - 'Parameter is not a HTTP request adapter', |
|
849 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
850 | - ); |
|
851 | - } |
|
852 | - $this->adapter = $adapter; |
|
853 | - |
|
854 | - return $this; |
|
855 | - } |
|
856 | - |
|
857 | - /** |
|
858 | - * Sets the cookie jar |
|
859 | - * |
|
860 | - * A cookie jar is used to maintain cookies across HTTP requests and |
|
861 | - * responses. Cookies from jar will be automatically added to the request |
|
862 | - * headers based on request URL. |
|
863 | - * |
|
864 | - * @param HTTP_Request2_CookieJar|bool $jar Existing CookieJar object, true to |
|
865 | - * create a new one, false to remove |
|
866 | - * |
|
867 | - * @return HTTP_Request2 |
|
868 | - * @throws HTTP_Request2_LogicException |
|
869 | - */ |
|
870 | - public function setCookieJar($jar = true) |
|
871 | - { |
|
872 | - if (!class_exists('HTTP_Request2_CookieJar', false)) { |
|
873 | - require_once 'HTTP/Request2/CookieJar.php'; |
|
874 | - } |
|
875 | - |
|
876 | - if ($jar instanceof HTTP_Request2_CookieJar) { |
|
877 | - $this->cookieJar = $jar; |
|
878 | - } elseif (true === $jar) { |
|
879 | - $this->cookieJar = new HTTP_Request2_CookieJar(); |
|
880 | - } elseif (!$jar) { |
|
881 | - $this->cookieJar = null; |
|
882 | - } else { |
|
883 | - throw new HTTP_Request2_LogicException( |
|
884 | - 'Invalid parameter passed to setCookieJar()', |
|
885 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
886 | - ); |
|
887 | - } |
|
888 | - |
|
889 | - return $this; |
|
890 | - } |
|
891 | - |
|
892 | - /** |
|
893 | - * Returns current CookieJar object or null if none |
|
894 | - * |
|
895 | - * @return HTTP_Request2_CookieJar|null |
|
896 | - */ |
|
897 | - public function getCookieJar() |
|
898 | - { |
|
899 | - return $this->cookieJar; |
|
900 | - } |
|
901 | - |
|
902 | - /** |
|
903 | - * Sends the request and returns the response |
|
904 | - * |
|
905 | - * @throws HTTP_Request2_Exception |
|
906 | - * @return HTTP_Request2_Response |
|
907 | - */ |
|
908 | - public function send() |
|
909 | - { |
|
910 | - // Sanity check for URL |
|
911 | - if (!$this->url instanceof Net_URL2 |
|
912 | - || !$this->url->isAbsolute() |
|
913 | - || !in_array(strtolower($this->url->getScheme()), array('https', 'http')) |
|
914 | - ) { |
|
915 | - throw new HTTP_Request2_LogicException( |
|
916 | - 'HTTP_Request2 needs an absolute HTTP(S) request URL, ' |
|
917 | - . ($this->url instanceof Net_URL2 |
|
918 | - ? "'" . $this->url->__toString() . "'" : 'none') |
|
919 | - . ' given', |
|
920 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
921 | - ); |
|
922 | - } |
|
923 | - if (empty($this->adapter)) { |
|
924 | - $this->setAdapter($this->getConfig('adapter')); |
|
925 | - } |
|
926 | - // magic_quotes_runtime may break file uploads and chunked response |
|
927 | - // processing; see bug #4543. Don't use ini_get() here; see bug #16440. |
|
928 | - if ($magicQuotes = get_magic_quotes_runtime()) { |
|
929 | - set_magic_quotes_runtime(false); |
|
930 | - } |
|
931 | - // force using single byte encoding if mbstring extension overloads |
|
932 | - // strlen() and substr(); see bug #1781, bug #10605 |
|
933 | - if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
934 | - $oldEncoding = mb_internal_encoding(); |
|
935 | - mb_internal_encoding('8bit'); |
|
936 | - } |
|
937 | - |
|
938 | - try { |
|
939 | - $response = $this->adapter->sendRequest($this); |
|
940 | - } catch (Exception $e) { |
|
941 | - } |
|
942 | - // cleanup in either case (poor man's "finally" clause) |
|
943 | - if ($magicQuotes) { |
|
944 | - set_magic_quotes_runtime(true); |
|
945 | - } |
|
946 | - if (!empty($oldEncoding)) { |
|
947 | - mb_internal_encoding($oldEncoding); |
|
948 | - } |
|
949 | - // rethrow the exception |
|
950 | - if (!empty($e)) { |
|
951 | - throw $e; |
|
952 | - } |
|
953 | - return $response; |
|
954 | - } |
|
955 | - |
|
956 | - /** |
|
957 | - * Wrapper around fopen()/fstat() used by setBody() and addUpload() |
|
958 | - * |
|
959 | - * @param string|resource $file file name or pointer to open file |
|
960 | - * @param bool $detectType whether to try autodetecting MIME |
|
961 | - * type of file, will only work if $file is a |
|
962 | - * filename, not pointer |
|
963 | - * |
|
964 | - * @return array array('fp' => file pointer, 'size' => file size, 'type' => MIME type) |
|
965 | - * @throws HTTP_Request2_LogicException |
|
966 | - */ |
|
967 | - protected function fopenWrapper($file, $detectType = false) |
|
968 | - { |
|
969 | - if (!is_string($file) && !is_resource($file)) { |
|
970 | - throw new HTTP_Request2_LogicException( |
|
971 | - "Filename or file pointer resource expected", |
|
972 | - HTTP_Request2_Exception::INVALID_ARGUMENT |
|
973 | - ); |
|
974 | - } |
|
975 | - $fileData = array( |
|
976 | - 'fp' => is_string($file)? null: $file, |
|
977 | - 'type' => 'application/octet-stream', |
|
978 | - 'size' => 0 |
|
979 | - ); |
|
980 | - if (is_string($file)) { |
|
981 | - if (!($fileData['fp'] = @fopen($file, 'rb'))) { |
|
982 | - $error = error_get_last(); |
|
983 | - throw new HTTP_Request2_LogicException( |
|
984 | - $error['message'], HTTP_Request2_Exception::READ_ERROR |
|
985 | - ); |
|
986 | - } |
|
987 | - if ($detectType) { |
|
988 | - $fileData['type'] = self::detectMimeType($file); |
|
989 | - } |
|
990 | - } |
|
991 | - if (!($stat = fstat($fileData['fp']))) { |
|
992 | - throw new HTTP_Request2_LogicException( |
|
993 | - "fstat() call failed", HTTP_Request2_Exception::READ_ERROR |
|
994 | - ); |
|
995 | - } |
|
996 | - $fileData['size'] = $stat['size']; |
|
997 | - |
|
998 | - return $fileData; |
|
999 | - } |
|
1000 | - |
|
1001 | - /** |
|
1002 | - * Tries to detect MIME type of a file |
|
1003 | - * |
|
1004 | - * The method will try to use fileinfo extension if it is available, |
|
1005 | - * deprecated mime_content_type() function in the other case. If neither |
|
1006 | - * works, default 'application/octet-stream' MIME type is returned |
|
1007 | - * |
|
1008 | - * @param string $filename file name |
|
1009 | - * |
|
1010 | - * @return string file MIME type |
|
1011 | - */ |
|
1012 | - protected static function detectMimeType($filename) |
|
1013 | - { |
|
1014 | - // finfo extension from PECL available |
|
1015 | - if (function_exists('finfo_open')) { |
|
1016 | - if (!isset(self::$_fileinfoDb)) { |
|
1017 | - self::$_fileinfoDb = @finfo_open(FILEINFO_MIME); |
|
1018 | - } |
|
1019 | - if (self::$_fileinfoDb) { |
|
1020 | - $info = finfo_file(self::$_fileinfoDb, $filename); |
|
1021 | - } |
|
1022 | - } |
|
1023 | - // (deprecated) mime_content_type function available |
|
1024 | - if (empty($info) && function_exists('mime_content_type')) { |
|
1025 | - return mime_content_type($filename); |
|
1026 | - } |
|
1027 | - return empty($info)? 'application/octet-stream': $info; |
|
1028 | - } |
|
64 | + const AUTH_BASIC = 'basic'; |
|
65 | + const AUTH_DIGEST = 'digest'; |
|
66 | + /**#@-*/ |
|
67 | + |
|
68 | + /** |
|
69 | + * Regular expression used to check for invalid symbols in RFC 2616 tokens |
|
70 | + * @link http://pear.php.net/bugs/bug.php?id=15630 |
|
71 | + */ |
|
72 | + const REGEXP_INVALID_TOKEN = '![\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]!'; |
|
73 | + |
|
74 | + /** |
|
75 | + * Regular expression used to check for invalid symbols in cookie strings |
|
76 | + * @link http://pear.php.net/bugs/bug.php?id=15630 |
|
77 | + * @link http://web.archive.org/web/20080331104521/http://cgi.netscape.com/newsref/std/cookie_spec.html |
|
78 | + */ |
|
79 | + const REGEXP_INVALID_COOKIE = '/[\s,;]/'; |
|
80 | + |
|
81 | + /** |
|
82 | + * Fileinfo magic database resource |
|
83 | + * @var resource |
|
84 | + * @see detectMimeType() |
|
85 | + */ |
|
86 | + private static $_fileinfoDb; |
|
87 | + |
|
88 | + /** |
|
89 | + * Observers attached to the request (instances of SplObserver) |
|
90 | + * @var array |
|
91 | + */ |
|
92 | + protected $observers = array(); |
|
93 | + |
|
94 | + /** |
|
95 | + * Request URL |
|
96 | + * @var Net_URL2 |
|
97 | + */ |
|
98 | + protected $url; |
|
99 | + |
|
100 | + /** |
|
101 | + * Request method |
|
102 | + * @var string |
|
103 | + */ |
|
104 | + protected $method = self::METHOD_GET; |
|
105 | + |
|
106 | + /** |
|
107 | + * Authentication data |
|
108 | + * @var array |
|
109 | + * @see getAuth() |
|
110 | + */ |
|
111 | + protected $auth; |
|
112 | + |
|
113 | + /** |
|
114 | + * Request headers |
|
115 | + * @var array |
|
116 | + */ |
|
117 | + protected $headers = array(); |
|
118 | + |
|
119 | + /** |
|
120 | + * Configuration parameters |
|
121 | + * @var array |
|
122 | + * @see setConfig() |
|
123 | + */ |
|
124 | + protected $config = array( |
|
125 | + 'adapter' => 'HTTP_Request2_Adapter_Socket', |
|
126 | + 'connect_timeout' => 10, |
|
127 | + 'timeout' => 0, |
|
128 | + 'use_brackets' => true, |
|
129 | + 'protocol_version' => '1.1', |
|
130 | + 'buffer_size' => 16384, |
|
131 | + 'store_body' => true, |
|
132 | + 'local_ip' => null, |
|
133 | + |
|
134 | + 'proxy_host' => '', |
|
135 | + 'proxy_port' => '', |
|
136 | + 'proxy_user' => '', |
|
137 | + 'proxy_password' => '', |
|
138 | + 'proxy_auth_scheme' => self::AUTH_BASIC, |
|
139 | + 'proxy_type' => 'http', |
|
140 | + |
|
141 | + 'ssl_verify_peer' => true, |
|
142 | + 'ssl_verify_host' => true, |
|
143 | + 'ssl_cafile' => null, |
|
144 | + 'ssl_capath' => null, |
|
145 | + 'ssl_local_cert' => null, |
|
146 | + 'ssl_passphrase' => null, |
|
147 | + |
|
148 | + 'digest_compat_ie' => false, |
|
149 | + |
|
150 | + 'follow_redirects' => false, |
|
151 | + 'max_redirects' => 5, |
|
152 | + 'strict_redirects' => false |
|
153 | + ); |
|
154 | + |
|
155 | + /** |
|
156 | + * Last event in request / response handling, intended for observers |
|
157 | + * @var array |
|
158 | + * @see getLastEvent() |
|
159 | + */ |
|
160 | + protected $lastEvent = array( |
|
161 | + 'name' => 'start', |
|
162 | + 'data' => null |
|
163 | + ); |
|
164 | + |
|
165 | + /** |
|
166 | + * Request body |
|
167 | + * @var string|resource |
|
168 | + * @see setBody() |
|
169 | + */ |
|
170 | + protected $body = ''; |
|
171 | + |
|
172 | + /** |
|
173 | + * Array of POST parameters |
|
174 | + * @var array |
|
175 | + */ |
|
176 | + protected $postParams = array(); |
|
177 | + |
|
178 | + /** |
|
179 | + * Array of file uploads (for multipart/form-data POST requests) |
|
180 | + * @var array |
|
181 | + */ |
|
182 | + protected $uploads = array(); |
|
183 | + |
|
184 | + /** |
|
185 | + * Adapter used to perform actual HTTP request |
|
186 | + * @var HTTP_Request2_Adapter |
|
187 | + */ |
|
188 | + protected $adapter; |
|
189 | + |
|
190 | + /** |
|
191 | + * Cookie jar to persist cookies between requests |
|
192 | + * @var HTTP_Request2_CookieJar |
|
193 | + */ |
|
194 | + protected $cookieJar = null; |
|
195 | + |
|
196 | + /** |
|
197 | + * Constructor. Can set request URL, method and configuration array. |
|
198 | + * |
|
199 | + * Also sets a default value for User-Agent header. |
|
200 | + * |
|
201 | + * @param string|Net_Url2 $url Request URL |
|
202 | + * @param string $method Request method |
|
203 | + * @param array $config Configuration for this Request instance |
|
204 | + */ |
|
205 | + public function __construct( |
|
206 | + $url = null, $method = self::METHOD_GET, array $config = array() |
|
207 | + ) { |
|
208 | + $this->setConfig($config); |
|
209 | + if (!empty($url)) { |
|
210 | + $this->setUrl($url); |
|
211 | + } |
|
212 | + if (!empty($method)) { |
|
213 | + $this->setMethod($method); |
|
214 | + } |
|
215 | + $this->setHeader( |
|
216 | + 'user-agent', 'HTTP_Request2/2.2.1 ' . |
|
217 | + '(http://pear.php.net/package/http_request2) PHP/' . phpversion() |
|
218 | + ); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Sets the URL for this request |
|
223 | + * |
|
224 | + * If the URL has userinfo part (username & password) these will be removed |
|
225 | + * and converted to auth data. If the URL does not have a path component, |
|
226 | + * that will be set to '/'. |
|
227 | + * |
|
228 | + * @param string|Net_URL2 $url Request URL |
|
229 | + * |
|
230 | + * @return HTTP_Request2 |
|
231 | + * @throws HTTP_Request2_LogicException |
|
232 | + */ |
|
233 | + public function setUrl($url) |
|
234 | + { |
|
235 | + if (is_string($url)) { |
|
236 | + $url = new Net_URL2( |
|
237 | + $url, array(Net_URL2::OPTION_USE_BRACKETS => $this->config['use_brackets']) |
|
238 | + ); |
|
239 | + } |
|
240 | + if (!$url instanceof Net_URL2) { |
|
241 | + throw new HTTP_Request2_LogicException( |
|
242 | + 'Parameter is not a valid HTTP URL', |
|
243 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
244 | + ); |
|
245 | + } |
|
246 | + // URL contains username / password? |
|
247 | + if ($url->getUserinfo()) { |
|
248 | + $username = $url->getUser(); |
|
249 | + $password = $url->getPassword(); |
|
250 | + $this->setAuth(rawurldecode($username), $password? rawurldecode($password): ''); |
|
251 | + $url->setUserinfo(''); |
|
252 | + } |
|
253 | + if ('' == $url->getPath()) { |
|
254 | + $url->setPath('/'); |
|
255 | + } |
|
256 | + $this->url = $url; |
|
257 | + |
|
258 | + return $this; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Returns the request URL |
|
263 | + * |
|
264 | + * @return Net_URL2 |
|
265 | + */ |
|
266 | + public function getUrl() |
|
267 | + { |
|
268 | + return $this->url; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Sets the request method |
|
273 | + * |
|
274 | + * @param string $method one of the methods defined in RFC 2616 |
|
275 | + * |
|
276 | + * @return HTTP_Request2 |
|
277 | + * @throws HTTP_Request2_LogicException if the method name is invalid |
|
278 | + */ |
|
279 | + public function setMethod($method) |
|
280 | + { |
|
281 | + // Method name should be a token: http://tools.ietf.org/html/rfc2616#section-5.1.1 |
|
282 | + if (preg_match(self::REGEXP_INVALID_TOKEN, $method)) { |
|
283 | + throw new HTTP_Request2_LogicException( |
|
284 | + "Invalid request method '{$method}'", |
|
285 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
286 | + ); |
|
287 | + } |
|
288 | + $this->method = $method; |
|
289 | + |
|
290 | + return $this; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Returns the request method |
|
295 | + * |
|
296 | + * @return string |
|
297 | + */ |
|
298 | + public function getMethod() |
|
299 | + { |
|
300 | + return $this->method; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Sets the configuration parameter(s) |
|
305 | + * |
|
306 | + * The following parameters are available: |
|
307 | + * <ul> |
|
308 | + * <li> 'adapter' - adapter to use (string)</li> |
|
309 | + * <li> 'connect_timeout' - Connection timeout in seconds (integer)</li> |
|
310 | + * <li> 'timeout' - Total number of seconds a request can take. |
|
311 | + * Use 0 for no limit, should be greater than |
|
312 | + * 'connect_timeout' if set (integer)</li> |
|
313 | + * <li> 'use_brackets' - Whether to append [] to array variable names (bool)</li> |
|
314 | + * <li> 'protocol_version' - HTTP Version to use, '1.0' or '1.1' (string)</li> |
|
315 | + * <li> 'buffer_size' - Buffer size to use for reading and writing (int)</li> |
|
316 | + * <li> 'store_body' - Whether to store response body in response object. |
|
317 | + * Set to false if receiving a huge response and |
|
318 | + * using an Observer to save it (boolean)</li> |
|
319 | + * <li> 'local_ip' - Specifies the IP address that will be used for accessing |
|
320 | + * the network (string)</li> |
|
321 | + * <li> 'proxy_type' - Proxy type, 'http' or 'socks5' (string)</li> |
|
322 | + * <li> 'proxy_host' - Proxy server host (string)</li> |
|
323 | + * <li> 'proxy_port' - Proxy server port (integer)</li> |
|
324 | + * <li> 'proxy_user' - Proxy auth username (string)</li> |
|
325 | + * <li> 'proxy_password' - Proxy auth password (string)</li> |
|
326 | + * <li> 'proxy_auth_scheme' - Proxy auth scheme, one of HTTP_Request2::AUTH_* constants (string)</li> |
|
327 | + * <li> 'proxy' - Shorthand for proxy_* parameters, proxy given as URL, |
|
328 | + * e.g. 'socks5://localhost:1080/' (string)</li> |
|
329 | + * <li> 'ssl_verify_peer' - Whether to verify peer's SSL certificate (bool)</li> |
|
330 | + * <li> 'ssl_verify_host' - Whether to check that Common Name in SSL |
|
331 | + * certificate matches host name (bool)</li> |
|
332 | + * <li> 'ssl_cafile' - Cerificate Authority file to verify the peer |
|
333 | + * with (use with 'ssl_verify_peer') (string)</li> |
|
334 | + * <li> 'ssl_capath' - Directory holding multiple Certificate |
|
335 | + * Authority files (string)</li> |
|
336 | + * <li> 'ssl_local_cert' - Name of a file containing local cerificate (string)</li> |
|
337 | + * <li> 'ssl_passphrase' - Passphrase with which local certificate |
|
338 | + * was encoded (string)</li> |
|
339 | + * <li> 'digest_compat_ie' - Whether to imitate behaviour of MSIE 5 and 6 |
|
340 | + * in using URL without query string in digest |
|
341 | + * authentication (boolean)</li> |
|
342 | + * <li> 'follow_redirects' - Whether to automatically follow HTTP Redirects (boolean)</li> |
|
343 | + * <li> 'max_redirects' - Maximum number of redirects to follow (integer)</li> |
|
344 | + * <li> 'strict_redirects' - Whether to keep request method on redirects via status 301 and |
|
345 | + * 302 (true, needed for compatibility with RFC 2616) |
|
346 | + * or switch to GET (false, needed for compatibility with most |
|
347 | + * browsers) (boolean)</li> |
|
348 | + * </ul> |
|
349 | + * |
|
350 | + * @param string|array $nameOrConfig configuration parameter name or array |
|
351 | + * ('parameter name' => 'parameter value') |
|
352 | + * @param mixed $value parameter value if $nameOrConfig is not an array |
|
353 | + * |
|
354 | + * @return HTTP_Request2 |
|
355 | + * @throws HTTP_Request2_LogicException If the parameter is unknown |
|
356 | + */ |
|
357 | + public function setConfig($nameOrConfig, $value = null) |
|
358 | + { |
|
359 | + if (is_array($nameOrConfig)) { |
|
360 | + foreach ($nameOrConfig as $name => $value) { |
|
361 | + $this->setConfig($name, $value); |
|
362 | + } |
|
363 | + |
|
364 | + } elseif ('proxy' == $nameOrConfig) { |
|
365 | + $url = new Net_URL2($value); |
|
366 | + $this->setConfig(array( |
|
367 | + 'proxy_type' => $url->getScheme(), |
|
368 | + 'proxy_host' => $url->getHost(), |
|
369 | + 'proxy_port' => $url->getPort(), |
|
370 | + 'proxy_user' => rawurldecode($url->getUser()), |
|
371 | + 'proxy_password' => rawurldecode($url->getPassword()) |
|
372 | + )); |
|
373 | + |
|
374 | + } else { |
|
375 | + if (!array_key_exists($nameOrConfig, $this->config)) { |
|
376 | + throw new HTTP_Request2_LogicException( |
|
377 | + "Unknown configuration parameter '{$nameOrConfig}'", |
|
378 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
379 | + ); |
|
380 | + } |
|
381 | + $this->config[$nameOrConfig] = $value; |
|
382 | + } |
|
383 | + |
|
384 | + return $this; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Returns the value(s) of the configuration parameter(s) |
|
389 | + * |
|
390 | + * @param string $name parameter name |
|
391 | + * |
|
392 | + * @return mixed value of $name parameter, array of all configuration |
|
393 | + * parameters if $name is not given |
|
394 | + * @throws HTTP_Request2_LogicException If the parameter is unknown |
|
395 | + */ |
|
396 | + public function getConfig($name = null) |
|
397 | + { |
|
398 | + if (null === $name) { |
|
399 | + return $this->config; |
|
400 | + } elseif (!array_key_exists($name, $this->config)) { |
|
401 | + throw new HTTP_Request2_LogicException( |
|
402 | + "Unknown configuration parameter '{$name}'", |
|
403 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
404 | + ); |
|
405 | + } |
|
406 | + return $this->config[$name]; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Sets the autentification data |
|
411 | + * |
|
412 | + * @param string $user user name |
|
413 | + * @param string $password password |
|
414 | + * @param string $scheme authentication scheme |
|
415 | + * |
|
416 | + * @return HTTP_Request2 |
|
417 | + */ |
|
418 | + public function setAuth($user, $password = '', $scheme = self::AUTH_BASIC) |
|
419 | + { |
|
420 | + if (empty($user)) { |
|
421 | + $this->auth = null; |
|
422 | + } else { |
|
423 | + $this->auth = array( |
|
424 | + 'user' => (string)$user, |
|
425 | + 'password' => (string)$password, |
|
426 | + 'scheme' => $scheme |
|
427 | + ); |
|
428 | + } |
|
429 | + |
|
430 | + return $this; |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Returns the authentication data |
|
435 | + * |
|
436 | + * The array has the keys 'user', 'password' and 'scheme', where 'scheme' |
|
437 | + * is one of the HTTP_Request2::AUTH_* constants. |
|
438 | + * |
|
439 | + * @return array |
|
440 | + */ |
|
441 | + public function getAuth() |
|
442 | + { |
|
443 | + return $this->auth; |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Sets request header(s) |
|
448 | + * |
|
449 | + * The first parameter may be either a full header string 'header: value' or |
|
450 | + * header name. In the former case $value parameter is ignored, in the latter |
|
451 | + * the header's value will either be set to $value or the header will be |
|
452 | + * removed if $value is null. The first parameter can also be an array of |
|
453 | + * headers, in that case method will be called recursively. |
|
454 | + * |
|
455 | + * Note that headers are treated case insensitively as per RFC 2616. |
|
456 | + * |
|
457 | + * <code> |
|
458 | + * $req->setHeader('Foo: Bar'); // sets the value of 'Foo' header to 'Bar' |
|
459 | + * $req->setHeader('FoO', 'Baz'); // sets the value of 'Foo' header to 'Baz' |
|
460 | + * $req->setHeader(array('foo' => 'Quux')); // sets the value of 'Foo' header to 'Quux' |
|
461 | + * $req->setHeader('FOO'); // removes 'Foo' header from request |
|
462 | + * </code> |
|
463 | + * |
|
464 | + * @param string|array $name header name, header string ('Header: value') |
|
465 | + * or an array of headers |
|
466 | + * @param string|array|null $value header value if $name is not an array, |
|
467 | + * header will be removed if value is null |
|
468 | + * @param bool $replace whether to replace previous header with the |
|
469 | + * same name or append to its value |
|
470 | + * |
|
471 | + * @return HTTP_Request2 |
|
472 | + * @throws HTTP_Request2_LogicException |
|
473 | + */ |
|
474 | + public function setHeader($name, $value = null, $replace = true) |
|
475 | + { |
|
476 | + if (is_array($name)) { |
|
477 | + foreach ($name as $k => $v) { |
|
478 | + if (is_string($k)) { |
|
479 | + $this->setHeader($k, $v, $replace); |
|
480 | + } else { |
|
481 | + $this->setHeader($v, null, $replace); |
|
482 | + } |
|
483 | + } |
|
484 | + } else { |
|
485 | + if (null === $value && strpos($name, ':')) { |
|
486 | + list($name, $value) = array_map('trim', explode(':', $name, 2)); |
|
487 | + } |
|
488 | + // Header name should be a token: http://tools.ietf.org/html/rfc2616#section-4.2 |
|
489 | + if (preg_match(self::REGEXP_INVALID_TOKEN, $name)) { |
|
490 | + throw new HTTP_Request2_LogicException( |
|
491 | + "Invalid header name '{$name}'", |
|
492 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
493 | + ); |
|
494 | + } |
|
495 | + // Header names are case insensitive anyway |
|
496 | + $name = strtolower($name); |
|
497 | + if (null === $value) { |
|
498 | + unset($this->headers[$name]); |
|
499 | + |
|
500 | + } else { |
|
501 | + if (is_array($value)) { |
|
502 | + $value = implode(', ', array_map('trim', $value)); |
|
503 | + } elseif (is_string($value)) { |
|
504 | + $value = trim($value); |
|
505 | + } |
|
506 | + if (!isset($this->headers[$name]) || $replace) { |
|
507 | + $this->headers[$name] = $value; |
|
508 | + } else { |
|
509 | + $this->headers[$name] .= ', ' . $value; |
|
510 | + } |
|
511 | + } |
|
512 | + } |
|
513 | + |
|
514 | + return $this; |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Returns the request headers |
|
519 | + * |
|
520 | + * The array is of the form ('header name' => 'header value'), header names |
|
521 | + * are lowercased |
|
522 | + * |
|
523 | + * @return array |
|
524 | + */ |
|
525 | + public function getHeaders() |
|
526 | + { |
|
527 | + return $this->headers; |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Adds a cookie to the request |
|
532 | + * |
|
533 | + * If the request does not have a CookieJar object set, this method simply |
|
534 | + * appends a cookie to "Cookie:" header. |
|
535 | + * |
|
536 | + * If a CookieJar object is available, the cookie is stored in that object. |
|
537 | + * Data from request URL will be used for setting its 'domain' and 'path' |
|
538 | + * parameters, 'expires' and 'secure' will be set to null and false, |
|
539 | + * respectively. If you need further control, use CookieJar's methods. |
|
540 | + * |
|
541 | + * @param string $name cookie name |
|
542 | + * @param string $value cookie value |
|
543 | + * |
|
544 | + * @return HTTP_Request2 |
|
545 | + * @throws HTTP_Request2_LogicException |
|
546 | + * @see setCookieJar() |
|
547 | + */ |
|
548 | + public function addCookie($name, $value) |
|
549 | + { |
|
550 | + if (!empty($this->cookieJar)) { |
|
551 | + $this->cookieJar->store( |
|
552 | + array('name' => $name, 'value' => $value), $this->url |
|
553 | + ); |
|
554 | + |
|
555 | + } else { |
|
556 | + $cookie = $name . '=' . $value; |
|
557 | + if (preg_match(self::REGEXP_INVALID_COOKIE, $cookie)) { |
|
558 | + throw new HTTP_Request2_LogicException( |
|
559 | + "Invalid cookie: '{$cookie}'", |
|
560 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
561 | + ); |
|
562 | + } |
|
563 | + $cookies = empty($this->headers['cookie'])? '': $this->headers['cookie'] . '; '; |
|
564 | + $this->setHeader('cookie', $cookies . $cookie); |
|
565 | + } |
|
566 | + |
|
567 | + return $this; |
|
568 | + } |
|
569 | + |
|
570 | + /** |
|
571 | + * Sets the request body |
|
572 | + * |
|
573 | + * If you provide file pointer rather than file name, it should support |
|
574 | + * fstat() and rewind() operations. |
|
575 | + * |
|
576 | + * @param string|resource|HTTP_Request2_MultipartBody $body Either a |
|
577 | + * string with the body or filename containing body or |
|
578 | + * pointer to an open file or object with multipart body data |
|
579 | + * @param bool $isFilename Whether |
|
580 | + * first parameter is a filename |
|
581 | + * |
|
582 | + * @return HTTP_Request2 |
|
583 | + * @throws HTTP_Request2_LogicException |
|
584 | + */ |
|
585 | + public function setBody($body, $isFilename = false) |
|
586 | + { |
|
587 | + if (!$isFilename && !is_resource($body)) { |
|
588 | + if (!$body instanceof HTTP_Request2_MultipartBody) { |
|
589 | + $this->body = (string)$body; |
|
590 | + } else { |
|
591 | + $this->body = $body; |
|
592 | + } |
|
593 | + } else { |
|
594 | + $fileData = $this->fopenWrapper($body, empty($this->headers['content-type'])); |
|
595 | + $this->body = $fileData['fp']; |
|
596 | + if (empty($this->headers['content-type'])) { |
|
597 | + $this->setHeader('content-type', $fileData['type']); |
|
598 | + } |
|
599 | + } |
|
600 | + $this->postParams = $this->uploads = array(); |
|
601 | + |
|
602 | + return $this; |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Returns the request body |
|
607 | + * |
|
608 | + * @return string|resource|HTTP_Request2_MultipartBody |
|
609 | + */ |
|
610 | + public function getBody() |
|
611 | + { |
|
612 | + if (self::METHOD_POST == $this->method |
|
613 | + && (!empty($this->postParams) || !empty($this->uploads)) |
|
614 | + ) { |
|
615 | + if (0 === strpos($this->headers['content-type'], 'application/x-www-form-urlencoded')) { |
|
616 | + $body = http_build_query($this->postParams, '', '&'); |
|
617 | + if (!$this->getConfig('use_brackets')) { |
|
618 | + $body = preg_replace('/%5B\d+%5D=/', '=', $body); |
|
619 | + } |
|
620 | + // support RFC 3986 by not encoding '~' symbol (request #15368) |
|
621 | + return str_replace('%7E', '~', $body); |
|
622 | + |
|
623 | + } elseif (0 === strpos($this->headers['content-type'], 'multipart/form-data')) { |
|
624 | + require_once 'HTTP/Request2/MultipartBody.php'; |
|
625 | + return new HTTP_Request2_MultipartBody( |
|
626 | + $this->postParams, $this->uploads, $this->getConfig('use_brackets') |
|
627 | + ); |
|
628 | + } |
|
629 | + } |
|
630 | + return $this->body; |
|
631 | + } |
|
632 | + |
|
633 | + /** |
|
634 | + * Adds a file to form-based file upload |
|
635 | + * |
|
636 | + * Used to emulate file upload via a HTML form. The method also sets |
|
637 | + * Content-Type of HTTP request to 'multipart/form-data'. |
|
638 | + * |
|
639 | + * If you just want to send the contents of a file as the body of HTTP |
|
640 | + * request you should use setBody() method. |
|
641 | + * |
|
642 | + * If you provide file pointers rather than file names, they should support |
|
643 | + * fstat() and rewind() operations. |
|
644 | + * |
|
645 | + * @param string $fieldName name of file-upload field |
|
646 | + * @param string|resource|array $filename full name of local file, |
|
647 | + * pointer to open file or an array of files |
|
648 | + * @param string $sendFilename filename to send in the request |
|
649 | + * @param string $contentType content-type of file being uploaded |
|
650 | + * |
|
651 | + * @return HTTP_Request2 |
|
652 | + * @throws HTTP_Request2_LogicException |
|
653 | + */ |
|
654 | + public function addUpload( |
|
655 | + $fieldName, $filename, $sendFilename = null, $contentType = null |
|
656 | + ) { |
|
657 | + if (!is_array($filename)) { |
|
658 | + $fileData = $this->fopenWrapper($filename, empty($contentType)); |
|
659 | + $this->uploads[$fieldName] = array( |
|
660 | + 'fp' => $fileData['fp'], |
|
661 | + 'filename' => !empty($sendFilename)? $sendFilename |
|
662 | + :(is_string($filename)? basename($filename): 'anonymous.blob') , |
|
663 | + 'size' => $fileData['size'], |
|
664 | + 'type' => empty($contentType)? $fileData['type']: $contentType |
|
665 | + ); |
|
666 | + } else { |
|
667 | + $fps = $names = $sizes = $types = array(); |
|
668 | + foreach ($filename as $f) { |
|
669 | + if (!is_array($f)) { |
|
670 | + $f = array($f); |
|
671 | + } |
|
672 | + $fileData = $this->fopenWrapper($f[0], empty($f[2])); |
|
673 | + $fps[] = $fileData['fp']; |
|
674 | + $names[] = !empty($f[1])? $f[1] |
|
675 | + :(is_string($f[0])? basename($f[0]): 'anonymous.blob'); |
|
676 | + $sizes[] = $fileData['size']; |
|
677 | + $types[] = empty($f[2])? $fileData['type']: $f[2]; |
|
678 | + } |
|
679 | + $this->uploads[$fieldName] = array( |
|
680 | + 'fp' => $fps, 'filename' => $names, 'size' => $sizes, 'type' => $types |
|
681 | + ); |
|
682 | + } |
|
683 | + if (empty($this->headers['content-type']) |
|
684 | + || 'application/x-www-form-urlencoded' == $this->headers['content-type'] |
|
685 | + ) { |
|
686 | + $this->setHeader('content-type', 'multipart/form-data'); |
|
687 | + } |
|
688 | + |
|
689 | + return $this; |
|
690 | + } |
|
691 | + |
|
692 | + /** |
|
693 | + * Adds POST parameter(s) to the request. |
|
694 | + * |
|
695 | + * @param string|array $name parameter name or array ('name' => 'value') |
|
696 | + * @param mixed $value parameter value (can be an array) |
|
697 | + * |
|
698 | + * @return HTTP_Request2 |
|
699 | + */ |
|
700 | + public function addPostParameter($name, $value = null) |
|
701 | + { |
|
702 | + if (!is_array($name)) { |
|
703 | + $this->postParams[$name] = $value; |
|
704 | + } else { |
|
705 | + foreach ($name as $k => $v) { |
|
706 | + $this->addPostParameter($k, $v); |
|
707 | + } |
|
708 | + } |
|
709 | + if (empty($this->headers['content-type'])) { |
|
710 | + $this->setHeader('content-type', 'application/x-www-form-urlencoded'); |
|
711 | + } |
|
712 | + |
|
713 | + return $this; |
|
714 | + } |
|
715 | + |
|
716 | + /** |
|
717 | + * Attaches a new observer |
|
718 | + * |
|
719 | + * @param SplObserver $observer any object implementing SplObserver |
|
720 | + */ |
|
721 | + public function attach(SplObserver $observer) |
|
722 | + { |
|
723 | + foreach ($this->observers as $attached) { |
|
724 | + if ($attached === $observer) { |
|
725 | + return; |
|
726 | + } |
|
727 | + } |
|
728 | + $this->observers[] = $observer; |
|
729 | + } |
|
730 | + |
|
731 | + /** |
|
732 | + * Detaches an existing observer |
|
733 | + * |
|
734 | + * @param SplObserver $observer any object implementing SplObserver |
|
735 | + */ |
|
736 | + public function detach(SplObserver $observer) |
|
737 | + { |
|
738 | + foreach ($this->observers as $key => $attached) { |
|
739 | + if ($attached === $observer) { |
|
740 | + unset($this->observers[$key]); |
|
741 | + return; |
|
742 | + } |
|
743 | + } |
|
744 | + } |
|
745 | + |
|
746 | + /** |
|
747 | + * Notifies all observers |
|
748 | + */ |
|
749 | + public function notify() |
|
750 | + { |
|
751 | + foreach ($this->observers as $observer) { |
|
752 | + $observer->update($this); |
|
753 | + } |
|
754 | + } |
|
755 | + |
|
756 | + /** |
|
757 | + * Sets the last event |
|
758 | + * |
|
759 | + * Adapters should use this method to set the current state of the request |
|
760 | + * and notify the observers. |
|
761 | + * |
|
762 | + * @param string $name event name |
|
763 | + * @param mixed $data event data |
|
764 | + */ |
|
765 | + public function setLastEvent($name, $data = null) |
|
766 | + { |
|
767 | + $this->lastEvent = array( |
|
768 | + 'name' => $name, |
|
769 | + 'data' => $data |
|
770 | + ); |
|
771 | + $this->notify(); |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * Returns the last event |
|
776 | + * |
|
777 | + * Observers should use this method to access the last change in request. |
|
778 | + * The following event names are possible: |
|
779 | + * <ul> |
|
780 | + * <li>'connect' - after connection to remote server, |
|
781 | + * data is the destination (string)</li> |
|
782 | + * <li>'disconnect' - after disconnection from server</li> |
|
783 | + * <li>'sentHeaders' - after sending the request headers, |
|
784 | + * data is the headers sent (string)</li> |
|
785 | + * <li>'sentBodyPart' - after sending a part of the request body, |
|
786 | + * data is the length of that part (int)</li> |
|
787 | + * <li>'sentBody' - after sending the whole request body, |
|
788 | + * data is request body length (int)</li> |
|
789 | + * <li>'receivedHeaders' - after receiving the response headers, |
|
790 | + * data is HTTP_Request2_Response object</li> |
|
791 | + * <li>'receivedBodyPart' - after receiving a part of the response |
|
792 | + * body, data is that part (string)</li> |
|
793 | + * <li>'receivedEncodedBodyPart' - as 'receivedBodyPart', but data is still |
|
794 | + * encoded by Content-Encoding</li> |
|
795 | + * <li>'receivedBody' - after receiving the complete response |
|
796 | + * body, data is HTTP_Request2_Response object</li> |
|
797 | + * </ul> |
|
798 | + * Different adapters may not send all the event types. Mock adapter does |
|
799 | + * not send any events to the observers. |
|
800 | + * |
|
801 | + * @return array The array has two keys: 'name' and 'data' |
|
802 | + */ |
|
803 | + public function getLastEvent() |
|
804 | + { |
|
805 | + return $this->lastEvent; |
|
806 | + } |
|
807 | + |
|
808 | + /** |
|
809 | + * Sets the adapter used to actually perform the request |
|
810 | + * |
|
811 | + * You can pass either an instance of a class implementing HTTP_Request2_Adapter |
|
812 | + * or a class name. The method will only try to include a file if the class |
|
813 | + * name starts with HTTP_Request2_Adapter_, it will also try to prepend this |
|
814 | + * prefix to the class name if it doesn't contain any underscores, so that |
|
815 | + * <code> |
|
816 | + * $request->setAdapter('curl'); |
|
817 | + * </code> |
|
818 | + * will work. |
|
819 | + * |
|
820 | + * @param string|HTTP_Request2_Adapter $adapter Adapter to use |
|
821 | + * |
|
822 | + * @return HTTP_Request2 |
|
823 | + * @throws HTTP_Request2_LogicException |
|
824 | + */ |
|
825 | + public function setAdapter($adapter) |
|
826 | + { |
|
827 | + if (is_string($adapter)) { |
|
828 | + if (!class_exists($adapter, false)) { |
|
829 | + if (false === strpos($adapter, '_')) { |
|
830 | + $adapter = 'HTTP_Request2_Adapter_' . ucfirst($adapter); |
|
831 | + } |
|
832 | + if (!class_exists($adapter, false) |
|
833 | + && preg_match('/^HTTP_Request2_Adapter_([a-zA-Z0-9]+)$/', $adapter) |
|
834 | + ) { |
|
835 | + include_once str_replace('_', DIRECTORY_SEPARATOR, $adapter) . '.php'; |
|
836 | + } |
|
837 | + if (!class_exists($adapter, false)) { |
|
838 | + throw new HTTP_Request2_LogicException( |
|
839 | + "Class {$adapter} not found", |
|
840 | + HTTP_Request2_Exception::MISSING_VALUE |
|
841 | + ); |
|
842 | + } |
|
843 | + } |
|
844 | + $adapter = new $adapter; |
|
845 | + } |
|
846 | + if (!$adapter instanceof HTTP_Request2_Adapter) { |
|
847 | + throw new HTTP_Request2_LogicException( |
|
848 | + 'Parameter is not a HTTP request adapter', |
|
849 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
850 | + ); |
|
851 | + } |
|
852 | + $this->adapter = $adapter; |
|
853 | + |
|
854 | + return $this; |
|
855 | + } |
|
856 | + |
|
857 | + /** |
|
858 | + * Sets the cookie jar |
|
859 | + * |
|
860 | + * A cookie jar is used to maintain cookies across HTTP requests and |
|
861 | + * responses. Cookies from jar will be automatically added to the request |
|
862 | + * headers based on request URL. |
|
863 | + * |
|
864 | + * @param HTTP_Request2_CookieJar|bool $jar Existing CookieJar object, true to |
|
865 | + * create a new one, false to remove |
|
866 | + * |
|
867 | + * @return HTTP_Request2 |
|
868 | + * @throws HTTP_Request2_LogicException |
|
869 | + */ |
|
870 | + public function setCookieJar($jar = true) |
|
871 | + { |
|
872 | + if (!class_exists('HTTP_Request2_CookieJar', false)) { |
|
873 | + require_once 'HTTP/Request2/CookieJar.php'; |
|
874 | + } |
|
875 | + |
|
876 | + if ($jar instanceof HTTP_Request2_CookieJar) { |
|
877 | + $this->cookieJar = $jar; |
|
878 | + } elseif (true === $jar) { |
|
879 | + $this->cookieJar = new HTTP_Request2_CookieJar(); |
|
880 | + } elseif (!$jar) { |
|
881 | + $this->cookieJar = null; |
|
882 | + } else { |
|
883 | + throw new HTTP_Request2_LogicException( |
|
884 | + 'Invalid parameter passed to setCookieJar()', |
|
885 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
886 | + ); |
|
887 | + } |
|
888 | + |
|
889 | + return $this; |
|
890 | + } |
|
891 | + |
|
892 | + /** |
|
893 | + * Returns current CookieJar object or null if none |
|
894 | + * |
|
895 | + * @return HTTP_Request2_CookieJar|null |
|
896 | + */ |
|
897 | + public function getCookieJar() |
|
898 | + { |
|
899 | + return $this->cookieJar; |
|
900 | + } |
|
901 | + |
|
902 | + /** |
|
903 | + * Sends the request and returns the response |
|
904 | + * |
|
905 | + * @throws HTTP_Request2_Exception |
|
906 | + * @return HTTP_Request2_Response |
|
907 | + */ |
|
908 | + public function send() |
|
909 | + { |
|
910 | + // Sanity check for URL |
|
911 | + if (!$this->url instanceof Net_URL2 |
|
912 | + || !$this->url->isAbsolute() |
|
913 | + || !in_array(strtolower($this->url->getScheme()), array('https', 'http')) |
|
914 | + ) { |
|
915 | + throw new HTTP_Request2_LogicException( |
|
916 | + 'HTTP_Request2 needs an absolute HTTP(S) request URL, ' |
|
917 | + . ($this->url instanceof Net_URL2 |
|
918 | + ? "'" . $this->url->__toString() . "'" : 'none') |
|
919 | + . ' given', |
|
920 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
921 | + ); |
|
922 | + } |
|
923 | + if (empty($this->adapter)) { |
|
924 | + $this->setAdapter($this->getConfig('adapter')); |
|
925 | + } |
|
926 | + // magic_quotes_runtime may break file uploads and chunked response |
|
927 | + // processing; see bug #4543. Don't use ini_get() here; see bug #16440. |
|
928 | + if ($magicQuotes = get_magic_quotes_runtime()) { |
|
929 | + set_magic_quotes_runtime(false); |
|
930 | + } |
|
931 | + // force using single byte encoding if mbstring extension overloads |
|
932 | + // strlen() and substr(); see bug #1781, bug #10605 |
|
933 | + if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { |
|
934 | + $oldEncoding = mb_internal_encoding(); |
|
935 | + mb_internal_encoding('8bit'); |
|
936 | + } |
|
937 | + |
|
938 | + try { |
|
939 | + $response = $this->adapter->sendRequest($this); |
|
940 | + } catch (Exception $e) { |
|
941 | + } |
|
942 | + // cleanup in either case (poor man's "finally" clause) |
|
943 | + if ($magicQuotes) { |
|
944 | + set_magic_quotes_runtime(true); |
|
945 | + } |
|
946 | + if (!empty($oldEncoding)) { |
|
947 | + mb_internal_encoding($oldEncoding); |
|
948 | + } |
|
949 | + // rethrow the exception |
|
950 | + if (!empty($e)) { |
|
951 | + throw $e; |
|
952 | + } |
|
953 | + return $response; |
|
954 | + } |
|
955 | + |
|
956 | + /** |
|
957 | + * Wrapper around fopen()/fstat() used by setBody() and addUpload() |
|
958 | + * |
|
959 | + * @param string|resource $file file name or pointer to open file |
|
960 | + * @param bool $detectType whether to try autodetecting MIME |
|
961 | + * type of file, will only work if $file is a |
|
962 | + * filename, not pointer |
|
963 | + * |
|
964 | + * @return array array('fp' => file pointer, 'size' => file size, 'type' => MIME type) |
|
965 | + * @throws HTTP_Request2_LogicException |
|
966 | + */ |
|
967 | + protected function fopenWrapper($file, $detectType = false) |
|
968 | + { |
|
969 | + if (!is_string($file) && !is_resource($file)) { |
|
970 | + throw new HTTP_Request2_LogicException( |
|
971 | + "Filename or file pointer resource expected", |
|
972 | + HTTP_Request2_Exception::INVALID_ARGUMENT |
|
973 | + ); |
|
974 | + } |
|
975 | + $fileData = array( |
|
976 | + 'fp' => is_string($file)? null: $file, |
|
977 | + 'type' => 'application/octet-stream', |
|
978 | + 'size' => 0 |
|
979 | + ); |
|
980 | + if (is_string($file)) { |
|
981 | + if (!($fileData['fp'] = @fopen($file, 'rb'))) { |
|
982 | + $error = error_get_last(); |
|
983 | + throw new HTTP_Request2_LogicException( |
|
984 | + $error['message'], HTTP_Request2_Exception::READ_ERROR |
|
985 | + ); |
|
986 | + } |
|
987 | + if ($detectType) { |
|
988 | + $fileData['type'] = self::detectMimeType($file); |
|
989 | + } |
|
990 | + } |
|
991 | + if (!($stat = fstat($fileData['fp']))) { |
|
992 | + throw new HTTP_Request2_LogicException( |
|
993 | + "fstat() call failed", HTTP_Request2_Exception::READ_ERROR |
|
994 | + ); |
|
995 | + } |
|
996 | + $fileData['size'] = $stat['size']; |
|
997 | + |
|
998 | + return $fileData; |
|
999 | + } |
|
1000 | + |
|
1001 | + /** |
|
1002 | + * Tries to detect MIME type of a file |
|
1003 | + * |
|
1004 | + * The method will try to use fileinfo extension if it is available, |
|
1005 | + * deprecated mime_content_type() function in the other case. If neither |
|
1006 | + * works, default 'application/octet-stream' MIME type is returned |
|
1007 | + * |
|
1008 | + * @param string $filename file name |
|
1009 | + * |
|
1010 | + * @return string file MIME type |
|
1011 | + */ |
|
1012 | + protected static function detectMimeType($filename) |
|
1013 | + { |
|
1014 | + // finfo extension from PECL available |
|
1015 | + if (function_exists('finfo_open')) { |
|
1016 | + if (!isset(self::$_fileinfoDb)) { |
|
1017 | + self::$_fileinfoDb = @finfo_open(FILEINFO_MIME); |
|
1018 | + } |
|
1019 | + if (self::$_fileinfoDb) { |
|
1020 | + $info = finfo_file(self::$_fileinfoDb, $filename); |
|
1021 | + } |
|
1022 | + } |
|
1023 | + // (deprecated) mime_content_type function available |
|
1024 | + if (empty($info) && function_exists('mime_content_type')) { |
|
1025 | + return mime_content_type($filename); |
|
1026 | + } |
|
1027 | + return empty($info)? 'application/octet-stream': $info; |
|
1028 | + } |
|
1029 | 1029 | } |
1030 | 1030 | ?> |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | /** |
262 | 262 | * Returns the request URL |
263 | 263 | * |
264 | - * @return Net_URL2 |
|
264 | + * @return string |
|
265 | 265 | */ |
266 | 266 | public function getUrl() |
267 | 267 | { |
@@ -573,7 +573,7 @@ discard block |
||
573 | 573 | * If you provide file pointer rather than file name, it should support |
574 | 574 | * fstat() and rewind() operations. |
575 | 575 | * |
576 | - * @param string|resource|HTTP_Request2_MultipartBody $body Either a |
|
576 | + * @param string $body Either a |
|
577 | 577 | * string with the body or filename containing body or |
578 | 578 | * pointer to an open file or object with multipart body data |
579 | 579 | * @param bool $isFilename Whether |
@@ -213,8 +213,8 @@ discard block |
||
213 | 213 | $this->setMethod($method); |
214 | 214 | } |
215 | 215 | $this->setHeader( |
216 | - 'user-agent', 'HTTP_Request2/2.2.1 ' . |
|
217 | - '(http://pear.php.net/package/http_request2) PHP/' . phpversion() |
|
216 | + 'user-agent', 'HTTP_Request2/2.2.1 '. |
|
217 | + '(http://pear.php.net/package/http_request2) PHP/'.phpversion() |
|
218 | 218 | ); |
219 | 219 | } |
220 | 220 | |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | if ($url->getUserinfo()) { |
248 | 248 | $username = $url->getUser(); |
249 | 249 | $password = $url->getPassword(); |
250 | - $this->setAuth(rawurldecode($username), $password? rawurldecode($password): ''); |
|
250 | + $this->setAuth(rawurldecode($username), $password ? rawurldecode($password) : ''); |
|
251 | 251 | $url->setUserinfo(''); |
252 | 252 | } |
253 | 253 | if ('' == $url->getPath()) { |
@@ -421,8 +421,8 @@ discard block |
||
421 | 421 | $this->auth = null; |
422 | 422 | } else { |
423 | 423 | $this->auth = array( |
424 | - 'user' => (string)$user, |
|
425 | - 'password' => (string)$password, |
|
424 | + 'user' => (string) $user, |
|
425 | + 'password' => (string) $password, |
|
426 | 426 | 'scheme' => $scheme |
427 | 427 | ); |
428 | 428 | } |
@@ -506,7 +506,7 @@ discard block |
||
506 | 506 | if (!isset($this->headers[$name]) || $replace) { |
507 | 507 | $this->headers[$name] = $value; |
508 | 508 | } else { |
509 | - $this->headers[$name] .= ', ' . $value; |
|
509 | + $this->headers[$name] .= ', '.$value; |
|
510 | 510 | } |
511 | 511 | } |
512 | 512 | } |
@@ -553,15 +553,15 @@ discard block |
||
553 | 553 | ); |
554 | 554 | |
555 | 555 | } else { |
556 | - $cookie = $name . '=' . $value; |
|
556 | + $cookie = $name.'='.$value; |
|
557 | 557 | if (preg_match(self::REGEXP_INVALID_COOKIE, $cookie)) { |
558 | 558 | throw new HTTP_Request2_LogicException( |
559 | 559 | "Invalid cookie: '{$cookie}'", |
560 | 560 | HTTP_Request2_Exception::INVALID_ARGUMENT |
561 | 561 | ); |
562 | 562 | } |
563 | - $cookies = empty($this->headers['cookie'])? '': $this->headers['cookie'] . '; '; |
|
564 | - $this->setHeader('cookie', $cookies . $cookie); |
|
563 | + $cookies = empty($this->headers['cookie']) ? '' : $this->headers['cookie'].'; '; |
|
564 | + $this->setHeader('cookie', $cookies.$cookie); |
|
565 | 565 | } |
566 | 566 | |
567 | 567 | return $this; |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | { |
587 | 587 | if (!$isFilename && !is_resource($body)) { |
588 | 588 | if (!$body instanceof HTTP_Request2_MultipartBody) { |
589 | - $this->body = (string)$body; |
|
589 | + $this->body = (string) $body; |
|
590 | 590 | } else { |
591 | 591 | $this->body = $body; |
592 | 592 | } |
@@ -658,10 +658,10 @@ discard block |
||
658 | 658 | $fileData = $this->fopenWrapper($filename, empty($contentType)); |
659 | 659 | $this->uploads[$fieldName] = array( |
660 | 660 | 'fp' => $fileData['fp'], |
661 | - 'filename' => !empty($sendFilename)? $sendFilename |
|
662 | - :(is_string($filename)? basename($filename): 'anonymous.blob') , |
|
661 | + 'filename' => !empty($sendFilename) ? $sendFilename |
|
662 | + :(is_string($filename) ? basename($filename) : 'anonymous.blob'), |
|
663 | 663 | 'size' => $fileData['size'], |
664 | - 'type' => empty($contentType)? $fileData['type']: $contentType |
|
664 | + 'type' => empty($contentType) ? $fileData['type'] : $contentType |
|
665 | 665 | ); |
666 | 666 | } else { |
667 | 667 | $fps = $names = $sizes = $types = array(); |
@@ -671,10 +671,10 @@ discard block |
||
671 | 671 | } |
672 | 672 | $fileData = $this->fopenWrapper($f[0], empty($f[2])); |
673 | 673 | $fps[] = $fileData['fp']; |
674 | - $names[] = !empty($f[1])? $f[1] |
|
675 | - :(is_string($f[0])? basename($f[0]): 'anonymous.blob'); |
|
674 | + $names[] = !empty($f[1]) ? $f[1] |
|
675 | + :(is_string($f[0]) ? basename($f[0]) : 'anonymous.blob'); |
|
676 | 676 | $sizes[] = $fileData['size']; |
677 | - $types[] = empty($f[2])? $fileData['type']: $f[2]; |
|
677 | + $types[] = empty($f[2]) ? $fileData['type'] : $f[2]; |
|
678 | 678 | } |
679 | 679 | $this->uploads[$fieldName] = array( |
680 | 680 | 'fp' => $fps, 'filename' => $names, 'size' => $sizes, 'type' => $types |
@@ -827,12 +827,12 @@ discard block |
||
827 | 827 | if (is_string($adapter)) { |
828 | 828 | if (!class_exists($adapter, false)) { |
829 | 829 | if (false === strpos($adapter, '_')) { |
830 | - $adapter = 'HTTP_Request2_Adapter_' . ucfirst($adapter); |
|
830 | + $adapter = 'HTTP_Request2_Adapter_'.ucfirst($adapter); |
|
831 | 831 | } |
832 | 832 | if (!class_exists($adapter, false) |
833 | 833 | && preg_match('/^HTTP_Request2_Adapter_([a-zA-Z0-9]+)$/', $adapter) |
834 | 834 | ) { |
835 | - include_once str_replace('_', DIRECTORY_SEPARATOR, $adapter) . '.php'; |
|
835 | + include_once str_replace('_', DIRECTORY_SEPARATOR, $adapter).'.php'; |
|
836 | 836 | } |
837 | 837 | if (!class_exists($adapter, false)) { |
838 | 838 | throw new HTTP_Request2_LogicException( |
@@ -915,7 +915,7 @@ discard block |
||
915 | 915 | throw new HTTP_Request2_LogicException( |
916 | 916 | 'HTTP_Request2 needs an absolute HTTP(S) request URL, ' |
917 | 917 | . ($this->url instanceof Net_URL2 |
918 | - ? "'" . $this->url->__toString() . "'" : 'none') |
|
918 | + ? "'".$this->url->__toString()."'" : 'none') |
|
919 | 919 | . ' given', |
920 | 920 | HTTP_Request2_Exception::INVALID_ARGUMENT |
921 | 921 | ); |
@@ -973,7 +973,7 @@ discard block |
||
973 | 973 | ); |
974 | 974 | } |
975 | 975 | $fileData = array( |
976 | - 'fp' => is_string($file)? null: $file, |
|
976 | + 'fp' => is_string($file) ? null: $file, |
|
977 | 977 | 'type' => 'application/octet-stream', |
978 | 978 | 'size' => 0 |
979 | 979 | ); |
@@ -1024,7 +1024,7 @@ discard block |
||
1024 | 1024 | if (empty($info) && function_exists('mime_content_type')) { |
1025 | 1025 | return mime_content_type($filename); |
1026 | 1026 | } |
1027 | - return empty($info)? 'application/octet-stream': $info; |
|
1027 | + return empty($info) ? 'application/octet-stream' : $info; |
|
1028 | 1028 | } |
1029 | 1029 | } |
1030 | 1030 | ?> |
@@ -268,29 +268,29 @@ discard block |
||
268 | 268 | |
269 | 269 | // set HTTP version |
270 | 270 | switch ($this->request->getConfig('protocol_version')) { |
271 | - case '1.0': |
|
272 | - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
|
273 | - break; |
|
274 | - case '1.1': |
|
275 | - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
|
271 | + case '1.0': |
|
272 | + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
|
273 | + break; |
|
274 | + case '1.1': |
|
275 | + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | // set request method |
279 | 279 | switch ($this->request->getMethod()) { |
280 | - case HTTP_Request2::METHOD_GET: |
|
281 | - curl_setopt($ch, CURLOPT_HTTPGET, true); |
|
282 | - break; |
|
283 | - case HTTP_Request2::METHOD_POST: |
|
284 | - curl_setopt($ch, CURLOPT_POST, true); |
|
285 | - break; |
|
286 | - case HTTP_Request2::METHOD_HEAD: |
|
287 | - curl_setopt($ch, CURLOPT_NOBODY, true); |
|
288 | - break; |
|
289 | - case HTTP_Request2::METHOD_PUT: |
|
290 | - curl_setopt($ch, CURLOPT_UPLOAD, true); |
|
291 | - break; |
|
292 | - default: |
|
293 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->request->getMethod()); |
|
280 | + case HTTP_Request2::METHOD_GET: |
|
281 | + curl_setopt($ch, CURLOPT_HTTPGET, true); |
|
282 | + break; |
|
283 | + case HTTP_Request2::METHOD_POST: |
|
284 | + curl_setopt($ch, CURLOPT_POST, true); |
|
285 | + break; |
|
286 | + case HTTP_Request2::METHOD_HEAD: |
|
287 | + curl_setopt($ch, CURLOPT_NOBODY, true); |
|
288 | + break; |
|
289 | + case HTTP_Request2::METHOD_PUT: |
|
290 | + curl_setopt($ch, CURLOPT_UPLOAD, true); |
|
291 | + break; |
|
292 | + default: |
|
293 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->request->getMethod()); |
|
294 | 294 | } |
295 | 295 | |
296 | 296 | // set proxy, if needed |
@@ -307,25 +307,25 @@ discard block |
||
307 | 307 | $user . ':' . $this->request->getConfig('proxy_password') |
308 | 308 | ); |
309 | 309 | switch ($this->request->getConfig('proxy_auth_scheme')) { |
310 | - case HTTP_Request2::AUTH_BASIC: |
|
311 | - curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); |
|
312 | - break; |
|
313 | - case HTTP_Request2::AUTH_DIGEST: |
|
314 | - curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_DIGEST); |
|
310 | + case HTTP_Request2::AUTH_BASIC: |
|
311 | + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); |
|
312 | + break; |
|
313 | + case HTTP_Request2::AUTH_DIGEST: |
|
314 | + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_DIGEST); |
|
315 | 315 | } |
316 | 316 | } |
317 | 317 | if ($type = $this->request->getConfig('proxy_type')) { |
318 | 318 | switch ($type) { |
319 | - case 'http': |
|
320 | - curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
321 | - break; |
|
322 | - case 'socks5': |
|
323 | - curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
|
324 | - break; |
|
325 | - default: |
|
326 | - throw new HTTP_Request2_NotImplementedException( |
|
327 | - "Proxy type '{$type}' is not supported" |
|
328 | - ); |
|
319 | + case 'http': |
|
320 | + curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
321 | + break; |
|
322 | + case 'socks5': |
|
323 | + curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
|
324 | + break; |
|
325 | + default: |
|
326 | + throw new HTTP_Request2_NotImplementedException( |
|
327 | + "Proxy type '{$type}' is not supported" |
|
328 | + ); |
|
329 | 329 | } |
330 | 330 | } |
331 | 331 | } |
@@ -334,11 +334,11 @@ discard block |
||
334 | 334 | if ($auth = $this->request->getAuth()) { |
335 | 335 | curl_setopt($ch, CURLOPT_USERPWD, $auth['user'] . ':' . $auth['password']); |
336 | 336 | switch ($auth['scheme']) { |
337 | - case HTTP_Request2::AUTH_BASIC: |
|
338 | - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
|
339 | - break; |
|
340 | - case HTTP_Request2::AUTH_DIGEST: |
|
341 | - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
|
337 | + case HTTP_Request2::AUTH_BASIC: |
|
338 | + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
|
339 | + break; |
|
340 | + case HTTP_Request2::AUTH_DIGEST: |
|
341 | + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
|
342 | 342 | } |
343 | 343 | } |
344 | 344 |
@@ -35,533 +35,533 @@ |
||
35 | 35 | */ |
36 | 36 | class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter |
37 | 37 | { |
38 | - /** |
|
39 | - * Mapping of header names to cURL options |
|
40 | - * @var array |
|
41 | - */ |
|
42 | - protected static $headerMap = array( |
|
43 | - 'accept-encoding' => CURLOPT_ENCODING, |
|
44 | - 'cookie' => CURLOPT_COOKIE, |
|
45 | - 'referer' => CURLOPT_REFERER, |
|
46 | - 'user-agent' => CURLOPT_USERAGENT |
|
47 | - ); |
|
48 | - |
|
49 | - /** |
|
50 | - * Mapping of SSL context options to cURL options |
|
51 | - * @var array |
|
52 | - */ |
|
53 | - protected static $sslContextMap = array( |
|
54 | - 'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER, |
|
55 | - 'ssl_cafile' => CURLOPT_CAINFO, |
|
56 | - 'ssl_capath' => CURLOPT_CAPATH, |
|
57 | - 'ssl_local_cert' => CURLOPT_SSLCERT, |
|
58 | - 'ssl_passphrase' => CURLOPT_SSLCERTPASSWD |
|
59 | - ); |
|
60 | - |
|
61 | - /** |
|
62 | - * Mapping of CURLE_* constants to Exception subclasses and error codes |
|
63 | - * @var array |
|
64 | - */ |
|
65 | - protected static $errorMap = array( |
|
66 | - CURLE_UNSUPPORTED_PROTOCOL => array('HTTP_Request2_MessageException', |
|
67 | - HTTP_Request2_Exception::NON_HTTP_REDIRECT), |
|
68 | - CURLE_COULDNT_RESOLVE_PROXY => array('HTTP_Request2_ConnectionException'), |
|
69 | - CURLE_COULDNT_RESOLVE_HOST => array('HTTP_Request2_ConnectionException'), |
|
70 | - CURLE_COULDNT_CONNECT => array('HTTP_Request2_ConnectionException'), |
|
71 | - // error returned from write callback |
|
72 | - CURLE_WRITE_ERROR => array('HTTP_Request2_MessageException', |
|
73 | - HTTP_Request2_Exception::NON_HTTP_REDIRECT), |
|
74 | - CURLE_OPERATION_TIMEOUTED => array('HTTP_Request2_MessageException', |
|
75 | - HTTP_Request2_Exception::TIMEOUT), |
|
76 | - CURLE_HTTP_RANGE_ERROR => array('HTTP_Request2_MessageException'), |
|
77 | - CURLE_SSL_CONNECT_ERROR => array('HTTP_Request2_ConnectionException'), |
|
78 | - CURLE_LIBRARY_NOT_FOUND => array('HTTP_Request2_LogicException', |
|
79 | - HTTP_Request2_Exception::MISCONFIGURATION), |
|
80 | - CURLE_FUNCTION_NOT_FOUND => array('HTTP_Request2_LogicException', |
|
81 | - HTTP_Request2_Exception::MISCONFIGURATION), |
|
82 | - CURLE_ABORTED_BY_CALLBACK => array('HTTP_Request2_MessageException', |
|
83 | - HTTP_Request2_Exception::NON_HTTP_REDIRECT), |
|
84 | - CURLE_TOO_MANY_REDIRECTS => array('HTTP_Request2_MessageException', |
|
85 | - HTTP_Request2_Exception::TOO_MANY_REDIRECTS), |
|
86 | - CURLE_SSL_PEER_CERTIFICATE => array('HTTP_Request2_ConnectionException'), |
|
87 | - CURLE_GOT_NOTHING => array('HTTP_Request2_MessageException'), |
|
88 | - CURLE_SSL_ENGINE_NOTFOUND => array('HTTP_Request2_LogicException', |
|
89 | - HTTP_Request2_Exception::MISCONFIGURATION), |
|
90 | - CURLE_SSL_ENGINE_SETFAILED => array('HTTP_Request2_LogicException', |
|
91 | - HTTP_Request2_Exception::MISCONFIGURATION), |
|
92 | - CURLE_SEND_ERROR => array('HTTP_Request2_MessageException'), |
|
93 | - CURLE_RECV_ERROR => array('HTTP_Request2_MessageException'), |
|
94 | - CURLE_SSL_CERTPROBLEM => array('HTTP_Request2_LogicException', |
|
95 | - HTTP_Request2_Exception::INVALID_ARGUMENT), |
|
96 | - CURLE_SSL_CIPHER => array('HTTP_Request2_ConnectionException'), |
|
97 | - CURLE_SSL_CACERT => array('HTTP_Request2_ConnectionException'), |
|
98 | - CURLE_BAD_CONTENT_ENCODING => array('HTTP_Request2_MessageException'), |
|
99 | - ); |
|
100 | - |
|
101 | - /** |
|
102 | - * Response being received |
|
103 | - * @var HTTP_Request2_Response |
|
104 | - */ |
|
105 | - protected $response; |
|
106 | - |
|
107 | - /** |
|
108 | - * Whether 'sentHeaders' event was sent to observers |
|
109 | - * @var boolean |
|
110 | - */ |
|
111 | - protected $eventSentHeaders = false; |
|
112 | - |
|
113 | - /** |
|
114 | - * Whether 'receivedHeaders' event was sent to observers |
|
115 | - * @var boolean |
|
116 | - */ |
|
117 | - protected $eventReceivedHeaders = false; |
|
118 | - |
|
119 | - /** |
|
120 | - * Position within request body |
|
121 | - * @var integer |
|
122 | - * @see callbackReadBody() |
|
123 | - */ |
|
124 | - protected $position = 0; |
|
125 | - |
|
126 | - /** |
|
127 | - * Information about last transfer, as returned by curl_getinfo() |
|
128 | - * @var array |
|
129 | - */ |
|
130 | - protected $lastInfo; |
|
131 | - |
|
132 | - /** |
|
133 | - * Creates a subclass of HTTP_Request2_Exception from curl error data |
|
134 | - * |
|
135 | - * @param resource $ch curl handle |
|
136 | - * |
|
137 | - * @return HTTP_Request2_Exception |
|
138 | - */ |
|
139 | - protected static function wrapCurlError($ch) |
|
140 | - { |
|
141 | - $nativeCode = curl_errno($ch); |
|
142 | - $message = 'Curl error: ' . curl_error($ch); |
|
143 | - if (!isset(self::$errorMap[$nativeCode])) { |
|
144 | - return new HTTP_Request2_Exception($message, 0, $nativeCode); |
|
145 | - } else { |
|
146 | - $class = self::$errorMap[$nativeCode][0]; |
|
147 | - $code = empty(self::$errorMap[$nativeCode][1]) |
|
148 | - ? 0 : self::$errorMap[$nativeCode][1]; |
|
149 | - return new $class($message, $code, $nativeCode); |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sends request to the remote server and returns its response |
|
155 | - * |
|
156 | - * @param HTTP_Request2 $request HTTP request message |
|
157 | - * |
|
158 | - * @return HTTP_Request2_Response |
|
159 | - * @throws HTTP_Request2_Exception |
|
160 | - */ |
|
161 | - public function sendRequest(HTTP_Request2 $request) |
|
162 | - { |
|
163 | - if (!extension_loaded('curl')) { |
|
164 | - throw new HTTP_Request2_LogicException( |
|
165 | - 'cURL extension not available', HTTP_Request2_Exception::MISCONFIGURATION |
|
166 | - ); |
|
167 | - } |
|
168 | - |
|
169 | - $this->request = $request; |
|
170 | - $this->response = null; |
|
171 | - $this->position = 0; |
|
172 | - $this->eventSentHeaders = false; |
|
173 | - $this->eventReceivedHeaders = false; |
|
174 | - |
|
175 | - try { |
|
176 | - if (false === curl_exec($ch = $this->createCurlHandle())) { |
|
177 | - $e = self::wrapCurlError($ch); |
|
178 | - } |
|
179 | - } catch (Exception $e) { |
|
180 | - } |
|
181 | - if (isset($ch)) { |
|
182 | - $this->lastInfo = curl_getinfo($ch); |
|
183 | - curl_close($ch); |
|
184 | - } |
|
185 | - |
|
186 | - $response = $this->response; |
|
187 | - unset($this->request, $this->requestBody, $this->response); |
|
188 | - |
|
189 | - if (!empty($e)) { |
|
190 | - throw $e; |
|
191 | - } |
|
192 | - |
|
193 | - if ($jar = $request->getCookieJar()) { |
|
194 | - $jar->addCookiesFromResponse($response, $request->getUrl()); |
|
195 | - } |
|
196 | - |
|
197 | - if (0 < $this->lastInfo['size_download']) { |
|
198 | - $request->setLastEvent('receivedBody', $response); |
|
199 | - } |
|
200 | - return $response; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Returns information about last transfer |
|
205 | - * |
|
206 | - * @return array associative array as returned by curl_getinfo() |
|
207 | - */ |
|
208 | - public function getInfo() |
|
209 | - { |
|
210 | - return $this->lastInfo; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Creates a new cURL handle and populates it with data from the request |
|
215 | - * |
|
216 | - * @return resource a cURL handle, as created by curl_init() |
|
217 | - * @throws HTTP_Request2_LogicException |
|
218 | - * @throws HTTP_Request2_NotImplementedException |
|
219 | - */ |
|
220 | - protected function createCurlHandle() |
|
221 | - { |
|
222 | - $ch = curl_init(); |
|
223 | - |
|
224 | - curl_setopt_array($ch, array( |
|
225 | - // setup write callbacks |
|
226 | - CURLOPT_HEADERFUNCTION => array($this, 'callbackWriteHeader'), |
|
227 | - CURLOPT_WRITEFUNCTION => array($this, 'callbackWriteBody'), |
|
228 | - // buffer size |
|
229 | - CURLOPT_BUFFERSIZE => $this->request->getConfig('buffer_size'), |
|
230 | - // connection timeout |
|
231 | - CURLOPT_CONNECTTIMEOUT => $this->request->getConfig('connect_timeout'), |
|
232 | - // save full outgoing headers, in case someone is interested |
|
233 | - CURLINFO_HEADER_OUT => true, |
|
234 | - // request url |
|
235 | - CURLOPT_URL => $this->request->getUrl()->getUrl() |
|
236 | - )); |
|
237 | - |
|
238 | - // set up redirects |
|
239 | - if (!$this->request->getConfig('follow_redirects')) { |
|
240 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); |
|
241 | - } else { |
|
242 | - if (!@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)) { |
|
243 | - throw new HTTP_Request2_LogicException( |
|
244 | - 'Redirect support in curl is unavailable due to open_basedir or safe_mode setting', |
|
245 | - HTTP_Request2_Exception::MISCONFIGURATION |
|
246 | - ); |
|
247 | - } |
|
248 | - curl_setopt($ch, CURLOPT_MAXREDIRS, $this->request->getConfig('max_redirects')); |
|
249 | - // limit redirects to http(s), works in 5.2.10+ |
|
250 | - if (defined('CURLOPT_REDIR_PROTOCOLS')) { |
|
251 | - curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); |
|
252 | - } |
|
253 | - // works in 5.3.2+, http://bugs.php.net/bug.php?id=49571 |
|
254 | - if ($this->request->getConfig('strict_redirects') && defined('CURLOPT_POSTREDIR')) { |
|
255 | - curl_setopt($ch, CURLOPT_POSTREDIR, 3); |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - // set local IP via CURLOPT_INTERFACE (request #19515) |
|
260 | - if ($ip = $this->request->getConfig('local_ip')) { |
|
261 | - curl_setopt($ch, CURLOPT_INTERFACE, $ip); |
|
262 | - } |
|
263 | - |
|
264 | - // request timeout |
|
265 | - if ($timeout = $this->request->getConfig('timeout')) { |
|
266 | - curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
|
267 | - } |
|
268 | - |
|
269 | - // set HTTP version |
|
270 | - switch ($this->request->getConfig('protocol_version')) { |
|
271 | - case '1.0': |
|
272 | - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
|
273 | - break; |
|
274 | - case '1.1': |
|
275 | - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
|
276 | - } |
|
277 | - |
|
278 | - // set request method |
|
279 | - switch ($this->request->getMethod()) { |
|
280 | - case HTTP_Request2::METHOD_GET: |
|
281 | - curl_setopt($ch, CURLOPT_HTTPGET, true); |
|
282 | - break; |
|
283 | - case HTTP_Request2::METHOD_POST: |
|
284 | - curl_setopt($ch, CURLOPT_POST, true); |
|
285 | - break; |
|
286 | - case HTTP_Request2::METHOD_HEAD: |
|
287 | - curl_setopt($ch, CURLOPT_NOBODY, true); |
|
288 | - break; |
|
289 | - case HTTP_Request2::METHOD_PUT: |
|
290 | - curl_setopt($ch, CURLOPT_UPLOAD, true); |
|
291 | - break; |
|
292 | - default: |
|
293 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->request->getMethod()); |
|
294 | - } |
|
295 | - |
|
296 | - // set proxy, if needed |
|
297 | - if ($host = $this->request->getConfig('proxy_host')) { |
|
298 | - if (!($port = $this->request->getConfig('proxy_port'))) { |
|
299 | - throw new HTTP_Request2_LogicException( |
|
300 | - 'Proxy port not provided', HTTP_Request2_Exception::MISSING_VALUE |
|
301 | - ); |
|
302 | - } |
|
303 | - curl_setopt($ch, CURLOPT_PROXY, $host . ':' . $port); |
|
304 | - if ($user = $this->request->getConfig('proxy_user')) { |
|
305 | - curl_setopt( |
|
306 | - $ch, CURLOPT_PROXYUSERPWD, |
|
307 | - $user . ':' . $this->request->getConfig('proxy_password') |
|
308 | - ); |
|
309 | - switch ($this->request->getConfig('proxy_auth_scheme')) { |
|
310 | - case HTTP_Request2::AUTH_BASIC: |
|
311 | - curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); |
|
312 | - break; |
|
313 | - case HTTP_Request2::AUTH_DIGEST: |
|
314 | - curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_DIGEST); |
|
315 | - } |
|
316 | - } |
|
317 | - if ($type = $this->request->getConfig('proxy_type')) { |
|
318 | - switch ($type) { |
|
319 | - case 'http': |
|
320 | - curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
321 | - break; |
|
322 | - case 'socks5': |
|
323 | - curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
|
324 | - break; |
|
325 | - default: |
|
326 | - throw new HTTP_Request2_NotImplementedException( |
|
327 | - "Proxy type '{$type}' is not supported" |
|
328 | - ); |
|
329 | - } |
|
330 | - } |
|
331 | - } |
|
332 | - |
|
333 | - // set authentication data |
|
334 | - if ($auth = $this->request->getAuth()) { |
|
335 | - curl_setopt($ch, CURLOPT_USERPWD, $auth['user'] . ':' . $auth['password']); |
|
336 | - switch ($auth['scheme']) { |
|
337 | - case HTTP_Request2::AUTH_BASIC: |
|
338 | - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
|
339 | - break; |
|
340 | - case HTTP_Request2::AUTH_DIGEST: |
|
341 | - curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
|
342 | - } |
|
343 | - } |
|
344 | - |
|
345 | - // set SSL options |
|
346 | - foreach ($this->request->getConfig() as $name => $value) { |
|
347 | - if ('ssl_verify_host' == $name && null !== $value) { |
|
348 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $value? 2: 0); |
|
349 | - } elseif (isset(self::$sslContextMap[$name]) && null !== $value) { |
|
350 | - curl_setopt($ch, self::$sslContextMap[$name], $value); |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - $headers = $this->request->getHeaders(); |
|
355 | - // make cURL automagically send proper header |
|
356 | - if (!isset($headers['accept-encoding'])) { |
|
357 | - $headers['accept-encoding'] = ''; |
|
358 | - } |
|
359 | - |
|
360 | - if (($jar = $this->request->getCookieJar()) |
|
361 | - && ($cookies = $jar->getMatching($this->request->getUrl(), true)) |
|
362 | - ) { |
|
363 | - $headers['cookie'] = (empty($headers['cookie'])? '': $headers['cookie'] . '; ') . $cookies; |
|
364 | - } |
|
365 | - |
|
366 | - // set headers having special cURL keys |
|
367 | - foreach (self::$headerMap as $name => $option) { |
|
368 | - if (isset($headers[$name])) { |
|
369 | - curl_setopt($ch, $option, $headers[$name]); |
|
370 | - unset($headers[$name]); |
|
371 | - } |
|
372 | - } |
|
373 | - |
|
374 | - $this->calculateRequestLength($headers); |
|
375 | - if (isset($headers['content-length']) || isset($headers['transfer-encoding'])) { |
|
376 | - $this->workaroundPhpBug47204($ch, $headers); |
|
377 | - } |
|
378 | - |
|
379 | - // set headers not having special keys |
|
380 | - $headersFmt = array(); |
|
381 | - foreach ($headers as $name => $value) { |
|
382 | - $canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); |
|
383 | - $headersFmt[] = $canonicalName . ': ' . $value; |
|
384 | - } |
|
385 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $headersFmt); |
|
386 | - |
|
387 | - return $ch; |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Workaround for PHP bug #47204 that prevents rewinding request body |
|
392 | - * |
|
393 | - * The workaround consists of reading the entire request body into memory |
|
394 | - * and setting it as CURLOPT_POSTFIELDS, so it isn't recommended for large |
|
395 | - * file uploads, use Socket adapter instead. |
|
396 | - * |
|
397 | - * @param resource $ch cURL handle |
|
398 | - * @param array &$headers Request headers |
|
399 | - */ |
|
400 | - protected function workaroundPhpBug47204($ch, &$headers) |
|
401 | - { |
|
402 | - // no redirects, no digest auth -> probably no rewind needed |
|
403 | - if (!$this->request->getConfig('follow_redirects') |
|
404 | - && (!($auth = $this->request->getAuth()) |
|
405 | - || HTTP_Request2::AUTH_DIGEST != $auth['scheme']) |
|
406 | - ) { |
|
407 | - curl_setopt($ch, CURLOPT_READFUNCTION, array($this, 'callbackReadBody')); |
|
408 | - |
|
409 | - } else { |
|
410 | - // rewind may be needed, read the whole body into memory |
|
411 | - if ($this->requestBody instanceof HTTP_Request2_MultipartBody) { |
|
412 | - $this->requestBody = $this->requestBody->__toString(); |
|
413 | - |
|
414 | - } elseif (is_resource($this->requestBody)) { |
|
415 | - $fp = $this->requestBody; |
|
416 | - $this->requestBody = ''; |
|
417 | - while (!feof($fp)) { |
|
418 | - $this->requestBody .= fread($fp, 16384); |
|
419 | - } |
|
420 | - } |
|
421 | - // curl hangs up if content-length is present |
|
422 | - unset($headers['content-length']); |
|
423 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody); |
|
424 | - } |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Callback function called by cURL for reading the request body |
|
429 | - * |
|
430 | - * @param resource $ch cURL handle |
|
431 | - * @param resource $fd file descriptor (not used) |
|
432 | - * @param integer $length maximum length of data to return |
|
433 | - * |
|
434 | - * @return string part of the request body, up to $length bytes |
|
435 | - */ |
|
436 | - protected function callbackReadBody($ch, $fd, $length) |
|
437 | - { |
|
438 | - if (!$this->eventSentHeaders) { |
|
439 | - $this->request->setLastEvent( |
|
440 | - 'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT) |
|
441 | - ); |
|
442 | - $this->eventSentHeaders = true; |
|
443 | - } |
|
444 | - if (in_array($this->request->getMethod(), self::$bodyDisallowed) |
|
445 | - || 0 == $this->contentLength || $this->position >= $this->contentLength |
|
446 | - ) { |
|
447 | - return ''; |
|
448 | - } |
|
449 | - if (is_string($this->requestBody)) { |
|
450 | - $string = substr($this->requestBody, $this->position, $length); |
|
451 | - } elseif (is_resource($this->requestBody)) { |
|
452 | - $string = fread($this->requestBody, $length); |
|
453 | - } else { |
|
454 | - $string = $this->requestBody->read($length); |
|
455 | - } |
|
456 | - $this->request->setLastEvent('sentBodyPart', strlen($string)); |
|
457 | - $this->position += strlen($string); |
|
458 | - return $string; |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Callback function called by cURL for saving the response headers |
|
463 | - * |
|
464 | - * @param resource $ch cURL handle |
|
465 | - * @param string $string response header (with trailing CRLF) |
|
466 | - * |
|
467 | - * @return integer number of bytes saved |
|
468 | - * @see HTTP_Request2_Response::parseHeaderLine() |
|
469 | - */ |
|
470 | - protected function callbackWriteHeader($ch, $string) |
|
471 | - { |
|
472 | - // we may receive a second set of headers if doing e.g. digest auth |
|
473 | - if ($this->eventReceivedHeaders || !$this->eventSentHeaders) { |
|
474 | - // don't bother with 100-Continue responses (bug #15785) |
|
475 | - if (!$this->eventSentHeaders |
|
476 | - || $this->response->getStatus() >= 200 |
|
477 | - ) { |
|
478 | - $this->request->setLastEvent( |
|
479 | - 'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT) |
|
480 | - ); |
|
481 | - } |
|
482 | - $upload = curl_getinfo($ch, CURLINFO_SIZE_UPLOAD); |
|
483 | - // if body wasn't read by a callback, send event with total body size |
|
484 | - if ($upload > $this->position) { |
|
485 | - $this->request->setLastEvent( |
|
486 | - 'sentBodyPart', $upload - $this->position |
|
487 | - ); |
|
488 | - $this->position = $upload; |
|
489 | - } |
|
490 | - if ($upload && (!$this->eventSentHeaders |
|
491 | - || $this->response->getStatus() >= 200) |
|
492 | - ) { |
|
493 | - $this->request->setLastEvent('sentBody', $upload); |
|
494 | - } |
|
495 | - $this->eventSentHeaders = true; |
|
496 | - // we'll need a new response object |
|
497 | - if ($this->eventReceivedHeaders) { |
|
498 | - $this->eventReceivedHeaders = false; |
|
499 | - $this->response = null; |
|
500 | - } |
|
501 | - } |
|
502 | - if (empty($this->response)) { |
|
503 | - $this->response = new HTTP_Request2_Response( |
|
504 | - $string, false, curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) |
|
505 | - ); |
|
506 | - } else { |
|
507 | - $this->response->parseHeaderLine($string); |
|
508 | - if ('' == trim($string)) { |
|
509 | - // don't bother with 100-Continue responses (bug #15785) |
|
510 | - if (200 <= $this->response->getStatus()) { |
|
511 | - $this->request->setLastEvent('receivedHeaders', $this->response); |
|
512 | - } |
|
513 | - |
|
514 | - if ($this->request->getConfig('follow_redirects') && $this->response->isRedirect()) { |
|
515 | - $redirectUrl = new Net_URL2($this->response->getHeader('location')); |
|
516 | - |
|
517 | - // for versions lower than 5.2.10, check the redirection URL protocol |
|
518 | - if (!defined('CURLOPT_REDIR_PROTOCOLS') && $redirectUrl->isAbsolute() |
|
519 | - && !in_array($redirectUrl->getScheme(), array('http', 'https')) |
|
520 | - ) { |
|
521 | - return -1; |
|
522 | - } |
|
523 | - |
|
524 | - if ($jar = $this->request->getCookieJar()) { |
|
525 | - $jar->addCookiesFromResponse($this->response, $this->request->getUrl()); |
|
526 | - if (!$redirectUrl->isAbsolute()) { |
|
527 | - $redirectUrl = $this->request->getUrl()->resolve($redirectUrl); |
|
528 | - } |
|
529 | - if ($cookies = $jar->getMatching($redirectUrl, true)) { |
|
530 | - curl_setopt($ch, CURLOPT_COOKIE, $cookies); |
|
531 | - } |
|
532 | - } |
|
533 | - } |
|
534 | - $this->eventReceivedHeaders = true; |
|
535 | - } |
|
536 | - } |
|
537 | - return strlen($string); |
|
538 | - } |
|
539 | - |
|
540 | - /** |
|
541 | - * Callback function called by cURL for saving the response body |
|
542 | - * |
|
543 | - * @param resource $ch cURL handle (not used) |
|
544 | - * @param string $string part of the response body |
|
545 | - * |
|
546 | - * @return integer number of bytes saved |
|
547 | - * @throws HTTP_Request2_MessageException |
|
548 | - * @see HTTP_Request2_Response::appendBody() |
|
549 | - */ |
|
550 | - protected function callbackWriteBody($ch, $string) |
|
551 | - { |
|
552 | - // cURL calls WRITEFUNCTION callback without calling HEADERFUNCTION if |
|
553 | - // response doesn't start with proper HTTP status line (see bug #15716) |
|
554 | - if (empty($this->response)) { |
|
555 | - throw new HTTP_Request2_MessageException( |
|
556 | - "Malformed response: {$string}", |
|
557 | - HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
558 | - ); |
|
559 | - } |
|
560 | - if ($this->request->getConfig('store_body')) { |
|
561 | - $this->response->appendBody($string); |
|
562 | - } |
|
563 | - $this->request->setLastEvent('receivedBodyPart', $string); |
|
564 | - return strlen($string); |
|
565 | - } |
|
38 | + /** |
|
39 | + * Mapping of header names to cURL options |
|
40 | + * @var array |
|
41 | + */ |
|
42 | + protected static $headerMap = array( |
|
43 | + 'accept-encoding' => CURLOPT_ENCODING, |
|
44 | + 'cookie' => CURLOPT_COOKIE, |
|
45 | + 'referer' => CURLOPT_REFERER, |
|
46 | + 'user-agent' => CURLOPT_USERAGENT |
|
47 | + ); |
|
48 | + |
|
49 | + /** |
|
50 | + * Mapping of SSL context options to cURL options |
|
51 | + * @var array |
|
52 | + */ |
|
53 | + protected static $sslContextMap = array( |
|
54 | + 'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER, |
|
55 | + 'ssl_cafile' => CURLOPT_CAINFO, |
|
56 | + 'ssl_capath' => CURLOPT_CAPATH, |
|
57 | + 'ssl_local_cert' => CURLOPT_SSLCERT, |
|
58 | + 'ssl_passphrase' => CURLOPT_SSLCERTPASSWD |
|
59 | + ); |
|
60 | + |
|
61 | + /** |
|
62 | + * Mapping of CURLE_* constants to Exception subclasses and error codes |
|
63 | + * @var array |
|
64 | + */ |
|
65 | + protected static $errorMap = array( |
|
66 | + CURLE_UNSUPPORTED_PROTOCOL => array('HTTP_Request2_MessageException', |
|
67 | + HTTP_Request2_Exception::NON_HTTP_REDIRECT), |
|
68 | + CURLE_COULDNT_RESOLVE_PROXY => array('HTTP_Request2_ConnectionException'), |
|
69 | + CURLE_COULDNT_RESOLVE_HOST => array('HTTP_Request2_ConnectionException'), |
|
70 | + CURLE_COULDNT_CONNECT => array('HTTP_Request2_ConnectionException'), |
|
71 | + // error returned from write callback |
|
72 | + CURLE_WRITE_ERROR => array('HTTP_Request2_MessageException', |
|
73 | + HTTP_Request2_Exception::NON_HTTP_REDIRECT), |
|
74 | + CURLE_OPERATION_TIMEOUTED => array('HTTP_Request2_MessageException', |
|
75 | + HTTP_Request2_Exception::TIMEOUT), |
|
76 | + CURLE_HTTP_RANGE_ERROR => array('HTTP_Request2_MessageException'), |
|
77 | + CURLE_SSL_CONNECT_ERROR => array('HTTP_Request2_ConnectionException'), |
|
78 | + CURLE_LIBRARY_NOT_FOUND => array('HTTP_Request2_LogicException', |
|
79 | + HTTP_Request2_Exception::MISCONFIGURATION), |
|
80 | + CURLE_FUNCTION_NOT_FOUND => array('HTTP_Request2_LogicException', |
|
81 | + HTTP_Request2_Exception::MISCONFIGURATION), |
|
82 | + CURLE_ABORTED_BY_CALLBACK => array('HTTP_Request2_MessageException', |
|
83 | + HTTP_Request2_Exception::NON_HTTP_REDIRECT), |
|
84 | + CURLE_TOO_MANY_REDIRECTS => array('HTTP_Request2_MessageException', |
|
85 | + HTTP_Request2_Exception::TOO_MANY_REDIRECTS), |
|
86 | + CURLE_SSL_PEER_CERTIFICATE => array('HTTP_Request2_ConnectionException'), |
|
87 | + CURLE_GOT_NOTHING => array('HTTP_Request2_MessageException'), |
|
88 | + CURLE_SSL_ENGINE_NOTFOUND => array('HTTP_Request2_LogicException', |
|
89 | + HTTP_Request2_Exception::MISCONFIGURATION), |
|
90 | + CURLE_SSL_ENGINE_SETFAILED => array('HTTP_Request2_LogicException', |
|
91 | + HTTP_Request2_Exception::MISCONFIGURATION), |
|
92 | + CURLE_SEND_ERROR => array('HTTP_Request2_MessageException'), |
|
93 | + CURLE_RECV_ERROR => array('HTTP_Request2_MessageException'), |
|
94 | + CURLE_SSL_CERTPROBLEM => array('HTTP_Request2_LogicException', |
|
95 | + HTTP_Request2_Exception::INVALID_ARGUMENT), |
|
96 | + CURLE_SSL_CIPHER => array('HTTP_Request2_ConnectionException'), |
|
97 | + CURLE_SSL_CACERT => array('HTTP_Request2_ConnectionException'), |
|
98 | + CURLE_BAD_CONTENT_ENCODING => array('HTTP_Request2_MessageException'), |
|
99 | + ); |
|
100 | + |
|
101 | + /** |
|
102 | + * Response being received |
|
103 | + * @var HTTP_Request2_Response |
|
104 | + */ |
|
105 | + protected $response; |
|
106 | + |
|
107 | + /** |
|
108 | + * Whether 'sentHeaders' event was sent to observers |
|
109 | + * @var boolean |
|
110 | + */ |
|
111 | + protected $eventSentHeaders = false; |
|
112 | + |
|
113 | + /** |
|
114 | + * Whether 'receivedHeaders' event was sent to observers |
|
115 | + * @var boolean |
|
116 | + */ |
|
117 | + protected $eventReceivedHeaders = false; |
|
118 | + |
|
119 | + /** |
|
120 | + * Position within request body |
|
121 | + * @var integer |
|
122 | + * @see callbackReadBody() |
|
123 | + */ |
|
124 | + protected $position = 0; |
|
125 | + |
|
126 | + /** |
|
127 | + * Information about last transfer, as returned by curl_getinfo() |
|
128 | + * @var array |
|
129 | + */ |
|
130 | + protected $lastInfo; |
|
131 | + |
|
132 | + /** |
|
133 | + * Creates a subclass of HTTP_Request2_Exception from curl error data |
|
134 | + * |
|
135 | + * @param resource $ch curl handle |
|
136 | + * |
|
137 | + * @return HTTP_Request2_Exception |
|
138 | + */ |
|
139 | + protected static function wrapCurlError($ch) |
|
140 | + { |
|
141 | + $nativeCode = curl_errno($ch); |
|
142 | + $message = 'Curl error: ' . curl_error($ch); |
|
143 | + if (!isset(self::$errorMap[$nativeCode])) { |
|
144 | + return new HTTP_Request2_Exception($message, 0, $nativeCode); |
|
145 | + } else { |
|
146 | + $class = self::$errorMap[$nativeCode][0]; |
|
147 | + $code = empty(self::$errorMap[$nativeCode][1]) |
|
148 | + ? 0 : self::$errorMap[$nativeCode][1]; |
|
149 | + return new $class($message, $code, $nativeCode); |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sends request to the remote server and returns its response |
|
155 | + * |
|
156 | + * @param HTTP_Request2 $request HTTP request message |
|
157 | + * |
|
158 | + * @return HTTP_Request2_Response |
|
159 | + * @throws HTTP_Request2_Exception |
|
160 | + */ |
|
161 | + public function sendRequest(HTTP_Request2 $request) |
|
162 | + { |
|
163 | + if (!extension_loaded('curl')) { |
|
164 | + throw new HTTP_Request2_LogicException( |
|
165 | + 'cURL extension not available', HTTP_Request2_Exception::MISCONFIGURATION |
|
166 | + ); |
|
167 | + } |
|
168 | + |
|
169 | + $this->request = $request; |
|
170 | + $this->response = null; |
|
171 | + $this->position = 0; |
|
172 | + $this->eventSentHeaders = false; |
|
173 | + $this->eventReceivedHeaders = false; |
|
174 | + |
|
175 | + try { |
|
176 | + if (false === curl_exec($ch = $this->createCurlHandle())) { |
|
177 | + $e = self::wrapCurlError($ch); |
|
178 | + } |
|
179 | + } catch (Exception $e) { |
|
180 | + } |
|
181 | + if (isset($ch)) { |
|
182 | + $this->lastInfo = curl_getinfo($ch); |
|
183 | + curl_close($ch); |
|
184 | + } |
|
185 | + |
|
186 | + $response = $this->response; |
|
187 | + unset($this->request, $this->requestBody, $this->response); |
|
188 | + |
|
189 | + if (!empty($e)) { |
|
190 | + throw $e; |
|
191 | + } |
|
192 | + |
|
193 | + if ($jar = $request->getCookieJar()) { |
|
194 | + $jar->addCookiesFromResponse($response, $request->getUrl()); |
|
195 | + } |
|
196 | + |
|
197 | + if (0 < $this->lastInfo['size_download']) { |
|
198 | + $request->setLastEvent('receivedBody', $response); |
|
199 | + } |
|
200 | + return $response; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Returns information about last transfer |
|
205 | + * |
|
206 | + * @return array associative array as returned by curl_getinfo() |
|
207 | + */ |
|
208 | + public function getInfo() |
|
209 | + { |
|
210 | + return $this->lastInfo; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Creates a new cURL handle and populates it with data from the request |
|
215 | + * |
|
216 | + * @return resource a cURL handle, as created by curl_init() |
|
217 | + * @throws HTTP_Request2_LogicException |
|
218 | + * @throws HTTP_Request2_NotImplementedException |
|
219 | + */ |
|
220 | + protected function createCurlHandle() |
|
221 | + { |
|
222 | + $ch = curl_init(); |
|
223 | + |
|
224 | + curl_setopt_array($ch, array( |
|
225 | + // setup write callbacks |
|
226 | + CURLOPT_HEADERFUNCTION => array($this, 'callbackWriteHeader'), |
|
227 | + CURLOPT_WRITEFUNCTION => array($this, 'callbackWriteBody'), |
|
228 | + // buffer size |
|
229 | + CURLOPT_BUFFERSIZE => $this->request->getConfig('buffer_size'), |
|
230 | + // connection timeout |
|
231 | + CURLOPT_CONNECTTIMEOUT => $this->request->getConfig('connect_timeout'), |
|
232 | + // save full outgoing headers, in case someone is interested |
|
233 | + CURLINFO_HEADER_OUT => true, |
|
234 | + // request url |
|
235 | + CURLOPT_URL => $this->request->getUrl()->getUrl() |
|
236 | + )); |
|
237 | + |
|
238 | + // set up redirects |
|
239 | + if (!$this->request->getConfig('follow_redirects')) { |
|
240 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); |
|
241 | + } else { |
|
242 | + if (!@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)) { |
|
243 | + throw new HTTP_Request2_LogicException( |
|
244 | + 'Redirect support in curl is unavailable due to open_basedir or safe_mode setting', |
|
245 | + HTTP_Request2_Exception::MISCONFIGURATION |
|
246 | + ); |
|
247 | + } |
|
248 | + curl_setopt($ch, CURLOPT_MAXREDIRS, $this->request->getConfig('max_redirects')); |
|
249 | + // limit redirects to http(s), works in 5.2.10+ |
|
250 | + if (defined('CURLOPT_REDIR_PROTOCOLS')) { |
|
251 | + curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); |
|
252 | + } |
|
253 | + // works in 5.3.2+, http://bugs.php.net/bug.php?id=49571 |
|
254 | + if ($this->request->getConfig('strict_redirects') && defined('CURLOPT_POSTREDIR')) { |
|
255 | + curl_setopt($ch, CURLOPT_POSTREDIR, 3); |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + // set local IP via CURLOPT_INTERFACE (request #19515) |
|
260 | + if ($ip = $this->request->getConfig('local_ip')) { |
|
261 | + curl_setopt($ch, CURLOPT_INTERFACE, $ip); |
|
262 | + } |
|
263 | + |
|
264 | + // request timeout |
|
265 | + if ($timeout = $this->request->getConfig('timeout')) { |
|
266 | + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
|
267 | + } |
|
268 | + |
|
269 | + // set HTTP version |
|
270 | + switch ($this->request->getConfig('protocol_version')) { |
|
271 | + case '1.0': |
|
272 | + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
|
273 | + break; |
|
274 | + case '1.1': |
|
275 | + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
|
276 | + } |
|
277 | + |
|
278 | + // set request method |
|
279 | + switch ($this->request->getMethod()) { |
|
280 | + case HTTP_Request2::METHOD_GET: |
|
281 | + curl_setopt($ch, CURLOPT_HTTPGET, true); |
|
282 | + break; |
|
283 | + case HTTP_Request2::METHOD_POST: |
|
284 | + curl_setopt($ch, CURLOPT_POST, true); |
|
285 | + break; |
|
286 | + case HTTP_Request2::METHOD_HEAD: |
|
287 | + curl_setopt($ch, CURLOPT_NOBODY, true); |
|
288 | + break; |
|
289 | + case HTTP_Request2::METHOD_PUT: |
|
290 | + curl_setopt($ch, CURLOPT_UPLOAD, true); |
|
291 | + break; |
|
292 | + default: |
|
293 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->request->getMethod()); |
|
294 | + } |
|
295 | + |
|
296 | + // set proxy, if needed |
|
297 | + if ($host = $this->request->getConfig('proxy_host')) { |
|
298 | + if (!($port = $this->request->getConfig('proxy_port'))) { |
|
299 | + throw new HTTP_Request2_LogicException( |
|
300 | + 'Proxy port not provided', HTTP_Request2_Exception::MISSING_VALUE |
|
301 | + ); |
|
302 | + } |
|
303 | + curl_setopt($ch, CURLOPT_PROXY, $host . ':' . $port); |
|
304 | + if ($user = $this->request->getConfig('proxy_user')) { |
|
305 | + curl_setopt( |
|
306 | + $ch, CURLOPT_PROXYUSERPWD, |
|
307 | + $user . ':' . $this->request->getConfig('proxy_password') |
|
308 | + ); |
|
309 | + switch ($this->request->getConfig('proxy_auth_scheme')) { |
|
310 | + case HTTP_Request2::AUTH_BASIC: |
|
311 | + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); |
|
312 | + break; |
|
313 | + case HTTP_Request2::AUTH_DIGEST: |
|
314 | + curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_DIGEST); |
|
315 | + } |
|
316 | + } |
|
317 | + if ($type = $this->request->getConfig('proxy_type')) { |
|
318 | + switch ($type) { |
|
319 | + case 'http': |
|
320 | + curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
321 | + break; |
|
322 | + case 'socks5': |
|
323 | + curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
|
324 | + break; |
|
325 | + default: |
|
326 | + throw new HTTP_Request2_NotImplementedException( |
|
327 | + "Proxy type '{$type}' is not supported" |
|
328 | + ); |
|
329 | + } |
|
330 | + } |
|
331 | + } |
|
332 | + |
|
333 | + // set authentication data |
|
334 | + if ($auth = $this->request->getAuth()) { |
|
335 | + curl_setopt($ch, CURLOPT_USERPWD, $auth['user'] . ':' . $auth['password']); |
|
336 | + switch ($auth['scheme']) { |
|
337 | + case HTTP_Request2::AUTH_BASIC: |
|
338 | + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
|
339 | + break; |
|
340 | + case HTTP_Request2::AUTH_DIGEST: |
|
341 | + curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
|
342 | + } |
|
343 | + } |
|
344 | + |
|
345 | + // set SSL options |
|
346 | + foreach ($this->request->getConfig() as $name => $value) { |
|
347 | + if ('ssl_verify_host' == $name && null !== $value) { |
|
348 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $value? 2: 0); |
|
349 | + } elseif (isset(self::$sslContextMap[$name]) && null !== $value) { |
|
350 | + curl_setopt($ch, self::$sslContextMap[$name], $value); |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + $headers = $this->request->getHeaders(); |
|
355 | + // make cURL automagically send proper header |
|
356 | + if (!isset($headers['accept-encoding'])) { |
|
357 | + $headers['accept-encoding'] = ''; |
|
358 | + } |
|
359 | + |
|
360 | + if (($jar = $this->request->getCookieJar()) |
|
361 | + && ($cookies = $jar->getMatching($this->request->getUrl(), true)) |
|
362 | + ) { |
|
363 | + $headers['cookie'] = (empty($headers['cookie'])? '': $headers['cookie'] . '; ') . $cookies; |
|
364 | + } |
|
365 | + |
|
366 | + // set headers having special cURL keys |
|
367 | + foreach (self::$headerMap as $name => $option) { |
|
368 | + if (isset($headers[$name])) { |
|
369 | + curl_setopt($ch, $option, $headers[$name]); |
|
370 | + unset($headers[$name]); |
|
371 | + } |
|
372 | + } |
|
373 | + |
|
374 | + $this->calculateRequestLength($headers); |
|
375 | + if (isset($headers['content-length']) || isset($headers['transfer-encoding'])) { |
|
376 | + $this->workaroundPhpBug47204($ch, $headers); |
|
377 | + } |
|
378 | + |
|
379 | + // set headers not having special keys |
|
380 | + $headersFmt = array(); |
|
381 | + foreach ($headers as $name => $value) { |
|
382 | + $canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); |
|
383 | + $headersFmt[] = $canonicalName . ': ' . $value; |
|
384 | + } |
|
385 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headersFmt); |
|
386 | + |
|
387 | + return $ch; |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Workaround for PHP bug #47204 that prevents rewinding request body |
|
392 | + * |
|
393 | + * The workaround consists of reading the entire request body into memory |
|
394 | + * and setting it as CURLOPT_POSTFIELDS, so it isn't recommended for large |
|
395 | + * file uploads, use Socket adapter instead. |
|
396 | + * |
|
397 | + * @param resource $ch cURL handle |
|
398 | + * @param array &$headers Request headers |
|
399 | + */ |
|
400 | + protected function workaroundPhpBug47204($ch, &$headers) |
|
401 | + { |
|
402 | + // no redirects, no digest auth -> probably no rewind needed |
|
403 | + if (!$this->request->getConfig('follow_redirects') |
|
404 | + && (!($auth = $this->request->getAuth()) |
|
405 | + || HTTP_Request2::AUTH_DIGEST != $auth['scheme']) |
|
406 | + ) { |
|
407 | + curl_setopt($ch, CURLOPT_READFUNCTION, array($this, 'callbackReadBody')); |
|
408 | + |
|
409 | + } else { |
|
410 | + // rewind may be needed, read the whole body into memory |
|
411 | + if ($this->requestBody instanceof HTTP_Request2_MultipartBody) { |
|
412 | + $this->requestBody = $this->requestBody->__toString(); |
|
413 | + |
|
414 | + } elseif (is_resource($this->requestBody)) { |
|
415 | + $fp = $this->requestBody; |
|
416 | + $this->requestBody = ''; |
|
417 | + while (!feof($fp)) { |
|
418 | + $this->requestBody .= fread($fp, 16384); |
|
419 | + } |
|
420 | + } |
|
421 | + // curl hangs up if content-length is present |
|
422 | + unset($headers['content-length']); |
|
423 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody); |
|
424 | + } |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Callback function called by cURL for reading the request body |
|
429 | + * |
|
430 | + * @param resource $ch cURL handle |
|
431 | + * @param resource $fd file descriptor (not used) |
|
432 | + * @param integer $length maximum length of data to return |
|
433 | + * |
|
434 | + * @return string part of the request body, up to $length bytes |
|
435 | + */ |
|
436 | + protected function callbackReadBody($ch, $fd, $length) |
|
437 | + { |
|
438 | + if (!$this->eventSentHeaders) { |
|
439 | + $this->request->setLastEvent( |
|
440 | + 'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT) |
|
441 | + ); |
|
442 | + $this->eventSentHeaders = true; |
|
443 | + } |
|
444 | + if (in_array($this->request->getMethod(), self::$bodyDisallowed) |
|
445 | + || 0 == $this->contentLength || $this->position >= $this->contentLength |
|
446 | + ) { |
|
447 | + return ''; |
|
448 | + } |
|
449 | + if (is_string($this->requestBody)) { |
|
450 | + $string = substr($this->requestBody, $this->position, $length); |
|
451 | + } elseif (is_resource($this->requestBody)) { |
|
452 | + $string = fread($this->requestBody, $length); |
|
453 | + } else { |
|
454 | + $string = $this->requestBody->read($length); |
|
455 | + } |
|
456 | + $this->request->setLastEvent('sentBodyPart', strlen($string)); |
|
457 | + $this->position += strlen($string); |
|
458 | + return $string; |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Callback function called by cURL for saving the response headers |
|
463 | + * |
|
464 | + * @param resource $ch cURL handle |
|
465 | + * @param string $string response header (with trailing CRLF) |
|
466 | + * |
|
467 | + * @return integer number of bytes saved |
|
468 | + * @see HTTP_Request2_Response::parseHeaderLine() |
|
469 | + */ |
|
470 | + protected function callbackWriteHeader($ch, $string) |
|
471 | + { |
|
472 | + // we may receive a second set of headers if doing e.g. digest auth |
|
473 | + if ($this->eventReceivedHeaders || !$this->eventSentHeaders) { |
|
474 | + // don't bother with 100-Continue responses (bug #15785) |
|
475 | + if (!$this->eventSentHeaders |
|
476 | + || $this->response->getStatus() >= 200 |
|
477 | + ) { |
|
478 | + $this->request->setLastEvent( |
|
479 | + 'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT) |
|
480 | + ); |
|
481 | + } |
|
482 | + $upload = curl_getinfo($ch, CURLINFO_SIZE_UPLOAD); |
|
483 | + // if body wasn't read by a callback, send event with total body size |
|
484 | + if ($upload > $this->position) { |
|
485 | + $this->request->setLastEvent( |
|
486 | + 'sentBodyPart', $upload - $this->position |
|
487 | + ); |
|
488 | + $this->position = $upload; |
|
489 | + } |
|
490 | + if ($upload && (!$this->eventSentHeaders |
|
491 | + || $this->response->getStatus() >= 200) |
|
492 | + ) { |
|
493 | + $this->request->setLastEvent('sentBody', $upload); |
|
494 | + } |
|
495 | + $this->eventSentHeaders = true; |
|
496 | + // we'll need a new response object |
|
497 | + if ($this->eventReceivedHeaders) { |
|
498 | + $this->eventReceivedHeaders = false; |
|
499 | + $this->response = null; |
|
500 | + } |
|
501 | + } |
|
502 | + if (empty($this->response)) { |
|
503 | + $this->response = new HTTP_Request2_Response( |
|
504 | + $string, false, curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) |
|
505 | + ); |
|
506 | + } else { |
|
507 | + $this->response->parseHeaderLine($string); |
|
508 | + if ('' == trim($string)) { |
|
509 | + // don't bother with 100-Continue responses (bug #15785) |
|
510 | + if (200 <= $this->response->getStatus()) { |
|
511 | + $this->request->setLastEvent('receivedHeaders', $this->response); |
|
512 | + } |
|
513 | + |
|
514 | + if ($this->request->getConfig('follow_redirects') && $this->response->isRedirect()) { |
|
515 | + $redirectUrl = new Net_URL2($this->response->getHeader('location')); |
|
516 | + |
|
517 | + // for versions lower than 5.2.10, check the redirection URL protocol |
|
518 | + if (!defined('CURLOPT_REDIR_PROTOCOLS') && $redirectUrl->isAbsolute() |
|
519 | + && !in_array($redirectUrl->getScheme(), array('http', 'https')) |
|
520 | + ) { |
|
521 | + return -1; |
|
522 | + } |
|
523 | + |
|
524 | + if ($jar = $this->request->getCookieJar()) { |
|
525 | + $jar->addCookiesFromResponse($this->response, $this->request->getUrl()); |
|
526 | + if (!$redirectUrl->isAbsolute()) { |
|
527 | + $redirectUrl = $this->request->getUrl()->resolve($redirectUrl); |
|
528 | + } |
|
529 | + if ($cookies = $jar->getMatching($redirectUrl, true)) { |
|
530 | + curl_setopt($ch, CURLOPT_COOKIE, $cookies); |
|
531 | + } |
|
532 | + } |
|
533 | + } |
|
534 | + $this->eventReceivedHeaders = true; |
|
535 | + } |
|
536 | + } |
|
537 | + return strlen($string); |
|
538 | + } |
|
539 | + |
|
540 | + /** |
|
541 | + * Callback function called by cURL for saving the response body |
|
542 | + * |
|
543 | + * @param resource $ch cURL handle (not used) |
|
544 | + * @param string $string part of the response body |
|
545 | + * |
|
546 | + * @return integer number of bytes saved |
|
547 | + * @throws HTTP_Request2_MessageException |
|
548 | + * @see HTTP_Request2_Response::appendBody() |
|
549 | + */ |
|
550 | + protected function callbackWriteBody($ch, $string) |
|
551 | + { |
|
552 | + // cURL calls WRITEFUNCTION callback without calling HEADERFUNCTION if |
|
553 | + // response doesn't start with proper HTTP status line (see bug #15716) |
|
554 | + if (empty($this->response)) { |
|
555 | + throw new HTTP_Request2_MessageException( |
|
556 | + "Malformed response: {$string}", |
|
557 | + HTTP_Request2_Exception::MALFORMED_RESPONSE |
|
558 | + ); |
|
559 | + } |
|
560 | + if ($this->request->getConfig('store_body')) { |
|
561 | + $this->response->appendBody($string); |
|
562 | + } |
|
563 | + $this->request->setLastEvent('receivedBodyPart', $string); |
|
564 | + return strlen($string); |
|
565 | + } |
|
566 | 566 | } |
567 | 567 | ?> |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | protected static function wrapCurlError($ch) |
140 | 140 | { |
141 | 141 | $nativeCode = curl_errno($ch); |
142 | - $message = 'Curl error: ' . curl_error($ch); |
|
142 | + $message = 'Curl error: '.curl_error($ch); |
|
143 | 143 | if (!isset(self::$errorMap[$nativeCode])) { |
144 | 144 | return new HTTP_Request2_Exception($message, 0, $nativeCode); |
145 | 145 | } else { |
@@ -300,11 +300,11 @@ discard block |
||
300 | 300 | 'Proxy port not provided', HTTP_Request2_Exception::MISSING_VALUE |
301 | 301 | ); |
302 | 302 | } |
303 | - curl_setopt($ch, CURLOPT_PROXY, $host . ':' . $port); |
|
303 | + curl_setopt($ch, CURLOPT_PROXY, $host.':'.$port); |
|
304 | 304 | if ($user = $this->request->getConfig('proxy_user')) { |
305 | 305 | curl_setopt( |
306 | 306 | $ch, CURLOPT_PROXYUSERPWD, |
307 | - $user . ':' . $this->request->getConfig('proxy_password') |
|
307 | + $user.':'.$this->request->getConfig('proxy_password') |
|
308 | 308 | ); |
309 | 309 | switch ($this->request->getConfig('proxy_auth_scheme')) { |
310 | 310 | case HTTP_Request2::AUTH_BASIC: |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | |
333 | 333 | // set authentication data |
334 | 334 | if ($auth = $this->request->getAuth()) { |
335 | - curl_setopt($ch, CURLOPT_USERPWD, $auth['user'] . ':' . $auth['password']); |
|
335 | + curl_setopt($ch, CURLOPT_USERPWD, $auth['user'].':'.$auth['password']); |
|
336 | 336 | switch ($auth['scheme']) { |
337 | 337 | case HTTP_Request2::AUTH_BASIC: |
338 | 338 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | // set SSL options |
346 | 346 | foreach ($this->request->getConfig() as $name => $value) { |
347 | 347 | if ('ssl_verify_host' == $name && null !== $value) { |
348 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $value? 2: 0); |
|
348 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $value ? 2 : 0); |
|
349 | 349 | } elseif (isset(self::$sslContextMap[$name]) && null !== $value) { |
350 | 350 | curl_setopt($ch, self::$sslContextMap[$name], $value); |
351 | 351 | } |
@@ -360,7 +360,7 @@ discard block |
||
360 | 360 | if (($jar = $this->request->getCookieJar()) |
361 | 361 | && ($cookies = $jar->getMatching($this->request->getUrl(), true)) |
362 | 362 | ) { |
363 | - $headers['cookie'] = (empty($headers['cookie'])? '': $headers['cookie'] . '; ') . $cookies; |
|
363 | + $headers['cookie'] = (empty($headers['cookie']) ? '' : $headers['cookie'].'; ').$cookies; |
|
364 | 364 | } |
365 | 365 | |
366 | 366 | // set headers having special cURL keys |
@@ -380,7 +380,7 @@ discard block |
||
380 | 380 | $headersFmt = array(); |
381 | 381 | foreach ($headers as $name => $value) { |
382 | 382 | $canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); |
383 | - $headersFmt[] = $canonicalName . ': ' . $value; |
|
383 | + $headersFmt[] = $canonicalName.': '.$value; |
|
384 | 384 | } |
385 | 385 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headersFmt); |
386 | 386 |