From bb8f712db66f8e572eba4b14f636aeeace7cd9e7 Mon Sep 17 00:00:00 2001 From: Knah Tsaeb Date: Mon, 4 Mar 2013 10:18:39 +0100 Subject: [add] https://github.com/sebsauvage/Shaarli/issues/20 New links created as private by default. --- index.php | 117 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 60 insertions(+), 57 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index 222f3295..0b4d454d 100644 --- a/index.php +++ b/index.php @@ -97,6 +97,7 @@ if (empty($GLOBALS['title'])) $GLOBALS['title']='Shared links on '.htmlspecialch if (empty($GLOBALS['timezone'])) $GLOBALS['timezone']=date_default_timezone_get(); if (empty($GLOBALS['disablesessionprotection'])) $GLOBALS['disablesessionprotection']=false; if (empty($GLOBALS['disablejquery'])) $GLOBALS['disablejquery']=false; +if (empty($GLOBALS['privateLinkByDefault'])) $GLOBALS['privateLinkByDefault']=false; // I really need to rewrite Shaarli with a proper configuation manager. autoLocale(); // Sniff browser language and set date format accordingly. @@ -146,7 +147,7 @@ class pageCache 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. */ @@ -155,7 +156,7 @@ class pageCache $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). @@ -183,7 +184,7 @@ class pageCache $handler = opendir($GLOBALS['config']['PAGECACHE']); if ($handler!==false) { - while (($filename = readdir($handler))!==false) + while (($filename = readdir($handler))!==false) { if (endsWith($filename,'.cache')) { unlink($GLOBALS['config']['PAGECACHE'].'/'.$filename); } } @@ -240,7 +241,7 @@ function text2clickable($url) function keepMultipleSpaces($text) { return str_replace(' ','  ',$text); - + } // ------------------------------------------------------------------------------------------ // Sniff browser language to display dates in the right format automatically. @@ -306,7 +307,7 @@ function check_auth($login,$password) function isLoggedIn() { if ($GLOBALS['config']['OPEN_SHAARLI']) return true; - + if (!isset($GLOBALS['login'])) return false; // Shaarli is not configured yet. // If session does not exist on server side, or IP address has changed, or session has expired, logout. @@ -606,7 +607,7 @@ function tokenOk($token) p = new pageBuilder; p.assign('myfield','myvalue'); p.renderPage('mytemplate'); - + */ class pageBuilder { @@ -615,11 +616,11 @@ class pageBuilder function __construct() { $this->tpl=false; - } + } private function initialize() - { - $this->tpl = new RainTPL; + { + $this->tpl = new RainTPL; $this->tpl->assign('newversion',checkUpdate()); $this->tpl->assign('feedurl',htmlspecialchars(indexUrl())); $searchcrits=''; // Search criteria @@ -634,16 +635,16 @@ class pageBuilder if (!empty($GLOBALS['title'])) $this->tpl->assign('pagetitle',$GLOBALS['title']); if (!empty($GLOBALS['pagetitle'])) $this->tpl->assign('pagetitle',$GLOBALS['pagetitle']); $this->tpl->assign('shaarlititle',empty($GLOBALS['title']) ? 'Shaarli': $GLOBALS['title'] ); - return; + return; } - + // The following assign() method is basically the same as RainTPL (except that it's lazy) public function assign($what,$where) { if ($this->tpl===false) $this->initialize(); // Lazy initialization $this->tpl->assign($what,$where); } - + // Render a specific page (using a template). // eg. pb.renderPage('picwall') public function renderPage($page) @@ -661,14 +662,14 @@ class pageBuilder echo $mylinks['20110826_161819']['title']; foreach($mylinks as $link) echo $link['title'].' at url '.$link['url'].' ; description:'.$link['description']; - + Available keys: title : Title of the link url : URL of the link. Can be absolute or relative. Relative URLs are permalinks (eg.'?m-ukcw') description : description of the entry private : Is this link private ? 0=no, other value=yes linkdate : date of the creation of this entry, in the form YYYYMMDD_HHMMSS (eg.'20110914_192317') - tags : tags attached to this entry (separated by spaces) + tags : tags attached to this entry (separated by spaces) We implement 3 interfaces: - ArrayAccess so that this object behaves like an associative array. @@ -847,7 +848,7 @@ class linkdb implements Iterator, Countable, ArrayAccess arsort($tags); // Sort tags by usage (most used tag first) return $tags; } - + // Returns the list of days containing articles (oldest first) // Output: An array containing days (in format YYYYMMDD). public function days() @@ -1007,7 +1008,7 @@ function showATOM() $feed.=$entries; $feed.=''; echo $feed; - + $cache->cache(ob_get_contents()); ob_end_flush(); exit; @@ -1025,11 +1026,11 @@ function showDailyRSS() $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(isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI']); // Read links from database (and filter private links if used it not logged in). - + /* Some Shaarlies may have very few links, so we need to look back in time (rsort()) until we have enough days ($nb_of_days). */ - $linkdates=array(); foreach($LINKSDB as $linkdate=>$value) { $linkdates[]=$linkdate; } + $linkdates=array(); foreach($LINKSDB as $linkdate=>$value) { $linkdates[]=$linkdate; } rsort($linkdates); $nb_of_days=7; // We take 7 days. $today=Date('Ymd'); @@ -1044,14 +1045,14 @@ function showDailyRSS() } if (count($days)>$nb_of_days) break; // Have we collected enough days ? } - + // Build the RSS feed. header('Content-Type: application/rss+xml; charset=utf-8'); $pageaddr=htmlspecialchars(indexUrl()); echo ''; echo 'Daily - '.htmlspecialchars($GLOBALS['title']).''.$pageaddr.''; echo 'Daily shared linksen-en'.$pageaddr.''."\n"; - + foreach($days as $day=>$linkdates) // For each day. { $daydate = utf8_encode(strftime('%A %d, %B %Y',linkdate2timestamp($day.'_000000'))); // Full text date @@ -1059,7 +1060,7 @@ function showDailyRSS() $absurl=htmlspecialchars(indexUrl().'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. echo ''.htmlspecialchars($GLOBALS['title'].' - '.$daydate).''.$absurl.''.$absurl.''; echo ''.htmlspecialchars($rfc822date).""; - + // Build the HTML body of this RSS entry. $html=''; $href=''; @@ -1069,21 +1070,21 @@ function showDailyRSS() { $l = $LINKSDB[$linkdate]; $l['formatedDescription']=nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($l['description'])))); - $l['thumbnail'] = thumbnail($l['url']); - $l['localdate']=linkdate2locale($l['linkdate']); + $l['thumbnail'] = thumbnail($l['url']); + $l['localdate']=linkdate2locale($l['linkdate']); if (startsWith($l['url'],'?')) $l['url']=indexUrl().$l['url']; // make permalink URL absolute - $links[$linkdate]=$l; + $links[$linkdate]=$l; } // Then build the HTML for this day: - $tpl = new RainTPL; + $tpl = new RainTPL; $tpl->assign('links',$links); $html = $tpl->draw('dailyrss',$return_string=true); echo "\n"; echo ''."\n\n\n"; - } + } echo ''; - + $cache->cache(ob_get_contents()); ob_end_flush(); exit; @@ -1097,12 +1098,12 @@ function showDaily() $day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD. if (isset($_GET['day'])) $day=$_GET['day']; - + $days = $LINKSDB->days(); $i = array_search($day,$days); if ($i==false) { $i=count($days)-1; $day=$days[$i]; } - $previousday=''; - $nextday=''; + $previousday=''; + $nextday=''; if ($i!==false) { if ($i>1) $previousday=$days[$i-1]; @@ -1117,12 +1118,12 @@ function showDaily() uasort($taglist, 'strcasecmp'); $linksToDisplay[$key]['taglist']=$taglist; $linksToDisplay[$key]['formatedDescription']=nl2br(keepMultipleSpaces(text2clickable(htmlspecialchars($link['description'])))); - $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); + $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); } - + /* We need to spread the articles on 3 columns. I did not want to use a javascript lib like http://masonry.desandro.com/ - so I manually spread entries with a simple method: I roughly evaluate the + so I manually spread entries with a simple method: I roughly evaluate the height of a div according to title and description length. */ $columns=array(array(),array(),array()); // Entries to display, for each column. @@ -1150,7 +1151,7 @@ function showDaily() $PAGE->assign('col3',$columns[2]); $PAGE->assign('day',utf8_encode(strftime('%A %d, %B %Y',linkdate2timestamp($day.'_000000')))); $PAGE->assign('previousday',$previousday); - $PAGE->assign('nextday',$nextday); + $PAGE->assign('nextday',$nextday); $PAGE->renderPage('daily'); exit; } @@ -1229,7 +1230,7 @@ function renderPage() $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('tags',$tagList); $PAGE->renderPage('tagcloud'); - exit; + exit; } // -------- User clicks on a tag in a link: The tag is added to the list of searched tags (searchtags=...) @@ -1272,7 +1273,7 @@ function renderPage() header('Location: '.$referer); exit; } - + // -------- User wants to see only private links (toggle) if (isset($_GET['privateonly'])) { @@ -1363,6 +1364,7 @@ function renderPage() $GLOBALS['redirector']=$_POST['redirector']; $GLOBALS['disablesessionprotection']=!empty($_POST['disablesessionprotection']); $GLOBALS['disablejquery']=!empty($_POST['disablejquery']); + $GLOBALS['privateLinkByDefault']=!empty($_POST['privateLinkByDefault']); writeConfig(); echo ''; exit; @@ -1740,7 +1742,7 @@ function buildLinkList($PAGE,$LINKSDB) } else $linksToDisplay = $LINKSDB; // otherwise, display without filtering. - + // Option: Show only private links if (!empty($_SESSION['privateonly'])) { @@ -1785,7 +1787,7 @@ function buildLinkList($PAGE,$LINKSDB) $linkDisp[$keys[$i]] = $link; $i++; } - + // Compute paging navigation $searchterm= ( empty($_GET['searchterm']) ? '' : '&searchterm='.$_GET['searchterm'] ); $searchtags= ( empty($_GET['searchtags']) ? '' : '&searchtags='.$_GET['searchtags'] ); @@ -1793,8 +1795,8 @@ function buildLinkList($PAGE,$LINKSDB) $previous_page_url=''; if ($i!=count($keys)) $previous_page_url='?page='.($page+1).$searchterm.$searchtags; $next_page_url='';if ($page>1) $next_page_url='?page='.($page-1).$searchterm.$searchtags; - $token = ''; if (isLoggedIn()) $token=getToken(); - + $token = ''; if (isLoggedIn()) $token=getToken(); + // Fill all template fields. $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('previous_page_url',$previous_page_url); @@ -1803,7 +1805,7 @@ function buildLinkList($PAGE,$LINKSDB) $PAGE->assign('page_max',$pagecount); $PAGE->assign('result_count',count($linksToDisplay)); $PAGE->assign('search_type',$search_type); - $PAGE->assign('search_crits',$search_crits); + $PAGE->assign('search_crits',$search_crits); $PAGE->assign('redirector',empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector']); // optional redirector URL $PAGE->assign('token',$token); $PAGE->assign('links',$linkDisp); @@ -1811,7 +1813,7 @@ function buildLinkList($PAGE,$LINKSDB) } // 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. @@ -1838,15 +1840,15 @@ function computeThumbnail($url,$href=false) { $path = parse_url($url,PHP_URL_PATH); return array('src'=>'http://img.youtube.com/vi'.$path.'/default.jpg', - 'href'=>$href,'width'=>'120','height'=>'90','alt'=>'YouTube thumbnail'); + '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'); - } - + '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); @@ -1925,7 +1927,7 @@ function computeThumbnail($url,$href=false) { $sign = hash_hmac('sha256', $url, $GLOBALS['salt']); // We use the salt to sign data (it's random, secret, and specific to each installation) return array('src'=>indexUrl().'?do=genthumbnail&hmac='.htmlspecialchars($sign).'&url='.urlencode($url), - 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail'); + 'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail'); } return array(); // No thumbnail. @@ -1942,7 +1944,7 @@ function thumbnail($url,$href=false) { $t = computeThumbnail($url,$href); if (count($t)==0) return ''; // Empty array = no thumbnail for this URL. - + $html=''; - + // Lazy image (only loaded by javascript when in the viewport). if (!empty($GLOBALS['disablejquery'])) // (except if jQuery is disabled) $html.=''; - + $PAGE = new pageBuilder; $PAGE->assign('timezone_html',$timezone_html); $PAGE->assign('timezone_js',$timezone_js); @@ -2163,6 +2165,7 @@ function writeConfig() $config .= '$GLOBALS[\'redirector\']='.var_export($GLOBALS['redirector'],true).'; '; $config .= '$GLOBALS[\'disablesessionprotection\']='.var_export($GLOBALS['disablesessionprotection'],true).'; '; $config .= '$GLOBALS[\'disablejquery\']='.var_export($GLOBALS['disablejquery'],true).'; '; + $config .= '$GLOBALS[\'privateLinkByDefault\']='.var_export($GLOBALS['privateLinkByDefault'],true).'; '; $config .= ' ?>'; if (!file_put_contents($GLOBALS['config']['CONFIG_FILE'],$config) || strcmp(file_get_contents($GLOBALS['config']['CONFIG_FILE']),$config)!=0) { @@ -2279,7 +2282,7 @@ function genThumbnail() // The thumbnail for TED talks is located in the tag on that page // http://www.ted.com/talks/mikko_hypponen_fighting_viruses_defending_the_net.html // - list($httpstatus,$headers,$data) = getHTTP($url,5); + list($httpstatus,$headers,$data) = getHTTP($url,5); if (strpos($httpstatus,'200 OK')!==false) { // Extract the link to the thumbnail @@ -2302,7 +2305,7 @@ function genThumbnail() } } } - + elseif ($domain=='xkcd.com' || endsWith($domain,'.xkcd.com')) { // There is no thumbnail available for xkcd comics, so download the whole image and resize it. @@ -2330,7 +2333,7 @@ function genThumbnail() } } } - } + } else { @@ -2402,7 +2405,7 @@ if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=g if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=rss')) { showRSS(); exit; } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=atom')) { showATOM(); exit; } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=dailyrss')) { showDailyRSS(); exit; } -if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=daily')) { showDaily(); exit; } +if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=daily')) { showDaily(); exit; } if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'ws=')) { processWS(); exit; } // Webservices (for jQuery/jQueryUI) if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE']; renderPage(); -- cgit v1.2.3