aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/render/PageBuilder.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/render/PageBuilder.php')
-rw-r--r--application/render/PageBuilder.php215
1 files changed, 215 insertions, 0 deletions
diff --git a/application/render/PageBuilder.php b/application/render/PageBuilder.php
new file mode 100644
index 00000000..3f86fc26
--- /dev/null
+++ b/application/render/PageBuilder.php
@@ -0,0 +1,215 @@
1<?php
2
3namespace Shaarli\Render;
4
5use Exception;
6use RainTPL;
7use Shaarli\ApplicationUtils;
8use Shaarli\Bookmark\LinkDB;
9use Shaarli\Config\ConfigManager;
10use Shaarli\Thumbnailer;
11
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 */
19class PageBuilder
20{
21 /**
22 * @var RainTPL RainTPL instance.
23 */
24 private $tpl;
25
26 /**
27 * @var ConfigManager $conf Configuration Manager instance.
28 */
29 protected $conf;
30
31 /**
32 * @var array $_SESSION
33 */
34 protected $session;
35
36 /**
37 * @var LinkDB $linkDB instance.
38 */
39 protected $linkDB;
40
41 /**
42 * @var null|string XSRF token
43 */
44 protected $token;
45
46 /**
47 * @var bool $isLoggedIn Whether the user is logged in
48 */
49 protected $isLoggedIn = false;
50
51 /**
52 * PageBuilder constructor.
53 * $tpl is initialized at false for lazy loading.
54 *
55 * @param ConfigManager $conf Configuration Manager instance (reference).
56 * @param array $session $_SESSION array
57 * @param LinkDB $linkDB instance.
58 * @param string $token Session token
59 * @param bool $isLoggedIn
60 */
61 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
62 {
63 $this->tpl = false;
64 $this->conf = $conf;
65 $this->session = $session;
66 $this->linkDB = $linkDB;
67 $this->token = $token;
68 $this->isLoggedIn = $isLoggedIn;
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(
80 SHAARLI_VERSION,
81 $this->conf->get('resource.update_check'),
82 $this->conf->get('updates.check_updates_interval'),
83 $this->conf->get('updates.check_updates'),
84 $this->isLoggedIn,
85 $this->conf->get('updates.check_updates_branch')
86 );
87 $this->tpl->assign('newVersion', escape($version));
88 $this->tpl->assign('versionError', '');
89 } catch (Exception $exc) {
90 logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
91 $this->tpl->assign('newVersion', '');
92 $this->tpl->assign('versionError', escape($exc->getMessage()));
93 }
94
95 $this->tpl->assign('is_logged_in', $this->isLoggedIn);
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));
106 $this->tpl->assign('version', SHAARLI_VERSION);
107 $this->tpl->assign(
108 'version_hash',
109 ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
110 );
111 $this->tpl->assign('index_url', index_url($_SERVER));
112 $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
113 $this->tpl->assign('visibility', $visibility);
114 $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
115 $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
116 if ($this->conf->exists('general.header_link')) {
117 $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
118 }
119 $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
120 $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
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');
123 $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
124 $this->tpl->assign('token', $this->token);
125
126 $this->tpl->assign('language', $this->conf->get('translation.language'));
127
128 if ($this->linkDB !== null) {
129 $this->tpl->assign('tags', $this->linkDB->linksCountPerTag());
130 }
131
132 $this->tpl->assign(
133 'thumbnails_enabled',
134 $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
135 );
136 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
137 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
138
139 if (!empty($_SESSION['warnings'])) {
140 $this->tpl->assign('global_warnings', $_SESSION['warnings']);
141 unset($_SESSION['warnings']);
142 }
143
144 // To be removed with a proper theme configuration.
145 $this->tpl->assign('conf', $this->conf);
146 }
147
148 /**
149 * The following assign() method is basically the same as RainTPL (except lazy loading)
150 *
151 * @param string $placeholder Template placeholder.
152 * @param mixed $value Value to assign.
153 */
154 public function assign($placeholder, $value)
155 {
156 if ($this->tpl === false) {
157 $this->initialize();
158 }
159 $this->tpl->assign($placeholder, $value);
160 }
161
162 /**
163 * Assign an array of data to the template builder.
164 *
165 * @param array $data Data to assign.
166 *
167 * @return false if invalid data.
168 */
169 public function assignAll($data)
170 {
171 if ($this->tpl === false) {
172 $this->initialize();
173 }
174
175 if (empty($data) || !is_array($data)) {
176 return false;
177 }
178
179 foreach ($data as $key => $value) {
180 $this->assign($key, $value);
181 }
182 return true;
183 }
184
185 /**
186 * Render a specific page (using a template file).
187 * e.g. $pb->renderPage('picwall');
188 *
189 * @param string $page Template filename (without extension).
190 */
191 public function renderPage($page)
192 {
193 if ($this->tpl === false) {
194 $this->initialize();
195 }
196
197 $this->tpl->draw($page);
198 }
199
200 /**
201 * Render a 404 page (uses the template : tpl/404.tpl)
202 * usage: $PAGE->render404('The link was deleted')
203 *
204 * @param string $message A message to display what is not found
205 */
206 public function render404($message = '')
207 {
208 if (empty($message)) {
209 $message = t('The page you are trying to reach does not exist or has been deleted.');
210 }
211 header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found'));
212 $this->tpl->assign('error_message', $message);
213 $this->renderPage('404');
214 }
215}