]> git.mxchange.org Git - friendica.git/blob - include/poller.php
be073b93b0b2c33badf1fe8ca1c44d86aba87b83
[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, $install);
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 if($argc < 2)
19         exit;
20 dbg(3);
21         $a->set_baseurl($argv[1]);
22
23         $contacts = q("SELECT * FROM `contact` WHERE `dfrn-id` != '' AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
24
25         if(! count($contacts))
26                 killme();
27
28         foreach($contacts as $contact) {
29
30                 if($contact['priority']) {
31
32                         $update = false;
33                         $t = $contact['last-update'];
34
35                         switch ($contact['priority']) {
36                                 case 5:
37                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month"))
38                                                 $update = true;
39                                         break;                                  
40                                 case 4:
41                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week"))
42                                                 $update = true;
43                                         break;
44                                 case 3:
45                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day"))
46                                                 $update = true;
47                                         break;
48                                 case 2:
49                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour"))
50                                                 $update = true;
51                                         break;
52                                 case 1:
53                                 default:
54                                         if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour"))
55                                                 $update = true;
56                                         break;
57                         }
58                         if(! $update)
59                                 continue;
60                 }
61
62
63                 $importer_uid = $contact['uid'];
64
65                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
66                         intval($importer_uid)
67                 );
68                 if(! count($r))
69                         continue;
70
71                 $importer = $r[0];
72
73                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
74                         ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
75                         : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
76
77                 $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ;
78
79                 $xml = fetch_url($url);
80 echo "URL: " . $url;
81 echo "XML: " . $xml;
82                 if(! $xml)
83                         continue;
84
85                 $res = simplexml_load_string($xml);
86
87                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
88                         continue;
89
90                 $postvars = array();
91
92                 $sent_dfrn_id = hex2bin($res->dfrn_id);
93
94                 $final_dfrn_id = '';
95                 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
96                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
97                 if($final_dfrn_id != $contact['dfrn-id']) {
98                         // did not decode properly - cannot trust this site 
99                         continue;
100                 }
101
102                 $postvars['dfrn_id'] = $contact['dfrn-id'];
103                 $challenge = hex2bin($res->challenge);
104
105                 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
106
107                 $xml = post_url($contact['poll'],$postvars);
108
109 echo "XML response:" . $xml . "\r\n";
110 echo "Length:" . strlen($xml) . "\r\n";
111
112                 if(! strlen($xml)) {
113                         // an empty response may mean there's nothing new - record the fact that we checked
114                         $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
115                                 dbesc(datetime_convert()),
116                                 intval($contact['id'])
117                         );
118                         continue;
119                 }
120
121                 $feed = new SimplePie();
122                 $feed->set_raw_data($xml);
123                 $feed->enable_order_by_date(false);
124                 $feed->init();
125
126                 foreach($feed->get_items() as $item) {
127
128                         $deleted = false;
129
130                         $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry');
131                         if(isset($rawdelete[0]['attribs']['']['ref'])) {
132                                 $uri = $rawthread[0]['attribs']['']['ref'];
133                                 $deleted = true;
134                                 if(isset($rawdelete[0]['attribs']['']['when'])) {
135                                         $when = $rawthread[0]['attribs']['']['when'];
136                                         $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
137                                 }
138                                 else
139                                         $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
140                         }
141                         if($deleted) {
142                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
143                                         dbesc($uri),
144                                         intval($importer['uid'])
145                                 );
146                                 if(count($r)) {
147                                         if($r[0]['uri'] == $r[0]['parent-uri']) {
148                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s'
149                                                         WHERE `parent-uri` = '%s'",
150                                                         dbesc($when),
151                                                         dbesc($r[0]['uri'])
152                                                 );
153                                         }
154                                         else {
155                                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s' 
156                                                         WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
157                                                         dbesc($when),
158                                                         dbesc($uri),
159                                                         intval($importer['uid'])
160                                                 );
161                                         }
162                                 }       
163                                 continue;
164                         }
165
166
167                         $is_reply = false;              
168                         $item_id = $item->get_id();
169                         $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to');
170                         if(isset($rawthread[0]['attribs']['']['ref'])) {
171                                 $is_reply = true;
172                                 $parent_uri = $rawthread[0]['attribs']['']['ref'];
173                         }
174
175
176                         if($is_reply) {
177
178                                 // Have we seen it? If not, import it.
179
180                                 $item_id = $item->get_id();
181
182                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
183                                         dbesc($item_id),
184                                         intval($importer['uid'])
185                                 );
186                                 // FIXME update content if 'updated' changes
187                                 if(count($r)) {
188                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
189                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
190                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
191                                                         intval($allow[0]['data']),
192                                                         dbesc($item_id),
193                                                         intval($importer['uid'])
194                                                 );
195                                         }
196                                         continue;
197                                 }
198                                 $datarray = get_atom_elements($item);
199                                 $datarray['parent-uri'] = $parent_uri;
200                                 $datarray['uid'] = $importer['uid'];
201                                 $datarray['contact-id'] = $contact['id'];
202                                 $r = post_remote($a,$datarray);
203                                 continue;
204                         }
205
206                         else {
207                                 // Head post of a conversation. Have we seen it? If not, import it.
208
209                                 $item_id = $item->get_id();
210                                 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
211                                         dbesc($item_id),
212                                         intval($importer['uid'])
213                                 );
214                                 if(count($r)) {
215                                         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
216                                         if($allow && $allow[0]['data'] != $r[0]['last-child']) {
217                                                 $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
218                                                         intval($allow[0]['data']),
219                                                         dbesc($item_id),
220                                                         intval($importer['uid'])
221                                                 );
222                                         }
223                                         continue;
224                                 }
225
226                                 $datarray = get_atom_elements($item);
227                                 $datarray['parent-uri'] = $item_id;
228                                 $datarray['uid'] = $importer['uid'];
229                                 $datarray['contact-id'] = $contact['id'];
230                                 $r = post_remote($a,$datarray);
231                                 continue;
232
233                         }
234
235                 }
236
237                 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
238                         dbesc(datetime_convert()),
239                         intval($contact['id'])
240                 );
241
242         }
243                 
244         killme();
245
246
247