use \Shaarli\Languages;
use \Shaarli\ThemeUtils;
use \Shaarli\Config\ConfigManager;
+use \Shaarli\LoginManager;
use \Shaarli\SessionManager;
// Ensure the PHP version is supported
}
$conf = new ConfigManager();
+$loginManager = new LoginManager($GLOBALS, $conf);
$sessionManager = new SessionManager($_SESSION, $conf);
+// LC_MESSAGES isn't defined without php-intl, in this case use LC_COLLATE locale instead.
+if (! defined('LC_MESSAGES')) {
+ define('LC_MESSAGES', LC_COLLATE);
+}
+
// Sniff browser language and set date format accordingly.
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
autoLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']);
unset($_SESSION['uid']);
unset($_SESSION['ip']);
unset($_SESSION['username']);
- unset($_SESSION['privateonly']);
+ unset($_SESSION['visibility']);
unset($_SESSION['untaggedonly']);
}
setcookie('shaarli_staySignedIn', FALSE, 0, WEB_PATH);
}
-
-// ------------------------------------------------------------------------------------------
-// Brute force protection system
-// Several consecutive failed logins will ban the IP address for 30 minutes.
-if (!is_file($conf->get('resource.ban_file', 'data/ipbans.php'))) {
- // FIXME! globals
- file_put_contents(
- $conf->get('resource.ban_file', 'data/ipbans.php'),
- "<?php\n\$GLOBALS['IPBANS']=".var_export(array('FAILURES'=>array(),'BANS'=>array()),true).";\n?>"
- );
-}
-include $conf->get('resource.ban_file', 'data/ipbans.php');
-/**
- * Signal a failed login. Will ban the IP if too many failures:
- *
- * @param ConfigManager $conf Configuration Manager instance.
- */
-function ban_loginFailed($conf)
-{
- $ip = $_SERVER['REMOTE_ADDR'];
- $trusted = $conf->get('security.trusted_proxies', array());
- if (in_array($ip, $trusted)) {
- $ip = getIpAddressFromProxy($_SERVER, $trusted);
- if (!$ip) {
- return;
- }
- }
- $gb = $GLOBALS['IPBANS'];
- if (! isset($gb['FAILURES'][$ip])) {
- $gb['FAILURES'][$ip]=0;
- }
- $gb['FAILURES'][$ip]++;
- if ($gb['FAILURES'][$ip] > ($conf->get('security.ban_after') - 1))
- {
- $gb['BANS'][$ip] = time() + $conf->get('security.ban_after', 1800);
- logm($conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], 'IP address banned from login');
- }
- $GLOBALS['IPBANS'] = $gb;
- file_put_contents(
- $conf->get('resource.ban_file', 'data/ipbans.php'),
- "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>"
- );
-}
-
-/**
- * Signals a successful login. Resets failed login counter.
- *
- * @param ConfigManager $conf Configuration Manager instance.
- */
-function ban_loginOk($conf)
-{
- $ip = $_SERVER['REMOTE_ADDR'];
- $gb = $GLOBALS['IPBANS'];
- unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]);
- $GLOBALS['IPBANS'] = $gb;
- file_put_contents(
- $conf->get('resource.ban_file', 'data/ipbans.php'),
- "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>"
- );
-}
-
-/**
- * Checks if the user CAN login. If 'true', the user can try to login.
- *
- * @param ConfigManager $conf Configuration Manager instance.
- *
- * @return bool: true if the user is allowed to login.
- */
-function ban_canLogin($conf)
-{
- $ip=$_SERVER["REMOTE_ADDR"]; $gb=$GLOBALS['IPBANS'];
- if (isset($gb['BANS'][$ip]))
- {
- // User is banned. Check if the ban has expired:
- if ($gb['BANS'][$ip]<=time())
- { // Ban expired, user can try to login again.
- logm($conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], 'Ban lifted.');
- unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]);
- file_put_contents(
- $conf->get('resource.ban_file', 'data/ipbans.php'),
- "<?php\n\$GLOBALS['IPBANS']=".var_export($gb,true).";\n?>"
- );
- return true; // Ban has expired, user can login.
- }
- return false; // User is banned.
- }
- return true; // User is not banned.
-}
-
// ------------------------------------------------------------------------------------------
// Process login form: Check if login/password is correct.
if (isset($_POST['login']))
{
- if (!ban_canLogin($conf)) die(t('I said: NO. You are banned for the moment. Go away.'));
+ if (! $loginManager->canLogin($_SERVER)) {
+ die(t('I said: NO. You are banned for the moment. Go away.'));
+ }
if (isset($_POST['password'])
&& $sessionManager->checkToken($_POST['token'])
&& (check_auth($_POST['login'], $_POST['password'], $conf))
- ) { // Login/password is OK.
- ban_loginOk($conf);
+ ) {
+ // Login/password is OK.
+ $loginManager->handleSuccessfulLogin($_SERVER);
+
// If user wants to keep the session cookie even after the browser closes:
- if (!empty($_POST['longlastingsession']))
- {
+ if (!empty($_POST['longlastingsession'])) {
$_SESSION['longlastingsession'] = 31536000; // (31536000 seconds = 1 year)
$expiration = time() + $_SESSION['longlastingsession']; // calculate relative cookie expiration (1 year from now)
setcookie('shaarli_staySignedIn', STAY_SIGNED_IN_TOKEN, $expiration, WEB_PATH);
}
}
header('Location: ?'); exit;
- }
- else
- {
- ban_loginFailed($conf);
- $redir = '&username='. $_POST['login'];
+ } else {
+ $loginManager->handleFailedLogin($_SERVER);
+ $redir = '&username='. urlencode($_POST['login']);
if (isset($_GET['post'])) {
$redir .= '&post=' . urlencode($_GET['post']);
foreach (array('description', 'source', 'title', 'tags') as $param) {
// We pre-format some fields for proper output.
foreach ($links as &$link) {
- $link['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url'));
+ $link['formatedDescription'] = format_description(
+ $link['description'],
+ $conf->get('redirector.url'),
+ $conf->get('redirector.encode_url')
+ );
$link['thumbnail'] = thumbnail($conf, $link['url']);
$link['timestamp'] = $link['created']->getTimestamp();
if (startsWith($link['url'], '?')) {
$taglist = explode(' ',$link['tags']);
uasort($taglist, 'strcasecmp');
$linksToDisplay[$key]['taglist']=$taglist;
- $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url'));
+ $linksToDisplay[$key]['formatedDescription'] = format_description(
+ $link['description'],
+ $conf->get('redirector.url'),
+ $conf->get('redirector.encode_url')
+ );
$linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']);
$linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp();
}
+ $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
+ $data = array(
+ 'pagetitle' => $conf->get('general.title') .' - '. format_date($dayDate, false),
+ 'linksToDisplay' => $linksToDisplay,
+ 'day' => $dayDate->getTimestamp(),
+ 'dayDate' => $dayDate,
+ 'previousday' => $previousday,
+ 'nextday' => $nextday,
+ );
+
+ /* Hook is called before column construction so that plugins don't have
+ to deal with columns. */
+ $pluginManager->executeHooks('render_daily', $data, array('loggedin' => isLoggedIn()));
+
/* We need to spread the articles on 3 columns.
I did not want to use a JavaScript lib like http://masonry.desandro.com/
so I manually spread entries with a simple method: I roughly evaluate the
*/
$columns = array(array(), array(), array()); // Entries to display, for each column.
$fill = array(0, 0, 0); // Rough estimate of columns fill.
- foreach($linksToDisplay as $key => $link) {
+ foreach($data['linksToDisplay'] as $key => $link) {
// Roughly estimate length of entry (by counting characters)
// Title: 30 chars = 1 line. 1 line is 30 pixels height.
// Description: 836 characters gives roughly 342 pixel height.
$fill[$index] += $length;
}
- $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
- $data = array(
- 'pagetitle' => $conf->get('general.title') .' - '. format_date($dayDate, false),
- 'linksToDisplay' => $linksToDisplay,
- 'cols' => $columns,
- 'day' => $dayDate->getTimestamp(),
- 'dayDate' => $dayDate,
- 'previousday' => $previousday,
- 'nextday' => $nextday,
- );
-
- $pluginManager->executeHooks('render_daily', $data, array('loggedin' => isLoggedIn()));
+ $data['cols'] = $columns;
foreach ($data as $key => $value) {
$pageBuilder->assign($key, $value);
* @param LinkDB $LINKSDB
* @param History $history instance
* @param SessionManager $sessionManager SessionManager instance
+ * @param LoginManager $loginManager LoginManager instance
*/
-function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager)
+function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $loginManager)
{
$updater = new Updater(
read_updates_file($conf->get('resource.updates')),
$PAGE->assign('returnurl',(isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']):''));
// add default state of the 'remember me' checkbox
$PAGE->assign('remember_user_default', $conf->get('privacy.remember_user_default'));
+ $PAGE->assign('user_can_login', $loginManager->canLogin($_SERVER));
$PAGE->renderPage('loginform');
exit;
}
// -------- Tag cloud
if ($targetPage == Router::$PAGE_TAGCLOUD)
{
- $visibility = ! empty($_SESSION['privateonly']) ? 'private' : 'all';
+ $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
$filteringTags = isset($_GET['searchtags']) ? explode(' ', $_GET['searchtags']) : [];
$tags = $LINKSDB->linksCountPerTag($filteringTags, $visibility);
// -------- Tag list
if ($targetPage == Router::$PAGE_TAGLIST)
{
- $visibility = ! empty($_SESSION['privateonly']) ? 'private' : 'all';
+ $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
$filteringTags = isset($_GET['searchtags']) ? explode(' ', $_GET['searchtags']) : [];
$tags = $LINKSDB->linksCountPerTag($filteringTags, $visibility);
foreach ($filteringTags as $tag) {
}
// -------- User wants to see only private links (toggle)
- if (isset($_GET['privateonly'])) {
- if (empty($_SESSION['privateonly'])) {
- $_SESSION['privateonly'] = 1; // See only private links
- } else {
- unset($_SESSION['privateonly']); // See all links
+ if (isset($_GET['visibility'])) {
+ if ($_GET['visibility'] === 'private') {
+ // Visibility not set or not already private, set private, otherwise reset it
+ if (empty($_SESSION['visibility']) || $_SESSION['visibility'] !== 'private') {
+ // See only private links
+ $_SESSION['visibility'] = 'private';
+ } else {
+ unset($_SESSION['visibility']);
+ }
+ } else if ($_GET['visibility'] === 'public') {
+ if (empty($_SESSION['visibility']) || $_SESSION['visibility'] !== 'public') {
+ // See only public links
+ $_SESSION['visibility'] = 'public';
+ } else {
+ unset($_SESSION['visibility']);
+ }
}
if (! empty($_SERVER['HTTP_REFERER'])) {
- $location = generateLocation($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'], array('privateonly'));
+ $location = generateLocation($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'], array('visibility'));
} else {
$location = '?';
}
// If this is an HTTP(S) link, we try go get the page to extract the title (otherwise we will to straight to the edit form.)
if (empty($title) && strpos(get_url_scheme($url), 'http') !== false) {
// Short timeout to keep the application responsive
- list($headers, $content) = get_http_response($url, 4);
- if (strpos($headers[0], '200 OK') !== false) {
- // Retrieve charset.
- $charset = get_charset($headers, $content);
- // Extract title.
- $title = html_extract_title($content);
- // Re-encode title in utf-8 if necessary.
- if (! empty($title) && strtolower($charset) != 'utf-8') {
- $title = mb_convert_encoding($title, 'utf-8', $charset);
- }
+ // The callback will fill $charset and $title with data from the downloaded page.
+ get_http_response($url, 25, 4194304, get_curl_download_callback($charset, $title));
+ if (! empty($title) && strtolower($charset) != 'utf-8') {
+ $title = mb_convert_encoding($title, 'utf-8', $charset);
}
}
}
} else {
// Filter links according search parameters.
- $visibility = ! empty($_SESSION['privateonly']) ? 'private' : 'all';
+ $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
$request = [
'searchtags' => $searchtags,
'searchterm' => $searchterm,
while ($i<$end && $i<count($keys))
{
$link = $linksToDisplay[$keys[$i]];
- $link['description'] = format_description($link['description'], $conf->get('redirector.url'));
+ $link['description'] = format_description(
+ $link['description'],
+ $conf->get('redirector.url'),
+ $conf->get('redirector.encode_url')
+ );
$classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
$link['class'] = $link['private'] == 0 ? $classLi : 'private';
$link['timestamp'] = $link['created']->getTimestamp();
'result_count' => count($linksToDisplay),
'search_term' => $searchterm,
'search_tags' => $searchtags,
- 'visibility' => ! empty($_SESSION['privateonly']) ? 'private' : '',
+ 'visibility' => ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '',
'redirector' => $conf->get('redirector.url'), // Optional redirector URL.
'links' => $linkDisp,
);
if ($response->getStatusCode() == 404 && strpos($_SERVER['REQUEST_URI'], '/api/v1') === false) {
// We use UTF-8 for proper international characters handling.
header('Content-Type: text/html; charset=utf-8');
- renderPage($conf, $pluginManager, $linkDb, $history, $sessionManager);
+ renderPage($conf, $pluginManager, $linkDb, $history, $sessionManager, $loginManager);
} else {
$app->respond($response);
}