X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=index.php;h=d8fb3007648f151fcfc363b544c51da9f31548de;hb=bba021defce15184c986d5e3db138bdbdd4df5d5;hp=5771dd8880043d48ada8b2aa1d36ea77d6fd18e0;hpb=9186ab95943b7c2467a0f27f30bed9db3c589b9d;p=github%2Fshaarli%2FShaarli.git diff --git a/index.php b/index.php index 5771dd88..d8fb3007 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))); @@ -66,9 +63,15 @@ checkphpversion(); error_reporting(E_ALL^E_WARNING); // See all error except warnings. //error_reporting(-1); // See all errors (for debugging only) +// User configuration +if (is_file($GLOBALS['config']['CONFIG_FILE'])) { + require_once $GLOBALS['config']['CONFIG_FILE']; +} + // 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,15 +103,15 @@ 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. // Run config screen if first run: -if (!is_file($GLOBALS['config']['CONFIG_FILE'])) install(); +if (! is_file($GLOBALS['config']['CONFIG_FILE'])) { + install(); +} -require $GLOBALS['config']['CONFIG_FILE']; // Read login/password hash into $GLOBALS. $GLOBALS['title'] = !empty($GLOBALS['title']) ? escape($GLOBALS['title']) : ''; $GLOBALS['titleLink'] = !empty($GLOBALS['titleLink']) ? escape($GLOBALS['titleLink']) : ''; $GLOBALS['redirector'] = !empty($GLOBALS['redirector']) ? escape($GLOBALS['redirector']) : ''; @@ -856,15 +859,18 @@ function showATOM() // Daily RSS feed: 1 RSS entry per day giving all the links on that day. // Gives the last 7 days (which have links). // This RSS feed cannot be filtered. -function showDailyRSS() -{ +function showDailyRSS() { // Cache system $query = $_SERVER["QUERY_STRING"]; - $cache = new pageCache(pageUrl(),startsWith($query,'do=dailyrss') && !isLoggedIn()); - $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: + $cache = new pageCache(pageUrl(), startsWith($query, 'do=dailyrss') && !isLoggedIn()); + $cached = $cache->cachedVersion(); + if (!empty($cached)) { + echo $cached; + exit; + } -// Read links from database (and filter private links if used it not logged in). + // If cached was not found (or not usable), then read the database and build the response: + // Read links from database (and filter private links if used it not logged in). $LINKSDB = new LinkDB( $GLOBALS['config']['DATASTORE'], isLoggedIn() || $GLOBALS['config']['OPEN_SHAARLI'], @@ -874,60 +880,75 @@ function showDailyRSS() /* 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'); - $days=array(); - foreach($linkdates as $linkdate) - { - $day=substr($linkdate,0,8); // Extract day (without time) - if (strcmp($day,$today)<0) - { - if (empty($days[$day])) $days[$day]=array(); - $days[$day][]=$linkdate; + $nb_of_days = 7; // We take 7 days. + $today = Date('Ymd'); + $days = array(); + + foreach ($linkdates as $linkdate) { + $day = substr($linkdate, 0, 8); // Extract day (without time) + if (strcmp($day,$today) < 0) { + if (empty($days[$day])) { + $days[$day] = array(); + } + $days[$day][] = $linkdate; + } + + if (count($days) > $nb_of_days) { + break; // Have we collected enough days? } - 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=escape(indexUrl()); + $pageaddr = escape(indexUrl()); echo ''; - echo 'Daily - '.$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 + echo ''; + echo 'Daily - '. $GLOBALS['title'] . ''; + echo ''. $pageaddr .''; + echo 'Daily shared links'; + echo 'en-en'; + echo ''. $pageaddr .''. PHP_EOL; + + // For each day. + foreach ($days as $day => $linkdates) { + $daydate = linkdate2timestamp($day.'_000000'); // Full text date $rfc822date = linkdate2rfc822($day.'_000000'); - $absurl=escape(indexUrl().'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. - echo ''.$GLOBALS['title'].' - '.$daydate.''.$absurl.''.$absurl.''; - echo ''.escape($rfc822date).""; + $absurl = escape(indexUrl().'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. // Build the HTML body of this RSS entry. - $html=''; - $href=''; - $links=array(); + $html = ''; + $href = ''; + $links = array(); + // We pre-format some fields for proper output. - foreach($linkdates as $linkdate) - { + foreach ($linkdates as $linkdate) { $l = $LINKSDB[$linkdate]; - $l['formatedDescription']=nl2br(keepMultipleSpaces(text2clickable($l['description']))); + $l['formatedDescription'] = nl2br(keepMultipleSpaces(text2clickable($l['description']))); $l['thumbnail'] = thumbnail($l['url']); $l['timestamp'] = linkdate2timestamp($l['linkdate']); - if (startsWith($l['url'],'?')) $l['url']=indexUrl().$l['url']; // make permalink URL absolute - $links[$linkdate]=$l; + if (startsWith($l['url'], '?')) { + $l['url'] = indexUrl() . $l['url']; // make permalink URL absolute + } + $links[$linkdate] = $l; } + // Then build the HTML for this day: $tpl = new RainTPL; - $tpl->assign('links',$links); - $html = $tpl->draw('dailyrss',$return_string=true); - echo "\n"; - echo ''."\n\n\n"; + $tpl->assign('title', $GLOBALS['title']); + $tpl->assign('daydate', $daydate); + $tpl->assign('absurl', $absurl); + $tpl->assign('links', $links); + $tpl->assign('rfc822date', escape($rfc822date)); + $html = $tpl->draw('dailyrss', $return_string=true); + echo $html . PHP_EOL; } - echo ''; + echo ''; $cache->cache(ob_get_contents()); ob_end_flush(); @@ -1220,7 +1241,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; } @@ -1249,12 +1282,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; } @@ -2013,7 +2057,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; } @@ -2127,30 +2183,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. @@ -2379,6 +2412,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; }