]>
Commit | Line | Data |
---|---|---|
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 LinkDB $linkDB instance. | |
26 | */ | |
27 | protected $linkDB; | |
28 | ||
29 | /** @var bool $isLoggedIn Whether the user is logged in **/ | |
30 | protected $isLoggedIn = false; | |
31 | ||
32 | /** | |
33 | * PageBuilder constructor. | |
34 | * $tpl is initialized at false for lazy loading. | |
35 | * | |
36 | * @param ConfigManager $conf Configuration Manager instance (reference). | |
37 | * @param LinkDB $linkDB instance. | |
38 | * @param string $token Session token | |
39 | */ | |
40 | public function __construct(&$conf, $linkDB = null, $token = null, $isLoggedIn = false) | |
41 | { | |
42 | $this->tpl = false; | |
43 | $this->conf = $conf; | |
44 | $this->linkDB = $linkDB; | |
45 | $this->token = $token; | |
46 | $this->isLoggedIn = $isLoggedIn; | |
47 | } | |
48 | ||
49 | /** | |
50 | * Initialize all default tpl tags. | |
51 | */ | |
52 | private function initialize() | |
53 | { | |
54 | $this->tpl = new RainTPL(); | |
55 | ||
56 | try { | |
57 | $version = ApplicationUtils::checkUpdate( | |
58 | SHAARLI_VERSION, | |
59 | $this->conf->get('resource.update_check'), | |
60 | $this->conf->get('updates.check_updates_interval'), | |
61 | $this->conf->get('updates.check_updates'), | |
62 | $this->isLoggedIn, | |
63 | $this->conf->get('updates.check_updates_branch') | |
64 | ); | |
65 | $this->tpl->assign('newVersion', escape($version)); | |
66 | $this->tpl->assign('versionError', ''); | |
67 | ||
68 | } catch (Exception $exc) { | |
69 | logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage()); | |
70 | $this->tpl->assign('newVersion', ''); | |
71 | $this->tpl->assign('versionError', escape($exc->getMessage())); | |
72 | } | |
73 | ||
74 | $this->tpl->assign('is_logged_in', $this->isLoggedIn); | |
75 | $this->tpl->assign('feedurl', escape(index_url($_SERVER))); | |
76 | $searchcrits = ''; // Search criteria | |
77 | if (!empty($_GET['searchtags'])) { | |
78 | $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']); | |
79 | } | |
80 | if (!empty($_GET['searchterm'])) { | |
81 | $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']); | |
82 | } | |
83 | $this->tpl->assign('searchcrits', $searchcrits); | |
84 | $this->tpl->assign('source', index_url($_SERVER)); | |
85 | $this->tpl->assign('version', SHAARLI_VERSION); | |
86 | $this->tpl->assign( | |
87 | 'version_hash', | |
88 | ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt')) | |
89 | ); | |
90 | $this->tpl->assign('scripturl', index_url($_SERVER)); | |
91 | $visibility = ! empty($_SESSION['visibility']) ? $_SESSION['visibility'] : ''; | |
92 | $this->tpl->assign('visibility', $visibility); | |
93 | $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly'])); | |
94 | $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli')); | |
95 | if ($this->conf->exists('general.header_link')) { | |
96 | $this->tpl->assign('titleLink', $this->conf->get('general.header_link')); | |
97 | } | |
98 | $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli')); | |
99 | $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false)); | |
100 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); | |
101 | $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); | |
102 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); | |
103 | $this->tpl->assign('token', $this->token); | |
104 | ||
105 | if ($this->linkDB !== null) { | |
106 | $this->tpl->assign('tags', $this->linkDB->linksCountPerTag()); | |
107 | } | |
108 | // To be removed with a proper theme configuration. | |
109 | $this->tpl->assign('conf', $this->conf); | |
110 | } | |
111 | ||
112 | /** | |
113 | * The following assign() method is basically the same as RainTPL (except lazy loading) | |
114 | * | |
115 | * @param string $placeholder Template placeholder. | |
116 | * @param mixed $value Value to assign. | |
117 | */ | |
118 | public function assign($placeholder, $value) | |
119 | { | |
120 | if ($this->tpl === false) { | |
121 | $this->initialize(); | |
122 | } | |
123 | $this->tpl->assign($placeholder, $value); | |
124 | } | |
125 | ||
126 | /** | |
127 | * Assign an array of data to the template builder. | |
128 | * | |
129 | * @param array $data Data to assign. | |
130 | * | |
131 | * @return false if invalid data. | |
132 | */ | |
133 | public function assignAll($data) | |
134 | { | |
135 | if ($this->tpl === false) { | |
136 | $this->initialize(); | |
137 | } | |
138 | ||
139 | if (empty($data) || !is_array($data)){ | |
140 | return false; | |
141 | } | |
142 | ||
143 | foreach ($data as $key => $value) { | |
144 | $this->assign($key, $value); | |
145 | } | |
146 | return true; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Render a specific page (using a template file). | |
151 | * e.g. $pb->renderPage('picwall'); | |
152 | * | |
153 | * @param string $page Template filename (without extension). | |
154 | */ | |
155 | public function renderPage($page) | |
156 | { | |
157 | if ($this->tpl === false) { | |
158 | $this->initialize(); | |
159 | } | |
160 | ||
161 | $this->tpl->draw($page); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Render a 404 page (uses the template : tpl/404.tpl) | |
166 | * usage : $PAGE->render404('The link was deleted') | |
167 | * | |
168 | * @param string $message A messate to display what is not found | |
169 | */ | |
170 | public function render404($message = '') | |
171 | { | |
172 | if (empty($message)) { | |
173 | $message = t('The page you are trying to reach does not exist or has been deleted.'); | |
174 | } | |
175 | header($_SERVER['SERVER_PROTOCOL'] .' '. t('404 Not Found')); | |
176 | $this->tpl->assign('error_message', $message); | |
177 | $this->renderPage('404'); | |
178 | } | |
179 | } |