X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=index.php;h=236fd4e27c7d588e1d24d6bd200f96a7bde3cb60;hb=e92f1ba59edb9fd60b185dc633e64a62dffe3b04;hp=bbe302a63a4c9bb32ef85df27bd689432f14e286;hpb=2fbadc3c631e0c7e32b9b956fb5551822729bc33;p=github%2Fshaarli%2FShaarli.git diff --git a/index.php b/index.php index bbe302a6..236fd4e2 100644 --- a/index.php +++ b/index.php @@ -11,7 +11,8 @@ date_default_timezone_set('UTC'); // ----------------------------------------------------------------------------------------------- -// Hardcoded parameter (These parameters can be overwritten by creating the file /data/options.php) +// Hardcoded parameter (These parameters can be overwritten by editing the file /data/config.php) +// You should not touch any code below (or at your own risks!) $GLOBALS['config']['DATADIR'] = 'data'; // Data subdirectory $GLOBALS['config']['CONFIG_FILE'] = $GLOBALS['config']['DATADIR'].'/config.php'; // Configuration file (user login/password) $GLOBALS['config']['DATASTORE'] = $GLOBALS['config']['DATADIR'].'/datastore.php'; // Data storage file. @@ -36,10 +37,6 @@ $GLOBALS['config']['ARCHIVE_ORG'] = false; // For each link, add a link to an ar $GLOBALS['config']['ENABLE_RSS_PERMALINKS'] = true; // Enable RSS permalinks by default. This corresponds to the default behavior of shaarli before this was added as an option. $GLOBALS['config']['HIDE_PUBLIC_LINKS'] = false; // ----------------------------------------------------------------------------------------------- -// You should not touch below (or at your own risks!) -// Optional config file. -if (is_file($GLOBALS['config']['DATADIR'].'/options.php')) require($GLOBALS['config']['DATADIR'].'/options.php'); - define('shaarli_version','0.0.45beta'); // http://server.com/x/shaarli --> /shaarli/ define('WEB_PATH', substr($_SERVER["REQUEST_URI"], 0, 1+strrpos($_SERVER["REQUEST_URI"], '/', 0))); @@ -69,6 +66,7 @@ error_reporting(E_ALL^E_WARNING); // See all error except warnings. // Shaarli library require_once 'application/LinkDB.php'; require_once 'application/Utils.php'; +require_once 'application/Config.php'; include "inc/rain.tpl.class.php"; //include Rain TPL raintpl::$tpl_dir = $GLOBALS['config']['RAINTPL_TPL']; // template directory @@ -100,7 +98,6 @@ if (empty($GLOBALS['title'])) $GLOBALS['title']='Shared links on '.escape(indexU if (empty($GLOBALS['timezone'])) $GLOBALS['timezone']=date_default_timezone_get(); if (empty($GLOBALS['redirector'])) $GLOBALS['redirector']=''; if (empty($GLOBALS['disablesessionprotection'])) $GLOBALS['disablesessionprotection']=false; -if (empty($GLOBALS['disablejquery'])) $GLOBALS['disablejquery']=false; if (empty($GLOBALS['privateLinkByDefault'])) $GLOBALS['privateLinkByDefault']=false; if (empty($GLOBALS['titleLink'])) $GLOBALS['titleLink']='?'; // I really need to rewrite Shaarli with a proper configuation manager. @@ -709,7 +706,7 @@ function showRSS() if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']); else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); else $linksToDisplay = $LINKSDB; - + $nblinksToDisplay = 50; // Number of links to display. if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. { @@ -789,7 +786,7 @@ function showATOM() if (!empty($_GET['searchterm'])) $linksToDisplay = $LINKSDB->filterFulltext($_GET['searchterm']); else if (!empty($_GET['searchtags'])) $linksToDisplay = $LINKSDB->filterTags(trim($_GET['searchtags'])); else $linksToDisplay = $LINKSDB; - + $nblinksToDisplay = 50; // Number of links to display. if (!empty($_GET['nb'])) // In URL, you can specificy the number of links. Example: nb=200 or nb=all for all links. { @@ -948,16 +945,22 @@ function showDaily() $days = $LINKSDB->days(); $i = array_search($day,$days); - if ($i==false) { $i=count($days)-1; $day=$days[$i]; } + if ($i===false) { $i=count($days)-1; $day=$days[$i]; } $previousday=''; $nextday=''; if ($i!==false) { - if ($i>1) $previousday=$days[$i-1]; + if ($i>=1) $previousday=$days[$i-1]; if ($ifilterDay($day); + try { + $linksToDisplay = $LINKSDB->filterDay($day); + } catch (Exception $exc) { + error_log($exc); + $linksToDisplay = []; + } + // We pre-format some fields for proper output. foreach($linksToDisplay as $key=>$link) { @@ -1041,7 +1044,7 @@ function renderPage() if (!empty($_GET['searchterm'])) $links = $LINKSDB->filterFulltext($_GET['searchterm']); elseif (!empty($_GET['searchtags'])) $links = $LINKSDB->filterTags(trim($_GET['searchtags'])); else $links = $LINKSDB; - + $body=''; $linksToDisplay=array(); @@ -1056,7 +1059,7 @@ function renderPage() $linksToDisplay[]=$link; // Add to array. } } - + $PAGE = new pageBuilder; $PAGE->assign('linkcount',count($LINKSDB)); $PAGE->assign('linksToDisplay',$linksToDisplay); @@ -1214,7 +1217,19 @@ function renderPage() // Save new password $GLOBALS['salt'] = sha1(uniqid('',true).'_'.mt_rand()); // Salt renders rainbow-tables attacks useless. $GLOBALS['hash'] = sha1($_POST['setpassword'].$GLOBALS['login'].$GLOBALS['salt']); - writeConfig(); + try { + writeConfig($GLOBALS, isLoggedIn()); + } + catch(Exception $e) { + error_log( + 'ERROR while writing config file after changing password.' . PHP_EOL . + $e->getMessage() + ); + + // TODO: do not handle exceptions/errors in JS. + echo ''; + exit; + } echo ''; exit; } @@ -1243,12 +1258,23 @@ function renderPage() $GLOBALS['titleLink']=$_POST['titleLink']; $GLOBALS['redirector']=$_POST['redirector']; $GLOBALS['disablesessionprotection']=!empty($_POST['disablesessionprotection']); - $GLOBALS['disablejquery']=!empty($_POST['disablejquery']); $GLOBALS['privateLinkByDefault']=!empty($_POST['privateLinkByDefault']); $GLOBALS['config']['ENABLE_RSS_PERMALINKS']= !empty($_POST['enableRssPermalinks']); $GLOBALS['config']['ENABLE_UPDATECHECK'] = !empty($_POST['updateCheck']); $GLOBALS['config']['HIDE_PUBLIC_LINKS'] = !empty($_POST['hidePublicLinks']); - writeConfig(); + try { + writeConfig($GLOBALS, isLoggedIn()); + } + catch(Exception $e) { + error_log( + 'ERROR while writing config file after configuration update.' . PHP_EOL . + $e->getMessage() + ); + + // TODO: do not handle exceptions/errors in JS. + echo ''; + exit; + } echo ''; exit; } @@ -2007,7 +2033,19 @@ function install() $GLOBALS['hash'] = sha1($_POST['setpassword'].$GLOBALS['login'].$GLOBALS['salt']); $GLOBALS['title'] = (empty($_POST['title']) ? 'Shared links on '.escape(indexUrl()) : $_POST['title'] ); $GLOBALS['config']['ENABLE_UPDATECHECK'] = !empty($_POST['updateCheck']); - writeConfig(); + try { + writeConfig($GLOBALS, isLoggedIn()); + } + catch(Exception $e) { + error_log( + 'ERROR while writing config file after installation.' . PHP_EOL . + $e->getMessage() + ); + + // TODO: do not handle exceptions/errors in JS. + echo ''; + exit; + } echo ''; exit; } @@ -2121,30 +2159,7 @@ if (!function_exists('json_encode')) { } } -// Re-write configuration file according to globals. -// Requires some $GLOBALS to be set (login,hash,salt,title). -// If the config file cannot be saved, an error message is displayed and the user is redirected to "Tools" menu. -// (otherwise, the function simply returns.) -function writeConfig() -{ - if (is_file($GLOBALS['config']['CONFIG_FILE']) && !isLoggedIn()) die('You are not authorized to alter config.'); // Only logged in user can alter config. - $config=''; - if (!file_put_contents($GLOBALS['config']['CONFIG_FILE'],$config) || strcmp(file_get_contents($GLOBALS['config']['CONFIG_FILE']),$config)!=0) - { - echo ''; - exit; - } -} + /* Because some f*cking services like flickr require an extra HTTP request to get the thumbnail URL, I have deported the thumbnail URL code generation here, otherwise this would slow down page generation. @@ -2373,6 +2388,15 @@ function invalidateCaches() pageCache::purgeCache(); // Purge page cache shared by sessions. } +try { + mergeDeprecatedConfig($GLOBALS, isLoggedIn()); +} catch(Exception $e) { + error_log( + 'ERROR while merging deprecated options.php file.' . PHP_EOL . + $e->getMessage() + ); +} + 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; }