1 | <?php |
---|
2 | /** |
---|
3 | * logs.php |
---|
4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
5 | */ |
---|
6 | |
---|
7 | // require_once dirname(__FILE__) . '/_config.inc.php'; |
---|
8 | |
---|
9 | $auth->requireLogin(); |
---|
10 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG); |
---|
11 | $app->sslOn(); |
---|
12 | |
---|
13 | require_once 'codebase/lib/PageNumbers.inc.php'; |
---|
14 | require_once 'codebase/lib/SortOrder.inc.php'; |
---|
15 | require_once 'codebase/lib/TemplateGlue.inc.php'; |
---|
16 | require_once 'codebase/lib/Prefs.inc.php'; |
---|
17 | require_once 'codebase/lib/Upload.inc.php'; |
---|
18 | |
---|
19 | |
---|
20 | /****************************************************************************** |
---|
21 | * CODE CONFIG |
---|
22 | *****************************************************************************/ |
---|
23 | |
---|
24 | // Files with these extentions will be displayed at the top of the log list. |
---|
25 | $valid_file_extensions = array('', 'txt', 'log'); |
---|
26 | |
---|
27 | // Files that cannot be deleted (preg search expressions). |
---|
28 | $no_delete_files = '/^php_error_log$|^access_log$|^error_log$|^ssl_request_log$/'; |
---|
29 | |
---|
30 | // Files that cannot be cleared (preg search expressions). |
---|
31 | $no_clear_files = '/__|^access_log$|^error_log$|^ssl_request_log$/'; |
---|
32 | |
---|
33 | // Files that cannot be archived (preg search expressions). |
---|
34 | $no_archive_files = '/__|^access_log$|^error_log$|^ssl_request_log$/'; |
---|
35 | |
---|
36 | // Files that cannot be archived (preg search expressions). |
---|
37 | $no_download_files = '/^$/'; |
---|
38 | |
---|
39 | // Configure the prefs object. |
---|
40 | $tmp_prefs = new Prefs('admin_logs'); |
---|
41 | $tmp_prefs->setParam(array('persistent' => false)); |
---|
42 | $tmp_prefs->setDefaults(array( |
---|
43 | 'log_file' => $app->getParam('log_filename') |
---|
44 | )); |
---|
45 | if (getFormData('log', false)) { |
---|
46 | $tmp_prefs->set('log_file', getFormData('log')); |
---|
47 | } |
---|
48 | |
---|
49 | // Titles and navigation header. |
---|
50 | $nav->add(sprintf(_("Viewing log: <em>%s</em>"), $tmp_prefs->get('log_file')), '/admin/logs.php'); |
---|
51 | |
---|
52 | /******************************************************************** |
---|
53 | * MAIN |
---|
54 | ********************************************************************/ |
---|
55 | |
---|
56 | // Allow realtime file stats. |
---|
57 | clearstatcache(); |
---|
58 | |
---|
59 | // What action to take. |
---|
60 | switch (getFormData('op')) { |
---|
61 | case 'delete' : |
---|
62 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); |
---|
63 | deleteLog($tmp_prefs->get('log_file')); |
---|
64 | $tmp_prefs->set('log_file', $app->getParam('log_filename')); |
---|
65 | if ($app->validBoomerangURL('app_log')) { |
---|
66 | // Display boomerang page. |
---|
67 | $app->dieBoomerangURL('app_log'); |
---|
68 | } |
---|
69 | // Display default page. |
---|
70 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
71 | break; |
---|
72 | |
---|
73 | case 'clear' : |
---|
74 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); |
---|
75 | clearLog($tmp_prefs->get('log_file')); |
---|
76 | if ($app->validBoomerangURL('app_log')) { |
---|
77 | // Display boomerang page. |
---|
78 | $app->dieBoomerangURL('app_log'); |
---|
79 | } |
---|
80 | // Display default page. |
---|
81 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
82 | break; |
---|
83 | |
---|
84 | case 'archive' : |
---|
85 | // $auth->requireAccessClearance(ZONE_ADMIN_APPLOG_FUNC_RESET); |
---|
86 | if (archiveLog($tmp_prefs->get('log_file'))) { |
---|
87 | // Now flush current log. |
---|
88 | $app->dieURL($_SERVER['PHP_SELF'] . '?op=clear'); |
---|
89 | } |
---|
90 | if ($app->validBoomerangURL('app_log')) { |
---|
91 | // Display boomerang page. |
---|
92 | $app->dieBoomerangURL('app_log'); |
---|
93 | } |
---|
94 | // Display default page. |
---|
95 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
96 | break; |
---|
97 | |
---|
98 | // case 'ouput' : |
---|
99 | // $main_template = 'ouput'; |
---|
100 | // break; |
---|
101 | |
---|
102 | case 'download' : |
---|
103 | header('Content-Type: application/octet-stream'); |
---|
104 | header(sprintf('Content-Disposition: attachment; filename=%s.txt', $tmp_prefs->get('log_file'))); |
---|
105 | printLog($tmp_prefs->get('log_file')); |
---|
106 | die; |
---|
107 | break; |
---|
108 | |
---|
109 | default : |
---|
110 | $list =& getLog($tmp_prefs->get('log_file'), getFormData('search_query')); |
---|
111 | $main_template = 'log_list.ihtml'; |
---|
112 | break; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | /****************************************************************************** |
---|
117 | * TEMPLATE INITIALIZATION |
---|
118 | *****************************************************************************/ |
---|
119 | |
---|
120 | $logs = &getLogList(); |
---|
121 | |
---|
122 | // Instantiate page numbers. Total items are set and calculation is done in the getRecordList function. |
---|
123 | $page = new PageNumbers(); |
---|
124 | $page->setPerPage(getFormData('per_page'), 500); |
---|
125 | $page->setPageNumber(getFormData('page_number')); |
---|
126 | $page->setTotalItems(sizeof($list)); |
---|
127 | $page->per_page_options = array(100, 250, 500, 600, 800, 1000, 2000, 5000, 10000); |
---|
128 | $page->calculate(); |
---|
129 | |
---|
130 | include 'header.ihtml'; |
---|
131 | if ('output' == $main_template) { |
---|
132 | printLog($tmp_prefs->get('log_file')); |
---|
133 | } else { |
---|
134 | include 'codebase/services/templates/' . $main_template; |
---|
135 | } |
---|
136 | include 'footer.ihtml'; |
---|
137 | |
---|
138 | |
---|
139 | /******************************************************************** |
---|
140 | * FUNCTIONS |
---|
141 | ********************************************************************/ |
---|
142 | |
---|
143 | function deleteLog($log_file) |
---|
144 | { |
---|
145 | $app =& App::getInstance(); |
---|
146 | |
---|
147 | if (!file_exists($app->getParam('log_directory') . '/' . $log_file)) { |
---|
148 | $app->raiseMsg(sprintf(_("Log file %s does not exist."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
149 | $app->logMsg(sprintf('Cannot delete nonexistent log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
150 | return false; |
---|
151 | } |
---|
152 | |
---|
153 | if (!is_writable($app->getParam('log_directory') . '/' . $log_file) && !is_writable($app->getParam('log_directory'))) { |
---|
154 | $app->raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
155 | $app->logMsg(sprintf('Cannot delete log file %s, not writable.', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
156 | return false; |
---|
157 | } |
---|
158 | |
---|
159 | if (unlink($app->getParam('log_directory') . '/' . $log_file)) { |
---|
160 | $app->raiseMsg(sprintf(_("Log file %s has been deleted."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
161 | $app->logMsg(sprintf('Log file %s has been deleted', $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
162 | return true; |
---|
163 | } else { |
---|
164 | $app->raiseMsg(sprintf(_("Log file %s could not be deleted."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
165 | $app->logMsg(sprintf('unlink failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
166 | return false; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | function clearLog($log_file) |
---|
171 | { |
---|
172 | $app =& App::getInstance(); |
---|
173 | |
---|
174 | if (!$fp = fopen($app->getParam('log_directory') . '/' . $log_file, 'r+')) { |
---|
175 | $app->raiseMsg(sprintf(_("Log file %s could not be opened."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
176 | $app->logMsg(sprintf('fopen failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
177 | return false; |
---|
178 | } |
---|
179 | |
---|
180 | flock($fp, LOCK_EX); |
---|
181 | $ftruncate_return = ftruncate($fp, 0); |
---|
182 | flock($fp, LOCK_UN); |
---|
183 | fclose($fp); |
---|
184 | if (!$ftruncate_return) { |
---|
185 | $app->raiseMsg(sprintf(_("Log file %s could not be cleared."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
186 | $app->logMsg(sprintf('ftruncate failed on log file %s', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
187 | return false; |
---|
188 | } else { |
---|
189 | $app->raiseMsg(sprintf(_("Log file %s has been cleared."), $log_file), MSG_NOTICE, __FILE__, __LINE__); |
---|
190 | $app->logMsg(sprintf('Log file %s has been cleared', $log_file), LOG_INFO, __FILE__, __LINE__); |
---|
191 | return true; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | function archiveLog($log_file) |
---|
196 | { |
---|
197 | $app =& App::getInstance(); |
---|
198 | |
---|
199 | $old_file_name = $log_file; |
---|
200 | $new_file_name = $log_file . '__' . date('Y-m-d'); |
---|
201 | If (!is_writable($app->getParam('log_directory') . '')) { |
---|
202 | $app->raiseMsg(sprintf('Cannot archive log, log directory not writable: %s', $app->getParam('log_directory')), MSG_WARNING, __FILE__, __LINE__); |
---|
203 | $app->logMsg(sprintf('Cannot archive log, log directory not writable: %s', $app->getParam('log_directory')), LOG_WARNING, __FILE__, __LINE__); |
---|
204 | return false; |
---|
205 | } |
---|
206 | If (!copy($app->getParam('log_directory') . '/' . $old_file_name, $app->getParam('log_directory') . '/' . $new_file_name)) { |
---|
207 | $app->raiseMsg(sprintf(_("Cannot archive log, copying old log file failed."), null), MSG_WARNING, __FILE__, __LINE__); |
---|
208 | $app->logMsg(sprintf('Cannot archive log, copying old log file failed.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
209 | return false; |
---|
210 | } |
---|
211 | |
---|
212 | $app->raiseMsg(sprintf(_("Log file %s has been archived to %s."), $old_file_name, $new_file_name), MSG_NOTICE, __FILE__, __LINE__); |
---|
213 | $app->logMsg(sprintf('Log file %s has been archived to %s.', $old_file_name, $new_file_name), LOG_NOTICE, __FILE__, __LINE__); |
---|
214 | return true; |
---|
215 | } |
---|
216 | |
---|
217 | function printLog($log_file) |
---|
218 | { |
---|
219 | $app =& App::getInstance(); |
---|
220 | |
---|
221 | if (!is_file($app->getParam('log_directory') . '/' . $log_file)) { |
---|
222 | $app->raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
223 | $app->logMsg(sprintf('Log file %s not found.', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
224 | return false; |
---|
225 | } |
---|
226 | |
---|
227 | readfile($app->getParam('log_directory') . '/' . $log_file); |
---|
228 | } |
---|
229 | |
---|
230 | function &getLog($log_file, $search_query='') |
---|
231 | { |
---|
232 | $app =& App::getInstance(); |
---|
233 | |
---|
234 | $log = array(); |
---|
235 | |
---|
236 | if (!is_file($app->getParam('log_directory') . '/' . $log_file)) { |
---|
237 | $app->raiseMsg(sprintf(_("Log file %s not found."), $log_file), MSG_WARNING, __FILE__, __LINE__); |
---|
238 | $app->logMsg(sprintf('Log file %s not found.', $app->getParam('log_directory') . '/' . $log_file), LOG_WARNING, __FILE__, __LINE__); |
---|
239 | return $log; |
---|
240 | } |
---|
241 | $log = file($app->getParam('log_directory') . '/' . $log_file); |
---|
242 | |
---|
243 | if ('' != trim($search_query)) { |
---|
244 | if (getFormData('search_grep')) { |
---|
245 | $log = preg_grep('/' . str_replace('/', '\/', $search_query) . '/', $log); |
---|
246 | } else { |
---|
247 | $log = preg_grep('/' . preg_quote($search_query, '/') . '/i', $log); |
---|
248 | } |
---|
249 | } |
---|
250 | $log = array_values($log); |
---|
251 | return $log; |
---|
252 | } |
---|
253 | |
---|
254 | function &getLogList() |
---|
255 | { |
---|
256 | global $valid_file_extensions; |
---|
257 | $app =& App::getInstance(); |
---|
258 | |
---|
259 | // Get a list of all files in the log directory. |
---|
260 | $dir_handle = opendir($app->getParam('log_directory')); |
---|
261 | $list = array(); |
---|
262 | while ($dir_handle && ($file = readdir($dir_handle)) !== false) { |
---|
263 | if (!preg_match('/^\./', $file) && is_file($app->getParam('log_directory') . '/' . $file) && in_array(mb_strtolower(Upload::getFilenameExtension($file)), $valid_file_extensions)) { |
---|
264 | $list[] = array( |
---|
265 | 'filename' => $file, |
---|
266 | 'filesize' => filesize($app->getParam('log_directory') . '/' . $file), |
---|
267 | 'modified' => filemtime($app->getParam('log_directory') . '/' . $file), |
---|
268 | ); |
---|
269 | } |
---|
270 | } |
---|
271 | if (is_array($list) && !empty($list)) { |
---|
272 | sort($list); |
---|
273 | } |
---|
274 | return $list; |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | ?> |
---|