]> git.mxchange.org Git - friendica.git/blob - include/poller.php
view directory cleanup
[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         $force = false;
22         if(($argc > 1) && ($argv[1] == 'force'))
23                 $force = true;
24
25         // 'stat' clause is a temporary measure until we have federation subscriptions working both directions
26         $contacts = q("SELECT * FROM `contact` 
27                 WHERE ( ( `network` = 'dfrn' AND ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1)))
28                 OR ( `network` = 'stat' AND `poll` != '' ) ) 
29                 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
30
31         if(! count($contacts))
32                 killme();
33
34         foreach($contacts as $contact) {
35
36                 if($contact['priority'] || $contact['subhub']) {
37
38                         $update = false;
39
40                         // We should be getting everything via a hub. But just to be sure, let's check once a day.
41                         // This also lets us update our subscription to the hub, and add or replace hubs in case it
42                         // changed. 
43
44                         if($contact['subhub'])
45                                 $contact['priority'] = 3;
46
47                         $t = $contact['last-update'];
48
49                         switch ($contact['priority']) {
50                                 case 5:
51                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
52                                                 $update = true;
53                                         break;                                  
54                                 case 4:
55                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
56                                                 $update = true;
57                                         break;
58                                 case 3:
59                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
60                                                 $update = true;
61                                         break;
62                                 case 2:
63                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
64                                                 $update = true;
65                                         break;
66                                 case 1:
67                                 default:
68                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
69                                                 $update = true;
70                                         break;
71                         }
72                         if((! $update) && (! $force))
73                                 continue;
74                 }
75
76                 $importer_uid = $contact['uid'];
77
78                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
79                         intval($importer_uid)
80                 );
81                 if(! count($r))
82                         continue;
83
84                 $importer = $r[0];
85
86                 logger("poller: poll: IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
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                         logger('poller: handshake with url ' . $url . ' returns xml: ' . $xml, LOGGER_DATA);
109
110                         if(! $xml) {
111                                 logger("poller: $url appears to be dead - marking for death ");
112                                 // dead connection - might be a transient event, or this might
113                                 // mean the software was uninstalled or the domain expired. 
114                                 // Will keep trying for one month.
115                                 mark_for_death($contact);
116                                 continue;
117                         }
118
119
120                         $res = simplexml_load_string($xml);
121
122                         if(intval($res->status) == 1) {
123                                 logger("poller: $url replied status 1 - marking for death ");
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                                         logger("poller: $url back from the dead - removing mark for death");
130                                         unmark_for_death($contact);
131                                 }
132                         }
133
134                         if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
135                                 continue;
136
137                         $postvars = array();
138
139                         $sent_dfrn_id = hex2bin($res->dfrn_id);
140                         $challenge    = hex2bin($res->challenge);
141
142                         $final_dfrn_id = '';
143
144                         if(($contact['duplex']) && strlen($contact['prvkey'])) {
145                                 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
146                                 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
147                         }
148                         else {
149                                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
150                                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
151                         }
152
153                         $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
154
155                         if(strpos($final_dfrn_id,':') == 1)
156                                 $final_dfrn_id = substr($final_dfrn_id,2);
157
158                         if($final_dfrn_id != $orig_id) {
159
160                                 // did not decode properly - cannot trust this site 
161                                 continue;
162                         }
163
164                         $postvars['dfrn_id'] = $idtosend;
165                         $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
166
167                         $xml = post_url($contact['poll'],$postvars);
168                 }
169                 else {
170                         // $contact['network'] !== 'dfrn'
171
172                         $xml = fetch_url($contact['poll']);
173                 }
174
175                 logger('poller: received xml : ' . $xml, LOGGER_DATA);
176
177                 if(! strlen($xml))
178                         continue;
179
180                 consume_feed($xml,$importer,$contact,$hub);
181
182                 // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
183
184                 consume_feed($xml,$importer,$contact,$hub);
185
186
187                 if((strlen($hub)) && (($contact['rel'] == REL_BUD) || (($contact['network'] === 'stat') && (! $contact['readonly'])))) {
188                         logger('poller: subscribing to hub(s) : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : ' . $importer['name']);
189                         $hubs = explode(',', $hub);
190                         if(count($hubs)) {
191                                 foreach($hubs as $h) {
192                                         $h = trim($h);
193                                         if(! strlen($h))
194                                                 continue;
195                                         subscribe_to_hub($h,$importer,$contact);
196                                 }
197                         }
198                 }
199
200
201                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
202                         dbesc(datetime_convert()),
203                         intval($contact['id'])
204                 );
205
206                 // loop - next contact
207         }  
208                 
209         killme();
210
211
212