]>
Commit | Line | Data |
---|---|---|
03eb19ac A |
1 | <?php |
2 | ||
8c0f19c7 V |
3 | namespace Shaarli\Render; |
4 | ||
8c0f19c7 | 5 | use Exception; |
b38a1b02 | 6 | use Psr\Log\LoggerInterface; |
8c0f19c7 | 7 | use RainTPL; |
cf92b4dd | 8 | use Shaarli\Bookmark\BookmarkServiceInterface; |
ae3aa968 | 9 | use Shaarli\Config\ConfigManager; |
c2cd15da | 10 | use Shaarli\Helper\ApplicationUtils; |
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 | ||
b38a1b02 A |
38 | /** @var LoggerInterface */ |
39 | protected $logger; | |
40 | ||
73c89626 | 41 | /** |
cf92b4dd | 42 | * @var BookmarkServiceInterface $bookmarkService instance. |
73c89626 | 43 | */ |
cf92b4dd | 44 | protected $bookmarkService; |
28f26524 A |
45 | |
46 | /** | |
47 | * @var null|string XSRF token | |
48 | */ | |
49 | protected $token; | |
50 | ||
8c0f19c7 V |
51 | /** |
52 | * @var bool $isLoggedIn Whether the user is logged in | |
53 | */ | |
89ccc83b | 54 | protected $isLoggedIn = false; |
73c89626 | 55 | |
03eb19ac A |
56 | /** |
57 | * PageBuilder constructor. | |
58 | * $tpl is initialized at false for lazy loading. | |
278d9ee2 | 59 | * |
b38a1b02 A |
60 | * @param ConfigManager $conf Configuration Manager instance (reference). |
61 | * @param array $session $_SESSION array | |
62 | * @param LoggerInterface $logger | |
63 | * @param null $linkDB instance. | |
64 | * @param null $token Session token | |
65 | * @param bool $isLoggedIn | |
03eb19ac | 66 | */ |
b38a1b02 A |
67 | public function __construct( |
68 | ConfigManager &$conf, | |
69 | array $session, | |
70 | LoggerInterface $logger, | |
71 | $linkDB = null, | |
72 | $token = null, | |
73 | $isLoggedIn = false | |
74 | ) { | |
03eb19ac | 75 | $this->tpl = false; |
278d9ee2 | 76 | $this->conf = $conf; |
28f26524 | 77 | $this->session = $session; |
b38a1b02 | 78 | $this->logger = $logger; |
cf92b4dd | 79 | $this->bookmarkService = $linkDB; |
ebd650c0 | 80 | $this->token = $token; |
89ccc83b | 81 | $this->isLoggedIn = $isLoggedIn; |
03eb19ac A |
82 | } |
83 | ||
1a8ac737 A |
84 | /** |
85 | * Reset current state of template rendering. | |
86 | * Mostly useful for error handling. We remove everything, and display the error template. | |
87 | */ | |
88 | public function reset(): void | |
89 | { | |
90 | $this->tpl = false; | |
91 | } | |
92 | ||
03eb19ac A |
93 | /** |
94 | * Initialize all default tpl tags. | |
95 | */ | |
96 | private function initialize() | |
97 | { | |
98 | $this->tpl = new RainTPL(); | |
99 | ||
100 | try { | |
101 | $version = ApplicationUtils::checkUpdate( | |
b3e1f92e | 102 | SHAARLI_VERSION, |
894a3c4b A |
103 | $this->conf->get('resource.update_check'), |
104 | $this->conf->get('updates.check_updates_interval'), | |
105 | $this->conf->get('updates.check_updates'), | |
89ccc83b | 106 | $this->isLoggedIn, |
894a3c4b | 107 | $this->conf->get('updates.check_updates_branch') |
03eb19ac A |
108 | ); |
109 | $this->tpl->assign('newVersion', escape($version)); | |
110 | $this->tpl->assign('versionError', ''); | |
03eb19ac | 111 | } catch (Exception $exc) { |
b38a1b02 | 112 | $this->logger->error(format_log('Error: ' . $exc->getMessage(), client_ip_id($_SERVER))); |
03eb19ac A |
113 | $this->tpl->assign('newVersion', ''); |
114 | $this->tpl->assign('versionError', escape($exc->getMessage())); | |
115 | } | |
116 | ||
89ccc83b | 117 | $this->tpl->assign('is_logged_in', $this->isLoggedIn); |
03eb19ac A |
118 | $this->tpl->assign('feedurl', escape(index_url($_SERVER))); |
119 | $searchcrits = ''; // Search criteria | |
120 | if (!empty($_GET['searchtags'])) { | |
121 | $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); | |
122 | } | |
123 | if (!empty($_GET['searchterm'])) { | |
124 | $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); | |
125 | } | |
126 | $this->tpl->assign('searchcrits', $searchcrits); | |
127 | $this->tpl->assign('source', index_url($_SERVER)); | |
b3e1f92e | 128 | $this->tpl->assign('version', SHAARLI_VERSION); |
bfe4f536 A |
129 | $this->tpl->assign( |
130 | 'version_hash', | |
131 | ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt')) | |
132 | ); | |
a120fb29 | 133 | $this->tpl->assign('index_url', index_url($_SERVER)); |
8c0f19c7 | 134 | $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : ''; |
9d4736a3 | 135 | $this->tpl->assign('visibility', $visibility); |
f210d94f | 136 | $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly'])); |
97ef33bb | 137 | $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli')); |
278d9ee2 A |
138 | if ($this->conf->exists('general.header_link')) { |
139 | $this->tpl->assign('titleLink', $this->conf->get('general.header_link')); | |
03eb19ac | 140 | } |
97ef33bb | 141 | $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli')); |
894a3c4b | 142 | $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); |
2ea89aba A |
143 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); |
144 | $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); | |
894a3c4b | 145 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); |
ebd650c0 | 146 | $this->tpl->assign('token', $this->token); |
bfe4f536 | 147 | |
cb974e47 A |
148 | $this->tpl->assign('language', $this->conf->get('translation.language')); |
149 | ||
cf92b4dd | 150 | if ($this->bookmarkService !== null) { |
72fbbcd6 | 151 | $this->tpl->assign('tags', escape($this->bookmarkService->bookmarksCountPerTag())); |
73c89626 | 152 | } |
1b93137e | 153 | |
b302b3c5 A |
154 | $this->tpl->assign( |
155 | 'thumbnails_enabled', | |
156 | $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | |
157 | ); | |
1b93137e A |
158 | $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); |
159 | $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); | |
160 | ||
cf92b4dd A |
161 | $this->tpl->assign('formatter', $this->conf->get('formatter', 'default')); |
162 | ||
d3f6d525 | 163 | $this->tpl->assign('links_per_page', $this->session['LINKS_PER_PAGE'] ?? 20); |
b3bd8c3e | 164 | $this->tpl->assign('tags_separator', $this->conf->get('general.tags_separator', ' ')); |
816ffba7 | 165 | |
b302c77c A |
166 | // To be removed with a proper theme configuration. |
167 | $this->tpl->assign('conf', $this->conf); | |
03eb19ac A |
168 | } |
169 | ||
66063ed1 A |
170 | /** |
171 | * Affect variable after controller processing. | |
172 | * Used for alert messages. | |
173 | */ | |
9fbc4229 | 174 | protected function finalize(string $basePath): void |
ef00f9d2 A |
175 | { |
176 | // TODO: use the SessionManager | |
177 | $messageKeys = [ | |
178 | SessionManager::KEY_SUCCESS_MESSAGES, | |
179 | SessionManager::KEY_WARNING_MESSAGES, | |
180 | SessionManager::KEY_ERROR_MESSAGES | |
181 | ]; | |
182 | foreach ($messageKeys as $messageKey) { | |
183 | if (!empty($_SESSION[$messageKey])) { | |
184 | $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]); | |
185 | unset($_SESSION[$messageKey]); | |
186 | } | |
187 | } | |
9fbc4229 | 188 | |
7f525042 | 189 | $rootPath = preg_replace('#/index\.php$#', '', $basePath); |
9fbc4229 | 190 | $this->assign('base_path', $basePath); |
7f525042 | 191 | $this->assign('root_path', $rootPath); |
9fbc4229 A |
192 | $this->assign( |
193 | 'asset_path', | |
7f525042 | 194 | $rootPath . '/' . |
9fbc4229 A |
195 | rtrim($this->conf->get('resource.raintpl_tpl', 'tpl'), '/') . '/' . |
196 | $this->conf->get('resource.theme', 'default') | |
197 | ); | |
ef00f9d2 A |
198 | } |
199 | ||
03eb19ac A |
200 | /** |
201 | * The following assign() method is basically the same as RainTPL (except lazy loading) | |
202 | * | |
203 | * @param string $placeholder Template placeholder. | |
204 | * @param mixed $value Value to assign. | |
205 | */ | |
206 | public function assign($placeholder, $value) | |
207 | { | |
03eb19ac A |
208 | if ($this->tpl === false) { |
209 | $this->initialize(); | |
210 | } | |
211 | $this->tpl->assign($placeholder, $value); | |
212 | } | |
213 | ||
214 | /** | |
215 | * Assign an array of data to the template builder. | |
216 | * | |
217 | * @param array $data Data to assign. | |
218 | * | |
219 | * @return false if invalid data. | |
220 | */ | |
221 | public function assignAll($data) | |
222 | { | |
03eb19ac A |
223 | if ($this->tpl === false) { |
224 | $this->initialize(); | |
225 | } | |
226 | ||
f211e417 | 227 | if (empty($data) || !is_array($data)) { |
03eb19ac A |
228 | return false; |
229 | } | |
230 | ||
231 | foreach ($data as $key => $value) { | |
232 | $this->assign($key, $value); | |
233 | } | |
278d9ee2 | 234 | return true; |
03eb19ac A |
235 | } |
236 | ||
6c50a6cc A |
237 | /** |
238 | * Render a specific page as string (using a template file). | |
239 | * e.g. $pb->render('picwall'); | |
240 | * | |
241 | * @param string $page Template filename (without extension). | |
242 | * | |
243 | * @return string Processed template content | |
244 | */ | |
9fbc4229 | 245 | public function render(string $page, string $basePath): string |
6c50a6cc A |
246 | { |
247 | if ($this->tpl === false) { | |
248 | $this->initialize(); | |
249 | } | |
250 | ||
9fbc4229 | 251 | $this->finalize($basePath); |
ef00f9d2 | 252 | |
6c50a6cc A |
253 | return $this->tpl->draw($page, true); |
254 | } | |
03eb19ac | 255 | } |