]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/render/PageBuilder.php
Support using Shaarli without URL rewriting
[github/shaarli/Shaarli.git] / application / render / PageBuilder.php
CommitLineData
03eb19ac
A
1<?php
2
8c0f19c7
V
3namespace Shaarli\Render;
4
8c0f19c7 5use Exception;
9fbc4229 6use exceptions\MissingBasePathException;
8c0f19c7 7use RainTPL;
dea72c71 8use Shaarli\ApplicationUtils;
cf92b4dd 9use Shaarli\Bookmark\BookmarkServiceInterface;
ae3aa968 10use Shaarli\Config\ConfigManager;
ef00f9d2 11use Shaarli\Security\SessionManager;
b302b3c5 12use Shaarli\Thumbnailer;
ae3aa968 13
03eb19ac
A
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 */
21class PageBuilder
22{
23 /**
24 * @var RainTPL RainTPL instance.
25 */
26 private $tpl;
27
278d9ee2
A
28 /**
29 * @var ConfigManager $conf Configuration Manager instance.
30 */
31 protected $conf;
32
28f26524
A
33 /**
34 * @var array $_SESSION
35 */
36 protected $session;
37
73c89626 38 /**
cf92b4dd 39 * @var BookmarkServiceInterface $bookmarkService instance.
73c89626 40 */
cf92b4dd 41 protected $bookmarkService;
28f26524
A
42
43 /**
44 * @var null|string XSRF token
45 */
46 protected $token;
47
8c0f19c7
V
48 /**
49 * @var bool $isLoggedIn Whether the user is logged in
50 */
89ccc83b 51 protected $isLoggedIn = false;
73c89626 52
03eb19ac
A
53 /**
54 * PageBuilder constructor.
55 * $tpl is initialized at false for lazy loading.
278d9ee2 56 *
cf92b4dd
A
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
03eb19ac 62 */
28f26524 63 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
03eb19ac
A
64 {
65 $this->tpl = false;
278d9ee2 66 $this->conf = $conf;
28f26524 67 $this->session = $session;
cf92b4dd 68 $this->bookmarkService = $linkDB;
ebd650c0 69 $this->token = $token;
89ccc83b 70 $this->isLoggedIn = $isLoggedIn;
03eb19ac
A
71 }
72
1a8ac737
A
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
03eb19ac
A
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(
b3e1f92e 91 SHAARLI_VERSION,
894a3c4b
A
92 $this->conf->get('resource.update_check'),
93 $this->conf->get('updates.check_updates_interval'),
94 $this->conf->get('updates.check_updates'),
89ccc83b 95 $this->isLoggedIn,
894a3c4b 96 $this->conf->get('updates.check_updates_branch')
03eb19ac
A
97 );
98 $this->tpl->assign('newVersion', escape($version));
99 $this->tpl->assign('versionError', '');
03eb19ac 100 } catch (Exception $exc) {
894a3c4b 101 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
03eb19ac
A
102 $this->tpl->assign('newVersion', '');
103 $this->tpl->assign('versionError', escape($exc->getMessage()));
104 }
105
89ccc83b 106 $this->tpl->assign('is_logged_in', $this->isLoggedIn);
03eb19ac
A
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));
b3e1f92e 117 $this->tpl->assign('version', SHAARLI_VERSION);
bfe4f536
A
118 $this->tpl->assign(
119 'version_hash',
120 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
121 );
a120fb29 122 $this->tpl->assign('index_url', index_url($_SERVER));
8c0f19c7 123 $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
9d4736a3 124 $this->tpl->assign('visibility', $visibility);
f210d94f 125 $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
97ef33bb 126 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
278d9ee2
A
127 if ($this->conf->exists('general.header_link')) {
128 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
03eb19ac 129 }
97ef33bb 130 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
894a3c4b 131 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
2ea89aba
A
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');
894a3c4b 134 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
ebd650c0 135 $this->tpl->assign('token', $this->token);
bfe4f536 136
cb974e47
A
137 $this->tpl->assign('language', $this->conf->get('translation.language'));
138
cf92b4dd 139 if ($this->bookmarkService !== null) {
72fbbcd6 140 $this->tpl->assign('tags', escape($this->bookmarkService->bookmarksCountPerTag()));
73c89626 141 }
1b93137e 142
b302b3c5
A
143 $this->tpl->assign(
144 'thumbnails_enabled',
145 $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
146 );
1b93137e
A
147 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
148 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
149
cf92b4dd
A
150 $this->tpl->assign('formatter', $this->conf->get('formatter', 'default'));
151
4479aff1 152 $this->tpl->assign('links_per_page', $this->session['LINKS_PER_PAGE']);
816ffba7 153
b302c77c
A
154 // To be removed with a proper theme configuration.
155 $this->tpl->assign('conf', $this->conf);
03eb19ac
A
156 }
157
66063ed1
A
158 /**
159 * Affect variable after controller processing.
160 * Used for alert messages.
161 */
9fbc4229 162 protected function finalize(string $basePath): void
ef00f9d2
A
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 }
9fbc4229 176
7f525042 177 $rootPath = preg_replace('#/index\.php$#', '', $basePath);
9fbc4229 178 $this->assign('base_path', $basePath);
7f525042 179 $this->assign('root_path', $rootPath);
9fbc4229
A
180 $this->assign(
181 'asset_path',
7f525042 182 $rootPath . '/' .
9fbc4229
A
183 rtrim($this->conf->get('resource.raintpl_tpl', 'tpl'), '/') . '/' .
184 $this->conf->get('resource.theme', 'default')
185 );
ef00f9d2
A
186 }
187
03eb19ac
A
188 /**
189 * The following assign() method is basically the same as RainTPL (except lazy loading)
190 *
191 * @param string $placeholder Template placeholder.
192 * @param mixed $value Value to assign.
193 */
194 public function assign($placeholder, $value)
195 {
03eb19ac
A
196 if ($this->tpl === false) {
197 $this->initialize();
198 }
199 $this->tpl->assign($placeholder, $value);
200 }
201
202 /**
203 * Assign an array of data to the template builder.
204 *
205 * @param array $data Data to assign.
206 *
207 * @return false if invalid data.
208 */
209 public function assignAll($data)
210 {
03eb19ac
A
211 if ($this->tpl === false) {
212 $this->initialize();
213 }
214
f211e417 215 if (empty($data) || !is_array($data)) {
03eb19ac
A
216 return false;
217 }
218
219 foreach ($data as $key => $value) {
220 $this->assign($key, $value);
221 }
278d9ee2 222 return true;
03eb19ac
A
223 }
224
6c50a6cc
A
225 /**
226 * Render a specific page as string (using a template file).
227 * e.g. $pb->render('picwall');
228 *
229 * @param string $page Template filename (without extension).
230 *
231 * @return string Processed template content
232 */
9fbc4229 233 public function render(string $page, string $basePath): string
6c50a6cc
A
234 {
235 if ($this->tpl === false) {
236 $this->initialize();
237 }
238
9fbc4229 239 $this->finalize($basePath);
ef00f9d2 240
6c50a6cc
A
241 return $this->tpl->draw($page, true);
242 }
03eb19ac 243}