From ca2012366bfbe008a2177336d9b787d2bab43a3d Mon Sep 17 00:00:00 2001 From: Seb Sauvage Date: Mon, 19 Sep 2011 18:17:42 +0200 Subject: [PATCH] Version 0.0.13 beta --- index.php | 96 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 24 deletions(-) diff --git a/index.php b/index.php index 1423cafb..369f4926 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@ =5 && $minor>=1) return; // 5.1.x or higher is ok. - die('Your server supports php '.$ver.'. Shaarli requires at last php 5.1, and thus cannot run. Sorry.'); + header('Content-Type: text/plain; charset=utf-8'); + echo 'Your server supports php '.$ver.'. Shaarli requires at last php 5.1, and thus cannot run. Sorry.'; + exit; } // if cannot check php version... well, at your own risks. } @@ -516,8 +519,8 @@ class linkdb implements Iterator, Countable, ArrayAccess $tags=array(); foreach($this->links as $link) foreach(explode(' ',$link['tags']) as $tag) - if (!empty($tag)) $tags[$tag]=0; - ksort($tags); // FIXME: sort by usage ? That would be better. + if (!empty($tag)) $tags[$tag]=(empty($tags[$tag]) ? 1 : $tags[$tag]+1); + arsort($tags); // Sort tags by usage (most used tag first) return $tags; } @@ -528,16 +531,23 @@ class linkdb implements Iterator, Countable, ArrayAccess function showRSS() { global $LINKSDB; + + // Optionnaly filter the results: + $linksToDisplay=array(); + if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']); + elseif (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags($_GET['searchtags']); + else $linksToDisplay = $LINKSDB; + header('Content-Type: application/xhtml+xml; charset=utf-8'); $pageaddr=htmlspecialchars(serverUrl().$_SERVER["SCRIPT_NAME"]); echo ''; echo 'Shared links on '.$pageaddr.''.$pageaddr.''; echo 'Shared links'.$pageaddr.''."\n\n"; $i=0; - $keys=array(); foreach($LINKSDB as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys(). + $keys=array(); foreach($linksToDisplay as $key=>$value) { $keys[]=$key; } // No, I can't use array_keys(). while ($i<50 && $i'.htmlspecialchars($link['title']).''.htmlspecialchars($link['url']).''.htmlspecialchars($link['url']).''.htmlspecialchars($rfc822date).''; echo ''."\n"; @@ -579,6 +589,7 @@ function renderPage() // -------- User wants to logout. if (startswith($_SERVER["QUERY_STRING"],'do=logout')) { + invalidateCaches(); logout(); header('Location: ?'); exit; @@ -654,10 +665,10 @@ HTML; $pageabsaddr=serverUrl().$_SERVER["SCRIPT_NAME"]; // Why doesn't php have a built-in function for that ? // The javascript code for the bookmarklet: $toolbar= << - Import - Import Netscape html bookmarks (as exported from Firefox, Chrome, Opera, delicious...)
- Export - Export Netscape html bookmarks (which can be imported in Firefox, Chrome, Opera, delicious...)
- Shaare link - Drag this link to your bookmarks toolbar (or right-click it and choose Bookmark This Link....). Then click "Shaare link" button in any page you want to share. +

+ Import - Import Netscape html bookmarks (as exported from Firefox, Chrome, Opera, delicious...)

+ Export - Export Netscape html bookmarks (which can be imported in Firefox, Chrome, Opera, delicious...)

+ Shaare link - Drag this link to your bookmarks toolbar (or right-click it and choose Bookmark This Link....). Then click "Shaare link" button in any page you want to share.

HTML; $data = array('pageheader'=>$toolbar,'body'=>'','onload'=>''); @@ -685,6 +696,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 + invalidateCaches(); // If we are called from the bookmarklet, we must close the popup: if (isset($_GET['source']) && $_GET['source']=='bookmarklet') { echo ''; exit; } @@ -713,6 +725,7 @@ HTML; $linkdate=$_POST['lf_linkdate']; unset($LINKSDB[$linkdate]); $LINKSDB->savedb(); // save to disk + invalidateCaches(); // If we are called from the bookmarklet, we must close the popup: if (isset($_GET['source']) && $_GET['source']=='bookmarklet') { echo ''; exit; } $returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' ); @@ -766,9 +779,25 @@ HTML; // -------- Export as Netscape Bookmarks HTML file. if (startswith($_SERVER["QUERY_STRING"],'do=export')) - { + { + if (empty($_GET['what'])) + { + $toolbar= <<
+ Export all - Export all links

+ Export public - Export public links only

+ Export private - Export private links only

+ +HTML; + $data = array('pageheader'=>$toolbar,'body'=>'','onload'=>''); + templatePage($data); + exit; + } + $exportWhat=$_GET['what']; + if (!array_intersect(array('all','public','private'),array($exportWhat))) die('What are you trying to export ???'); + header('Content-Type: text/html; charset=utf-8'); - header('Content-disposition: attachment; filename=bookmarks_'.strval(date('Ymd_His')).'.html'); + header('Content-disposition: attachment; filename=bookmarks_'.$exportWhat.'_'.strval(date('Ymd_His')).'.html'); echo << \n"; + echo '\n"; exit; } @@ -817,7 +851,8 @@ Import Netscape html bookmarks (as exported from Firefox/Chrome/Opera/delicious/ - +
+  Import all links as private HTML; @@ -847,6 +882,7 @@ function importFile() $filename=$_FILES['filetoupload']['name']; $filesize=$_FILES['filetoupload']['size']; $data=file_get_contents($_FILES['filetoupload']['tmp_name']); + $private = (empty($_POST['private']) ? 0 : 1); // Should the links be imported as private ? // Sniff file type: $type='unknown'; @@ -876,11 +912,16 @@ function importFile() elseif ($attr=='PRIVATE') $link['private']=($value=='0'?0:1); elseif ($attr=='TAGS') $link['tags']=str_replace(',',' ',$value); } - if ($link['linkdate']!='' && $link['url']!='') $LINKSDB[$link['linkdate']] = $link; + if ($link['linkdate']!='' && $link['url']!='' && empty($LINKSDB[$link['linkdate']])) + { + if ($private==1) $link['private']=1; + $LINKSDB[$link['linkdate']] = $link; + } } } $import_count = count($LINKSDB)-$before; $LINKSDB->savedb(); + invalidateCaches(); echo ''; } else @@ -1200,6 +1241,13 @@ function processWS() } } +// Invalidate caches when the database is changed or the user logs out. +// (eg. tags cache). +function invalidateCaches() +{ + unset($_SESSION['tags']); +} + $LINKSDB=new linkdb(isLoggedIn() || OPEN_SHAARLI); // Read links from database (and filter private links if used it not logged in). if (startswith($_SERVER["QUERY_STRING"],'ws=')) { processWS(); exit; } // Webservices (for jQuery/jQueryUI) if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=LINKS_PER_PAGE; -- 2.41.0