3 namespace Shaarli\Render
;
7 use Shaarli\ApplicationUtils
;
8 use Shaarli\Bookmark\LinkDB
;
9 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Thumbnailer
;
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');
22 * @var RainTPL RainTPL instance.
27 * @var ConfigManager $conf Configuration Manager instance.
32 * @var array $_SESSION
37 * @var LinkDB $linkDB instance.
42 * @var null|string XSRF token
47 * @var bool $isLoggedIn Whether the user is logged in
49 protected $isLoggedIn = false;
52 * PageBuilder constructor.
53 * $tpl is initialized at false for lazy loading.
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
61 public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
65 $this->session
= $session;
66 $this->linkDB
= $linkDB;
67 $this->token
= $token;
68 $this->isLoggedIn
= $isLoggedIn;
72 * Initialize all default tpl tags.
74 private function initialize()
76 $this->tpl
= new RainTPL();
79 $version = ApplicationUtils
::checkUpdate(
81 $this->conf
->get('resource.update_check'),
82 $this->conf
->get('updates.check_updates_interval'),
83 $this->conf
->get('updates.check_updates'),
85 $this->conf
->get('updates.check_updates_branch')
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()));
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']);
101 if (!empty($_GET['searchterm'])) {
102 $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
104 $this->tpl
->assign('searchcrits', $searchcrits);
105 $this->tpl
->assign('source', index_url($_SERVER));
106 $this->tpl
->assign('version', SHAARLI_VERSION
);
109 ApplicationUtils
::getVersionHash(SHAARLI_VERSION
, $this->conf
->get('credentials.salt'))
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'));
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
);
126 if ($this->linkDB
!== null) {
127 $this->tpl
->assign('tags', $this->linkDB
->linksCountPerTag());
131 'thumbnails_enabled',
132 $this->conf
->get('thumbnails.mode', Thumbnailer
::MODE_NONE
) !== Thumbnailer
::MODE_NONE
134 $this->tpl
->assign('thumbnails_width', $this->conf
->get('thumbnails.width'));
135 $this->tpl
->assign('thumbnails_height', $this->conf
->get('thumbnails.height'));
137 if (!empty($_SESSION['warnings'])) {
138 $this->tpl
->assign('global_warnings', $_SESSION['warnings']);
139 unset($_SESSION['warnings']);
142 // To be removed with a proper theme configuration.
143 $this->tpl
->assign('conf', $this->conf
);
147 * The following assign() method is basically the same as RainTPL (except lazy loading)
149 * @param string $placeholder Template placeholder.
150 * @param mixed $value Value to assign.
152 public function assign($placeholder, $value)
154 if ($this->tpl
=== false) {
157 $this->tpl
->assign($placeholder, $value);
161 * Assign an array of data to the template builder.
163 * @param array $data Data to assign.
165 * @return false if invalid data.
167 public function assignAll($data)
169 if ($this->tpl
=== false) {
173 if (empty($data) || !is_array($data)) {
177 foreach ($data as $key => $value) {
178 $this->assign($key, $value);
184 * Render a specific page (using a template file).
185 * e.g. $pb->renderPage('picwall');
187 * @param string $page Template filename (without extension).
189 public function renderPage($page)
191 if ($this->tpl
=== false) {
195 $this->tpl
->draw($page);
199 * Render a 404 page (uses the template : tpl/404.tpl)
200 * usage: $PAGE->render404('The link was deleted')
202 * @param string $message A message to display what is not found
204 public function render404($message = '')
206 if (empty($message)) {
207 $message = t('The page you are trying to reach does not exist or has been deleted.');
209 header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found'));
210 $this->tpl
->assign('error_message', $message);
211 $this->renderPage('404');