3 namespace Shaarli\Render
;
7 use Shaarli\ApplicationUtils
;
8 use Shaarli\Bookmark\BookmarkServiceInterface
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Security\SessionManager
;
11 use Shaarli\Thumbnailer
;
14 * This class is in charge of building the final page.
15 * (This is basically a wrapper around RainTPL which pre-fills some fields.)
16 * $p = new PageBuilder();
17 * $p->assign('myfield','myvalue');
18 * $p->renderPage('mytemplate');
23 * @var RainTPL RainTPL instance.
28 * @var ConfigManager $conf Configuration Manager instance.
33 * @var array $_SESSION
38 * @var BookmarkServiceInterface $bookmarkService instance.
40 protected $bookmarkService;
43 * @var null|string XSRF token
48 * @var bool $isLoggedIn Whether the user is logged in
50 protected $isLoggedIn = false;
53 * PageBuilder constructor.
54 * $tpl is initialized at false for lazy loading.
56 * @param ConfigManager $conf Configuration Manager instance (reference).
57 * @param array $session $_SESSION array
58 * @param BookmarkServiceInterface $linkDB instance.
59 * @param string $token Session token
60 * @param bool $isLoggedIn
62 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
66 $this->session
= $session;
67 $this->bookmarkService
= $linkDB;
68 $this->token
= $token;
69 $this->isLoggedIn
= $isLoggedIn;
73 * Reset current state of template rendering.
74 * Mostly useful for error handling. We remove everything, and display the error template.
76 public function reset(): void
82 * Initialize all default tpl tags.
84 private function initialize()
86 $this->tpl
= new RainTPL();
89 $version = ApplicationUtils
::checkUpdate(
91 $this->conf
->get('resource.update_check'),
92 $this->conf
->get('updates.check_updates_interval'),
93 $this->conf
->get('updates.check_updates'),
95 $this->conf
->get('updates.check_updates_branch')
97 $this->tpl
->assign('newVersion', escape($version));
98 $this->tpl
->assign('versionError', '');
99 } catch (Exception
$exc) {
100 logm($this->conf
->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
101 $this->tpl
->assign('newVersion', '');
102 $this->tpl
->assign('versionError', escape($exc->getMessage()));
105 $this->tpl
->assign('is_logged_in', $this->isLoggedIn
);
106 $this->tpl
->assign('feedurl', escape(index_url($_SERVER)));
107 $searchcrits = ''; // Search criteria
108 if (!empty($_GET['searchtags'])) {
109 $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
111 if (!empty($_GET['searchterm'])) {
112 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
114 $this->tpl
->assign('searchcrits', $searchcrits);
115 $this->tpl
->assign('source', index_url($_SERVER));
116 $this->tpl
->assign('version', SHAARLI_VERSION
);
119 ApplicationUtils
::getVersionHash(SHAARLI_VERSION
, $this->conf
->get('credentials.salt'))
121 $this->tpl
->assign('index_url', index_url($_SERVER));
122 $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
123 $this->tpl
->assign('visibility', $visibility);
124 $this->tpl
->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
125 $this->tpl
->assign('pagetitle', $this->conf
->get('general.title', 'Shaarli'));
126 if ($this->conf
->exists('general.header_link')) {
127 $this->tpl
->assign('titleLink', $this->conf
->get('general.header_link'));
129 $this->tpl
->assign('shaarlititle', $this->conf
->get('general.title', 'Shaarli'));
130 $this->tpl
->assign('openshaarli', $this->conf
->get('security.open_shaarli', false));
131 $this->tpl
->assign('showatom', $this->conf
->get('feed.show_atom', true));
132 $this->tpl
->assign('feed_type', $this->conf
->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
133 $this->tpl
->assign('hide_timestamps', $this->conf
->get('privacy.hide_timestamps', false));
134 $this->tpl
->assign('token', $this->token
);
136 $this->tpl
->assign('language', $this->conf
->get('translation.language'));
138 if ($this->bookmarkService
!== null) {
139 $this->tpl
->assign('tags', $this->bookmarkService
->bookmarksCountPerTag());
143 'thumbnails_enabled',
144 $this->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
146 $this->tpl
->assign('thumbnails_width', $this->conf
->get('thumbnails.width'));
147 $this->tpl
->assign('thumbnails_height', $this->conf
->get('thumbnails.height'));
149 $this->tpl
->assign('formatter', $this->conf
->get('formatter', 'default'));
151 // To be removed with a proper theme configuration.
152 $this->tpl
->assign('conf', $this->conf
);
156 * Affect variable after controller processing.
157 * Used for alert messages.
159 protected function finalize(): void
161 //FIXME - DEV _ REMOVE ME
162 $this->assign('base_path', '/Shaarli');
163 $this->assign('asset_path', '/Shaarli/tpl/default');
165 // TODO: use the SessionManager
167 SessionManager
::KEY_SUCCESS_MESSAGES
,
168 SessionManager
::KEY_WARNING_MESSAGES
,
169 SessionManager
::KEY_ERROR_MESSAGES
171 foreach ($messageKeys as $messageKey) {
172 if (!empty($_SESSION[$messageKey])) {
173 $this->tpl
->assign('global_' . $messageKey, $_SESSION[$messageKey]);
174 unset($_SESSION[$messageKey]);
180 * The following assign() method is basically the same as RainTPL (except lazy loading)
182 * @param string $placeholder Template placeholder.
183 * @param mixed $value Value to assign.
185 public function assign($placeholder, $value)
187 if ($this->tpl
=== false) {
190 $this->tpl
->assign($placeholder, $value);
194 * Assign an array of data to the template builder.
196 * @param array $data Data to assign.
198 * @return false if invalid data.
200 public function assignAll($data)
202 if ($this->tpl
=== false) {
206 if (empty($data) || !is_array($data)) {
210 foreach ($data as $key => $value) {
211 $this->assign($key, $value);
217 * Render a specific page (using a template file).
218 * e.g. $pb->renderPage('picwall');
220 * @param string $page Template filename (without extension).
222 public function renderPage($page)
224 if ($this->tpl
=== false) {
230 $this->tpl
->draw($page);
234 * Render a specific page as string (using a template file).
235 * e.g. $pb->render('picwall');
237 * @param string $page Template filename (without extension).
239 * @return string Processed template content
241 public function render(string $page): string
243 if ($this->tpl
=== false) {
249 return $this->tpl
->draw($page, true);
253 * Render a 404 page (uses the template : tpl/404.tpl)
254 * usage: $PAGE->render404('The link was deleted')
256 * @param string $message A message to display what is not found
258 public function render404($message = '')
260 if (empty($message)) {
261 $message = t('The page you are trying to reach does not exist or has been deleted.');
263 header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found'));
264 $this->tpl
->assign('error_message', $message);
265 $this->renderPage('404');