PageRenderTime 54ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/request.php

https://github.com/MHordecki/milionkostek
PHP | 217 lines | 100 code | 35 blank | 82 comment | 13 complexity | fab787613da48d3f27e81dc74f75b29f MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Request helper class.
  4. *
  5. * $Id: request.php 2740 2008-06-02 16:20:31Z Shadowhand $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class request_Core {
  13. // Possible HTTP methods
  14. protected static $http_methods = array('get', 'head', 'options', 'post', 'put', 'delete');
  15. // Content types from client's HTTP Accept request header (array)
  16. protected static $accept_types;
  17. /**
  18. * Returns the HTTP referrer, or the default if the referrer is not set.
  19. *
  20. * @param mixed default to return
  21. * @return string
  22. */
  23. public static function referrer($default = FALSE)
  24. {
  25. if ( ! empty($_SERVER['HTTP_REFERER']))
  26. {
  27. // Set referrer
  28. $ref = $_SERVER['HTTP_REFERER'];
  29. if (strpos($ref, url::base(FALSE)) === 0)
  30. {
  31. // Remove the base URL from the referrer
  32. $ref = substr($ref, strlen(url::base(TRUE)));
  33. }
  34. }
  35. return isset($ref) ? $ref : $default;
  36. }
  37. /**
  38. * Tests if the current request is an AJAX request by checking the X-Requested-With HTTP
  39. * request header that most popular JS frameworks now set for AJAX calls.
  40. *
  41. * @return boolean
  42. */
  43. public static function is_ajax()
  44. {
  45. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
  46. }
  47. /**
  48. * Returns current request method.
  49. *
  50. * @throws Kohana_Exception in case of an unknown request method
  51. * @return string
  52. */
  53. public static function method()
  54. {
  55. $method = strtolower($_SERVER['REQUEST_METHOD']);
  56. if ( ! in_array($method, self::$http_methods))
  57. throw new Kohana_Exception('request.unknown_method', $method);
  58. return $method;
  59. }
  60. /**
  61. * Returns boolean of whether client accepts content type.
  62. *
  63. * @param string content type
  64. * @param boolean set to TRUE to disable wildcard checking
  65. * @return boolean
  66. */
  67. public static function accepts($type = NULL, $explicit_check = FALSE)
  68. {
  69. request::parse_accept_header();
  70. if ($type === NULL)
  71. return self::$accept_types;
  72. return (request::accepts_at_quality($type, $explicit_check) > 0);
  73. }
  74. /**
  75. * Compare the q values for given array of content types and return the one with the highest value.
  76. * If items are found to have the same q value, the first one encountered in the given array wins.
  77. * If all items in the given array have a q value of 0, FALSE is returned.
  78. *
  79. * @param array content types
  80. * @param boolean set to TRUE to disable wildcard checking
  81. * @return mixed string mime type with highest q value, FALSE if none of the given types are accepted
  82. */
  83. public static function preferred_accept($types, $explicit_check = FALSE)
  84. {
  85. // Initialize
  86. $mime_types = array();
  87. $max_q = 0;
  88. $preferred = FALSE;
  89. // Load q values for all given content types
  90. foreach (array_unique($types) as $type)
  91. {
  92. $mime_types[$type] = request::accepts_at_quality($type, $explicit_check);
  93. }
  94. // Look for the highest q value
  95. foreach ($mime_types as $type => $q)
  96. {
  97. if ($q > $max_q)
  98. {
  99. $max_q = $q;
  100. $preferred = $type;
  101. }
  102. }
  103. return $preferred;
  104. }
  105. /**
  106. * Returns quality factor at which the client accepts content type.
  107. *
  108. * @param string content type (e.g. "image/jpg", "jpg")
  109. * @param boolean set to TRUE to disable wildcard checking
  110. * @return integer|float
  111. */
  112. public static function accepts_at_quality($type = NULL, $explicit_check = FALSE)
  113. {
  114. request::parse_accept_header();
  115. // Normalize type
  116. $type = strtolower((string) $type);
  117. // General content type (e.g. "jpg")
  118. if (strpos($type, '/') === FALSE)
  119. {
  120. // Don't accept anything by default
  121. $q = 0;
  122. // Look up relevant mime types
  123. foreach ((array) Config::item('mimes.'.$type) as $type)
  124. {
  125. $q2 = request::accepts_at_quality($type, $explicit_check);
  126. $q = ($q2 > $q) ? $q2 : $q;
  127. }
  128. return $q;
  129. }
  130. // Content type with subtype given (e.g. "image/jpg")
  131. $type = explode('/', $type, 2);
  132. // Exact match
  133. if (isset(self::$accept_types[$type[0]][$type[1]]))
  134. return self::$accept_types[$type[0]][$type[1]];
  135. // Wildcard match (if not checking explicitly)
  136. if ($explicit_check === FALSE AND isset(self::$accept_types[$type[0]]['*']))
  137. return self::$accept_types[$type[0]]['*'];
  138. // Catch-all wildcard match (if not checking explicitly)
  139. if ($explicit_check === FALSE AND isset(self::$accept_types['*']['*']))
  140. return self::$accept_types['*']['*'];
  141. // Content type not accepted
  142. return 0;
  143. }
  144. /**
  145. * Parses client's HTTP Accept request header, and builds array structure representing it.
  146. *
  147. * @return void
  148. */
  149. protected static function parse_accept_header()
  150. {
  151. // Run this function just once
  152. if (self::$accept_types !== NULL)
  153. return;
  154. // Initialize accept_types array
  155. self::$accept_types = array();
  156. // No HTTP Accept header found
  157. if (empty($_SERVER['HTTP_ACCEPT']))
  158. {
  159. // Accept everything
  160. self::$accept_types['*']['*'] = 1;
  161. return;
  162. }
  163. // Remove linebreaks and parse the HTTP Accept header
  164. foreach (explode(',', str_replace(array("\r", "\n"), '', $_SERVER['HTTP_ACCEPT'])) as $accept_entry)
  165. {
  166. // Explode each entry in content type and possible quality factor
  167. $accept_entry = explode(';', trim($accept_entry), 2);
  168. // Explode each content type (e.g. "text/html")
  169. $type = explode('/', $accept_entry[0], 2);
  170. // Skip invalid content types
  171. if ( ! isset($type[1]))
  172. continue;
  173. // Assume a default quality factor of 1 if no custom q value found
  174. $q = (isset($accept_entry[1]) AND preg_match('~\bq\s*+=\s*+([.0-9]+)~', $accept_entry[1], $match)) ? (float) $match[1] : 1;
  175. // Populate accept_types array
  176. if ( ! isset(self::$accept_types[$type[0]][$type[1]]) OR $q > self::$accept_types[$type[0]][$type[1]])
  177. {
  178. self::$accept_types[$type[0]][$type[1]] = $q;
  179. }
  180. }
  181. }
  182. } // End request