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