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