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