start.php ➔ logrotate_init()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg log rotator.
4
 *
5
 * @package ElggLogRotate
6
 */
7
8
elgg_register_event_handler('init', 'system', 'logrotate_init');
9
10
function logrotate_init() {
11
	$period = elgg_get_plugin_setting('period', 'logrotate');
12
	$delete = elgg_get_plugin_setting('delete', 'logrotate');
13
	switch ($period) {
14
		case 'weekly':
15
		case 'monthly' :
16
		case 'yearly' :
17
			break;
18
		default:
19
			$period = 'monthly';
20
	}
21
22
	// Register cron hook for archival of logs
23
	elgg_register_plugin_hook_handler('cron', $period, 'logrotate_archive_cron');
24
25
	if ($delete != 'never') {
26
		// Register cron hook for deletion of selected archived logs
27
		elgg_register_plugin_hook_handler('cron', $delete, 'logrotate_delete_cron');
28
	}
29
}
30
31
/**
32
 * Trigger the log rotation.
33
 */
34 View Code Duplication
function logrotate_archive_cron($hook, $entity_type, $returnvalue, $params) {
35
	$resulttext = elgg_echo("logrotate:logrotated");
36
37
	$day = 86400;
38
39
	$offset = 0;
40
	$period = elgg_get_plugin_setting('period', 'logrotate');
41
	switch ($period) {
42
		case 'weekly':
43
			$offset = $day * 7;
44
			break;
45
		case 'yearly':
46
			$offset = $day * 365;
47
			break;
48
		case 'monthly':
49
		default:
50
			// assume 28 days even if a month is longer. Won't cause data loss.
51
			$offset = $day * 28;
52
	}
53
54
	if (!archive_log($offset)) {
55
		$resulttext = elgg_echo("logrotate:lognotrotated");
56
	}
57
58
	return $returnvalue . $resulttext;
59
}
60
61
/**
62
 * Trigger the log deletion.
63
 */
64 View Code Duplication
function logrotate_delete_cron($hook, $entity_type, $returnvalue, $params) {
65
	$resulttext = elgg_echo("logrotate:logdeleted");
66
67
	$day = 86400;
68
69
	$offset = 0;
70
	$period = elgg_get_plugin_setting('delete', 'logrotate');
71
	switch ($period) {
72
		case 'weekly':
73
			$offset = $day * 7;
74
			break;
75
		case 'yearly':
76
			$offset = $day * 365;
77
			break;
78
		case 'monthly':
79
		default:
80
			// assume 28 days even if a month is longer. Won't cause data loss.
81
			$offset = $day * 28;
82
	}
83
84
	if (!log_browser_delete_log($offset)) {
85
		$resulttext = elgg_echo("logrotate:lognotdeleted");
86
	}
87
88
	return $returnvalue . $resulttext;
89
}
90
91
/**
92
 * This function deletes archived copies of the system logs that are older than specified.
93
 *
94
 * @param int $time_of_delete An offset in seconds from now to delete log tables
95
 * @return bool Were any log tables deleted
96
 */
97
function log_browser_delete_log($time_of_delete) {
98
	global $CONFIG;
99
100
	$cutoff = time() - (int)$time_of_delete;
101
102
	$deleted_tables = false;
103
	$results = get_data("SHOW TABLES like '{$CONFIG->dbprefix}system_log_%'");
104
	if ($results) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $results of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
105
		foreach ($results as $result) {
106
			$data = (array)$result;
107
			$table_name = array_shift($data);
108
			// extract log table rotation time
109
			$log_time = str_replace("{$CONFIG->dbprefix}system_log_", '', $table_name);
110
			if ($log_time < $cutoff) {
111
				if (delete_data("DROP TABLE $table_name") !== false) {
112
					// delete_data returns 0 when dropping a table (false for failure)
113
					$deleted_tables = true;
114
				} else {
115
					elgg_log("Failed to delete the log table $table_name", 'ERROR');
116
				}
117
			}
118
		}
119
	}
120
121
	return $deleted_tables;
122
}
123