X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=index.php;h=8e04fa3ef5724667056f022ef67bf17df77eee60;hb=d7efade5d651ec60a05a86baa53f99188ad5d72c;hp=2c731e9f85a3ff82e603c74d929f0f6a0938a122;hpb=5ac5349ac053b1e560b136c62f8c764fd3230039;p=github%2Fshaarli%2FShaarli.git diff --git a/index.php b/index.php index 2c731e9f..8e04fa3e 100755 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@ /shaarli/ define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0))); @@ -70,8 +70,11 @@ if (is_file($GLOBALS['config']['CONFIG_FILE'])) { } // Shaarli library +require_once 'application/Cache.php'; +require_once 'application/CachedPage.php'; require_once 'application/LinkDB.php'; require_once 'application/TimeZone.php'; +require_once 'application/Url.php'; require_once 'application/Utils.php'; require_once 'application/Config.php'; @@ -202,63 +205,6 @@ function checkUpdate() } -// ----------------------------------------------------------------------------------------------- -// Simple cache system (mainly for the RSS/ATOM feeds). - -class pageCache -{ - private $url; // Full URL of the page to cache (typically the value returned by pageUrl()) - private $shouldBeCached; // boolean: Should this url be cached? - private $filename; // Name of the cache file for this url. - - /* - $url = URL (typically the value returned by pageUrl()) - $shouldBeCached = boolean. If false, the cache will be disabled. - */ - public function __construct($url,$shouldBeCached) - { - $this->url = $url; - $this->filename = $GLOBALS['config']['PAGECACHE'].'/'.sha1($url).'.cache'; - $this->shouldBeCached = $shouldBeCached; - } - - // If the page should be cached and a cached version exists, - // returns the cached version (otherwise, return null). - public function cachedVersion() - { - if (!$this->shouldBeCached) return null; - if (is_file($this->filename)) { return file_get_contents($this->filename); exit; } - return null; - } - - // Put a page in the cache. - public function cache($page) - { - if (!$this->shouldBeCached) return; - file_put_contents($this->filename,$page); - } - - // Purge the whole cache. - // (call with pageCache::purgeCache()) - public static function purgeCache() - { - if (is_dir($GLOBALS['config']['PAGECACHE'])) - { - $handler = opendir($GLOBALS['config']['PAGECACHE']); - if ($handler!==false) - { - while (($filename = readdir($handler))!==false) - { - if (endsWith($filename,'.cache')) { unlink($GLOBALS['config']['PAGECACHE'].'/'.$filename); } - } - closedir($handler); - } - } - } - -} - - // ----------------------------------------------------------------------------------------------- // Log to text file function logm($message) @@ -718,8 +664,16 @@ function showRSS() // Cache system $query = $_SERVER["QUERY_STRING"]; - $cache = new pageCache(pageUrl(),startsWith($query,'do=rss') && !isLoggedIn()); - $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } + $cache = new CachedPage( + $GLOBALS['config']['PAGECACHE'], + pageUrl(), + startsWith($query,'do=rss') && !isLoggedIn() + ); + $cached = $cache->cachedVersion(); + if (! empty($cached)) { + echo $cached; + exit; + } // If cached was not found (or not usable), then read the database and build the response: $LINKSDB = new LinkDB( @@ -798,11 +752,19 @@ function showATOM() // Cache system $query = $_SERVER["QUERY_STRING"]; - $cache = new pageCache(pageUrl(),startsWith($query,'do=atom') && !isLoggedIn()); - $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; exit; } - // If cached was not found (or not usable), then read the database and build the response: + $cache = new CachedPage( + $GLOBALS['config']['PAGECACHE'], + pageUrl(), + startsWith($query,'do=atom') && !isLoggedIn() + ); + $cached = $cache->cachedVersion(); + if (!empty($cached)) { + echo $cached; + exit; + } -// Read links from database (and filter private links if used it not logged in). + // If cached was not found (or not usable), then read the database and build the response: + // Read links from database (and filter private links if used it not logged in). $LINKSDB = new LinkDB( $GLOBALS['config']['DATASTORE'], isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], @@ -884,7 +846,11 @@ function showATOM() function showDailyRSS() { // Cache system $query = $_SERVER["QUERY_STRING"]; - $cache = new pageCache(pageUrl(), startsWith($query, 'do=dailyrss') && !isLoggedIn()); + $cache = new CachedPage( + $GLOBALS['config']['PAGECACHE'], + pageUrl(), + startsWith($query,'do=dailyrss') && !isLoggedIn() + ); $cached = $cache->cachedVersion(); if (!empty($cached)) { echo $cached; @@ -1076,7 +1042,7 @@ function renderPage() // -------- User wants to logout. if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=logout')) { - invalidateCaches(); + invalidateCaches($GLOBALS['config']['PAGECACHE']); logout(); header('Location: ?'); exit; @@ -1383,7 +1349,7 @@ function renderPage() $value['tags']=trim(implode(' ',$tags)); $LINKSDB[$key]=$value; } - $LINKSDB->savedb(); // Save to disk. + $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. echo ''; exit; } @@ -1400,7 +1366,7 @@ function renderPage() $value['tags']=trim(implode(' ',$tags)); $LINKSDB[$key]=$value; } - $LINKSDB->savedb(); // Save to disk. + $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. echo ''; exit; } @@ -1429,7 +1395,7 @@ function renderPage() 'linkdate'=>$linkdate,'tags'=>str_replace(',',' ',$tags)); if ($link['title']=='') $link['title']=$link['url']; // If title is empty, use the URL as title. $LINKSDB[$linkdate] = $link; - $LINKSDB->savedb(); // Save to disk. + $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // Save to disk. pubsubhub(); // If we are called from the bookmarklet, we must close the popup: @@ -1462,7 +1428,7 @@ function renderPage() // - we are protected from XSRF by the token. $linkdate=$_POST['lf_linkdate']; unset($LINKSDB[$linkdate]); - $LINKSDB->savedb(); // save to disk + $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); // save to disk // If we are called from the bookmarklet, we must close the popup: if (isset($_GET['source']) && ($_GET['source']=='bookmarklet' || $_GET['source']=='firefoxsocialapi')) { echo ''; exit; } @@ -1514,29 +1480,9 @@ function renderPage() } // -------- User want to post a new link: Display link edit form. - if (isset($_GET['post'])) - { - $url=$_GET['post']; - - // We remove the annoying parameters added by FeedBurner, GoogleFeedProxy, Facebook... - $annoyingpatterns = array('/[\?&]utm_source=[^&]*/', - '/[\?&]utm_campaign=[^&]*/', - '/[\?&]utm_medium=[^&]*/', - '/#xtor=RSS-[^&]*/', - '/[\?&]fb_[^&]*/', - '/[\?&]__scoop[^&]*/', - '/#tk\.rss_all\?/', - '/[\?&]action_ref_map=[^&]*/', - '/[\?&]action_type_map=[^&]*/', - '/[\?&]action_object_map=[^&]*/', - '/[\?&]utm_content=[^&]*/', - '/[\?&]fb=[^&]*/', - '/[\?&]xtor=[^&]*/' - ); - foreach($annoyingpatterns as $pattern) - { - $url = preg_replace($pattern, "", $url); - } + if (isset($_GET['post'])) { + $url = new Url($_GET['post']); + $url->cleanup(); $link_is_new = false; $link = $LINKSDB->getLinkFromUrl($url); // Check if URL is not already in database (in this case, we will edit the existing link) @@ -1751,7 +1697,7 @@ function importFile() } } } - $LINKSDB->savedb(); + $LINKSDB->savedb($GLOBALS['config']['PAGECACHE']); echo ''; } @@ -2386,14 +2332,6 @@ function resizeImage($filepath) return true; } -// Invalidate caches when the database is changed or the user logs out. -// (e.g. tags cache). -function invalidateCaches() -{ - unset($_SESSION['tags']); // Purge cache attached to session. - pageCache::purgeCache(); // Purge page cache shared by sessions. -} - try { mergeDeprecatedConfig($GLOBALS, isLoggedIn()); } catch(Exception $e) {