]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge pull request #163 from fabrixxm/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
28         load_config('config');
29         load_config('system');
30
31         $a->set_baseurl(get_config('system','url'));
32
33         load_hooks();
34
35         logger('poller: start');
36         
37         // run queue delivery process in the background
38
39         proc_run('php',"include/queue.php");
40         
41         // once daily run expire in background
42
43         $d1 = get_config('system','last_expire_day');
44         $d2 = intval(datetime_convert('UTC','UTC','now','d'));
45
46         if($d2 != intval($d1)) {
47                 set_config('system','last_expire_day',$d2);
48                 proc_run('php','include/expire.php');
49         }
50
51         // clear old cache
52         q("DELETE FROM `cache` WHERE `updated` < '%s'",
53                 dbesc(datetime_convert('UTC','UTC',"now - 30 days")));
54
55         $manual_id  = 0;
56         $generation = 0;
57         $hub_update = false;
58         $force      = false;
59         $restart    = false;
60
61         if(($argc > 1) && ($argv[1] == 'force'))
62                 $force = true;
63
64         if(($argc > 1) && ($argv[1] == 'restart')) {
65                 $restart = true;
66                 $generation = intval($argv[2]);
67                 if(! $generation)
68                         killme();               
69         }
70
71         if(($argc > 1) && intval($argv[1])) {
72                 $manual_id = intval($argv[1]);
73                 $force     = true;
74         }
75
76         $sql_extra = (($manual_id) ? " AND `id` = $manual_id " : "");
77
78         reload_plugins();
79
80         $d = datetime_convert();
81
82         if(! $restart)
83                 proc_run('php','include/cronhooks.php');
84
85         // Only poll from those with suitable relationships,
86         // and which have a polling address and ignore Diaspora since 
87         // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
88
89         $contacts = q("SELECT `id` FROM `contact` 
90                 WHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''
91                 AND `network` != '%s'
92                 $sql_extra 
93                 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()",
94                 intval(CONTACT_IS_SHARING),
95                 intval(CONTACT_IS_FRIEND),
96                 dbesc(NETWORK_DIASPORA)
97         );
98
99         if(! count($contacts)) {
100                 return;
101         }
102
103         foreach($contacts as $c) {
104
105                 $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
106                         intval($c['id'])
107                 );
108
109                 if((! $res) || (! count($res)))
110                         continue;
111
112                 foreach($res as $contact) {
113
114                         $xml = false;
115
116                         if($manual_id)
117                                 $contact['last-update'] = '0000-00-00 00:00:00';
118
119                         if($contact['priority'] || $contact['subhub']) {
120
121                                 $hub_update = true;
122                                 $update     = false;
123
124                                 $t = $contact['last-update'];
125
126                                 // We should be getting everything via a hub. But just to be sure, let's check once a day.
127                                 // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
128                                 // This also lets us update our subscription to the hub, and add or replace hubs in case it
129                                 // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'. 
130
131
132                                 if($contact['subhub']) {
133                                         $interval = get_config('system','pushpoll_frequency');
134                                         $contact['priority'] = (($interval !== false) ? intval($interval) : 3);
135                                         $hub_update = false;
136         
137                                         if((datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day")) || $force)
138                                                         $hub_update = true;
139                                 }
140
141                                 /**
142                                  * Based on $contact['priority'], should we poll this site now? Or later?
143                                  */                     
144
145                                 switch ($contact['priority']) {
146                                         case 5:
147                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
148                                                         $update = true;
149                                                 break;                                  
150                                         case 4:
151                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
152                                                         $update = true;
153                                                 break;
154                                         case 3:
155                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
156                                                         $update = true;
157                                                 break;
158                                         case 2:
159                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
160                                                         $update = true;
161                                                 break;
162                                         case 1:
163                                         default:
164                                                 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
165                                                         $update = true;
166                                                 break;
167                                 }
168                                 if((! $update) && (! $force))
169                                         continue;
170                         }
171
172                         // Check to see if we are running out of memory - if so spawn a new process and kill this one
173
174                         $avail_memory = return_bytes(ini_get('memory_limit'));
175                         $memused = memory_get_peak_usage(true);
176                         if(intval($avail_memory)) {
177                                 if(($memused / $avail_memory) > 0.95) {
178                                         if($generation + 1 > 10) {
179                                                 logger('poller: maximum number of spawns exceeded. Terminating.');
180                                                 killme();
181                                         }
182                                         logger('poller: memory exceeded. ' . $memused . ' bytes used. Spawning new poll.');
183                                         proc_run('php', 'include/poller.php', 'restart', (string) $generation + 1);
184                                         killme();
185                                 }
186                         }
187
188                         $importer_uid = $contact['uid'];
189                 
190                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
191                                 intval($importer_uid)
192                         );
193                         if(! count($r))
194                                 continue;
195
196                         $importer = $r[0];
197
198                         logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
199
200                         $last_update = (($contact['last-update'] === '0000-00-00 00:00:00') 
201                                 ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
202                                 : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
203                         );
204
205                         if($contact['network'] === NETWORK_DFRN) {
206
207                                 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
208
209                                 if(intval($contact['duplex']) && $contact['dfrn-id'])
210                                         $idtosend = '0:' . $orig_id;
211                                 if(intval($contact['duplex']) && $contact['issued-id'])
212                                         $idtosend = '1:' . $orig_id;
213
214                                 // they have permission to write to us. We already filtered this in the contact query.
215                                 $perm = 'rw';
216
217                                 $url = $contact['poll'] . '?dfrn_id=' . $idtosend 
218                                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION 
219                                         . '&type=data&last_update=' . $last_update 
220                                         . '&perm=' . $perm ;
221         
222                                 $handshake_xml = fetch_url($url);
223
224                                 logger('poller: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
225
226
227                                 if(! $handshake_xml) {
228                                         logger("poller: $url appears to be dead - marking for death ");
229                                         // dead connection - might be a transient event, or this might
230                                         // mean the software was uninstalled or the domain expired. 
231                                         // Will keep trying for one month.
232                                         mark_for_death($contact);
233
234                                         // set the last-update so we don't keep polling
235
236                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
237                                                 dbesc(datetime_convert()),
238                                                 intval($contact['id'])
239                                         );
240
241                                         continue;
242                                 }
243
244                                 if(! strstr($handshake_xml,'<?xml')) {
245                                         logger('poller: response from ' . $url . ' did not contain XML.');
246                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
247                                                 dbesc(datetime_convert()),
248                                                 intval($contact['id'])
249                                         );
250                                         continue;
251                                 }
252
253
254                                 $res = parse_xml_string($handshake_xml);
255         
256                                 if(intval($res->status) == 1) {
257                                         logger("poller: $url replied status 1 - marking for death ");
258
259                                         // we may not be friends anymore. Will keep trying for one month.
260                                         // set the last-update so we don't keep polling
261
262                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
263                                                 dbesc(datetime_convert()),
264                                                 intval($contact['id'])
265                                         );
266
267                                         mark_for_death($contact);
268                                 }
269                                 else {
270                                         if($contact['term-date'] != '0000-00-00 00:00:00') {
271                                                 logger("poller: $url back from the dead - removing mark for death");
272                                                 unmark_for_death($contact);
273                                         }
274                                 }
275
276                                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
277                                         continue;
278
279                                 $postvars = array();
280
281                                 $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
282                                 $challenge    = hex2bin((string) $res->challenge);
283
284                                 $final_dfrn_id = '';
285
286                                 if(($contact['duplex']) && strlen($contact['prvkey'])) {
287                                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
288                                         openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
289                                 }
290                                 else {
291                                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
292                                         openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
293                                 }
294
295                                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
296
297                                 if(strpos($final_dfrn_id,':') == 1)
298                                         $final_dfrn_id = substr($final_dfrn_id,2);
299
300                                 if($final_dfrn_id != $orig_id) {
301                                         logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);    
302                                         // did not decode properly - cannot trust this site 
303                                         continue;
304                                 }
305
306                                 $postvars['dfrn_id'] = $idtosend;
307                                 $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
308                                 $postvars['perm'] = 'rw';
309
310                                 $xml = post_url($contact['poll'],$postvars);
311                         }
312                         elseif(($contact['network'] === NETWORK_OSTATUS) 
313                                 || ($contact['network'] === NETWORK_DIASPORA)
314                                 || ($contact['network'] === NETWORK_FEED) ) {
315
316                                 // Upgrading DB fields from an older Friendika version
317                                 // Will only do this once per notify-enabled OStatus contact
318                                 // or if relationship changes
319
320                                 $stat_writeable = ((($contact['notify']) && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND)) ? 1 : 0);
321
322                                 if($stat_writeable != $contact['writable']) {
323                                         q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1",
324                                                 intval($stat_writeable),
325                                                 intval($contact['id'])
326                                         );
327                                 }
328
329                                 // Are we allowed to import from this person?
330
331                                 if($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly'])
332                                         continue;
333
334                                 $xml = fetch_url($contact['poll']);
335                         }
336                         elseif($contact['network'] === NETWORK_MAIL) {
337
338                                 $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
339                                 if($mail_disabled)
340                                         continue;
341
342                                 $mbox = null;
343                                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
344                                         intval($importer_uid)
345                                 );
346                                 $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1",
347                                         intval($importer_uid)
348                                 );
349                                 if(count($x) && count($mailconf)) {
350                                     $mailbox = construct_mailbox_name($mailconf[0]);
351                                         $password = '';
352                                         openssl_private_decrypt(hex2bin($mailconf[0]['pass']),$password,$x[0]['prvkey']);
353                                         $mbox = email_connect($mailbox,$mailconf[0]['user'],$password);
354                                         unset($password);
355                                         if($mbox) {
356                                                 q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
357                                                         dbesc(datetime_convert()),
358                                                         intval($mailconf[0]['id']),
359                                                         intval($importer_uid)
360                                                 );
361                                         }
362                                 }
363                                 if($mbox) {
364
365                                         $msgs = email_poll($mbox,$contact['addr']);
366
367                                         if(count($msgs)) {
368                                                 foreach($msgs as $msg_uid) {
369                                                         $datarray = array();
370                                                         $meta = email_msg_meta($mbox,$msg_uid);
371                                                         $headers = email_msg_headers($mbox,$msg_uid);
372
373                                                         // look for a 'references' header and try and match with a parent item we have locally.
374
375                                                         $raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
376                                                         $datarray['uri'] = trim($meta->message_id,'<>');
377
378                                                         if($raw_refs) {
379                                                                 $refs_arr = explode(' ', $raw_refs);
380                                                                 if(count($refs_arr)) {
381                                                                         for($x = 0; $x < count($refs_arr); $x ++)
382                                                                                 $refs_arr[$x] = "'" . str_replace(array('<','>',' '),array('','',''),dbesc($refs_arr[$x])) . "'";
383                                                                 }
384                                                                 $qstr = implode(',',$refs_arr);
385                                                                 $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( $qstr ) AND `uid` = %d LIMIT 1",
386                                                                         intval($importer_uid)
387                                                                 );
388                                                                 if(count($r))
389                                                                         $datarray['parent-uri'] = $r[0]['uri'];
390                                                         }
391
392
393                                                         if(! x($datarray,'parent-uri'))
394                                                                 $datarray['parent-uri'] = $datarray['uri'];
395
396                                                         // Have we seen it before?
397                                                         $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
398                                                                 intval($importer_uid),
399                                                                 dbesc($datarray['uri'])
400                                                         );
401
402                                                         if(count($r)) {
403                                                                 if($meta->deleted && ! $r[0]['deleted']) {
404                                                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
405                                                                                 dbesc(datetime_convert()),
406                                                                                 intval($r[0]['id'])
407                                                                         );
408                                                                 }               
409                                                                 continue;
410                                                         }
411                                                         $datarray['title'] = notags(trim($meta->subject));
412                                                         $datarray['created'] = datetime_convert('UTC','UTC',$meta->date);
413         
414                                                         $r = email_get_msg($mbox,$msg_uid);
415                                                         if(! $r)
416                                                                 continue;
417                                                         $datarray['body'] = escape_tags($r['body']);
418
419                                                         // some mailing lists have the original author as 'from' - add this sender info to msg body. 
420                                                         // todo: adding a gravatar for the original author would be cool
421
422                                                         if(! stristr($meta->from,$contact['addr']))
423                                                                 $datarray['body'] = t('From: ') . escape_tags($meta->from) . "\n\n" . $datarray['body'];
424
425                                                         $datarray['uid'] = $importer_uid;
426                                                         $datarray['contact-id'] = $contact['id'];
427                                                         if($datarray['parent-uri'] === $datarray['uri'])
428                                                                 $datarray['private'] = 1;
429                                                         if(! get_pconfig($importer_uid,'system','allow_public_email_replies')) {
430                                                                 $datarray['private'] = 1;
431                                                                 $datarray['allow_cid'] = '<' . $contact['id'] . '>';
432                                                         }
433                                                         $datarray['author-name'] = $contact['name'];
434                                                         $datarray['author-link'] = 'mailbox';
435                                                         $datarray['author-avatar'] = $contact['photo'];
436                                                 
437                                                         $stored_item = item_store($datarray);
438                                                         q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d",
439                                                                 dbesc($datarray['parent-uri']),
440                                                                 intval($importer_uid)
441                                                         );
442                                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
443                                                                 intval($stored_item)
444                                                         );
445                                                 }
446                                         }
447
448                                         imap_close($mbox);
449                                 }
450                         }
451                         elseif($contact['network'] === NETWORK_FACEBOOK) {
452                                 // This is picked up by the Facebook plugin on a cron hook.
453                                 // Ignored here.                        
454                         }
455
456                         if($xml) {
457                                 logger('poller: received xml : ' . $xml, LOGGER_DATA);
458
459                                 if(! strstr($xml,'<?xml')) {
460                                         logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
461                                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
462                                                 dbesc(datetime_convert()),
463                                                 intval($contact['id'])
464                                         );
465                                         continue;
466                                 }
467
468
469                                 consume_feed($xml,$importer,$contact,$hub,1, true);
470
471                                 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
472         
473                                 consume_feed($xml,$importer,$contact,$hub,1);
474
475
476                                 if((strlen($hub)) && ($hub_update) && (($contact['rel'] == CONTACT_IS_FRIEND) || (($contact['network'] === NETWORK_OSTATUS) && (! $contact['readonly'])))) {
477                                         logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
478                                         $hubs = explode(',', $hub);
479                                         if(count($hubs)) {
480                                                 foreach($hubs as $h) {
481                                                         $h = trim($h);
482                                                         if(! strlen($h))
483                                                                 continue;
484                                                         subscribe_to_hub($h,$importer,$contact);
485                                                 }
486                                         }
487                                 }
488                         }
489
490                         $updated = datetime_convert();
491
492                         $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1",
493                                 dbesc($updated),
494                                 dbesc($updated),
495                                 intval($contact['id'])
496                         );
497
498                         // loop - next contact
499                 }
500         }
501
502                 
503         return;
504 }
505
506 if (array_search(__file__,get_included_files())===0){
507   poller_run($argv,$argc);
508   killme();
509 }