Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Do not load disabled app-themes #31478

Merged
merged 1 commit into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/private/Helper/EnvironmentHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* @author Viktar Dubiniuk <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Helper;

/**
* This class provides non-static wrappers for the static OC class members
*/
class EnvironmentHelper {
/**
* Get the ownCloud root path for http requests (e.g. owncloud/)
*
* @return string
*/
public function getWebRoot() {
return \OC::$WEBROOT;
}

/**
* Get the installation path for owncloud on the server
* (e.g. /srv/http/owncloud)
*
* @return string
*/
public function getServerRoot() {
return \OC::$SERVERROOT;
}

/**
* Get the apps folders location on the server as an array of
* arrays with 'path' and 'url' keys
* where 'path' keys holds an absolute filesystem path to the folder
* and 'url' key holds a web path relative to the ownCloud webroot
*
* @return string[][]
*/
public function getAppsRoots() {
return \OC::$APPSROOTS;
}
}
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,8 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerService('ThemeService', function ($c) {
return new ThemeService(
$this->getSystemConfig()->getValue('theme'),
\OC::$SERVERROOT
$c->getAppManager(),
new \OC\Helper\EnvironmentHelper()
);
});
$this->registerAlias('OCP\Theme\IThemeService', 'ThemeService');
Expand Down
97 changes: 60 additions & 37 deletions lib/private/Theme/ThemeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
*/
namespace OC\Theme;

use OCP\App\IAppManager;
use OCP\Theme\IThemeService;
use OC\Helper\EnvironmentHelper;

