]> git.mxchange.org Git - friendica.git/blob - include/poller.php
e8bdc94a30df62a0cd0786d64de275fff0f2e784
[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
18         require_once('include/Contact.php');
19
20         $debugging = get_config('system','debugging');
21
22         $a->set_baseurl(get_config('system','url'));
23
24         // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
25         $contacts = q("SELECT * FROM `contact` 
26                 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
27                 OR ( `network` = 'stat' AND `poll` != '' ) ) 
28                 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
29
30         if(! count($contacts))
31                 killme();
32
33         foreach($contacts as $contact) {
34
35                 if($contact['priority'] || $contact['subhub']) {
36
37                         $update = false;
38
39                         // We should be getting everything via a hub. But just to be sure, let's check once a day.
40                         // This also lets us update our subscription to the hub, and add or replace hubs in case it
41                         // changed. 
42
43                         if($contact['subhub'])
44                                 $contact['priority'] = 3;
45
46                         $t = $contact['last-update'];
47
48                         switch ($contact['priority']) {
49                                 case 5:
50                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
51                                                 $update = true;
52                                         break;                                  
53                                 case 4:
54                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
55                                                 $update = true;
56                                         break;
57                                 case 3:
58                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
59                                                 $update = true;
60                                         break;
61                                 case 2:
62                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
63                                                 $update = true;
64                                         break;
65                                 case 1:
66                                 default:
67                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
68                                                 $update = true;
69                                         break;
70                         }
71                         if(! $update)
72                                 continue;
73                 }
74
75                 $importer_uid = $contact['uid'];
76
77                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
78                         intval($importer_uid)
79                 );
80                 if(! count($r))
81                         continue;
82
83                 $importer = $r[0];
84
85                 if($debugging)
86                         echo "IMPORTER: {$importer['name']}\n";
87
88                 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00') 
89                         ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
90                         : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
91                 );
92
93                 if($contact['network'] === 'dfrn') {
94
95                         $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
96
97                         if(intval($contact['duplex']) && $contact['dfrn-id'])
98                                 $idtosend = '0:' . $orig_id;
99                         if(intval($contact['duplex']) && $contact['issued-id'])
100                                 $idtosend = '1:' . $orig_id;            
101
102                         $url = $contact['poll'] . '?dfrn_id=' . $idtosend 
103                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION 
104                                 . '&type=data&last_update=' . $last_update ;
105         
106                         $xml = fetch_url($url);
107
108                         if($debugging) {
109                                 echo "URL: " . $url . "\n";
110                                 echo "XML: " . $xml . "\n";
111                         }
112
113                         if(! $xml) {
114                                 // dead connection - might be a transient event, or this might
115                                 // mean the software was uninstalled or the domain expired. 
116                                 // Will keep trying for one month.
117                                 mark_for_death($contact);
118                                 continue;
119                         }
120
121
122                         $res = simplexml_load_string($xml);
123
124                         if(intval($res->status) == 1) {
125                                 // we may not be friends anymore. Will keep trying for one month.
126                                 mark_for_death($contact);
127                         }
128                         else {
129                                 if($contact['term-date'] != '0000-00-00 00:00:00')
130                                         unmark_for_death($contact);
131                         }
132
133                         if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
134                                 continue;
135
136                         $postvars = array();
137
138                         $sent_dfrn_id = hex2bin($res->dfrn_id);
139                         $challenge    = hex2bin($res->challenge);
140
141                         $final_dfrn_id = '';
142
143                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
144                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
145                                 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
146                         }
147                         else {
148                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
149                                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
150                         }
151
152                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
153
154                         if(strpos($final_dfrn_id,':') == 1)
155                                 $final_dfrn_id = substr($final_dfrn_id,2);
156
157                         if($final_dfrn_id != $orig_id) {
158
159                                 // did not decode properly - cannot trust this site 
160                                 continue;
161                         }
162
163                         $postvars['dfrn_id'] = $idtosend;
164                         $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
165
166                         $xml = post_url($contact['poll'],$postvars);
167                 }
168                 else {
169                         // $contact['network'] !== 'dfrn'
170
171                         $xml = fetch_url($contact['poll']);
172                 }
173
174                 if($debugging) {
175                         echo "XML response:" . $xml . "\n";
176                         echo "Length:" . strlen($xml) . "\n";
177                 }
178
179                 if(! strlen($xml))
180                         continue;
181
182                 consume_feed($xml,$importer,$contact,$hub);
183                 
184                 if((strlen($hub)) && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly']))) && ($contact['priority'] == 0)) {
185                         $hubs = explode(',', $hub);
186                         if(count($hubs)) {
187                                 foreach($hubs as $h) {
188                                         $h = trim($h);
189                                         if(! strlen($h))
190                                                 continue;
191                                         subscribe_to_hub($h,$importer,$contact);
192                                 }
193                         }
194                 }
195
196
197                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
198                         dbesc(datetime_convert()),
199                         intval($contact['id'])
200                 );
201
202                 // loop - next contact
203         }  
204                 
205         killme();
206
207
208