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