BxDolLogsStorageFolder.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolLogsStorageFolder extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected function __construct()
  12. {
  13. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  14. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  15. parent::__construct();
  16. }
  17. /**
  18. * Prevent cloning the instance
  19. */
  20. public function __clone()
  21. {
  22. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  23. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  24. }
  25. /**
  26. * Get singleton instance of the class
  27. */
  28. public static function getInstance()
  29. {
  30. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  31. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLogsStorageFolder();
  32. return $GLOBALS['bxDolClasses'][__CLASS__];
  33. }
  34. /**
  35. * Write to log
  36. * @param $oObject logs object
  37. * @param $mixed string or array to log
  38. * @return true on success or false on error
  39. */
  40. public function add($oObject, $mixed)
  41. {
  42. if (!$mixed)
  43. return true;
  44. $sFile = BX_DIRECTORY_PATH_LOGS . $oObject->getObjectName() . '.log';
  45. $s = $this->formatLogString($oObject, $mixed);
  46. $bNewFile = !file_exists($sFile);
  47. $bRet = file_put_contents($sFile, $s, FILE_APPEND) ? true : false;
  48. if ($bNewFile && $bRet)
  49. chmod($sFile, BX_DOL_FILE_RIGHTS);
  50. return $bRet;
  51. }
  52. protected function formatLogString($oObject, $mixed, $bIncludeObjectName = false)
  53. {
  54. $s = date('M d H:i:s');
  55. if ($bIncludeObjectName)
  56. $s .= ' ' . $oObject->getObjectName();
  57. $s .= ' [' . (int)bx_get_logged_profile_id() . '] ';
  58. if (!empty($_SERVER['REQUEST_URI']))
  59. $s .= $_SERVER['REQUEST_URI'] . (!empty($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '');
  60. elseif (defined('BX_DOL_CRON_EXECUTE'))
  61. $s .= 'CRON';
  62. else
  63. $s .= 'EMPTY';
  64. $s .= ' ' . trim(is_array($mixed) ? print_r($mixed, true) : $mixed);
  65. $s .= "\n";
  66. return $s;
  67. }
  68. /**
  69. * Get logs
  70. * @param $oObject logs object
  71. * @param $iLines number of lines from the tail
  72. * @param $sFilter filter lines which include this phrase
  73. * @return array of strings
  74. */
  75. public function get($oObject, $iLines, $sFilter = false)
  76. {
  77. $sFile = BX_DIRECTORY_PATH_LOGS . $oObject->getObjectName() . '.log';
  78. $iSize = filesize($sFile);
  79. if (!($fd = fopen($sFile, 'r+')))
  80. return false;
  81. $iPos = $iSize;
  82. $n = 0;
  83. while ($n < $iLines+1 && $iPos > 0) {
  84. if (-1 == fseek($fd, $iPos))
  85. break;
  86. if (false === ($s = fread($fd, 1)))
  87. break;
  88. if ($s === "\n")
  89. ++$n;
  90. $iPos--;
  91. }
  92. $a = array();
  93. for ($i = 0; $i < $iLines; $i++) {
  94. if (false === ($s = fgets($fd)))
  95. break;
  96. if (!$sFilter || false !== stripos($s, $sFilter))
  97. array_push($a, $s);
  98. }
  99. fclose($fd);
  100. return $a;
  101. }
  102. /**
  103. * Check if filtering supported in `get` method
  104. */
  105. public function isFilterAvail()
  106. {
  107. return true;
  108. }
  109. /**
  110. * Check if `get` method available
  111. */
  112. public function isGetAvail()
  113. {
  114. return true;
  115. }
  116. }
  117. /** @} */