Http.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Api;
  3. use ThurData\Servers\KerioEmail\App\Libs\Exceptions\WhmcsApiException;
  4. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  5. use ThurData\Servers\KerioEmail\Core\Traits\IsDebugOn;
  6. use ThurData\Servers\KerioEmail\App\Libs\Exceptions\ApiException;
  7. /**
  8. * Description of Http
  9. *
  10. * @autor ThurData <info@thrudata.ch>
  11. */
  12. class Http
  13. {
  14. use IsDebugOn;
  15. /**
  16. * @var \AltoRouter;
  17. */
  18. protected $router;
  19. public function __construct($basepath)
  20. {
  21. $this->loadRouter($basepath);
  22. $this->router->addMatchTypes(["d" => "[^/]+"]);
  23. }
  24. /**
  25. * Parse API request
  26. */
  27. public function run()
  28. {
  29. try
  30. {
  31. $logger = $this->getLoggerObject();
  32. $match = $this->router->match();
  33. if($match)
  34. {
  35. $auth = $this->getAuthObject();
  36. $auth->run($match["name"]);
  37. $validator = $this->getValidatorObject();
  38. $validator->run($match["name"]);
  39. $request = explode("#", $match['target']);
  40. $action = [$this->getController($request[0]), $request[1]];
  41. $result = call_user_func_array($action, $match['params']);
  42. $logger->logInfo($match["name"], array_merge($match["params"], $_REQUEST), $result);
  43. echo json_encode($result);
  44. }
  45. else
  46. {
  47. header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
  48. echo json_encode(["error" => "Action not found"]);
  49. }
  50. exit;
  51. }
  52. catch (ApiException $mgex)
  53. {
  54. $code = $mgex->getMgHttpCode();
  55. $exdata = $mgex->getAdditionalData();
  56. $message = "{$mgex->getMgMessage(false)}" . ($this->isDebugOn() ? " | " . print_r($exdata, true) : "");
  57. }
  58. catch (WhmcsApiException $whmcsex)
  59. {
  60. $exdata = $whmcsex->getAdditionalData();
  61. $message = "{$exdata["data"]["result"]["message"]}: {$exdata["data"]["result"]["error"]}";
  62. }
  63. catch (\Exception $ex)
  64. {
  65. $exdata = $this->isDebugOn() ? print_r($ex, true) : null;
  66. $message = "Please contact administration (server side issue)" . ($exdata ? " | ". $exdata : "");
  67. }
  68. $logger->logError($match["name"], array_merge($match["params"], $_REQUEST), $exdata);
  69. $response = $this->getResponseBuilderObject();
  70. $message = $response->build($match["name"], $message);
  71. http_response_code($code ?: 500);
  72. echo json_encode(["error" => $message]);
  73. }
  74. /**
  75. * Load router object
  76. *
  77. * @param $basePath
  78. * @throws \Exception
  79. */
  80. protected function loadRouter($basePath)
  81. {
  82. $this->router = new \AltoRouter();
  83. $this->router->setBasePath($basePath);
  84. $routes = require ModuleConstants::getDevConfigDir().DS."api".DS."routes.php";
  85. $this->router->addRoutes($routes);
  86. }
  87. /**
  88. * Get controller object
  89. *
  90. * @return Object
  91. */
  92. protected function getController($classname)
  93. {
  94. $classname = "\\ThurData\\Servers\\KerioEmail\\App\\Http\\Api\\{$classname}";
  95. return new $classname;
  96. }
  97. /**
  98. * Get Authorization class object
  99. *
  100. * @return Auth class object
  101. */
  102. protected function getAuthObject()
  103. {
  104. $config = $this->getConfigElement("auth");
  105. $auth = new $config["class"];
  106. return $auth;
  107. }
  108. /**
  109. * @return mixed
  110. */
  111. protected function getValidatorObject()
  112. {
  113. $config = $this->getConfigElement("validator");
  114. $validator = new $config["class"];
  115. return $validator;
  116. }
  117. /**
  118. * Get Logger class object
  119. *
  120. * @return Logger class object
  121. */
  122. protected function getLoggerObject()
  123. {
  124. $config = $this->getConfigElement("logger");
  125. $auth = new $config["class"];
  126. return $auth;
  127. }
  128. /**
  129. * Get Logger class object
  130. *
  131. * @return Logger class object
  132. */
  133. protected function getResponseBuilderObject()
  134. {
  135. $config = $this->getConfigElement("responseBuilder");
  136. $auth = new $config["class"];
  137. return $auth;
  138. }
  139. /**
  140. * Get configuration element by type
  141. *
  142. * @param $type
  143. * @return mixed
  144. */
  145. protected function getConfigElement($type)
  146. {
  147. $config = require ModuleConstants::getDevConfigDir().DS."api".DS."config.php";
  148. foreach($config as $element)
  149. {
  150. if($element["type"] == $type)
  151. {
  152. return $element;
  153. }
  154. }
  155. }
  156. }