]>
Commit | Line | Data |
---|---|---|
03eb19ac A |
1 | <?php |
2 | ||
8c0f19c7 V |
3 | namespace Shaarli\Render; |
4 | ||
8c0f19c7 | 5 | use Exception; |
9fbc4229 | 6 | use exceptions\MissingBasePathException; |
8c0f19c7 | 7 | use RainTPL; |
dea72c71 | 8 | use Shaarli\ApplicationUtils; |
cf92b4dd | 9 | use Shaarli\Bookmark\BookmarkServiceInterface; |
ae3aa968 | 10 | use Shaarli\Config\ConfigManager; |
ef00f9d2 | 11 | use Shaarli\Security\SessionManager; |
b302b3c5 | 12 | use 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 | */ | |
21 | class 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 A |
139 | if ($this->bookmarkService !== null) { |
140 | $this->tpl->assign('tags', $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 A |
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 | ); | |
ef00f9d2 A |
184 | } |
185 | ||
03eb19ac A |
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 | { | |
03eb19ac A |
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 | { | |
03eb19ac A |
209 | if ($this->tpl === false) { |
210 | $this->initialize(); | |
211 | } | |
212 | ||
f211e417 | 213 | if (empty($data) || !is_array($data)) { |
03eb19ac A |
214 | return false; |
215 | } | |
216 | ||
217 | foreach ($data as $key => $value) { | |
218 | $this->assign($key, $value); | |
219 | } | |
278d9ee2 | 220 | return true; |
03eb19ac A |
221 | } |
222 | ||
6c50a6cc A |
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 | */ | |
9fbc4229 | 231 | public function render(string $page, string $basePath): string |
6c50a6cc A |
232 | { |
233 | if ($this->tpl === false) { | |
234 | $this->initialize(); | |
235 | } | |
236 | ||
9fbc4229 | 237 | $this->finalize($basePath); |
ef00f9d2 | 238 | |
6c50a6cc A |
239 | return $this->tpl->draw($page, true); |
240 | } | |
03eb19ac | 241 | } |