forked from s9y/additional_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserendipity_event_cronjob.php
176 lines (149 loc) · 6.68 KB
/
serendipity_event_cronjob.php
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
<?php #
if (IN_serendipity !== true) {
die ("Don't hack!");
}
// Probe for a language include with constants. Still include defines later on, if some constants were missing
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
if (file_exists($probelang)) {
include $probelang;
}
include dirname(__FILE__) . '/lang_en.inc.php';
class serendipity_event_cronjob extends serendipity_event {
function introspect(&$propbag)
{
global $serendipity;
$propbag->add('name', PLUGIN_EVENT_CRONJOB_NAME);
$propbag->add('description', PLUGIN_EVENT_CRONJOB_DESC);
$propbag->add('stackable', false);
$propbag->add('author', 'Garvin Hicking');
$propbag->add('version', '0.10');
$propbag->add('requirements', array(
'serendipity' => '0.8',
'php' => '4.1.0'
));
$hooks = array('frontend_configure' => true, 'frontend_footer' => true, 'cronjob');
$ts = $this->getValues('fetch');
foreach($ts as $span => $unixsec) {
$hooks['cronjob_' . $span] = true;
}
$propbag->add('event_hooks', $hooks);
$propbag->add('groups', array('BACKEND_FEATURES'));
$propbag->add('configuration', array('visitor_cronjob'));
$this->dependencies = array();
}
static function getValues($mode = 'select') {
// Add future timespans here
static $timespan = array(
'off' => 0,
'always' => 1,
'5min' => 300,
'30min' => 1800,
'1h' => 3600,
'12h' => 43200,
'daily' => 86400,
'weekly' => 604800,
'monthly' => 2419200,
);
if ($mode === 'fetch') {
return $timespan;
}
$r = array();
foreach($timespan AS $span => $unix) {
$r[$span] = $span;
}
return $r;
}
function example() {
global $serendipity;
$s = '';
$s .= '<br /><div style="border: 1px solid red; padding: 5px;">' . PLUGIN_EVENT_CRONJOB_DETAILS . '</div>';
$s .= '<br /><fieldset><legend>' . PLUGIN_EVENT_CRONJOB_LOG . '</legend><table cellspacing=1 cellpadding=2>';
$s .= '<tr><th>' . DATE . '</th><th>' . TYPE . '</th><th>' . DESCRIPTION . '</th></tr>';
$res = serendipity_db_query("SELECT timestamp, type, reason FROM {$serendipity['dbPrefix']}cronjoblog ORDER BY timestamp DESC");
if (is_array($res)) {
foreach($res AS $row) {
$s .= '<tr><td>' . date('d.m.Y H:i', $row['timestamp']) . '</td><td>' . (function_exists('serendipity_specialchars') ? serendipity_specialchars($row['type']) : htmlspecialchars($row['type'], ENT_COMPAT, LANG_CHARSET)) . '</td><td>' . (function_exists('serendipity_specialchars') ? serendipity_specialchars($row['reason']) : htmlspecialchars($row['reason'], ENT_COMPAT, LANG_CHARSET)) . '</td></tr>' . "\n";
}
}
$s .= '</table></fieldset>';
return $s;
}
function introspect_config_item($name, &$propbag) {
switch($name) {
case 'visitor_cronjob':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_EVENT_CRONJOB_VISITOR);
$propbag->add('description', PLUGIN_EVENT_CRONJOB_VISITOR_DESC);
$propbag->add('default', 'true');
break;
default:
return false;
}
return true;
}
function generate_content(&$title) {
$title = $this->title;
}
function checkDB() {
global $serendipity;
if ($this->get_config('db_ver') < 2) {
$q = "CREATE TABLE {$serendipity['dbPrefix']}cronjoblog (
timestamp int(10) {UNSIGNED} default null,
type varchar(255),
reason text)";
$sql = serendipity_db_schema_import($q);
$q = "CREATE INDEX kspamidx ON {$serendipity['dbPrefix']}cronjoblog (timestamp);";
$sql = serendipity_db_schema_import($q);
$q = "CREATE INDEX kspamtypeidx ON {$serendipity['dbPrefix']}cronjoblog (type(191));";
$sql = serendipity_db_schema_import($q);
$this->set_config('db_ver', 2);
}
}
static function log($msg, $type = 'global') {
global $serendipity;
$now = time();
echo '[' . date('d.m.Y H:i', $now) . '] ' . $msg . "<br />\n";
$r = serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}cronjoblog (timestamp, type, reason) VALUES ($now, '$type', '" . serendipity_db_escape_string($msg) . "')");
}
function event_hook($event, &$bag, &$eventData, $addData = null) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'frontend_footer':
if (serendipity_db_bool($this->get_config('visitor_cronjob'))) {
echo '<img alt="cronjob" src="' . $serendipity['baseURL'] . 'index.php?serendipity[cronjob]=true" style="display: none" width="0" height="0" />';
}
break;
case 'frontend_configure':
if ($serendipity['GET']['cronjob']) {
session_write_close();
$this->checkDB();
$now = time();
$ts = $this->getValues('fetch');
foreach($ts as $span => $time) {
if ($span == 'off') continue;
if ($this->get_config('last_' . $span) < ($now - $time)) {
$this->set_config('last_' . $span, $now);
$this->log('Executing "cronjob_' . $span . '"', 'execute');
serendipity_plugin_api::hook_event('cronjob', $span);
serendipity_plugin_api::hook_event('cronjob_' . $span, $serendipity['GET']);
$this->log('Next scheduled run: ' . date('d.m.Y H:i', $now+$time), 'next');
} else {
echo 'Discarding "' . $span . '"<br />' . "\n";
}
}
die('Cronjobs finished.');
}
break;
default:
return false;
break;
}
return true;
} else {
return false;
}
}
}
/* vim: set sts=4 ts=4 expandtab : */