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