]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/PageBuilder.php
5da70811610ddbcc22d4b49e4fd134db3f416978
[github/shaarli/Shaarli.git] / application / PageBuilder.php
1 <?php
2
3 use Shaarli\Config\ConfigManager;
4
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
19 /**
20 * @var ConfigManager $conf Configuration Manager instance.
21 */
22 protected $conf;
23
24 /**
25 * @var array $_SESSION
26 */
27 protected $session;
28
29 /**
30 * @var LinkDB $linkDB instance.
31 */
32 protected $linkDB;
33
34 /**
35 * @var null|string XSRF token
36 */
37 protected $token;
38
39 /** @var bool $isLoggedIn Whether the user is logged in **/
40 protected $isLoggedIn = false;
41
42 /**
43 * PageBuilder constructor.
44 * $tpl is initialized at false for lazy loading.
45 *
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
51 */
52 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
53 {
54 $this->tpl = false;
55 $this->conf = $conf;
56 $this->session = $session;
57 $this->linkDB = $linkDB;
58 $this->token = $token;
59 $this->isLoggedIn = $isLoggedIn;
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(
71 SHAARLI_VERSION,
72 $this->conf->get('resource.update_check'),
73 $this->conf->get('updates.check_updates_interval'),
74 $this->conf->get('updates.check_updates'),
75 $this->isLoggedIn,
76 $this->conf->get('updates.check_updates_branch')
77 );
78 $this->tpl->assign('newVersion', escape($version));
79 $this->tpl->assign('versionError', '');
80
81 } catch (Exception $exc) {
82 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
83 $this->tpl->assign('newVersion', '');
84 $this->tpl->assign('versionError', escape($exc->getMessage()));
85 }
86
87 $this->tpl->assign('is_logged_in', $this->isLoggedIn);
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));
98 $this->tpl->assign('version', SHAARLI_VERSION);
99 $this->tpl->assign(
100 'version_hash',
101 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
102 );
103 $this->tpl->assign('scripturl', index_url($_SERVER));
104 $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
105 $this->tpl->assign('visibility', $visibility);
106 $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
107 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
108 if ($this->conf->exists('general.header_link')) {
109 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
110 }
111 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
112 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
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');
115 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
116 $this->tpl->assign('token', $this->token);
117
118 if ($this->linkDB !== null) {
119 $this->tpl->assign('tags', $this->linkDB->linksCountPerTag());
120 }
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
126 if (! empty($_SESSION['warnings'])) {
127 $this->tpl->assign('global_warnings', $_SESSION['warnings']);
128 unset($_SESSION['warnings']);
129 }
130
131 // To be removed with a proper theme configuration.
132 $this->tpl->assign('conf', $this->conf);
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 {
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 {
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 }
169 return true;
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 {
180 if ($this->tpl === false) {
181 $this->initialize();
182 }
183
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 */
193 public function render404($message = '')
194 {
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'));
199 $this->tpl->assign('error_message', $message);
200 $this->renderPage('404');
201 }
202 }