-/**
- * Compute the thumbnail for a link.
- *
- * With a link to the original URL.
- * Understands various services (youtube.com...)
- * Input: $url = URL for which the thumbnail must be found.
- * $href = if provided, this URL will be followed instead of $url
- * Returns an associative array with thumbnail attributes (src,href,width,height,style,alt)
- * Some of them may be missing.
- * Return an empty array if no thumbnail available.
- *
- * @param ConfigManager $conf Configuration Manager instance.
- * @param string $url
- * @param string|bool $href
- *
- * @return array
- */
-function computeThumbnail($conf, $url, $href = false)
-{
- if (!$conf->get('thumbnail.enable_thumbnails')) return array();
- if ($href==false) $href=$url;
-
- // For most hosts, the URL of the thumbnail can be easily deduced from the URL of the link.
- // (e.g. http://www.youtube.com/watch?v=spVypYk4kto ---> http://img.youtube.com/vi/spVypYk4kto/default.jpg )
- // ^^^^^^^^^^^ ^^^^^^^^^^^
- $domain = parse_url($url,PHP_URL_HOST);
- if ($domain=='youtube.com' || $domain=='www.youtube.com')
- {
- parse_str(parse_url($url,PHP_URL_QUERY), $params); // Extract video ID and get thumbnail
- if (!empty($params['v'])) return array('src'=>'https://img.youtube.com/vi/'.$params['v'].'/default.jpg',
- 'href'=>$href,'width'=>'120','height'=>'90','alt'=>'YouTube thumbnail');
- }
- if ($domain=='youtu.be') // Youtube short links
- {
- $path = parse_url($url,PHP_URL_PATH);
- return array('src'=>'https://img.youtube.com/vi'.$path.'/default.jpg',
- 'href'=>$href,'width'=>'120','height'=>'90','alt'=>'YouTube thumbnail');
- }
- if ($domain=='pix.toile-libre.org') // pix.toile-libre.org image hosting
- {
- parse_str(parse_url($url,PHP_URL_QUERY), $params); // Extract image filename.
- if (!empty($params) && !empty($params['img'])) return array('src'=>'http://pix.toile-libre.org/upload/thumb/'.urlencode($params['img']),
- 'href'=>$href,'style'=>'max-width:120px; max-height:150px','alt'=>'pix.toile-libre.org thumbnail');
- }
-
- if ($domain=='imgur.com')
- {
- $path = parse_url($url,PHP_URL_PATH);
- if (startsWith($path,'/a/')) return array(); // Thumbnails for albums are not available.
- if (startsWith($path,'/r/')) return array('src'=>'https://i.imgur.com/'.basename($path).'s.jpg',
- 'href'=>$href,'width'=>'90','height'=>'90','alt'=>'imgur.com thumbnail');
- if (startsWith($path,'/gallery/')) return array('src'=>'https://i.imgur.com'.substr($path,8).'s.jpg',
- 'href'=>$href,'width'=>'90','height'=>'90','alt'=>'imgur.com thumbnail');
-
- if (substr_count($path,'/')==1) return array('src'=>'https://i.imgur.com/'.substr($path,1).'s.jpg',
- 'href'=>$href,'width'=>'90','height'=>'90','alt'=>'imgur.com thumbnail');
- }
- if ($domain=='i.imgur.com')
- {
- $pi = pathinfo(parse_url($url,PHP_URL_PATH));
- if (!empty($pi['filename'])) return array('src'=>'https://i.imgur.com/'.$pi['filename'].'s.jpg',
- 'href'=>$href,'width'=>'90','height'=>'90','alt'=>'imgur.com thumbnail');
- }
- if ($domain=='dailymotion.com' || $domain=='www.dailymotion.com')
- {
- if (strpos($url,'dailymotion.com/video/')!==false)
- {
- $thumburl=str_replace('dailymotion.com/video/','dailymotion.com/thumbnail/video/',$url);
- return array('src'=>$thumburl,
- 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'DailyMotion thumbnail');
- }
- }
- if (endsWith($domain,'.imageshack.us'))
- {
- $ext=strtolower(pathinfo($url,PATHINFO_EXTENSION));
- if ($ext=='jpg' || $ext=='jpeg' || $ext=='png' || $ext=='gif')
- {
- $thumburl = substr($url,0,strlen($url)-strlen($ext)).'th.'.$ext;
- return array('src'=>$thumburl,
- 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'imageshack.us thumbnail');
- }
- }
-
- // Some other hosts are SLOW AS HELL and usually require an extra HTTP request to get the thumbnail URL.
- // So we deport the thumbnail generation in order not to slow down page generation
- // (and we also cache the thumbnail)
-
- if (! $conf->get('thumbnail.enable_localcache')) return array(); // If local cache is disabled, no thumbnails for services which require the use a local cache.
-
- if ($domain=='flickr.com' || endsWith($domain,'.flickr.com')
- || $domain=='vimeo.com'
- || $domain=='ted.com' || endsWith($domain,'.ted.com')
- || $domain=='xkcd.com' || endsWith($domain,'.xkcd.com')
- )
- {
- if ($domain=='vimeo.com')
- { // Make sure this vimeo URL points to a video (/xxx... where xxx is numeric)
- $path = parse_url($url,PHP_URL_PATH);
- if (!preg_match('!/\d+.+?!',$path)) return array(); // This is not a single video URL.
- }
- if ($domain=='xkcd.com' || endsWith($domain,'.xkcd.com'))
- { // Make sure this URL points to a single comic (/xxx... where xxx is numeric)
- $path = parse_url($url,PHP_URL_PATH);
- if (!preg_match('!/\d+.+?!',$path)) return array();
- }
- if ($domain=='ted.com' || endsWith($domain,'.ted.com'))
- { // Make sure this TED URL points to a video (/talks/...)
- $path = parse_url($url,PHP_URL_PATH);
- if ("/talks/" !== substr($path,0,7)) return array(); // This is not a single video URL.
- }
- $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),
- '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
- // Technically speaking, we should download ALL links and check their Content-Type to see if they are images.
- // But using the extension will do.
- $ext=strtolower(pathinfo($url,PATHINFO_EXTENSION));
- if ($ext=='jpg' || $ext=='jpeg' || $ext=='png' || $ext=='gif')
- {
- $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),
- 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail');
- }
- return array(); // No thumbnail.
-
-}
-
-
-// Returns the HTML code to display a thumbnail for a link
-// with a link to the original URL.
-// Understands various services (youtube.com...)
-// Input: $url = URL for which the thumbnail must be found.
-// $href = if provided, this URL will be followed instead of $url
-// Returns '' if no thumbnail available.
-function thumbnail($url,$href=false)
-{
- // FIXME!
- global $conf;
- $t = computeThumbnail($conf, $url,$href);
- if (count($t)==0) return ''; // Empty array = no thumbnail for this URL.
-
- $html='<a href="'.escape($t['href']).'"><img src="'.escape($t['src']).'"';
- if (!empty($t['width'])) $html.=' width="'.escape($t['width']).'"';
- if (!empty($t['height'])) $html.=' height="'.escape($t['height']).'"';
- if (!empty($t['style'])) $html.=' style="'.escape($t['style']).'"';
- if (!empty($t['alt'])) $html.=' alt="'.escape($t['alt']).'"';
- $html.='></a>';
- return $html;
-}
-
-// Returns the HTML code to display a thumbnail for a link
-// for the picture wall (using lazy image loading)
-// Understands various services (youtube.com...)
-// Input: $url = URL for which the thumbnail must be found.
-// $href = if provided, this URL will be followed instead of $url
-// Returns '' if no thumbnail available.
-function lazyThumbnail($conf, $url,$href=false)
-{
- // FIXME!
- global $conf;
- $t = computeThumbnail($conf, $url,$href);
- if (count($t)==0) return ''; // Empty array = no thumbnail for this URL.
-
- $html='<a href="'.escape($t['href']).'">';
-
- // Lazy image
- $html.='<img class="b-lazy" src="#" data-src="'.escape($t['src']).'"';
-
- if (!empty($t['width'])) $html.=' width="'.escape($t['width']).'"';
- if (!empty($t['height'])) $html.=' height="'.escape($t['height']).'"';
- if (!empty($t['style'])) $html.=' style="'.escape($t['style']).'"';
- if (!empty($t['alt'])) $html.=' alt="'.escape($t['alt']).'"';
- $html.='>';
-
- // No-JavaScript fallback.
- $html.='<noscript><img src="'.escape($t['src']).'"';
- if (!empty($t['width'])) $html.=' width="'.escape($t['width']).'"';
- if (!empty($t['height'])) $html.=' height="'.escape($t['height']).'"';
- if (!empty($t['style'])) $html.=' style="'.escape($t['style']).'"';
- if (!empty($t['alt'])) $html.=' alt="'.escape($t['alt']).'"';
- $html.='></noscript></a>';
-
- return $html;
-}
-
-