diff options
author | brettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-04-27 19:47:24 +0000 |
---|---|---|
committer | brettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2011-04-27 19:47:24 +0000 |
commit | 2af513f0ad0e6b692e95bf9f0ec1ba9dc4501ed0 (patch) | |
tree | e9a4bbf267eb39b558784a95933060a960ad7121 | |
parent | 312cab981f0b8dc8bb7e03d19460884b8d673aff (diff) | |
download | elgg-2af513f0ad0e6b692e95bf9f0ec1ba9dc4501ed0.tar.gz elgg-2af513f0ad0e6b692e95bf9f0ec1ba9dc4501ed0.tar.bz2 |
Fixes #3253. Merged in Shashank's patch.
git-svn-id: http://code.elgg.org/elgg/trunk@9033 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r-- | mod/logrotate/languages/en.php | 11 | ||||
-rw-r--r-- | mod/logrotate/start.php | 75 | ||||
-rw-r--r-- | mod/logrotate/views/default/settings/logrotate/edit.php | 22 |
3 files changed, 103 insertions, 5 deletions
diff --git a/mod/logrotate/languages/en.php b/mod/logrotate/languages/en.php index e60d1b841..c3cf2d8ad 100644 --- a/mod/logrotate/languages/en.php +++ b/mod/logrotate/languages/en.php @@ -14,6 +14,15 @@ $english = array( 'logrotate:logrotated' => "Log rotated\n", 'logrotate:lognotrotated' => "Error rotating log\n", + + 'logrotate:date' => 'Delete archived logs older than a', + + 'logrotate:week' => 'week', + 'logrotate:month' => 'month', + 'logrotate:year' => 'year', + + 'logrotate:logdeleted' => "Log deleted\n", + 'logrotate:lognotdeleted' => "Error deleting log\n", ); -add_translation("en", $english);
\ No newline at end of file +add_translation("en", $english); diff --git a/mod/logrotate/start.php b/mod/logrotate/start.php index 25c01d45b..c05506d83 100644 --- a/mod/logrotate/start.php +++ b/mod/logrotate/start.php @@ -9,6 +9,7 @@ elgg_register_event_handler('init', 'system', 'logrotate_init'); function logrotate_init() { $period = elgg_get_plugin_setting('period', 'logrotate'); + $time = elgg_get_plugin_setting('time', 'logrotate'); switch ($period) { case 'weekly': case 'monthly' : @@ -18,14 +19,16 @@ function logrotate_init() { $period = 'monthly'; } - // Register cron hook - elgg_register_plugin_hook_handler('cron', $period, 'logrotate_cron'); + // Register cron hook for archival of logs + elgg_register_plugin_hook_handler('cron', $period, 'logrotate_archive_cron'); + // Register cron hook for deletion of selected archived logs + elgg_register_plugin_hook_handler('cron', $time, 'logrotate_delete_cron'); } /** * Trigger the log rotation. */ -function logrotate_cron($hook, $entity_type, $returnvalue, $params) { +function logrotate_archive_cron($hook, $entity_type, $returnvalue, $params) { $resulttext = elgg_echo("logrotate:logrotated"); $day = 86400; @@ -51,3 +54,69 @@ function logrotate_cron($hook, $entity_type, $returnvalue, $params) { return $returnvalue . $resulttext; } + +/** + * Trigger the log deletion. + */ +function logrotate_delete_cron($hook, $entity_type, $returnvalue, $params) { + $resulttext = elgg_echo("logrotate:logdeleted"); + + $day = 86400; + + $offset = 0; + $period = elgg_get_plugin_setting('time', 'logrotate'); + switch ($period) { + case 'weekly': + $offset = $day * 7; + break; + case 'yearly': + $offset = $day * 365; + break; + case 'monthly': + default: + // assume 28 days even if a month is longer. Won't cause data loss. + $offset = $day * 28; + } + + if (!log_browser_delete_log($offset)) { + $resulttext = elgg_echo("logrotate:lognotdeleted"); + } + + return $returnvalue . $resulttext; +} + +/** + * This function deletes archived copies of the system logs that are older than specified. + * + * @param int $time_of_delete An offset in seconds from now to delete (useful for log deletion) + */ + +function log_browser_delete_log($time_of_delete) { + global $CONFIG; + + $offset = (int)$time_of_delete; + $now = time(); + + $ts = $now - $offset; + + $FLAG = 1; + $result = mysql_query("SHOW TABLES like '{$CONFIG->dbprefix}system_log_%'"); + while ($showtablerow = mysql_fetch_array($result)) { + //To obtain time of archival + $log_time = explode("{$CONFIG->dbprefix}system_log_", $showtablerow[0]); + if ($log_time < $ts) { + //If the time of archival is before the required offset then delete + if (!mysql_query("DROP TABLE $showtablerow[0]")) { + $FLAG = 0; + } + } + } + + //Check if the appropriate tables have been deleted and return true if yes + if ($FLAG) { + return true; + } else { + return false; + } + +} diff --git a/mod/logrotate/views/default/settings/logrotate/edit.php b/mod/logrotate/views/default/settings/logrotate/edit.php index c73bec52c..206051780 100644 --- a/mod/logrotate/views/default/settings/logrotate/edit.php +++ b/mod/logrotate/views/default/settings/logrotate/edit.php @@ -6,10 +6,14 @@ */ $period = $vars['entity']->period; +$time = $vars['entity']->time; if (!$period) { $period = 'monthly'; } - + +if (!$time) { + $time = 'monthly'; +} ?> <p> <?php echo elgg_echo('logrotate:period'); ?> @@ -25,4 +29,20 @@ if (!$period) { 'value' => $period )); ?> + + <?php echo elgg_echo('<br>'); ?> + + <?php echo elgg_echo('logrotate:date'); ?> + + <?php + echo elgg_view('input/pulldown', array( + 'internalname' => 'params[time]', + 'options_values' => array( + 'weekly' => elgg_echo('logrotate:week'), + 'monthly' => elgg_echo('logrotate:month'), + 'yearly' => elgg_echo('logrotate:year'), + ), + 'value' => $time + )); + ?> </p> |