AjaxHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /**
  30. * Called functions should reply in the following way
  31. * return $this->reply($result, $data, $msg, $extra);
  32. *
  33. * @param Request $request
  34. * @return string
  35. */
  36. public function dispatch(Request $request)
  37. {
  38. VestaSession::start();
  39. $allowed_actions = array(
  40. 'MAIN.signin',
  41. 'MAIN.logout',
  42. 'MAIN.requestPassword',
  43. 'MAIN.resetPassword',
  44. 'MAIN.about');
  45. $method = Request::parseAjaxMethod($request);
  46. if (!in_array($method['namespace'].'.'.$method['function'], $allowed_actions)) {
  47. $user = $this->getLoggedUser();
  48. }
  49. $inc_file = V_ROOT_DIR . 'api' . DIRECTORY_SEPARATOR . $method['namespace'] . '.class.php';
  50. if (!is_readable($inc_file)) {
  51. throw new SystemException(Message::INVALID_METHOD);
  52. }
  53. require $inc_file;
  54. $space = new $method['namespace'];
  55. $method = $method['function'] . 'Execute';
  56. if (!method_exists($space, $method)) {
  57. throw new SystemException(Message::INVALID_METHOD);
  58. }
  59. return $space->$method($request);
  60. }
  61. /**
  62. * Prepare response for ajax
  63. */
  64. public function reply($result, $data, $message = '', $extra = array())
  65. {
  66. return json_encode(array('result' => $result,
  67. 'data' => $data,
  68. 'message' => $message,
  69. 'extra' => $extra,
  70. 'errors' => $this->errors
  71. ));
  72. }
  73. static function makeReply($reply)
  74. {
  75. print $reply;
  76. }
  77. //
  78. // Error handlers
  79. //
  80. static function generalError($error)
  81. {
  82. self::renderGlobalError(Message::ERROR, Message::GENERAL_ERROR, $error);
  83. }
  84. static function protectionError($error)
  85. {
  86. self::renderGlobalError(Message::ERROR, Message::PROTECTION_ERROR, $error);
  87. }
  88. static function systemError($error)
  89. {
  90. self::renderGlobalError(Message::ERROR, Message::SYSTEM_ERROR, $error);
  91. }
  92. static function renderGlobalError($type, $message, $error)
  93. {
  94. /*$trace = $error->getTrace();
  95. AjaxHandler::makeReply(
  96. AjaxHandler::getInstance()->reply(false, $type, $message . ': ' . $error->getMessage(), $trace[0]['file'] . ' / ' . $trace[0]['line'])
  97. );*/
  98. print $message;
  99. }
  100. }