aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/render/PageBuilder.php
blob: 2d6d2dbed983d4fe2d8f0d2db00003f118e55141 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php

namespace Shaarli\Render;

use Exception;
use exceptions\MissingBasePathException;
use RainTPL;
use Shaarli\ApplicationUtils;
use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Security\SessionManager;
use Shaarli\Thumbnailer;

/**
 * This class is in charge of building the final page.
 * (This is basically a wrapper around RainTPL which pre-fills some fields.)
 * $p = new PageBuilder();
 * $p->assign('myfield','myvalue');
 * $p->renderPage('mytemplate');
 */
class PageBuilder
{
    /**
     * @var RainTPL RainTPL instance.
     */
    private $tpl;

    /**
     * @var ConfigManager $conf Configuration Manager instance.
     */
    protected $conf;

    /**
     * @var array $_SESSION
     */
    protected $session;

    /**
     * @var BookmarkServiceInterface $bookmarkService instance.
     */
    protected $bookmarkService;

    /**
     * @var null|string XSRF token
     */
    protected $token;

    /**
     * @var bool $isLoggedIn Whether the user is logged in
     */
    protected $isLoggedIn = false;

    /**
     * PageBuilder constructor.
     * $tpl is initialized at false for lazy loading.
     *
     * @param ConfigManager            $conf    Configuration Manager instance (reference).
     * @param array                    $session $_SESSION array
     * @param BookmarkServiceInterface $linkDB  instance.
     * @param string                   $token   Session token
     * @param bool                     $isLoggedIn
     */
    public function __construct(&$conf, $session, $linkDB = null, $token = null, $isLoggedIn = false)
    {
        $this->tpl = false;
        $this->conf = $conf;
        $this->session = $session;
        $this->bookmarkService = $linkDB;
        $this->token = $token;
        $this->isLoggedIn = $isLoggedIn;
    }

    /**
     * Reset current state of template rendering.
     * Mostly useful for error handling. We remove everything, and display the error template.
     */
    public function reset(): void
    {
        $this->tpl = false;
    }

    /**
     * Initialize all default tpl tags.
     */
    private function initialize()
    {
        $this->tpl = new RainTPL();

        try {
            $version = ApplicationUtils::checkUpdate(
                SHAARLI_VERSION,
                $this->conf->get('resource.update_check'),
                $this->conf->get('updates.check_updates_interval'),
                $this->conf->get('updates.check_updates'),
                $this->isLoggedIn,
                $this->conf->get('updates.check_updates_branch')
            );
            $this->tpl->assign('newVersion', escape($version));
            $this->tpl->assign('versionError', '');
        } catch (Exception $exc) {
            logm($this->conf->get('resource.log'), $_SERVER['REMOTE_ADDR'], $exc->getMessage());
            $this->tpl->assign('newVersion', '');
            $this->tpl->assign('versionError', escape($exc->getMessage()));
        }

        $this->tpl->assign('is_logged_in', $this->isLoggedIn);
        $this->tpl->assign('feedurl', escape(index_url($_SERVER)));
        $searchcrits = ''; // Search criteria
        if (!empty($_GET['searchtags'])) {
            $searchcrits .= '&searchtags=' . urlencode($_GET['searchtags']);
        }
        if (!empty($_GET['searchterm'])) {
            $searchcrits .= '&searchterm=' . urlencode($_GET['searchterm']);
        }
        $this->tpl->assign('searchcrits', $searchcrits);
        $this->tpl->assign('source', index_url($_SERVER));
        $this->tpl->assign('version', SHAARLI_VERSION);
        $this->tpl->assign(
            'version_hash',
            ApplicationUtils::getVersionHash(SHAARLI_VERSION, $this->conf->get('credentials.salt'))
        );
        $this->tpl->assign('index_url', index_url($_SERVER));
        $visibility = !empty($_SESSION['visibility']) ? $_SESSION['visibility'] : '';
        $this->tpl->assign('visibility', $visibility);
        $this->tpl->assign('untaggedonly', !empty($_SESSION['untaggedonly']));
        $this->tpl->assign('pagetitle', $this->conf->get('general.title', 'Shaarli'));
        if ($this->conf->exists('general.header_link')) {
            $this->tpl->assign('titleLink', $this->conf->get('general.header_link'));
        }
        $this->tpl->assign('shaarlititle', $this->conf->get('general.title', 'Shaarli'));
        $this->tpl->assign('openshaarli', $this->conf->get('security.open_shaarli', false));
        $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true));
        $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss');
        $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false));
        $this->tpl->assign('token', $this->token);

        $this->tpl->assign('language', $this->conf->get('translation.language'));

        if ($this->bookmarkService !== null) {
            $this->tpl->assign('tags', escape($this->bookmarkService->bookmarksCountPerTag()));
        }

        $this->tpl->assign(
            'thumbnails_enabled',
            $this->conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
        );
        $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
        $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));

        $this->tpl->assign('formatter', $this->conf->get('formatter', 'default'));

        $this->tpl->assign('links_per_page', $this->session['LINKS_PER_PAGE']);

        // To be removed with a proper theme configuration.
        $this->tpl->assign('conf', $this->conf);
    }

    /**
     * Affect variable after controller processing.
     * Used for alert messages.
     */
    protected function finalize(string $basePath): void
    {
        // TODO: use the SessionManager
        $messageKeys = [
            SessionManager::KEY_SUCCESS_MESSAGES,
            SessionManager::KEY_WARNING_MESSAGES,
            SessionManager::KEY_ERROR_MESSAGES
        ];
        foreach ($messageKeys as $messageKey) {
            if (!empty($_SESSION[$messageKey])) {
                $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]);
                unset($_SESSION[$messageKey]);
            }
        }

        $rootPath = preg_replace('#/index\.php$#', '', $basePath);
        $this->assign('base_path', $basePath);
        $this->assign('root_path', $rootPath);
        $this->assign(
            'asset_path',
            $rootPath . '/' .
            rtrim($this->conf->get('resource.raintpl_tpl', 'tpl'), '/') . '/' .
            $this->conf->get('resource.theme', 'default')
        );
    }

    /**
     * The following assign() method is basically the same as RainTPL (except lazy loading)
     *
     * @param string $placeholder Template placeholder.
     * @param mixed  $value       Value to assign.
     */
    public function assign($placeholder, $value)
    {
        if ($this->tpl === false) {
            $this->initialize();
        }
        $this->tpl->assign($placeholder, $value);
    }

    /**
     * Assign an array of data to the template builder.
     *
     * @param array $data Data to assign.
     *
     * @return false if invalid data.
     */
    public function assignAll($data)
    {
        if ($this->tpl === false) {
            $this->initialize();
        }

        if (empty($data) || !is_array($data)) {
            return false;
        }

        foreach ($data as $key => $value) {
            $this->assign($key, $value);
        }
        return true;
    }

    /**
     * Render a specific page as string (using a template file).
     * e.g. $pb->render('picwall');
     *
     * @param string $page Template filename (without extension).
     *
     * @return string Processed template content
     */
    public function render(string $page, string $basePath): string
    {
        if ($this->tpl === false) {
            $this->initialize();
        }

        $this->finalize($basePath);

        return $this->tpl->draw($page, true);
    }
}