Request.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.4.0
  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. // it might be a creation or a deletion, the latter is detected below
  99. $this->_operation = 'create';
  100. $this->_params = Json::decode(
  101. file_get_contents(self::$_inputStream)
  102. );
  103. break;
  104. default:
  105. $this->_params = $_GET;
  106. }
  107. if (
  108. !array_key_exists('pasteid', $this->_params) &&
  109. !array_key_exists('jsonld', $this->_params) &&
  110. !array_key_exists('link', $this->_params) &&
  111. array_key_exists('QUERY_STRING', $_SERVER) &&
  112. !empty($_SERVER['QUERY_STRING'])
  113. ) {
  114. $this->_params['pasteid'] = $this->getPasteId();
  115. }
  116. // prepare operation, depending on current parameters
  117. if (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid'])) {
  118. if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken'])) {
  119. $this->_operation = 'delete';
  120. } elseif ($this->_operation != 'create') {
  121. $this->_operation = 'read';
  122. }
  123. } elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld'])) {
  124. $this->_operation = 'jsonld';
  125. } elseif (array_key_exists('link', $this->_params) && !empty($this->_params['link'])) {
  126. $request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
  127. if (strpos($request_url, '/shortenviayourls?link=') !== false) {
  128. $this->_operation = 'yourlsproxy';
  129. }
  130. }
  131. }
  132. /**
  133. * Get current operation
  134. *
  135. * @access public
  136. * @return string
  137. */
  138. public function getOperation()
  139. {
  140. return $this->_operation;
  141. }
  142. /**
  143. * Get data of paste or comment
  144. *
  145. * @access public
  146. * @return array
  147. */
  148. public function getData()
  149. {
  150. $data = array(
  151. 'adata' => $this->getParam('adata'),
  152. );
  153. $required_keys = array('v', 'ct');
  154. $meta = $this->getParam('meta');
  155. if (empty($meta)) {
  156. $required_keys[] = 'pasteid';
  157. $required_keys[] = 'parentid';
  158. } else {
  159. $data['meta'] = $meta;
  160. }
  161. foreach ($required_keys as $key) {
  162. $data[$key] = $this->getParam($key, $key == 'v' ? 1 : '');
  163. }
  164. // forcing a cast to int or float
  165. $data['v'] = $data['v'] + 0;
  166. return $data;
  167. }
  168. /**
  169. * Get a request parameter
  170. *
  171. * @access public
  172. * @param string $param
  173. * @param string $default
  174. * @return string
  175. */
  176. public function getParam($param, $default = '')
  177. {
  178. return array_key_exists($param, $this->_params) ?
  179. $this->_params[$param] : $default;
  180. }
  181. /**
  182. * Get host as requested by the client
  183. *
  184. * @access public
  185. * @return string
  186. */
  187. public function getHost()
  188. {
  189. return array_key_exists('HTTP_HOST', $_SERVER) ?
  190. htmlspecialchars($_SERVER['HTTP_HOST']) :
  191. 'localhost';
  192. }
  193. /**
  194. * Get request URI
  195. *
  196. * @access public
  197. * @return string
  198. */
  199. public function getRequestUri()
  200. {
  201. return array_key_exists('REQUEST_URI', $_SERVER) ?
  202. htmlspecialchars(
  203. parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
  204. ) : '/';
  205. }
  206. /**
  207. * If we are in a JSON API context
  208. *
  209. * @access public
  210. * @return bool
  211. */
  212. public function isJsonApiCall()
  213. {
  214. return $this->_isJsonApi;
  215. }
  216. /**
  217. * Override the default input stream source, used for unit testing
  218. *
  219. * @param string $input
  220. */
  221. public static function setInputStream($input)
  222. {
  223. self::$_inputStream = $input;
  224. }
  225. /**
  226. * Detect the clients supported media type and decide if its a JSON API call or not
  227. *
  228. * Adapted from: https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#3771447
  229. *
  230. * @access private
  231. * @return bool
  232. */
  233. private function _detectJsonRequest()
  234. {
  235. $hasAcceptHeader = array_key_exists('HTTP_ACCEPT', $_SERVER);
  236. $acceptHeader = $hasAcceptHeader ? $_SERVER['HTTP_ACCEPT'] : '';
  237. // simple cases
  238. if (
  239. (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
  240. $_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
  241. ($hasAcceptHeader &&
  242. strpos($acceptHeader, self::MIME_JSON) !== false &&
  243. strpos($acceptHeader, self::MIME_HTML) === false &&
  244. strpos($acceptHeader, self::MIME_XHTML) === false)
  245. ) {
  246. return true;
  247. }
  248. // advanced case: media type negotiation
  249. $mediaTypes = array();
  250. if ($hasAcceptHeader) {
  251. $mediaTypeRanges = explode(',', trim($acceptHeader));
  252. foreach ($mediaTypeRanges as $mediaTypeRange) {
  253. if (preg_match(
  254. '#(\*/\*|[a-z\-]+/[a-z\-+*]+(?:\s*;\s*[^q]\S*)*)(?:\s*;\s*q\s*=\s*(0(?:\.\d{0,3})|1(?:\.0{0,3})))?#',
  255. trim($mediaTypeRange), $match
  256. )) {
  257. if (!isset($match[2])) {
  258. $match[2] = '1.0';
  259. } else {
  260. $match[2] = (string) floatval($match[2]);
  261. }
  262. if (!isset($mediaTypes[$match[2]])) {
  263. $mediaTypes[$match[2]] = array();
  264. }
  265. $mediaTypes[$match[2]][] = strtolower($match[1]);
  266. }
  267. }
  268. krsort($mediaTypes);
  269. foreach ($mediaTypes as $acceptedQuality => $acceptedValues) {
  270. if ($acceptedQuality === '0.0') {
  271. continue;
  272. }
  273. foreach ($acceptedValues as $acceptedValue) {
  274. if (
  275. strpos($acceptedValue, self::MIME_HTML) === 0 ||
  276. strpos($acceptedValue, self::MIME_XHTML) === 0
  277. ) {
  278. return false;
  279. } elseif (strpos($acceptedValue, self::MIME_JSON) === 0) {
  280. return true;
  281. }
  282. }
  283. }
  284. }
  285. return false;
  286. }
  287. }