Request.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Request
  4. *
  5. * Holds parameters, decorating them and providing easy access
  6. *
  7. * @author Malishev Dima <dima.malishev@gmail.com>
  8. * @author vesta, http://vestacp.com
  9. * @copyright vesta 2010-2011
  10. */
  11. class Request
  12. {
  13. protected $server = array();
  14. protected $post = array();
  15. protected $get = array();
  16. protected $global = array();
  17. protected $_merged = array();
  18. //protected $_spell = array();
  19. /**
  20. *
  21. */
  22. public function __construct()
  23. {
  24. $this->post = $_POST;
  25. $this->get = $_GET;
  26. $this->server = $_SERVER;
  27. $this->mergeContainers();
  28. }
  29. /**
  30. * Merge holders into single holder
  31. */
  32. public function mergeContainers()
  33. {
  34. $this->_merged = array_merge($this->server,
  35. $this->post,
  36. $this->get,
  37. $this->global);
  38. //$this->_spell = json_decode($this->_merged['spell'], true);
  39. }
  40. /**
  41. * Get parameter
  42. *
  43. * @param string $key
  44. * @param mixed $default
  45. * @return mixed
  46. */
  47. public function getParameter($key, $default=false)
  48. {
  49. $param = isset($this->_merged[$key]) ? $this->_merged[$key] : $default;
  50. if ($json = @json_decode($param, true)) {
  51. return $json;
  52. }
  53. return $param;
  54. }
  55. /**
  56. * Get spell variable from parameters
  57. *
  58. * @return array
  59. */
  60. /*public function getSpell()
  61. {
  62. return $this->_spell;
  63. }*/
  64. /**
  65. * Check if parameter is set
  66. *
  67. * @param string $key
  68. * @param mixed $default
  69. * @return mixed
  70. *
  71. */
  72. public function hasParameter($key, $default=false)
  73. {
  74. return isset($this->_merged[$key]);
  75. }
  76. /**
  77. * Check if request is post
  78. *
  79. * TODO: write the method
  80. *
  81. * @return boolean
  82. */
  83. public function isPost()
  84. {
  85. return true;
  86. }
  87. /**
  88. * Dissassemble ajax method
  89. * Breaks ajax requested method param into "ENTITY"."ACTION"
  90. * for instance DNS.getList into "namespase" => DNS, "function" => "getList"
  91. * for triggering ($DNS->getListExecuty();)
  92. *
  93. * TODO: write the method
  94. *
  95. * @return array
  96. */
  97. static public function parseAjaxMethod($request)
  98. {
  99. if (!$request->hasParameter('jedi_method'))
  100. {
  101. throw new ProtectionException(Message::INVALID_METHOD);
  102. }
  103. $method = explode('.', $request->getParameter('jedi_method'));
  104. if (count($method) != 2)
  105. {
  106. throw new ProtectionException(Message::INVALID_METHOD);
  107. }
  108. return array('namespace' => ucfirst($method[0]), 'function' => strtolower($method[1]));
  109. }
  110. }