-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCronable.php
319 lines (293 loc) · 11.1 KB
/
Cronable.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
316
317
318
319
<?php
/**
* Cronable v1.2.3 (last modified: 2023.02.03).
* @link https://github.com/Maikuolan/Cronable
*
* Description: Cronable is a simple script that allows auto-updating CIDRAM
* and phpMussel via cronjobs.
*
* CRONABLE COPYRIGHT 2017 and beyond by Caleb Mazalevskis (Maikuolan).
*
* License: MIT License
* @see LICENSE.txt
*/
namespace Maikuolan\Cronable;
class Cronable
{
/**
* @var string Output we'll send upon completing tasks.
*/
public $Output = '';
/**
* @var bool Determines whether to display debugging information when relevant.
*/
public $Debugging = false;
/**
* @var string Cronable user agent.
*/
private $ScriptUA = 'Cronable v1.2.3';
/**
* @var int Default timeout.
*/
private $Timeout = 12;
/**
* @var array Will be populated by tasks.
*/
private $Tasks = [];
/**
* Create task method.
*
* @param string $Package
* @param string $Username
* @param string $Password
* @param string $Location
* @param string $Mode What to update (specify 'Signatures' to update just signatures; anything else to update everything).
* @return void
*/
public function createTask($Package, $Username, $Password, $Location, $Mode = 'All')
{
$this->Tasks[] = [
'Package' => $Package,
'Username' => $Username,
'Password' => $Password,
'Location' => $Location,
'Mode' => $Mode
];
}
/**
* Execute all tasks.
*
* @return void
*/
public function execute()
{
$this->Output .= $this->ScriptUA . "\nTime: " . date('r') . "\n\n===\n";
$Tasks = $this->Tasks;
foreach ($Tasks as $Task) {
$Identifier = empty($Task['Location']) ? '[Unknown]' : $this->buildIdentifier($Task['Package'], $Task['Location']);
$Results = $this->update($Task);
if ($Results === true) {
$this->Output .= 'Everything already up-to-date at ' . $Identifier . ". :-)\n\n";
} elseif ($Results === false) {
$this->Output .= 'An error occurred while attempting to update at ' . $Identifier . ". :-(\n\n";
if ($this->Debugging) {
$this->cronableError($Identifier, 'execute()', $Task, 'Results === `false`');
}
} else {
$this->Output .= 'Status for ' . $Identifier . " is as follows:\n" . $Results . "\n\n";
}
}
$this->Output .= "===\n\nTime: " . date('r') . "\n\n\n";
}
/**
* Update locally.
*
* @param string $Package
* @param string $Username
* @param string $Password
* @param string $Location
* @param string $UpdateAll What to update (specify 'Signatures' to update just signatures; anything else to update everything).
* @return void
*/
public function localUpdate($Package, $Username, $Password, $Location, $UpdateAll = 'All')
{
/** Let's fake it all. */
$_POST['CronMode'] = $UpdateAll;
$_POST['username'] = $Username;
$_POST['password'] = $Password;
$_SERVER['HTTP_USER_AGENT'] = $this->ScriptUA;
if ($PackageKnown = ($Package === 'CIDRAM' || $Package === 'phpMussel')) {
if ($Package === 'CIDRAM') {
$_SERVER['QUERY_STRING'] = 'cidram-page=updates';
$_POST['cidram-form-target'] = 'updates';
} elseif ($Package === 'phpMussel') {
$_SERVER['QUERY_STRING'] = 'phpmussel-page=updates';
$_POST['phpmussel-form-target'] = 'updates';
}
}
if (empty($_SERVER['REMOTE_ADDR'])) {
$_SERVER['REMOTE_ADDR'] = '::1';
}
$this->Output .= $this->ScriptUA . "\nTime: " . date('r') . "\n\n===\n";
$Identifier = empty($Location) ? '[Unknown]' : '[' . $Package . '@' . $Location . ']';
if (is_readable($Location) && $PackageKnown) {
/** To prevent the HTML output that we'd normally see when accessing everything normally. */
ob_start();
/** Let's call the package. */
require $Location;
/** We're done here. Reenable output. */
ob_end_clean();
/** v3 fix. */
if (!isset($Results) && isset($GLOBALS['Results'])) {
$Results = $GLOBALS['Results'];
}
if (!isset($Results['state_msg'])) {
$this->Output .= 'An error occurred while attempting to update at ' . $Identifier . ". :-(\n\n";
if ($this->Debugging) {
$this->cronableError($Identifier, 'localUpdate()', $Location);
}
} elseif ($Results['state_msg'] === '') {
$this->Output .= 'Everything already up-to-date at ' . $Identifier . ". :-)\n\n";
} else {
$this->Output .= 'Status for ' . $Identifier . " is as follows:\n" . $Results['state_msg'] . "\n\n";
}
} else {
$this->Output .= 'An error occurred while attempting to update at ' . $Identifier . ". :-(\n\n";
if ($this->Debugging) {
$this->cronableError($Identifier, 'localUpdate()', $Location, 'Package not known or location unreadable');
}
}
$this->Output .= "===\n\nTime: " . date('r') . "\n\n\n";
}
/**
* Generate error logs when debugging is enabled.
*
* @param string $Identifier
* @param string $Method
* @param string $Task
* @param string $Results
* @return void
*/
private function cronableError($Identifier, $Method, $Task, $Results = 'Results are empty')
{
$Data = sprintf("Debugging (%1\$s):\n- Method: `%2\$s`.\n- Task: `%3\$s`.\n- %4\$s.\n\n", $Identifier, $Method, $Task, $Results);
$File = __DIR__ . '/error.log';
if ($Handle = fopen($File, 'a')) {
fwrite($Handle, $Data);
fclose($Handle);
}
$this->Output .= $Data;
}
/**
* Used to send cURL requests.
*
* @param string $URI The resource to request.
* @param array $Params An optional associative array of key-value
* pairs to to send along with the request.
* @param string $Timeout An optional timeout limit (defaults to 12
* seconds).
* @return string The results of the request.
*/
private function request($URI, $Params = '', $Timeout = '')
{
if (!$Timeout) {
$Timeout = $this->Timeout;
}
/** Initialise the cURL session. */
$Request = curl_init($URI);
$LCURI = strtolower($URI);
$SSL = (substr($LCURI, 0, 6) === 'https:');
curl_setopt($Request, CURLOPT_FRESH_CONNECT, true);
curl_setopt($Request, CURLOPT_HEADER, false);
if (empty($Params)) {
curl_setopt($Request, CURLOPT_POST, false);
} else {
curl_setopt($Request, CURLOPT_POST, true);
curl_setopt($Request, CURLOPT_POSTFIELDS, $Params);
}
if ($SSL) {
curl_setopt($Request, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($Request, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($Request, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($Request, CURLOPT_MAXREDIRS, 1);
curl_setopt($Request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($Request, CURLOPT_TIMEOUT, $Timeout);
curl_setopt($Request, CURLOPT_USERAGENT, $this->ScriptUA);
/** Execute and get the response. */
$Response = curl_exec($Request);
/** Close the cURL session. */
curl_close($Request);
/** Return the results of the request. */
return $Response;
}
/**
* Update method.
*
* @param array $Arr Instructions for the update method (e.g., package,
* location, etc).
* @return bool|string False when update fails; True when already
* up-to-date; Actual update status message when
* update succeeds.
*/
private function update($Arr)
{
if (!empty($Arr['Package'])) {
if ($Arr['Package'] === 'CIDRAM') {
$Package = 'CIDRAM';
$Query = '?cidram-page=updates';
$FormTarget = 'cidram-form-target';
} elseif ($Arr['Package'] === 'phpMussel') {
$Package = 'phpMussel';
$Query .= '?phpmussel-page=updates';
$FormTarget = 'phpmussel-form-target';
}
}
/** Guard. */
if (empty($Package) || empty($Arr['Username']) || empty($Arr['Password']) || empty($Arr['Location'])) {
return false;
}
$Location = $Arr['Location'] . $Query;
$Arr = [
'CronMode' => empty($Arr['Mode']) ? 'All' : $Arr['Mode'],
$FormTarget => 'updates',
'do' => 'get-list',
'username' => $Arr['Username'],
'password' => $Arr['Password'],
];
if (($Request = $this->request($Location, http_build_query($Arr))) === '') {
return true;
}
if (substr($Request, 0, 1) !== '{' || substr($Request, -1) !== '}') {
return false;
}
$Request = json_decode($Request, true, 3);
if (isset($Request['state_msg']) && $Request['state_msg'] !== '') {
return $Request['state_msg'];
}
if (
!isset($Request['outdated_signature_files'], $Request['outdated']) ||
!is_array($Request['outdated_signature_files']) ||
!is_array($Request['outdated']) ||
(
($OutdatedSigFileCount = count($Request['outdated_signature_files'])) === 0 &&
($OutdatedCount = count($Request['outdated'])) === 0
)
) {
return false;
}
if ($Arr['CronMode'] === 'Signatures') {
if ($OutdatedSigFileCount === 0) {
return "All signature files are already all up-to-date.\nOther outdated components were detected, but not updated at this time due to Cronable settings.";
}
$Arr['ID'] = $Request['outdated_signature_files'];
} else {
if ($OutdatedCount === 0) {
return false;
}
$Arr['ID'] = $Request['outdated'];
}
$Arr['do'] = 'update-component';
$Request = $this->request($Location, http_build_query($Arr));
if (substr($Request, 0, 1) !== '{' || substr($Request, -1) !== '}') {
return false;
}
$Request = json_decode($Request, true, 3);
if (isset($Request['state_msg']) && $Request['state_msg'] !== '') {
return $Request['state_msg'];
}
return false;
}
/**
* Build identifier.
*
* @param string $Package
* @param string $Location
* @return string
*/
private function buildIdentifier($Package, $Location)
{
$Location = preg_replace('~^(?:https?\:\/\/)?(?:www\d{0,3}\.)?~i', '', $Location);
return '[' . $Package . '@' . $Location . ']';
}
}