]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/PageBuilder.php
namespacing: move HTTP utilities along \Shaarli\Http\ classes
[github/shaarli/Shaarli.git] / application / PageBuilder.php
1 <?php
2
3 use Shaarli\Config\ConfigManager;
4 use Shaarli\Thumbnailer;
5
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
20 /**
21 * @var ConfigManager $conf Configuration Manager instance.
22 */
23 protected $conf;
24
25 /**
26 * @var array $_SESSION
27 */
28 protected $session;
29
30 /**
31 * @var LinkDB $linkDB instance.
32 */
33 protected $linkDB;
34
35 /**
36 * @var null|string XSRF token
37 */
38 protected $token;
39
40 /** @var bool $isLoggedIn Whether the user is logged in **/
41 protected $isLoggedIn = false;
42
43 /**
44 * PageBuilder constructor.
45 * $tpl is initialized at false for lazy loading.
46 *
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
52 */
53 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
54 {
55 $this->tpl = false;
56 $this->conf = $conf;
57 $this->session = $session;
58 $this->linkDB = $linkDB;
59 $this->token = $token;
60 $this->isLoggedIn = $isLoggedIn;
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(
72 SHAARLI_VERSION,
73 $this->conf->get('resource.update_check'),
74 $this->conf->get('updates.check_updates_interval'),
75 $this->conf->get('updates.check_updates'),
76 $this->isLoggedIn,
77 $this->conf->get('updates.check_updates_branch')
78 );
79 $this->tpl->assign('newVersion', escape($version));
80 $this->tpl->assign('versionError', '');
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('index_url', 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(
123 'thumbnails_enabled',
124 $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
125 );
126 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
127 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
128
129 if (! empty($_SESSION['warnings'])) {
130 $this->tpl->assign('global_warnings', $_SESSION['warnings']);
131 unset($_SESSION['warnings']);
132 }
133
134 // To be removed with a proper theme configuration.
135 $this->tpl->assign('conf', $this->conf);
136 }
137
138 /**
139 * The following assign() method is basically the same as RainTPL (except lazy loading)
140 *
141 * @param string $placeholder Template placeholder.
142 * @param mixed $value Value to assign.
143 */
144 public function assign($placeholder, $value)
145 {
146 if ($this->tpl === false) {
147 $this->initialize();
148 }
149 $this->tpl->assign($placeholder, $value);
150 }
151
152 /**
153 * Assign an array of data to the template builder.
154 *
155 * @param array $data Data to assign.
156 *
157 * @return false if invalid data.
158 */
159 public function assignAll($data)
160 {
161 if ($this->tpl === false) {
162 $this->initialize();
163 }
164
165 if (empty($data) || !is_array($data)) {
166 return false;
167 }
168
169 foreach ($data as $key => $value) {
170 $this->assign($key, $value);
171 }
172 return true;
173 }
174
175 /**
176 * Render a specific page (using a template file).
177 * e.g. $pb->renderPage('picwall');
178 *
179 * @param string $page Template filename (without extension).
180 */
181 public function renderPage($page)
182 {
183 if ($this->tpl === false) {
184 $this->initialize();
185 }
186
187 $this->tpl->draw($page);
188 }
189
190 /**
191 * Render a 404 page (uses the template : tpl/404.tpl)
192 * usage : $PAGE->render404('The link was deleted')
193 *
194 * @param string $message A messate to display what is not found
195 */
196 public function render404($message = '')
197 {
198 if (empty($message)) {
199 $message = t('The page you are trying to reach does not exist or has been deleted.');
200 }
201 header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found'));
202 $this->tpl->assign('error_message', $message);
203 $this->renderPage('404');
204 }
205 }