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