]> git.mxchange.org Git - friendica.git/blob - include/poller.php
installation issues
[friendica.git] / include / poller.php
1 <?php
2 require_once("boot.php");
3
4 function poller_run($argv, $argc){
5         global $a, $db;
6
7         if(is_null($a)) {
8                 $a = new App;
9         }
10   
11         if(is_null($db)) {
12             @include(".htconfig.php");
13         require_once("dba.php");
14             $db = new dba($db_host, $db_user, $db_pass, $db_data);
15         unset($db_host, $db_user, $db_pass, $db_data);
16         };
17
18         require_once('session.php');
19         require_once('datetime.php');
20         require_once('simplepie/simplepie.inc');
21         require_once('include/items.php');
22         require_once('include/Contact.php');
23
24         $a->set_baseurl(get_config('system','url'));
25
26         logger('poller: start');
27         
28         // run queue delivery process in the background
29
30         proc_run('php',"include/queue.php");
31         
32         // once daily run expire in background
33
34         $d1 = get_config('system','last_expire_day');
35         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
36
37         if($d2 != intval($d1)) {
38                 set_config('system','last_expire_day',$d2);
39                 proc_run('php','include/expire.php');
40         }
41
42
43         // clear old cache
44         q("DELETE FROM `cache` WHERE `updated` < '%s'",
45                 dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
46
47         $manual_id  = 0;
48         $hub_update = false;
49         $force      = false;
50
51         if(($argc > 1) && ($argv[1] == 'force'))
52                 $force = true;
53
54         if(($argc > 1) && intval($argv[1])) {
55                 $manual_id = intval($argv[1]);
56                 $force     = true;
57         }
58
59         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
60
61         $d = datetime_convert();
62         call_hooks('cron', $d);
63
64         reload_plugins();
65
66         $contacts = q("SELECT `id` FROM `contact` 
67                 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
68                 $sql_extra 
69                 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()",
70                 intval(REL_FAN),
71                 intval(REL_BUD)
72         );
73
74         if(! count($contacts)) {
75                 return;
76         }
77
78         foreach($contacts as $c) {
79
80                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
81                         intval($c['id'])
82                 );
83
84                 if(! count($res))
85                         continue;
86
87                 foreach($res as $contact) {
88                         if($manual_id)
89                                 $contact['last-update'] = '0000-00-00 00:00:00';
90
91                         if($contact['priority'] || $contact['subhub']) {
92
93                                 $hub_update = true;
94                                 $update     = false;
95
96                                 $t = $contact['last-update'];
97
98                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
99                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
100                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
101                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'. 
102
103
104                                 if($contact['subhub']) {
105                                         $interval = get_config('system','pushpoll_frequency');
106                                         $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
107                                         $hub_update = false;
108         
109                                         if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
110                                                         $hub_update = true;
111                                 }
112
113                                 /**
114                                  * Based on $contact['priority'], should we poll this site now? Or later?
115                                  */                     
116
117                                 switch ($contact['priority']) {
118                                         case 5:
119                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
120                                                         $update = true;
121                                                 break;                                  
122                                         case 4:
123                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
124                                                         $update = true;
125                                                 break;
126                                         case 3:
127                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
128                                                         $update = true;
129                                                 break;
130                                         case 2:
131                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
132                                                         $update = true;
133                                                 break;
134                                         case 1:
135                                         default:
136                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
137                                                         $update = true;
138                                                 break;
139                                 }
140                                 if((! $update) && (! $force))
141                                         continue;
142                         }
143
144                         $importer_uid = $contact['uid'];
145                 
146                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
147                                 intval($importer_uid)
148                         );
149                         if(! count($r))
150                                 continue;
151
152                         $importer = $r[0];
153
154                         logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
155
156                         $last_update = (($contact['last-update'] === '0000-00-00 00:00:00') 
157                                 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
158                                 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
159                         );
160
161                         if($contact['network'] === 'dfrn') {
162
163                                 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
164
165                                 if(intval($contact['duplex']) && $contact['dfrn-id'])
166                                         $idtosend = '0:' . $orig_id;
167                                 if(intval($contact['duplex']) && $contact['issued-id'])
168                                         $idtosend = '1:' . $orig_id;            
169
170                                 $url = $contact['poll'] . '?dfrn_id=' . $idtosend 
171                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION 
172                                         . '&type=data&last_update=' . $last_update ;
173         
174                                 $xml = fetch_url($url);
175
176                                 logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
177
178
179                                 if(! $xml) {
180                                         logger("poller: $url appears to be dead - marking for death ");
181                                         // dead connection - might be a transient event, or this might
182                                         // mean the software was uninstalled or the domain expired. 
183                                         // Will keep trying for one month.
184                                         mark_for_death($contact);
185
186                                         // set the last-update so we don't keep polling
187
188                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
189                                                 dbesc(datetime_convert()),
190                                                 intval($contact['id'])
191                                         );
192
193                                         continue;
194                                 }
195
196                                 if(! strstr($xml,'<?xml')) {
197                                         logger('poller: response from ' . $url . ' did not contain XML.');
198                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
199                                                 dbesc(datetime_convert()),
200                                                 intval($contact['id'])
201                                         );
202                                         continue;
203                                 }
204
205
206                                 $res = simplexml_load_string($xml);
207         
208                                 if(intval($res->status) == 1) {
209                                         logger("poller: $url replied status 1 - marking for death ");
210
211                                         // we may not be friends anymore. Will keep trying for one month.
212                                         // set the last-update so we don't keep polling
213
214                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
215                                                 dbesc(datetime_convert()),
216                                                 intval($contact['id'])
217                                         );
218
219                                         mark_for_death($contact);
220                                 }
221                                 else {
222                                         if($contact['term-date'] != '0000-00-00 00:00:00') {
223                                                 logger("poller: $url back from the dead - removing mark for death");
224                                                 unmark_for_death($contact);
225                                         }
226                                 }
227
228                                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
229                                         continue;
230
231                                 $postvars = array();
232
233                                 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
234                                 $challenge    = hex2bin((string) $res->challenge);
235
236                                 $final_dfrn_id = '';
237
238                                 if(($contact['duplex']) && strlen($contact['prvkey'])) {
239                                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
240                                         openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
241                                 }
242                                 else {
243                                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
244                                         openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
245                                 }
246
247                                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
248
249                                 if(strpos($final_dfrn_id,':') == 1)
250                                         $final_dfrn_id = substr($final_dfrn_id,2);
251
252                                 if($final_dfrn_id != $orig_id) {
253         
254                                         // did not decode properly - cannot trust this site 
255                                         continue;
256                                 }
257
258                                 $postvars['dfrn_id'] = $idtosend;
259                                 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
260
261                                 $xml = post_url($contact['poll'],$postvars);
262                         }
263                         else {
264
265                                 // $contact['network'] !== 'dfrn'
266
267                                 $xml = fetch_url($contact['poll']);
268                         }
269
270                         logger('poller: received xml : ' . $xml, LOGGER_DATA);
271
272                         if(! strstr($xml,'<?xml')) {
273                                 logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
274                                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
275                                         dbesc(datetime_convert()),
276                                         intval($contact['id'])
277                                 );
278                                 continue;
279                         }
280
281                         consume_feed($xml,$importer,$contact,$hub,1);
282
283                         // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
284
285                         consume_feed($xml,$importer,$contact,$hub,1);
286
287
288                         if((strlen($hub)) && ($hub_update) 
289                                 && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
290                                 logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
291                                 $hubs = explode(',', $hub);
292                                 if(count($hubs)) {
293                                         foreach($hubs as $h) {
294                                                 $h = trim($h);
295                                                 if(! strlen($h))
296                                                         continue;
297                                                 subscribe_to_hub($h,$importer,$contact);
298                                         }
299                                 }
300                         }
301
302
303                         $updated = datetime_convert();
304
305                         $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
306                                 dbesc($updated),
307                                 dbesc($updated),
308                                 intval($contact['id'])
309                         );
310
311                         // loop - next contact
312                 }
313         }
314                 
315         return;
316 }
317
318 if (array_search(__file__,get_included_files())===0){
319   poller_run($argv,$argc);
320   killme();
321 }