]> git.mxchange.org Git - friendica.git/blob - include/poller.php
more little stuff
[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 AND `subhub` = 0 ORDER BY RAND()");
27
28         if(! count($contacts))
29                 killme();
30
31         foreach($contacts as $contact) {
32
33                 if($contact['priority']) {
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                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', $t . " + 1 hour"))
58                                                 $update = true;
59                                         break;
60                         }
61                         if(! $update)
62                                 continue;
63                 }
64
65                 $importer_uid = $contact['uid'];
66
67                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
68                         intval($importer_uid)
69                 );
70                 if(! count($r))
71                         continue;
72
73                 $importer = $r[0];
74
75                 if($debugging)
76                         echo "IMPORTER: {$importer['name']}";
77
78                 $last_update = (($contact['last-update'] === '0000-00-00 00:00:00') 
79                         ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
80                         : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
81
82
83
84                 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
85
86                 if(intval($contact['duplex']) && $contact['dfrn-id'])
87                         $idtosend = '0:' . $orig_id;
88                 if(intval($contact['duplex']) && $contact['issued-id'])
89                         $idtosend = '1:' . $orig_id;            
90
91                 $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&type=data&last_update=' . $last_update ;
92                 $xml = fetch_url($url);
93
94                 if($debugging) {
95                         echo "URL: " . $url . "\r\n";
96                         echo "XML: " . $xml . "\r\n";
97                 }
98
99                 if(! $xml) {
100                         // dead connection - might be a transient event, or this might
101                         // mean the software was uninstalled or the domain expired. 
102                         // Will keep trying for one month.
103                         mark_for_death($contact);
104                         continue;
105                 }
106
107
108                 $res = simplexml_load_string($xml);
109
110                 if(intval($res->status) == 1) {
111                         // we may not be friends anymore. Will keep trying for one month.
112                         mark_for_death($contact);
113                 }
114                 else {
115                         if($contact['term-date'] != '0000-00-00 00:00:00')
116                                 unmark_for_death($contact);
117                 }
118
119                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
120                         continue;
121
122                 $postvars = array();
123
124                 $sent_dfrn_id = hex2bin($res->dfrn_id);
125                 $challenge    = hex2bin($res->challenge);
126
127                 $final_dfrn_id = '';
128
129                 if(($contact['duplex']) && strlen($contact['prvkey'])) {
130                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
131                         openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
132
133                 }
134                 else {
135                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
136                         openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
137                 }
138
139                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
140
141                 if(strpos($final_dfrn_id,':') == 1)
142                         $final_dfrn_id = substr($final_dfrn_id,2);
143
144                 if($final_dfrn_id != $orig_id) {
145
146                         // did not decode properly - cannot trust this site 
147                         continue;
148                 }
149
150                 $postvars['dfrn_id'] = $idtosend;
151
152
153                 $xml = post_url($contact['poll'],$postvars);
154
155                 if($debugging) {
156                         echo "XML response:" . $xml . "\r\n";
157                         echo "Length:" . strlen($xml) . "\r\n";
158                 }
159
160                 if(! strlen($xml))
161                         continue;
162
163
164                 consume_feed($xml,$importer,$contact,$hub);
165                 
166
167                 if((strlen($hub)) && ($contact['rel'] == REL_BUD) && ($contact['priority'] == 0)) {
168                         subscribe_to_hub($hub,$importer,$contact);
169                 }
170
171
172                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
173                         dbesc(datetime_convert()),
174                         intval($contact['id'])
175                 );
176
177         }
178                 
179         killme();
180
181
182