+/* Because some f*cking services like Flickr require an extra HTTP request to get the thumbnail URL,
+ I have deported the thumbnail URL code generation here, otherwise this would slow down page generation.
+ The following function takes the URL a link (eg. a flickr page) and return the proper thumbnail.
+ This function is called by passing the url:
+ http://mywebsite.com/shaarli/?do=genthumbnail&hmac=[HMAC]&url=[URL]
+ [URL] is the URL of the link (eg. a flickr page)
+ [HMAC] is the signature for the [URL] (so that these URL cannot be forged).
+ The function below will fetch the image from the webservice and store it in the cache.
+*/
+function genThumbnail()
+{
+ // Make sure the parameters in the URL were generated by us.
+ $sign = hash_hmac('sha256', $_GET['url'], $GLOBALS['salt']);
+ if ($sign!=$_GET['hmac']) die('Naughty boy !');
+
+ // Let's see if we don't already have the image for this URL in the cache.
+ $thumbname=hash('sha1',$_GET['url']).'.jpg';
+ if (is_file(CACHEDIR.'/'.$thumbname))
+ { // We have the thumbnail, just serve it:
+ header('Content-Type: image/jpeg');
+ echo file_get_contents(CACHEDIR.'/'.$thumbname);
+ return;
+ }
+ // We may also serve a blank image (if service did not respond)
+ $blankname=hash('sha1',$_GET['url']).'.gif';
+ if (is_file(CACHEDIR.'/'.$blankname))
+ {
+ header('Content-Type: image/gif');
+ echo file_get_contents(CACHEDIR.'/'.$blankname);
+ return;
+ }
+
+ // Otherwise, generate the thumbnail.
+ $url = $_GET['url'];
+ $domain = parse_url($url,PHP_URL_HOST);
+
+ if ($domain=='flickr.com' || endsWith($domain,'.flickr.com'))
+ {
+ // WTF ? I need a flickr API key to get a fucking thumbnail ? No way.
+ // I'll extract the thumbnail URL myself. First, we have to get the flickr HTML page.
+ // All images in Flickr are in the form:
+ // http://farm[farm].static.flickr.com/[server]/[id]_[secret]_[size].jpg
+ // Example: http://farm7.static.flickr.com/6205/6088513739_fc158467fe_z.jpg
+ // We want the 240x120 format, which is _m.jpg.
+ // We search for the first image in the page which does not have the _s size,
+ // when use the _m to get the thumbnail.
+
+ // Is this a link to an image, or to a flickr page ?
+ $imageurl='';
+ logm('url: '.$url);
+ if (endswith(parse_url($url,PHP_URL_PATH),'.jpg'))
+ { // This is a direct link to an image. eg. http://farm1.static.flickr.com/5/5921913_ac83ed27bd_o.jpg
+ preg_match('!(http://farm\d+.static.flickr.com/\d+/\d+_\w+_)\w.jpg!',$url,$matches);
+ if (!empty($matches[1])) $imageurl=$matches[1].'m.jpg';
+ }
+ else // this is a flickr page (html)
+ {
+ list($httpstatus,$headers,$data) = getHTTP($url,20); // Get the flickr html page.
+ if (strpos($httpstatus,'200 OK'))
+ {
+ preg_match('!(http://farm\d+.static.flickr.com/\d+/\d+_\w+_)[^s].jpg!',$data,$matches);
+ if (!empty($matches[1])) $imageurl=$matches[1].'m.jpg';
+ }
+ }
+ if ($imageurl!='')
+ { // Let's download the image.
+ list($httpstatus,$headers,$data) = getHTTP($imageurl,10); // Image is 240x120, so 10 seconds to download should be enough.
+ if (strpos($httpstatus,'200 OK'))
+ {
+ file_put_contents(CACHEDIR.'/'.$thumbname,$data); // Save image to cache.
+ header('Content-Type: image/jpeg');
+ echo $data;
+ return;
+ }
+ }
+ else logm('unkown flickr url: '.$url);
+ }
+
+ if ($domain=='vimeo.com' )
+ {
+ // This is more complex: we have to perform a HTTP request, then parse the result.
+ // Maybe we should deport this to javascript ? Example: http://stackoverflow.com/questions/1361149/get-img-thumbnails-from-vimeo/4285098#4285098
+ $vid = substr(parse_url($url,PHP_URL_PATH),1);
+ list($httpstatus,$headers,$data) = getHTTP('http://vimeo.com/api/v2/video/'.htmlspecialchars($vid).'.php',5);
+ if (strpos($httpstatus,'200 OK'))
+ {
+ $t = unserialize($data);
+ $imageurl = $t[0]['thumbnail_medium'];
+ // Then we download the image and serve it to our client.
+ list($httpstatus,$headers,$data) = getHTTP($imageurl,10);
+ if (strpos($httpstatus,'200 OK'))
+ {
+ file_put_contents(CACHEDIR.'/'.$thumbname,$data); // Save image to cache.
+ header('Content-Type: image/jpeg');
+ echo $data;
+ return;
+ }
+ }
+ }
+
+ // Otherwise, return an empty image (8x8 transparent gif)
+ $blankgif = base64_decode('R0lGODlhCAAIAIAAAP///////yH5BAEKAAEALAAAAAAIAAgAAAIHjI+py+1dAAA7');
+ file_put_contents(CACHEDIR.'/'.$blankname,$blankgif); // Also put something in cache so that this URL is not requested twice.
+ header('Content-Type: image/gif');
+ echo $blankgif;
+}
+