request.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.22
  11. */
  12. /**
  13. * request
  14. *
  15. * parses request parameters and provides helper functions for routing
  16. */
  17. class request
  18. {
  19. /**
  20. * Input stream to use for PUT parameter parsing.
  21. *
  22. * @access private
  23. * @var string
  24. */
  25. private static $_inputStream = 'php://input';
  26. /**
  27. * Operation to perform.
  28. *
  29. * @access private
  30. * @var string
  31. */
  32. private $_operation = 'view';
  33. /**
  34. * Request parameters.
  35. *
  36. * @access private
  37. * @var array
  38. */
  39. private $_params = array();
  40. /**
  41. * If we are in a JSON API context.
  42. *
  43. * @access private
  44. * @var bool
  45. */
  46. private $_isJsonApi = false;
  47. /**
  48. * Constructor.
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. public function __construct()
  54. {
  55. // in case stupid admin has left magic_quotes enabled in php.ini (for PHP < 5.4)
  56. if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
  57. {
  58. $_POST = array_map('filter::stripslashes_deep', $_POST);
  59. $_GET = array_map('filter::stripslashes_deep', $_GET);
  60. $_COOKIE = array_map('filter::stripslashes_deep', $_COOKIE);
  61. }
  62. // decide if we are in JSON API or HTML context
  63. if (
  64. (array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) &&
  65. $_SERVER['HTTP_X_REQUESTED_WITH'] == 'JSONHttpRequest') ||
  66. (array_key_exists('HTTP_ACCEPT', $_SERVER) &&
  67. strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)
  68. )
  69. {
  70. $this->_isJsonApi = true;
  71. }
  72. // parse parameters, depending on request type
  73. switch (array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : 'GET')
  74. {
  75. case 'DELETE':
  76. case 'PUT':
  77. parse_str(file_get_contents(self::$_inputStream), $this->_params);
  78. break;
  79. case 'POST':
  80. $this->_params = $_POST;
  81. break;
  82. default:
  83. $this->_params = $_GET;
  84. }
  85. if (
  86. !array_key_exists('pasteid', $this->_params) &&
  87. !array_key_exists('jsonld', $this->_params) &&
  88. array_key_exists('QUERY_STRING', $_SERVER) &&
  89. !empty($_SERVER['QUERY_STRING'])
  90. )
  91. {
  92. $this->_params['pasteid'] = $_SERVER['QUERY_STRING'];
  93. }
  94. // prepare operation, depending on current parameters
  95. if (
  96. (array_key_exists('data', $this->_params) && !empty($this->_params['data'])) ||
  97. (array_key_exists('attachment', $this->_params) && !empty($this->_params['attachment']))
  98. )
  99. {
  100. $this->_operation = 'create';
  101. }
  102. elseif (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid']))
  103. {
  104. if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken']))
  105. {
  106. $this->_operation = 'delete';
  107. }
  108. else
  109. {
  110. $this->_operation = 'read';
  111. }
  112. }
  113. elseif (array_key_exists('jsonld', $this->_params) && !empty($this->_params['jsonld']))
  114. {
  115. $this->_operation = 'jsonld';
  116. }
  117. }
  118. /**
  119. * Get current operation.
  120. *
  121. * @access public
  122. * @return string
  123. */
  124. public function getOperation()
  125. {
  126. return $this->_operation;
  127. }
  128. /**
  129. * Get a request parameter.
  130. *
  131. * @access public
  132. * @param string $param
  133. * @param string $default
  134. * @return string
  135. */
  136. public function getParam($param, $default = '')
  137. {
  138. return array_key_exists($param, $this->_params) ? $this->_params[$param] : $default;
  139. }
  140. /**
  141. * If we are in a JSON API context.
  142. *
  143. * @access public
  144. * @return bool
  145. */
  146. public function isJsonApiCall()
  147. {
  148. return $this->_isJsonApi;
  149. }
  150. /**
  151. * Override the default input stream source, used for unit testing.
  152. *
  153. * @param string $input
  154. */
  155. public static function setInputStream($input)
  156. {
  157. self::$_inputStream = $input;
  158. }
  159. }