]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Support for the new nodeinfo protocol that will replace statistics.json
[friendica.git] / include / poller.php
1 <?php
2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3         $directory = dirname($_SERVER["argv"][0]);
4
5         if (substr($directory, 0, 1) != "/")
6                 $directory = $_SERVER["PWD"]."/".$directory;
7
8         $directory = realpath($directory."/..");
9
10         chdir($directory);
11 }
12
13 require_once("boot.php");
14
15
16 function poller_run(&$argv, &$argc){
17         global $a, $db;
18
19         if(is_null($a)) {
20                 $a = new App;
21         }
22
23         if(is_null($db)) {
24             @include(".htconfig.php");
25         require_once("include/dba.php");
26             $db = new dba($db_host, $db_user, $db_pass, $db_data);
27         unset($db_host, $db_user, $db_pass, $db_data);
28         };
29
30
31         require_once('include/session.php');
32         require_once('include/datetime.php');
33         require_once('library/simplepie/simplepie.inc');
34         require_once('include/items.php');
35         require_once('include/Contact.php');
36         require_once('include/email.php');
37         require_once('include/socgraph.php');
38         require_once('include/pidfile.php');
39         require_once('mod/nodeinfo.php');
40
41         load_config('config');
42         load_config('system');
43
44         $maxsysload = intval(get_config('system','maxloadavg'));
45         if($maxsysload < 1)
46                 $maxsysload = 50;
47         if(function_exists('sys_getloadavg')) {
48                 $load = sys_getloadavg();
49                 if(intval($load[0]) > $maxsysload) {
50                         logger('system: load ' . $load[0] . ' too high. Poller deferred to next scheduled run.');
51                         return;
52                 }
53         }
54
55         $lockpath = get_lockpath();
56         if ($lockpath != '') {
57                 $pidfile = new pidfile($lockpath, 'poller');
58                 if($pidfile->is_already_running()) {
59                         logger("poller: Already running");
60                         if ($pidfile->running_time() > 9*60) {
61                                 $pidfile->kill();
62                                 logger("poller: killed stale process");
63                                 // Calling a new instance
64                                 proc_run('php','include/poller.php');
65                         }
66                         exit;
67                 }
68         }
69
70
71
72         $a->set_baseurl(get_config('system','url'));
73
74         load_hooks();
75
76         logger('poller: start');
77
78         // run queue delivery process in the background
79
80         proc_run('php',"include/queue.php");
81
82         // run diaspora photo queue process in the background
83
84         proc_run('php',"include/dsprphotoq.php");
85
86         // run the process to discover global contacts in the background
87
88         proc_run('php',"include/discover_poco.php");
89
90         // run the process to update locally stored global contacts in the background
91
92         proc_run('php',"include/discover_poco.php", "checkcontact");
93
94         // expire any expired accounts
95
96         q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0
97                 AND `account_expires_on` != '0000-00-00 00:00:00'
98                 AND `account_expires_on` < UTC_TIMESTAMP() ");
99
100         // delete user and contact records for recently removed accounts
101
102         $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
103         if ($r) {
104                 foreach($r as $user) {
105                         q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
106                         q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
107                 }
108         }
109
110         $abandon_days = intval(get_config('system','account_abandon_days'));
111         if($abandon_days < 1)
112                 $abandon_days = 0;
113
114         // Check OStatus conversations
115         // Check only conversations with mentions (for a longer time)
116         check_conversations(true);
117
118         // Check every conversation
119         check_conversations(false);
120
121         // update nodeinfo data
122         nodeinfo_cron();
123
124         // To-Do: Regenerate usage statistics
125         // q("ANALYZE TABLE `item`");
126
127         // once daily run birthday_updates and then expire in background
128
129         $d1 = get_config('system','last_expire_day');
130         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
131
132         if($d2 != intval($d1)) {
133
134                 update_contact_birthdays();
135
136                 update_suggestions();
137
138                 set_config('system','last_expire_day',$d2);
139                 proc_run('php','include/expire.php');
140         }
141
142         $last = get_config('system','cache_last_cleared');
143
144         if($last) {
145                 $next = $last + (3600); // Once per hour
146                 $clear_cache = ($next <= time());
147         } else
148                 $clear_cache = true;
149
150         if ($clear_cache) {
151                 // clear old cache
152                 Cache::clear();
153
154                 // clear old item cache files
155                 clear_cache();
156
157                 // clear cache for photos
158                 clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
159
160                 // clear smarty cache
161                 clear_cache($a->get_basepath()."/view/smarty3/compiled", $a->get_basepath()."/view/smarty3/compiled");
162
163                 // clear cache for image proxy
164                 if (!get_config("system", "proxy_disabled")) {
165                         clear_cache($a->get_basepath(), $a->get_basepath()."/proxy");
166
167                         $cachetime = get_config('system','proxy_cache_time');
168                         if (!$cachetime) $cachetime = PROXY_DEFAULT_TIME;
169
170                         q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
171                 }
172
173                 set_config('system','cache_last_cleared', time());
174         }
175
176         $manual_id  = 0;
177         $generation = 0;
178         $force      = false;
179         $restart    = false;
180
181         if(($argc > 1) && ($argv[1] == 'force'))
182                 $force = true;
183
184         if(($argc > 1) && ($argv[1] == 'restart')) {
185                 $restart = true;
186                 $generation = intval($argv[2]);
187                 if(! $generation)
188                         killme();
189         }
190
191         if(($argc > 1) && intval($argv[1])) {
192                 $manual_id = intval($argv[1]);
193                 $force     = true;
194         }
195
196         $interval = intval(get_config('system','poll_interval'));
197         if(! $interval)
198                 $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
199
200         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
201
202         reload_plugins();
203
204         $d = datetime_convert();
205
206         if(! $restart)
207                 proc_run('php','include/cronhooks.php');
208
209         // Only poll from those with suitable relationships,
210         // and which have a polling address and ignore Diaspora since
211         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
212
213         $abandon_sql = (($abandon_days)
214                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days))
215                 : ''
216         );
217
218         $contacts = q("SELECT `contact`.`id` FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
219                 WHERE `rel` IN (%d, %d) AND `poll` != '' AND `network` IN ('%s', '%s', '%s', '%s', '%s', '%s')
220                 $sql_extra
221                 AND NOT `self` AND NOT `contact`.`blocked` AND NOT `contact`.`readonly` AND NOT `contact`.`archive`
222                 AND NOT `user`.`account_expired` AND NOT `user`.`account_removed` $abandon_sql ORDER BY RAND()",
223                 intval(CONTACT_IS_SHARING),
224                 intval(CONTACT_IS_FRIEND),
225                 dbesc(NETWORK_DFRN),
226                 dbesc(NETWORK_ZOT),
227                 dbesc(NETWORK_OSTATUS),
228                 dbesc(NETWORK_FEED),
229                 dbesc(NETWORK_MAIL),
230                 dbesc(NETWORK_MAIL2)
231         );
232
233         if(! count($contacts)) {
234                 return;
235         }
236
237         foreach($contacts as $c) {
238
239                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
240                         intval($c['id'])
241                 );
242
243                 if((! $res) || (! count($res)))
244                         continue;
245
246                 foreach($res as $contact) {
247
248                         $xml = false;
249
250                         if($manual_id)
251                                 $contact['last-update'] = '0000-00-00 00:00:00';
252
253                         if(in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS)))
254                                 $contact['priority'] = 2;
255
256                         if($contact['subhub'] AND in_array($contact['network'], array(NETWORK_DFRN, NETWORK_ZOT, NETWORK_OSTATUS))) {
257                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
258                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
259                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
260                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
261
262                                 $poll_interval = get_config('system','pushpoll_frequency');
263                                 $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
264                         }
265
266                         if($contact['priority'] AND !$force) {
267
268                                 $update     = false;
269
270                                 $t = $contact['last-update'];
271
272                                 /**
273                                  * Based on $contact['priority'], should we poll this site now? Or later?
274                                  */
275
276                                 switch ($contact['priority']) {
277                                         case 5:
278                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
279                                                         $update = true;
280                                                 break;
281                                         case 4:
282                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
283                                                         $update = true;
284                                                 break;
285                                         case 3:
286                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
287                                                         $update = true;
288                                                 break;
289                                         case 2:
290                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
291                                                         $update = true;
292                                                 break;
293                                         case 1:
294                                         default:
295                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
296                                                         $update = true;
297                                                 break;
298                                 }
299                                 if(!$update)
300                                         continue;
301                         }
302
303                         logger("Polling ".$contact["network"]." ".$contact["id"]." ".$contact["nick"]." ".$contact["name"]);
304
305                         proc_run('php','include/onepoll.php',$contact['id']);
306
307                         if($interval)
308                                 @time_sleep_until(microtime(true) + (float) $interval);
309                 }
310         }
311
312         logger('poller: end');
313
314         return;
315 }
316
317 if (array_search(__file__,get_included_files())===0){
318   poller_run($_SERVER["argv"],$_SERVER["argc"]);
319   killme();
320 }