Request.php 6.8 KB

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