)
-// This function fills all the necessary fields in the $PAGE for the template 'linklist.html'
+/**
+ * Template for the list of links (
)
+ * 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
- $search_type = '';
- $search_crits = '';
- $privateonly = !empty($_SESSION['privateonly']) ? true : false;
-
- // Fulltext search
- if (isset($_GET['searchterm'])) {
- $search_crits = escape(trim($_GET['searchterm']));
- $search_type = LinkFilter::$FILTER_TEXT;
- $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly);
- }
- // Search by tag
- elseif (isset($_GET['searchtags'])) {
- $search_crits = explode(' ', escape(trim($_GET['searchtags'])));
- $search_type = LinkFilter::$FILTER_TAG;
- $linksToDisplay = $LINKSDB->filter($search_type, $search_crits, false, $privateonly);
- }
- // Detect smallHashes in URL.
- elseif (isset($_SERVER['QUERY_STRING'])
- && preg_match('/[a-zA-Z0-9-_@]{6}(&.+?)?/', $_SERVER['QUERY_STRING'])) {
- $search_type = LinkFilter::$FILTER_HASH;
- $search_crits = substr(trim($_SERVER["QUERY_STRING"], '/'), 0, 6);
- $linksToDisplay = $LINKSDB->filter($search_type, $search_crits);
-
- if (count($linksToDisplay) == 0) {
- $PAGE->render404('The link you are trying to reach does not exist or has been deleted.');
+ $conf = ConfigManager::getInstance();
+ // Used in templates
+ $searchtags = !empty($_GET['searchtags']) ? escape($_GET['searchtags']) : '';
+ $searchterm = !empty($_GET['searchterm']) ? escape($_GET['searchterm']) : '';
+
+ // 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.
@@ -2023,7 +1627,7 @@ function buildLinkList($PAGE,$LINKSDB)
// If there is only a single link, we change on-the-fly the title of the page.
if (count($linksToDisplay) == 1) {
- $GLOBALS['pagetitle'] = $linksToDisplay[$keys[0]]['title'].' - '.$GLOBALS['title'];
+ $conf->set('pagetitle', $linksToDisplay[$keys[0]]['title'] .' - '. $conf->get('general.title'));
}
// Select articles according to paging.
@@ -2039,10 +1643,11 @@ function buildLinkList($PAGE,$LINKSDB)
while ($i<$end && $iget('extras.redirector'));
$classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
$link['class'] = $link['private'] == 0 ? $classLi : 'private';
- $link['timestamp'] = linkdate2timestamp($link['linkdate']);
+ $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
+ $link['timestamp'] = $date->getTimestamp();
$taglist = explode(' ', $link['tags']);
uasort($taglist, 'strcasecmp');
$link['taglist'] = $taglist;
@@ -2058,40 +1663,36 @@ function buildLinkList($PAGE,$LINKSDB)
}
// Compute paging navigation
- $searchterm = empty($_GET['searchterm']) ? '' : '&searchterm=' . $_GET['searchterm'];
- $searchtags = empty($_GET['searchtags']) ? '' : '&searchtags=' . $_GET['searchtags'];
+ $searchtagsUrl = empty($searchtags) ? '' : '&searchtags=' . urlencode($searchtags);
+ $searchtermUrl = empty($searchterm) ? '' : '&searchterm=' . urlencode($searchterm);
$previous_page_url = '';
if ($i != count($keys)) {
- $previous_page_url = '?page=' . ($page+1) . $searchterm . $searchtags;
+ $previous_page_url = '?page=' . ($page+1) . $searchtermUrl . $searchtagsUrl;
}
$next_page_url='';
if ($page>1) {
- $next_page_url = '?page=' . ($page-1) . $searchterm . $searchtags;
+ $next_page_url = '?page=' . ($page-1) . $searchtermUrl . $searchtagsUrl;
}
- $token = '';
- if (isLoggedIn()) {
- $token = getToken();
- }
+ $token = isLoggedIn() ? getToken() : '';
// Fill all template fields.
$data = array(
- 'linkcount' => count($LINKSDB),
'previous_page_url' => $previous_page_url,
'next_page_url' => $next_page_url,
'page_current' => $page,
'page_max' => $pagecount,
'result_count' => count($linksToDisplay),
- 'search_type' => $search_type,
- 'search_crits' => $search_crits,
- 'redirector' => empty($GLOBALS['redirector']) ? '' : $GLOBALS['redirector'], // Optional redirector URL.
+ 'search_term' => $searchterm,
+ 'search_tags' => $searchtags,
+ 'redirector' => $conf->get('extras.redirector'), // Optional redirector URL.
'token' => $token,
'links' => $linkDisp,
'tags' => $LINKSDB->allTags(),
);
// FIXME! temporary fix - see #399.
- if (!empty($GLOBALS['pagetitle']) && count($linkDisp) == 1) {
- $data['pagetitle'] = $GLOBALS['pagetitle'];
+ if ($conf->exists('pagetitle') && count($linkDisp) == 1) {
+ $data['pagetitle'] = $conf->get('pagetitle');
}
$pluginManager = PluginManager::getInstance();
@@ -2115,7 +1716,8 @@ function buildLinkList($PAGE,$LINKSDB)
// Return an empty array if no thumbnail available.
function computeThumbnail($url,$href=false)
{
- if (!$GLOBALS['config']['ENABLE_THUMBNAILS']) return array();
+ $conf = ConfigManager::getInstance();
+ if (!$conf->get('general.enable_thumbnails')) return array();
if ($href==false) $href=$url;
// For most hosts, the URL of the thumbnail can be easily deduced from the URL of the link.
@@ -2183,7 +1785,7 @@ function computeThumbnail($url,$href=false)
// So we deport the thumbnail generation in order not to slow down page generation
// (and we also cache the thumbnail)
- if (!$GLOBALS['config']['ENABLE_LOCALCACHE']) return array(); // If local cache is disabled, no thumbnails for services which require the use a local cache.
+ if (! $conf->get('general.enable_localcache')) return array(); // 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'
@@ -2206,7 +1808,7 @@ function computeThumbnail($url,$href=false)
$path = parse_url($url,PHP_URL_PATH);
if ("/talks/" !== substr($path,0,7)) return array(); // 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)
+ $sign = hash_hmac('sha256', $url, $conf->get('credentials.salt')); // We use the salt to sign data (it's random, secret, and specific to each installation)
return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url),
'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail');
}
@@ -2217,7 +1819,7 @@ function computeThumbnail($url,$href=false)
$ext=strtolower(pathinfo($url,PATHINFO_EXTENSION));
if ($ext=='jpg' || $ext=='jpeg' || $ext=='png' || $ext=='gif')
{
- $sign = hash_hmac('sha256', $url, $GLOBALS['salt']); // We use the salt to sign data (it's random, secret, and specific to each installation)
+ $sign = hash_hmac('sha256', $url, $conf->get('credentials.salt')); // We use the salt to sign data (it's random, secret, and specific to each installation)
return array('src'=>index_url($_SERVER).'?do=genthumbnail&hmac='.$sign.'&url='.urlencode($url),
'href'=>$href,'width'=>'120','style'=>'height:auto;','alt'=>'thumbnail');
}
@@ -2314,21 +1916,28 @@ function install()
if (!empty($_POST['setlogin']) && !empty($_POST['setpassword']))
{
+ $conf = ConfigManager::getInstance();
$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'];
+ }
+ $conf->set('general.timezone', $tz);
+ $login = $_POST['setlogin'];
+ $conf->set('credentials.login', $login);
+ $salt = sha1(uniqid('', true) .'_'. mt_rand());
+ $conf->set('credentials.salt', $salt);
+ $conf->set('credentials.hash', sha1($_POST['setpassword'] . $login . $salt));
+ if (!empty($_POST['title'])) {
+ $conf->set('general.title', escape($_POST['title']));
+ } else {
+ $conf->set('general.title', 'Shared links on '.escape(index_url($_SERVER)));
}
- $GLOBALS['timezone'] = $tz;
- // Everything is ok, let's create config file.
- $GLOBALS['login'] = $_POST['setlogin'];
- $GLOBALS['salt'] = sha1(uniqid('',true).'_'.mt_rand()); // Salt renders rainbow-tables attacks useless.
- $GLOBALS['hash'] = sha1($_POST['setpassword'].$GLOBALS['login'].$GLOBALS['salt']);
- $GLOBALS['title'] = (empty($_POST['title']) ? 'Shared links on '.escape(index_url($_SERVER)) : $_POST['title'] );
- $GLOBALS['config']['ENABLE_UPDATECHECK'] = !empty($_POST['updateCheck']);
+ $conf->set('general.check_updates', !empty($_POST['updateCheck']));
try {
- writeConfig($GLOBALS, isLoggedIn());
+ // Everything is ok, let's create config file.
+ $conf->write(isLoggedIn());
}
catch(Exception $e) {
error_log(
@@ -2351,7 +1960,7 @@ function install()
$timezone_html = 'Timezone: | '.$timezone_form.' |
';
}
- $PAGE = new pageBuilder;
+ $PAGE = new PageBuilder();
$PAGE->assign('timezone_html',$timezone_html);
$PAGE->assign('timezone_js',$timezone_js);
$PAGE->renderPage('install');
@@ -2369,24 +1978,26 @@ function install()
*/
function genThumbnail()
{
+ $conf = ConfigManager::getInstance();
// Make sure the parameters in the URL were generated by us.
- $sign = hash_hmac('sha256', $_GET['url'], $GLOBALS['salt']);
+ $sign = hash_hmac('sha256', $_GET['url'], $conf->get('credentials.salt'));
if ($sign!=$_GET['hmac']) die('Naughty boy!');
+ $cacheDir = $conf->get('path.thumbnails_cache', 'cache');
// Let's see if we don't already have the image for this URL in the cache.
$thumbname=hash('sha1',$_GET['url']).'.jpg';
- if (is_file($GLOBALS['config']['CACHEDIR'].'/'.$thumbname))
+ if (is_file($cacheDir .'/'. $thumbname))
{ // We have the thumbnail, just serve it:
header('Content-Type: image/jpeg');
- echo file_get_contents($GLOBALS['config']['CACHEDIR'].'/'.$thumbname);
+ echo file_get_contents($cacheDir .'/'. $thumbname);
return;
}
// We may also serve a blank image (if service did not respond)
$blankname=hash('sha1',$_GET['url']).'.gif';
- if (is_file($GLOBALS['config']['CACHEDIR'].'/'.$blankname))
+ if (is_file($cacheDir .'/'. $blankname))
{
header('Content-Type: image/gif');
- echo file_get_contents($GLOBALS['config']['CACHEDIR'].'/'.$blankname);
+ echo file_get_contents($cacheDir .'/'. $blankname);
return;
}
@@ -2401,7 +2012,7 @@ function genThumbnail()
// Is this a link to an image, or to a flickr page ?
$imageurl='';
- if (endswith(parse_url($url,PHP_URL_PATH),'.jpg'))
+ if (endsWith(parse_url($url, PHP_URL_PATH), '.jpg'))
{ // This is a direct link to an image. e.g. http://farm1.staticflickr.com/5/5921913_ac83ed27bd_o.jpg
preg_match('!(http://farm\d+\.staticflickr\.com/\d+/\d+_\w+_)\w.jpg!',$url,$matches);
if (!empty($matches[1])) $imageurl=$matches[1].'m.jpg';
@@ -2433,7 +2044,7 @@ function genThumbnail()
list($headers, $content) = get_http_response($imageurl, 10);
if (strpos($headers[0], '200 OK') !== false) {
// Save image to cache.
- file_put_contents($GLOBALS['config']['CACHEDIR'].'/' . $thumbname, $content);
+ file_put_contents($cacheDir .'/'. $thumbname, $content);
header('Content-Type: image/jpeg');
echo $content;
return;
@@ -2454,7 +2065,7 @@ function genThumbnail()
list($headers, $content) = get_http_response($imageurl, 10);
if (strpos($headers[0], '200 OK') !== false) {
// Save image to cache.
- file_put_contents($GLOBALS['config']['CACHEDIR'] . '/' . $thumbname, $content);
+ file_put_contents($cacheDir .'/'. $thumbname, $content);
header('Content-Type: image/jpeg');
echo $content;
return;
@@ -2477,7 +2088,7 @@ function genThumbnail()
// No control on image size, so wait long enough
list($headers, $content) = get_http_response($imageurl, 20);
if (strpos($headers[0], '200 OK') !== false) {
- $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname;
+ $filepath = $cacheDir .'/'. $thumbname;
file_put_contents($filepath, $content); // Save image to cache.
if (resizeImage($filepath))
{
@@ -2505,7 +2116,7 @@ function genThumbnail()
// No control on image size, so wait long enough
list($headers, $content) = get_http_response($imageurl, 20);
if (strpos($headers[0], '200 OK') !== false) {
- $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname;
+ $filepath = $cacheDir.'/'.$thumbname;
// Save image to cache.
file_put_contents($filepath, $content);
if (resizeImage($filepath))
@@ -2525,7 +2136,7 @@ function genThumbnail()
// We allow 30 seconds max to download (and downloads are limited to 4 Mb)
list($headers, $content) = get_http_response($url, 30);
if (strpos($headers[0], '200 OK') !== false) {
- $filepath=$GLOBALS['config']['CACHEDIR'].'/'.$thumbname;
+ $filepath = $cacheDir .'/'.$thumbname;
// Save image to cache.
file_put_contents($filepath, $content);
if (resizeImage($filepath))
@@ -2540,7 +2151,8 @@ function genThumbnail()
// Otherwise, return an empty image (8x8 transparent gif)
$blankgif = base64_decode('R0lGODlhCAAIAIAAAP///////yH5BAEKAAEALAAAAAAIAAgAAAIHjI+py+1dAAA7');
- file_put_contents($GLOBALS['config']['CACHEDIR'].'/'.$blankname,$blankgif); // Also put something in cache so that this URL is not requested twice.
+ // Also put something in cache so that this URL is not requested twice.
+ file_put_contents($cacheDir .'/'. $blankname, $blankgif);
header('Content-Type: image/gif');
echo $blankgif;
}
@@ -2578,10 +2190,10 @@ function resizeImage($filepath)
return true;
}
-if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database.
-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($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE'];
+if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=genthumbnail')) { genThumbnail(); exit; } // Thumbnail generation/cache does not need the link database.
+if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=dailyrss')) { showDailyRSS(); exit; }
+if (!isset($_SESSION['LINKS_PER_PAGE'])) {
+ $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20);
+}
renderPage();
?>