]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge remote-tracking branch 'upstream/master'
[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_config('system','lockpath');
45         if ($lockpath != '') {
46                 $pidfile = new pidfile($lockpath, 'poller.lck');
47                 if($pidfile->is_already_running()) {
48                         logger("poller: Already running");
49                         exit;
50                 }
51         }
52
53
54
55         $a->set_baseurl(get_config('system','url'));
56
57         load_hooks();
58
59         logger('poller: start');
60         
61         // run queue delivery process in the background
62
63         proc_run('php',"include/queue.php");
64         
65         // run diaspora photo queue process in the background
66
67         proc_run('php',"include/dsprphotoq.php");
68         
69         // expire any expired accounts
70
71         q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0 
72                 AND `account_expires_on` != '0000-00-00 00:00:00' 
73                 AND `account_expires_on` < UTC_TIMESTAMP() ");
74         
75         // delete user and contact records for recently removed accounts
76
77         $r = q("SELECT * FROM `user` WHERE `account_removed` = 1 AND `account_expires_on` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
78         if ($r) {
79                 foreach($r as $user) {
80                         q("DELETE FROM `contact` WHERE `uid` = %d", intval($user['uid']));
81                         q("DELETE FROM `user` WHERE `uid` = %d", intval($user['uid']));
82                 }
83         }
84   
85         $abandon_days = intval(get_config('system','account_abandon_days'));
86         if($abandon_days < 1)
87                 $abandon_days = 0;
88
89         
90
91         // once daily run birthday_updates and then expire in background
92
93         $d1 = get_config('system','last_expire_day');
94         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
95
96         if($d2 != intval($d1)) {
97
98                 update_contact_birthdays();
99
100                 update_suggestions();
101
102                 set_config('system','last_expire_day',$d2);
103                 proc_run('php','include/expire.php');
104         }
105
106         $last = get_config('system','cache_last_cleared');
107
108         if($last) {
109                 $next = $last + (3600); // Once per hour
110                 $clear_cache = ($next <= time());
111         } else
112                 $clear_cache = true;
113
114         if ($clear_cache) {
115                 // clear old cache
116                 Cache::clear();
117
118                 // clear old item cache files
119                 clear_cache();
120
121                 // clear cache for photos
122                 clear_cache($a->get_basepath(), $a->get_basepath()."/photo");
123
124                 set_config('system','cache_last_cleared', time());
125         }
126
127         $manual_id  = 0;
128         $generation = 0;
129         $hub_update = false;
130         $force      = false;
131         $restart    = false;
132
133         if(($argc > 1) && ($argv[1] == 'force'))
134                 $force = true;
135
136         if(($argc > 1) && ($argv[1] == 'restart')) {
137                 $restart = true;
138                 $generation = intval($argv[2]);
139                 if(! $generation)
140                         killme();
141         }
142
143         if(($argc > 1) && intval($argv[1])) {
144                 $manual_id = intval($argv[1]);
145                 $force     = true;
146         }
147
148         $interval = intval(get_config('system','poll_interval'));
149         if(! $interval) 
150                 $interval = ((get_config('system','delivery_interval') === false) ? 3 : intval(get_config('system','delivery_interval')));
151
152         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
153
154         reload_plugins();
155
156         $d = datetime_convert();
157
158         if(! $restart)
159                 proc_run('php','include/cronhooks.php');
160
161         // Only poll from those with suitable relationships,
162         // and which have a polling address and ignore Diaspora since 
163         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
164
165         $abandon_sql = (($abandon_days) 
166                 ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days)) 
167                 : '' 
168         );
169
170         $contacts = q("SELECT `contact`.`id` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
171                 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
172                 AND NOT `network` IN ( '%s', '%s' )
173                 $sql_extra 
174                 AND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0 
175                 AND `contact`.`archive` = 0 
176                 AND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 $abandon_sql ORDER BY RAND()",
177                 intval(CONTACT_IS_SHARING),
178                 intval(CONTACT_IS_FRIEND),
179                 dbesc(NETWORK_DIASPORA),
180                 dbesc(NETWORK_FACEBOOK)
181         );
182
183         if(! count($contacts)) {
184                 return;
185         }
186
187         foreach($contacts as $c) {
188
189                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
190                         intval($c['id'])
191                 );
192
193                 if((! $res) || (! count($res)))
194                         continue;
195
196                 foreach($res as $contact) {
197
198                         $xml = false;
199
200                         if($manual_id)
201                                 $contact['last-update'] = '0000-00-00 00:00:00';
202
203                         if($contact['network'] === NETWORK_DFRN)
204                                 $contact['priority'] = 2;
205
206                         if(!get_config('system','ostatus_use_priority') and ($contact['network'] === NETWORK_OSTATUS))
207                                 $contact['priority'] = 2;
208
209                         if($contact['priority'] || $contact['subhub']) {
210
211                                 $hub_update = true;
212                                 $update     = false;
213
214                                 $t = $contact['last-update'];
215
216                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
217                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
218                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
219                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'. 
220
221
222                                 if($contact['subhub']) {
223                                         $poll_interval = get_config('system','pushpoll_frequency');
224                                         $contact['priority'] = (($poll_interval !== false) ? intval($poll_interval) : 3);
225                                         $hub_update = false;
226         
227                                         if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
228                                                         $hub_update = true;
229                                 }
230                                 else
231                                         $hub_update = false;
232
233                                 /**
234                                  * Based on $contact['priority'], should we poll this site now? Or later?
235                                  */                     
236
237                                 switch ($contact['priority']) {
238                                         case 5:
239                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
240                                                         $update = true;
241                                                 break;                                  
242                                         case 4:
243                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
244                                                         $update = true;
245                                                 break;
246                                         case 3:
247                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
248                                                         $update = true;
249                                                 break;
250                                         case 2:
251                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
252                                                         $update = true;
253                                                 break;
254                                         case 1:
255                                         default:
256                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
257                                                         $update = true;
258                                                 break;
259                                 }
260                                 if((! $update) && (! $force))
261                                         continue;
262                         }
263
264                         proc_run('php','include/onepoll.php',$contact['id']);
265                         if($interval)
266                                 @time_sleep_until(microtime(true) + (float) $interval);
267                 }
268         }
269
270         return;
271 }
272
273 if (array_search(__file__,get_included_files())===0){
274   poller_run($argv,$argc);
275   killme();
276 }