This repository was archived by the owner on Oct 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgit-wp-log-spooler.php
More file actions
307 lines (238 loc) · 9.27 KB
/
cgit-wp-log-spooler.php
File metadata and controls
307 lines (238 loc) · 9.27 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/*
Plugin Name: Castlegate IT WP Log Spooler
Plugin URI: http://github.com/castlegateit/cgit-wp-log-spooler
Description: Provides a page within WP admin to spool any logs.
Version: 2.2.0
Author: Castlegate IT
Author URI: http://www.castlegateit.co.uk/
License: MIT
*/
if (!class_exists('CGIT_Log_Spooler')) {
class CGIT_Log_Spooler
{
/**
* Array of sources for CSVs available for download. Logs are added to
* this array by using the cgit_log_spooler filter. Can be a file or a query.
* See docs for full information on filtering this array.
*
* @var array
*/
static private $log_sources = array();
/**
* Array of available resources for download. Populated by scanning log
* directories and adding in results of queries sent
*
* @var array
*/
static private $logs = array();
/**
* The WordPress database object stored here for our comfort and convenience
*
* @var object
*/
static private $wpdb;
/**
* Loader method
*
* @return void
*/
static function on_load()
{
global $wpdb;
self::$wpdb = $wpdb;
// The log directory array is populated by filters
self::$log_sources = apply_filters('cgit_log_spooler', self::$log_sources);
// Add WordPress menu
add_action('admin_menu', array(__CLASS__,'add_download_page'));
// Spool any downloads
self::spool_download();
}
/**
* Add WordPress menu page
*
* @return void
*/
static function add_download_page()
{
add_menu_page(
'Website log downloads',
'Website Logs',
'edit_pages',
'contact-logs',
array(__CLASS__, 'logs_page'),
'',
74
);
}
/**
* Scan all log directories for CSV files
*
* @return void
*/
static function scan_for_logs()
{
// Loop through logs array
foreach (self::$log_sources as $key => $settings) {
// Disregard anything without a callback, or a `dir`, `label` or `query` index
if (empty($settings['label']) ||
(empty($settings['dir']) && empty($settings['query']) && empty($settings['callback']))
) {
continue;
}
if (!empty($settings['dir'])) {
// Get the CSV files from a directory
$files = self::read_directory($settings['dir']);
// Add to the list of files
if ($files) {
self::$logs[$key] = array(
'files' => $files
);
}
} else if (!empty($settings['callback'])) {
// Accept the incoming data and move it into array for spooling.
$data = $settings['callback'];
self::$logs[$key] = array (
'callback' => $data
);
} else {
// Run the query
if ($result = self::$wpdb->get_results($settings['query'], ARRAY_A)) {
if (self::$wpdb->num_rows)
self::$logs[$key] = array(
'result' => $result
);
}
}
}
}
/**
* Read a log directory for CSV files
*
* @param string $directory
* @return array
*/
static function read_directory($directory)
{
$files = scandir($directory);
$logs = array();
foreach ($files as $file) {
if (substr($file, -4) == '.csv') {
$logs[] = $file;
}
}
return $logs;
}
/**
* Generate the download log page
*
* @return void
*/
static function logs_page()
{
// Scan for logs
self::scan_for_logs();
?>
<div class="wrap">
<h2>Download site logs</h2>
<p>Download logs in .CSV format suitable for import into spreadsheet applications such as Excel.</p>
<?php if (!self::$logs) : ?>
<p>There are no logs currently available for download.</p>
<?php else : ?>
<?php foreach (self::$logs as $key => $resources) : ?>
<?php if (count($resources)==1 && !empty($resources['result'])) : ?>
<p> • <a href="<?php echo $_SERVER['REQUEST_URI']; ?>&cgit_log_key=<?=$key; ?>&cgit_log_index=result"><?=self::$log_sources[$key]['label']?></a></p>
<?php elseif (count($resources)==1 && !empty($resources['callback'])) : ?>
<p> • <a href="<?php echo $_SERVER['REQUEST_URI']; ?>&cgit_log_key=<?=$key; ?>&cgit_log_index=callback"><?=self::$log_sources[$key]['label']?></a></p>
<?php else : ?>
<h3><?=self::$log_sources[$key]['label']?></h3>
<?php foreach ($resources['files'] as $index => $file) : ?>
<p> • <a href="<?php echo $_SERVER['REQUEST_URI']; ?>&cgit_log_key=<?=$key; ?>&cgit_log_index=<?=$index?>"><?php echo $file; ?></a></p>
<?php endforeach ?>
<?php endif; ?>
<?php endforeach ?>
<?php endif ?>
</div><?php
}
/**
* Spool a CSV download if one has been requested.
*/
static function spool_download()
{
global $pagenow;
if ($pagenow == 'admin.php' &&
isset($_GET['cgit_log_key']) &&
isset($_GET['cgit_log_index']) &&
current_user_can('edit_pages') &&
array_key_exists($_GET['cgit_log_key'], self::$log_sources)
) {
// Scan for logs
self::scan_for_logs();
$key = $_GET['cgit_log_key'];
$file_index = $_GET['cgit_log_index'];
$resource = self::$logs[$key];
// Is it a file or a query result?
if (!empty($resource['files'])) {
if (array_key_exists($file_index, $resource['files'])) {
// Build the filename and path
$path = self::$log_sources[$key]['dir'] . '/';
$filename = $resource['files'][$file_index];
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=" . $filename);
header("Cache-Control: private");
header("Expires: 0");
readfile($path . $filename);
exit();
}
} elseif (!empty($resource['callback'])) {
$first_row = true;
$filename = self::$log_sources[$key]['label'] . '.csv';
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=" . $filename);
header("Cache-Control: private");
header("Expires: 0");
$fh = fopen('php://output','w');
foreach ($resource['callback'] as $row) {
if ($first_row) {
// Spool the header
fputcsv($fh, array_keys($row));
$first_row = false;
}
fputcsv($fh, $row);
}
fclose($fh);
exit();
} else if (!empty($resource['result'])) {
$first_row = true;
$filename = self::$log_sources[$key]['label'] . '.csv';
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=" . $filename);
header("Cache-Control: private");
header("Expires: 0");
$fh = fopen('php://output','w');
foreach ($resource['result'] as $row) {
if ($first_row) {
// Spool the header
fputcsv($fh, array_keys($row));
$first_row = false;
}
fputcsv($fh, $row);
}
fclose($fh);
exit();
}
}
}
}
/**
* Delay the loading of the plugin until the `init` event to allow for
* logs to be added to the array
*
* @return void
*/
function cgit_log_spool_wait()
{
CGIT_Log_Spooler::on_load();
}
add_action('init', 'cgit_log_spool_wait', 999);
}