2013-02-26 10:09:41 +01:00
|
|
|
<?php
|
2015-09-14 20:54:13 +02:00
|
|
|
/**
|
2017-03-21 20:08:40 +01:00
|
|
|
* Shaarli - The personal, minimalist, super-fast, database free, bookmarking service.
|
2015-09-14 20:54:13 +02:00
|
|
|
*
|
|
|
|
* Friendly fork by the Shaarli community:
|
|
|
|
* - https://github.com/shaarli/Shaarli
|
|
|
|
*
|
|
|
|
* Original project by sebsauvage.net:
|
|
|
|
* - http://sebsauvage.net/wiki/doku.php?id=php:shaarli
|
|
|
|
* - https://github.com/sebsauvage/Shaarli
|
|
|
|
*
|
|
|
|
* Licence: http://www.opensource.org/licenses/zlib-license.php
|
|
|
|
*
|
2017-01-15 19:27:57 +01:00
|
|
|
* Requires: PHP 5.5.x
|
2015-09-14 20:54:13 +02:00
|
|
|
*/
|
2015-08-04 18:31:16 +02:00
|
|
|
|
|
|
|
// Set 'UTC' as the default timezone if it is not defined in php.ini
|
|
|
|
// See http://php.net/manual/en/datetime.configuration.php#ini.date.timezone
|
|
|
|
if (date_default_timezone_get() == '') {
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
}
|
2013-03-10 14:06:12 +01:00
|
|
|
|
2015-11-11 18:45:46 +01:00
|
|
|
/*
|
|
|
|
* PHP configuration
|
|
|
|
*/
|
|
|
|
|
2013-12-05 18:23:02 +01:00
|
|
|
// http://server.com/x/shaarli --> /shaarli/
|
2016-05-18 21:48:24 +02:00
|
|
|
define('WEB_PATH', substr($_SERVER['REQUEST_URI'], 0, 1+strrpos($_SERVER['REQUEST_URI'], '/', 0)));
|
2013-02-26 10:09:41 +01:00
|
|
|
|
2015-11-11 18:45:46 +01:00
|
|
|
// High execution time in case of problematic imports/exports.
|
2018-10-13 00:31:11 +02:00
|
|
|
ini_set('max_input_time', '60');
|
2015-11-11 18:45:46 +01:00
|
|
|
|
|
|
|
// Try to set max upload file size and read
|
|
|
|
ini_set('memory_limit', '128M');
|
2013-02-26 10:09:41 +01:00
|
|
|
ini_set('post_max_size', '16M');
|
|
|
|
ini_set('upload_max_filesize', '16M');
|
|
|
|
|
2015-11-11 18:45:46 +01:00
|
|
|
// See all error except warnings
|
|
|
|
error_reporting(E_ALL^E_WARNING);
|
|
|
|
// See all errors (for debugging only)
|
|
|
|
//error_reporting(-1);
|
|
|
|
|
2015-07-10 22:53:43 +02:00
|
|
|
|
2016-07-28 22:54:33 +02:00
|
|
|
// 3rd-party libraries
|
2016-09-04 23:57:21 +02:00
|
|
|
if (! file_exists(__DIR__ . '/vendor/autoload.php')) {
|
|
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
|
|
echo "Error: missing Composer configuration\n\n"
|
|
|
|
."If you installed Shaarli through Git or using the development branch,\n"
|
|
|
|
."please refer to the installation documentation to install PHP"
|
|
|
|
." dependencies using Composer:\n"
|
2018-06-26 22:18:51 +02:00
|
|
|
."- https://shaarli.readthedocs.io/en/master/Server-configuration/\n"
|
2017-08-26 09:40:57 +02:00
|
|
|
."- https://shaarli.readthedocs.io/en/master/Download-and-Installation/";
|
2016-09-04 23:57:21 +02:00
|
|
|
exit;
|
|
|
|
}
|
2016-07-28 22:54:33 +02:00
|
|
|
require_once 'inc/rain.tpl.class.php';
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
2015-03-12 00:43:02 +01:00
|
|
|
// Shaarli library
|
2018-12-03 01:35:14 +01:00
|
|
|
require_once 'application/bookmark/LinkUtils.php';
|
2017-03-08 19:59:00 +01:00
|
|
|
require_once 'application/config/ConfigPlugin.php';
|
2018-12-03 00:08:04 +01:00
|
|
|
require_once 'application/feed/Cache.php';
|
2018-12-03 00:34:53 +01:00
|
|
|
require_once 'application/http/HttpUtils.php';
|
|
|
|
require_once 'application/http/UrlUtils.php';
|
2018-12-03 23:49:20 +01:00
|
|
|
require_once 'application/updater/UpdaterUtils.php';
|
2015-11-11 22:49:58 +01:00
|
|
|
require_once 'application/FileUtils.php';
|
2015-07-11 01:29:12 +02:00
|
|
|
require_once 'application/TimeZone.php';
|
2015-03-12 00:43:02 +01:00
|
|
|
require_once 'application/Utils.php';
|
2018-12-03 01:10:39 +01:00
|
|
|
|
2018-12-03 23:58:59 +01:00
|
|
|
use \Shaarli\ApplicationUtils;
|
2018-12-03 01:22:45 +01:00
|
|
|
use \Shaarli\Bookmark\Exception\LinkNotFoundException;
|
2018-12-03 01:10:39 +01:00
|
|
|
use \Shaarli\Bookmark\LinkDB;
|
2017-03-03 23:06:12 +01:00
|
|
|
use \Shaarli\Config\ConfigManager;
|
2018-12-03 00:08:04 +01:00
|
|
|
use \Shaarli\Feed\CachedPage;
|
|
|
|
use \Shaarli\Feed\FeedBuilder;
|
|
|
|
use \Shaarli\History;
|
2017-11-11 14:01:21 +01:00
|
|
|
use \Shaarli\Languages;
|
2018-12-04 00:13:42 +01:00
|
|
|
use \Shaarli\Netscape\NetscapeBookmarkUtils;
|
2018-12-04 00:26:50 +01:00
|
|
|
use \Shaarli\Plugin\PluginManager;
|
2018-12-03 00:46:04 +01:00
|
|
|
use \Shaarli\Render\PageBuilder;
|
|
|
|
use \Shaarli\Render\ThemeUtils;
|
2018-12-04 00:02:17 +01:00
|
|
|
use \Shaarli\Router;
|
2018-04-27 22:12:22 +02:00
|
|
|
use \Shaarli\Security\LoginManager;
|
|
|
|
use \Shaarli\Security\SessionManager;
|
2017-11-11 14:01:21 +01:00
|
|
|
use \Shaarli\Thumbnailer;
|
2018-12-03 23:58:59 +01:00
|
|
|
use \Shaarli\Updater\Updater;
|
2015-03-12 00:43:02 +01:00
|
|
|
|
2015-07-11 01:29:12 +02:00
|
|
|
// Ensure the PHP version is supported
|
|
|
|
try {
|
2017-01-15 19:27:57 +01:00
|
|
|
ApplicationUtils::checkPHPVersion('5.5', PHP_VERSION);
|
2018-10-13 00:31:11 +02:00
|
|
|
} catch (Exception $exc) {
|
2015-07-11 01:29:12 +02:00
|
|
|
header('Content-Type: text/plain; charset=utf-8');
|
2015-11-11 22:49:58 +01:00
|
|
|
echo $exc->getMessage();
|
2015-07-11 01:29:12 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2017-10-01 11:09:12 +02:00
|
|
|
define('SHAARLI_VERSION', ApplicationUtils::getVersion(__DIR__ .'/'. ApplicationUtils::$VERSION_FILE));
|
2017-03-21 20:08:40 +01:00
|
|
|
|
2015-07-25 13:15:47 +02:00
|
|
|
// Force cookie path (but do not change lifetime)
|
|
|
|
$cookie = session_get_cookie_params();
|
|
|
|
$cookiedir = '';
|
|
|
|
if (dirname($_SERVER['SCRIPT_NAME']) != '/') {
|
|
|
|
$cookiedir = dirname($_SERVER["SCRIPT_NAME"]).'/';
|
|
|
|
}
|
|
|
|
// Set default cookie expiration and path.
|
|
|
|
session_set_cookie_params($cookie['lifetime'], $cookiedir, $_SERVER['SERVER_NAME']);
|
|
|
|
// Set session parameters on server side.
|
|
|
|
// Use cookies to store session.
|
|
|
|
ini_set('session.use_cookies', 1);
|
|
|
|
// Force cookies for session (phpsessionID forbidden in URL).
|
|
|
|
ini_set('session.use_only_cookies', 1);
|
|
|
|
// Prevent PHP form using sessionID in URL if cookies are disabled.
|
|
|
|
ini_set('session.use_trans_sid', false);
|
|
|
|
|
|
|
|
session_name('shaarli');
|
|
|
|
// Start session if needed (Some server auto-start sessions).
|
2018-10-31 14:09:35 +01:00
|
|
|
if (session_status() == PHP_SESSION_NONE) {
|
2015-07-25 13:15:47 +02:00
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
|
2015-09-03 23:12:58 +02:00
|
|
|
// Regenerate session ID if invalid or not defined in cookie.
|
2017-10-22 19:54:44 +02:00
|
|
|
if (isset($_COOKIE['shaarli']) && !SessionManager::checkId($_COOKIE['shaarli'])) {
|
2015-09-03 23:12:58 +02:00
|
|
|
session_regenerate_id(true);
|
|
|
|
$_COOKIE['shaarli'] = session_id();
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
$conf = new ConfigManager();
|
2017-10-22 18:44:46 +02:00
|
|
|
$sessionManager = new SessionManager($_SESSION, $conf);
|
2018-02-17 01:46:27 +01:00
|
|
|
$loginManager = new LoginManager($GLOBALS, $conf, $sessionManager);
|
2018-05-06 17:06:36 +02:00
|
|
|
$loginManager->generateStaySignedInToken($_SERVER['REMOTE_ADDR']);
|
2018-04-18 23:09:45 +02:00
|
|
|
$clientIpId = client_ip_id($_SERVER);
|
2017-05-09 18:12:15 +02:00
|
|
|
|
2018-01-31 12:39:17 +01:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:12:15 +02:00
|
|
|
// Sniff browser language and set date format accordingly.
|
|
|
|
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
|
|
|
autoLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']);
|
|
|
|
}
|
|
|
|
|
|
|
|
new Languages(setlocale(LC_MESSAGES, 0), $conf);
|
|
|
|
|
2016-05-30 20:15:36 +02:00
|
|
|
$conf->setEmpty('general.timezone', date_default_timezone_get());
|
2017-05-09 18:12:15 +02:00
|
|
|
$conf->setEmpty('general.title', t('Shared links on '). escape(index_url($_SERVER)));
|
2016-12-07 11:58:25 +01:00
|
|
|
RainTPL::$tpl_dir = $conf->get('resource.raintpl_tpl').'/'.$conf->get('resource.theme').'/'; // template directory
|
2016-06-11 09:08:02 +02:00
|
|
|
RainTPL::$cache_dir = $conf->get('resource.raintpl_tmp'); // cache directory
|
2013-02-26 10:09:41 +01:00
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
$pluginManager = new PluginManager($conf);
|
2016-05-29 16:10:32 +02:00
|
|
|
$pluginManager->load($conf->get('general.enabled_plugins'));
|
2015-07-15 11:42:15 +02:00
|
|
|
|
2016-05-29 16:10:32 +02:00
|
|
|
date_default_timezone_set($conf->get('general.timezone', 'UTC'));
|
2016-05-29 14:26:23 +02:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
ob_start(); // Output buffering for the page cache.
|
|
|
|
|
|
|
|
// Prevent caching on client side or proxy: (yes, it's ugly)
|
|
|
|
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
|
|
|
|
header("Cache-Control: no-store, no-cache, must-revalidate");
|
|
|
|
header("Cache-Control: post-check=0, pre-check=0", false);
|
|
|
|
header("Pragma: no-cache");
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
if (! is_file($conf->getConfigFileExt())) {
|
2015-11-11 22:49:58 +01:00
|
|
|
// Ensure Shaarli has proper access to its resources
|
2016-06-09 20:04:02 +02:00
|
|
|
$errors = ApplicationUtils::checkResourcePermissions($conf);
|
2015-11-11 22:49:58 +01:00
|
|
|
|
|
|
|
if ($errors != array()) {
|
2017-05-09 18:12:15 +02:00
|
|
|
$message = '<p>'. t('Insufficient permissions:') .'</p><ul>';
|
2015-11-11 22:49:58 +01:00
|
|
|
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
$message .= '<li>'.$error.'</li>';
|
|
|
|
}
|
|
|
|
$message .= '</ul>';
|
|
|
|
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
|
|
echo $message;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display the installation form if no existing config is found
|
2018-06-07 19:58:58 +02:00
|
|
|
install($conf, $sessionManager, $loginManager);
|
2015-07-10 22:53:43 +02:00
|
|
|
}
|
2013-03-04 21:02:24 +01:00
|
|
|
|
2018-05-06 17:06:36 +02:00
|
|
|
$loginManager->checkLoginState($_COOKIE, $clientIpId);
|
2013-02-26 10:09:41 +01:00
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
/**
|
2018-04-18 23:45:05 +02:00
|
|
|
* Adapter function to ensure compatibility with third-party templates
|
2016-06-09 20:04:02 +02:00
|
|
|
*
|
2018-04-18 23:45:05 +02:00
|
|
|
* @see https://github.com/shaarli/Shaarli/pull/1086
|
|
|
|
*
|
|
|
|
* @return bool true when the user is logged in, false otherwise
|
2016-06-09 20:04:02 +02:00
|
|
|
*/
|
2013-02-26 10:09:41 +01:00
|
|
|
function isLoggedIn()
|
|
|
|
{
|
2018-02-17 01:46:27 +01:00
|
|
|
global $loginManager;
|
|
|
|
return $loginManager->isLoggedIn();
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
|
|
|
|
2018-02-17 01:46:27 +01:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Process login form: Check if login/password is correct.
|
2018-02-16 22:21:59 +01:00
|
|
|
if (isset($_POST['login'])) {
|
2017-10-25 23:03:31 +02:00
|
|
|
if (! $loginManager->canLogin($_SERVER)) {
|
|
|
|
die(t('I said: NO. You are banned for the moment. Go away.'));
|
|
|
|
}
|
2016-06-09 20:04:02 +02:00
|
|
|
if (isset($_POST['password'])
|
2017-10-22 18:44:46 +02:00
|
|
|
&& $sessionManager->checkToken($_POST['token'])
|
2018-04-18 23:09:45 +02:00
|
|
|
&& $loginManager->checkCredentials($_SERVER['REMOTE_ADDR'], $clientIpId, $_POST['login'], $_POST['password'])
|
2017-10-25 23:03:31 +02:00
|
|
|
) {
|
|
|
|
$loginManager->handleSuccessfulLogin($_SERVER);
|
|
|
|
|
2018-04-27 23:17:38 +02:00
|
|
|
$cookiedir = '';
|
|
|
|
if (dirname($_SERVER['SCRIPT_NAME']) != '/') {
|
2014-08-11 20:41:50 +02:00
|
|
|
// Note: Never forget the trailing slash on the cookie path!
|
2018-04-27 23:17:38 +02:00
|
|
|
$cookiedir = dirname($_SERVER["SCRIPT_NAME"]) . '/';
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
2018-04-27 23:17:38 +02:00
|
|
|
|
|
|
|
if (!empty($_POST['longlastingsession'])) {
|
|
|
|
// Keep the session cookie even after the browser closes
|
|
|
|
$sessionManager->setStaySignedIn(true);
|
|
|
|
$expirationTime = $sessionManager->extendSession();
|
|
|
|
|
|
|
|
setcookie(
|
2018-05-06 17:06:36 +02:00
|
|
|
$loginManager::$STAY_SIGNED_IN_COOKIE,
|
|
|
|
$loginManager->getStaySignedInToken(),
|
2018-04-27 23:17:38 +02:00
|
|
|
$expirationTime,
|
|
|
|
WEB_PATH
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Standard session expiration (=when browser closes)
|
|
|
|
$expirationTime = 0;
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
2016-01-20 10:57:07 +01:00
|
|
|
|
2018-04-27 23:17:38 +02:00
|
|
|
// Send cookie with the new expiration date to the browser
|
|
|
|
session_set_cookie_params($expirationTime, $cookiedir, $_SERVER['SERVER_NAME']);
|
|
|
|
session_regenerate_id(true);
|
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
// Optional redirect after login:
|
2015-07-29 15:32:41 +02:00
|
|
|
if (isset($_GET['post'])) {
|
|
|
|
$uri = '?post='. urlencode($_GET['post']);
|
2017-03-25 19:41:01 +01:00
|
|
|
foreach (array('description', 'source', 'title', 'tags') as $param) {
|
2015-07-29 15:32:41 +02:00
|
|
|
if (!empty($_GET[$param])) {
|
|
|
|
$uri .= '&'.$param.'='.urlencode($_GET[$param]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header('Location: '. $uri);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_GET['edit_link'])) {
|
|
|
|
header('Location: ?edit_link='. escape($_GET['edit_link']));
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_POST['returnurl'])) {
|
|
|
|
// Prevent loops over login screen.
|
|
|
|
if (strpos($_POST['returnurl'], 'do=login') === false) {
|
2016-03-21 19:06:46 +01:00
|
|
|
header('Location: '. generateLocation($_POST['returnurl'], $_SERVER['HTTP_HOST']));
|
2015-07-29 15:32:41 +02:00
|
|
|
exit;
|
|
|
|
}
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
2018-10-13 00:31:11 +02:00
|
|
|
header('Location: ?');
|
|
|
|
exit;
|
2017-10-25 23:03:31 +02:00
|
|
|
} else {
|
|
|
|
$loginManager->handleFailedLogin($_SERVER);
|
2018-01-04 15:53:48 +01:00
|
|
|
$redir = '&username='. urlencode($_POST['login']);
|
2015-07-29 15:32:41 +02:00
|
|
|
if (isset($_GET['post'])) {
|
2016-05-06 20:03:10 +02:00
|
|
|
$redir .= '&post=' . urlencode($_GET['post']);
|
2017-03-25 19:41:01 +01:00
|
|
|
foreach (array('description', 'source', 'title', 'tags') as $param) {
|
2015-07-29 15:32:41 +02:00
|
|
|
if (!empty($_GET[$param])) {
|
|
|
|
$redir .= '&' . $param . '=' . urlencode($_GET[$param]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 18:12:15 +02:00
|
|
|
// Redirect to login screen.
|
|
|
|
echo '<script>alert("'. t("Wrong login/password.") .'");document.location=\'?do=login'.$redir.'\';</script>';
|
2013-02-26 10:09:41 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------
|
|
|
|
// Token management for XSRF protection
|
|
|
|
// Token should be used in any form which acts on data (create,update,delete,import...).
|
2018-10-13 00:31:11 +02:00
|
|
|
if (!isset($_SESSION['tokens'])) {
|
|
|
|
$_SESSION['tokens']=array(); // Token are attached to the session.
|
|
|
|
}
|
2013-02-26 10:09:41 +01:00
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2018-02-17 01:46:27 +01:00
|
|
|
* @param ConfigManager $conf Configuration Manager instance
|
|
|
|
* @param LoginManager $loginManager LoginManager instance
|
2016-06-09 20:04:02 +02:00
|
|
|
*/
|
2018-10-13 00:31:11 +02:00
|
|
|
function showDailyRSS($conf, $loginManager)
|
|
|
|
{
|
2013-02-26 10:09:41 +01:00
|
|
|
// Cache system
|
2016-05-10 23:31:41 +02:00
|
|
|
$query = $_SERVER['QUERY_STRING'];
|
2015-07-09 22:14:39 +02:00
|
|
|
$cache = new CachedPage(
|
2016-05-18 21:48:24 +02:00
|
|
|
$conf->get('config.PAGE_CACHE'),
|
2015-09-06 21:31:37 +02:00
|
|
|
page_url($_SERVER),
|
2018-10-13 00:31:11 +02:00
|
|
|
startsWith($query, 'do=dailyrss') && !$loginManager->isLoggedIn()
|
2015-07-09 22:14:39 +02:00
|
|
|
);
|
2015-07-10 15:41:59 +02:00
|
|
|
$cached = $cache->cachedVersion();
|
|
|
|
if (!empty($cached)) {
|
|
|
|
echo $cached;
|
|
|
|
exit;
|
|
|
|
}
|
2015-06-23 22:34:07 +02:00
|
|
|
|
2015-07-10 15:41:59 +02:00
|
|
|
// 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).
|
2015-06-23 22:34:07 +02:00
|
|
|
$LINKSDB = new LinkDB(
|
2016-06-11 09:08:02 +02:00
|
|
|
$conf->get('resource.datastore'),
|
2018-02-17 01:46:27 +01:00
|
|
|
$loginManager->isLoggedIn(),
|
2019-02-09 13:52:12 +01:00
|
|
|
$conf->get('privacy.hide_public_links')
|
2015-06-23 22:34:07 +02:00
|
|
|
);
|
2013-03-04 10:18:39 +01:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
/* Some Shaarlies may have very few links, so we need to look
|
2016-11-28 16:16:44 +01:00
|
|
|
back in time until we have enough days ($nb_of_days).
|
2013-02-26 10:09:41 +01:00
|
|
|
*/
|
2015-07-10 15:41:59 +02:00
|
|
|
$nb_of_days = 7; // We take 7 days.
|
2016-05-18 21:48:24 +02:00
|
|
|
$today = date('Ymd');
|
2015-07-10 15:41:59 +02:00
|
|
|
$days = array();
|
|
|
|
|
2016-11-28 18:24:15 +01:00
|
|
|
foreach ($LINKSDB as $link) {
|
|
|
|
$day = $link['created']->format('Ymd'); // Extract day (without time)
|
2016-11-28 16:16:44 +01:00
|
|
|
if (strcmp($day, $today) < 0) {
|
2015-07-10 15:41:59 +02:00
|
|
|
if (empty($days[$day])) {
|
|
|
|
$days[$day] = array();
|
|
|
|
}
|
2016-11-28 18:24:15 +01:00
|
|
|
$days[$day][] = $link;
|
2015-07-10 15:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count($days) > $nb_of_days) {
|
|
|
|
break; // Have we collected enough days?
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-04 10:18:39 +01:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
// Build the RSS feed.
|
|
|
|
header('Content-Type: application/rss+xml; charset=utf-8');
|
2015-09-06 21:31:37 +02:00
|
|
|
$pageaddr = escape(index_url($_SERVER));
|
2013-02-26 10:09:41 +01:00
|
|
|
echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">';
|
2015-07-10 15:41:59 +02:00
|
|
|
echo '<channel>';
|
2016-05-29 16:10:32 +02:00
|
|
|
echo '<title>Daily - '. $conf->get('general.title') . '</title>';
|
2015-07-10 15:41:59 +02:00
|
|
|
echo '<link>'. $pageaddr .'</link>';
|
|
|
|
echo '<description>Daily shared links</description>';
|
|
|
|
echo '<language>en-en</language>';
|
|
|
|
echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL;
|
|
|
|
|
|
|
|
// For each day.
|
2016-11-28 18:24:15 +01:00
|
|
|
foreach ($days as $day => $links) {
|
2016-02-17 22:46:50 +01:00
|
|
|
$dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
|
2015-09-06 21:31:37 +02:00
|
|
|
$absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page.
|
2013-03-04 10:18:39 +01:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
// We pre-format some fields for proper output.
|
2016-11-28 18:24:15 +01:00
|
|
|
foreach ($links as &$link) {
|
2019-02-09 13:52:12 +01:00
|
|
|
$link['formatedDescription'] = format_description($link['description']);
|
2016-11-28 18:24:15 +01:00
|
|
|
$link['timestamp'] = $link['created']->getTimestamp();
|
2019-02-09 14:13:08 +01:00
|
|
|
if (is_note($link['url'])) {
|
2016-11-28 18:24:15 +01:00
|
|
|
$link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute
|
2015-07-10 15:41:59 +02:00
|
|
|
}
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
2015-07-10 15:41:59 +02:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
// Then build the HTML for this day:
|
2013-03-04 10:18:39 +01:00
|
|
|
$tpl = new RainTPL;
|
2016-05-29 16:10:32 +02:00
|
|
|
$tpl->assign('title', $conf->get('general.title'));
|
2016-02-17 22:46:50 +01:00
|
|
|
$tpl->assign('daydate', $dayDate->getTimestamp());
|
2015-07-10 15:41:59 +02:00
|
|
|
$tpl->assign('absurl', $absurl);
|
|
|
|
$tpl->assign('links', $links);
|
2016-02-17 22:46:50 +01:00
|
|
|
$tpl->assign('rssdate', escape($dayDate->format(DateTime::RSS)));
|
2016-06-11 09:08:02 +02:00
|
|
|
$tpl->assign('hide_timestamps', $conf->get('privacy.hide_timestamps', false));
|
2018-07-29 17:40:05 +02:00
|
|
|
$tpl->assign('index_url', $pageaddr);
|
2017-01-05 19:20:41 +01:00
|
|
|
$html = $tpl->draw('dailyrss', true);
|
2013-02-26 10:09:41 +01:00
|
|
|
|
2015-07-10 15:41:59 +02:00
|
|
|
echo $html . PHP_EOL;
|
2013-03-04 10:18:39 +01:00
|
|
|
}
|
2015-09-06 21:31:37 +02:00
|
|
|
echo '</channel></rss><!-- Cached version of '. escape(page_url($_SERVER)) .' -->';
|
2013-03-04 10:18:39 +01:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
$cache->cache(ob_get_contents());
|
|
|
|
ob_end_flush();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2015-12-07 11:25:11 +01:00
|
|
|
/**
|
|
|
|
* Show the 'Daily' page.
|
|
|
|
*
|
2016-06-09 20:04:02 +02:00
|
|
|
* @param PageBuilder $pageBuilder Template engine wrapper.
|
|
|
|
* @param LinkDB $LINKSDB LinkDB instance.
|
|
|
|
* @param ConfigManager $conf Configuration Manager instance.
|
2018-04-18 23:45:05 +02:00
|
|
|
* @param PluginManager $pluginManager Plugin Manager instance.
|
|
|
|
* @param LoginManager $loginManager Login Manager instance
|
2015-12-07 11:25:11 +01:00
|
|
|
*/
|
2018-04-18 23:45:05 +02:00
|
|
|
function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager, $loginManager)
|
2013-02-26 10:09:41 +01:00
|
|
|
{
|
2017-08-27 19:36:48 +02:00
|
|
|
$day = date('Ymd', strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
|
|
|
|
if (isset($_GET['day'])) {
|
2018-10-13 00:31:11 +02:00
|
|
|
$day = $_GET['day'];
|
2017-08-27 19:36:48 +02:00
|
|
|
}
|
2013-03-04 10:18:39 +01:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
$days = $LINKSDB->days();
|
2017-08-27 19:36:48 +02:00
|
|
|
$i = array_search($day, $days);
|
|
|
|
if ($i === false && count($days)) {
|
|
|
|
// no links for day, but at least one day with links
|
|
|
|
$i = count($days) - 1;
|
|
|
|
$day = $days[$i];
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
2017-08-27 19:36:48 +02:00
|
|
|
$previousday = '';
|
|
|
|
$nextday = '';
|
2013-02-26 10:09:41 +01:00
|
|
|
|
2017-08-27 19:36:48 +02:00
|
|
|
if ($i !== false) {
|
|
|
|
if ($i >= 1) {
|
|
|
|
$previousday=$days[$i - 1];
|
|
|
|
}
|
|
|
|
if ($i < count($days) - 1) {
|
2018-10-13 00:31:11 +02:00
|
|
|
$nextday = $days[$i + 1];
|
2017-08-27 19:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-27 14:57:44 +02:00
|
|
|
try {
|
2016-03-21 21:40:49 +01:00
|
|
|
$linksToDisplay = $LINKSDB->filterDay($day);
|
2015-06-27 14:57:44 +02:00
|
|
|
} catch (Exception $exc) {
|
|
|
|
error_log($exc);
|
2015-07-11 01:29:12 +02:00
|
|
|
$linksToDisplay = array();
|
2015-06-27 14:57:44 +02:00
|
|
|
}
|
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
// We pre-format some fields for proper output.
|
2018-10-13 00:31:11 +02:00
|
|
|
foreach ($linksToDisplay as $key => $link) {
|
|
|
|
$taglist = explode(' ', $link['tags']);
|
2013-03-01 17:09:52 +01:00
|
|
|
uasort($taglist, 'strcasecmp');
|
|
|
|
$linksToDisplay[$key]['taglist']=$taglist;
|
2019-02-09 13:52:12 +01:00
|
|
|
$linksToDisplay[$key]['formatedDescription'] = format_description($link['description']);
|
2016-11-28 16:16:44 +01:00
|
|
|
$linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp();
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
2013-03-04 10:18:39 +01:00
|
|
|
|
2018-02-01 13:16:58 +01:00
|
|
|
$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. */
|
2018-02-17 01:46:27 +01:00
|
|
|
$pluginManager->executeHooks('render_daily', $data, array('loggedin' => $loginManager->isLoggedIn()));
|
2018-02-01 13:16:58 +01:00
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
/* We need to spread the articles on 3 columns.
|
2014-08-11 20:41:50 +02:00
|
|
|
I did not want to use a JavaScript lib like http://masonry.desandro.com/
|
2013-03-04 10:18:39 +01:00
|
|
|
so I manually spread entries with a simple method: I roughly evaluate the
|
2013-02-26 10:09:41 +01:00
|
|
|
height of a div according to title and description length.
|
|
|
|
*/
|
2017-08-27 19:36:48 +02:00
|
|
|
$columns = array(array(), array(), array()); // Entries to display, for each column.
|
|
|
|
$fill = array(0, 0, 0); // Rough estimate of columns fill.
|
2018-10-13 00:31:11 +02:00
|
|
|
foreach ($data['linksToDisplay'] as $key => $link) {
|
2013-02-26 10:09:41 +01:00
|
|
|
// 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.
|
2014-08-11 20:41:50 +02:00
|
|
|
// This is not perfect, but it's usually OK.
|
2017-08-27 19:36:48 +02:00
|
|
|
$length = strlen($link['title']) + (342 * strlen($link['description'])) / 836;
|
|
|
|
if ($link['thumbnail']) {
|
2018-10-13 00:31:11 +02:00
|
|
|
$length += 100; // 1 thumbnails roughly takes 100 pixels height.
|
2017-08-27 19:36:48 +02:00
|
|
|
}
|
2013-02-26 10:09:41 +01:00
|
|
|
// Then put in column which is the less filled:
|
2017-08-27 19:36:48 +02:00
|
|
|
$smallest = min($fill); // find smallest value in array.
|
|
|
|
$index = array_search($smallest, $fill); // find index of this smallest value.
|
|
|
|
array_push($columns[$index], $link); // Put entry in this column.
|
|
|
|
$fill[$index] += $length;
|
2013-02-26 10:09:41 +01:00
|
|
|
}
|
2015-12-07 11:25:11 +01:00
|
|
|
|
2018-02-01 13:16:58 +01:00
|
|
|
$data['cols'] = $columns;
|
2015-07-15 11:42:15 +02:00
|
|
|
|
|
|
|
foreach ($data as $key => $value) {
|
2015-12-07 11:25:11 +01:00
|
|
|
$pageBuilder->assign($key, $value);
|
2015-07-15 11:42:15 +02:00
|
|
|
}
|
|
|
|
|
2018-01-24 19:38:03 +01:00
|
|
|
$pageBuilder->assign('pagetitle', t('Daily') .' - '. $conf->get('general.title', 'Shaarli'));
|
2015-12-07 11:25:11 +01:00
|
|
|
$pageBuilder->renderPage('daily');
|
2013-02-26 10:09:41 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
/**
|
|
|
|
* Renders the linklist
|
|
|
|
*
|
|
|
|
* @param pageBuilder $PAGE pageBuilder instance.
|
|
|
|
* @param LinkDB $LINKSDB LinkDB instance.
|
|
|
|
* @param ConfigManager $conf Configuration Manager instance.
|
|
|
|
* @param PluginManager $pluginManager Plugin Manager instance.
|
|
|
|
*/
|
2018-10-13 00:31:11 +02:00
|
|
|
function showLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager)
|
|
|
|
{
|
|
|
|
buildLinkList($PAGE, $LINKSDB, $conf, $pluginManager, $loginManager);
|
2015-07-15 11:42:15 +02:00
|
|
|
$PAGE->renderPage('linklist');
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
/**
|
|
|
|
* Render HTML page (according to URL parameters and user rights)
|
|
|
|
*
|
2017-10-22 18:44:46 +02:00
|
|
|
* @param ConfigManager $conf Configuration Manager instance.
|
|
|
|
* @param PluginManager $pluginManager Plugin Manager instance,
|
|
|
|
* @param LinkDB $LINKSDB
|
|
|
|
* @param History $history instance
|
|
|
|
* @param SessionManager $sessionManager SessionManager instance
|
2017-10-25 23:03:31 +02:00
|
|
|
* @param LoginManager $loginManager LoginManager instance
|
2016-06-09 20:04:02 +02:00
|
|
|
*/
|
2017-10-25 23:03:31 +02:00
|
|
|
function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager, $loginManager)
|
2013-02-26 10:09:41 +01:00
|
|
|
{
|
2016-01-12 19:50:48 +01:00
|
|
|
$updater = new Updater(
|
2016-06-11 09:08:02 +02:00
|
|
|
read_updates_file($conf->get('resource.updates')),
|
2016-01-12 19:50:48 +01:00
|
|
|
$LINKSDB,
|
2016-06-09 20:04:02 +02:00
|
|
|
$conf,
|
2018-06-08 12:50:49 +02:00
|
|
|
$loginManager->isLoggedIn(),
|
|
|
|
$_SESSION
|
2016-01-12 19:50:48 +01:00
|
|
|
);
|
|
|
|
try {
|
|
|
|
$newUpdates = $updater->update();
|
|
|
|
if (! empty($newUpdates)) {
|
|
|
|
write_updates_file(
|
2016-06-11 09:08:02 +02:00
|
|
|
$conf->get('resource.updates'),
|
2016-01-12 19:50:48 +01:00
|
|
|
$updater->getDoneUpdates()
|
|
|
|
);
|
|
|
|
}
|
2018-10-13 00:31:11 +02:00
|
|
|
} catch (Exception $e) {
|
2016-01-12 19:50:48 +01:00
|
|
|
die($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2018-06-08 12:50:49 +02:00
|
|
|
$PAGE = new PageBuilder($conf, $_SESSION, $LINKSDB, $sessionManager->generateToken(), $loginManager->isLoggedIn());
|
2016-05-11 00:05:22 +02:00
|
|
|
$PAGE->assign('linkcount', count($LINKSDB));
|
|
|
|
$PAGE->assign('privateLinkcount', count_private($LINKSDB));
|
2016-10-14 13:22:58 +02:00
|
|
|
$PAGE->assign('plugin_errors', $pluginManager->getErrors());
|
2015-07-15 11:42:15 +02:00
|
|
|
|
|
|
|
// Determine which page will be rendered.
|
|
|
|
$query = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : '';
|
2018-02-17 01:46:27 +01:00
|
|
|
$targetPage = Router::findPage($query, $_GET, $loginManager->isLoggedIn());
|
2015-07-15 11:42:15 +02:00
|
|
|
|
2018-10-13 00:31:11 +02:00
|
|
|
if (// if the user isn't logged in
|
2018-02-17 01:46:27 +01:00
|
|
|
!$loginManager->isLoggedIn() &&
|
2017-08-31 00:39:15 +02:00
|
|
|
// and Shaarli doesn't have public content...
|
|
|
|
$conf->get('privacy.hide_public_links') &&
|
|
|
|
// and is configured to enforce the login
|
|
|
|
$conf->get('privacy.force_login') &&
|
|
|
|
// and the current page isn't already the login page
|
|
|
|
$targetPage !== Router::$PAGE_LOGIN &&
|
|
|
|
// and the user is not requesting a feed (which would lead to a different content-type as expected)
|
|
|
|
$targetPage !== Router::$PAGE_FEED_ATOM &&
|
|
|
|
$targetPage !== Router::$PAGE_FEED_RSS
|
|
|
|
) {
|
|
|
|
// force current page to be the login page
|
|
|
|
$targetPage = Router::$PAGE_LOGIN;
|
|
|
|
}
|
|
|
|
|
2015-07-15 11:42:15 +02:00
|
|
|
// Call plugin hooks for header, footer and includes, specifying which page will be rendered.
|
|
|
|
// Then assign generated data to RainTPL.
|
|
|
|
$common_hooks = array(
|
2016-02-10 15:40:11 +01:00
|
|
|
'includes',
|
2015-07-15 11:42:15 +02:00
|
|
|
'header',
|
|
|
|
'footer',
|
|
|
|
);
|
2016-06-09 20:04:02 +02:00
|
|
|
|
2018-10-13 00:31:11 +02:00
|
|
|
foreach ($common_hooks as $name) {
|
2015-07-15 11:42:15 +02:00
|
|
|
$plugin_data = array();
|
2018-10-13 00:31:11 +02:00
|
|
|
$pluginManager->executeHooks(
|
|
|
|
'render_' . $name,
|
|
|
|
$plugin_data,
|
2015-07-15 11:42:15 +02:00
|
|
|
array(
|
|
|
|
'target' => $targetPage,
|
2018-02-17 01:46:27 +01:00
|
|
|
'loggedin' => $loginManager->isLoggedIn()
|
2015-07-15 11:42:15 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$PAGE->assign('plugins_' . $name, $plugin_data);
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:09:41 +01:00
|
|
|
// -------- Display login form.
|
2018-10-13 00:31:11 +02:00
|
|
|
if ($targetPage == Router::$PAGE_LOGIN) {
|
|
|
|
if ($conf->get('security.open_shaarli')) {
|
|
|
|
header('Location: ?');
|
|
|
|
exit;
|
|
|
|
} // No need to login for open Shaarli
|
2016-05-06 20:03:10 +02:00
|
|
|
if (isset($_GET['username'])) {
|
|
|
|
$PAGE->assign('username', escape($_GET['username']));
|
|
|
|
}
|
2018-10-13 00:31:11 +02:00
|
|
|
$PAGE->assign('returnurl', (isset($_SERVER['HTTP_REFERER']) ? escape($_SERVER['HTTP_REFERER']):''));
|
2017-08-26 09:27:10 +02:00
|
|
|
// add default state of the 'remember me' checkbox
|
|
|
|
$PAGE->assign('remember_user_default', $conf->get('privacy.remember_user_default'));
|
2017-10-25 23:03:31 +02:00
|
|
|
$PAGE->assign('user_can_login', $loginManager->canLogin($_SERVER));
|
2018-01-24 19:38:03 +01:00
|
|
|
$PAGE->assign('pagetitle', t('Login') .' - '. $conf->get('general.title', 'Shaarli'));
|
2013-02-26 10:09:41 +01:00
|
|
|
$PAGE->renderPage('loginform');
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
// -------- User wants to logout.
|
2018-10-13 00:31:11 +02:00
|
|
|
if (isset($_SERVER['QUERY_STRING']) && startsWith($_SERVER['QUERY_STRING'], 'do=logout')) {
|
2016-06-11 09:08:02 +02:00
|
|
|
invalidateCaches($conf->get('resource.page_cache'));
|
2018-04-27 23:17:38 +02:00
|
|
|
$sessionManager->logout();
|
2018-05-06 17:06:36 +02:00
|
|
|
setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, WEB_PATH);
|
2013-02-26 10:09:41 +01:00
|
|
|
header('Location: ?');
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------- Picture wall
|
2018-10-13 00:31:11 +02:00
|
|
|
if ($targetPage == Router::$PAGE_PICWALL) {
|
2018-07-05 20:29:55 +02:00
|
|
|
$PAGE->assign('pagetitle', t('Picture wall') .' - '. $conf->get('general.title', 'Shaarli'));
|
|
|
|
if (! $conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) === Thumbnailer::MODE_NONE) {
|
|
|
|
$PAGE->assign('linksToDisplay', []);
|
|