]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Page.php
Creating interfaces for Config/PConfig & fix tests
[friendica.git] / src / App / Page.php
index 5298a66cbd0f2e097d765e5c6264a5c4d92d2781..5efb9f7ca3d64deae2f9fd1da93fce07964395d4 100644 (file)
@@ -7,14 +7,15 @@ use DOMDocument;
 use DOMXPath;
 use Friendica\App;
 use Friendica\Content\Nav;
-use Friendica\Core\Config\Configuration;
-use Friendica\Core\Config\PConfiguration;
+use Friendica\Core\Config\IConfiguration;
+use Friendica\Core\Config\IPConfiguration;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n\L10n;
 use Friendica\Core\Renderer;
 use Friendica\Core\Theme;
 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
 use Friendica\Network\HTTPException;
+use Friendica\Util\Strings;
 
 /**
  * Contains the page specific environment variables for the current Page
@@ -29,15 +30,26 @@ class Page implements ArrayAccess
        /**
         * @var array Contains all stylesheets, which should get loaded during page
         */
-       private $stylesheets;
+       private $stylesheets = [];
        /**
         * @var array Contains all scripts, which are added to the footer at last
         */
-       private $footerScripts;
+       private $footerScripts = [];
        /**
         * @var array The page content, which are showed directly
         */
-       private $page;
+       private $page = [
+               'aside'       => '',
+               'bottom'      => '',
+               'content'     => '',
+               'footer'      => '',
+               'htmlhead'    => '',
+               'nav'         => '',
+               'page_title'  => '',
+               'right_aside' => '',
+               'template'    => '',
+               'title'       => '',
+       ];
        /**
         * @var string The basepath of the page
         */
@@ -49,19 +61,6 @@ class Page implements ArrayAccess
        public function __construct(string $basepath)
        {
                $this->basePath = $basepath;
-
-               $this->page = [
-                       'aside'       => '',
-                       'bottom'      => '',
-                       'content'     => '',
-                       'footer'      => '',
-                       'htmlhead'    => '',
-                       'nav'         => '',
-                       'page_title'  => '',
-                       'right_aside' => '',
-                       'template'    => '',
-                       'title'       => ''
-               ];
        }
 
        /**
@@ -172,12 +171,12 @@ class Page implements ArrayAccess
         * @param App            $app     The Friendica App instance
         * @param Module         $module  The loaded Friendica module
         * @param L10n           $l10n    The l10n language instance
-        * @param Configuration  $config  The Friendica configuration
-        * @param PConfiguration $pConfig The Friendica personal configuration (for user)
+        * @param IConfiguration  $config  The Friendica configuration
+        * @param IPConfiguration $pConfig The Friendica personal configuration (for user)
         *
         * @throws HTTPException\InternalServerErrorException
         */
-       private function initHead(App $app, Module $module, L10n $l10n, Configuration $config, PConfiguration $pConfig)
+       private function initHead(App $app, Module $module, L10n $l10n, IConfiguration $config, IPConfiguration $pConfig)
        {
                $interval = ((local_user()) ? $pConfig->get(local_user(), 'system', 'update_interval') : 40000);
 
@@ -224,15 +223,15 @@ class Page implements ArrayAccess
                 * being first
                 */
                $this->page['htmlhead'] = Renderer::replaceMacros($tpl, [
-                               '$local_user'      => local_user(),
-                               '$generator'       => 'Friendica' . ' ' . FRIENDICA_VERSION,
-                               '$delitem'         => $l10n->t('Delete this item?'),
-                               '$update_interval' => $interval,
-                               '$shortcut_icon'   => $shortcut_icon,
-                               '$touch_icon'      => $touch_icon,
-                               '$block_public'    => intval($config->get('system', 'block_public')),
-                               '$stylesheets'     => $this->stylesheets,
-                       ]) . $this->page['htmlhead'];
+                       '$local_user'      => local_user(),
+                       '$generator'       => 'Friendica' . ' ' . FRIENDICA_VERSION,
+                       '$delitem'         => $l10n->t('Delete this item?'),
+                       '$update_interval' => $interval,
+                       '$shortcut_icon'   => $shortcut_icon,
+                       '$touch_icon'      => $touch_icon,
+                       '$block_public'    => intval($config->get('system', 'block_public')),
+                       '$stylesheets'     => array_unique($this->stylesheets),
+               ]) . $this->page['htmlhead'];
        }
 
        /**
@@ -245,11 +244,12 @@ class Page implements ArrayAccess
         * - footer.tpl template
         *
         * @param App  $app  The Friendica App instance
+        * @param Mode $mode The Friendica runtime mode
         * @param L10n $l10n The l10n instance
         *
         * @throws HTTPException\InternalServerErrorException
         */
