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