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