3 namespace Friendica\App;
9 use Friendica\Content\Nav;
10 use Friendica\Core\Config\Configuration;
11 use Friendica\Core\Config\PConfiguration;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n\L10n;
14 use Friendica\Core\Renderer;
15 use Friendica\Core\Theme;
16 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
17 use Friendica\Network\HTTPException;
20 * Contains the page specific environment variables for the current Page
21 * - Contains all stylesheets
22 * - Contains all footer-scripts
23 * - Contains all page specific content (header, footer, content, ...)
25 * The run() method is the single point where the page will get printed to the screen
27 class Page implements ArrayAccess
30 * @var array Contains all stylesheets, which should get loaded during page
34 * @var array Contains all scripts, which are added to the footer at last
36 private $footerScripts;
38 * @var array The page content, which are showed directly
42 * @var string The basepath of the page
47 * @param string $basepath The Page basepath
49 public function __construct(string $basepath)
51 $this->basePath = $basepath;
68 * Whether a offset exists
70 * @link https://php.net/manual/en/arrayaccess.offsetexists.php
72 * @param mixed $offset <p>
73 * An offset to check for.
76 * @return boolean true on success or false on failure.
79 * The return value will be casted to boolean if non-boolean was returned.
82 public function offsetExists($offset)
84 return isset($this->page[$offset]);
90 * @link https://php.net/manual/en/arrayaccess.offsetget.php
92 * @param mixed $offset <p>
93 * The offset to retrieve.
96 * @return mixed Can return all value types.
99 public function offsetGet($offset)
101 return $this->page[$offset] ?? null;
107 * @link https://php.net/manual/en/arrayaccess.offsetset.php
109 * @param mixed $offset <p>
110 * The offset to assign the value to.
112 * @param mixed $value <p>
119 public function offsetSet($offset, $value)
121 $this->page[$offset] = $value;
127 * @link https://php.net/manual/en/arrayaccess.offsetunset.php
129 * @param mixed $offset <p>
130 * The offset to unset.
136 public function offsetUnset($offset)
138 if (isset($this->page[$offset])) {
139 unset($this->page[$offset]);
144 * Register a stylesheet file path to be included in the <head> tag of every page.
145 * Inclusion is done in App->initHead().
146 * The path can be absolute or relative to the Friendica installation base folder.
148 * @param string $path
150 * @see Page::initHead()
153 public function registerStylesheet($path)
155 if (mb_strpos($path, $this->basePath . DIRECTORY_SEPARATOR) === 0) {
156 $path = mb_substr($path, mb_strlen($this->basePath . DIRECTORY_SEPARATOR));
159 $this->stylesheets[] = trim($path, '/');
163 * Initializes Page->page['htmlhead'].
168 * - Registered stylesheets (through App->registerStylesheet())
169 * - Infinite scroll data
170 * - head.tpl template
172 * @param App $app The Friendica App instance
173 * @param Module $module The loaded Friendica module
174 * @param L10n $l10n The l10n language instance
175 * @param Configuration $config The Friendica configuration
176 * @param PConfiguration $pConfig The Friendica personal configuration (for user)
178 * @throws HTTPException\InternalServerErrorException
180 private function initHead(App $app, Module $module, L10n $l10n, Configuration $config, PConfiguration $pConfig)
182 $interval = ((local_user()) ? $pConfig->get(local_user(), 'system', 'update_interval') : 40000);
184 // If the update is 'deactivated' set it to the highest integer number (~24 days)
186 $interval = 2147483647;
189 if ($interval < 10000) {
193 // Default title: current module called
194 if (empty($this->page['title']) && $module->getName()) {
195 $this->page['title'] = ucfirst($module->getName());
198 // Prepend the sitename to the page title
199 $this->page['title'] = $config->get('config', 'sitename', '') . (!empty($this->page['title']) ? ' | ' . $this->page['title'] : '');
201 if (!empty(Renderer::$theme['stylesheet'])) {
202 $stylesheet = Renderer::$theme['stylesheet'];
204 $stylesheet = $app->getCurrentThemeStylesheetPath();
207 $this->registerStylesheet($stylesheet);
209 $shortcut_icon = $config->get('system', 'shortcut_icon');
210 if ($shortcut_icon == '') {
211 $shortcut_icon = 'images/friendica-32.png';
214 $touch_icon = $config->get('system', 'touch_icon');
215 if ($touch_icon == '') {
216 $touch_icon = 'images/friendica-128.png';
219 Hook::callAll('head', $this->page['htmlhead']);
221 $tpl = Renderer::getMarkupTemplate('head.tpl');
222 /* put the head template at the beginning of page['htmlhead']
223 * since the code added by the modules frequently depends on it
226 $this->page['htmlhead'] = Renderer::replaceMacros($tpl, [
227 '$local_user' => local_user(),
228 '$generator' => 'Friendica' . ' ' . FRIENDICA_VERSION,
229 '$delitem' => $l10n->t('Delete this item?'),
230 '$update_interval' => $interval,
231 '$shortcut_icon' => $shortcut_icon,
232 '$touch_icon' => $touch_icon,
233 '$block_public' => intval($config->get('system', 'block_public')),
234 '$stylesheets' => $this->stylesheets,
235 ]) . $this->page['htmlhead'];
239 * Initializes Page->page['footer'].
242 * - Javascript homebase
243 * - Mobile toggle link
244 * - Registered footer scripts (through App->registerFooterScript())
245 * - footer.tpl template
247 * @param App $app The Friendica App instance
248 * @param L10n $l10n The l10n instance
250 * @throws HTTPException\InternalServerErrorException
252 private function initFooter(App $app, L10n $l10n)
254 // If you're just visiting, let javascript take you home
255 if (!empty($_SESSION['visitor_home'])) {
256 $homebase = $_SESSION['visitor_home'];
257 } elseif (local_user()) {
258 $homebase = 'profile/' . $app->user['nickname'];
261 if (isset($homebase)) {
262 $this->page['footer'] .= '<script>var homebase="' . $homebase . '";</script>' . "\n";
266 * Add a "toggle mobile" link if we're using a mobile device
268 if ($app->is_mobile || $app->is_tablet) {
269 if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
270 $link = 'toggle_mobile?address=' . urlencode(curPageURL());
272 $link = 'toggle_mobile?off=1&address=' . urlencode(curPageURL());
274 $this->page['footer'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("toggle_mobile_footer.tpl"), [
275 '$toggle_link' => $link,
276 '$toggle_text' => $l10n->t('toggle mobile')
280 Hook::callAll('footer', $this->page['footer']);
282 $tpl = Renderer::getMarkupTemplate('footer.tpl');
283 $this->page['footer'] = Renderer::replaceMacros($tpl, [
284 '$footerScripts' => $this->footerScripts,
285 ]) . $this->page['footer'];
289 * Initializes Page->page['content'].
293 * - hooks for content
295 * @param Module $module The module
296 * @param Mode $mode The Friendica execution mode
298 * @throws HTTPException\InternalServerErrorException
300 private function initContent(Module $module, Mode $mode)
305 $moduleClass = $module->getClassName();
307 $arr = ['content' => $content];
308 Hook::callAll($moduleClass . '_mod_content', $arr);
309 $content = $arr['content'];
310 $arr = ['content' => call_user_func([$moduleClass, 'content'])];
311 Hook::callAll($moduleClass . '_mod_aftercontent', $arr);
312 $content .= $arr['content'];
313 } catch (HTTPException $e) {
314 $content = ModuleHTTPException::content($e);
317 // initialise content region
318 if ($mode->isNormal()) {
319 Hook::callAll('page_content_top', $this->page['content']);
322 $this->page['content'] .= $content;
326 * Register a javascript file path to be included in the <footer> tag of every page.
327 * Inclusion is done in App->initFooter().
328 * The path can be absolute or relative to the Friendica installation base folder.
330 * @param string $path
332 * @see Page::initFooter()
335 public function registerFooterScript($path)
337 $url = str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);
339 $this->footerScripts[] = trim($url, '/');
343 * Executes the creation of the current page and prints it to the screen
345 * @param App $app The Friendica App
346 * @param BaseURL $baseURL The Friendica Base URL
347 * @param Mode $mode The current node mode
348 * @param Module $module The loaded Friendica module
349 * @param L10n $l10n The l10n language class
350 * @param Configuration $config The Configuration of this node
351 * @param PConfiguration $pconfig The personal/user configuration
353 * @throws HTTPException\InternalServerErrorException
355 public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, Configuration $config, PConfiguration $pconfig)
357 $moduleName = $module->getName();
359 /* Create the page content.
360 * Calls all hooks which are including content operations
362 * Sets the $Page->page['content'] variable
364 $this->initContent($module, $mode);
366 /* Create the page head after setting the language
367 * and getting any auth credentials.
369 * Moved initHead() and initFooter() to after
370 * all the module functions have executed so that all
371 * theme choices made by the modules can take effect.
373 $this->initHead($app, $module, $l10n, $config, $pconfig);
375 /* Build the page ending -- this is stuff that goes right before
376 * the closing </body> tag
378 $this->initFooter($app, $l10n);
380 if (!$app->isAjax()) {
381 Hook::callAll('page_end', $this->page['content']);
384 // Add the navigation (menu) template
385 if ($moduleName != 'install' && $moduleName != 'maintenance') {
386 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('nav_head.tpl'), []);
387 $this->page['nav'] = Nav::build($app);
390 // Build the page - now that we have all the components
391 if (isset($_GET["mode"]) && (($_GET["mode"] == "raw") || ($_GET["mode"] == "minimal"))) {
392 $doc = new DOMDocument();
394 $target = new DOMDocument();
395 $target->loadXML("<root></root>");
397 $content = mb_convert_encoding($this->page["content"], 'HTML-ENTITIES', "UTF-8");
399 /// @TODO one day, kill those error-surpressing @ stuff, or PHP should ban it
400 @$doc->loadHTML($content);
402 $xpath = new DOMXPath($doc);
404 $list = $xpath->query("//*[contains(@id,'tread-wrapper-')]"); /* */
406 foreach ($list as $item) {
407 $item = $target->importNode($item, true);
409 // And then append it to the target
410 $target->documentElement->appendChild($item);
413 if ($_GET["mode"] == "raw") {
414 header("Content-type: text/html; charset=utf-8");
416 echo substr($target->saveHTML(), 6, -8);
423 $profile = $app->profile;
425 header("X-Friendica-Version: " . FRIENDICA_VERSION);
426 header("Content-type: text/html; charset=utf-8");
428 if ($config->get('system', 'hsts') && ($baseURL->getSSLPolicy() == BaseURL::SSL_POLICY_FULL)) {
429 header("Strict-Transport-Security: max-age=31536000");
432 // Some security stuff
433 header('X-Content-Type-Options: nosniff');
434 header('X-XSS-Protection: 1; mode=block');
435 header('X-Permitted-Cross-Domain-Policies: none');
436 header('X-Frame-Options: sameorigin');
438 // Things like embedded OSM maps don't work, when this is enabled
439 // header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; connect-src 'self'; style-src 'self' 'unsafe-inline'; font-src 'self'; img-src 'self' https: data:; media-src 'self' https:; child-src 'self' https:; object-src 'none'");
441 /* We use $_GET["mode"] for special page templates. So we will check if we have
442 * to load another page template than the default one.
443 * The page templates are located in /view/php/ or in the theme directory.
445 if (isset($_GET["mode"])) {
446 $template = Theme::getPathForFile($_GET["mode"] . '.php');
449 // If there is no page template use the default page template
450 if (empty($template)) {
451 $template = Theme::getPathForFile("default.php");
454 // Theme templates expect $a as an App instance
457 // Used as is in view/php/default.php
458 $lang = $l10n->getCurrentLang();
460 /// @TODO Looks unsafe (remote-inclusion), is maybe not but Core\Theme::getPathForFile() uses file_exists() but does not escape anything
461 require_once $template;