request.php 7.1 KB

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