]> git.mxchange.org Git - friendica.git/commitdiff
Rework theme override in Profile::load and App
authorHypolite Petovan <hypolite@mrpetovan.com>
Sun, 6 Oct 2019 16:07:06 +0000 (12:07 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 6 Oct 2019 16:07:06 +0000 (12:07 -0400)
- Separate desktop theme from mobile theme in App

src/App.php
src/Model/Profile.php

index 1f623c0cfeb8565c5101b8e80b3a203376004e73..c6ed818dd1a8ec23f932a1ef4eb826e8c45f7117 100644 (file)
@@ -92,10 +92,10 @@ class App
         */
        private $baseURL;
 
-       /**
-        * @var string The name of the current theme
-        */
+       /** @var string The name of the current theme */
        private $currentTheme;
+       /** @var string The name of the current mobile theme */
+       private $currentMobileTheme;
 
        /**
         * @var Configuration The config
@@ -450,10 +450,10 @@ class App
        }
 
        /**
-        * Returns the current theme name.
+        * Returns the current theme name. May be overriden by the mobile theme name.
         *
-        * @return string the name of the current theme
-        * @throws HTTPException\InternalServerErrorException
+        * @return string
+        * @throws Exception
         */
        public function getCurrentTheme()
        {
@@ -461,6 +461,16 @@ class App
                        return '';
                }
 
+               // Specific mobile theme override
+               if (($this->mode->isMobile() || $this->mode->isTablet()) && Core\Session::get('show-mobile', true)) {
+                       $user_mobile_theme = $this->getCurrentMobileTheme();
+
+                       // --- means same mobile theme as desktop
+                       if (!empty($user_mobile_theme) && $user_mobile_theme !== '---') {
+                               return $user_mobile_theme;
+                       }
+               }
+
                if (!$this->currentTheme) {
                        $this->computeCurrentTheme();
                }
@@ -468,13 +478,37 @@ class App
                return $this->currentTheme;
        }
 
+       /**
+        * Returns the current mobile theme name.
+        *
+        * @return string
+        * @throws Exception
+        */
+       public function getCurrentMobileTheme()
+       {
+               if ($this->mode->isInstall()) {
+                       return '';
+               }
+
+               if (is_null($this->currentMobileTheme)) {
+                       $this->computeCurrentMobileTheme();
+               }
+
+               return $this->currentMobileTheme;
+       }
+
        public function setCurrentTheme($theme)
        {
                $this->currentTheme = $theme;
        }
 
+       public function setCurrentMobileTheme($theme)
+       {
+               $this->currentMobileTheme = $theme;
+       }
+
        /**
-        * Computes the current theme name based on the node settings, the user settings and the device type
+        * Computes the current theme name based on the node settings, the page owner settings and the user settings
         *
         * @throws Exception
         */
@@ -486,7 +520,7 @@ class App
                }
 
                // Sane default
-               $this->currentTheme = $system_theme;
+               $this->setCurrentTheme($system_theme);
 
                $page_theme = null;
                // Find the theme that belongs to the user whose stuff we are looking at
@@ -499,24 +533,7 @@ class App
                        }
                }
 
-               $user_theme = Core\Session::get('theme', $system_theme);
-
-               // Specific mobile theme override
-               if (($this->is_mobile || $this->is_tablet) && Core\Session::get('show-mobile', true)) {
-                       $system_mobile_theme = $this->config->get('system', 'mobile-theme');
-                       $user_mobile_theme   = Core\Session::get('mobile-theme', $system_mobile_theme);
-
-                       // --- means same mobile theme as desktop
-                       if (!empty($user_mobile_theme) && $user_mobile_theme !== '---') {
-                               $user_theme = $user_mobile_theme;
-                       }
-               }
-
-               if ($page_theme) {
-                       $theme_name = $page_theme;
-               } else {
-                       $theme_name = $user_theme;
-               }
+               $theme_name = $page_theme ?: Core\Session::get('theme', $system_theme);
 
                $theme_name = Strings::sanitizeFilePathItem($theme_name);
                if ($theme_name
@@ -524,7 +541,40 @@ class App
                    && (file_exists('view/theme/' . $theme_name . '/style.css')
                        || file_exists('view/theme/' . $theme_name . '/style.php'))
                ) {
-                       $this->currentTheme = $theme_name;
+                       $this->setCurrentTheme($theme_name);
+               }
+       }
+
+       /**
+        * Computes the current mobile theme name based on the node settings, the page owner settings and the user settings
+        */
+       private function computeCurrentMobileTheme()
+       {
+               $system_mobile_theme = $this->config->get('system', 'mobile-theme', '');
+
+               // Sane default
+               $this->setCurrentMobileTheme($system_mobile_theme);
+
+               $page_mobile_theme = null;
+               // Find the theme that belongs to the user whose stuff we are looking at
+               if ($this->profile_uid && ($this->profile_uid != local_user())) {
+                       // Allow folks to override user themes and always use their own on their own site.
+                       // This works only if the user is on the same server
+                       if (!Core\PConfig::get(local_user(), 'system', 'always_my_theme')) {
+                               $page_mobile_theme = Core\PConfig::get($this->profile_uid, 'system', 'mobile-theme');
+                       }
+               }
+
+               $mobile_theme_name = $page_mobile_theme ?: Core\Session::get('mobile-theme', $system_mobile_theme);
+
+               $mobile_theme_name = Strings::sanitizeFilePathItem($mobile_theme_name);
+               if ($mobile_theme_name == '---'
+                       ||
+                       in_array($mobile_theme_name, Theme::getAllowedList())
+                       && (file_exists('view/theme/' . $mobile_theme_name . '/style.css')
+                               || file_exists('view/theme/' . $mobile_theme_name . '/style.php'))
+               ) {
+                       $this->setCurrentMobileTheme($mobile_theme_name);
                }
        }
 
@@ -534,7 +584,7 @@ class App
         * Provide a sane default if nothing is chosen or the specified theme does not exist.
         *
         * @return string
-        * @throws HTTPException\InternalServerErrorException
+        * @throws Exception
         */
        public function getCurrentThemeStylesheetPath()
        {
index cf39a02a913d9d120fd0e487e314ec6aa4e629d0..67c6a0eb68cd3ccdfe3f525248df802c4a55d58a 100644 (file)
@@ -20,6 +20,7 @@ use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\Session;
 use Friendica\Core\System;
+use Friendica\Core\Theme;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Protocol\Diaspora;
@@ -189,11 +190,10 @@ class Profile
                $a->page['title'] = $a->profile['name'] . ' @ ' . Config::get('config', 'sitename');
 
                if (!$profiledata && !PConfig::get(local_user(), 'system', 'always_my_theme')) {
-                       $_SESSION['theme'] = $a->profile['theme'];
+                       $a->setCurrentTheme($a->profile['theme']);
+                       $a->setCurrentMobileTheme($a->profile['mobile-theme']);
                }
 
-               $_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
-
                /*
                * load/reload current theme info
                */