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