]>
Commit | Line | Data |
---|---|---|
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 | * Initialize all default tpl tags. | |
74 | */ | |
75 | private function initialize() | |
76 | { | |
77 | $this->tpl = new RainTPL(); | |
78 | ||
79 | try { | |
80 | $version = ApplicationUtils::checkUpdate( | |
81 | SHAARLI_VERSION, | |
82 | $this->conf->get('resource.update_check'), | |
83 | $this->conf->get('updates.check_updates_interval'), | |
84 | $this->conf->get('updates.check_updates'), | |
85 | $this->isLoggedIn, | |
86 | $this->conf->get('updates.check_updates_branch') | |
87 | ); | |
88 | $this->tpl->assign('newVersion', escape($version)); | |
89 | $this->tpl->assign('versionError', ''); | |
90 | } catch (Exception $exc) { | |
91 | logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); | |
92 | $this->tpl->assign('newVersion', ''); | |
93 | $this->tpl->assign('versionError', escape($exc->getMessage())); | |
94 | } | |
95 | ||
96 | $this->tpl->assign('is_logged_in', $this->isLoggedIn); | |
97 | $this->tpl->assign('feedurl', escape(index_url($_SERVER))); | |
98 | $searchcrits = ''; // Search criteria | |
99 | if (!empty($_GET['searchtags'])) { | |
100 | $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); | |
101 | } | |
102 | if (!empty($_GET['searchterm'])) { | |
103 | $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); | |
104 | } | |
105 | $this->tpl->assign('searchcrits', $searchcrits); | |
106 | $this->tpl->assign('source', index_url($_SERVER)); | |
107 | $this->tpl->assign('version', SHAARLI_VERSION); | |
108 | $this->tpl->assign( | |
109 | 'version_hash', | |
110 | ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt')) | |
111 | ); | |
112 | $this->tpl->assign('index_url', index_url($_SERVER)); | |
113 | $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : ''; | |
114 | $this->tpl->assign('visibility', $visibility); | |
115 | $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly'])); | |
116 | $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli')); | |
117 | if ($this->conf->exists('general.header_link')) { | |
118 | $this->tpl->assign('titleLink', $this->conf->get('general.header_link')); | |
119 | } | |
120 | $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli')); | |
121 | $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); | |
122 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); | |
123 | $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); | |
124 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); | |
125 | $this->tpl->assign('token', $this->token); | |
126 | ||
127 | $this->tpl->assign('language', $this->conf->get('translation.language')); | |
128 | ||
129 | if ($this->bookmarkService !== null) { | |
130 | $this->tpl->assign('tags', $this->bookmarkService->bookmarksCountPerTag()); | |
131 | } | |
132 | ||
133 | $this->tpl->assign( | |
134 | 'thumbnails_enabled', | |
135 | $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | |
136 | ); | |
137 | $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); | |
138 | $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); | |
139 | ||
140 | $this->tpl->assign('formatter', $this->conf->get('formatter', 'default')); | |
141 | ||
142 | // To be removed with a proper theme configuration. | |
143 | $this->tpl->assign('conf', $this->conf); | |
144 | } | |
145 | ||
146 | protected function finalize(): void | |
147 | { | |
148 | // TODO: use the SessionManager | |
149 | $messageKeys = [ | |
150 | SessionManager::KEY_SUCCESS_MESSAGES, | |
151 | SessionManager::KEY_WARNING_MESSAGES, | |
152 | SessionManager::KEY_ERROR_MESSAGES | |
153 | ]; | |
154 | foreach ($messageKeys as $messageKey) { | |
155 | if (!empty($_SESSION[$messageKey])) { | |
156 | $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]); | |
157 | unset($_SESSION[$messageKey]); | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | /** | |
163 | * The following assign() method is basically the same as RainTPL (except lazy loading) | |
164 | * | |
165 | * @param string $placeholder Template placeholder. | |
166 | * @param mixed $value Value to assign. | |
167 | */ | |
168 | public function assign($placeholder, $value) | |
169 | { | |
170 | if ($this->tpl === false) { | |
171 | $this->initialize(); | |
172 | } | |
173 | $this->tpl->assign($placeholder, $value); | |
174 | } | |
175 | ||
176 | /** | |
177 | * Assign an array of data to the template builder. | |
178 | * | |
179 | * @param array $data Data to assign. | |
180 | * | |
181 | * @return false if invalid data. | |
182 | */ | |
183 | public function assignAll($data) | |
184 | { | |
185 | if ($this->tpl === false) { | |
186 | $this->initialize(); | |
187 | } | |
188 | ||
189 | if (empty($data) || !is_array($data)) { | |
190 | return false; | |
191 | } | |
192 | ||
193 | foreach ($data as $key => $value) { | |
194 | $this->assign($key, $value); | |
195 | } | |
196 | return true; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Render a specific page (using a template file). | |
201 | * e.g. $pb->renderPage('picwall'); | |
202 | * | |
203 | * @param string $page Template filename (without extension). | |
204 | */ | |
205 | public function renderPage($page) | |
206 | { | |
207 | if ($this->tpl === false) { | |
208 | $this->initialize(); | |
209 | } | |
210 | ||
211 | $this->finalize(); | |
212 | ||
213 | $this->tpl->draw($page); | |
214 | } | |
215 | ||
216 | /** | |
217 | * Render a specific page as string (using a template file). | |
218 | * e.g. $pb->render('picwall'); | |
219 | * | |
220 | * @param string $page Template filename (without extension). | |
221 | * | |
222 | * @return string Processed template content | |
223 | */ | |
224 | public function render(string $page): string | |
225 | { | |
226 | if ($this->tpl === false) { | |
227 | $this->initialize(); | |
228 | } | |
229 | ||
230 | $this->finalize(); | |
231 | ||
232 | return $this->tpl->draw($page, true); | |
233 | } | |
234 | ||
235 | /** | |
236 | * Render a 404 page (uses the template : tpl/404.tpl) | |
237 | * usage: $PAGE->render404('The link was deleted') | |
238 | * | |
239 | * @param string $message A message to display what is not found | |
240 | */ | |
241 | public function render404($message = '') | |
242 | { | |
243 | if (empty($message)) { | |
244 | $message = t('The page you are trying to reach does not exist or has been deleted.'); | |
245 | } | |
246 | header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found')); | |
247 | $this->tpl->assign('error_message', $message); | |
248 | $this->renderPage('404'); | |
249 | } | |
250 | } |