]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - index.php
Added: Support for TED Talks (ted.com/talks) thumbnails
[github/shaarli/Shaarli.git] / index.php
index 01dae52b3aa3ffd505d6f5b29b3ce96bd9382537..d9692927ca292f41a43323ab6713b732b633a7df 100644 (file)
--- a/index.php
+++ b/index.php
@@ -1,5 +1,5 @@
 <?php
-// Shaarli 0.0.29 beta - Shaare your links...
+// Shaarli 0.0.30 beta - Shaare your links...
 // The personal, minimalist, super-fast, no-database delicious clone. By sebsauvage.net
 // http://sebsauvage.net/wiki/doku.php?id=php:shaarli
 // Licence: http://www.opensource.org/licenses/zlib-license.php
@@ -20,7 +20,7 @@ $GLOBALS['config']['HIDE_TIMESTAMPS'] = false; // If true, the moment when links
 $GLOBALS['config']['ENABLE_THUMBNAILS'] = true; // Enable thumbnails in links.
 $GLOBALS['config']['CACHEDIR'] = 'cache'; // Cache directory for thumbnails for SLOW services (like flickr)
 $GLOBALS['config']['ENABLE_LOCALCACHE'] = true; // Enable Shaarli to store thumbnail in a local cache. Disable to reduce webspace usage.
-$GLOBALS['config']['PUBSUBHUB_URL'] = ''; // PubSubHub support. Put an empty string to disable, or put your hub url here to enable.
+$GLOBALS['config']['PUBSUBHUB_URL'] = ''; // PubSubHubbub support. Put an empty string to disable, or put your hub url here to enable.
                                           // Note: You must have publisher.php in the same directory as Shaarli index.php   
 // -----------------------------------------------------------------------------------------------
 // Program config (touch at your own risks !)
@@ -54,7 +54,7 @@ header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 header("Cache-Control: no-store, no-cache, must-revalidate");
 header("Cache-Control: post-check=0, pre-check=0", false);
 header("Pragma: no-cache");
-define('shaarli_version','0.0.29 beta');
+define('shaarli_version','0.0.30 beta');
 if (!is_dir($GLOBALS['config']['DATADIR'])) { mkdir($GLOBALS['config']['DATADIR'],0705); chmod($GLOBALS['config']['DATADIR'],0705); }
 if (!is_file($GLOBALS['config']['DATADIR'].'/.htaccess')) { file_put_contents($GLOBALS['config']['DATADIR'].'/.htaccess',"Allow from none\nDeny from all\n"); } // Protect data files.    
 if ($GLOBALS['config']['ENABLE_LOCALCACHE'])
@@ -151,7 +151,7 @@ function autoLocale()
 }
 
 // ------------------------------------------------------------------------------------------
-// PubSubHub protocol support (if enabled)  [UNTESTED]
+// PubSubHubbub protocol support (if enabled)  [UNTESTED]
 // (Source: http://aldarone.fr/les-flux-rss-shaarli-et-pubsubhubbub/ )
 if (!empty($GLOBALS['config']['PUBSUBHUB_URL'])) include './publisher.php';
 function pubsubhub()
@@ -1025,7 +1025,6 @@ HTML;
                 $LINKSDB[$key]=$value;
             }
             $LINKSDB->savedb(); // save to disk
-            pubsubhub();
             invalidateCaches();
             echo '<script language="JavaScript">alert("Tag was removed from '.count($linksToAlter).' links.");document.location=\'?\';</script>';
             exit;
@@ -1071,6 +1070,7 @@ HTML;
         if ($link['title']=='') $link['title']=$link['url']; // If title is empty, use the URL as title.
         $LINKSDB[$linkdate] = $link;
         $LINKSDB->savedb(); // save to disk
+        pubsubhub();
         invalidateCaches();
         
         // If we are called from the bookmarklet, we must close the popup:
@@ -1406,6 +1406,7 @@ function templateLinkList()
     $i = ($page-1)*$_SESSION['LINKS_PER_PAGE']; // Start index.
     $end = $i+$_SESSION['LINKS_PER_PAGE'];  
     $redir = empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector']; // optional redirector URL
