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