1
0

Request.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.1
  11. */
  12. namespace PrivateBin;
  13. /**
  14. * Request
  15. *
  16. * parses request parameters and provides helper functions for routing
  17. */
  18. class Request
  19. {
  20. /**
  21. * MIME type for JSON
  22. *
  23. * @const string
  24. */
  25. const MIME_JSON = 'application/json';
  26. /**
  27. * MIME type for HTML
  28. *
  29. * @const string
  30. */
  31. const MIME_HTML = 'text/html';
  32. /**
  33. * MIME type for XHTML
  34. *
  35. * @const string
  36. */
  37. const MIME_XHTML = 'application/xhtml+xml';
  38. /**
  39. * Input stream to use for PUT parameter parsing.
  40. *
  41. * @access private
  42. * @var string
  43. */
  44. private static $_inputStream = 'php://input';
  45. /**
  46. * Operation to perform.
  47. *
  48. * @access private
  49. * @var string
  50. */
  51. private $_operation = 'view';
  52. /**
  53. * Request parameters.
  54. *
  55. * @access private
  56. * @var array
  57. */
  58. private $_params = array();
  59. /**
  60. * If we are in a JSON API context.
  61. *
  62. * @access private
  63. * @var bool
  64. */
  65. private $_isJsonApi = false;
  66. /**
  67. * Constructor.
  68. *
  69. * @access public
  70. * @return void
  71. */
  72. public function __construct()
  73. {
  74. // in case stupid admin has left magic_quotes enabled in php.ini (for PHP < 5.4)
  75. if (version_compare(PHP_VERSION, '5.4.0') < 0 && get_magic_quotes_gpc()) {
  76. $_POST = array_map('PrivateBin\\Filter::stripslashesDeep', $_POST);
  77. $_GET = array_map('PrivateBin\\Filter::stripslashesDeep', $_GET);
  78. $_COOKIE = array_map('PrivateBin\\Filter::stripslashesDeep', $_COOKIE);
  79. }
  80. // decide if we are in JSON API or HTML context
  81. $this->_isJsonApi = $this->_detectJsonRequest();
  82. // parse parameters, depending on request type
  83. switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET') {
  84. case 'DELETE':
  85. case 'PUT':
  86. parse_str(file_get_contents(self::$_inputStream), $this->_params);
  87. break;
  88. case 'POST':
  89. $this->_params = $_POST;
  90. break;
  91. default:
  92. $this->_params = $_GET;
  93. }
  94. if (
  95. !array_key_exists('pasteid', $this->_params) &&
  96. !array_key_exists('jsonld', $this->_params) &&
  97. array_key_exists('QUERY_STRING', $_SERVER) &&
  98. !empty($_SERVER['QUERY_STRING'])
  99. ) {
  100. $this->_params['pasteid'] = $_SERVER['QUERY_STRING'];
  101. }
  102. // prepare operation, depending on current parameters
  103. if (
  104. (array_key_exists('data', $this->_params) && !empty($this->_params['data'])) ||
  105. (array_key_exists('attachment', $this->_params) && !empty($this->_params['attachment']))
  106. ) {
  107. $this->_operation = 'create';
  108. } elseif (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid'])) {
  109. if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken'])) {
  110. $this->_operation = 'delete';
  111. } else {
  112. $this->_operation = 'read';
  113. }
  114. } elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) {
  115. $this->_operation = 'jsonld';
  116. }
  117. }
  118. /**
  119. * Get current operation.
  120. *
  121. * @access public
  122. * @return string
  123. */
  124. public function getOperation()
  125. {
  126. return $this->_operation;
  127. }
  128. /**
  129. * Get a request parameter.
  130. *
  131. * @access public
  132. * @param string $param
  133. * @param string $default
  134. * @return string
  135. */
  136. public function getParam($param, $default = '')
  137. {
  138. return array_key_exists($param, $this->_params) ? $this->_params[$param] : $default;
  139. }
  140. /**
  141. * If we are in a JSON API context.
  142. *
  143. * @access public
  144. * @return bool
  145. */
  146. public function isJsonApiCall()
  147. {
  148. return $this->_isJsonApi;
  149. }
  150. /**
  151. * Override the default input stream source, used for unit testing.
  152. *
  153. * @param string $input
  154. */
  155. public static function setInputStream($input)
  156. {
  157. self::$_inputStream = $input;
  158. }
  159. /**
  160. * detect the clients supported media type and decide if its a JSON API call or not
  161. *
  162. * Adapted from: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
  163. *
  164. * @access private
  165. * @return bool
  166. */
  167. private function _detectJsonRequest()
  168. {
  169. $hasAcceptHeader = array_key_exists('HTTP_ACCEPT', $_SERVER);
  170. $acceptHeader = $hasAcceptHeader ? $_SERVER['HTTP_ACCEPT'] : '';
  171. // simple cases
  172. if (
  173. (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
  174. $_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
  175. ($hasAcceptHeader &&
  176. strpos($acceptHeader, self::MIME_JSON) !== false &&
  177. strpos($acceptHeader, self::MIME_HTML) === false &&
  178. strpos($acceptHeader, self::MIME_XHTML) === false)
  179. ) {
  180. return true;
  181. }
  182. // advanced case: media type negotiation
  183. $mediaTypes = array();
  184. if ($hasAcceptHeader) {
  185. $mediaTypeRanges = explode(',', trim($acceptHeader));
  186. foreach ($mediaTypeRanges as $mediaTypeRange) {
  187. if (preg_match(
  188. '#(\*/\*|[a-z\-]+/[a-z\-+*]+(?:\s*;\s*[^q]\S*)*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?#',
  189. trim($mediaTypeRange), $match
  190. )) {
  191. if (!isset($match[2])) {
  192. $match[2] = '1.0';
  193. } else {
  194. $match[2] = (string) floatval($match[2]);
  195. }
  196. if (!isset($mediaTypes[$match[2]])) {
  197. $mediaTypes[$match[2]] = array();
  198. }
  199. $mediaTypes[$match[2]][] = strtolower($match[1]);
  200. }
  201. }
  202. krsort($mediaTypes);
  203. foreach ($mediaTypes as $acceptedQuality => $acceptedValues) {
  204. if ($acceptedQuality === 0.0) {
  205. continue;
  206. }
  207. foreach ($acceptedValues as $acceptedValue) {
  208. if (
  209. strpos($acceptedValue, self::MIME_HTML) === 0 ||
  210. strpos($acceptedValue, self::MIME_XHTML) === 0
  211. ) {
  212. return false;
  213. } elseif (strpos($acceptedValue, self::MIME_JSON) === 0) {
  214. return true;
  215. }
  216. }
  217. }
  218. }
  219. return false;
  220. }
  221. }