+    $token = ''; if (isLoggedIn()) $token=getToken();
     
     while ($i<$end && $i<count($keys))
     {  
@@ -1414,7 +1415,12 @@ function templateLinkList()
         $title=$link['title'];
         $classLi =  $i%2!=0 ? '' : 'class="publicLinkHightLight"';
         $classprivate = ($link['private']==0 ? $classLi : 'class="private"');
-        if (isLoggedIn()) $actions=' <form method="GET" class="buttoneditform"><input type="hidden" name="edit_link" value="'.$link['linkdate'].'"><input type="submit" value="Edit" class="smallbutton"></form>';
+        if (isLoggedIn())
+        {
+            $actions=' <form method="GET" class="buttoneditform"><input type="hidden" name="edit_link" value="'.$link['linkdate'].'"><input type="submit" value="Edit" class="smallbutton"></form>';
+            $actions.=' <form method="POST" class="buttoneditform"><input type="hidden" name="lf_linkdate" value="'.$link['linkdate'].'">';
+            $actions.='<input type="hidden" name="token" value="'.$token.'"><input type="submit" value="Delete" name="delete_link" class="smallbutton" onClick="return confirmDeleteLink();"></form>';
+        }
         $tags='';
         if ($link['tags']!='')
         {
@@ -1509,13 +1515,21 @@ function thumbnail($url,$href=false)
     
     if (!$GLOBALS['config']['ENABLE_LOCALCACHE']) return ''; // 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')
+    if ($domain=='flickr.com' || endsWith($domain,'.flickr.com')
+        || $domain=='vimeo.com'
+        || $domain=='ted.com' || endsWith($domain,'.ted.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 ''; // This is not a single video URL.
        }
+       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 ''; // 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)
         return '<a href="'.htmlspecialchars($href).'"><img src="?do=genthumbnail&hmac='.htmlspecialchars($sign).'&url='.urlencode($url).'" width="120" style="height:auto;"></a>';
     }
@@ -1875,6 +1889,35 @@ function genThumbnail()
             }           
         }  
     }
+
+    if ($domain=='ted.com' || endsWith($domain,'.ted.com'))
+    {
+        // The thumbnail for TED talks is located in the <link rel="image_src" [...]> tag on that page
+        // http://www.ted.com/talks/mikko_hypponen_fighting_viruses_defending_the_net.html
+        // <link rel="image_src" href="http://images.ted.com/images/ted/28bced335898ba54d4441809c5b1112ffaf36781_389x292.jpg" />
+        list($httpstatus,$headers,$data) = getHTTP($url,5);
+        if (strpos($httpstatus,'200 OK')!==false)
+        {
+            // Extract the link to the thumbnail
+            preg_match('!link rel="image_src" href="(http://images.ted.com/images/ted/.+_\d+x\d+\.jpg)[^s]!',$data,$matches);
+            if (!empty($matches[1]))
+            {   // Let's download the image.
+                $imageurl=$matches[1];
+                list($httpstatus,$headers,$data) = getHTTP($imageurl,20); // No control on image size, so wait long enough.
+                if (strpos($httpstatus,'200 OK')!==false)
+                {
+                    $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname;
+                    file_put_contents($filepath,$data); // Save image to cache.
+                    if (resizeImage($filepath))
+                    {
+                        header('Content-Type: image/jpeg');
+                        echo file_get_contents($filepath);  
+                        return;
+                    }
+                }
+            }
+        }
+    }
     
     // For all other domains, we try to download the image and make a thumbnail.
     list($httpstatus,$headers,$data) = getHTTP($url,30);  // We allow 30 seconds max to download (and downloads are limited to 4 Mb)
@@ -1944,4 +1987,4 @@ if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['c
 if (startswith($_SERVER["QUERY_STRING"],'do=rss')) { showRSS(); exit; }
 if (startswith($_SERVER["QUERY_STRING"],'do=atom')) { showATOM(); exit; }
 renderPage();
-?>
\ No newline at end of file
+?>