forked from Pistolebob/spotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.php
executable file
·341 lines (298 loc) · 11.3 KB
/
retrieve.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
error_reporting(2147483647);
/*
* If we are run from another directory, try to change the current
* working directory to a directory the script is in
*/
if (@!file_exists(getcwd() . '/' . basename($argv[0]))) {
chdir(dirname(__FILE__));
} # if
require_once "lib/SpotTranslation.php";
require_once "lib/SpotClassAutoload.php";
try {
require_once "settings.php";
}
catch(InvalidOwnSettingsSettingException $x) {
echo "There is an error in your ownsettings.php" . PHP_EOL . PHP_EOL;
echo $x->getMessage() . PHP_EOL;
die();
} # InvalidOwnSettingsSetting
require_once "lib/SpotTiming.php";
require_once "lib/exceptions/ParseSpotXmlException.php";
require_once "lib/exceptions/NntpException.php";
/*
* disable timing, all queries which are ran by retrieve this would make it use
* large amounts of memory
*/
SpotTiming::disable();
# Initialize commandline arguments
SpotCommandline::initialize(array('force', 'debug', 'retro'), array('force' => false, 'debug' => false, 'retro' => false));
# Initialize translation to english
SpotTranslation::initialize('en_US');
# in safe mode, max execution time cannot be set, warn the user
if (ini_get('safe_mode') ) {
echo "WARNING: PHP safemode is enabled, maximum execution cannot be reset! Turn off safemode if this causes problems" . PHP_EOL . PHP_EOL;
} # if
try {
$db = new SpotDb($settings['db']);
$db->connect();
}
catch(DatabaseConnectionException $x) {
die("Unable to connect to database: " . $x->getMessage() . PHP_EOL);
} # catch
# Creer het settings object
$settings = SpotSettings::singleton($db, $settings);
# Controleer dat we niet een schema upgrade verwachten
if (!$settings->schemaValid()) {
die("Database schema has been changed, please run upgrade-db.php" . PHP_EOL);
} # if
# Controleer eerst of de settings versie nog wel geldig zijn
if (!$settings->settingsValid()) {
die("Global settings have been changed, please run upgrade-db.php" . PHP_EOL);
} # if
$req = new SpotReq();
$req->initialize($settings);
# We willen alleen uitgevoerd worden door een user die dat mag als
# we via de browser aangeroepen worden. Via console halen we altijd
# het admin-account op
$spotUserSystem = new SpotUserSystem($db, $settings);
if (!SpotCommandline::isCommandline()) {
# Vraag de API key op die de gebruiker opgegeven heeft
$apiKey = $req->getDef('apikey', '');
$userSession = $spotUserSystem->verifyApi($apiKey);
if (($userSession == false) || (!$userSession['security']->allowed(SpotSecurity::spotsec_retrieve_spots, ''))) {
die("Access denied");
} # if
# Add the user's ip addres, we need it for sending notifications
$userSession['session'] = array('ipaddr' => '');
} else {
$userSession['user'] = $db->getUser(SPOTWEB_ADMIN_USERID);
$userSession['security'] = new SpotSecurity($db, $settings, $userSession['user'], '');
$userSession['session'] = array('ipaddr' => '');
} # if
if ($req->getDef('output', '') == 'xml') {
echo "<xml>";
} # if
# We vragen de nntp_hdr settings alvast op
$settings_nntp_hdr = $settings->get('nntp_hdr');
if (empty($settings_nntp_hdr['host'])) {
die("Unable to continue: You did not setup any newsserver yet." . PHP_EOL);
} # if
## Als we forceren om de "already running" check te bypassen, doe dat dan
if (SpotCommandline::get('force')) {
$db->setRetrieverRunning($settings_nntp_hdr['host'], false);
} # if
## Moeten we debugloggen? Kan alleen als geen --force opgegeven wordt
$debugLog = SpotCommandline::get('debug');
## RETRO MODE! Hiermee kunnen we de fullspots, fullcomments en/of cache achteraf ophalen
$retroMode = SpotCommandline::get('retro');
/*
* Retention cleanup. Basically when we ask for Spotweb to only
* keep spots for 'xx' days (eg: 30 days), we either have to delete
* everyting older than 'xx' days, or delete all 'full' resources
* older than the specified time period.
*
* The full resources are everything beyond the bare minimum to
* display the spots, so we delete nzb's, images, comments, etc.
*/
try {
if ($settings->get('retention') > 0 && !$retroMode) {
if ($settings->get('retentiontype') == 'everything') {
$db->deleteSpotsRetention($settings->get('retention'));
} elseif ($settings->get('retentiontype') == 'fullonly') {
$db->expireCache($settings->get('retention'));
$db->expireCommentsFull($settings->get('retention'));
$db->expireSpotsFull($settings->get('retention'));
} else {
throw new NotImplementedException("Unknown retentiontype specified");
}
} # if
} catch(Exception $x) {
echo PHP_EOL . PHP_EOL;
echo 'SpotWeb v' . SPOTWEB_VERSION . ' on PHP v' . PHP_VERSION . ' crashed' . PHP_EOL . PHP_EOL;
echo "Fatal error occured while cleaning up messages due to retention:" . PHP_EOL;
echo " " . $x->getMessage() . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo $x->getTraceAsString();
echo PHP_EOL . PHP_EOL;
die();
} # catch
$newSpotCount = 0;
$newCommentCount = 0;
$newReportCount = 0;
$retriever = null;
## Spots
try {
/*
* Actually retrieve spots from the server
*/
$retriever = new SpotRetriever_Spots($settings_nntp_hdr,
$db,
$settings,
$req->getDef('output', ''),
$debugLog,
$retroMode);
$newSpotCount = $retriever->perform();
## Creating filter counts
if ($newSpotCount > 0) {
$spotsOverview = new SpotsOverview($db, $settings);
echo 'Calculating how many spots are new';
$notifyNewArray = $spotsOverview->cacheNewSpotCount();
echo ', done.' . PHP_EOL;
} # if
/*
* Should we retrieve comments?
*/
if ($settings->get('retrieve_comments')) {
$retriever = new SpotRetriever_Comments($settings_nntp_hdr,
$db,
$settings,
$req->getDef('output', ''),
$debugLog,
$retroMode);
$newCommentCount = $retriever->perform();
} # if
/*
* Retrieval of reports
*/
if ($settings->get('retrieve_reports') && !$retroMode) {
$retriever = new SpotRetriever_Reports($settings_nntp_hdr,
$db,
$settings,
$req->getDef('output', ''),
$debugLog);
$newReportCount = $retriever->perform();
} # if
/*
* SpotStateList cleanup
*/
$db->cleanSpotStateList();
if (!$retroMode) {
$db->expireCache(30);
} # if
}
catch(RetrieverRunningException $x) {
echo PHP_EOL . PHP_EOL;
die("retriever.php is already running, pass '--force' to ignore this warning." . PHP_EOL);
}
catch(NntpException $x) {
echo PHP_EOL . PHP_EOL;
echo 'SpotWeb v' . SPOTWEB_VERSION . ' on PHP v' . PHP_VERSION . ' crashed' . PHP_EOL . PHP_EOL;
echo "Fatal error occured while connecting to the newsserver:" . PHP_EOL;
echo " (" . $x->getCode() . ") " . $x->getMessage() . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo $x->getTraceAsString();
echo PHP_EOL . PHP_EOL;
if (!empty($retriever)){
echo "Updating retrieve status in the database" . PHP_EOL . PHP_EOL;
$retriever->quit();
}
die();
}
catch(Exception $x) {
echo PHP_EOL . PHP_EOL;
echo 'SpotWeb v' . SPOTWEB_VERSION . ' on PHP v' . PHP_VERSION . ' crashed' . PHP_EOL . PHP_EOL;
echo "Fatal error occured retrieving reports:" . PHP_EOL;
echo " " . $x->getMessage() . PHP_EOL . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo $x->getTraceAsString();
echo PHP_EOL . PHP_EOL;
die();
} # catch
## External blacklist
$settings_external_blacklist = $settings->get('external_blacklist');
if ($settings_external_blacklist) {
try {
$spotsOverview = new SpotsOverview($db, $settings);
# haal de blacklist op
list($http_code, $blacklist) = $spotsOverview->getFromWeb($settings->get('blacklist_url'), false, 30*60);
if ($http_code == 304) {
echo "Blacklist not modified, no need to update" . PHP_EOL;
} elseif (strpos($blacklist['content'],">")) {
echo "Error, blacklist does not have expected layout!" . PHP_EOL;
} else {
# update de blacklist
$blacklistarray = explode(chr(10),$blacklist['content']);
# Perform a very small snaity check on the blacklist
if ((count($blacklistarray) > 5) && (strlen($blacklistarray[0]) < 10)) {
$updateblacklist = $db->updateExternallist($blacklistarray, SpotDb::spotterlist_Black);
echo "Finished updating blacklist. Added " . $updateblacklist['added'] . ", removed " . $updateblacklist['removed'] . ", skipped " . $updateblacklist['skipped'] . " of " . count($blacklistarray) . " lines." . PHP_EOL;
} else {
echo "Blacklist is probably corrupt, skipping" . PHP_EOL;
} # else
}
} catch(Exception $x) {
echo "Fatal error occured while updating blacklist:" . PHP_EOL;
echo " " . $x->getMessage() . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo $x->getTraceAsString();
echo PHP_EOL . PHP_EOL;
}
} # if
## External whitelist
$settings_external_whitelist = $settings->get('external_whitelist');
if ($settings_external_whitelist) {
try {
$spotsOverview = new SpotsOverview($db, $settings);
# haal de whitelist op
list($http_code, $whitelist) = $spotsOverview->getFromWeb($settings->get('whitelist_url'), false, 30*60);
if ($http_code == 304) {
echo "Whitelist not modified, no need to update" . PHP_EOL;
} elseif (strpos($whitelist['content'],">")) {
echo "Error, whitelist does not have expected layout!" . PHP_EOL;
} else {
# update de whitelist
$whitelistarray = explode(chr(10),$whitelist['content']);
# Perform a very small snaity check on the whitelist
if ((count($whitelistarray) > 5) && (strlen($whitelistarray[0]) < 10)) {
$updatewhitelist = $db->updateExternallist($whitelistarray, SpotDb::spotterlist_White);
echo "Finished updating whitelist. Added " . $updatewhitelist['added'] . ", removed " . $updatewhitelist['removed'] . ", skipped " . $updatewhitelist['skipped'] . " of " . count($whitelistarray) . " lines." . PHP_EOL;
} else {
echo "Whitelist is probably corrupt, skipping" . PHP_EOL;
} # else
}
} catch(Exception $x) {
echo "Fatal error occured while updating whitelist:" . PHP_EOL;
echo " " . $x->getMessage() . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo $x->getTraceAsString();
echo PHP_EOL . PHP_EOL;
}
} # if
## Statistics
if ($settings->get('prepare_statistics') && $newSpotCount > 0) {
if (extension_loaded('gd') || extension_loaded('gd2')) {
$spotsOverview = new SpotsOverview($db, $settings);
$spotImage = new SpotImage($db);
$spotsOverview->setActiveRetriever(true);
echo "Starting to create statistics " . PHP_EOL;
foreach ($spotImage->getValidStatisticsLimits() as $limitValue => $limitName) {
# Reset timelimit
set_time_limit(60);
foreach($settings->get('system_languages') as $language => $name) {
foreach ($spotImage->getValidStatisticsGraphs() as $graphValue => $graphName) {
$spotsOverview->getStatisticsImage($graphValue, $limitValue, $settings_nntp_hdr, $language);
} # foreach graph
} # foreach language
echo "Finished creating statistics " . $limitName . PHP_EOL;
} # foreach limit
echo PHP_EOL;
} else {
echo "GD extension not loaded, not creating statistics" . PHP_EOL;
} # else
} # if
# Verstuur notificaties
$spotsNotifications = new SpotNotifications($db, $settings, $userSession);
if (!empty($notifyNewArray)) {
foreach($notifyNewArray as $userId => $newSpotInfo) {
foreach($newSpotInfo as $filterInfo) {
if (($filterInfo['newcount'] > 0) && ($filterInfo['enablenotify'])) {
$spotsNotifications->sendNewSpotsForFilter($userId, $filterInfo['title'], $filterInfo['newcount']);
} # if
} # foreach
} # foreach
} # if
$spotsNotifications->sendRetrieverFinished($newSpotCount, $newCommentCount, $newReportCount);
if ($req->getDef('output', '') == 'xml') {
echo "</xml>";
} # if