open graph support for shaarli

This commit is contained in:
n 2017-02-11 15:42:02 +01:00
parent 11c67f9458
commit 26ed251b29

View file

@ -1,54 +1,55 @@
diff --git index.php index.php --- Shaarli/index.php 2017-01-20 16:47:36.000000000 +0100
index 40a6fbe..2e1117a 100644 +++ Shaarli.0.8.3/index.php 2017-02-11 15:37:50.327954000 +0100
--- index.php @@ -1821,10 +1821,13 @@
+++ index.php
@@ -2087,10 +2087,14 @@ function computeThumbnail($url,$href=false)
$path = parse_url($url,PHP_URL_PATH); $path = parse_url($url,PHP_URL_PATH);
if ("/talks/" !== substr($path,0,7)) return array(); // This is not a single video URL. if ("/talks/" !== substr($path,0,7)) return array(); // This is not a single video URL.
} }
+ } + }
$sign = hash_hmac('sha256', $url, $GLOBALS['salt']); // We use the salt to sign data (it's random, secret, and specific to each installation) $sign = hash_hmac('sha256', $url, $conf->get('credentials.salt')); // We use the salt to sign data (it's random, secret, and specific to each installation)
- return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url),
+ $thumbname=hash('sha1',$url).'.gif'; + $thumbname=hash('sha1',$url).'.gif';
+ $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname; + $filepath = $conf->get('resource.thumbnails_cache').'/'.$thumbname;
+ if (sha1_file($filepath) != '48d45d7ada69f9f858849bbd6e459f66c9694ee7') + if (sha1_file($filepath) != '48d45d7ada69f9f858849bbd6e459f66c9694ee7')
+ + return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url),
return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url),
'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail'); 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail');
- } - }
// For all other, we try to make a thumbnail of links ending with .jpg/jpeg/png/gif // For all other, we try to make a thumbnail of links ending with .jpg/jpeg/png/gif
// Technically speaking, we should download ALL links and check their Content-Type to see if they are images. // Technically speaking, we should download ALL links and check their Content-Type to see if they are images.
@@ -2400,18 +2404,30 @@ function genThumbnail() @@ -2154,18 +2157,32 @@
else else
{ {
// For all other domains, we try to download the image and make a thumbnail. // For all other domains, we try to download the image and make a thumbnail.
- // We allow 30 seconds max to download (and downloads are limited to 4 Mb) - // We allow 30 seconds max to download (and downloads are limited to 4 Mb)
- list($headers, $data) = get_http_url($url, 30); - list($headers, $content) = get_http_response($url, 30);
- if (strpos($headers[0], '200 OK') !== false) { - if (strpos($headers[0], '200 OK') !== false) {
- $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname; - $filepath = $cacheDir .'/'.$thumbname;
- file_put_contents($filepath,$data); // Save image to cache. - // Save image to cache.
- file_put_contents($filepath, $content);
- if (resizeImage($filepath)) - if (resizeImage($filepath))
- { - {
- header('Content-Type: image/jpeg'); - header('Content-Type: image/jpeg');
- echo file_get_contents($filepath); - echo file_get_contents($filepath);
- return; - return;
- } - }
- } + list($httpstatus,$headers,$data) = get_http_response($url,30); // We allow 30 seconds max to download (and downloads are limited to 4 Mb)
+ list($httpstatus,$headers,$data) = get_http_url($url,30); // We allow 30 seconds max to download (and downloads are limited to 4 Mb) + if (strpos($httpstatus,'200 OK')!==false)
+ {
+ // Extract the link to the thumbnail + // Extract the link to the thumbnail
+ $doc = new DOMDocument(); + $doc = new DOMDocument();
+ libxml_use_internal_errors(true); + libxml_use_internal_errors(true);
+ $doc->loadHTML($data); + $doc->loadHTML($headers);
+ foreach( $doc->getElementsByTagName('meta') as $meta ) + foreach( $doc->getElementsByTagName('meta') as $meta )
+ if ($meta->getAttribute('property') == 'og:image') + if ($meta->getAttribute('property') == 'og:image')
+ $imageurl=$meta->getAttribute('content'); + $imageurl=$meta->getAttribute('content');
+
+ if (!empty($imageurl)) + if (!empty($imageurl))
+ { // Let's download the image. + { // Let's download the image.
+ list($httpstatus,$headers,$data) = get_http_url($imageurl,20); // No control on image size, so wait long enough. + list($headers, $content) = get_http_response($imageurl, 20);
+ if (strpos($httpstatus,'200 OK')!==false) + if (strpos($httpstatus,'200 OK')!==false)
+ { + {
+ $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname; + $filepath = $conf->get('resource.thumbnails_cache').'/'.$thumbname;
+ file_put_contents($filepath,$data); // Save image to cache. + file_put_contents($filepath,$content); // Save image to cache.
+ if (resizeImage($filepath)) + if (resizeImage($filepath))
+ { + {
+ header('Content-Type: image/jpeg'); + header('Content-Type: image/jpeg');
@ -57,7 +58,6 @@ index 40a6fbe..2e1117a 100644
+ } + }
+ } + }
+ } + }
+ }
} }