3 use Shaarli\Config\ConfigManager
;
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');
15 * @var RainTPL RainTPL instance.
20 * @var ConfigManager $conf Configuration Manager instance.
25 * @var array $_SESSION
30 * @var LinkDB $linkDB instance.
35 * @var null|string XSRF token
39 /** @var bool $isLoggedIn Whether the user is logged in **/
40 protected $isLoggedIn = false;
43 * PageBuilder constructor.
44 * $tpl is initialized at false for lazy loading.
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
52 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
56 $this->session
= $session;
57 $this->linkDB
= $linkDB;
58 $this->token
= $token;
59 $this->isLoggedIn
= $isLoggedIn;
63 * Initialize all default tpl tags.
65 private function initialize()
67 $this->tpl
= new RainTPL();
70 $version = ApplicationUtils
::checkUpdate(
72 $this->conf
->get('resource.update_check'),
73 $this->conf
->get('updates.check_updates_interval'),
74 $this->conf
->get('updates.check_updates'),
76 $this->conf
->get('updates.check_updates_branch')
78 $this->tpl
->assign('newVersion', escape($version));
79 $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()));
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']);
93 if (!empty($_GET['searchterm'])) {
94 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
96 $this->tpl
->assign('searchcrits', $searchcrits);
97 $this->tpl
->assign('source', index_url($_SERVER));
98 $this->tpl
->assign('version', SHAARLI_VERSION
);
101 ApplicationUtils
::getVersionHash(SHAARLI_VERSION
, $this->conf
->get('credentials.salt'))
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'));
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
);
118 if ($this->linkDB
!== null) {
119 $this->tpl
->assign('tags', $this->linkDB
->linksCountPerTag());
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'));
126 if (! empty($_SESSION['warnings'])) {
127 $this->tpl
->assign('global_warnings', $_SESSION['warnings']);
128 unset($_SESSION['warnings']);
131 // To be removed with a proper theme configuration.
132 $this->tpl
->assign('conf', $this->conf
);
136 * The following assign() method is basically the same as RainTPL (except lazy loading)
138 * @param string $placeholder Template placeholder.
139 * @param mixed $value Value to assign.
141 public function assign($placeholder, $value)
143 if ($this->tpl
=== false) {
146 $this->tpl
->assign($placeholder, $value);
150 * Assign an array of data to the template builder.
152 * @param array $data Data to assign.
154 * @return false if invalid data.
156 public function assignAll($data)
158 if ($this->tpl
=== false) {
162 if (empty($data) || !is_array($data)){
166 foreach ($data as $key => $value) {
167 $this->assign($key, $value);
173 * Render a specific page (using a template file).
174 * e.g. $pb->renderPage('picwall');
176 * @param string $page Template filename (without extension).
178 public function renderPage($page)
180 if ($this->tpl
=== false) {
184 $this->tpl
->draw($page);
188 * Render a 404 page (uses the template : tpl/404.tpl)
189 * usage : $PAGE->render404('The link was deleted')
191 * @param string $message A messate to display what is not found
193 public function render404($message = '')
195 if (empty($message)) {
196 $message = t('The page you are trying to reach does not exist or has been deleted.');
198 header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found'));
199 $this->tpl
->assign('error_message', $message);
200 $this->renderPage('404');