]> git.mxchange.org Git - friendica.git/blob - include/poller.php
minor 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
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 ( `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                         $t = $contact['last-update'];
37
38                         switch ($contact['priority']) {
39                                 case 5:
40                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 month"))
41                                                 $update = true;
42                                         break;                                  
43                                 case 4:
44                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 week"))
45                                                 $update = true;
46                                         break;
47                                 case 3:
48                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 day"))
49                                                 $update = true;
50                                         break;
51                                 case 2:
52                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 12 hour"))
53                                                 $update = true;
54                                         break;
55                                 case 1:
56                                 default:
57
58                                                         // if pubsub delivery is in effect, poll no more than once every hour.
59                                                         // We shouldn't need to poll at all with pubsub, but the hub isn't 
60                                                         // always the most reliable postmaster... This way we still get the 
61                                                         // messages when the hub goes flaky, just perhaps a bit slower. 
62                                                         // At least it doesn't hurt to get them twice. 
63
64                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
65                                                 $update = true;
66                                         break;
67                         }
68                         if(! $update)
69                                 continue;
70                 }
71
72                 $importer_uid = $contact['uid'];
73
74                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
75                         intval($importer_uid)
76                 );
77                 if(! count($r))
78                         continue;
79
80                 $importer = $r[0];
81
82                 if($debugging)
83                         echo "IMPORTER: {$importer['name']}\n";
84
85                 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00') 
86                         ? datetime_convert('UTC','UTC','now - 30 days', ATOM_TIME)
87                         : datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
88                 );
89
90                 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
91
92                 if(intval($contact['duplex']) && $contact['dfrn-id'])
93                         $idtosend = '0:' . $orig_id;
94                 if(intval($contact['duplex']) && $contact['issued-id'])
95                         $idtosend = '1:' . $orig_id;            
96
97                 $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&type=data&last_update=' . $last_update ;
98                 $xml = fetch_url($url);
99
100                 if($debugging) {
101                         echo "URL: " . $url . "\r\n";
102                         echo "XML: " . $xml . "\r\n";
103                 }
104
105                 if(! $xml) {
106                         // dead connection - might be a transient event, or this might
107                         // mean the software was uninstalled or the domain expired. 
108                         // Will keep trying for one month.
109                         mark_for_death($contact);
110                         continue;
111                 }
112
113
114                 $res = simplexml_load_string($xml);
115
116                 if(intval($res->status) == 1) {
117                         // we may not be friends anymore. Will keep trying for one month.
118                         mark_for_death($contact);
119                 }
120                 else {
121                         if($contact['term-date'] != '0000-00-00 00:00:00')
122                                 unmark_for_death($contact);
123                 }
124
125                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
126                         continue;
127
128                 $postvars = array();
129
130                 $sent_dfrn_id = hex2bin($res->dfrn_id);
131                 $challenge    = hex2bin($res->challenge);
132
133                 $final_dfrn_id = '';
134
135                 if(($contact['duplex']) && strlen($contact['prvkey'])) {
136                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
137                         openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
138
139                 }
140                 else {
141                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
142                         openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
143                 }
144
145                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
146
147                 if(strpos($final_dfrn_id,':') == 1)
148                         $final_dfrn_id = substr($final_dfrn_id,2);
149
150                 if($final_dfrn_id != $orig_id) {
151
152                         // did not decode properly - cannot trust this site 
153                         continue;
154                 }
155
156                 $postvars['dfrn_id'] = $idtosend;
157
158
159                 $xml = post_url($contact['poll'],$postvars);
160
161                 if($debugging) {
162                         echo "XML response:" . $xml . "\n";
163                         echo "Length:" . strlen($xml) . "\n";
164                 }
165
166                 if(! strlen($xml))
167                         continue;
168
169
170                 consume_feed($xml,$importer,$contact,$hub);
171                 
172
173                 if((strlen($hub)) && ($contact['rel'] == REL_BUD) && ($contact['priority'] == 0)) {
174                         subscribe_to_hub($hub,$importer,$contact);
175                 }
176
177
178                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
179                         dbesc(datetime_convert()),
180                         intval($contact['id'])
181                 );
182
183         }
184                 
185         killme();
186
187
188