Request.php 6.5 KB

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