WhmcsLogsHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\HandlerError;
  3. use \ModulesGarden\ProxmoxAddon\Core\App\Controllers\Instances\Addon\Config;
  4. use \ModulesGarden\ProxmoxAddon\Core\Http\Request;
  5. /**
  6. * Handles adding new records to WHMCS Module and Activity Logs
  7. *
  8. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  9. */
  10. class WhmcsLogsHandler
  11. {
  12. /**
  13. * @var Config
  14. */
  15. private $addon;
  16. /**
  17. * @var Request
  18. */
  19. private $request;
  20. /**
  21. * @param Addon $addon
  22. * @param Request $request
  23. */
  24. public function __construct(Config $addon, Request $request)
  25. {
  26. $this->addon = $addon;
  27. $this->request = $request;
  28. }
  29. /**
  30. * @param mixed $responseData
  31. * @param array $replaceVars
  32. * @return $this
  33. */
  34. public function addModuleLog($responseData = [], array $replaceVars = [])
  35. {
  36. if ($this->isDebugin())
  37. {
  38. if (is_array($responseData) === false)
  39. {
  40. if (is_object($responseData))
  41. {
  42. $responseData = print_r($responseData, true);
  43. }
  44. }
  45. logModuleCall(
  46. $this->getModuleName(), $this->getFullAction(), $this->getRequestData(), $responseData, print_r($responseData, true), $replaceVars
  47. );
  48. }
  49. return $this;
  50. }
  51. /**
  52. * @param mixed $message
  53. * @param int $userId
  54. * @return $this
  55. */
  56. public function addActiveLog($message, $userId = 0)
  57. {
  58. if ($this->isDebugin())
  59. {
  60. if (is_string($message) === false)
  61. {
  62. $message = print_r($message, true);
  63. }
  64. logActivity($message, $userId);
  65. }
  66. return $this;
  67. }
  68. /**
  69. * @return array
  70. */
  71. private function getRequestData()
  72. {
  73. return array_merge($this->request->request->all(), $this->request->query->all());
  74. }
  75. /**
  76. * @return string
  77. */
  78. private function getModuleName()
  79. {
  80. return $this->addon->getConfigValue("name", 'proxmoxAddon');
  81. }
  82. /**
  83. * @return bool
  84. */
  85. private function isDebugin()
  86. {
  87. return (bool) ((int) $this->addon->getConfigValue("debug", "0"));
  88. }
  89. /**
  90. * @return string
  91. */
  92. private function getFullAction()
  93. {
  94. return $this->request->get('mg-page', 'Home') . $this->request->get('mg-action', 'Index');
  95. }
  96. public function addModuleLogError(Exceptions\Exception $exception, array $replaceVars = [])
  97. {
  98. $responseData = $exception->getOriginalException();
  99. if (!$exception->isLogable())
  100. {
  101. return;
  102. }
  103. if (is_object($responseData))
  104. {
  105. $responseData = print_r($responseData, true);
  106. }
  107. if (!$responseData)
  108. {
  109. $responseData = [
  110. 'message' => $exception->getMgMessage(false),
  111. 'trace' => $exception->getTrace()
  112. ];
  113. }
  114. logModuleCall(
  115. $this->getModuleName(), $this->getFullAction(), [
  116. 'deteils' => $exception->getDetailsToLog(),
  117. 'request' => $this->getRequestData()
  118. ], $responseData, print_r($responseData, true), $replaceVars
  119. );
  120. return $this;
  121. }
  122. }