WhmcsLogsHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\HandlerError;
  3. use \ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Addon\Config;
  4. use \ThurData\Servers\KerioEmail\Core\Http\Request;
  5. /**
  6. * Handles adding new records to WHMCS Module and Activity Logs
  7. *
  8. * @autor ThurData <info@thurdata.ch>
  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(),
  47. $this->getFullAction(),
  48. $this->getRequestData(),
  49. $responseData,
  50. print_r($responseData, true),
  51. $replaceVars
  52. );
  53. }
  54. return $this;
  55. }
  56. /**
  57. * @param mixed $message
  58. * @param int $userId
  59. * @return $this
  60. */
  61. public function addActiveLog($message, $userId = 0)
  62. {
  63. if ($this->isDebugin())
  64. {
  65. if (is_string($message) === false)
  66. {
  67. $message = print_r($message, true);
  68. }
  69. logActivity($message, $userId);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * @return array
  75. */
  76. private function getRequestData()
  77. {
  78. return array_merge($this->request->request->all(), $this->request->query->all());
  79. }
  80. /**
  81. * @return string
  82. */
  83. private function getModuleName()
  84. {
  85. return $this->addon->getConfigValue("name", 'KerioEmail');
  86. }
  87. /**
  88. * @return bool
  89. */
  90. private function isDebugin()
  91. {
  92. return (bool)((int)$this->addon->getConfigValue("debug", "0"));
  93. }
  94. /**
  95. * @return string
  96. */
  97. private function getFullAction()
  98. {
  99. return $this->request->get('mg-page', 'Home') . $this->request->get('mg-action', 'Index');
  100. }
  101. public function addModuleLogError(Exceptions\Exception $exception, array $replaceVars = [])
  102. {
  103. $responseData = $exception->getOriginalException();
  104. if (!$exception->isLogable())
  105. {
  106. return;
  107. }
  108. if (is_object($responseData))
  109. {
  110. $responseData = print_r([
  111. 'message' => $responseData->getMessage(),
  112. 'code' => $responseData->getCode(),
  113. 'file' => $responseData->getFile(),
  114. 'line' => $responseData->getLine(),
  115. 'trace' => $responseData->getTraceAsString(),
  116. //'previous' => $responseData->getPrevious()
  117. ], true);
  118. }
  119. if (!$responseData)
  120. {
  121. $responseData = [
  122. 'message' => $exception->getMgMessage(false),
  123. 'trace' => $exception->getTraceAsString()
  124. ];
  125. }
  126. logModuleCall(
  127. $this->getModuleName(),
  128. $this->getFullAction(),
  129. [
  130. 'deteils' => $exception->getDetailsToLog(),
  131. 'request' => $this->getRequestData()
  132. ],
  133. $responseData,
  134. print_r($responseData, true),
  135. $replaceVars
  136. );
  137. return $this;
  138. }
  139. }