2013-03-16 22:02:51 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author tux-rampage <tux-rampage@users.noreply.github.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @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,
|
2019-12-03 18:57:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
2013-03-16 22:02:51 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-03-16 22:02:51 +00:00
|
|
|
namespace OC\Template;
|
|
|
|
|
|
|
|
abstract class ResourceLocator {
|
|
|
|
protected $theme;
|
|
|
|
|
|
|
|
protected $mapping;
|
|
|
|
protected $serverroot;
|
|
|
|
protected $thirdpartyroot;
|
|
|
|
protected $webroot;
|
|
|
|
|
|
|
|
protected $resources = array();
|
|
|
|
|
2015-03-04 12:24:24 +00:00
|
|
|
/** @var \OCP\ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
2014-02-06 15:30:58 +00:00
|
|
|
/**
|
2015-03-04 12:24:24 +00:00
|
|
|
* @param \OCP\ILogger $logger
|
2014-02-06 15:30:58 +00:00
|
|
|
* @param string $theme
|
2015-03-04 12:24:24 +00:00
|
|
|
* @param array $core_map
|
|
|
|
* @param array $party_map
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2015-03-04 12:24:24 +00:00
|
|
|
public function __construct(\OCP\ILogger $logger, $theme, $core_map, $party_map) {
|
|
|
|
$this->logger = $logger;
|
2013-03-16 22:02:51 +00:00
|
|
|
$this->theme = $theme;
|
|
|
|
$this->mapping = $core_map + $party_map;
|
|
|
|
$this->serverroot = key($core_map);
|
|
|
|
$this->thirdpartyroot = key($party_map);
|
|
|
|
$this->webroot = $this->mapping[$this->serverroot];
|
|
|
|
}
|
|
|
|
|
2015-03-04 12:24:24 +00:00
|
|
|
/**
|
|
|
|
* @param string $resource
|
|
|
|
*/
|
|
|
|
abstract public function doFind($resource);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $resource
|
|
|
|
*/
|
|
|
|
abstract public function doFindTheme($resource);
|
2013-03-16 22:02:51 +00:00
|
|
|
|
2015-03-04 12:24:24 +00:00
|
|
|
/**
|
|
|
|
* Finds the resources and adds them to the list
|
|
|
|
*
|
|
|
|
* @param array $resources
|
|
|
|
*/
|
|
|
|
public function find($resources) {
|
|
|
|
foreach ($resources as $resource) {
|
|
|
|
try {
|
2013-03-16 22:02:51 +00:00
|
|
|
$this->doFind($resource);
|
2015-03-04 12:24:24 +00:00
|
|
|
} catch (ResourceNotFoundException $e) {
|
|
|
|
$resourceApp = substr($resource, 0, strpos($resource, '/'));
|
2017-02-03 12:09:33 +00:00
|
|
|
$this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
|
2013-03-16 22:02:51 +00:00
|
|
|
}
|
2015-03-04 12:24:24 +00:00
|
|
|
}
|
|
|
|
if (!empty($this->theme)) {
|
|
|
|
foreach ($resources as $resource) {
|
|
|
|
try {
|
2013-03-16 22:02:51 +00:00
|
|
|
$this->doFindTheme($resource);
|
2015-03-04 12:24:24 +00:00
|
|
|
} catch (ResourceNotFoundException $e) {
|
|
|
|
$resourceApp = substr($resource, 0, strpos($resource, '/'));
|
2017-03-02 09:50:54 +00:00
|
|
|
$this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
|
2013-03-16 22:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 12:24:24 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* append the $file resource if exist at $root
|
2015-03-04 12:24:24 +00:00
|
|
|
*
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $root path to check
|
|
|
|
* @param string $file the filename
|
2015-03-04 12:24:24 +00:00
|
|
|
* @param string|null $webRoot base for path, default map $root to $webRoot
|
|
|
|
* @return bool True if the resource was found, false otherwise
|
2014-02-06 15:30:58 +00:00
|
|
|
*/
|
2015-03-04 12:24:24 +00:00
|
|
|
protected function appendIfExist($root, $file, $webRoot = null) {
|
2013-03-16 22:02:51 +00:00
|
|
|
if (is_file($root.'/'.$file)) {
|
2015-03-04 12:24:24 +00:00
|
|
|
$this->append($root, $file, $webRoot, false);
|
2013-03-16 22:02:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-06 19:28:45 +00:00
|
|
|
/**
|
|
|
|
* Attempt to find the webRoot
|
|
|
|
*
|
|
|
|
* traverse the potential web roots upwards in the path
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* - root: /srv/www/apps/myapp
|
|
|
|
* - available mappings: ['/srv/www']
|
|
|
|
*
|
|
|
|
* First we check if a mapping for /srv/www/apps/myapp is available,
|
|
|
|
* then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
|
|
|
|
* valid web root
|
|
|
|
*
|
|
|
|
* @param string $root
|
|
|
|
* @return string|null The web root or null on failure
|
|
|
|
*/
|
|
|
|
protected function findWebRoot($root) {
|
|
|
|
$webRoot = null;
|
|
|
|
$tmpRoot = $root;
|
|
|
|
|
|
|
|
while ($webRoot === null) {
|
|
|
|
if (isset($this->mapping[$tmpRoot])) {
|
|
|
|
$webRoot = $this->mapping[$tmpRoot];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($tmpRoot === '/') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmpRoot = dirname($tmpRoot);
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:08:30 +00:00
|
|
|
if ($webRoot === null) {
|
2017-09-06 19:28:45 +00:00
|
|
|
$realpath = realpath($root);
|
|
|
|
|
|
|
|
if ($realpath && ($realpath !== $root)) {
|
|
|
|
return $this->findWebRoot($realpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $webRoot;
|
|
|
|
}
|
|
|
|
|
2015-03-04 12:24:24 +00:00
|
|
|
/**
|
|
|
|
* append the $file resource at $root
|
|
|
|
*
|
|
|
|
* @param string $root path to check
|
|
|
|
* @param string $file the filename
|
|
|
|
* @param string|null $webRoot base for path, default map $root to $webRoot
|
|
|
|
* @param bool $throw Throw an exception, when the route does not exist
|
|
|
|
* @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
|
|
|
|
*/
|
|
|
|
protected function append($root, $file, $webRoot = null, $throw = true) {
|
2017-05-19 09:28:40 +00:00
|
|
|
|
|
|
|
if (!is_string($root)) {
|
|
|
|
if ($throw) {
|
|
|
|
throw new ResourceNotFoundException($file, $webRoot);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-22 05:17:35 +00:00
|
|
|
if (!$webRoot) {
|
2017-09-06 19:28:45 +00:00
|
|
|
$webRoot = $this->findWebRoot($root);
|
|
|
|
|
2017-09-19 15:08:30 +00:00
|
|
|
if ($webRoot === null) {
|
2017-09-06 19:28:45 +00:00
|
|
|
$webRoot = '';
|
|
|
|
$this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
|
|
|
|
'app' => 'lib',
|
|
|
|
'root' => $root,
|
|
|
|
'file' => $file,
|
|
|
|
'webRoot' => $webRoot,
|
|
|
|
'throw' => $throw ? 'true' : 'false'
|
|
|
|
]);
|
|
|
|
}
|
2015-03-04 12:24:24 +00:00
|
|
|
}
|
|
|
|
$this->resources[] = array($root, $webRoot, $file);
|
|
|
|
|
|
|
|
if ($throw && !is_file($root . '/' . $file)) {
|
|
|
|
throw new ResourceNotFoundException($file, $webRoot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of all resources that should be loaded
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-03-16 22:02:51 +00:00
|
|
|
public function getResources() {
|
|
|
|
return $this->resources;
|
|
|
|
}
|
|
|
|
}
|