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