diff options
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 71 |
1 files changed, 24 insertions, 47 deletions
@@ -78,6 +78,7 @@ require_once 'application/Updater.php'; | |||
78 | use \Shaarli\Languages; | 78 | use \Shaarli\Languages; |
79 | use \Shaarli\ThemeUtils; | 79 | use \Shaarli\ThemeUtils; |
80 | use \Shaarli\Config\ConfigManager; | 80 | use \Shaarli\Config\ConfigManager; |
81 | use \Shaarli\SessionManager; | ||
81 | 82 | ||
82 | // Ensure the PHP version is supported | 83 | // Ensure the PHP version is supported |
83 | try { | 84 | try { |
@@ -121,6 +122,7 @@ if (isset($_COOKIE['shaarli']) && !is_session_id_valid($_COOKIE['shaarli'])) { | |||
121 | } | 122 | } |
122 | 123 | ||
123 | $conf = new ConfigManager(); | 124 | $conf = new ConfigManager(); |
125 | $sessionManager = new SessionManager($_SESSION, $conf); | ||
124 | 126 | ||
125 | // Sniff browser language and set date format accordingly. | 127 | // Sniff browser language and set date format accordingly. |
126 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { | 128 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
@@ -165,7 +167,7 @@ if (! is_file($conf->getConfigFileExt())) { | |||
165 | } | 167 | } |
166 | 168 | ||
167 | // Display the installation form if no existing config is found | 169 | // Display the installation form if no existing config is found |
168 | install($conf); | 170 | install($conf, $sessionManager); |
169 | } | 171 | } |
170 | 172 | ||
171 | // a token depending of deployment salt, user password, and the current ip | 173 | // a token depending of deployment salt, user password, and the current ip |
@@ -381,7 +383,7 @@ if (isset($_POST['login'])) | |||
381 | { | 383 | { |
382 | if (!ban_canLogin($conf)) die(t('I said: NO. You are banned for the moment. Go away.')); | 384 | if (!ban_canLogin($conf)) die(t('I said: NO. You are banned for the moment. Go away.')); |
383 | if (isset($_POST['password']) | 385 | if (isset($_POST['password']) |
384 | && tokenOk($_POST['token']) | 386 | && $sessionManager->checkToken($_POST['token']) |
385 | && (check_auth($_POST['login'], $_POST['password'], $conf)) | 387 | && (check_auth($_POST['login'], $_POST['password'], $conf)) |
386 | ) { // Login/password is OK. | 388 | ) { // Login/password is OK. |
387 | ban_loginOk($conf); | 389 | ban_loginOk($conf); |
@@ -455,32 +457,6 @@ if (isset($_POST['login'])) | |||
455 | if (!isset($_SESSION['tokens'])) $_SESSION['tokens']=array(); // Token are attached to the session. | 457 | if (!isset($_SESSION['tokens'])) $_SESSION['tokens']=array(); // Token are attached to the session. |
456 | 458 | ||
457 | /** | 459 | /** |
458 | * Returns a token. | ||
459 | * | ||
460 | * @param ConfigManager $conf Configuration Manager instance. | ||
461 | * | ||
462 | * @return string token. | ||
463 | */ | ||
464 | function getToken($conf) | ||
465 | { | ||
466 | $rnd = sha1(uniqid('', true) .'_'. mt_rand() . $conf->get('credentials.salt')); // We generate a random string. | ||
467 | $_SESSION['tokens'][$rnd]=1; // Store it on the server side. | ||
468 | return $rnd; | ||
469 | } | ||
470 | |||
471 | // Tells if a token is OK. Using this function will destroy the token. | ||
472 | // true=token is OK. | ||
473 | function tokenOk($token) | ||
474 | { | ||
475 | if (isset($_SESSION['tokens'][$token])) | ||
476 | { | ||
477 | unset($_SESSION['tokens'][$token]); // Token is used: destroy it. | ||
478 | return true; // Token is OK. | ||
479 | } | ||
480 | return false; // Wrong token, or already used. | ||
481 | } | ||
482 | |||
483 | /** | ||
484 | * Daily RSS feed: 1 RSS entry per day giving all the links on that day. | 460 | * Daily RSS feed: 1 RSS entry per day giving all the links on that day. |
485 | * Gives the last 7 days (which have links). | 461 | * Gives the last 7 days (which have links). |
486 | * This RSS feed cannot be filtered. | 462 | * This RSS feed cannot be filtered. |
@@ -687,12 +663,13 @@ function showLinkList($PAGE, $LINKSDB, $conf, $pluginManager) { | |||
687 | /** | 663 | /** |
688 | * Render HTML page (according to URL parameters and user rights) | 664 | * Render HTML page (according to URL parameters and user rights) |
689 | * | 665 | * |
690 | * @param ConfigManager $conf Configuration Manager instance. | 666 | * @param ConfigManager $conf Configuration Manager instance. |
691 | * @param PluginManager $pluginManager Plugin Manager instance, | 667 | * @param PluginManager $pluginManager Plugin Manager instance, |
692 | * @param LinkDB $LINKSDB | 668 | * @param LinkDB $LINKSDB |
693 | * @param History $history instance | 669 | * @param History $history instance |
670 | * @param SessionManager $sessionManager SessionManager instance | ||
694 | */ | 671 | */ |
695 | function renderPage($conf, $pluginManager, $LINKSDB, $history) | 672 | function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager) |
696 | { | 673 | { |
697 | $updater = new Updater( | 674 | $updater = new Updater( |
698 | read_updates_file($conf->get('resource.updates')), | 675 | read_updates_file($conf->get('resource.updates')), |
@@ -713,7 +690,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
713 | die($e->getMessage()); | 690 | die($e->getMessage()); |
714 | } | 691 | } |
715 | 692 | ||
716 | $PAGE = new PageBuilder($conf, $LINKSDB); | 693 | $PAGE = new PageBuilder($conf, $LINKSDB, $sessionManager->generateToken()); |
717 | $PAGE->assign('linkcount', count($LINKSDB)); | 694 | $PAGE->assign('linkcount', count($LINKSDB)); |
718 | $PAGE->assign('privateLinkcount', count_private($LINKSDB)); | 695 | $PAGE->assign('privateLinkcount', count_private($LINKSDB)); |
719 | $PAGE->assign('plugin_errors', $pluginManager->getErrors()); | 696 | $PAGE->assign('plugin_errors', $pluginManager->getErrors()); |
@@ -1109,13 +1086,13 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1109 | 1086 | ||
1110 | if (!empty($_POST['setpassword']) && !empty($_POST['oldpassword'])) | 1087 | if (!empty($_POST['setpassword']) && !empty($_POST['oldpassword'])) |
1111 | { | 1088 | { |
1112 | if (!tokenOk($_POST['token'])) die(t('Wrong token.')); // Go away! | 1089 | if (!$sessionManager->checkToken($_POST['token'])) die(t('Wrong token.')); // Go away! |
1113 | 1090 | ||
1114 | // Make sure old password is correct. | 1091 | // Make sure old password is correct. |
1115 | $oldhash = sha1($_POST['oldpassword'].$conf->get('credentials.login').$conf->get('credentials.salt')); | 1092 | $oldhash = sha1($_POST['oldpassword'].$conf->get('credentials.login').$conf->get('credentials.salt')); |
1116 | if ($oldhash!= $conf->get('credentials.hash')) { | 1093 | if ($oldhash!= $conf->get('credentials.hash')) { |
1117 | echo '<script>alert("'. t('The old password is not correct.') .'");document.location=\'?do=changepasswd\';</script>'; | 1094 | echo '<script>alert("'. t('The old password is not correct.') .'");document.location=\'?do=changepasswd\';</script>'; |
1118 | exit; | 1095 | exit; |
1119 | } | 1096 | } |
1120 | // Save new password | 1097 | // Save new password |
1121 | // Salt renders rainbow-tables attacks useless. | 1098 | // Salt renders rainbow-tables attacks useless. |
@@ -1149,7 +1126,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1149 | { | 1126 | { |
1150 | if (!empty($_POST['title']) ) | 1127 | if (!empty($_POST['title']) ) |
1151 | { | 1128 | { |
1152 | if (!tokenOk($_POST['token'])) { | 1129 | if (!$sessionManager->checkToken($_POST['token'])) { |
1153 | die(t('Wrong token.')); // Go away! | 1130 | die(t('Wrong token.')); // Go away! |
1154 | } | 1131 | } |
1155 | $tz = 'UTC'; | 1132 | $tz = 'UTC'; |
@@ -1225,7 +1202,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1225 | exit; | 1202 | exit; |
1226 | } | 1203 | } |
1227 | 1204 | ||
1228 | if (!tokenOk($_POST['token'])) { | 1205 | if (!$sessionManager->checkToken($_POST['token'])) { |
1229 | die(t('Wrong token.')); | 1206 | die(t('Wrong token.')); |
1230 | } | 1207 | } |
1231 | 1208 | ||
@@ -1255,7 +1232,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1255 | if (isset($_POST['save_edit'])) | 1232 | if (isset($_POST['save_edit'])) |
1256 | { | 1233 | { |
1257 | // Go away! | 1234 | // Go away! |
1258 | if (! tokenOk($_POST['token'])) { | 1235 | if (! $sessionManager->checkToken($_POST['token'])) { |
1259 | die(t('Wrong token.')); | 1236 | die(t('Wrong token.')); |
1260 | } | 1237 | } |
1261 | 1238 | ||
@@ -1355,7 +1332,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1355 | // -------- User clicked the "Delete" button when editing a link: Delete link from database. | 1332 | // -------- User clicked the "Delete" button when editing a link: Delete link from database. |
1356 | if ($targetPage == Router::$PAGE_DELETELINK) | 1333 | if ($targetPage == Router::$PAGE_DELETELINK) |
1357 | { | 1334 | { |
1358 | if (! tokenOk($_GET['token'])) { | 1335 | if (! $sessionManager->checkToken($_GET['token'])) { |
1359 | die(t('Wrong token.')); | 1336 | die(t('Wrong token.')); |
1360 | } | 1337 | } |
1361 | 1338 | ||
@@ -1572,7 +1549,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1572 | echo '<script>alert("'. $msg .'");document.location=\'?do='.Router::$PAGE_IMPORT .'\';</script>'; | 1549 | echo '<script>alert("'. $msg .'");document.location=\'?do='.Router::$PAGE_IMPORT .'\';</script>'; |
1573 | exit; | 1550 | exit; |
1574 | } | 1551 | } |
1575 | if (! tokenOk($_POST['token'])) { | 1552 | if (! $sessionManager->checkToken($_POST['token'])) { |
1576 | die('Wrong token.'); | 1553 | die('Wrong token.'); |
1577 | } | 1554 | } |
1578 | $status = NetscapeBookmarkUtils::import( | 1555 | $status = NetscapeBookmarkUtils::import( |
@@ -1639,7 +1616,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1639 | // Get a fresh token | 1616 | // Get a fresh token |
1640 | if ($targetPage == Router::$GET_TOKEN) { | 1617 | if ($targetPage == Router::$GET_TOKEN) { |
1641 | header('Content-Type:text/plain'); | 1618 | header('Content-Type:text/plain'); |
1642 | echo getToken($conf); | 1619 | echo $sessionManager->generateToken($conf); |
1643 | exit; | 1620 | exit; |
1644 | } | 1621 | } |
1645 | 1622 | ||
@@ -1965,10 +1942,10 @@ function lazyThumbnail($conf, $url,$href=false) | |||
1965 | * Installation | 1942 | * Installation |
1966 | * This function should NEVER be called if the file data/config.php exists. | 1943 | * This function should NEVER be called if the file data/config.php exists. |
1967 | * | 1944 | * |
1968 | * @param ConfigManager $conf Configuration Manager instance. | 1945 | * @param ConfigManager $conf Configuration Manager instance. |
1946 | * @param SessionManager $sessionManager SessionManager instance | ||
1969 | */ | 1947 | */ |
1970 | function install($conf) | 1948 | function install($conf, $sessionManager) { |
1971 | { | ||
1972 | // On free.fr host, make sure the /sessions directory exists, otherwise login will not work. | 1949 | // On free.fr host, make sure the /sessions directory exists, otherwise login will not work. |
1973 | if (endsWith($_SERVER['HTTP_HOST'],'.free.fr') && !is_dir($_SERVER['DOCUMENT_ROOT'].'/sessions')) mkdir($_SERVER['DOCUMENT_ROOT'].'/sessions',0705); | 1950 | if (endsWith($_SERVER['HTTP_HOST'],'.free.fr') && !is_dir($_SERVER['DOCUMENT_ROOT'].'/sessions')) mkdir($_SERVER['DOCUMENT_ROOT'].'/sessions',0705); |
1974 | 1951 | ||
@@ -2051,7 +2028,7 @@ function install($conf) | |||
2051 | exit; | 2028 | exit; |
2052 | } | 2029 | } |
2053 | 2030 | ||
2054 | $PAGE = new PageBuilder($conf); | 2031 | $PAGE = new PageBuilder($conf, null, $sessionManager->generateToken()); |
2055 | list($continents, $cities) = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get()); | 2032 | list($continents, $cities) = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get()); |
2056 | $PAGE->assign('continents', $continents); | 2033 | $PAGE->assign('continents', $continents); |
2057 | $PAGE->assign('cities', $cities); | 2034 | $PAGE->assign('cities', $cities); |
@@ -2328,7 +2305,7 @@ $response = $app->run(true); | |||
2328 | if ($response->getStatusCode() == 404 && strpos($_SERVER['REQUEST_URI'], '/api/v1') === false) { | 2305 | if ($response->getStatusCode() == 404 && strpos($_SERVER['REQUEST_URI'], '/api/v1') === false) { |
2329 | // We use UTF-8 for proper international characters handling. | 2306 | // We use UTF-8 for proper international characters handling. |
2330 | header('Content-Type: text/html; charset=utf-8'); | 2307 | header('Content-Type: text/html; charset=utf-8'); |
2331 | renderPage($conf, $pluginManager, $linkDb, $history); | 2308 | renderPage($conf, $pluginManager, $linkDb, $history, $sessionManager); |
2332 | } else { | 2309 | } else { |
2333 | $app->respond($response); | 2310 | $app->respond($response); |
2334 | } | 2311 | } |