-
Notifications
You must be signed in to change notification settings - Fork 12
/
cron.php
315 lines (260 loc) · 9.59 KB
/
cron.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
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
308
309
310
311
312
313
314
315
<?php
if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../../');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
/**
* The Cron Class is just an initial wrapper which tries to create a blank cron.conf.php file
* It can also add/edit/delete Cron Jobs that are being configured at the GUI
*
* @author i-net software <[email protected]>
* @author Gerry Weissbach <[email protected]>
*
*/
class cron_plugin_siteexport {
private $configFile;
public $configuration;
/**
* Initial setup for this class
*/
function __construct()
{
$this->configFile = DOKU_INC . 'conf/cron.config.php';
$this->readCronSettings();
}
/**
* Save a new entry, or overwrite an existing entry in the cron config
* @param $parameters parameters which define the cron job
* @param $canOverwrite information if an existing entry should be overwritten
*/
public function saveCronDataWithParameters($parameters, $canOverwrite=false)
{
if ( !$canOverwrite && $this->hasCronJobForParameters($parameters) )
{
return "Cannot save. The Cron Job already exists - but no permission given to overwrite";
}
$this->configuration[$this->cronJobNameForParameters($parameters)] = $parameters;
if ( !$this->writeCronSettings() ) {
return "There was an error while saving the Cron configuration during a save action";
}
}
public function deleteCronDataWithParameters($parameters)
{
unset($this->configuration[$this->cronJobNameForParameters($parameters)]);
if ( !$this->writeCronSettings() ) {
return "There was an error while saving the Cron configuration during a deletion action";
}
}
/**
* Reads the Configuration if not loaded yet
*/
public function readCronSettings()
{
if ( !$this->configFile )
{
return false;
}
$settings = array();
if ( file_exists($this->configFile) )
{
include($this->configFile);
}
$this->configuration = $settings;
return true;
}
/**
* Writes the Configuration if it is being set
*/
public function writeCronSettings()
{
global $conf;
if ( !$this->configFile )
{
// Nothing has changed.
return true;
}
// backup current file (remove any existing backup)
if (@file_exists($this->configFile)) {
if (@file_exists($this->configFile.'.bak')) @unlink($this->configFile.'.bak');
if (!io_rename($this->configFile, $this->configFile.'.bak')) return false;
}
if (!$fh = @fopen($this->configFile, 'wb')) {
io_rename($this->configFile.'.bak', $this->configFile); // problem opening, restore the backup
return false;
}
$out = $this->cronHeading();
$out .= $this->recurseSettingsToOut($this->configuration);
$out .= $this->cronFooter();
// Finally write it out.
@fwrite($fh, $out);
fclose($fh);
if($conf['fperm']) chmod($this->configFile, $conf['fperm']);
return true;
}
/**
* Header text for the cron config file
*/
private function cronHeading()
{
$DOKU_URL = DOKU_URL;
return <<<OUTPUT
<?php
/*
* Siteexport Cron Job Configuration
* Auto-generated by siteexport
* Run for user: {$_SERVER['REMOTE_USER']}
*/
// Required to set the HTTP_HOST which is usually not set in CLI Environments
\$_SERVER['HTTP_HOST'] = "{$_SERVER['HTTP_HOST']}";
// This is not the nice way to inject another ROOT-URL, but we do not want to modify other stuff.
if(!defined('DOKU_URL')) define('DOKU_URL', "{$DOKU_URL}");
OUTPUT;
}
/**
* Footer Text for the cron config file
*/
private function cronFooter()
{
return <<<OUTPUT
// end of auto-generated content
OUTPUT;
}
/**
* Checks if the settings file is writeable
*/
public function canWriteSettings()
{
if ( !file_exists($this->configFile) && !$this->writeCronSettings() )
{
return false;
}
return is_writable($this->configFile);
}
/**
* Recursively walk through the settings and generate a nice looking string
* @param $settings Array of named settings
* @param $levelPrefix Prefix that will be build during recursion. It will contain a string for the named array depth
*/
private function recurseSettingsToOut($settings, $levelPrefix = null)
{
if ( !is_array($settings) )
{
// If this is a value and the levelPrefix is not empty, print it out
if ( $levelPrefix == null || empty($settings) )
{
return '';
}
return '$settings' . $levelPrefix . ' = "' . trim($settings) . "\";\n";
}
$out = '';
// walk recursively through the content and giv it all back
foreach ($settings as $name => $value )
{
$out .= $this->recurseSettingsToOut($value, $levelPrefix . '["' . trim($name) . '"]');
}
return $out;
}
/**
* Checks if there is already a Cron Job for the given parameters
* @param $parameters
*/
public function hasCronJobForParameters($parameters)
{
return array_key_exists($this->cronJobNameForParameters($parameters), $this->configuration);
}
/**
* returns a name for the parameters
* @param $parameters
*/
public function cronJobNameForParameters($parameters)
{
return md5($parameters);
}
}
// ensure that the request comes from the cli
if ( !array_key_exists('REMOTE_ADDR', $_SERVER) && 'cli' == php_sapi_name()) {
ini_set('memory_limit','512M');
// overriding this will lead to too many error messages
// error_reporting(E_ALL);
/**
* Cli Cron is responsible for doing the actual fetching of documentation
* @author gamma
*
*/
class plugin_siteexport_cli_cron {
private $cronPlugin;
private $siteexportAjax;
public $error;
/**
* Instantiate and load plugin
*/
public function __construct()
{
// Needs to go first, to initialize the config which holds some special treatment
$this->cronPlugin = new cron_plugin_siteexport();
// Load later to have the config up and running.
// the config needs to adjust some variables of the server
@require_once(DOKU_INC . 'inc/init.php');
@require_once(DOKU_INC . 'inc/common.php');
@require_once(DOKU_INC . 'inc/indexer.php');
@require_once(DOKU_INC . 'inc/io.php');
@require_once(DOKU_INC . 'inc/confutils.php');
if ( !$this->siteexportAjax = plugin_load('action', 'siteexport_ajax' ) ) {
$this->error = "Faild! Ajax Plugin not loaded\n";
}
}
/**
* let the plugin run!
*/
public function run() {
global $ID;
global $INFO;
$originalREquest = $_REQUEST;
foreach ( $this->cronPlugin->configuration as $name => $config )
{
// retrieve parameters
list($id,$parameters) = explode('?', $config, 2);
$function = new siteexport_functions(false);
$_REQUEST = $function->parseStringToRequestArray($parameters, true);
unset($function);
$ID = $_REQUEST['id'] = cleanID($id); // re-set the ID
// $ID = getID();
// Lets start over!
$this->siteexportAjax->__init_functions();
$this->siteexportAjax->functions->settings->isCLI = true;
$this->siteexportAjax->functions->settings->isAuthed = true;
$INFO['perm'] = AUTH_DELETE; // Fake authentication
// Fake security Token if none given
if ( empty( $_REQUEST['sectok'] ) ) {
$_REQUEST['sectok'] = $this->siteexportAjax->functions->getSecurityToken();
}
$data = $this->siteexportAjax->__get_siteexport_list_and_init_tocs($ID);
// If there is nothing in there - ignore. This may mean we have a valid cache
if ( count($data) == 0 ) {
continue;
}
foreach ( $data as $site ) {
// We want to create a specific file! - have to reset it every time in here
// $_REQUEST['pattern'] = $name;
$status = $this->siteexportAjax->__siteexport_add_site($site['id']);
}
$this->siteexportAjax->functions->checkIfCacheFileExistsForFileWithPattern($this->siteexportAjax->functions->getCacheFileNameForPattern(), $name);
// Wat zum geier?
$this->siteexportAjax->cleanCacheFiles();
}
}
}
$cron = new plugin_siteexport_cli_cron();
if ( empty($cron->error) )
{
$cron->run();
} else
{
echo <<<OUTPUT
************************************************************************
* ERROR *
************************************************************************
{$cron->error}
************************************************************************
OUTPUT;
}
}
?>