5 require_once('boot.php');
9 @include('.htconfig.php');
10 require_once('dba.php');
11 $db = new dba($db_host, $db_user, $db_pass, $db_data);
12 unset($db_host, $db_user, $db_pass, $db_data);
14 require_once('session.php');
15 require_once('datetime.php');
16 require_once('simplepie/simplepie.inc');
17 require_once('include/items.php');
19 require_once('include/Contact.php');
21 $a->set_baseurl(get_config('system','url'));
23 $contacts = q("SELECT * FROM `contact`
24 WHERE ( `dfrn-id` != '' OR (`issued-id` != '' AND `duplex` = 1))
25 AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()");
27 if(! count($contacts))
30 foreach($contacts as $contact) {
32 if($contact['priority']) {
35 $t = $contact['last-update'];
37 switch ($contact['priority']) {
39 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month"))
43 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week"))
47 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day"))
51 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour"))
56 if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour"))
65 $importer_uid = $contact['uid'];
67 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
76 echo "IMPORTER: {$importer['name']}";
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'));
82 $idtosend = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
84 $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&type=data&last_update=' . $last_update ;
86 $xml = fetch_url($url);
89 echo "URL: " . $url . "\r\n";
90 echo "XML: " . $xml . "\r\n";
96 $res = simplexml_load_string($xml);
98 if(intval($res->status) == 1)
99 mark_for_death($contact);
101 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
104 if($contact['term-date'] != '0000-00-00 00:00:00')
105 unmark_for_death($contact);
109 $sent_dfrn_id = hex2bin($res->dfrn_id);
110 $challenge = hex2bin($res->challenge);
114 if(($contact['duplex']) && strlen($contact['prvkey'])) {
115 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
116 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
120 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
121 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
124 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
125 if($final_dfrn_id != $idtosend) {
126 // did not decode properly - cannot trust this site
130 $postvars['dfrn_id'] = $idtosend;
132 $xml = post_url($contact['poll'],$postvars);
135 echo "XML response:" . $xml . "\r\n";
136 echo "Length:" . strlen($xml) . "\r\n";
140 // an empty response may mean there's nothing new - record the fact that we checked
141 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
142 dbesc(datetime_convert()),
143 intval($contact['id'])
148 $feed = new SimplePie();
149 $feed->set_raw_data($xml);
150 $feed->enable_order_by_date(false);
153 // Check at the feed level for updated contact name and/or photo
157 $photo_timestamp = '';
160 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, author);
162 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
163 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
164 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
165 $new_name = $elems['name'][0]['data'];
167 if(($elems['link'][0]['attribs']['']['rel'] == 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
168 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
169 $photo_url = $elems['link'][0]['attribs']['']['href'];
172 if(! $photo_timestamp) {
173 $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
174 if($photo_rawupdate) {
175 $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
176 $photo_url = $feed->get_image_url();
179 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
181 require_once("Photo.php");
182 $photo_failure = false;
184 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
185 intval($contact['id']),
186 intval($contact['uid'])
189 $resource_id = $r[0]['resource-id'];
190 $img_str = fetch_url($photo_url,true);
191 $img = new Photo($img_str);
193 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
195 intval($contact['id']),
196 intval($contact['uid'])
199 $img->scaleImageSquare(175);
201 $hash = $resource_id;
202 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
204 $img->scaleImage(80);
205 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
207 q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
208 dbesc(datetime_convert()),
209 intval($contact['uid']),
210 intval($contact['id'])
216 if(($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
217 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
218 dbesc(notags(trim($new_name))),
219 dbesc(datetime_convert()),
220 intval($contact['uid']),
221 intval($contact['id'])
225 // Now process the feed
227 foreach($feed->get_items() as $item) {
231 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
232 if(isset($rawdelete[0]['attribs']['']['ref'])) {
233 $uri = $rawthread[0]['attribs']['']['ref'];
235 if(isset($rawdelete[0]['attribs']['']['when'])) {
236 $when = $rawthread[0]['attribs']['']['when'];
237 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
240 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
243 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
245 intval($importer['uid'])
249 if($item['uri'] == $item['parent-uri']) {
250 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
251 `body` = '', `title` = ''
252 WHERE `parent-uri` = '%s' AND `uid` = %d",
254 dbesc(datetime_convert()),
256 intval($importer['uid'])
260 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
261 `body` = '', `title` = ''
262 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
264 dbesc(datetime_convert()),
266 intval($importer['uid'])
268 if($item['last-child']) {
269 // ensure that last-child is set in case the comment that had it just got wiped.
270 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
271 dbesc(datetime_convert()),
272 dbesc($item['parent-uri']),
275 // who is the last child now?
276 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
277 ORDER BY `edited` DESC LIMIT 1",
278 dbesc($item['parent-uri']),
279 intval($importer['uid'])
282 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
294 $item_id = $item->get_id();
295 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
296 if(isset($rawthread[0]['attribs']['']['ref'])) {
298 $parent_uri = $rawthread[0]['attribs']['']['ref'];
304 // Have we seen it? If not, import it.
306 $item_id = $item->get_id();
308 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
310 intval($importer['uid'])
312 // FIXME update content if 'updated' changes
314 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
315 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
316 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
317 dbesc(datetime_convert()),
319 intval($importer['uid'])
321 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
322 intval($allow[0]['data']),
323 dbesc(datetime_convert()),
325 intval($importer['uid'])
332 $datarray = get_atom_elements($item);
333 $datarray['parent-uri'] = $parent_uri;
334 $datarray['uid'] = $importer['uid'];
335 $datarray['contact-id'] = $contact['id'];
336 $r = post_remote($a,$datarray);
341 // Head post of a conversation. Have we seen it? If not, import it.
343 $item_id = $item->get_id();
344 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
346 intval($importer['uid'])
349 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
350 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
351 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
352 intval($allow[0]['data']),
353 dbesc(datetime_convert()),
355 intval($importer['uid'])
361 $datarray = get_atom_elements($item);
362 $datarray['parent-uri'] = $item_id;
363 $datarray['uid'] = $importer['uid'];
364 $datarray['contact-id'] = $contact['id'];
365 $r = post_remote($a,$datarray);
372 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
373 dbesc(datetime_convert()),
374 intval($contact['id'])