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