Request.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.3.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. * Return the paste ID of the current paste.
  68. *
  69. * @access private
  70. * @return string
  71. */
  72. private function getPasteId()
  73. {
  74. // RegEx to check for valid paste ID (16 base64 chars)
  75. $pasteIdRegEx = '/^[a-f0-9]{16}$/';
  76. foreach ($_GET as $key => $value) {
  77. // only return if value is empty and key matches RegEx
  78. if (($value === '') and preg_match($pasteIdRegEx, $key, $match)) {
  79. return $match[0];
  80. }
  81. }
  82. return 'invalid id';
  83. }
  84. /**
  85. * Constructor
  86. *
  87. * @access public
  88. */
  89. public function __construct()
  90. {
  91. // decide if we are in JSON API or HTML context
  92. $this->_isJsonApi = $this->_detectJsonRequest();
  93. // parse parameters, depending on request type
  94. switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET') {
  95. case 'DELETE':
  96. case 'PUT':
  97. case 'POST':
  98. $this->_params = Json::decode(
  99. file_get_contents(self::$_inputStream)
  100. );
  101. break;
  102. default:
  103. $this->_params = $_GET;
  104. }
  105. if (
  106. !array_key_exists('pasteid', $this->_params) &&
  107. !array_key_exists('jsonld', $this->_params) &&
  108. array_key_exists('QUERY_STRING', $_SERVER) &&
  109. !empty($_SERVER['QUERY_STRING'])
  110. ) {
  111. $this->_params['pasteid'] = $this->getPasteId();
  112. }
  113. // prepare operation, depending on current parameters
  114. if (
  115. array_key_exists('ct', $this->_params) &&
  116. !empty($this->_params['ct'])
  117. ) {
  118. $this->_operation = 'create';
  119. } elseif (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid'])) {
  120. if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken'])) {
  121. $this->_operation = 'delete';
  122. } else {
  123. $this->_operation = 'read';
  124. }
  125. } elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) {
  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 data of paste or comment
  141. *
  142. * @access public
  143. * @return array
  144. */
  145. public function getData()
  146. {
  147. $data = array(
  148. 'adata' => $this->getParam('adata'),
  149. );
  150. $required_keys = array('v', 'ct');
  151. $meta = $this->getParam('meta');
  152. if (empty($meta)) {
  153. $required_keys[] = 'pasteid';
  154. $required_keys[] = 'parentid';
  155. } else {
  156. $data['meta'] = $meta;
  157. }
  158. foreach ($required_keys as $key) {
  159. $data[$key] = $this->getParam($key);
  160. }
  161. // forcing a cast to int or float
  162. $data['v'] = $data['v'] + 0;
  163. return $data;
  164. }
  165. /**
  166. * Get a request parameter
  167. *
  168. * @access public
  169. * @param string $param
  170. * @param string $default
  171. * @return string
  172. */
  173. public function getParam($param, $default = '')
  174. {
  175. return array_key_exists($param, $this->_params) ?
  176. $this->_params[$param] : $default;
  177. }
  178. /**
  179. * Get host as requested by the client
  180. *
  181. * @access public
  182. * @return string
  183. */
  184. public function getHost()
  185. {
  186. return array_key_exists('HTTP_HOST', $_SERVER) ?
  187. htmlspecialchars($_SERVER['HTTP_HOST']) :
  188. 'localhost';
  189. }
  190. /**
  191. * Get request URI
  192. *
  193. * @access public
  194. * @return string
  195. */
  196. public function getRequestUri()
  197. {
  198. return array_key_exists('REQUEST_URI', $_SERVER) ?
  199. htmlspecialchars(
  200. parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
  201. ) : '/';
  202. }
  203. /**
  204. * If we are in a JSON API context
  205. *
  206. * @access public
  207. * @return bool
  208. */
  209. public function isJsonApiCall()
  210. {
  211. return $this->_isJsonApi;
  212. }
  213. /**
  214. * Override the default input stream source, used for unit testing
  215. *
  216. * @param string $input
  217. */
  218. public static function setInputStream($input)
  219. {
  220. self::$_inputStream = $input;
  221. }
  222. /**
  223. * Detect the clients supported media type and decide if its a JSON API call or not
  224. *
  225. * Adapted from: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
  226. *
  227. * @access private
  228. * @return bool
  229. */
  230. private function _detectJsonRequest()
  231. {
  232. $hasAcceptHeader = array_key_exists('HTTP_ACCEPT', $_SERVER);
  233. $acceptHeader = $hasAcceptHeader ? $_SERVER['HTTP_ACCEPT'] : '';
  234. // simple cases
  235. if (
  236. (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
  237. $_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
  238. ($hasAcceptHeader &&
  239. strpos($acceptHeader, self::MIME_JSON) !== false &&
  240. strpos($acceptHeader, self::MIME_HTML) === false &&
  241. strpos($acceptHeader, self::MIME_XHTML) === false)
  242. ) {
  243. return true;
  244. }
  245. // advanced case: media type negotiation
  246. $mediaTypes = array();
  247. if ($hasAcceptHeader) {
  248. $mediaTypeRanges = explode(',', trim($acceptHeader));
  249. foreach ($mediaTypeRanges as $mediaTypeRange) {
  250. if (preg_match(
  251. '#(\*/\*|[a-z\-]+/[a-z\-+*]+(?:\s*;\s*[^q]\S*)*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?#',
  252. trim($mediaTypeRange), $match
  253. )) {
  254. if (!isset($match[2])) {
  255. $match[2] = '1.0';
  256. } else {
  257. $match[2] = (string) floatval($match[2]);
  258. }
  259. if (!isset($mediaTypes[$match[2]])) {
  260. $mediaTypes[$match[2]] = array();
  261. }
  262. $mediaTypes[$match[2]][] = strtolower($match[1]);
  263. }
  264. }
  265. krsort($mediaTypes);
  266. foreach ($mediaTypes as $acceptedQuality => $acceptedValues) {
  267. if ($acceptedQuality === 0.0) {
  268. continue;
  269. }
  270. foreach ($acceptedValues as $acceptedValue) {
  271. if (
  272. strpos($acceptedValue, self::MIME_HTML) === 0 ||
  273. strpos($acceptedValue, self::MIME_XHTML) === 0
  274. ) {
  275. return false;
  276. } elseif (strpos($acceptedValue, self::MIME_JSON) === 0) {
  277. return true;
  278. }
  279. }
  280. }
  281. }
  282. return false;
  283. }
  284. }