-       private function initFooter(App $app, L10n $l10n)
+       private function initFooter(App $app, Mode $mode, L10n $l10n)
        {
                // If you're just visiting, let javascript take you home
                if (!empty($_SESSION['visitor_home'])) {
@@ -265,7 +265,7 @@ class Page implements ArrayAccess
                /*
                 * Add a "toggle mobile" link if we're using a mobile device
                 */
-               if ($app->is_mobile || $app->is_tablet) {
+               if ($mode->isMobile() || $mode->isTablet()) {
                        if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) {
                                $link = 'toggle_mobile?address=' . urlencode(curPageURL());
                        } else {
@@ -281,8 +281,8 @@ class Page implements ArrayAccess
 
                $tpl                  = Renderer::getMarkupTemplate('footer.tpl');
                $this->page['footer'] = Renderer::replaceMacros($tpl, [
-                               '$footerScripts' => $this->footerScripts,
-                       ]) . $this->page['footer'];
+                       '$footerScripts' => array_unique($this->footerScripts),
+               ]) . $this->page['footer'];
        }
 
        /**
@@ -307,7 +307,7 @@ class Page implements ArrayAccess
                        $arr = ['content' => $content];
                        Hook::callAll($moduleClass . '_mod_content', $arr);
                        $content = $arr['content'];
-                       $arr     = ['content' => call_user_func([$moduleClass, 'content'])];
+                       $arr     = ['content' => call_user_func([$moduleClass, 'content'], $module->getParameters())];
                        Hook::callAll($moduleClass . '_mod_aftercontent', $arr);
                        $content .= $arr['content'];
                } catch (HTTPException $e) {
@@ -347,12 +347,12 @@ class Page implements ArrayAccess
         * @param Mode           $mode    The current node mode
         * @param Module         $module  The loaded Friendica module
         * @param L10n           $l10n    The l10n language class
-        * @param Configuration  $config  The Configuration of this node
-        * @param PConfiguration $pconfig The personal/user configuration
+        * @param IConfiguration  $config  The Configuration of this node
+        * @param IPConfiguration $pconfig The personal/user configuration
         *
         * @throws HTTPException\InternalServerErrorException
         */
-       public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, Configuration $config, PConfiguration $pconfig)
+       public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, IConfiguration $config, IPConfiguration $pconfig)
        {
                $moduleName = $module->getName();
 
@@ -363,6 +363,18 @@ class Page implements ArrayAccess
                 */
                $this->initContent($module, $mode);
 
+               // Load current theme info after module has been initialized as theme could have been set in module
+               $currentTheme = $app->getCurrentTheme();
+               $theme_info_file = 'view/theme/' . $currentTheme . '/theme.php';
+               if (file_exists($theme_info_file)) {
+                       require_once $theme_info_file;
+               }
+
+               if (function_exists(str_replace('-', '_', $currentTheme) . '_init')) {
+                       $func = str_replace('-', '_', $currentTheme) . '_init';
+                       $func($app);
+               }
+
                /* Create the page head after setting the language
                 * and getting any auth credentials.
                 *
@@ -375,9 +387,9 @@ class Page implements ArrayAccess
                /* Build the page ending -- this is stuff that goes right before
                 * the closing </body> tag
                 */
-               $this->initFooter($app, $l10n);
+               $this->initFooter($app, $mode, $l10n);
 
-               if (!$app->isAjax()) {
+               if (!$mode->isAjax()) {
                        Hook::callAll('page_end', $this->page['content']);
                }
 
@@ -442,13 +454,13 @@ class Page implements ArrayAccess
                 * to load another page template than the default one.
                 * The page templates are located in /view/php/ or in the theme directory.
                 */
-               if (isset($_GET["mode"])) {
-                       $template = Theme::getPathForFile($_GET["mode"] . '.php');
+               if (isset($_GET['mode'])) {
+                       $template = Theme::getPathForFile('php/' . Strings::sanitizeFilePathItem($_GET['mode']) . '.php');
                }
 
                // If there is no page template use the default page template
                if (empty($template)) {
-                       $template = Theme::getPathForFile("default.php");
+                       $template = Theme::getPathForFile('php/default.php');
                }
 
                // Theme templates expect $a as an App instance
@@ -457,7 +469,6 @@ class Page implements ArrayAccess
                // Used as is in view/php/default.php
                $lang = $l10n->getCurrentLang();
 
-               /// @TODO Looks unsafe (remote-inclusion), is maybe not but Core\Theme::getPathForFile() uses file_exists() but does not escape anything
                require_once $template;
        }
 }