AjaxHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Ajax Handler
  4. *
  5. * @author vesta, http://vestacp.com/
  6. * @author Dmitry Malishev <dima.malishev@gmail.com>
  7. * @author Dmitry Naumov-Socolov <naumov.socolov@gmail.com>
  8. * @copyright vesta 2010-2011
  9. */
  10. class AjaxHandler {
  11. static public $instance = null;
  12. const JSON = 'json';
  13. const TEXT = 'text';
  14. public $errors = array();
  15. public $status = TRUE;
  16. /**
  17. * Grab current instance or create it
  18. *
  19. * @return AjaxHandler
  20. */
  21. static function getInstance($request=null)
  22. {
  23. return null == self::$instance ? self::$instance = new self() : self::$instance;
  24. }
  25. public function getLoggedUser()
  26. {
  27. return VestaSession::getInstance()->getUser();
  28. }
  29. public function getLoggedUserRole()
  30. {
  31. return VestaSession::getInstance()->getUserRole();
  32. }
  33. /**
  34. * Called functions should reply in the following way
  35. * return $this->reply($result, $data, $msg, $extra);
  36. *
  37. * @param Request $request
  38. * @return string
  39. */
  40. public function dispatch(Request $request)
  41. {
  42. VestaSession::start();
  43. $allowed_actions = array(
  44. 'MAIN.signin',
  45. 'MAIN.logout',
  46. 'MAIN.requestPassword',
  47. 'MAIN.resetPassword',
  48. 'MAIN.about');
  49. $method = Request::parseAjaxMethod($request);
  50. if (!in_array($method['namespace'].'.'.$method['function'], $allowed_actions)) {
  51. $user = $this->getLoggedUser();
  52. }
  53. $inc_file = V_ROOT_DIR . 'api' . DIRECTORY_SEPARATOR . $method['namespace'] . '.class.php';
  54. if (!is_readable($inc_file)) {
  55. throw new SystemException(Message::INVALID_METHOD);
  56. }
  57. require $inc_file;
  58. $space = new $method['namespace'];
  59. $method = $method['function'] . 'Execute';
  60. if (!method_exists($space, $method)) {
  61. throw new SystemException(Message::INVALID_METHOD);
  62. }
  63. return $space->$method($request);
  64. }
  65. /**
  66. * Prepare response for ajax
  67. */
  68. public function reply($result, $data = null, $message = '', $extra = array())
  69. {
  70. return json_encode(array('result' => $result,
  71. 'data' => $data,
  72. 'message' => $message,
  73. 'extra' => $extra,
  74. 'errors' => $this->errors
  75. ));
  76. }
  77. static function makeReply($reply)
  78. {
  79. print $reply;
  80. }
  81. //
  82. // Error handlers
  83. //
  84. static function generalError($error)
  85. {
  86. self::renderGlobalError(Message::ERROR, Message::GENERAL_ERROR, $error);
  87. }
  88. static function protectionError($error)
  89. {
  90. self::renderGlobalError(Message::ERROR, Message::PROTECTION_ERROR, $error);
  91. }
  92. static function systemError($error)
  93. {
  94. self::renderGlobalError(Message::ERROR, Message::SYSTEM_ERROR, $error);
  95. }
  96. static function renderGlobalError($type, $message, $error)
  97. {
  98. /*$trace = $error->getTrace();
  99. AjaxHandler::makeReply(
  100. AjaxHandler::getInstance()->reply(false, $type, $message . ': ' . $error->getMessage(), $trace[0]['file'] . ' / ' . $trace[0]['line'])
  101. );*/
  102. print $message;
  103. }
  104. }