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