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"))
64 $importer_uid = $contact['uid'];
66 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
75 echo "IMPORTER: {$importer['name']}";
77 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
78 ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z')
79 : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z'));
83 $idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
85 if(intval($contact['duplex']) && $contact['dfrn-id'])
86 $idtosend = '0:' . $orig_id;
87 if(intval($contact['duplex']) && $contact['issued-id'])
88 $idtosend = '1:' . $orig_id;
90 $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&type=data&last_update=' . $last_update ;
91 $xml = fetch_url($url);
94 echo "URL: " . $url . "\r\n";
95 echo "XML: " . $xml . "\r\n";
99 // dead connection - might be a transient event, or this might
100 // mean the software was uninstalled or the domain expired.
101 // Will keep trying for one month.
102 mark_for_death($contact);
107 $res = simplexml_load_string($xml);
109 if(intval($res->status) == 1) {
110 // we may not be friends anymore. Will keep trying for one month.
111 mark_for_death($contact);
114 if($contact['term-date'] != '0000-00-00 00:00:00')
115 unmark_for_death($contact);
118 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
123 $sent_dfrn_id = hex2bin($res->dfrn_id);
124 $challenge = hex2bin($res->challenge);
128 if(($contact['duplex']) && strlen($contact['prvkey'])) {
129 openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
130 openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
134 openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
135 openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
138 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
140 if(strpos($final_dfrn_id,':') == 1)
141 $final_dfrn_id = substr($final_dfrn_id,2);
143 if($final_dfrn_id != $orig_id) {
145 // did not decode properly - cannot trust this site
149 $postvars['dfrn_id'] = $idtosend;
152 $xml = post_url($contact['poll'],$postvars);
155 echo "XML response:" . $xml . "\r\n";
156 echo "Length:" . strlen($xml) . "\r\n";
162 $feed = new SimplePie();
163 $feed->set_raw_data($xml);
164 $feed->enable_order_by_date(false);
167 // Check at the feed level for updated contact name and/or photo
171 $photo_timestamp = '';
174 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, author);
176 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
177 if($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
178 $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
179 $new_name = $elems['name'][0]['data'];
181 if(($elems['link'][0]['attribs']['']['rel'] == 'photo') && ($elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated'])) {
182 $photo_timestamp = datetime_convert('UTC','UTC',$elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
183 $photo_url = $elems['link'][0]['attribs']['']['href'];
186 if(! $photo_timestamp) {
187 $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated');
188 if($photo_rawupdate) {
189 $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']);
190 $photo_url = $feed->get_image_url();
193 if(($photo_timestamp) && (strlen($photo_url)) && ($photo_timestamp > $contact['avatar-date'])) {
195 require_once("Photo.php");
196 $photo_failure = false;
198 $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d LIMIT 1",
199 intval($contact['id']),
200 intval($contact['uid'])
203 $resource_id = $r[0]['resource-id'];
204 $img_str = fetch_url($photo_url,true);
205 $img = new Photo($img_str);
207 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d AND `uid` = %d",
209 intval($contact['id']),
210 intval($contact['uid'])
213 $img->scaleImageSquare(175);
215 $hash = $resource_id;
216 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4);
218 $img->scaleImage(80);
219 $r = $img->store($contact['uid'], $contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5);
221 q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
222 dbesc(datetime_convert()),
223 intval($contact['uid']),
224 intval($contact['id'])
230 if(($name_updated) && (strlen($new_name)) && ($name_updated > $contact['name-date'])) {
231 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
232 dbesc(notags(trim($new_name))),
233 dbesc(datetime_convert()),
234 intval($contact['uid']),
235 intval($contact['id'])
239 // Now process the feed
240 if($feed->get_item_quantity()) {
241 foreach($feed->get_items() as $item) {
245 $rawdelete = $item->get_item_tags( NAMESPACE_TOMB, 'deleted-entry');
246 if(isset($rawdelete[0]['attribs']['']['ref'])) {
247 $uri = $rawthread[0]['attribs']['']['ref'];
249 if(isset($rawdelete[0]['attribs']['']['when'])) {
250 $when = $rawthread[0]['attribs']['']['when'];
251 $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s');
254 $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
257 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
259 intval($importer['uid'])
263 if($item['uri'] == $item['parent-uri']) {
264 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
265 `body` = '', `title` = ''
266 WHERE `parent-uri` = '%s' AND `uid` = %d",
268 dbesc(datetime_convert()),
270 intval($importer['uid'])
274 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',
275 `body` = '', `title` = ''
276 WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
278 dbesc(datetime_convert()),
280 intval($importer['uid'])
282 if($item['last-child']) {
283 // ensure that last-child is set in case the comment that had it just got wiped.
284 $q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
285 dbesc(datetime_convert()),
286 dbesc($item['parent-uri']),
289 // who is the last child now?
290 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d
291 ORDER BY `edited` DESC LIMIT 1",
292 dbesc($item['parent-uri']),
293 intval($importer['uid'])
296 q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
308 $item_id = $item->get_id();
309 $rawthread = $item->get_item_tags( NAMESPACE_THREAD,'in-reply-to');
310 if(isset($rawthread[0]['attribs']['']['ref'])) {
312 $parent_uri = $rawthread[0]['attribs']['']['ref'];
318 // Have we seen it? If not, import it.
320 $item_id = $item->get_id();
322 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
324 intval($importer['uid'])
326 // FIXME update content if 'updated' changes
328 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
329 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
330 $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
331 dbesc(datetime_convert()),
333 intval($importer['uid'])
335 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
336 intval($allow[0]['data']),
337 dbesc(datetime_convert()),
339 intval($importer['uid'])
346 $datarray = get_atom_elements($item);
347 $datarray['parent-uri'] = $parent_uri;
348 $datarray['uid'] = $importer['uid'];
349 $datarray['contact-id'] = $contact['id'];
350 if(($datarray['verb'] == ACTIVITY_LIKE) || ($datarray['verb'] == ACTIVITY_DISLIKE)) {
351 $datarray['type'] = 'activity';
352 $datarray['gravity'] = GRAVITY_LIKE;
355 $r = item_store($datarray);
360 // Head post of a conversation. Have we seen it? If not, import it.
362 $item_id = $item->get_id();
363 $r = q("SELECT `uid`, `last-child`, `edited` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
365 intval($importer['uid'])
368 $allow = $item->get_item_tags( NAMESPACE_DFRN, 'comment-allow');
369 if($allow && $allow[0]['data'] != $r[0]['last-child']) {
370 $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
371 intval($allow[0]['data']),
372 dbesc(datetime_convert()),
374 intval($importer['uid'])
380 $datarray = get_atom_elements($item);
381 $datarray['parent-uri'] = $item_id;
382 $datarray['uid'] = $importer['uid'];
383 $datarray['contact-id'] = $contact['id'];
384 $r = item_store($datarray);
390 $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1",
391 dbesc(datetime_convert()),
392 intval($contact['id'])