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