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