/**
* Class ThemeService
*
* @package OC\Theme
*/
class ThemeService implements IThemeService {

const DEFAULT_THEME_PATH = '/themes/default';
Expand All @@ -29,23 +36,29 @@ class ThemeService implements IThemeService {
* @var Theme
*/
private $theme;

/** @var string */
private $serverRoot;

/** @var string */
private $defaultThemeDirectory;
/**
* @var IAppManager
*/
private $appManager;

/**
* @var EnvironmentHelper
*/
private $environmentHelper;

/**
* ThemeService constructor.
*
* @param string $themeName
* @param string $serverRoot
* @param IAppManager $appManager
* @param EnvironmentHelper $environmentHelper
*/
public function __construct($themeName, $serverRoot) {
$this->serverRoot = $serverRoot;
$this->defaultThemeDirectory = $this->serverRoot . self::DEFAULT_THEME_PATH;

public function __construct($themeName,
IAppManager $appManager, EnvironmentHelper $environmentHelper
) {
$this->appManager = $appManager;
$this->environmentHelper = $environmentHelper;
if ($themeName === '' && $this->defaultThemeExists()) {
$themeName = 'default';
}
Expand All @@ -57,7 +70,9 @@ public function __construct($themeName, $serverRoot) {
* @return bool
*/
public function defaultThemeExists() {
return is_dir($this->defaultThemeDirectory);
$defaultThemePath = $this->environmentHelper->getServerRoot()
. self::DEFAULT_THEME_PATH;
return \is_dir($defaultThemePath);
}

/**
Expand All @@ -69,6 +84,8 @@ public function getTheme() {

/**
* @param string $themeName
*
* @return void
*/
public function setAppTheme($themeName = '') {
$this->theme = $this->makeTheme($themeName, true);
Expand All @@ -77,46 +94,51 @@ public function setAppTheme($themeName = '') {
/**
* @param string $themeName
* @param bool $appTheme
*
* @return Theme
*/
private function makeTheme($themeName, $appTheme = true) {
$baseDirectory = $this->serverRoot;
$serverRoot = $this->environmentHelper->getServerRoot();
$baseDirectory = $serverRoot;
$directory = '';
$webPath = '';
if ($themeName !== '') {
if ($appTheme) {
$themeDirectory = \OC_App::getAppPath($themeName);
// Use OC server root as a theme base directory if theme is located below it
// Use path to an app root as a theme base directory otherwise
// in any case theme directory is relative to theme base directory
if (strpos($themeDirectory, $this->serverRoot)===0) {
$directory = substr($themeDirectory, strlen($this->serverRoot) + 1);
$themeDirectory = $this->appManager->getAppPath($themeName);
// If theme is located below OC server root
// Theme base directory is OC server root
//
// if theme is located outside OC server root
// Theme base directory is a path to the appsRoot containing
// this theme
//
// In any case theme directory is relative to theme base directory
if (\strpos($themeDirectory, $serverRoot) === 0) {
$directory = \substr($themeDirectory, \strlen($serverRoot) + 1);
} else {
foreach (\OC::$APPSROOTS as $appRoot) {
if (strpos($themeDirectory, $appRoot['path'])===0) {
$baseDirectory = $appRoot['path'];
$directory = substr($themeDirectory, strlen($appRoot['path']) + 1);
$appsRoots = $this->environmentHelper->getAppsRoots();
foreach ($appsRoots as $appsRoot) {
if (\strpos($themeDirectory, $appsRoot['path']) === 0) {
$baseDirectory = $appsRoot['path'];
$directory = \substr(
$themeDirectory,
\strlen($appsRoot['path']) + 1
);
}
}
}

$webPath = \OC_App::getAppWebPath($themeName);
$webPath = $this->appManager->getAppWebPath($themeName);
} else {
$directory = 'themes/' . $themeName;
$webPath = '/themes/' . $themeName;
}
}

if (is_null($this->theme)) {
$this->theme = new Theme($themeName, $directory, $webPath);
} else {
$this->theme->setName($themeName);
$this->theme->setDirectory($directory);
$this->theme->setWebPath($webPath);
}
$this->theme->setBaseDirectory($baseDirectory);
$theme = new Theme($themeName, $directory, $webPath);
$theme->setBaseDirectory($baseDirectory);

return $this->theme;
return $theme;
}

/**
Expand All @@ -131,7 +153,7 @@ public function getAllThemes() {
*/
private function getAllAppThemes() {
$themes = [];
foreach (\OC::$server->getAppManager()->getAllApps() as $app) {
foreach ($this->appManager->getInstalledApps() as $app) {
if (\OC_App::isType($app, 'theme')) {
$themes[$app] = $this->makeTheme($app);
}
Expand All @@ -143,14 +165,15 @@ private function getAllAppThemes() {
* @return Theme[]
*/
private function getAllLegacyThemes() {
$serverRoot = $this->environmentHelper->getServerRoot();
$themes = [];
if (is_dir($this->serverRoot . '/themes')) {
if ($handle = opendir($this->serverRoot . '/themes')) {
while (false !== ($entry = readdir($handle))) {
if (\is_dir($serverRoot . '/themes')) {
if ($handle = \opendir($serverRoot . '/themes')) {
while (($entry = \readdir($handle)) !== false) {
if ($entry === '.' || $entry === '..') {
continue;
}
if (is_dir($this->serverRoot . '/themes/' . $entry)) {
if (\is_dir($serverRoot . '/themes/' . $entry)) {
$themes[$entry] = $this->makeTheme($entry, false);
}
}
Expand Down
86 changes: 80 additions & 6 deletions tests/lib/Theme/ThemeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@
namespace Test\Theme;

use OC\Theme\ThemeService;
use OC\Helper\EnvironmentHelper;
use OCP\App\IAppManager;

class ThemeServiceTest extends \PHPUnit\Framework\TestCase {
/**
* @var IAppManager | \PHPUnit_Framework_MockObject_MockObject
*/
private $appManager;

/**
* @var EnvironmentHelper | \PHPUnit_Framework_MockObject_MockObject
*/
private $environmentHelper;

protected function setUp() {
parent::setUp();
$this->appManager = $this->getMockBuilder(IAppManager::class)
->getMock();
$this->environmentHelper = $this->getMockBuilder(EnvironmentHelper::class)
->getMock();
}

public function testCreatesThemeByGivenName() {
$themeService = new ThemeService('theme-name', \OC::$SERVERROOT);
$themeService = new ThemeService(
'theme-name',
$this->appManager,
$this->environmentHelper
);
$theme = $themeService->getTheme();
$this->assertEquals('theme-name', $theme->getName());
$this->assertEquals('themes/theme-name', $theme->getDirectory());
Expand All @@ -23,7 +46,11 @@ public function testCreatesEmptyThemeIfDefaultDoesNotExist() {
->method('defaultThemeExists')
->willReturn(false);

$themeService->__construct('', \OC::$SERVERROOT);
$themeService->__construct(
'',
$this->appManager,
$this->environmentHelper
);
$theme = $themeService->getTheme();

$this->assertEquals('', $theme->getName());
Expand All @@ -40,17 +67,64 @@ public function testCreatesDefaultThemeIfItExists() {
->method('defaultThemeExists')
->willReturn(true);

$themeService->__construct('', \OC::$SERVERROOT);
$themeService->__construct(
'',
$this->appManager,
$this->environmentHelper
);
$theme = $themeService->getTheme();

$this->assertEquals('default', $theme->getName());
$this->assertEquals('themes/default', $theme->getDirectory());
}

public function testSetAppThemeSetsName() {
$themeService = new ThemeService('', \OC::$SERVERROOT);
$appThemeName = 'some-app-theme';
$this->appManager->expects($this->once())
->method('getAppPath')
->willReturn("/srv/www/apps/$appThemeName");
$this->environmentHelper->expects($this->any())
->method('getServerRoot')
->willReturn("/srv/www");

$themeService = new ThemeService(
'',
$this->appManager,
$this->environmentHelper
);
$this->assertEmpty($themeService->getTheme()->getName());
$themeService->setAppTheme($appThemeName);
$this->assertEquals($appThemeName, $themeService->getTheme()->getName());
}

public function testThemeDirAboveOcRoot() {
$appThemeName = 'some-app-theme';
$this->appManager->expects($this->once())
->method('getAppPath')
->willReturn("/srv/www/apps/$appThemeName");

$this->environmentHelper->expects($this->any())
->method('getServerRoot')
->willReturn("/srv/www/owncloud");
$this->environmentHelper->expects($this->once())
->method('getAppsRoots')
->willReturn(
[
[
'path' => "/srv/www/apps",
'url' => '../apps',
'writable' => true,
]
]
);

$themeService = new ThemeService(
'',
$this->appManager,
$this->environmentHelper
);
$this->assertEmpty($themeService->getTheme()->getName());
$themeService->setAppTheme('some-app-theme');
$this->assertEquals('some-app-theme', $themeService->getTheme()->getName());
$themeService->setAppTheme($appThemeName);
$this->assertEquals($appThemeName, $themeService->getTheme()->getName());
}
}