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