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] Fix path resolution for appdirs above ocwebroot #30477

Merged
merged 9 commits into from
Feb 15, 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
18 changes: 17 additions & 1 deletion lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,14 @@ public function getAppPath($appId) {
*/
public function getAppWebPath($appId) {
if (($appRoot = $this->findAppInDirectories($appId)) !== false) {
return \OC::$WEBROOT . $appRoot['url'];
$ocWebRoot = $this->getOcWebRoot();
// consider all relative ../ in the app web path as an adjustment
// for oC web root
while (strpos($appRoot['url'], '../') === 0) {
$appRoot['url'] = substr($appRoot['url'], 3);
$ocWebRoot = dirname($ocWebRoot);
}
return $ocWebRoot . '/' . ltrim($appRoot['url'], '/');
}
return false;
}
Expand Down Expand Up @@ -579,6 +586,15 @@ protected function saveAppPath($appId, $appData) {
$this->appDirs[$appId] = $appData;
}

/**
* Get OC web root
* Wrapper for easy mocking
* @return string
*/
protected function getOcWebRoot() {
return \OC::$WEBROOT;
}

/**
* Get apps roots as an array of path and url
* Wrapper for easy mocking
Expand Down
2 changes: 1 addition & 1 deletion lib/private/L10N/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ protected function getActiveLegacyThemeDirectory() {
protected function getActiveAppThemeDirectory() {
$theme = $this->themeService->getTheme();
if ($theme instanceof ITheme && $theme->getDirectory() !== '' ) {
return $this->serverRoot . '/' . $theme->getDirectory();
return $theme->getBaseDirectory(). '/' . $theme->getDirectory();
}
return '';
}
Expand Down
5 changes: 4 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,10 @@ public function __construct($webRoot, \OC\Config $config) {
});

$this->registerService('ThemeService', function ($c) {
return new ThemeService($this->getSystemConfig()->getValue('theme'));
return new ThemeService(
$this->getSystemConfig()->getValue('theme'),
\OC::$SERVERROOT
);
});
$this->registerAlias('OCP\Theme\IThemeService', 'ThemeService');
$this->registerAlias('OCP\IUserSession', 'UserSession');
Expand Down
9 changes: 4 additions & 5 deletions lib/private/Template/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ public function __construct($template, $requestToken, $l10n, $theme, $themeDefau
/**
* @param ITheme $theme
* @param string $app
* @param string $serverRoot
* @param string|false $appDir
* @return string[]
*/
protected function getAppTemplateDirs(ITheme $theme, $app, $serverRoot, $appDir) {
protected function getAppTemplateDirs(ITheme $theme, $app, $appDir) {
$templateDirectories = [];
// Templates dir from the active theme first
if ($theme->getDirectory() !== '') {
$templateDirectories[] = $serverRoot . '/' . $theme->getDirectory() . '/apps/' . $app . '/templates/';
$templateDirectories[] = $theme->getBaseDirectory() . '/' . $theme->getDirectory() . '/apps/' . $app . '/templates/';
}

// Templates dir from the app dir then
Expand Down Expand Up @@ -126,9 +125,9 @@ protected function getSettingsTemplateDirs(ITheme $theme, $serverRoot) {
protected function getTemplateDirs(ITheme $theme, $basePath, $relativePath) {
$directories = [];
if ($theme->getDirectory() !== '') {
$directories[] = $basePath . '/' . $theme->getDirectory() . $relativePath;
$directories[] = $theme->getBaseDirectory() . '/' . $theme->getDirectory() . $relativePath;
}
$directories[] = $basePath . $relativePath;
$directories[] = $basePath . $relativePath;

return $directories;
}
Expand Down
46 changes: 34 additions & 12 deletions lib/private/Template/CSSResourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,53 @@ class CSSResourceLocator extends ResourceLocator {
* @param string $style
*/
public function doFind($style) {
if (strpos($style, '3rdparty') === 0
&& $this->appendOnceIfExist($this->thirdpartyroot, $style.'.css')
|| $this->appendOnceIfExist($this->serverroot, $style.'.css')
|| $this->appendOnceIfExist($this->serverroot, 'core/'.$style.'.css')
$fullStyle = $this->addExtension($style);
if (
$this->appendOnceIfExist($this->serverroot, $fullStyle)
|| $this->appendOnceIfExist($this->serverroot, 'core/' . $fullStyle)
) {
return;
}
$app = substr($style, 0, strpos($style, '/'));
$style = substr($style, strpos($style, '/')+1);
$app = substr($fullStyle, 0, strpos($fullStyle, '/'));
$fullStyle = substr($fullStyle, strpos($fullStyle, '/')+1);

$app_path = \OC_App::getAppPath($app);
$app_path = $this->appManager->getAppPath($app);
if( $app_path === false ) { return; }
$app_url = \OC_App::getAppWebPath($app);
$app_url = $this->appManager->getAppWebPath($app);
$app_url = ($app_url !== false) ? $app_url : null;
$this->appendOnceIfExist($app_path, $style.'.css', $app_url);
$this->appendOnceIfExist($app_path, $fullStyle, $app_url);
}

/**
* @param string $style
*/
public function doFindTheme($style) {
$fullStyle = $this->addExtension($style);
$themeDirectory = $this->theme->getDirectory();
$baseDirectory = $this->theme->getBaseDirectory();
$webRoot = '';
if ($baseDirectory !== $this->serverroot) {
$webRoot = substr($this->theme->getWebPath(), 0, -strlen($themeDirectory));
}

$searchLocations = [
$this->buildPath([$themeDirectory, 'apps', $fullStyle]),
$this->buildPath([$themeDirectory, $fullStyle]),
$this->buildPath([$themeDirectory, 'core', $fullStyle]),
];

$this->appendOnceIfExist($this->serverroot, $themeDirectory . '/apps/' . $style . '.css')
|| $this->appendOnceIfExist($this->serverroot, $themeDirectory . '/' . $style . '.css')
|| $this->appendOnceIfExist($this->serverroot, $themeDirectory . '/core/' . $style . '.css');
foreach ($searchLocations as $location) {
if ($this->appendOnceIfExist($baseDirectory, $location, $webRoot)) {
break;
}
}
}

/**
* @param string $path
* @return string
*/
protected function addExtension($path) {
return $path . '.css';
}
}
49 changes: 29 additions & 20 deletions lib/private/Template/JSResourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,58 @@ class JSResourceLocator extends ResourceLocator {
* @param string $script
*/
public function doFind($script) {
$fullScript = $this->addExtension($script);
$themeDirectory = $this->theme->getDirectory();

if (strpos($script, '3rdparty') === 0
&& $this->appendOnceIfExist($this->thirdpartyroot, $script.'.js')) {
return;
$baseDirectory = $this->theme->getBaseDirectory();
$webRoot = '';
if ($baseDirectory !== $this->serverroot) {
$webRoot = substr($this->theme->getWebPath(), 0, -strlen($themeDirectory));
}

if (strpos($script, '/l10n/') !== false) {
// For language files we try to load them all, so themes can overwrite
// single l10n strings without having to translate all of them.
$found = 0;
$found += $this->appendOnceIfExist($this->serverroot, 'core/'.$script.'.js');
$found += $this->appendOnceIfExist($this->serverroot, $themeDirectory.'/core/'.$script.'.js');
$found += $this->appendOnceIfExist($this->serverroot, $script.'.js');
$found += $this->appendOnceIfExist($this->serverroot, $themeDirectory.'/'.$script.'.js');
$found += $this->appendOnceIfExist($this->serverroot, $themeDirectory.'/apps/'.$script.'.js');
$found += $this->appendOnceIfExist($this->serverroot, 'core/'.$fullScript);
$found += $this->appendOnceIfExist($baseDirectory, $themeDirectory.'/core/'.$fullScript, $webRoot);
$found += $this->appendOnceIfExist($this->serverroot, $fullScript);
$found += $this->appendOnceIfExist($baseDirectory, $themeDirectory.'/'.$fullScript, $webRoot);
$found += $this->appendOnceIfExist($baseDirectory, $themeDirectory.'/apps/'.$fullScript, $webRoot);

if ($found) {
return;
}
} else if ($this->appendOnceIfExist($this->serverroot, $themeDirectory.'/apps/'.$script.'.js')
|| $this->appendOnceIfExist($this->serverroot, $themeDirectory.'/'.$script.'.js')
|| $this->appendOnceIfExist($this->serverroot, $script.'.js')
|| $this->appendOnceIfExist($this->serverroot, $themeDirectory.'/core/'.$script.'.js')
|| $this->appendOnceIfExist($this->serverroot, 'core/'.$script.'.js')
} else if ($this->appendOnceIfExist($baseDirectory, $themeDirectory.'/apps/'.$fullScript, $webRoot)
|| $this->appendOnceIfExist($baseDirectory, $themeDirectory.'/'.$fullScript, $webRoot)
|| $this->appendOnceIfExist($this->serverroot, $fullScript)
|| $this->appendOnceIfExist($baseDirectory, $themeDirectory.'/core/'.$fullScript, $webRoot)
|| $this->appendOnceIfExist($this->serverroot, 'core/'.$fullScript)
) {
return;
}

$app = substr($script, 0, strpos($script, '/'));
$script = substr($script, strpos($script, '/')+1);
$app_path = \OC_App::getAppPath($app);
$app = substr($fullScript, 0, strpos($fullScript, '/'));
$fullScript = substr($fullScript, strpos($fullScript, '/')+1);
$app_path = $this->appManager->getAppPath($app);
if( $app_path === false ) { return; }
$app_url = \OC_App::getAppWebPath($app);
$app_url = $this->appManager->getAppWebPath($app);
$app_url = ($app_url !== false) ? $app_url : null;

// missing translations files fill be ignored
$this->appendOnceIfExist($app_path, $script . '.js', $app_url);
// missing translations files will be ignored
$this->appendOnceIfExist($app_path, $fullScript, $app_url);
}

/**
* @param string $script
*/
public function doFindTheme($script) {
}

/**
* @param string $path
* @return string
*/
protected function addExtension($path) {
return $path . '.js';
}
}
36 changes: 26 additions & 10 deletions lib/private/Template/ResourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace OC\Template;

use OCP\App\IAppManager;
use OCP\ILogger;
use OCP\Theme\ITheme;

abstract class ResourceLocator {
Expand All @@ -38,26 +40,28 @@ abstract class ResourceLocator {

protected $mapping;
protected $serverroot;
protected $thirdpartyroot;
protected $webroot;

protected $resources = [];

/** @var \OCP\ILogger */
/** @var $appManager */
protected $appManager;

/** @var ILogger */
protected $logger;

/**
* @param \OCP\ILogger $logger
* @param ITheme $theme
* @param IAppManager $appManager
* @param ILogger $logger
* @param array $core_map
* @param array $party_map
*/
public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
$this->logger = $logger;
public function __construct(ITheme $theme, IAppManager $appManager, ILogger $logger, $core_map) {
$this->theme = $theme;
$this->mapping = $core_map + $party_map;
$this->logger = $logger;
$this->appManager = $appManager;
$this->mapping = $core_map;
$this->serverroot = key($core_map);
$this->thirdpartyroot = key($party_map);
$this->webroot = $this->mapping[$this->serverroot];
}

Expand All @@ -71,6 +75,12 @@ abstract public function doFind($resource);
*/
abstract public function doFindTheme($resource);

/**
* @param string $path
* @return string
*/
abstract protected function addExtension($path);

/**
* Finds the resources and adds them to the list
*
Expand Down Expand Up @@ -144,8 +154,14 @@ protected function append($root, $file, $webRoot = null, $throw = true) {
* @param string[] $parts path parts to concatenate
* @return string $parts concatenated
*/
private function buildPath($parts){
return join(DIRECTORY_SEPARATOR, $parts);
protected function buildPath($parts){
$trimmedParts = array_map(
function($part){
return rtrim($part, '/');
},
$parts
);
return join(DIRECTORY_SEPARATOR, $trimmedParts);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public function __construct( $renderAs, $appId = '' ) {
*/
static public function findStylesheetFiles($styles) {
$locator = new \OC\Template\CSSResourceLocator(
\OC::$server->getLogger(),
\OC_Util::getTheme(),
[\OC::$SERVERROOT => \OC::$WEBROOT],
\OC::$server->getAppManager(),
\OC::$server->getLogger(),
[\OC::$SERVERROOT => \OC::$WEBROOT]);
$locator->find($styles);
return $locator->getResources();
Expand All @@ -176,9 +176,9 @@ static public function findStylesheetFiles($styles) {
*/
static public function findJavascriptFiles($scripts) {
$locator = new \OC\Template\JSResourceLocator(
\OC::$server->getLogger(),
\OC_Util::getTheme(),
[\OC::$SERVERROOT => \OC::$WEBROOT],
\OC::$server->getAppManager(),
\OC::$server->getLogger(),
[\OC::$SERVERROOT => \OC::$WEBROOT]);
$locator->find($scripts);
return $locator->getResources();
Expand Down
20 changes: 20 additions & 0 deletions lib/private/Theme/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class Theme implements ITheme {
*/
private $name;

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

/**
* @var string
*/
Expand Down Expand Up @@ -56,6 +61,21 @@ public function getName() {
return $this->name;
}

/**
* @param string $baseDirectory
* @return string
*/
public function setBaseDirectory($baseDirectory) {
$this->baseDirectory = $baseDirectory;
}

/**
* @return string
*/
public function getBaseDirectory() {
return $this->baseDirectory;
}

/**
* @return string
*/
Expand Down
Loading