3 namespace Shaarli\Render
;
6 use exceptions\MissingBasePathException
;
8 use Shaarli\ApplicationUtils
;
9 use Shaarli\Bookmark\BookmarkServiceInterface
;
10 use Shaarli\Config\ConfigManager
;
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
39 * @var BookmarkServiceInterface $bookmarkService instance.
41 protected $bookmarkService;
44 * @var null|string XSRF token
49 * @var bool $isLoggedIn Whether the user is logged in
51 protected $isLoggedIn = false;
54 * PageBuilder constructor.
55 * $tpl is initialized at false for lazy loading.
57 * @param ConfigManager $conf Configuration Manager instance (reference).
58 * @param array $session $_SESSION array
59 * @param BookmarkServiceInterface $linkDB instance.
60 * @param string $token Session token
61 * @param bool $isLoggedIn
63 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
67 $this->session
= $session;
68 $this->bookmarkService
= $linkDB;
69 $this->token
= $token;
70 $this->isLoggedIn
= $isLoggedIn;
74 * Reset current state of template rendering.
75 * Mostly useful for error handling. We remove everything, and display the error template.
77 public function reset(): void
83 * Initialize all default tpl tags.
85 private function initialize()
87 $this->tpl
= new RainTPL();
90 $version = ApplicationUtils
::checkUpdate(
92 $this->conf
->get('resource.update_check'),
93 $this->conf
->get('updates.check_updates_interval'),
94 $this->conf
->get('updates.check_updates'),
96 $this->conf
->get('updates.check_updates_branch')
98 $this->tpl
->assign('newVersion', escape($version));
99 $this->tpl
->assign('versionError', '');
100 } catch (Exception
$exc) {
101 logm($this->conf
->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
102 $this->tpl
->assign('newVersion', '');
103 $this->tpl
->assign('versionError', escape($exc->getMessage()));
106 $this->tpl
->assign('is_logged_in', $this->isLoggedIn
);
107 $this->tpl
->assign('feedurl', escape(index_url($_SERVER)));
108 $searchcrits = ''; // Search criteria
109 if (!empty($_GET['searchtags'])) {
110 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
112 if (!empty($_GET['searchterm'])) {
113 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
115 $this->tpl
->assign('searchcrits', $searchcrits);
116 $this->tpl
->assign('source', index_url($_SERVER));
117 $this->tpl
->assign('version', SHAARLI_VERSION
);
120 ApplicationUtils
::getVersionHash(SHAARLI_VERSION
, $this->conf
->get('credentials.salt'))
122 $this->tpl
->assign('index_url', index_url($_SERVER));
123 $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
124 $this->tpl
->assign('visibility', $visibility);
125 $this->tpl
->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
126 $this->tpl
->assign('pagetitle', $this->conf
->get('general.title', 'Shaarli'));
127 if ($this->conf
->exists('general.header_link')) {
128 $this->tpl
->assign('titleLink', $this->conf
->get('general.header_link'));
130 $this->tpl
->assign('shaarlititle', $this->conf
->get('general.title', 'Shaarli'));
131 $this->tpl
->assign('openshaarli', $this->conf
->get('security.open_shaarli', false));
132 $this->tpl
->assign('showatom', $this->conf
->get('feed.show_atom', true));
133 $this->tpl
->assign('feed_type', $this->conf
->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
134 $this->tpl
->assign('hide_timestamps', $this->conf
->get('privacy.hide_timestamps', false));
135 $this->tpl
->assign('token', $this->token
);
137 $this->tpl
->assign('language', $this->conf
->get('translation.language'));
139 if ($this->bookmarkService
!== null) {
140 $this->tpl
->assign('tags', $this->bookmarkService
->bookmarksCountPerTag());
144 'thumbnails_enabled',
145 $this->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
147 $this->tpl
->assign('thumbnails_width', $this->conf
->get('thumbnails.width'));
148 $this->tpl
->assign('thumbnails_height', $this->conf
->get('thumbnails.height'));
150 $this->tpl
->assign('formatter', $this->conf
->get('formatter', 'default'));
152 $this->tpl
->assign('links_per_page', $this->session
['LINKS_PER_PAGE']);
154 // To be removed with a proper theme configuration.
155 $this->tpl
->assign('conf', $this->conf
);
159 * Affect variable after controller processing.
160 * Used for alert messages.
162 protected function finalize(string $basePath): void
164 // TODO: use the SessionManager
166 SessionManager
::KEY_SUCCESS_MESSAGES
,
167 SessionManager
::KEY_WARNING_MESSAGES
,
168 SessionManager
::KEY_ERROR_MESSAGES
170 foreach ($messageKeys as $messageKey) {
171 if (!empty($_SESSION[$messageKey])) {
172 $this->tpl
->assign('global_' . $messageKey, $_SESSION[$messageKey]);
173 unset($_SESSION[$messageKey]);
177 $this->assign('base_path', $basePath);
181 rtrim($this->conf
->get('resource.raintpl_tpl', 'tpl'), '/') . '/' .
182 $this->conf
->get('resource.theme', 'default')
187 * The following assign() method is basically the same as RainTPL (except lazy loading)
189 * @param string $placeholder Template placeholder.
190 * @param mixed $value Value to assign.
192 public function assign($placeholder, $value)
194 if ($this->tpl
=== false) {
197 $this->tpl
->assign($placeholder, $value);
201 * Assign an array of data to the template builder.
203 * @param array $data Data to assign.
205 * @return false if invalid data.
207 public function assignAll($data)
209 if ($this->tpl
=== false) {
213 if (empty($data) || !is_array($data)) {
217 foreach ($data as $key => $value) {
218 $this->assign($key, $value);
224 * Render a specific page as string (using a template file).
225 * e.g. $pb->render('picwall');
227 * @param string $page Template filename (without extension).
229 * @return string Processed template content
231 public function render(string $page, string $basePath): string
233 if ($this->tpl
=== false) {
237 $this->finalize($basePath);
239 return $this->tpl
->draw($page, true);