2016-11-08 15:36:35 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, John Molakvoæ (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 Leafo\ScssPhp\Compiler;
|
|
|
|
use Leafo\ScssPhp\Exception\ParserException;
|
2016-11-30 18:30:04 +03:00
|
|
|
use Leafo\ScssPhp\Formatter\Crunched;
|
|
|
|
use Leafo\ScssPhp\Formatter\Expanded;
|
2017-02-17 18:42:07 +03:00
|
|
|
use OC\Files\AppData\Factory;
|
2016-11-30 18:30:04 +03:00
|
|
|
use OCP\Files\IAppData;
|
2016-11-09 13:18:43 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2017-03-20 14:31:29 +03:00
|
|
|
use OCP\Files\NotPermittedException;
|
2017-02-17 18:42:07 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2016-11-30 23:51:09 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2017-03-21 23:53:20 +03:00
|
|
|
use OCP\ICache;
|
2017-03-06 01:28:59 +03:00
|
|
|
use OCP\IConfig;
|
2016-11-30 18:30:04 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IURLGenerator;
|
2016-11-08 15:36:35 +03:00
|
|
|
|
|
|
|
class SCSSCacher {
|
|
|
|
|
2016-11-30 18:30:04 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
2017-03-06 01:28:59 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
2016-11-08 15:36:35 +03:00
|
|
|
|
2017-03-10 22:00:34 +03:00
|
|
|
/** @var string */
|
|
|
|
protected $serverRoot;
|
|
|
|
|
2017-03-21 23:53:20 +03:00
|
|
|
/** @var ICache */
|
|
|
|
protected $depsCache;
|
|
|
|
|
2016-11-08 15:36:35 +03:00
|
|
|
/**
|
2016-11-30 18:30:04 +03:00
|
|
|
* @param ILogger $logger
|
2017-02-17 18:42:07 +03:00
|
|
|
* @param Factory $appDataFactory
|
2016-11-30 18:30:04 +03:00
|
|
|
* @param IURLGenerator $urlGenerator
|
2017-03-21 23:53:20 +03:00
|
|
|
* @param IConfig $config
|
2017-02-17 18:42:07 +03:00
|
|
|
* @param \OC_Defaults $defaults
|
2017-03-10 22:00:34 +03:00
|
|
|
* @param string $serverRoot
|
2017-03-21 23:53:20 +03:00
|
|
|
* @param ICache $depsCache
|
2016-11-08 15:36:35 +03:00
|
|
|
*/
|
2017-03-10 22:00:34 +03:00
|
|
|
public function __construct(ILogger $logger,
|
2017-02-17 18:42:07 +03:00
|
|
|
Factory $appDataFactory,
|
2017-03-10 22:00:34 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
2017-03-06 01:28:59 +03:00
|
|
|
IConfig $config,
|
2017-02-17 18:42:07 +03:00
|
|
|
\OC_Defaults $defaults,
|
2017-03-21 23:53:20 +03:00
|
|
|
$serverRoot,
|
|
|
|
ICache $depsCache) {
|
2016-11-08 15:36:35 +03:00
|
|
|
$this->logger = $logger;
|
2017-02-17 18:42:07 +03:00
|
|
|
$this->appData = $appDataFactory->get('css');
|
2016-11-10 18:16:33 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2017-03-06 01:28:59 +03:00
|
|
|
$this->config = $config;
|
2017-02-17 18:42:07 +03:00
|
|
|
$this->defaults = $defaults;
|
2017-03-10 22:00:34 +03:00
|
|
|
$this->serverRoot = $serverRoot;
|
2017-03-21 23:53:20 +03:00
|
|
|
$this->depsCache = $depsCache;
|
2016-11-30 23:51:09 +03:00
|
|
|
}
|
2016-11-10 18:16:33 +03:00
|
|
|
|
2016-11-30 23:51:09 +03:00
|
|
|
/**
|
|
|
|
* Process the caching process if needed
|
|
|
|
* @param string $root Root path to the nextcloud installation
|
|
|
|
* @param string $file
|
2017-01-22 21:33:45 +03:00
|
|
|
* @param string $app The app name
|
2016-11-30 23:51:09 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-01-22 21:33:45 +03:00
|
|
|
public function process($root, $file, $app) {
|
2016-11-30 23:51:09 +03:00
|
|
|
$path = explode('/', $root . '/' . $file);
|
2016-11-08 15:36:35 +03:00
|
|
|
|
2016-11-30 23:51:09 +03:00
|
|
|
$fileNameSCSS = array_pop($path);
|
2017-06-15 23:14:39 +03:00
|
|
|
$fileNameCSS = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS));
|
2016-11-09 15:39:08 +03:00
|
|
|
|
2016-11-30 23:51:09 +03:00
|
|
|
$path = implode('/', $path);
|
2016-11-08 15:36:35 +03:00
|
|
|
|
2017-03-10 22:00:34 +03:00
|
|
|
$webDir = substr($path, strlen($this->serverRoot)+1);
|
2016-11-09 13:18:43 +03:00
|
|
|
|
|
|
|
try {
|
2017-01-22 21:33:45 +03:00
|
|
|
$folder = $this->appData->getFolder($app);
|
2016-11-09 13:18:43 +03:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
// creating css appdata folder
|
2017-01-22 21:33:45 +03:00
|
|
|
$folder = $this->appData->newFolder($app);
|
2016-11-09 13:18:43 +03:00
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
|
2017-02-17 18:42:07 +03:00
|
|
|
|
|
|
|
if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) {
|
2016-11-08 15:36:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-17 13:45:08 +03:00
|
|
|
return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir);
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
|
|
|
|
2017-02-17 18:42:07 +03:00
|
|
|
/**
|
|
|
|
* @param $appName
|
|
|
|
* @param $fileName
|
|
|
|
* @return ISimpleFile
|
|
|
|
*/
|
|
|
|
public function getCachedCSS($appName, $fileName) {
|
|
|
|
$folder = $this->appData->getFolder($appName);
|
2017-06-15 23:14:39 +03:00
|
|
|
return $folder->getFile($this->prependBaseurlPrefix($fileName));
|
2017-02-17 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Check if the file is cached or not
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $fileNameCSS
|
|
|
|
* @param ISimpleFolder $folder
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-03-21 23:53:20 +03:00
|
|
|
private function isCached($fileNameCSS, ISimpleFolder $folder) {
|
2017-02-17 13:45:08 +03:00
|
|
|
try {
|
2016-11-30 23:51:09 +03:00
|
|
|
$cachedFile = $folder->getFile($fileNameCSS);
|
2017-03-09 22:29:50 +03:00
|
|
|
if ($cachedFile->getSize() > 0) {
|
2017-03-21 23:53:20 +03:00
|
|
|
$depFileName = $fileNameCSS . '.deps';
|
|
|
|
$deps = $this->depsCache->get($folder->getName() . '-' . $depFileName);
|
|
|
|
if ($deps === null) {
|
|
|
|
$depFile = $folder->getFile($depFileName);
|
|
|
|
$deps = $depFile->getContent();
|
|
|
|
//Set to memcache for next run
|
|
|
|
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
|
|
|
|
}
|
|
|
|
$deps = json_decode($deps, true);
|
2017-03-09 22:29:50 +03:00
|
|
|
|
|
|
|
foreach ($deps as $file=>$mtime) {
|
|
|
|
if (!file_exists($file) || filemtime($file) > $mtime) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 13:18:43 +03:00
|
|
|
}
|
2017-03-09 22:29:50 +03:00
|
|
|
return true;
|
2016-11-09 13:18:43 +03:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-17 13:45:08 +03:00
|
|
|
}
|
|
|
|
|
2017-02-17 18:42:07 +03:00
|
|
|
/**
|
|
|
|
* Check if the variables file has changed
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function variablesChanged() {
|
|
|
|
$injectedVariables = $this->getInjectedVariables();
|
|
|
|
if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) {
|
|
|
|
$this->resetCache();
|
|
|
|
$this->config->setAppValue('core', 'scss.variables', md5($injectedVariables));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Cache the file with AppData
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $path
|
|
|
|
* @param string $fileNameCSS
|
|
|
|
* @param string $fileNameSCSS
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param string $webDir
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-11-30 23:51:09 +03:00
|
|
|
private function cache($path, $fileNameCSS, $fileNameSCSS, ISimpleFolder $folder, $webDir) {
|
2016-11-08 15:36:35 +03:00
|
|
|
$scss = new Compiler();
|
2017-02-17 13:45:08 +03:00
|
|
|
$scss->setImportPaths([
|
|
|
|
$path,
|
|
|
|
\OC::$SERVERROOT . '/core/css/',
|
|
|
|
]);
|
2017-03-06 01:28:59 +03:00
|
|
|
if($this->config->getSystemValue('debug')) {
|
2016-11-08 15:36:35 +03:00
|
|
|
// Debug mode
|
2016-11-30 18:30:04 +03:00
|
|
|
$scss->setFormatter(Expanded::class);
|
2016-11-08 15:36:35 +03:00
|
|
|
$scss->setLineNumberStyle(Compiler::LINE_COMMENTS);
|
|
|
|
} else {
|
2016-11-09 13:18:43 +03:00
|
|
|
// Compression
|
2016-11-30 18:30:04 +03:00
|
|
|
$scss->setFormatter(Crunched::class);
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-11-30 23:51:09 +03:00
|
|
|
$cachedfile = $folder->getFile($fileNameCSS);
|
2016-11-09 13:18:43 +03:00
|
|
|
} catch(NotFoundException $e) {
|
2016-11-30 23:51:09 +03:00
|
|
|
$cachedfile = $folder->newFile($fileNameCSS);
|
2016-11-09 13:18:43 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 22:29:50 +03:00
|
|
|
$depFileName = $fileNameCSS . '.deps';
|
|
|
|
try {
|
|
|
|
$depFile = $folder->getFile($depFileName);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
$depFile = $folder->newFile($depFileName);
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
// Compile
|
2016-11-09 13:18:43 +03:00
|
|
|
try {
|
2017-02-17 13:45:08 +03:00
|
|
|
$compiledScss = $scss->compile(
|
|
|
|
'@import "variables.scss";' .
|
2017-02-17 18:42:07 +03:00
|
|
|
$this->getInjectedVariables() .
|
2017-02-17 13:45:08 +03:00
|
|
|
'@import "'.$fileNameSCSS.'";');
|
2016-11-08 15:36:35 +03:00
|
|
|
} catch(ParserException $e) {
|
2016-11-10 18:16:33 +03:00
|
|
|
$this->logger->error($e, ['app' => 'core']);
|
2016-11-08 15:36:35 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-08 23:45:43 +03:00
|
|
|
|
2017-03-26 17:30:08 +03:00
|
|
|
// Gzip file
|
2016-11-09 15:39:08 +03:00
|
|
|
try {
|
2017-03-29 09:11:51 +03:00
|
|
|
$gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 17:30:08 +03:00
|
|
|
} catch (NotFoundException $e) {
|
2017-03-29 09:11:51 +03:00
|
|
|
$gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 17:30:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$data = $this->rebaseUrls($compiledScss, $webDir);
|
|
|
|
$cachedfile->putContent($data);
|
2017-05-21 23:10:00 +03:00
|
|
|
$deps = json_encode($scss->getParsedFiles());
|
|
|
|
$depFile->putContent($deps);
|
|
|
|
$this->depsCache->set($folder->getName() . '-' . $depFileName, $deps);
|
2017-03-29 00:13:59 +03:00
|
|
|
$gzipFile->putContent(gzencode($data, 9));
|
2016-11-30 23:51:09 +03:00
|
|
|
$this->logger->debug($webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']);
|
2016-11-08 23:45:43 +03:00
|
|
|
return true;
|
2017-03-20 14:31:29 +03:00
|
|
|
} catch(NotPermittedException $e) {
|
2016-11-09 15:39:08 +03:00
|
|
|
return false;
|
2016-11-08 23:45:43 +03:00
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
2017-02-17 18:42:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset scss cache by deleting all generated css files
|
|
|
|
* We need to regenerate all files when variables change
|
|
|
|
*/
|
|
|
|
private function resetCache() {
|
|
|
|
$appDirectory = $this->appData->getDirectoryListing();
|
|
|
|
if(empty($appDirectory)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($appDirectory as $folder) {
|
|
|
|
foreach ($folder->getDirectoryListing() as $file) {
|
|
|
|
if (substr($file->getName(), -3) === "css" || substr($file->getName(), -4) === "deps") {
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string SCSS code for variables from OC_Defaults
|
|
|
|
*/
|
|
|
|
private function getInjectedVariables() {
|
|
|
|
$variables = '';
|
|
|
|
foreach ($this->defaults->getScssVariables() as $key => $value) {
|
|
|
|
$variables .= '$' . $key . ': ' . $value . ';';
|
|
|
|
}
|
|
|
|
return $variables;
|
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Add the correct uri prefix to make uri valid again
|
|
|
|
* @param string $css
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $webDir
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-30 23:51:09 +03:00
|
|
|
private function rebaseUrls($css, $webDir) {
|
2016-11-10 18:20:50 +03:00
|
|
|
$re = '/url\([\'"]([\.\w?=\/-]*)[\'"]\)/x';
|
2017-01-05 22:29:03 +03:00
|
|
|
// OC\Route\Router:75
|
2017-03-06 01:28:59 +03:00
|
|
|
if(($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true')) {
|
2017-01-05 22:29:03 +03:00
|
|
|
$subst = 'url(\'../../'.$webDir.'/$1\')';
|
|
|
|
} else {
|
|
|
|
$subst = 'url(\'../../../'.$webDir.'/$1\')';
|
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
return preg_replace($re, $subst, $css);
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:39:08 +03:00
|
|
|
/**
|
|
|
|
* Return the cached css file uri
|
2016-11-10 18:36:58 +03:00
|
|
|
* @param string $appName the app name
|
2016-11-30 23:51:09 +03:00
|
|
|
* @param string $fileName
|
2016-11-09 15:39:08 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-11-30 23:51:09 +03:00
|
|
|
public function getCachedSCSS($appName, $fileName) {
|
2016-12-22 17:41:13 +03:00
|
|
|
$tmpfileLoc = explode('/', $fileName);
|
|
|
|
$fileName = array_pop($tmpfileLoc);
|
2017-06-15 23:14:39 +03:00
|
|
|
$fileName = $this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName));
|
2016-11-30 23:51:09 +03:00
|
|
|
|
2017-01-02 18:59:36 +03:00
|
|
|
return substr($this->urlGenerator->linkToRoute('core.Css.getCss', array('fileName' => $fileName, 'appName' => $appName)), strlen(\OC::$WEBROOT) + 1);
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|
2017-06-15 13:35:16 +03:00
|
|
|
|
2017-06-15 23:14:39 +03:00
|
|
|
/**
|
|
|
|
* Prepend hashed base url to the css file
|
|
|
|
* @param $cssFile
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function prependBaseurlPrefix($cssFile) {
|
|
|
|
return md5($this->urlGenerator->getBaseUrl()) . '-' . $cssFile;
|
2017-06-15 13:35:16 +03:00
|
|
|
}
|
2016-11-08 15:36:35 +03:00
|
|
|
}
|