summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2018-07-19 23:54:29 +0200
committerGitHub <noreply@github.com>2018-07-19 23:54:29 +0200
commit4751f1e7f766419fd013e5d6ddac4988e9158f5c (patch)
treeaece73c6e050c5adfd8a68f29f3c2eb02d51d961 /lib/private
parent8032e362ec27111f5813f70b9604e7d07c6bf2ab (diff)
parent9d94cc16976970c84d5a88e090ea55be273be712 (diff)
downloadnextcloud-server-4751f1e7f766419fd013e5d6ddac4988e9158f5c.tar.gz
nextcloud-server-4751f1e7f766419fd013e5d6ddac4988e9158f5c.zip
Merge pull request #9984 from nextcloud/inverted-svg-api
Svg color api
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/Server.php4
-rw-r--r--lib/private/Settings/Manager.php2
-rw-r--r--lib/private/Template/IconsCacher.php147
-rw-r--r--lib/private/Template/SCSSCacher.php118
-rw-r--r--lib/private/legacy/util.php11
5 files changed, 241 insertions, 41 deletions
diff --git a/lib/private/Server.php b/lib/private/Server.php
index c9f8001631e..0f406ebe9b7 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -116,6 +116,7 @@ use OC\Share20\ProviderFactory;
use OC\Share20\ShareHelper;
use OC\SystemTag\ManagerFactory as SystemTagManagerFactory;
use OC\Tagging\TagMapper;
+use OC\Template\IconsCacher;
use OC\Template\JSCombiner;
use OC\Template\SCSSCacher;
use OCA\Theming\ImageManager;
@@ -963,7 +964,8 @@ class Server extends ServerContainer implements IServerContainer {
$c->getConfig(),
$c->getThemingDefaults(),
\OC::$SERVERROOT,
- $this->getMemCacheFactory()
+ $this->getMemCacheFactory(),
+ $c->query(IconsCacher::class)
);
});
$this->registerService(JSCombiner::class, function (Server $c) {
diff --git a/lib/private/Settings/Manager.php b/lib/private/Settings/Manager.php
index 82cb223bb9f..e9f2a6f5976 100644
--- a/lib/private/Settings/Manager.php
+++ b/lib/private/Settings/Manager.php
@@ -231,7 +231,7 @@ class Manager implements IManager {
5 => [new Section('sharing', $this->l->t('Sharing'), 0, $this->url->imagePath('core', 'actions/share.svg'))],
10 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
45 => [new Section('encryption', $this->l->t('Encryption'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
- 50 => [new Section('groupware', $this->l->t('Groupware'), 0, $this->url->imagePath('core', 'places/contacts-dark.svg'))],
+ 50 => [new Section('groupware', $this->l->t('Groupware'), 0, $this->url->imagePath('core', 'places/contacts.svg'))],
98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
];
diff --git a/lib/private/Template/IconsCacher.php b/lib/private/Template/IconsCacher.php
new file mode 100644
index 00000000000..c262d26654f
--- /dev/null
+++ b/lib/private/Template/IconsCacher.php
@@ -0,0 +1,147 @@
+<?php
+declare (strict_types = 1);
+/**
+ * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
+ *
+ * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Template;
+
+use OCP\Files\IAppData;
+use OCP\Files\NotFoundException;
+use OCP\Files\SimpleFS\ISimpleFolder;
+use OCP\Files\SimpleFS\ISimpleFile;
+use OCP\ILogger;
+use OCP\IURLGenerator;
+use OC\Files\AppData\Factory;
+
+class IconsCacher {
+
+ /** @var ILogger */
+ protected $logger;
+
+ /** @var IAppData */
+ protected $appData;
+
+ /** @var ISimpleFolder */
+ private $folder;
+
+ /** @var IURLGenerator */
+ protected $urlGenerator;
+
+ /** @var string */
+ private $iconVarRE = '/--(icon-[a-z0-9-]+): url\(["\']([a-z0-9-_\~\/\?\&\=\.]+)[^;]+;/m';
+
+ /** @var string */
+ private $fileName = 'icons-vars.css';
+
+ /**
+ * @param ILogger $logger
+ * @param Factory $appDataFactory
+ * @param IURLGenerator $urlGenerator
+ */
+ public function __construct(ILogger $logger,
+ Factory $appDataFactory,
+ IURLGenerator $urlGenerator) {
+ $this->logger = $logger;
+ $this->appData = $appDataFactory->get('css');
+ $this->urlGenerator = $urlGenerator;
+
+ try {
+ $this->folder = $this->appData->getFolder('icons');
+ } catch (NotFoundException $e) {
+ $this->folder = $this->appData->newFolder('icons');
+ }
+ }
+
+ private function getIconsFromCss(string $css): array{
+ preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
+ $icons = [];
+ foreach ($matches as $icon) {
+ $icons[$icon[1]] = $icon[2];
+ }
+
+ return $icons;
+ }
+ /**
+ * Parse and cache css
+ *
+ * @param string $css
+ */
+ public function setIconsCss(string $css) {
+
+ $cachedFile = $this->getCachedCSS();
+ if (!$cachedFile) {
+ $currentData = '';
+ } else {
+ $currentData = $cachedFile->getContent();
+ }
+
+ // remove :root
+ $currentData = str_replace([':root {', '}'], '', $currentData);
+
+ $icons = $this->getIconsFromCss($currentData . $css);
+
+ $data = '';
+ foreach ($icons as $icon => $url) {
+ $data .= "--$icon: url('$url?v=1');";
+ }
+
+ if (strlen($data) > 0) {
+ if (!$cachedFile) {
+ $cachedFile = $this->folder->newFile($this->fileName);
+ }
+
+ $data = ":root {
+ $data
+ }";
+ $cachedFile->putContent($data);
+ }
+
+ return preg_replace($this->iconVarRE, '', $css);
+ }
+
+ /**
+ * Get icons css file
+ * @return ISimpleFile|boolean
+ */
+ public function getCachedCSS() {
+ try {
+ return $this->folder->getFile($this->fileName);
+ } catch (NotFoundException $e) {
+ return false;
+ }
+ }
+
+ public function injectCss() {
+ // Only inject once
+ foreach (\OC_Util::$headers as $header) {
+ if (
+ array_key_exists('attributes', $header) &&
+ array_key_exists('href', $header['attributes']) &&
+ strpos($header['attributes']['href'], $this->fileName) !== false) {
+ return;
+ }
+ }
+ $linkToCSS = substr($this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName]), strlen(\OC::$WEBROOT));
+ \OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true);
+ }
+
+} \ No newline at end of file
diff --git a/lib/private/Template/SCSSCacher.php b/lib/private/Template/SCSSCacher.php
index 6e25e1fa5de..9dcedb94daf 100644
--- a/lib/private/Template/SCSSCacher.php
+++ b/lib/private/Template/SCSSCacher.php
@@ -32,7 +32,6 @@ use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\Exception\ParserException;
use Leafo\ScssPhp\Formatter\Crunched;
use Leafo\ScssPhp\Formatter\Expanded;
-use OC\Files\AppData\Factory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
@@ -43,6 +42,8 @@ use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IURLGenerator;
+use OC\Files\AppData\Factory;
+use OC\Template\IconsCacher;
class SCSSCacher {
@@ -73,6 +74,9 @@ class SCSSCacher {
/** @var ICacheFactory */
private $cacheFactory;
+ /** @var IconsCacher */
+ private $iconsCacher;
+
/**
* @param ILogger $logger
* @param Factory $appDataFactory
@@ -81,6 +85,7 @@ class SCSSCacher {
* @param \OC_Defaults $defaults
* @param string $serverRoot
* @param ICacheFactory $cacheFactory
+ * @param IconsCacher $iconsCacher
*/
public function __construct(ILogger $logger,
Factory $appDataFactory,
@@ -88,15 +93,17 @@ class SCSSCacher {
IConfig $config,
\OC_Defaults $defaults,
$serverRoot,
- ICacheFactory $cacheFactory) {
- $this->logger = $logger;
- $this->appData = $appDataFactory->get('css');
+ ICacheFactory $cacheFactory,
+ IconsCacher $iconsCacher) {
+ $this->logger = $logger;
+ $this->appData = $appDataFactory->get('css');
$this->urlGenerator = $urlGenerator;
- $this->config = $config;
- $this->defaults = $defaults;
- $this->serverRoot = $serverRoot;
+ $this->config = $config;
+ $this->defaults = $defaults;
+ $this->serverRoot = $serverRoot;
$this->cacheFactory = $cacheFactory;
- $this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl()));
+ $this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl()));
+ $this->iconsCacher = $iconsCacher;
}
/**
@@ -112,23 +119,34 @@ class SCSSCacher {
$path = explode('/', $root . '/' . $file);
$fileNameSCSS = array_pop($path);
- $fileNameCSS = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)), $app);
+ $fileNameCSS = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)), $app);
- $path = implode('/', $path);
+ $path = implode('/', $path);
$webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT);
try {
$folder = $this->appData->getFolder($app);
- } catch(NotFoundException $e) {
+ } catch (NotFoundException $e) {
// creating css appdata folder
$folder = $this->appData->newFolder($app);
}
-
- if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) {
+ if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) {
+ // Inject icons vars css if any
+ if ($this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) {
+ $this->iconsCacher->injectCss();
+ }
return true;
}
- return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
+
+ $cached = $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
+
+ // Inject icons vars css if any
+ if ($this->iconsCacher->getCachedCSS() && $this->iconsCacher->getCachedCSS()->getSize() > 0) {
+ $this->iconsCacher->injectCss();
+ }
+
+ return $cached;
}
/**
@@ -137,8 +155,9 @@ class SCSSCacher {
* @return ISimpleFile
*/
public function getCachedCSS(string $appName, string $fileName): ISimpleFile {
- $folder = $this->appData->getFolder($appName);
+ $folder = $this->appData->getFolder($appName);
$cachedFileName = $this->prependVersionPrefix($this->prependBaseurlPrefix($fileName), $appName);
+
return $folder->getFile($cachedFileName);
}
@@ -153,24 +172,26 @@ class SCSSCacher {
$cachedFile = $folder->getFile($fileNameCSS);
if ($cachedFile->getSize() > 0) {
$depFileName = $fileNameCSS . '.deps';
- $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName);
+ $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName);
if ($deps === null) {
$depFile = $folder->getFile($depFileName);
- $deps = $depFile->getContent();
+ $deps = $depFile->getContent();
//Set to memcache for next run
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
}
$deps = json_decode($deps, true);
- foreach ((array)$deps as $file=>$mtime) {
+ foreach ((array) $deps as $file => $mtime) {
if (!file_exists($file) || filemtime($file) > $mtime) {
return false;
}
}
+
return true;
}
+
return false;
- } catch(NotFoundException $e) {
+ } catch (NotFoundException $e) {
return false;
}
}
@@ -181,11 +202,13 @@ class SCSSCacher {
*/
private function variablesChanged(): bool {
$injectedVariables = $this->getInjectedVariables();
- if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) {
+ if ($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) {
$this->resetCache();
$this->config->setAppValue('core', 'scss.variables', md5($injectedVariables));
+
return true;
}
+
return false;
}
@@ -204,11 +227,12 @@ class SCSSCacher {
$scss = new Compiler();
$scss->setImportPaths([
$path,
- $this->serverRoot . '/core/css/',
+ $this->serverRoot . '/core/css/'
]);
+
// Continue after throw
$scss->setIgnoreErrors(true);
- if($this->config->getSystemValue('debug')) {
+ if ($this->config->getSystemValue('debug')) {
// Debug mode
$scss->setFormatter(Expanded::class);
$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
@@ -219,7 +243,7 @@ class SCSSCacher {
try {
$cachedfile = $folder->getFile($fileNameCSS);
- } catch(NotFoundException $e) {
+ } catch (NotFoundException $e) {
$cachedfile = $folder->newFile($fileNameCSS);
}
@@ -233,14 +257,20 @@ class SCSSCacher {
// Compile
try {
$compiledScss = $scss->compile(
+ '$webroot: \'' . $this->getRoutePrefix() . '\';' .
'@import "variables.scss";' .
+ '@import "functions.scss";' .
$this->getInjectedVariables() .
- '@import "'.$fileNameSCSS.'";');
- } catch(ParserException $e) {
+ '@import "' . $fileNameSCSS . '";');
+ } catch (ParserException $e) {
$this->logger->error($e, ['app' => 'core']);
+
return false;
}
+ // Parse Icons and create related css variables
+ $compiledScss = $this->iconsCacher->setIconsCss($compiledScss);
+
// Gzip file
try {
$gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz
@@ -255,10 +285,12 @@ class SCSSCacher {
$depFile->putContent($deps);
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
$gzipFile->putContent(gzencode($data, 9));
- $this->logger->debug('SCSSCacher: '.$webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
+ $this->logger->debug('SCSSCacher: ' . $webDir . '/' . $fileNameSCSS . ' compiled and successfully cached', ['app' => 'core']);
+
return true;
- } catch(NotPermittedException $e) {
+ } catch (NotPermittedException $e) {
$this->logger->error('SCSSCacher: unable to cache: ' . $fileNameSCSS);
+
return false;
}
}
@@ -275,7 +307,7 @@ class SCSSCacher {
foreach ($folder->getDirectoryListing() as $file) {
try {
$file->delete();
- } catch(NotPermittedException $e) {
+ } catch (NotPermittedException $e) {
$this->logger->logException($e, ['message' => 'SCSSCacher: unable to delete file: ' . $file->getName()]);
}
}
@@ -313,8 +345,9 @@ class SCSSCacher {
* @return string
*/
private function rebaseUrls(string $css, string $webDir): string {
- $re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
- $subst = 'url(\''.$webDir.'/$1\')';
+ $re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x';
+ $subst = 'url(\'' . $webDir . '/$1\')';
+
return preg_replace($re, $subst, $css);
}
@@ -326,8 +359,8 @@ class SCSSCacher {
*/
public function getCachedSCSS(string $appName, string $fileName): string {
$tmpfileLoc = explode('/', $fileName);
- $fileName = array_pop($tmpfileLoc);
- $fileName = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)), $appName);
+ $fileName = array_pop($tmpfileLoc);
+ $fileName = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)), $appName);
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1);
}
@@ -338,8 +371,16 @@ class SCSSCacher {
* @return string
*/
private function prependBaseurlPrefix(string $cssFile): string {
- $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
- return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 4) . '-' . $cssFile;
+ return substr(md5($this->urlGenerator->getBaseUrl() . $this->getRoutePrefix()), 0, 4) . '-' . $cssFile;
+ }
+
+ private function getRoutePrefix() {
+ $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
+ $prefix = \OC::$WEBROOT . '/index.php';
+ if ($frontControllerActive) {
+ $prefix = \OC::$WEBROOT;
+ }
+ return $prefix;
}
/**
@@ -354,6 +395,7 @@ class SCSSCacher {
return substr(md5($appVersion), 0, 4) . '-' . $cssFile;
}
$coreVersion = \OC_Util::getVersionString();
+
return substr(md5($coreVersion), 0, 4) . '-' . $cssFile;
}
@@ -367,12 +409,14 @@ class SCSSCacher {
*/
private function getWebDir(string $path, string $appName, string $serverRoot, string $webRoot): string {
// Detect if path is within server root AND if path is within an app path
- if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) {
+ if (strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) {
// Get the file path within the app directory
$appDirectoryPath = explode($appName, $path)[1];
// Remove the webroot
- return str_replace($webRoot, '', $appWebPath.$appDirectoryPath);
+
+ return str_replace($webRoot, '', $appWebPath . $appDirectoryPath);
}
- return $webRoot.substr($path, strlen($serverRoot));
+
+ return $webRoot . substr($path, strlen($serverRoot));
}
}
diff --git a/lib/private/legacy/util.php b/lib/private/legacy/util.php
index ab595d885cb..5e9a46d44a9 100644
--- a/lib/private/legacy/util.php
+++ b/lib/private/legacy/util.php
@@ -687,13 +687,20 @@ class OC_Util {
* @param string $tag tag name of the element
* @param array $attributes array of attributes for the element
* @param string $text the text content for the element
+ * @param bool $prepend prepend the header to the beginning of the list
*/
- public static function addHeader($tag, $attributes, $text=null) {
- self::$headers[] = array(
+ public static function addHeader($tag, $attributes, $text = null, $prepend = false) {
+ $header = array(
'tag' => $tag,
'attributes' => $attributes,
'text' => $text
);
+ if ($prepend === true) {
+ array_unshift (self::$headers, $header);
+
+ } else {
+ self::$headers[] = $header;
+ }
}
/**