]>
Commit | Line | Data |
---|---|---|
03eb19ac A |
1 | <?php |
2 | ||
8c0f19c7 V |
3 | namespace Shaarli\Render; |
4 | ||
8c0f19c7 | 5 | use Exception; |
8c0f19c7 | 6 | use RainTPL; |
dea72c71 | 7 | use Shaarli\ApplicationUtils; |
cf92b4dd | 8 | use Shaarli\Bookmark\BookmarkServiceInterface; |
ae3aa968 | 9 | use Shaarli\Config\ConfigManager; |
b302b3c5 | 10 | use Shaarli\Thumbnailer; |
ae3aa968 | 11 | |
03eb19ac A |
12 | /** |
13 | * This class is in charge of building the final page. | |
14 | * (This is basically a wrapper around RainTPL which pre-fills some fields.) | |
15 | * $p = new PageBuilder(); | |
16 | * $p->assign('myfield','myvalue'); | |
17 | * $p->renderPage('mytemplate'); | |
18 | */ | |
19 | class PageBuilder | |
20 | { | |
21 | /** | |
22 | * @var RainTPL RainTPL instance. | |
23 | */ | |
24 | private $tpl; | |
25 | ||
278d9ee2 A |
26 | /** |
27 | * @var ConfigManager $conf Configuration Manager instance. | |
28 | */ | |
29 | protected $conf; | |
30 | ||
28f26524 A |
31 | /** |
32 | * @var array $_SESSION | |
33 | */ | |
34 | protected $session; | |
35 | ||
73c89626 | 36 | /** |
cf92b4dd | 37 | * @var BookmarkServiceInterface $bookmarkService instance. |
73c89626 | 38 | */ |
cf92b4dd | 39 | protected $bookmarkService; |
28f26524 A |
40 | |
41 | /** | |
42 | * @var null|string XSRF token | |
43 | */ | |
44 | protected $token; | |
45 | ||
8c0f19c7 V |
46 | /** |
47 | * @var bool $isLoggedIn Whether the user is logged in | |
48 | */ | |
89ccc83b | 49 | protected $isLoggedIn = false; |
73c89626 | 50 | |
03eb19ac A |
51 | /** |
52 | * PageBuilder constructor. | |
53 | * $tpl is initialized at false for lazy loading. | |
278d9ee2 | 54 | * |
cf92b4dd A |
55 | * @param ConfigManager $conf Configuration Manager instance (reference). |
56 | * @param array $session $_SESSION array | |
57 | * @param BookmarkServiceInterface $linkDB instance. | |
58 | * @param string $token Session token | |
59 | * @param bool $isLoggedIn | |
03eb19ac | 60 | */ |
28f26524 | 61 | public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false) |
03eb19ac A |
62 | { |
63 | $this->tpl = false; | |
278d9ee2 | 64 | $this->conf = $conf; |
28f26524 | 65 | $this->session = $session; |
cf92b4dd | 66 | $this->bookmarkService = $linkDB; |
ebd650c0 | 67 | $this->token = $token; |
89ccc83b | 68 | $this->isLoggedIn = $isLoggedIn; |
03eb19ac A |
69 | } |
70 | ||
71 | /** | |
72 | * Initialize all default tpl tags. | |
73 | */ | |
74 | private function initialize() | |
75 | { | |
76 | $this->tpl = new RainTPL(); | |
77 | ||
78 | try { | |
79 | $version = ApplicationUtils::checkUpdate( | |
b3e1f92e | 80 | SHAARLI_VERSION, |
894a3c4b A |
81 | $this->conf->get('resource.update_check'), |
82 | $this->conf->get('updates.check_updates_interval'), | |
83 | $this->conf->get('updates.check_updates'), | |
89ccc83b | 84 | $this->isLoggedIn, |
894a3c4b | 85 | $this->conf->get('updates.check_updates_branch') |
03eb19ac A |
86 | ); |
87 | $this->tpl->assign('newVersion', escape($version)); | |
88 | $this->tpl->assign('versionError', ''); | |
03eb19ac | 89 | } catch (Exception $exc) { |
894a3c4b | 90 | logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); |
03eb19ac A |
91 | $this->tpl->assign('newVersion', ''); |
92 | $this->tpl->assign('versionError', escape($exc->getMessage())); | |
93 | } | |
94 | ||
89ccc83b | 95 | $this->tpl->assign('is_logged_in', $this->isLoggedIn); |
03eb19ac A |
96 | $this->tpl->assign('feedurl', escape(index_url($_SERVER))); |
97 | $searchcrits = ''; // Search criteria | |
98 | if (!empty($_GET['searchtags'])) { | |
99 | $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); | |
100 | } | |
101 | if (!empty($_GET['searchterm'])) { | |
102 | $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); | |
103 | } | |
104 | $this->tpl->assign('searchcrits', $searchcrits); | |
105 | $this->tpl->assign('source', index_url($_SERVER)); | |
b3e1f92e | 106 | $this->tpl->assign('version', SHAARLI_VERSION); |
bfe4f536 A |
107 | $this->tpl->assign( |
108 | 'version_hash', | |
109 | ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt')) | |
110 | ); | |
a120fb29 | 111 | $this->tpl->assign('index_url', index_url($_SERVER)); |
8c0f19c7 | 112 | $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : ''; |
9d4736a3 | 113 | $this->tpl->assign('visibility', $visibility); |
f210d94f | 114 | $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly'])); |
97ef33bb | 115 | $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli')); |
278d9ee2 A |
116 | if ($this->conf->exists('general.header_link')) { |
117 | $this->tpl->assign('titleLink', $this->conf->get('general.header_link')); | |
03eb19ac | 118 | } |
97ef33bb | 119 | $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli')); |
894a3c4b | 120 | $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); |
2ea89aba A |
121 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); |
122 | $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); | |
894a3c4b | 123 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); |
ebd650c0 | 124 | $this->tpl->assign('token', $this->token); |
bfe4f536 | 125 | |
cb974e47 A |
126 | $this->tpl->assign('language', $this->conf->get('translation.language')); |
127 | ||
cf92b4dd A |
128 | if ($this->bookmarkService !== null) { |
129 | $this->tpl->assign('tags', $this->bookmarkService->bookmarksCountPerTag()); | |
73c89626 | 130 | } |
1b93137e | 131 | |
b302b3c5 A |
132 | $this->tpl->assign( |
133 | 'thumbnails_enabled', | |
134 | $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE | |
135 | ); | |
1b93137e A |
136 | $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); |
137 | $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); | |
138 | ||
8c0f19c7 | 139 | if (!empty($_SESSION['warnings'])) { |
28f26524 A |
140 | $this->tpl->assign('global_warnings', $_SESSION['warnings']); |
141 | unset($_SESSION['warnings']); | |
142 | } | |
143 | ||
cf92b4dd A |
144 | $this->tpl->assign('formatter', $this->conf->get('formatter', 'default')); |
145 | ||
b302c77c A |
146 | // To be removed with a proper theme configuration. |
147 | $this->tpl->assign('conf', $this->conf); | |
03eb19ac A |
148 | } |
149 | ||
150 | /** | |
151 | * The following assign() method is basically the same as RainTPL (except lazy loading) | |
152 | * | |
153 | * @param string $placeholder Template placeholder. | |
154 | * @param mixed $value Value to assign. | |
155 | */ | |
156 | public function assign($placeholder, $value) | |
157 | { | |
03eb19ac A |
158 | if ($this->tpl === false) { |
159 | $this->initialize(); | |
160 | } | |
161 | $this->tpl->assign($placeholder, $value); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Assign an array of data to the template builder. | |
166 | * | |
167 | * @param array $data Data to assign. | |
168 | * | |
169 | * @return false if invalid data. | |
170 | */ | |
171 | public function assignAll($data) | |
172 | { | |
03eb19ac A |
173 | if ($this->tpl === false) { |
174 | $this->initialize(); | |
175 | } | |
176 | ||
f211e417 | 177 | if (empty($data) || !is_array($data)) { |
03eb19ac A |
178 | return false; |
179 | } | |
180 | ||
181 | foreach ($data as $key => $value) { | |
182 | $this->assign($key, $value); | |
183 | } | |
278d9ee2 | 184 | return true; |
03eb19ac A |
185 | } |
186 | ||
187 | /** | |
188 | * Render a specific page (using a template file). | |
189 | * e.g. $pb->renderPage('picwall'); | |
190 | * | |
191 | * @param string $page Template filename (without extension). | |
192 | */ | |
193 | public function renderPage($page) | |
194 | { | |
278d9ee2 | 195 | if ($this->tpl === false) { |
03eb19ac A |
196 | $this->initialize(); |
197 | } | |
278d9ee2 | 198 | |
03eb19ac A |
199 | $this->tpl->draw($page); |
200 | } | |
201 | ||
6c50a6cc A |
202 | /** |
203 | * Render a specific page as string (using a template file). | |
204 | * e.g. $pb->render('picwall'); | |
205 | * | |
206 | * @param string $page Template filename (without extension). | |
207 | * | |
208 | * @return string Processed template content | |
209 | */ | |
210 | public function render(string $page): string | |
211 | { | |
212 | if ($this->tpl === false) { | |
213 | $this->initialize(); | |
214 | } | |
215 | ||
216 | return $this->tpl->draw($page, true); | |
217 | } | |
218 | ||
03eb19ac A |
219 | /** |
220 | * Render a 404 page (uses the template : tpl/404.tpl) | |
8c0f19c7 | 221 | * usage: $PAGE->render404('The link was deleted') |
03eb19ac | 222 | * |
8c0f19c7 | 223 | * @param string $message A message to display what is not found |
03eb19ac | 224 | */ |
12266213 | 225 | public function render404($message = '') |
03eb19ac | 226 | { |
12266213 A |
227 | if (empty($message)) { |
228 | $message = t('The page you are trying to reach does not exist or has been deleted.'); | |
229 | } | |
8c0f19c7 | 230 | header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found')); |
03eb19ac A |
231 | $this->tpl->assign('error_message', $message); |
232 | $this->renderPage('404'); | |
233 | } | |
234 | } |