XOOPS  2.6.0
HttpRequest.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
12 namespace Xoops\Core;
13 
39 {
43  protected $params;
44 
45 
53  protected $detectors = array(
54  'get' => array('env' => 'REQUEST_METHOD', 'value' => 'GET'),
55  'post' => array('env' => 'REQUEST_METHOD', 'value' => 'POST'),
56  'put' => array('env' => 'REQUEST_METHOD', 'value' => 'PUT'),
57  'delete' => array('env' => 'REQUEST_METHOD', 'value' => 'DELETE'),
58  'head' => array('env' => 'REQUEST_METHOD', 'value' => 'HEAD'),
59  'options' => array('env' => 'REQUEST_METHOD', 'value' => 'OPTIONS'),
60  'safemethod'=> array('env' => 'REQUEST_METHOD', 'options' => array('GET', 'HEAD')),
61  'ssl' => array('env' => 'HTTPS', 'value' => 1),
62  'ajax' => array('env' => 'HTTP_X_REQUESTED_WITH', 'value' => 'XMLHttpRequest'),
63  'flash' => array('env' => 'HTTP_USER_AGENT', 'pattern' => '/^(Shockwave|Adobe) Flash/'),
64  'mobile' => array(
65  'env' => 'HTTP_USER_AGENT',
66  'options' => array(
67  'Android',
68  'AvantGo',
69  'BlackBerry',
70  'DoCoMo',
71  'Fennec',
72  'iPod',
73  'iPhone',
74  'iPad',
75  'J2ME',
76  'MIDP',
77  'NetFront',
78  'Nokia',
79  'Opera Mini',
80  'Opera Mobi',
81  'PalmOS',
82  'PalmSource',
83  'portalmmm',
84  'Plucker',
85  'ReqwirelessWeb',
86  'SonyEricsson',
87  'Symbian',
88  'UP\\.Browser',
89  'webOS',
90  'Windows CE',
91  'Windows Phone OS',
92  'Xiino'
93  )
94  ),
95  'robot' => array(
96  'env' => 'HTTP_USER_AGENT',
97  'options' => array(
98  /* The most common ones. */
99  'Googlebot',
100  'msnbot',
101  'Slurp',
102  'Yahoo',
103  /* The rest alphabetically. */
104  'Arachnoidea',
105  'ArchitextSpider',
106  'Ask Jeeves',
107  'B-l-i-t-z-Bot',
108  'Baiduspider',
109  'BecomeBot',
110  'cfetch',
111  'ConveraCrawler',
112  'ExtractorPro',
113  'FAST-WebCrawler',
114  'FDSE robot',
115  'fido',
116  'geckobot',
117  'Gigabot',
118  'Girafabot',
119  'grub-client',
120  'Gulliver',
121  'HTTrack',
122  'ia_archiver',
123  'InfoSeek',
124  'kinjabot',
125  'KIT-Fireball',
126  'larbin',
127  'LEIA',
128  'lmspider',
129  'Lycos_Spider',
130  'Mediapartners-Google',
131  'MuscatFerret',
132  'NaverBot',
133  'OmniExplorer_Bot',
134  'polybot',
135  'Pompos',
136  'Scooter',
137  'Teoma',
138  'TheSuBot',
139  'TurnitinBot',
140  'Ultraseek',
141  'ViolaBot',
142  'webbandit',
143  'www\\.almaden\\.ibm\\.com\\/cs\\/crawler',
144  'ZyBorg',
145  )
146  ),
147  );
148 
152  private function __construct()
153  {
154  switch (strtolower($this->getEnv('REQUEST_METHOD'))) {
155  case 'get':
156  $params = $_GET;
157  break;
158  case 'put':
159  parse_str(file_get_contents('php://input'), $put);
160  $params = array_merge($_GET, $put);
161  break;
162  default:
163  $params = array_merge($_GET, $_POST);
164  }
165  $this->params = $params;
166  }
167 
171  public static function getInstance()
172  {
173  static $instance;
174  if (!isset($instance)) {
175  $thisClass = get_called_class();
176  $instance = new $thisClass();
177  }
178  return $instance;
179  }
180 
186  public function getHeader($name = null)
187  {
188  if ($name === null) {
189  return $name;
190  }
191 
192  // Try to get it from the $_SERVER array first
193  if ($res = $this->getEnv('HTTP_' . strtoupper(str_replace('-', '_', $name)))) {
194  return $res;
195  }
196 
197  // This seems to be the only way to get the Authorization header on
198  // Apache
199  if (function_exists('apache_request_headers')) {
200  $headers = apache_request_headers();
201  if (!empty($headers[$name])) {
202  return $headers[$name];
203  }
204  }
205  return null;
206  }
207 
211  public function getScheme()
212  {
213  return $this->getEnv('HTTPS') ? 'https' : 'http';
214  }
215 
219  public function getHost()
220  {
221  return $this->getEnv('HTTP_HOST') ? (string) $this->getEnv('HTTP_HOST') : 'localhost';
222  }
223 
227  public static function getUri()
228  {
229  if (empty($_SERVER['PHP_SELF']) || empty($_SERVER['REQUEST_URI'])) {
230  // IIS
231  $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
232  if (!empty($_SERVER['QUERY_STRING'])) {
233  $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
234  }
235  return $_SERVER['REQUEST_URI'];
236  }
237  return isset($_SERVER['ORIG_REQUEST_URI']) ? $_SERVER['ORIG_REQUEST_URI'] : $_SERVER['REQUEST_URI'];
238  }
239 
243  public function getReferer()
244  {
245  return $this->getEnv('HTTP_REFERER') ? $this->getEnv('HTTP_REFERER') : '';
246  }
247 
251  public function getScriptName()
252  {
253  return $this->getEnv('SCRIPT_NAME')
254  ? $this->getEnv('SCRIPT_NAME')
255  : ($this->getEnv('ORIG_SCRIPT_NAME') ? $this->getEnv('ORIG_SCRIPT_NAME') : '');
256  }
257 
263  public function getDomain()
264  {
265  $host = $this->getHost();
266  $domain = \Xoops::getInstance()->getBaseDomain($host, false);
267  return is_null($domain) ? $host : $domain;
268  }
269 
275  public function getSubdomains()
276  {
277  $host = $this->getHost();
278  $pdp = \Xoops::getInstance()->getBaseDomain($host, true, true);
279  $subdomain = $pdp->subdomain;
280  return is_null($subdomain) ? '' : $subdomain;
281  }
282 
290  public function getClientIp($considerProxy = false)
291  {
292  $default = (array_key_exists('REMOTE_ADDR', $_SERVER)) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
293 
294  if (!$considerProxy) {
295  return $default;
296  }
297 
298  $keys = array(
299  'HTTP_CLIENT_IP',
300  'HTTP_X_FORWARDED_FOR',
301  'HTTP_X_FORWARDED',
302  'HTTP_X_CLUSTER_CLIENT_IP',
303  'HTTP_FORWARDED_FOR',
304  'HTTP_FORWARDED',
305  );
306  foreach ($keys as $key) {
307  if (array_key_exists($key, $_SERVER) === true) {
308  foreach (explode(',', $_SERVER[$key]) as $ip) {
309  $ip = trim($ip);
310  if (false !== filter_var(
311  $ip,
312  FILTER_VALIDATE_IP,
313  FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
314  )) {
315  return $ip;
316  }
317  }
318  }
319  }
320 
321  return $default;
322  }
323 
329  public function getUrl()
330  {
331  $url = $this->getScheme() . "://" . $this->getHost();
332  $port = $this->getEnv('SERVER_PORT');
333  if (80 != $port) {
334  $url .= ":{$port}";
335  }
336  return $url . $this->getUri();
337  }
338 
354  public function getEnv($name, $default = null)
355  {
356  if ($name === 'HTTPS') {
357  if (isset($_SERVER['HTTPS'])) {
358  return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
359  }
360  return (strpos($this->getEnv('SCRIPT_URI'), 'https://') === 0);
361  }
362 
363  if ($name === 'SCRIPT_NAME' && !isset($_SERVER[$name])) {
364  if ($this->getEnv('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
365  return $_ENV['SCRIPT_URL'];
366  }
367  }
368 
369  if ($name === 'REMOTE_ADDR' && !isset($_SERVER[$name])) {
370  $addr = $this->getEnv('HTTP_PC_REMOTE_ADDR');
371  if ($addr !== null) {
372  return $addr;
373  }
374  }
375 
376  $val = null;
377  if (isset($_SERVER[$name])) {
378  $val = $_SERVER[$name];
379  } elseif (isset($_ENV[$name])) {
380  $val = $_ENV[$name];
381  }
382 
383  if ($val !== null) {
384  return $val;
385  }
386 
387  switch ($name) {
388  case 'SCRIPT_FILENAME':
389  $val = preg_replace('#//+#', '/', $this->getEnv('PATH_TRANSLATED'));
390  return preg_replace('#\\\\+#', '\\', $val);
391  break;
392  case 'DOCUMENT_ROOT':
393  $name = $this->getEnv('SCRIPT_NAME');
394  $filename = $this->getEnv('SCRIPT_FILENAME');
395  $offset = 0;
396  if (!strpos($name, '.php')) {
397  $offset = 4;
398  }
399  return substr($filename, 0, -(strlen($name) + $offset));
400  break;
401  case 'PHP_SELF':
402  return str_replace($this->getEnv('DOCUMENT_ROOT'), '', $this->getEnv('SCRIPT_FILENAME'));
403  break;
404  case 'CGI_MODE':
405  return (PHP_SAPI === 'cgi');
406  break;
407  case 'HTTP_BASE':
408  $host = $this->getEnv('HTTP_HOST');
409  $val = \Xoops::getInstance()->getBaseDomain($host);
410  if (is_null($val)) {
411  return $default;
412  } else {
413  return '.' . $val;
414  }
415  break;
416  }
417  return $default;
418  }
419 
425  public static function getFiles($name)
426  {
427  if (empty($_FILES)) {
428  return array();
429  }
430 
431  if (isset($_FILES[$name])) {
432  return $_FILES[$name];
433  }
434 
435  if (false === $pos = strpos($name, '[')) {
436  return array();
437  }
438 
439  $base = substr($name, 0, $pos);
440  $key = str_replace(array(']', '['), array('', '"]["'), substr($name, $pos + 1, -1));
441  $code = array(sprintf('if (!isset($_FILES["%s"]["name"]["%s"])) return array();', $base, $key));
442  $code[] = '$file = array();';
443  foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $property) {
444  $code[] = sprintf('$file["%1$s"] = $_FILES["%2$s"]["%1$s"]["%3$s"];', $property, $base, $key);
445  }
446  $code[] = 'return $file;';
447 
448  return eval(implode(PHP_EOL, $code));
449  }
450 
460  public function is($type)
461  {
462  $type = strtolower($type);
463  if (!isset($this->detectors[$type])) {
464  return false;
465  }
466  $detect = $this->detectors[$type];
467  if (isset($detect['env'])) {
468  return $this->detectByEnv($detect);
469  } elseif (isset($detect['param'])) {
470  return $this->detectByParam($detect);
471  } elseif (isset($detect['callback']) && is_callable($detect['callback'])) {
472  return call_user_func($detect['callback'], $this);
473  }
474  return false;
475  }
476 
484  protected function detectByEnv($detect)
485  {
486  if (isset($detect['value'])) {
487  return (bool) $this->getEnv($detect['env']) == $detect['value'];
488  } elseif (isset($detect['pattern'])) {
489  return (bool) preg_match($detect['pattern'], $this->getEnv($detect['env']));
490  } elseif (isset($detect['options'])) {
491  $pattern = '/' . implode('|', $detect['options']) . '/i';
492  return (bool) preg_match($pattern, $this->getEnv($detect['env']));
493  }
494  return false; // can't match a broken rule
495  }
496 
508  protected function detectByParam($detect)
509  {
510  $name = $detect['param'];
511  $value = $detect['value'];
512  return isset($this->params[$name]) ? $this->params[$name] == $value : false;
513  }
514 
542  public function addDetector($name, $options)
543  {
544  $name = strtolower($name);
545  if (isset($this->detectors[$name]) && isset($options['options'])) {
546  $options = Xoops_Utils::arrayRecursiveMerge($this->detectors[$name], $options);
547  }
548  $this->detectors[$name] = $options;
549  }
550 
558  public function clientAcceptsType($mediaType)
559  {
560  $accepts = $this->getAcceptMediaTypes();
561 
562  $mediaType = trim($mediaType);
563  if (isset($accepts[$mediaType])) {
564  return true;
565  }
566  list($type, $subtype) = explode('/', $mediaType);
567  if (isset($accepts[$type.'/*'])) {
568  return true;
569  }
570 
571  return isset($accepts['*/*']);
572  }
573 
581  public function getAcceptMediaTypes()
582  {
583  $types = array();
584  $accept = $this->getHeader('ACCEPT');
585 
586  if (!empty($accept)) {
587  $entries = explode(',', $accept);
588  foreach ($entries as $e) {
589  $mt = explode(';q=', $e);
590  if (!isset($mt[1])) {
591  $mt[1] = 1.0;
592  }
593  $types[trim($mt[0])] = (float) $mt[1];
594  }
595 
596  // sort list based on value
597  arsort($types, SORT_NUMERIC);
598  }
599 
600  return($types);
601  }
602 
610  public function getAcceptedLanguages()
611  {
612  $langs = array();
613  $accept = $this->getHeader('ACCEPT_LANGUAGE');
614 
615  if (!empty($accept)) {
616  $entries = explode(',', $accept);
617  foreach ($entries as $e) {
618  $l = explode(';q=', $e);
619  if (!isset($l[1])) {
620  $l[1] = 1.0;
621  }
622  $langs[trim($l[0])] = (float) $l[1];
623  }
624 
625  // sort list based on value
626  arsort($langs, SORT_NUMERIC);
627  }
628 
629  return($langs);
630  }
631 }
$l
Definition: main.php:88
if(!$dbm->isConnectable()) $res
static getInstance()
Definition: Xoops.php:160
$options['editor']
$_SERVER['REQUEST_URI']
$base
Definition: execute.php:30
clientAcceptsType($mediaType)
$type
Definition: misc.php:33
$keys
Definition: install_tpl.php:37
$url
Definition: register.php:72
static arrayRecursiveMerge(array $data, $merge)
Definition: Utils.php:113
getClientIp($considerProxy=false)
addDetector($name, $options)
static getFiles($name)
getEnv($name, $default=null)
$code
Definition: lostpass.php:48