From 9043e704f87ecfd6788b7acdf9d6797fd2f89f61 Mon Sep 17 00:00:00 2001 From: Friendika Date: Tue, 18 Oct 2011 15:56:35 -0700 Subject: [PATCH] diaspora reshares --- include/bb2diaspora.php | 3 + include/diaspora.php | 139 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) diff --git a/include/bb2diaspora.php b/include/bb2diaspora.php index bdf278213c..ef5477f1b4 100644 --- a/include/bb2diaspora.php +++ b/include/bb2diaspora.php @@ -11,6 +11,9 @@ function diaspora2bb($s) { $s = preg_replace('/\@\{(.+?)\; (.+?)\@(.+?)\}/','@[url=https://$3/u/$2]$1[/url]',$s); $s = Markdown($s); $s = html2bbcode($s); + $s = preg_replace("/\[url\=?(.*?)\]https?:\/\/www.youtube.com\/watch\?v\=(.*?)\[\/url\]/ism",'[youtube]$2[/youtube]',$s); + $s = preg_replace("/\[url\=?(.*?)\]https?:\/\/vimeo.com\/([0-9]+)(.*?)\[\/url\]/ism",'[vimeo]$2[/vimeo]',$s); + return $s; } diff --git a/include/diaspora.php b/include/diaspora.php index 52b25c5701..9c27f4f559 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -58,6 +58,9 @@ function diaspora_dispatch($importer,$msg) { elseif($xmlbase->asphoto) { $ret = diaspora_asphoto($importer,$xmlbase->asphoto); } + elseif($xmlbase->reshare) { + $ret = diaspora_reshare($importer,$xmlbase->reshare); + } elseif($xmlbase->retraction) { $ret = diaspora_retraction($importer,$xmlbase->retraction,$msg); } @@ -580,6 +583,142 @@ function diaspora_post($importer,$xml) { } +function diaspora_reshare($importer,$xml) { + + $a = get_app(); + $guid = notags(unxmlify($xml->guid)); + $diaspora_handle = notags(unxmlify($xml->diaspora_handle)); + + $contact = diaspora_get_contact_by_handle($importer['uid'],$diaspora_handle); + if(! $contact) + return; + + if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) { + logger('diaspora_reshare: Ignoring this author.'); + return 202; + } + + $message_id = $diaspora_handle . ':' . $guid; + $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1", + intval($importer['uid']), + dbesc($message_id), + dbesc($guid) + ); + if(count($r)) { + logger('diaspora_reshare: message exists: ' . $guid); + return; + } + + $orig_author = notags(unxmlify($xml->root_diaspora_id)); + $orig_guid = notags(unxmlify($xml->root_guid)); + + $source_url = 'https://' . substr($orig_author,strpos($orig_author,'@')+1) . '/p/' . $orig_guid . '.xml'; + $x = fetch_url($source_url); + if(! $x) + $x = fetch_url(str_replace('https://','http://',$source_url)); + if(! $x) { + logger('diaspora_reshare: unable to fetch source url ' . $source_url); + return; + } + $x = str_replace(array('',''),array('',''),$x); + $source_xml = parse_xml_string($x,true); + + if(strlen($source_xml->asphoto->objectId) && ($source_xml->asphoto->objectId != 0) && ($source_xml->asphoto->image_url)) + $body = '[url=' . notags(unxmlify($source_xml->asphoto->image_url)) . '][img=' . notags(unxmlify($source_xml->asphoto->objectId)) . '][/img][/url]' . "\n"; + elseif($source_xml->asphoto->image_url) + $body = '[img=' . notags(unxmlify($source_xml->asphoto->image_url)) . '][/img]' . "\n"; + elseif($source_xml->status_message) { + $body = diaspora2bb($source_xml->status_message->raw_message); + } + else { + logger('diaspora_reshare: no reshare content found.'); + return; + } + if(! $body) { + logger('diaspora_reshare: empty body: source= ' . $x); + return; + } + + $person = find_diaspora_person_by_handle($orig_author); + + if(is_array($person) && x($person,'name') && x($person,'url')) + $details = '[url=' . $person['url'] . ']' . $person['name'] . '[/url]'; + else + $details = $orig_author; + + $prefix = '♲ ' . $details . "\n"; + + + // allocate a guid on our system - we aren't fixing any collisions. + // we're ignoring them + + $g = q("select * from guid where guid = '%s' limit 1", + dbesc($guid) + ); + if(! count($g)) { + q("insert into guid ( guid ) values ( '%s' )", + dbesc($guid) + ); + } + + $created = unxmlify($xml->created_at); + $private = ((unxmlify($xml->public) == 'false') ? 1 : 0); + + $body = diaspora2bb($xml->raw_message); + + $datarray = array(); + + $str_tags = ''; + + $tags = get_tags($body); + + if(count($tags)) { + foreach($tags as $tag) { + if(strpos($tag,'#') === 0) { + if(strpos($tag,'[url=')) + continue; + $basetag = str_replace('_',' ',substr($tag,1)); + $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body); + if(strlen($str_tags)) + $str_tags .= ','; + $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; + continue; + } + } + } + + $datarray['uid'] = $importer['uid']; + $datarray['contact-id'] = $contact['id']; + $datarray['wall'] = 0; + $datarray['guid'] = $guid; + $datarray['uri'] = $datarray['parent-uri'] = $message_id; + $datarray['created'] = $datarray['edited'] = datetime_convert('UTC','UTC',$created); + $datarray['private'] = $private; + $datarray['parent'] = 0; + $datarray['owner-name'] = $contact['name']; + $datarray['owner-link'] = $contact['url']; + $datarray['owner-avatar'] = $contact['thumb']; + $datarray['author-name'] = $contact['name']; + $datarray['author-link'] = $contact['url']; + $datarray['author-avatar'] = $contact['thumb']; + $datarray['body'] = $prefix . $body; + $datarray['tag'] = $str_tags; + $datarray['app'] = 'Diaspora'; + + $message_id = item_store($datarray); + + if($message_id) { + q("update item set plink = '%s' where id = %d limit 1", + dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id), + intval($message_id) + ); + } + + return; + +} + + function diaspora_asphoto($importer,$xml) { logger('diaspora_asphoto called');