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'));
84 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
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;
91 $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&type=data&last_update=' . $last_update ;
92 $xml = fetch_url($url);
95 echo "URL: " . $url . "\r\n";
96 echo "XML: " . $xml . "\r\n";
104 $res = simplexml_load_string($xml);
106 if(intval($res->status) == 1)
107 mark_for_death($contact);
109 if($contact['term-date'] != '0000-00-00 00:00:00')
110 unmark_for_death($contact);
113 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
119 $sent_dfrn_id = hex2bin($res->dfrn_id);
120 $challenge = hex2bin($res->challenge);
124 if(($contact['duplex']) && strlen($contact['prvkey'])) {
125 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
126 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
130 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
131 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
134 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
136 if(strpos($final_dfrn_id,':') == 1)
137 $final_dfrn_id = substr($final_dfrn_id,2);
139 if($final_dfrn_id != $orig_id) {
141 // did not decode properly - cannot trust this site
145 $postvars['dfrn_id'] = $idtosend;
149 $xml = post_url($contact['poll'],$postvars);
152 echo "XML response:" . $xml . "\r\n";
153 echo "Length:" . strlen($xml) . "\r\n";
157 // an empty response may mean there's nothing new - record the fact that we checked
158 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
159 dbesc(datetime_convert()),
160 intval($contact['id'])
165 $feed = new SimplePie();
166 $feed->set_raw_data($xml);
167 $feed->enable_order_by_date(false);
170 // Check at the feed level for updated contact name and/or photo
174 $photo_timestamp = '';
177 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, author);
179 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
180 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
181 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
182 $new_name = $elems['name'][0]['data'];
184 if(($elems['link'][0]['attribs']['']['rel'] == 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
185 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
186 $photo_url = $elems['link'][0]['attribs']['']['href'];
189 if(! $photo_timestamp) {
190 $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
191 if($photo_rawupdate) {
192 $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
193 $photo_url = $feed->get_image_url();
196 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
198 require_once("Photo.php");
199 $photo_failure = false;
201 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
202 intval($contact['id']),
203 intval($contact['uid'])
206 $resource_id = $r[0]['resource-id'];
207 $img_str = fetch_url($photo_url,true);
208 $img = new Photo($img_str);
210 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
212 intval($contact['id']),
213 intval($contact['uid'])
216 $img->scaleImageSquare(175);
218 $hash = $resource_id;
219 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
221 $img->scaleImage(80);
222 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
224 q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
225 dbesc(datetime_convert()),
226 intval($contact['uid']),
227 intval($contact['id'])
233 if(($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
234 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
235 dbesc(notags(trim($new_name))),
236 dbesc(datetime_convert()),
237 intval($contact['uid']),
238 intval($contact['id'])
242 // Now process the feed
244 foreach($feed->get_items() as $item) {
248 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
249 if(isset($rawdelete[0]['attribs']['']['ref'])) {
250 $uri = $rawthread[0]['attribs']['']['ref'];
252 if(isset($rawdelete[0]['attribs']['']['when'])) {
253 $when = $rawthread[0]['attribs']['']['when'];
254 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
257 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
260 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
262 intval($importer['uid'])
266 if($item['uri'] == $item['parent-uri']) {
267 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
268 `body` = '', `title` = ''
269 WHERE `parent-uri` = '%s' AND `uid` = %d",
271 dbesc(datetime_convert()),
273 intval($importer['uid'])
277 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
278 `body` = '', `title` = ''
279 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
281 dbesc(datetime_convert()),
283 intval($importer['uid'])
285 if($item['last-child']) {
286 // ensure that last-child is set in case the comment that had it just got wiped.
287 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
288 dbesc(datetime_convert()),
289 dbesc($item['parent-uri']),
292 // who is the last child now?
293 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
294 ORDER BY `edited` DESC LIMIT 1",
295 dbesc($item['parent-uri']),
296 intval($importer['uid'])
299 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
311 $item_id = $item->get_id();
312 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
313 if(isset($rawthread[0]['attribs']['']['ref'])) {
315 $parent_uri = $rawthread[0]['attribs']['']['ref'];
321 // Have we seen it? If not, import it.
323 $item_id = $item->get_id();
325 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
327 intval($importer['uid'])
329 // FIXME update content if 'updated' changes
331 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
332 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
333 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
334 dbesc(datetime_convert()),
336 intval($importer['uid'])
338 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
339 intval($allow[0]['data']),
340 dbesc(datetime_convert()),
342 intval($importer['uid'])
349 $datarray = get_atom_elements($item);
350 $datarray['parent-uri'] = $parent_uri;
351 $datarray['uid'] = $importer['uid'];
352 $datarray['contact-id'] = $contact['id'];
353 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
354 $datarray['type'] = 'activity';
355 $datarray['gravity'] = GRAVITY_LIKE;
358 $r = item_store($datarray);
363 // Head post of a conversation. Have we seen it? If not, import it.
365 $item_id = $item->get_id();
366 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
368 intval($importer['uid'])
371 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
372 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
373 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
374 intval($allow[0]['data']),
375 dbesc(datetime_convert()),
377 intval($importer['uid'])
383 $datarray = get_atom_elements($item);
384 $datarray['parent-uri'] = $item_id;
385 $datarray['uid'] = $importer['uid'];
386 $datarray['contact-id'] = $contact['id'];
387 $r = item_store($datarray);
394 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
395 dbesc(datetime_convert()),
396 intval($contact['id'])