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