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 // To be removed with a proper theme configuration.
153 $this->tpl
->assign('conf', $this->conf
);
157 * Affect variable after controller processing.
158 * Used for alert messages.
160 protected function finalize(string $basePath): void
162 // TODO: use the SessionManager
164 SessionManager
::KEY_SUCCESS_MESSAGES
,
165 SessionManager
::KEY_WARNING_MESSAGES
,
166 SessionManager
::KEY_ERROR_MESSAGES
168 foreach ($messageKeys as $messageKey) {
169 if (!empty($_SESSION[$messageKey])) {
170 $this->tpl
->assign('global_' . $messageKey, $_SESSION[$messageKey]);
171 unset($_SESSION[$messageKey]);
175 $this->assign('base_path', $basePath);
179 rtrim($this->conf
->get('resource.raintpl_tpl', 'tpl'), '/') . '/' .
180 $this->conf
->get('resource.theme', 'default')
185 * The following assign() method is basically the same as RainTPL (except lazy loading)
187 * @param string $placeholder Template placeholder.
188 * @param mixed $value Value to assign.
190 public function assign($placeholder, $value)
192 if ($this->tpl
=== false) {
195 $this->tpl
->assign($placeholder, $value);
199 * Assign an array of data to the template builder.
201 * @param array $data Data to assign.
203 * @return false if invalid data.
205 public function assignAll($data)
207 if ($this->tpl
=== false) {
211 if (empty($data) || !is_array($data)) {
215 foreach ($data as $key => $value) {
216 $this->assign($key, $value);
222 * Render a specific page as string (using a template file).
223 * e.g. $pb->render('picwall');
225 * @param string $page Template filename (without extension).
227 * @return string Processed template content
229 public function render(string $page, string $basePath): string
231 if ($this->tpl
=== false) {
235 $this->finalize($basePath);
237 return $this->tpl
->draw($page, true);