$GLOBALS['config']['UPDATECHECK_BRANCH'] = 'stable';
$GLOBALS['config']['UPDATECHECK_INTERVAL'] = 86400;
+$GLOBALS['config']['REDIRECTOR_URLENCODE'] = true;
/*
* Plugin configuration
require_once 'application/LinkDB.php';
require_once 'application/LinkFilter.php';
require_once 'application/LinkUtils.php';
+require_once 'application/NetscapeBookmarkUtils.php';
require_once 'application/TimeZone.php';
require_once 'application/Url.php';
require_once 'application/Utils.php';
if (isset($_POST['returnurl'])) {
// Prevent loops over login screen.
if (strpos($_POST['returnurl'], 'do=login') === false) {
- header('Location: '. escape($_POST['returnurl']));
+ header('Location: '. generateLocation($_POST['returnurl'], $_SERVER['HTTP_HOST']));
exit;
}
}
$GLOBALS['config']['DATASTORE'],
isLoggedIn(),
$GLOBALS['config']['HIDE_PUBLIC_LINKS'],
- $GLOBALS['redirector']
+ $GLOBALS['redirector'],
+ $GLOBALS['config']['REDIRECTOR_URLENCODE']
);
/* Some Shaarlies may have very few links, so we need to look
* Show the 'Daily' page.
*
* @param PageBuilder $pageBuilder Template engine wrapper.
+ * @param LinkDB $LINKSDB LinkDB instance.
*/
-function showDaily($pageBuilder)
+function showDaily($pageBuilder, $LINKSDB)
{
- $LINKSDB = new LinkDB(
- $GLOBALS['config']['DATASTORE'],
- isLoggedIn(),
- $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
- $GLOBALS['redirector']
- );
-
$day=Date('Ymd',strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
if (isset($_GET['day'])) $day=$_GET['day'];
}
try {
- $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_DAY, $day);
+ $linksToDisplay = $LINKSDB->filterDay($day);
} catch (Exception $exc) {
error_log($exc);
$linksToDisplay = array();
$GLOBALS['config']['DATASTORE'],
isLoggedIn(),
$GLOBALS['config']['HIDE_PUBLIC_LINKS'],
- $GLOBALS['redirector']
+ $GLOBALS['redirector'],
+ $GLOBALS['config']['REDIRECTOR_URLENCODE']
);
$updater = new Updater(
if ($targetPage == Router::$PAGE_PICWALL)
{
// Optionally filter the results:
- $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
- $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
- if (! empty($searchtags) && ! empty($searchterm)) {
- $links = $LINKSDB->filter(
- LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
- array($searchtags, $searchterm)
- );
- }
- elseif ($searchtags) {
- $links = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $searchtags);
- }
- elseif ($searchterm) {
- $links = $LINKSDB->filter(LinkFilter::$FILTER_TEXT, $searchterm);
- }
- else {
- $links = $LINKSDB;
- }
-
+ $links = $LINKSDB->filterSearch($_GET);
$linksToDisplay = array();
// Get only links which have a thumbnail.
return strcasecmp($a, $b);
});
- $tagList=array();
- foreach($tags as $key=>$value)
- // Tag font size scaling: default 15 and 30 logarithm bases affect scaling, 22 and 6 are arbitrary font sizes for max and min sizes.
- {
- $tagList[$key] = array('count'=>$value,'size'=>log($value, 15) / log($maxcount, 30) * (22-6) + 6);
+ $tagList = array();
+ foreach($tags as $key => $value) {
+ // Tag font size scaling:
+ // default 15 and 30 logarithm bases affect scaling,
+ // 22 and 6 are arbitrary font sizes for max and min sizes.
+ $size = log($value, 15) / log($maxcount, 30) * 2.2 + 0.8;
+ $tagList[$key] = array(
+ 'count' => $value,
+ 'size' => number_format($size, 2, '.', ''),
+ );
}
$data = array(
// Daily page.
if ($targetPage == Router::$PAGE_DAILY) {
- showDaily($PAGE);
+ showDaily($PAGE, $LINKSDB);
}
// ATOM and RSS feed.
exit;
}
- // Same case as above except that user tried to access ?do=addlink without being logged in
- // Note: passing empty parameters makes Shaarli generate default URLs and descriptions.
- if (isset($_GET['do']) && $_GET['do'] === 'addlink') {
- header('Location: ?do=login&post=');
- exit;
- }
showLinkList($PAGE, $LINKSDB);
if (isset($_GET['edit_link'])) {
header('Location: ?do=login&edit_link='. escape($_GET['edit_link']));
{
if (!empty($_POST['title']) )
{
- if (!tokenOk($_POST['token'])) die('Wrong token.'); // Go away!
+ if (!tokenOk($_POST['token'])) {
+ die('Wrong token.'); // Go away!
+ }
$tz = 'UTC';
- if (!empty($_POST['continent']) && !empty($_POST['city']))
- if (isTimeZoneValid($_POST['continent'],$_POST['city']))
- $tz = $_POST['continent'].'/'.$_POST['city'];
+ if (!empty($_POST['continent']) && !empty($_POST['city'])
+ && isTimeZoneValid($_POST['continent'], $_POST['city'])
+ ) {
+ $tz = $_POST['continent'] . '/' . $_POST['city'];
+ }
$GLOBALS['timezone'] = $tz;
$GLOBALS['title']=$_POST['title'];
$GLOBALS['titleLink']=$_POST['titleLink'];
// Delete a tag:
if (isset($_POST['deletetag']) && !empty($_POST['fromtag'])) {
- $needle=trim($_POST['fromtag']);
+ $needle = trim($_POST['fromtag']);
// True for case-sensitive tag search.
- $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true);
+ $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
foreach($linksToAlter as $key=>$value)
{
$tags = explode(' ',trim($value['tags']));
// Rename a tag:
if (isset($_POST['renametag']) && !empty($_POST['fromtag']) && !empty($_POST['totag'])) {
- $needle=trim($_POST['fromtag']);
+ $needle = trim($_POST['fromtag']);
// True for case-sensitive tag search.
- $linksToAlter = $LINKSDB->filter(LinkFilter::$FILTER_TAG, $needle, true);
+ $linksToAlter = $LINKSDB->filterSearch(array('searchtags' => $needle), true);
foreach($linksToAlter as $key=>$value)
{
$tags = explode(' ',trim($value['tags']));
// -------- User want to post a new link: Display link edit form.
if (isset($_GET['post'])) {
- $url = cleanup_url(escape($_GET['post']));
+ $url = cleanup_url($_GET['post']);
$link_is_new = false;
// Check if URL is not already in database (in this case, we will edit the existing link)
// Extract title.
$title = html_extract_title($content);
// Re-encode title in utf-8 if necessary.
- if (! empty($title) && $charset != 'utf-8') {
- $title = mb_convert_encoding($title, $charset, 'utf-8');
+ if (! empty($title) && strtolower($charset) != 'utf-8') {
+ $title = mb_convert_encoding($title, 'utf-8', $charset);
}
}
}
$url = '?' . smallHash($linkdate);
$title = 'Note: ';
}
+ $url = escape($url);
+ $title = escape($title);
$link = array(
'linkdate' => $linkdate,
exit;
}
- // -------- Export as Netscape Bookmarks HTML file.
- if ($targetPage == Router::$PAGE_EXPORT)
- {
- if (empty($_GET['what']))
- {
+ if ($targetPage == Router::$PAGE_EXPORT) {
+ // Export links as a Netscape Bookmarks file
+
+ if (empty($_GET['selection'])) {
$PAGE->assign('linkcount',count($LINKSDB));
$PAGE->renderPage('export');
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_'.$exportWhat.'_'.strval(date('Ymd_His')).'.html');
- $currentdate=date('Y/m/d H:i:s');
- echo <<<HTML
-<!DOCTYPE NETSCAPE-Bookmark-file-1>
-<!-- This is an automatically generated file.
- It will be read and overwritten.
- DO NOT EDIT! -->
-<!-- Shaarli {$exportWhat} bookmarks export on {$currentdate} -->
-<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
-<TITLE>Bookmarks</TITLE>
-<H1>Bookmarks</H1>
-HTML;
- foreach($LINKSDB as $link)
- {
- if ($exportWhat=='all' ||
- ($exportWhat=='private' && $link['private']!=0) ||
- ($exportWhat=='public' && $link['private']==0))
- {
- $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
- echo '<DT><A HREF="'.$link['url'].'" ADD_DATE="'.$date->getTimestamp().'" PRIVATE="'.$link['private'].'"';
- if ($link['tags']!='') echo ' TAGS="'.str_replace(' ',',',$link['tags']).'"';
- echo '>'.$link['title']."</A>\n";
- if ($link['description']!='') echo '<DD>'.$link['description']."\n";
- }
+ // export as bookmarks_(all|private|public)_YYYYmmdd_HHMMSS.html
+ $selection = $_GET['selection'];
+ if (isset($_GET['prepend_note_url'])) {
+ $prependNoteUrl = $_GET['prepend_note_url'];
+ } else {
+ $prependNoteUrl = false;
}
- exit;
+
+ try {
+ $PAGE->assign(
+ 'links',
+ NetscapeBookmarkUtils::filterAndFormat(
+ $LINKSDB,
+ $selection,
+ $prependNoteUrl,
+ index_url($_SERVER)
+ )
+ );
+ } catch (Exception $exc) {
+ header('Content-Type: text/plain; charset=utf-8');
+ echo $exc->getMessage();
+ exit;
+ }
+ $now = new DateTime();
+ header('Content-Type: text/html; charset=utf-8');
+ header(
+ 'Content-disposition: attachment; filename=bookmarks_'
+ .$selection.'_'.$now->format(LinkDB::LINK_DATE_FORMAT).'.html'
+ );
+ $PAGE->assign('date', $now->format(DateTime::RFC822));
+ $PAGE->assign('eol', PHP_EOL);
+ $PAGE->assign('selection', $selection);
+ $PAGE->renderPage('export.bookmarks');
+ exit;
}
// -------- User is uploading a file for import
exit;
}
if (!tokenOk($_POST['token'])) die('Wrong token.');
- importFile();
+ importFile($LINKSDB);
exit;
}
// -----------------------------------------------------------------------------------------------
// Process the import file form.
-function importFile()
+function importFile($LINKSDB)
{
if (!isLoggedIn()) { die('Not allowed.'); }
- $LINKSDB = new LinkDB(
- $GLOBALS['config']['DATASTORE'],
- isLoggedIn(),
- $GLOBALS['config']['HIDE_PUBLIC_LINKS'],
- $GLOBALS['redirector']
- );
+
$filename=$_FILES['filetoupload']['name'];
$filesize=$_FILES['filetoupload']['size'];
$data=file_get_contents($_FILES['filetoupload']['tmp_name']);
}
}
-// -----------------------------------------------------------------------------------------------
-// Template for the list of links (<div id="linklist">)
-// This function fills all the necessary fields in the $PAGE for the template 'linklist.html'
+/**
+ * Template for the list of links (<div id="linklist">)
+ * This function fills all the necessary fields in the $PAGE for the template 'linklist.html'
+ *
+ * @param pageBuilder $PAGE pageBuilder instance.
+ * @param LinkDB $LINKSDB LinkDB instance.
+ */
function buildLinkList($PAGE,$LINKSDB)
{
- // Filter link database according to parameters.
+ // Used in templates
$searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
- $searchterm = !empty($_GET['searchterm']) ? escape(trim($_GET['searchterm'])) : '';
- $privateonly = !empty($_SESSION['privateonly']) ? true : false;
-
- // Search tags + fullsearch.
- if (! empty($searchtags) && ! empty($searchterm)) {
- $linksToDisplay = $LINKSDB->filter(
- LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
- array($searchtags, $searchterm),
- false,
- $privateonly
- );
- }
- // Search by tags.
- elseif (! empty($searchtags)) {
- $linksToDisplay = $LINKSDB->filter(
- LinkFilter::$FILTER_TAG,
- $searchtags,
- false,
- $privateonly
- );
- }
- // Fulltext search.
- elseif (! empty($searchterm)) {
- $linksToDisplay = $LINKSDB->filter(
- LinkFilter::$FILTER_TEXT,
- $searchterm,
- false,
- $privateonly
- );
- }
- // Detect smallHashes in URL.
- elseif (! empty($_SERVER['QUERY_STRING'])
- && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING'])
- ) {
- $linksToDisplay = $LINKSDB->filter(
- LinkFilter::$FILTER_HASH,
- substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6)
- );
+ $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
- if (count($linksToDisplay) == 0) {
- $PAGE->render404('The link you are trying to reach does not exist or has been deleted.');
+ // Smallhash filter
+ if (! empty($_SERVER['QUERY_STRING'])
+ && preg_match('/^[a-zA-Z0-9-_@]{6}($|&|#)/', $_SERVER['QUERY_STRING'])) {
+ try {
+ $linksToDisplay = $LINKSDB->filterHash($_SERVER['QUERY_STRING']);
+ } catch (LinkNotFoundException $e) {
+ $PAGE->render404($e->getMessage());
exit;
}
- }
- // Otherwise, display without filtering.
- else {
- $linksToDisplay = $LINKSDB->filter('', '', false, $privateonly);
+ } else {
+ // Filter links according search parameters.
+ $privateonly = !empty($_SESSION['privateonly']);
+ $linksToDisplay = $LINKSDB->filterSearch($_GET, false, $privateonly);
}
// ---- Handle paging.
if (!empty($_POST['setlogin']) && !empty($_POST['setpassword']))
{
$tz = 'UTC';
- if (!empty($_POST['continent']) && !empty($_POST['city'])) {
- if (isTimeZoneValid($_POST['continent'], $_POST['city'])) {
- $tz = $_POST['continent'].'/'.$_POST['city'];
- }
+ if (!empty($_POST['continent']) && !empty($_POST['city'])
+ && isTimeZoneValid($_POST['continent'], $_POST['city'])
+ ) {
+ $tz = $_POST['continent'].'/'.$_POST['city'];
}
$GLOBALS['timezone'] = $tz;
// Everything is ok, let's create config file.