start.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Elgg log rotator.
  4. *
  5. * @package ElggLogRotate
  6. */
  7. elgg_register_event_handler('init', 'system', 'logrotate_init');
  8. function logrotate_init() {
  9. $period = elgg_get_plugin_setting('period', 'logrotate');
  10. $delete = elgg_get_plugin_setting('delete', 'logrotate');
  11. switch ($period) {
  12. case 'weekly':
  13. case 'monthly' :
  14. case 'yearly' :
  15. break;
  16. default:
  17. $period = 'monthly';
  18. }
  19. // Register cron hook for archival of logs
  20. elgg_register_plugin_hook_handler('cron', $period, 'logrotate_archive_cron');
  21. if ($delete != 'never') {
  22. // Register cron hook for deletion of selected archived logs
  23. elgg_register_plugin_hook_handler('cron', $delete, 'logrotate_delete_cron');
  24. }
  25. }
  26. /**
  27. * Trigger the log rotation.
  28. */
  29. function logrotate_archive_cron($hook, $entity_type, $returnvalue, $params) {
  30. $resulttext = elgg_echo("logrotate:logrotated");
  31. $day = 86400;
  32. $offset = 0;
  33. $period = elgg_get_plugin_setting('period', 'logrotate');
  34. switch ($period) {
  35. case 'weekly':
  36. $offset = $day * 7;
  37. break;
  38. case 'yearly':
  39. $offset = $day * 365;
  40. break;
  41. case 'monthly':
  42. default:
  43. // assume 28 days even if a month is longer. Won't cause data loss.
  44. $offset = $day * 28;
  45. }
  46. if (!archive_log($offset)) {
  47. $resulttext = elgg_echo("logrotate:lognotrotated");
  48. }
  49. return $returnvalue . $resulttext;
  50. }
  51. /**
  52. * Trigger the log deletion.
  53. */
  54. function logrotate_delete_cron($hook, $entity_type, $returnvalue, $params) {
  55. $resulttext = elgg_echo("logrotate:logdeleted");
  56. $day = 86400;
  57. $offset = 0;
  58. $period = elgg_get_plugin_setting('delete', 'logrotate');
  59. switch ($period) {
  60. case 'weekly':
  61. $offset = $day * 7;
  62. break;
  63. case 'yearly':
  64. $offset = $day * 365;
  65. break;
  66. case 'monthly':
  67. default:
  68. // assume 28 days even if a month is longer. Won't cause data loss.
  69. $offset = $day * 28;
  70. }
  71. if (!log_browser_delete_log($offset)) {
  72. $resulttext = elgg_echo("logrotate:lognotdeleted");
  73. }
  74. return $returnvalue . $resulttext;
  75. }
  76. /**
  77. * This function deletes archived copies of the system logs that are older than specified.
  78. *
  79. * @param int $time_of_delete An offset in seconds from now to delete log tables
  80. * @return bool Were any log tables deleted
  81. */
  82. function log_browser_delete_log($time_of_delete) {
  83. global $CONFIG;
  84. $cutoff = time() - (int)$time_of_delete;
  85. $deleted_tables = false;
  86. $results = get_data("SHOW TABLES like '{$CONFIG->dbprefix}system_log_%'");
  87. if ($results) {
  88. foreach ($results as $result) {
  89. $data = (array)$result;
  90. $table_name = array_shift($data);
  91. // extract log table rotation time
  92. $log_time = str_replace("{$CONFIG->dbprefix}system_log_", '', $table_name);
  93. if ($log_time < $cutoff) {
  94. if (delete_data("DROP TABLE $table_name") !== false) {
  95. // delete_data returns 0 when dropping a table (false for failure)
  96. $deleted_tables = true;
  97. } else {
  98. elgg_log("Failed to delete the log table $table_name", 'ERROR');
  99. }
  100. }
  101. }
  102. }
  103. return $deleted_tables;
  104. }