2018-07-03 14:23:22 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-07-03 14:23:22 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2018-07-03 14:23:22 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2018-07-03 14:23:22 +03:00
|
|
|
*
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-07-03 14:23:22 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Template;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Files\AppData\Factory;
|
2018-11-12 13:00:56 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2018-07-03 14:23:22 +03:00
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
2018-07-04 19:50:49 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2018-07-03 14:23:22 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
|
|
|
|
class IconsCacher {
|
|
|
|
|
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
/** @var ISimpleFolder */
|
|
|
|
private $folder;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
2018-11-12 13:00:56 +03:00
|
|
|
/** @var ITimeFactory */
|
|
|
|
protected $timeFactory;
|
|
|
|
|
2018-07-03 14:23:22 +03:00
|
|
|
/** @var string */
|
2018-10-29 14:09:30 +03:00
|
|
|
private $iconVarRE = '/--(icon-[a-zA-Z0-9-]+):\s?url\(["\']?([a-zA-Z0-9-_\~\/\.\?\&\=\:\;\+\,]+)[^;]+;/m';
|
2018-07-03 14:23:22 +03:00
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $fileName = 'icons-vars.css';
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
private $iconList = 'icons-list.template';
|
|
|
|
|
2018-12-07 03:27:49 +03:00
|
|
|
private $cachedCss;
|
|
|
|
private $cachedList;
|
|
|
|
|
2018-07-03 14:23:22 +03:00
|
|
|
/**
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param Factory $appDataFactory
|
|
|
|
* @param IURLGenerator $urlGenerator
|
2018-11-12 13:00:56 +03:00
|
|
|
* @param ITimeFactory $timeFactory
|
2018-10-25 12:57:27 +03:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2018-07-03 14:23:22 +03:00
|
|
|
*/
|
|
|
|
public function __construct(ILogger $logger,
|
|
|
|
Factory $appDataFactory,
|
2018-11-12 13:00:56 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
ITimeFactory $timeFactory) {
|
2018-07-03 14:23:22 +03:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->appData = $appDataFactory->get('css');
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
2018-11-12 13:00:56 +03:00
|
|
|
$this->timeFactory = $timeFactory;
|
2018-07-03 14:23:22 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->folder = $this->appData->getFolder('icons');
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$this->folder = $this->appData->newFolder('icons');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
private function getIconsFromCss(string $css): array {
|
2018-07-03 14:23:22 +03:00
|
|
|
preg_match_all($this->iconVarRE, $css, $matches, PREG_SET_ORDER);
|
|
|
|
$icons = [];
|
|
|
|
foreach ($matches as $icon) {
|
|
|
|
$icons[$icon[1]] = $icon[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $icons;
|
|
|
|
}
|
2018-10-25 12:57:27 +03:00
|
|
|
|
2018-07-03 14:23:22 +03:00
|
|
|
/**
|
|
|
|
* @param string $css
|
2018-10-25 12:57:27 +03:00
|
|
|
* @return string
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2018-07-03 14:23:22 +03:00
|
|
|
*/
|
2018-10-25 12:57:27 +03:00
|
|
|
public function setIconsCss(string $css): string {
|
|
|
|
$cachedFile = $this->getCachedList();
|
2018-07-04 19:50:49 +03:00
|
|
|
if (!$cachedFile) {
|
2018-07-04 11:04:38 +03:00
|
|
|
$currentData = '';
|
2018-10-25 12:57:27 +03:00
|
|
|
$cachedFile = $this->folder->newFile($this->iconList);
|
2018-07-04 19:50:49 +03:00
|
|
|
} else {
|
|
|
|
$currentData = $cachedFile->getContent();
|
2018-07-03 14:23:22 +03:00
|
|
|
}
|
2018-07-04 11:04:38 +03:00
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
$cachedVarsCssFile = $this->getCachedCSS();
|
|
|
|
if (!$cachedVarsCssFile) {
|
|
|
|
$cachedVarsCssFile = $this->folder->newFile($this->fileName);
|
|
|
|
}
|
2018-07-04 11:04:38 +03:00
|
|
|
|
|
|
|
$icons = $this->getIconsFromCss($currentData . $css);
|
2018-07-03 14:23:22 +03:00
|
|
|
|
|
|
|
$data = '';
|
2018-10-25 12:57:27 +03:00
|
|
|
$list = '';
|
2018-07-03 14:23:22 +03:00
|
|
|
foreach ($icons as $icon => $url) {
|
2018-10-25 12:57:27 +03:00
|
|
|
$list .= "--$icon: url('$url');";
|
|
|
|
list($location,$color) = $this->parseUrl($url);
|
2018-10-29 15:06:55 +03:00
|
|
|
$svg = false;
|
2018-11-12 19:31:54 +03:00
|
|
|
if ($location !== '' && \file_exists($location)) {
|
|
|
|
$svg = \file_get_contents($location);
|
2018-10-29 15:06:55 +03:00
|
|
|
}
|
2018-10-24 15:30:30 +03:00
|
|
|
if ($svg === false) {
|
|
|
|
$this->logger->debug('Failed to get icon file ' . $location);
|
|
|
|
$data .= "--$icon: url('$url');";
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-25 12:57:27 +03:00
|
|
|
$encode = base64_encode($this->colorizeSvg($svg, $color));
|
|
|
|
$data .= '--' . $icon . ': url(data:image/svg+xml;base64,' . $encode . ');';
|
|
|
|
}
|
2018-10-24 15:30:30 +03:00
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
if (\strlen($data) > 0 && \strlen($list) > 0) {
|
|
|
|
$data = ":root {\n$data\n}";
|
|
|
|
$cachedVarsCssFile->putContent($data);
|
|
|
|
$list = ":root {\n$list\n}";
|
|
|
|
$cachedFile->putContent($list);
|
2018-12-07 03:27:49 +03:00
|
|
|
$this->cachedList = null;
|
|
|
|
$this->cachedCss = null;
|
2018-07-03 14:23:22 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
return preg_replace($this->iconVarRE, '', $css);
|
|
|
|
}
|
2018-07-03 14:23:22 +03:00
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
/**
|
|
|
|
* @param $url
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function parseUrl($url): array {
|
|
|
|
$location = '';
|
|
|
|
$color = '';
|
|
|
|
$base = $this->getRoutePrefix() . '/svg/';
|
|
|
|
$cleanUrl = \substr($url, \strlen($base));
|
|
|
|
if (\strpos($url, $base . 'core') === 0) {
|
2018-10-29 14:09:30 +03:00
|
|
|
$cleanUrl = \substr($cleanUrl, \strlen('core'));
|
|
|
|
if (\preg_match('/\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
|
|
|
|
list(,$cleanUrl,$color) = $matches;
|
|
|
|
$location = \OC::$SERVERROOT . '/core/img/' . $cleanUrl . '.svg';
|
|
|
|
}
|
2018-10-25 12:57:27 +03:00
|
|
|
} elseif (\strpos($url, $base) === 0) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (\preg_match('/([A-z0-9\_\-]+)\/([a-zA-Z0-9-_\~\/\.\=\:\;\+\,]+)\?color=([0-9a-fA-F]{3,6})/', $cleanUrl, $matches)) {
|
2018-10-29 14:09:30 +03:00
|
|
|
list(,$app,$cleanUrl, $color) = $matches;
|
|
|
|
$location = \OC_App::getAppPath($app) . '/img/' . $cleanUrl . '.svg';
|
|
|
|
if ($app === 'settings') {
|
|
|
|
$location = \OC::$SERVERROOT . '/settings/img/' . $cleanUrl . '.svg';
|
|
|
|
}
|
2018-10-25 12:57:27 +03:00
|
|
|
}
|
2018-07-03 14:23:22 +03:00
|
|
|
}
|
2018-10-25 12:57:27 +03:00
|
|
|
return [
|
|
|
|
$location,
|
|
|
|
$color
|
|
|
|
];
|
|
|
|
}
|
2018-07-03 14:23:22 +03:00
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
/**
|
|
|
|
* @param $svg
|
|
|
|
* @param $color
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function colorizeSvg($svg, $color): string {
|
2019-06-20 16:23:06 +03:00
|
|
|
if (!preg_match('/^[0-9a-f]{3,6}$/i', $color)) {
|
|
|
|
// Prevent not-sane colors from being written into the SVG
|
|
|
|
$color = '000';
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
// add fill (fill is not present on black elements)
|
2019-01-17 16:24:07 +03:00
|
|
|
$fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;,])+)\/>/mi';
|
2018-10-25 12:57:27 +03:00
|
|
|
$svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
|
|
|
|
|
|
|
|
// replace any fill or stroke colors
|
|
|
|
$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
|
|
|
|
$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
|
|
|
|
return $svg;
|
2018-07-03 14:23:22 +03:00
|
|
|
}
|
|
|
|
|
2018-10-24 15:30:30 +03:00
|
|
|
private function getRoutePrefix() {
|
|
|
|
$frontControllerActive = (\OC::$server->getConfig()->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
|
|
|
|
$prefix = \OC::$WEBROOT . '/index.php';
|
|
|
|
if ($frontControllerActive) {
|
|
|
|
$prefix = \OC::$WEBROOT;
|
|
|
|
}
|
|
|
|
return $prefix;
|
|
|
|
}
|
|
|
|
|
2018-07-03 14:23:22 +03:00
|
|
|
/**
|
|
|
|
* Get icons css file
|
|
|
|
* @return ISimpleFile|boolean
|
|
|
|
*/
|
|
|
|
public function getCachedCSS() {
|
|
|
|
try {
|
2018-12-07 03:27:49 +03:00
|
|
|
if (!$this->cachedCss) {
|
|
|
|
$this->cachedCss = $this->folder->getFile($this->fileName);
|
|
|
|
}
|
|
|
|
return $this->cachedCss;
|
2018-07-03 14:23:22 +03:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:57:27 +03:00
|
|
|
/**
|
|
|
|
* Get icon-vars list template
|
|
|
|
* @return ISimpleFile|boolean
|
|
|
|
*/
|
|
|
|
public function getCachedList() {
|
|
|
|
try {
|
2018-12-07 03:27:49 +03:00
|
|
|
if (!$this->cachedList) {
|
|
|
|
$this->cachedList = $this->folder->getFile($this->iconList);
|
|
|
|
}
|
|
|
|
return $this->cachedList;
|
2018-10-25 12:57:27 +03:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 17:09:07 +03:00
|
|
|
/**
|
|
|
|
* Add the icons cache css into the header
|
|
|
|
*/
|
2018-07-03 14:23:22 +03:00
|
|
|
public function injectCss() {
|
2018-11-12 13:00:56 +03:00
|
|
|
$mtime = $this->timeFactory->getTime();
|
|
|
|
$file = $this->getCachedList();
|
|
|
|
if ($file) {
|
|
|
|
$mtime = $file->getMTime();
|
|
|
|
}
|
2018-07-03 14:23:22 +03:00
|
|
|
// Only inject once
|
|
|
|
foreach (\OC_Util::$headers as $header) {
|
2018-07-04 11:04:38 +03:00
|
|
|
if (
|
|
|
|
array_key_exists('attributes', $header) &&
|
|
|
|
array_key_exists('href', $header['attributes']) &&
|
|
|
|
strpos($header['attributes']['href'], $this->fileName) !== false) {
|
2018-07-03 14:23:22 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-11-12 13:00:56 +03:00
|
|
|
$linkToCSS = $this->urlGenerator->linkToRoute('core.Css.getCss', ['appName' => 'icons', 'fileName' => $this->fileName, 'v' => $mtime]);
|
2018-07-04 12:19:45 +03:00
|
|
|
\OC_Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS], null, true);
|
2018-07-03 14:23:22 +03:00
|
|
|
}
|
2018-11-12 13:00:56 +03:00
|
|
|
}
|