3 namespace Shaarli\Render
;
6 use Psr\Log\LoggerInterface
;
8 use Shaarli\Bookmark\BookmarkServiceInterface
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Helper\ApplicationUtils
;
11 use Shaarli\Security\SessionManager
;
12 use Shaarli\Thumbnailer
;
15 * This class is in charge of building the final page.
16 * (This is basically a wrapper around RainTPL which pre-fills some fields.)
17 * $p = new PageBuilder();
18 * $p->assign('myfield','myvalue');
19 * $p->renderPage('mytemplate');
24 * @var RainTPL RainTPL instance.
29 * @var ConfigManager $conf Configuration Manager instance.
34 * @var array $_SESSION
38 /** @var LoggerInterface */
42 * @var BookmarkServiceInterface $bookmarkService instance.
44 protected $bookmarkService;
47 * @var null|string XSRF token
52 * @var bool $isLoggedIn Whether the user is logged in
54 protected $isLoggedIn = false;
57 * PageBuilder constructor.
58 * $tpl is initialized at false for lazy loading.
60 * @param ConfigManager $conf Configuration Manager instance (reference).
61 * @param array $session $_SESSION array
62 * @param LoggerInterface $logger
63 * @param null $linkDB instance.
64 * @param null $token Session token
65 * @param bool $isLoggedIn
67 public function __construct(
70 LoggerInterface
$logger,
77 $this->session
= $session;
78 $this->logger
= $logger;
79 $this->bookmarkService
= $linkDB;
80 $this->token
= $token;
81 $this->isLoggedIn
= $isLoggedIn;
85 * Reset current state of template rendering.
86 * Mostly useful for error handling. We remove everything, and display the error template.
88 public function reset(): void
94 * Initialize all default tpl tags.
96 private function initialize()
98 $this->tpl
= new RainTPL();
101 $version = ApplicationUtils
::checkUpdate(
103 $this->conf
->get('resource.update_check'),
104 $this->conf
->get('updates.check_updates_interval'),
105 $this->conf
->get('updates.check_updates'),
107 $this->conf
->get('updates.check_updates_branch')
109 $this->tpl
->assign('newVersion', escape($version));
110 $this->tpl
->assign('versionError', '');
111 } catch (Exception
$exc) {
112 $this->logger
->error(format_log('Error: ' . $exc->getMessage(), client_ip_id($_SERVER)));
113 $this->tpl
->assign('newVersion', '');
114 $this->tpl
->assign('versionError', escape($exc->getMessage()));
117 $this->tpl
->assign('is_logged_in', $this->isLoggedIn
);
118 $this->tpl
->assign('feedurl', escape(index_url($_SERVER)));
119 $searchcrits = ''; // Search criteria
120 if (!empty($_GET['searchtags'])) {
121 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
123 if (!empty($_GET['searchterm'])) {
124 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
126 $this->tpl
->assign('searchcrits', $searchcrits);
127 $this->tpl
->assign('source', index_url($_SERVER));
128 $this->tpl
->assign('version', SHAARLI_VERSION
);
131 ApplicationUtils
::getVersionHash(SHAARLI_VERSION
, $this->conf
->get('credentials.salt'))
133 $this->tpl
->assign('index_url', index_url($_SERVER));
134 $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
135 $this->tpl
->assign('visibility', $visibility);
136 $this->tpl
->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
137 $this->tpl
->assign('pagetitle', $this->conf
->get('general.title', 'Shaarli'));
138 if ($this->conf
->exists('general.header_link')) {
139 $this->tpl
->assign('titleLink', $this->conf
->get('general.header_link'));
141 $this->tpl
->assign('shaarlititle', $this->conf
->get('general.title', 'Shaarli'));
142 $this->tpl
->assign('openshaarli', $this->conf
->get('security.open_shaarli', false));
143 $this->tpl
->assign('showatom', $this->conf
->get('feed.show_atom', true));
144 $this->tpl
->assign('feed_type', $this->conf
->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
145 $this->tpl
->assign('hide_timestamps', $this->conf
->get('privacy.hide_timestamps', false));
146 $this->tpl
->assign('token', $this->token
);
148 $this->tpl
->assign('language', $this->conf
->get('translation.language'));
150 if ($this->bookmarkService
!== null) {
151 $this->tpl
->assign('tags', escape($this->bookmarkService
->bookmarksCountPerTag()));
155 'thumbnails_enabled',
156 $this->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
158 $this->tpl
->assign('thumbnails_width', $this->conf
->get('thumbnails.width'));
159 $this->tpl
->assign('thumbnails_height', $this->conf
->get('thumbnails.height'));
161 $this->tpl
->assign('formatter', $this->conf
->get('formatter', 'default'));
163 $this->tpl
->assign('links_per_page', $this->session
['LINKS_PER_PAGE'] ?? 20);
164 $this->tpl
->assign('tags_separator', $this->conf
->get('general.tags_separator', ' '));
166 // To be removed with a proper theme configuration.
167 $this->tpl
->assign('conf', $this->conf
);
171 * Affect variable after controller processing.
172 * Used for alert messages.
174 protected function finalize(string $basePath): void
176 // TODO: use the SessionManager
178 SessionManager
::KEY_SUCCESS_MESSAGES
,
179 SessionManager
::KEY_WARNING_MESSAGES
,
180 SessionManager
::KEY_ERROR_MESSAGES
182 foreach ($messageKeys as $messageKey) {
183 if (!empty($_SESSION[$messageKey])) {
184 $this->tpl
->assign('global_' . $messageKey, $_SESSION[$messageKey]);
185 unset($_SESSION[$messageKey]);
189 $rootPath = preg_replace('#/index\.php$#', '', $basePath);
190 $this->assign('base_path', $basePath);
191 $this->assign('root_path', $rootPath);
195 rtrim($this->conf
->get('resource.raintpl_tpl', 'tpl'), '/') . '/' .
196 $this->conf
->get('resource.theme', 'default')
201 * The following assign() method is basically the same as RainTPL (except lazy loading)
203 * @param string $placeholder Template placeholder.
204 * @param mixed $value Value to assign.
206 public function assign($placeholder, $value)
208 if ($this->tpl
=== false) {
211 $this->tpl
->assign($placeholder, $value);
215 * Assign an array of data to the template builder.
217 * @param array $data Data to assign.
219 * @return false if invalid data.
221 public function assignAll($data)
223 if ($this->tpl
=== false) {
227 if (empty($data) || !is_array($data)) {
231 foreach ($data as $key => $value) {
232 $this->assign($key, $value);
238 * Render a specific page as string (using a template file).
239 * e.g. $pb->render('picwall');
241 * @param string $page Template filename (without extension).
243 * @return string Processed template content
245 public function render(string $page, string $basePath): string
247 if ($this->tpl
=== false) {
251 $this->finalize($basePath);
253 return $this->tpl
->draw($page, true);