diff --git a/include/api.php b/include/api.php index b9187ca3a..d98ad970d 100644 --- a/include/api.php +++ b/include/api.php @@ -1625,6 +1625,8 @@ if ($include_entities != "true") return array(); + $bbcode = bb_CleanPictureLinks($bbcode); + // Change pure links in text to bbcode uris $bbcode = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1[url=$2]$2[/url]', $bbcode); @@ -2392,6 +2394,8 @@ function api_get_nick($profile) { function api_clean_plain_items($Text) { $include_entities = strtolower(x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:"false"); + $Text = bb_CleanPictureLinks($Text); + if ($include_entities == "true") { $URLSearchString = "^\[\]"; diff --git a/include/bbcode.php b/include/bbcode.php index 186caad7b..7f70563c7 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -533,22 +533,93 @@ function GetProfileUsername($profile, $username) { return($username); } -function RemovePictureLinks($match) { - $ch = @curl_init($match[2]); - @curl_setopt($ch, CURLOPT_NOBODY, true); - @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)"); - @curl_exec($ch); - $curl_info = @curl_getinfo($ch); +function bb_RemovePictureLinks($match) { + $text = Cache::get($match[1]); - if (substr($curl_info["content_type"], 0, 6) == "image/") - $text = "[url=".$match[2]."]".$match[2]."[/url]"; - else - $text = "[url=".$match[1]."]".$match[1]."[/url]"; + if(is_null($text)){ + $ch = @curl_init($match[1]); + @curl_setopt($ch, CURLOPT_NOBODY, true); + @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)"); + @curl_exec($ch); + $curl_info = @curl_getinfo($ch); + if (substr($curl_info["content_type"], 0, 6) == "image/") + $text = "[url=".$match[1]."]".$match[1]."[/url]"; + else { + $text = "[url=".$match[2]."]".$match[2]."[/url]"; + + // if its not a picture then look if its a page that contains a picture link + require_once("include/network.php"); + + $body = fetch_url($match[1]); + + $doc = new DOMDocument(); + @$doc->loadHTML($body); + $xpath = new DomXPath($doc); + $list = $xpath->query("//meta[@name]"); + foreach ($list as $node) { + $attr = array(); + + if ($node->attributes->length) + foreach ($node->attributes as $attribute) + $attr[$attribute->name] = $attribute->value; + + if (strtolower($attr["name"]) == "twitter:image") + $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]"; + } + } + Cache::set($match[1],$text); + } return($text); } +function bb_CleanPictureLinksSub($match) { + $text = Cache::get($match[1]); + + if(is_null($text)){ + $ch = @curl_init($match[1]); + @curl_setopt($ch, CURLOPT_NOBODY, true); + @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)"); + @curl_exec($ch); + $curl_info = @curl_getinfo($ch); + + // if its a link to a picture then embed this picture + if (substr($curl_info["content_type"], 0, 6) == "image/") + $text = "[img]".$match[1]."[/img]"; + else { + $text = "[img]".$match[2]."[/img]"; + + // if its not a picture then look if its a page that contains a picture link + require_once("include/network.php"); + + $body = fetch_url($match[1]); + + $doc = new DOMDocument(); + @$doc->loadHTML($body); + $xpath = new DomXPath($doc); + $list = $xpath->query("//meta[@name]"); + foreach ($list as $node) { + $attr = array(); + + if ($node->attributes->length) + foreach ($node->attributes as $attribute) + $attr[$attribute->name] = $attribute->value; + + if (strtolower($attr["name"]) == "twitter:image") + $text = "[img]".$attr["content"]."[/img]"; + } + } + Cache::set($match[1],$text); + } + return($text); +} + +function bb_CleanPictureLinks($text) { + $text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_CleanPictureLinksSub', $text); + return ($text); +} // BBcode 2 HTML was written by WAY2WEB.net // extended to work with Mistpark/Friendica - Mike Macgirvin @@ -635,6 +706,10 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal // Set up the parameters for a MAIL search string $MAILSearchString = $URLSearchString; + // Bookmarks in red - will be converted to bookmarks in friendica + $Text = preg_replace("/#\^\[url\]([$URLSearchString]*)\[\/url\]/ism", '[bookmark=$1]$1[/bookmark]', $Text); + $Text = preg_replace("/#\^\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[bookmark=$1]$2[/bookmark]', $Text); + if ($simplehtml == 5) $Text = preg_replace("/[^#@]\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '[url]$1[/url]', $Text); @@ -652,7 +727,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal $Text = preg_replace("/([^\]\='".'"'."]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism", '$1$2', $Text); else { $Text = preg_replace("(\[url\]([$URLSearchString]*)\[\/url\])ism"," $1 ",$Text); - $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'RemovePictureLinks', $Text); + $Text = preg_replace_callback("&\[url=([^\[\]]*)\]\[img\](.*)\[\/img\]\[\/url\]&Usi", 'bb_RemovePictureLinks', $Text); } if ($tryoembed) diff --git a/include/cache.php b/include/cache.php index 360c4acbd..e6ea8f045 100644 --- a/include/cache.php +++ b/include/cache.php @@ -2,29 +2,42 @@ /** * cache api */ - + class Cache { - public static function get($key){ + public static function get($key) { + if (function_exists("apc_fetch") AND function_exists("apc_exists")) + if (apc_exists($key)) + return(apc_fetch($key)); + $r = q("SELECT `v` FROM `cache` WHERE `k`='%s' limit 1", dbesc($key) ); - - if (count($r)) return $r[0]['v']; + + if (count($r)) { + if (function_exists("apc_store")) + apc_store($key, $r[0]['v'], 600); + + return $r[0]['v']; + } return null; } - + public static function set($key,$value) { q("REPLACE INTO `cache` (`k`,`v`,`updated`) VALUES ('%s','%s','%s')", dbesc($key), dbesc($value), dbesc(datetime_convert())); + + if (function_exists("apc_store")) + apc_store($key, $value, 600); + } - -/* + +/* * - * Leaving this legacy code temporaily to see how REPLACE fares + * Leaving this legacy code temporaily to see how REPLACE fares * as opposed to non-atomic checks when faced with fast moving key duplication. * As a MySQL extension it isn't portable, but we're not yet very portable. */ @@ -48,11 +61,11 @@ * } */ - + public static function clear(){ q("DELETE FROM `cache` WHERE `updated` < '%s'", - dbesc(datetime_convert('UTC','UTC',"now - 30 days"))); + dbesc(datetime_convert('UTC','UTC',"now - 30 days"))); } - + } - + diff --git a/mod/display.php b/mod/display.php index 75e51d5a9..116b4a17e 100644 --- a/mod/display.php +++ b/mod/display.php @@ -203,6 +203,10 @@ function display_content(&$a, $update = 0) { $title = trim(html2plain(bbcode($r[0]["title"], false, false), 0, true)); $author_name = $r[0]["author-name"]; + $image = ""; + if ($image == "") + $image = $r[0]["thumb"]; + if ($title == "") $title = $author_name; @@ -219,9 +223,16 @@ function display_content(&$a, $update = 0) { // Schema.org microdata $a->page['htmlhead'] .= ''."\n"; $a->page['htmlhead'] .= ''."\n"; - $a->page['htmlhead'] .= ''."\n"; + $a->page['htmlhead'] .= ''."\n"; $a->page['htmlhead'] .= ''."\n"; + // Twitter cards + $a->page['htmlhead'] .= ''."\n"; + $a->page['htmlhead'] .= ''."\n"; + $a->page['htmlhead'] .= ''."\n"; + $a->page['htmlhead'] .= ''."\n"; + $a->page['htmlhead'] .= ''."\n"; + // Dublin Core $a->page['htmlhead'] .= ''."\n"; $a->page['htmlhead'] .= ''."\n"; @@ -229,7 +240,7 @@ function display_content(&$a, $update = 0) { // Open Graph $a->page['htmlhead'] .= ''."\n"; $a->page['htmlhead'] .= ''."\n"; - $a->page['htmlhead'] .= ''."\n"; + $a->page['htmlhead'] .= ''."\n"; $a->page['htmlhead'] .= ''."\n"; $a->page['htmlhead'] .= ''."\n"; $a->page['htmlhead'] .= ''."\n"; diff --git a/mod/parse_url.php b/mod/parse_url.php index 66ad1e57c..52083dfbc 100644 --- a/mod/parse_url.php +++ b/mod/parse_url.php @@ -52,6 +52,9 @@ function completeurl($url, $scheme) { function parseurl_getsiteinfo($url, $no_guessing = false) { $siteinfo = array(); + + $siteinfo["type"] = "link"; + $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); @@ -165,6 +168,18 @@ function parseurl_getsiteinfo($url, $no_guessing = false) { case "description": $siteinfo["text"] = $attr["content"]; break; + case "twitter:image": + $siteinfo["image"] = $attr["content"]; + break; + case "twitter:card": + $siteinfo["type"] = $attr["content"]; + break; + case "twitter:description": + $siteinfo["text"] = $attr["content"]; + break; + case "twitter:title": + $siteinfo["title"] = $attr["content"]; + break; case "dc.title": $siteinfo["title"] = $attr["content"]; break; @@ -311,9 +326,9 @@ function parse_url_content(&$a) { logger('parse_url: ' . $url); if($textmode) - $template = '[bookmark=%s]%s[/bookmark]%s' . $br; + $template = '[bookmark=%s]%s[/bookmark]%s'; else - $template = "%s%s
"; + $template = "%s%s"; $arr = array('url' => $url, 'text' => ''); @@ -386,9 +401,10 @@ function parse_url_content(&$a) { $text = '
'.htmlspecialchars(trim($text)).'
'; } - if($image) { + if($image) $text = $br.$br.$image.$text; - } + else + $text = $br.$text; $title = str_replace(array("\r","\n"),array('',''),$title); @@ -398,7 +414,10 @@ function parse_url_content(&$a) { $sitedata .= trim($result); - echo "[class=type-link]".$sitedata."[/class]"; + if (($siteinfo["type"] != "photo")) + echo "[class=type-link]".$sitedata."[/class]"; + else + echo "[class=type-photo]".$title.$br.$image."[/class]"; killme(); } diff --git a/mod/photos.php b/mod/photos.php index 79a51fd4c..dac6dae9b 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -49,7 +49,7 @@ function photos_init(&$a) { if($albums_visible) { $o .= '