request.php 4.1 KB

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