request.php 4.1 KB

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