Request.class.php~ 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Request
  4. *
  5. * Holds parameters, decorating them and providing easy access
  6. *
  7. * @author vesta, http://vestacp.com/
  8. * @copyright vesta 2010
  9. */
  10. class Request {
  11. protected $server = array();
  12. protected $post = array();
  13. protected $get = array();
  14. protected $global = array();
  15. protected $_merged = array();
  16. protected $_spell = array();
  17. /**
  18. *
  19. */
  20. public function __construct() {
  21. $this->post = $_POST;
  22. $this->get = $_GET;
  23. $this->server = $_SERVER;
  24. $this->mergeContainers();
  25. }
  26. /**
  27. * Merge holders into single holder
  28. */
  29. function mergeContainers() {
  30. $this->_merged = array_merge($this->server, $this->post, $this->get, $this->global);
  31. $this->_spell = json_decode($this->_merged['spell'], true);
  32. }
  33. /**
  34. * Get parameter
  35. *
  36. * @param <string> $key
  37. * @param <mixed> $default
  38. * @return <mixed>
  39. *
  40. */
  41. function getParameter($key, $default=false) {
  42. return isset($this->_merged[$key]) ? $this->_merged[$key] : $default;
  43. // return isset($this->_spell[$key]) ? $this->_spell[$key] : $default;
  44. }
  45. function getSpell() {
  46. return $this->_spell;
  47. }
  48. function hasParameter($key, $default=false) {
  49. return isset($this->_merged[$key]);
  50. }
  51. /**
  52. * Check if request is post
  53. *
  54. * TODO: write the method
  55. *
  56. * @return <boolean>
  57. */
  58. function isPost() {
  59. return true;
  60. }
  61. static function parseAjaxMethod($request) {
  62. if (!$request->hasParameter('jedi_method')) {
  63. throw new ProtectionException(Message::INVALID_METHOD);
  64. }
  65. $method = explode('.', $request->getParameter('jedi_method'));
  66. if (count($method) != 2) {
  67. throw new ProtectionException(Message::INVALID_METHOD);
  68. }
  69. return array('namespace' => ucfirst($method[0]), 'function' => strtolower($method[1]));
  70. }
  71. }