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