request.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.21.1
  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('QUERY_STRING', $_SERVER) &&
  88. !empty($_SERVER['QUERY_STRING'])
  89. )
  90. {
  91. $this->_params['pasteid'] = $_SERVER['QUERY_STRING'];
  92. }
  93. // prepare operation, depending on current parameters
  94. if (
  95. (array_key_exists('data', $this->_params) && !empty($this->_params['data'])) ||
  96. (array_key_exists('attachment', $this->_params) && !empty($this->_params['attachment']))
  97. )
  98. {
  99. $this->_operation = 'create';
  100. }
  101. elseif (array_key_exists('pasteid', $this->_params) && !empty($this->_params['pasteid']))
  102. {
  103. if (array_key_exists('deletetoken', $this->_params) && !empty($this->_params['deletetoken']))
  104. {
  105. $this->_operation = 'delete';
  106. }
  107. else
  108. {
  109. $this->_operation = 'read';
  110. }
  111. }
  112. }
  113. /**
  114. * Get current operation.
  115. *
  116. * @access public
  117. * @return string
  118. */
  119. public function getOperation()
  120. {
  121. return $this->_operation;
  122. }
  123. /**
  124. * Get a request parameter.
  125. *
  126. * @access public
  127. * @param string $param
  128. * @param string $default
  129. * @return string
  130. */
  131. public function getParam($param, $default = '')
  132. {
  133. return array_key_exists($param, $this->_params) ? $this->_params[$param] : $default;
  134. }
  135. /**
  136. * If we are in a JSON API context.
  137. *
  138. * @access public
  139. * @return bool
  140. */
  141. public function isJsonApiCall()
  142. {
  143. return $this->_isJsonApi;
  144. }
  145. /**
  146. * Override the default input stream source, used for unit testing.
  147. *
  148. * @param unknown $input
  149. */
  150. public static function setInputStream($input)
  151. {
  152. self::$_inputStream = $input;
  153. }
  154. }