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