AppContext.php 10 KB

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