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