aboutsummaryrefslogtreecommitdiff
path: root/mod/logrotate/start.php
blob: fb6674356191080e4b9b360102544bf5c77a51c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
	/**
	 * Elgg log rotator.
	 * 
	 * @package ElggLogRotate
	 * @author Curverider Ltd
	 * @link http://elgg.com/
	 */

	/**
	 * Initialise the plugin.
	 *
	 */
	function logrotate_init()
	{
		$period = get_plugin_setting('period','logrotate');
		switch ($period)
		{
			case 'weekly':
			case 'monthly' :
			case 'yearly' : 
			break;
			default: $period = 'monthly';
		}
		
		// Register cron hook
		register_plugin_hook('cron', $period, 'logrotate_cron');
	}
	
	/**
	 * Trigger the log rotation.
	 *
	 */
	function logrotate_cron($hook, $entity_type, $returnvalue, $params)
	{
		$resulttext = elgg_echo("logrotate:logrotated");
		
		$day = 86400;
		
		$offset = 0;
		$period = get_plugin_setting('period','logrotate');
		switch ($period)
		{
			case 'weekly': $offset = $day * 7; break;
			case 'yearly' : $offset = $day * 365; break;
			case 'monthly' :  // assume 28 days even if a month is longer. Won't cause data loss.
			default: $offset = $day * 28;;
		}
		
		if (!archive_log($offset))
			$resulttext = elgg_echo("logrotate:lognotrotated");
			
		return $returnvalue . $resulttext;
	}
	
	// Initialise plugin
	register_elgg_event_handler('init','system','logrotate_init');
?>