]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/render/PageBuilder.php
471724c00f1f725c65ed5416523f101da04ffbf8
[github/shaarli/Shaarli.git] / application / render / PageBuilder.php
1 <?php
2
3 namespace Shaarli\Render;
4
5 use Exception;
6 use RainTPL;
7 use Shaarli\ApplicationUtils;
8 use Shaarli\Bookmark\BookmarkServiceInterface;
9 use Shaarli\Config\ConfigManager;
10 use Shaarli\Security\SessionManager;
11 use Shaarli\Thumbnailer;
12
13 /**
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');
19 */
20 class PageBuilder
21 {
22 /**
23 * @var RainTPL RainTPL instance.
24 */
25 private $tpl;
26
27 /**
28 * @var ConfigManager $conf Configuration Manager instance.
29 */
30 protected $conf;
31
32 /**
33 * @var array $_SESSION
34 */
35 protected $session;
36
37 /**
38 * @var BookmarkServiceInterface $bookmarkService instance.
39 */
40 protected $bookmarkService;
41
42 /**
43 * @var null|string XSRF token
44 */
45 protected $token;
46
47 /**
48 * @var bool $isLoggedIn Whether the user is logged in
49 */
50 protected $isLoggedIn = false;
51
52 /**
53 * PageBuilder constructor.
54 * $tpl is initialized at false for lazy loading.
55 *
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
61 */
62 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
63 {
64 $this->tpl = false;
65 $this->conf = $conf;
66 $this->session = $session;
67 $this->bookmarkService = $linkDB;
68 $this->token = $token;
69 $this->isLoggedIn = $isLoggedIn;
70 }
71
72 /**
73 * Reset current state of template rendering.
74 * Mostly useful for error handling. We remove everything, and display the error template.
75 */
76 public function reset(): void
77 {
78 $this->tpl = false;
79 }
80
81 /**
82 * Initialize all default tpl tags.
83 */
84 private function initialize()
85 {
86 $this->tpl = new RainTPL();
87
88 try {
89 $version = ApplicationUtils::checkUpdate(
90 SHAARLI_VERSION,
91 $this->conf->get('resource.update_check'),
92 $this->conf->get('updates.check_updates_interval'),
93 $this->conf->get('updates.check_updates'),
94 $this->isLoggedIn,
95 $this->conf->get('updates.check_updates_branch')
96 );
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()));
103 }
104
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']);
110 }
111 if (!empty($_GET['searchterm'])) {
112 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
113 }
114 $this->tpl->assign('searchcrits', $searchcrits);
115 $this->tpl->assign('source', index_url($_SERVER));
116 $this->tpl->assign('version', SHAARLI_VERSION);
117 $this->tpl->assign(
118 'version_hash',
119 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
120 );
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'));
128 }
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);
135
136 $this->tpl->assign('language', $this->conf->get('translation.language'));
137
138 if ($this->bookmarkService !== null) {
139 $this->tpl->assign('tags', $this->bookmarkService->bookmarksCountPerTag());
140 }
141
142 $this->tpl->assign(
143 'thumbnails_enabled',
144 $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
145 );
146 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
147 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
148
149 $this->tpl->assign('formatter', $this->conf->get('formatter', 'default'));
150
151 // To be removed with a proper theme configuration.
152 $this->tpl->assign('conf', $this->conf);
153 }
154
155 /**
156 * Affect variable after controller processing.
157 * Used for alert messages.
158 */
159 protected function finalize(): void
160 {
161 // TODO: use the SessionManager
162 $messageKeys = [
163 SessionManager::KEY_SUCCESS_MESSAGES,
164 SessionManager::KEY_WARNING_MESSAGES,
165 SessionManager::KEY_ERROR_MESSAGES
166 ];
167 foreach ($messageKeys as $messageKey) {
168 if (!empty($_SESSION[$messageKey])) {
169 $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]);
170 unset($_SESSION[$messageKey]);
171 }
172 }
173 }
174
175 /**
176 * The following assign() method is basically the same as RainTPL (except lazy loading)
177 *
178 * @param string $placeholder Template placeholder.
179 * @param mixed $value Value to assign.
180 */
181 public function assign($placeholder, $value)
182 {
183 if ($this->tpl === false) {
184 $this->initialize();
185 }
186 $this->tpl->assign($placeholder, $value);
187 }
188
189 /**
190 * Assign an array of data to the template builder.
191 *
192 * @param array $data Data to assign.
193 *
194 * @return false if invalid data.
195 */
196 public function assignAll($data)
197 {
198 if ($this->tpl === false) {
199 $this->initialize();
200 }
201
202 if (empty($data) || !is_array($data)) {
203 return false;
204 }
205
206 foreach ($data as $key => $value) {
207 $this->assign($key, $value);
208 }
209 return true;
210 }
211
212 /**
213 * Render a specific page (using a template file).
214 * e.g. $pb->renderPage('picwall');
215 *
216 * @param string $page Template filename (without extension).
217 */
218 public function renderPage($page)
219 {
220 if ($this->tpl === false) {
221 $this->initialize();
222 }
223
224 $this->finalize();
225
226 $this->tpl->draw($page);
227 }
228
229 /**
230 * Render a specific page as string (using a template file).
231 * e.g. $pb->render('picwall');
232 *
233 * @param string $page Template filename (without extension).
234 *
235 * @return string Processed template content
236 */
237 public function render(string $page): string
238 {
239 if ($this->tpl === false) {
240 $this->initialize();
241 }
242
243 $this->finalize();
244
245 return $this->tpl->draw($page, true);
246 }
247 }