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