LogDAO.php 936 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. class FreshRSS_LogDAO {
  3. private static function logPath(): string {
  4. return USERS_PATH . '/' . (Minz_User::name() ?? Minz_User::INTERNAL_USER) . '/' . LOG_FILENAME;
  5. }
  6. /** @return array<FreshRSS_Log> */
  7. public static function lines(): array {
  8. $logs = array();
  9. $handle = @fopen(self::logPath(), 'r');
  10. if ($handle) {
  11. while (($line = fgets($handle)) !== false) {
  12. if (preg_match('/^\[([^\[]+)\] \[([^\[]+)\] --- (.*)$/', $line, $matches)) {
  13. $myLog = new FreshRSS_Log ();
  14. $myLog->_date($matches[1]);
  15. $myLog->_level($matches[2]);
  16. $myLog->_info($matches[3]);
  17. $logs[] = $myLog;
  18. }
  19. }
  20. fclose($handle);
  21. }
  22. return array_reverse($logs);
  23. }
  24. public static function truncate(): void {
  25. file_put_contents(self::logPath(), '');
  26. if (FreshRSS_Auth::hasAccess('admin')) {
  27. file_put_contents(ADMIN_LOG, '');
  28. file_put_contents(API_LOG, '');
  29. file_put_contents(PSHB_LOG, '');
  30. }
  31. }
  32. }