/proxima/core/classes/proxima/admin/log.php

https://github.com/proxima-cms/core · PHP · 149 lines · 93 code · 34 blank · 22 comment · 3 complexity · 4eafb32e26362c1208e99db14f2f31ab MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Admin log helper class
  4. *
  5. * @package Proxima CMS
  6. * @category Core
  7. * @author Proxima CMS Team
  8. * @copyright (c) 2011-2012 Proxima CMS Team
  9. * @license https://raw.github.com/proxima-cms/core/master/LICENSE.md
  10. */
  11. class Proxima_Admin_Log {
  12. // Returns a list of logs ordered by date in reverse
  13. public static function latest_entries()
  14. {
  15. // Recursively get a list of log files
  16. $logs = Kohana::list_files('logs');
  17. // Find the month directory name
  18. $log_year = array_pop( $logs );
  19. $log_month = array_pop( $log_year );
  20. $entries = array();
  21. // Build an array of log entries
  22. foreach($log_month as $day => $path)
  23. {
  24. $path = str_replace(APPPATH.'logs/', '', $path);
  25. // Create array of log entries and merge it in
  26. $entries = array_merge($entries, static::get_entries($path));
  27. }
  28. return static::format_entries($entries);
  29. }
  30. public static function format_entries($entries = array())
  31. {
  32. $entries = array_reverse($entries);
  33. // Split the entry details into useful arrays
  34. foreach($entries as $key => $entry)
  35. {
  36. if (!strstr($entry, '---')) continue;
  37. list($date, $log) = explode('---', $entry);
  38. list($date, $time) = explode(' ', $date);
  39. list($year, $month, $day) = explode('-', $date);
  40. list($hour, $minute, $second) = explode(':', $time);
  41. $entries[$key] = array(
  42. 'date' => trim($date),
  43. 'log' => trim($log),
  44. 'path' => "{$year}/{$month}/{$day}.php",
  45. 'timestamp' => (string) mktime($hour, $minute, $second, $month, $day, $year)
  46. );
  47. }
  48. return $entries;
  49. }
  50. // Returns a list of log entries for a specified day ($file)
  51. public static function get_entries($file = NULL)
  52. {
  53. $entries = NULL;
  54. if ($file !== NULL)
  55. {
  56. // Strip extension from file name
  57. $file = preg_replace('/\..*?$/', '', $file);
  58. // Find the full path to the file
  59. $path = Kohana::find_file('logs', $file);
  60. if ($path !== FALSE)
  61. {
  62. // Get file contents
  63. $contents = trim(strip_tags(file_get_contents($path)));
  64. $entries = explode("\n", $contents);
  65. }
  66. }
  67. return $entries;
  68. }
  69. // Return a formatted array of log files (including directories)
  70. public static function get_directories_and_files()
  71. {
  72. $logs = array();
  73. $files = static::get_logs();
  74. foreach($files as $logs_year => $logs_months)
  75. {
  76. $year = str_replace('logs/', '', $logs_year);
  77. $months = array();
  78. foreach($logs_months as $logs_month => $logs_day)
  79. {
  80. $month = (int) str_replace("logs/{$year}/", '', $logs_month);
  81. $month = date('F', mktime(0, 0, 0, $month, 1, date('Y')));
  82. $days = array();
  83. foreach($logs_day as $logs_log)
  84. {
  85. $days[] = $logs_log;
  86. }
  87. $months[$month] = $days;
  88. }
  89. $logs[$year] = $months;
  90. }
  91. return $logs;
  92. }
  93. // Return a formatted array of log files (excluding log directories)
  94. public static function get_log_files()
  95. {
  96. $files = array();
  97. $logs = Kohana::list_files('logs');
  98. foreach($logs as $year => $months)
  99. {
  100. foreach($months as $month => $day)
  101. {
  102. foreach($day as $log)
  103. {
  104. $files[] = $log;
  105. }
  106. }
  107. }
  108. return $files;
  109. }
  110. // Return an array of all log directories and files
  111. public static function get_logs()
  112. {
  113. return Kohana::list_files('logs');
  114. }
  115. } // End Admin_Log