2016-05-10 23:48:51 +02:00
|
|
|
<?php
|
|
|
|
|
2018-12-03 00:46:04 +01:00
|
|
|
namespace Shaarli\Render;
|
|
|
|
|
|
|
|
use Exception;
|
2023-05-24 11:35:15 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-12-03 00:46:04 +01:00
|
|
|
use RainTPL;
|
2023-05-24 11:35:15 +02:00
|
|
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
2017-03-22 19:16:35 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2023-05-24 11:35:15 +02:00
|
|
|
use Shaarli\Helper\ApplicationUtils;
|
|
|
|
use Shaarli\Security\SessionManager;
|
2018-07-05 20:29:55 +02:00
|
|
|
use Shaarli\Thumbnailer;
|
2017-03-22 19:16:35 +01:00
|
|
|
|
2016-05-10 23:48:51 +02:00
|
|
|
/**
|
|
|
|
* This class is in charge of building the final page.
|
|
|
|
* (This is basically a wrapper around RainTPL which pre-fills some fields.)
|
|
|
|
* $p = new PageBuilder();
|
|
|
|
* $p->assign('myfield','myvalue');
|
|
|
|
* $p->renderPage('mytemplate');
|
|
|
|
*/
|
|
|
|
class PageBuilder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var RainTPL RainTPL instance.
|
|
|
|
*/
|
|
|
|
private $tpl;
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
/**
|
|
|
|
* @var ConfigManager $conf Configuration Manager instance.
|
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
2018-06-08 12:50:49 +02:00
|
|
|
/**
|
|
|
|
* @var array $_SESSION
|
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
protected $logger;
|
|
|
|
|
2017-05-07 18:19:09 +02:00
|
|
|
/**
|
2023-05-24 11:35:15 +02:00
|
|
|
* @var BookmarkServiceInterface $bookmarkService instance.
|
2017-05-07 18:19:09 +02:00
|
|
|
*/
|
2023-05-24 11:35:15 +02:00
|
|
|
protected $bookmarkService;
|
2018-06-08 12:50:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var null|string XSRF token
|
|
|
|
*/
|
|
|
|
protected $token;
|
|
|
|
|
2018-12-03 00:46:04 +01:00
|
|
|
/**
|
|
|
|
* @var bool $isLoggedIn Whether the user is logged in
|
|
|
|
*/
|
2018-04-18 23:45:05 +02:00
|
|
|
protected $isLoggedIn = false;
|
2017-05-07 18:19:09 +02:00
|
|
|
|
2016-05-10 23:48:51 +02:00
|
|
|
/**
|
|
|
|
* PageBuilder constructor.
|
|
|
|
* $tpl is initialized at false for lazy loading.
|
2016-06-09 20:04:02 +02:00
|
|
|
*
|
2023-05-24 11:35:15 +02:00
|
|
|
* @param ConfigManager $conf Configuration Manager instance (reference).
|
|
|
|
* @param array $session $_SESSION array
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
* @param null $linkDB instance.
|
|
|
|
* @param null $token Session token
|
|
|
|
* @param bool $isLoggedIn
|
2016-05-10 23:48:51 +02:00
|
|
|
*/
|
2023-05-24 11:35:15 +02:00
|
|
|
public function __construct(
|
|
|
|
ConfigManager &$conf,
|
|
|
|
array $session,
|
|
|
|
LoggerInterface $logger,
|
|
|
|
$linkDB = null,
|
|
|
|
$token = null,
|
|
|
|
$isLoggedIn = false
|
|
|
|
) {
|
2016-05-10 23:48:51 +02:00
|
|
|
$this->tpl = false;
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->conf = $conf;
|
2018-06-08 12:50:49 +02:00
|
|
|
$this->session = $session;
|
2023-05-24 11:35:15 +02:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->bookmarkService = $linkDB;
|
2017-10-22 18:44:46 +02:00
|
|
|
$this->token = $token;
|
2018-04-18 23:45:05 +02:00
|
|
|
$this->isLoggedIn = $isLoggedIn;
|
2016-05-10 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
/**
|
|
|
|
* Reset current state of template rendering.
|
|
|
|
* Mostly useful for error handling. We remove everything, and display the error template.
|
|
|
|
*/
|
|
|
|
public function reset(): void
|
|
|
|
{
|
|
|
|
$this->tpl = false;
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:48:51 +02:00
|
|
|
/**
|
|
|
|
* Initialize all default tpl tags.
|
|
|
|
*/
|
|
|
|
private function initialize()
|
|
|
|
{
|
|
|
|
$this->tpl = new RainTPL();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$version = ApplicationUtils::checkUpdate(
|
2017-10-01 11:09:12 +02:00
|
|
|
SHAARLI_VERSION,
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->conf->get('resource.update_check'),
|
|
|
|
$this->conf->get('updates.check_updates_interval'),
|
|
|
|
$this->conf->get('updates.check_updates'),
|
2024-12-10 16:31:21 +01:00
|
|
|
$this->isLoggedIn
|
2016-05-10 23:48:51 +02:00
|
|
|
);
|
|
|
|
$this->tpl->assign('newVersion', escape($version));
|
|
|
|
$this->tpl->assign('versionError', '');
|
|
|
|
} catch (Exception $exc) {
|
2023-05-24 11:35:15 +02:00
|
|
|
$this->logger->error(format_log('Error: ' . $exc->getMessage(), client_ip_id($_SERVER)));
|
2016-05-10 23:48:51 +02:00
|
|
|
$this->tpl->assign('newVersion', '');
|
|
|
|
$this->tpl->assign('versionError', escape($exc->getMessage()));
|
|
|
|
}
|
|
|
|
|
2018-04-18 23:45:05 +02:00
|
|
|
$this->tpl->assign('is_logged_in', $this->isLoggedIn);
|
2016-05-10 23:48:51 +02:00
|
|
|
$this->tpl->assign('feedurl', escape(index_url($_SERVER)));
|
|
|
|
$searchcrits = ''; // Search criteria
|
|
|
|
if (!empty($_GET['searchtags'])) {
|
|
|
|
$searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
|
|
|
|
}
|
|
|
|
if (!empty($_GET['searchterm'])) {
|
|
|
|
$searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
|
|
|
|
}
|
|
|
|
$this->tpl->assign('searchcrits', $searchcrits);
|
|
|
|
$this->tpl->assign('source', index_url($_SERVER));
|
2017-10-01 11:09:12 +02:00
|
|
|
$this->tpl->assign('version', SHAARLI_VERSION);
|
2017-10-01 11:02:48 +02:00
|
|
|
$this->tpl->assign(
|
|
|
|
'version_hash',
|
|
|
|
ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
|
|
|
|
);
|
2018-08-13 10:42:27 +02:00
|
|
|
$this->tpl->assign('index_url', index_url($_SERVER));
|
2018-12-03 00:46:04 +01:00
|
|
|
$visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
|
2017-12-16 12:36:59 +01:00
|
|
|
$this->tpl->assign('visibility', $visibility);
|
2017-06-01 17:55:26 +02:00
|
|
|
$this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
|
2016-07-19 18:03:09 +02:00
|
|
|
$this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
|
2016-06-09 20:04:02 +02:00
|
|
|
if ($this->conf->exists('general.header_link')) {
|
|
|
|
$this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
|
2016-05-10 23:48:51 +02:00
|
|
|
}
|
2016-07-19 18:03:09 +02:00
|
|
|
$this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
|
2017-03-11 14:11:06 +01:00
|
|
|
$this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
|
|
|
|
$this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
|
2017-10-22 18:44:46 +02:00
|
|
|
$this->tpl->assign('token', $this->token);
|
2017-10-01 11:02:48 +02:00
|
|
|
|
2019-02-09 14:29:35 +01:00
|
|
|
$this->tpl->assign('language', $this->conf->get('translation.language'));
|
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
if ($this->bookmarkService !== null) {
|
|
|
|
$this->tpl->assign('tags', escape($this->bookmarkService->bookmarksCountPerTag()));
|
2017-05-07 18:19:09 +02:00
|
|
|
}
|
2016-11-09 18:57:02 +01:00
|
|
|
|
2018-07-05 20:29:55 +02:00
|
|
|
$this->tpl->assign(
|
|
|
|
'thumbnails_enabled',
|
|
|
|
$this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
|
|
|
|
);
|
2016-11-09 18:57:02 +01:00
|
|
|
$this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
|
|
|
|
$this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
|
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
$this->tpl->assign('formatter', $this->conf->get('formatter', 'default'));
|
|
|
|
|
|
|
|
$this->tpl->assign('links_per_page', $this->session['LINKS_PER_PAGE'] ?? 20);
|
|
|
|
$this->tpl->assign('tags_separator', $this->conf->get('general.tags_separator', ' '));
|
2018-06-08 12:50:49 +02:00
|
|
|
|
2016-06-15 18:22:19 +02:00
|
|
|
// To be removed with a proper theme configuration.
|
|
|
|
$this->tpl->assign('conf', $this->conf);
|
2016-05-10 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
/**
|
|
|
|
* Affect variable after controller processing.
|
|
|
|
* Used for alert messages.
|
|
|
|
*/
|
|
|
|
protected function finalize(string $basePath): void
|
|
|
|
{
|
|
|
|
// TODO: use the SessionManager
|
|
|
|
$messageKeys = [
|
|
|
|
SessionManager::KEY_SUCCESS_MESSAGES,
|
|
|
|
SessionManager::KEY_WARNING_MESSAGES,
|
|
|
|
SessionManager::KEY_ERROR_MESSAGES
|
|
|
|
];
|
|
|
|
foreach ($messageKeys as $messageKey) {
|
|
|
|
if (!empty($_SESSION[$messageKey])) {
|
|
|
|
$this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]);
|
|
|
|
unset($_SESSION[$messageKey]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rootPath = preg_replace('#/index\.php$#', '', $basePath);
|
|
|
|
$this->assign('base_path', $basePath);
|
|
|
|
$this->assign('root_path', $rootPath);
|
|
|
|
$this->assign(
|
|
|
|
'asset_path',
|
|
|
|
$rootPath . '/' .
|
|
|
|
rtrim($this->conf->get('resource.raintpl_tpl', 'tpl'), '/') . '/' .
|
|
|
|
$this->conf->get('resource.theme', 'default')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:48:51 +02:00
|
|
|
/**
|
|
|
|
* The following assign() method is basically the same as RainTPL (except lazy loading)
|
|
|
|
*
|
|
|
|
* @param string $placeholder Template placeholder.
|
|
|
|
* @param mixed $value Value to assign.
|
|
|
|
*/
|
|
|
|
public function assign($placeholder, $value)
|
|
|
|
{
|
|
|
|
if ($this->tpl === false) {
|
|
|
|
$this->initialize();
|
|
|
|
}
|
|
|
|
$this->tpl->assign($placeholder, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign an array of data to the template builder.
|
|
|
|
*
|
|
|
|
* @param array $data Data to assign.
|
|
|
|
*
|
|
|
|
* @return false if invalid data.
|
|
|
|
*/
|
|
|
|
public function assignAll($data)
|
|
|
|
{
|
|
|
|
if ($this->tpl === false) {
|
|
|
|
$this->initialize();
|
|
|
|
}
|
|
|
|
|
2018-10-13 00:19:03 +02:00
|
|
|
if (empty($data) || !is_array($data)) {
|
2016-05-10 23:48:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$this->assign($key, $value);
|
|
|
|
}
|
2016-06-09 20:04:02 +02:00
|
|
|
return true;
|
2016-05-10 23:48:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-24 11:35:15 +02:00
|
|
|
* Render a specific page as string (using a template file).
|
|
|
|
* e.g. $pb->render('picwall');
|
2016-05-10 23:48:51 +02:00
|
|
|
*
|
|
|
|
* @param string $page Template filename (without extension).
|
2023-05-24 11:35:15 +02:00
|
|
|
*
|
|
|
|
* @return string Processed template content
|
2016-05-10 23:48:51 +02:00
|
|
|
*/
|
2023-05-24 11:35:15 +02:00
|
|
|
public function render(string $page, string $basePath): string
|
2016-05-10 23:48:51 +02:00
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
if ($this->tpl === false) {
|
2016-05-10 23:48:51 +02:00
|
|
|
$this->initialize();
|
|
|
|
}
|
2016-06-09 20:04:02 +02:00
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
$this->finalize($basePath);
|
2016-05-10 23:48:51 +02:00
|
|
|
|
2023-05-24 11:35:15 +02:00
|
|
|
return $this->tpl->draw($page, true);
|
2016-05-10 23:48:51 +02:00
|
|
|
}
|
|
|
|
}
|