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