AppContext.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\App;
  3. use ThurData\Servers\KerioEmail\Core\HandlerError\WhmcsErrorManagerWrapper;
  4. class AppContext
  5. {
  6. protected $debugMode = true;
  7. public function __construct()
  8. {
  9. require_once __DIR__ . DIRECTORY_SEPARATOR . 'ErrorHandler.php';
  10. register_shutdown_function([$this, 'handleShutdown']);
  11. set_error_handler([$this, 'handleError'], E_ALL);
  12. $this->loadDebugState();
  13. //require app bootstrap
  14. require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Bootstrap.php';
  15. if ($this->debugMode)
  16. {
  17. spl_autoload_register(array('\ThurData\Servers\KerioEmail\Core\App\AppContext', 'loadClassLoader'), true, false);
  18. }
  19. }
  20. public function runApp($callerName = null, $params = [])
  21. {
  22. try
  23. {
  24. $app = new Application();
  25. $result = $app->run($callerName, $params);
  26. restore_error_handler();
  27. }
  28. catch (\Exception $exc)
  29. {
  30. logModuleCall(
  31. 'kerioEmail',
  32. __FUNCTION__,
  33. $exc,
  34. 'Debug Application',
  35. $result
  36. );
  37. restore_error_handler();
  38. return [
  39. 'status' => 'error',
  40. 'message' => $exc->getMessage()
  41. ];
  42. }
  43. return $result;
  44. }
  45. public function handleError($errno, $errstr, $errfile, $errline, $errcontext = null)
  46. {
  47. if ($this->debugMode || (!in_array($errno, ErrorHandler::WARNINGS) && !in_array($errno, ErrorHandler::NOTICES)))
  48. {
  49. $handler = new ErrorHandler();
  50. $errorToken = md5(time());
  51. $handler->logError($errorToken, $errno, $errstr, $errfile, $errline, $errcontext);
  52. }
  53. return true;
  54. }
  55. public function handleShutdown()
  56. {
  57. $errorInstance = null;
  58. $errManager = WhmcsErrorManagerWrapper::getErrorManager();
  59. if (is_object($errManager) && method_exists($errManager, 'getRunner'))
  60. {
  61. $runner = $errManager->getRunner();
  62. if (is_object($runner) && method_exists($runner, 'getHandlers'))
  63. {
  64. $handlers = $runner->getHandlers();
  65. foreach ($handlers as $handler)
  66. {
  67. $rfHandler = new \ReflectionClass($handler);
  68. $method = $rfHandler->getMethod('getException');
  69. $method->setAccessible(true);
  70. $error = $method->invoke($handler);
  71. if (is_object($error))
  72. {
  73. $errorInstance = $error;
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. if ($errorInstance === null)
  80. {
  81. $errorInstance = error_get_last();
  82. if ($errorInstance === null)
  83. {
  84. return;
  85. }
  86. $this->handleError($errorInstance['type'], $errorInstance['message'], $errorInstance['file'], $errorInstance['line'], '');
  87. return;
  88. }
  89. $handler = new ErrorHandler();
  90. $errorToken = md5(time());
  91. $handler->logError($errorToken, $errorInstance->getCode(), $errorInstance->getMessage(), $errorInstance->getFile(), $errorInstance->getLine(), $errorInstance->getTrace());
  92. if ($errorToken)
  93. {
  94. echo '<input type="hidden" id="mg-sh-h-492318-64534" value="' . $errorToken . '" mg-sh-h-492318-64534-end >';
  95. }
  96. }
  97. public function loadDebugState()
  98. {
  99. $path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . '.debug';
  100. if (file_exists($path))
  101. {
  102. $this->debugMode = true;
  103. return;
  104. }
  105. $this->debugMode = false;
  106. }
  107. public static function loadClassLoader($class)
  108. {
  109. $rawClass = trim($class, '\\');
  110. $pos = strpos($rawClass, 'ThurData\Servers\KerioEmail');
  111. if ($pos === 0)
  112. {
  113. if (!class_exists($class) && self::DEPRECATED[$rawClass])
  114. {
  115. echo 'This class no longer exists: ' . $class . '<br>';
  116. echo 'Use: ' . self::DEPRECATED[$rawClass];
  117. die();
  118. }
  119. }
  120. }
  121. const DEPRECATED = [
  122. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\BaseMassActionButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonMassAction',
  123. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\AddIconModalButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonCreate',
  124. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\BaseSubmitButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonSubmitForm',
  125. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\BaseButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonBase',
  126. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\BaseDatatableModalButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonDatatableShowModal',
  127. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\BaseModalDataTableActionButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonDataTableModalAction',
  128. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\RedirectButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonRedirect',
  129. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\BaseModalButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonModal',
  130. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\RedirectWithOutTooltipButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonRedirect',
  131. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\OnOffAjaxSwitch' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonSwitchAjax',
  132. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\CustomActionButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonCustomAction',
  133. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\CustomAjaxActionButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonAjaxCustomAction',
  134. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DatatableModalButtonContextLang' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonDatatableModalContextLang',
  135. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdownButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonDropdown',
  136. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\MassActionButtonContextLang' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonMassActionContextLang',
  137. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\Submit' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonSubmitForm',
  138. 'ThurData\Servers\KerioEmail\Core\HandlerError\WhmcsRegisterLoggin' => 'ThurData\Servers\KerioEmail\Core\HandlerError\WhmcsLogsHandler',
  139. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ButtonDropdown' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdown',
  140. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  141. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemCustonAjaxButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  142. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemCustonButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  143. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemDivider' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  144. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemModalButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  145. 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemRedirectButton' => 'ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  146. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\ApiException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  147. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\ApiWhmcsException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  148. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\ControllerException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  149. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\DependencyInjectionException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  150. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\MGModuleException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  151. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\RegisterException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  152. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\ServiceLocatorException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  153. 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\SmartyException' => 'ThurData\Servers\KerioEmail\Core\HandlerError\Exceptions\Exception',
  154. ];
  155. }