request.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. 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 (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
  76. {
  77. $_POST = array_map('filter::stripslashes_deep', $_POST);
  78. $_GET = array_map('filter::stripslashes_deep', $_GET);
  79. $_COOKIE = array_map('filter::stripslashes_deep', $_COOKIE);
  80. }
  81. // decide if we are in JSON API or HTML context
  82. $this->_isJsonApi = $this->_detectJsonRequest();
  83. // parse parameters, depending on request type
  84. switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET')
  85. {
  86. case 'DELETE':
  87. case 'PUT':
  88. parse_str(file_get_contents(self::$_inputStream), $this->_params);
  89. break;
  90. case 'POST':
  91. $this->_params = $_POST;
  92. break;
  93. default:
  94. $this->_params = $_GET;
  95. }
  96. if (
  97. !array_key_exists('pasteid', $this->_params) &&
  98. !array_key_exists('jsonld', $this->_params) &&
  99. array_key_exists('QUERY_STRING', $_SERVER) &&
  100. !empty($_SERVER['QUERY_STRING'])
  101. )
  102. {
  103. $this->_params['pasteid'] = $_SERVER['QUERY_STRING'];
  104. }
  105. // prepare operation, depending on current parameters
  106. if (
  107. (array_key_exists('data', $this->_params) && !empty($this->_params['data'])) ||
  108. (array_key_exists('attachment', $this->_params) && !empty($this->_params['attachment']))
  109. )
  110. {
  111. $this->_operation = 'create';
  112. }
  113. elseif (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid']))
  114. {
  115. if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken']))
  116. {
  117. $this->_operation = 'delete';
  118. }
  119. else
  120. {
  121. $this->_operation = 'read';
  122. }
  123. }
  124. elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld']))
  125. {
  126. $this->_operation = 'jsonld';
  127. }
  128. }
  129. /**
  130. * Get current operation.
  131. *
  132. * @access public
  133. * @return string
  134. */
  135. public function getOperation()
  136. {
  137. return $this->_operation;
  138. }
  139. /**
  140. * Get a request parameter.
  141. *
  142. * @access public
  143. * @param string $param
  144. * @param string $default
  145. * @return string
  146. */
  147. public function getParam($param, $default = '')
  148. {
  149. return array_key_exists($param, $this->_params) ? $this->_params[$param] : $default;
  150. }
  151. /**
  152. * If we are in a JSON API context.
  153. *
  154. * @access public
  155. * @return bool
  156. */
  157. public function isJsonApiCall()
  158. {
  159. return $this->_isJsonApi;
  160. }
  161. /**
  162. * Override the default input stream source, used for unit testing.
  163. *
  164. * @param string $input
  165. */
  166. public static function setInputStream($input)
  167. {
  168. self::$_inputStream = $input;
  169. }
  170. /**
  171. * detect the clients supported media type and decide if its a JSON API call or not
  172. *
  173. * Adapted from: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
  174. *
  175. * @access private
  176. * @return bool
  177. */
  178. private function _detectJsonRequest()
  179. {
  180. $hasAcceptHeader = array_key_exists('HTTP_ACCEPT', $_SERVER);
  181. $acceptHeader = $hasAcceptHeader ? $_SERVER['HTTP_ACCEPT'] : '';
  182. // simple cases
  183. if (
  184. (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
  185. $_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
  186. ($hasAcceptHeader &&
  187. strpos($acceptHeader, self::MIME_JSON) !== false &&
  188. strpos($acceptHeader, self::MIME_HTML) === false &&
  189. strpos($acceptHeader, self::MIME_XHTML) === false)
  190. )
  191. {
  192. return true;
  193. }
  194. // advanced case: media type negotiation
  195. $mediaTypes = array();
  196. if ($hasAcceptHeader)
  197. {
  198. $mediaTypeRanges = explode(',', trim($acceptHeader));
  199. foreach ($mediaTypeRanges as $mediaTypeRange)
  200. {
  201. if (preg_match(
  202. '#(\*/\*|[a-z\-]+/[a-z\-+*]+(?:\s*;\s*[^q]\S*)*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?#',
  203. trim($mediaTypeRange), $match
  204. ))
  205. {
  206. if (!isset($match[2]))
  207. {
  208. $match[2] = '1.0';
  209. }
  210. else
  211. {
  212. $match[2] = (string) floatval($match[2]);
  213. }
  214. if (!isset($mediaTypes[$match[2]]))
  215. {
  216. $mediaTypes[$match[2]] = array();
  217. }
  218. $mediaTypes[$match[2]][] = strtolower($match[1]);
  219. }
  220. }
  221. krsort($mediaTypes);
  222. foreach ($mediaTypes as $acceptedQuality => $acceptedValues)
  223. {
  224. if ($acceptedQuality === 0.0) continue;
  225. foreach ($acceptedValues as $acceptedValue)
  226. {
  227. if (
  228. strpos($acceptedValue, self::MIME_HTML) === 0 ||
  229. strpos($acceptedValue, self::MIME_XHTML) === 0
  230. )
  231. {
  232. return false;
  233. }
  234. elseif (strpos($acceptedValue, self::MIME_JSON) === 0)
  235. {
  236. return true;
  237. }
  238. }
  239. }
  240. }
  241. return false;
  242. }
  243. }