@@ -46,334 +46,334 @@ |
||
| 46 | 46 | |
| 47 | 47 | class SCSSCacher { |
| 48 | 48 | |
| 49 | - /** @var ILogger */ |
|
| 50 | - protected $logger; |
|
| 51 | - |
|
| 52 | - /** @var IAppData */ |
|
| 53 | - protected $appData; |
|
| 54 | - |
|
| 55 | - /** @var IURLGenerator */ |
|
| 56 | - protected $urlGenerator; |
|
| 57 | - |
|
| 58 | - /** @var IConfig */ |
|
| 59 | - protected $config; |
|
| 60 | - |
|
| 61 | - /** @var \OC_Defaults */ |
|
| 62 | - private $defaults; |
|
| 63 | - |
|
| 64 | - /** @var string */ |
|
| 65 | - protected $serverRoot; |
|
| 66 | - |
|
| 67 | - /** @var ICache */ |
|
| 68 | - protected $depsCache; |
|
| 69 | - |
|
| 70 | - /** @var null|string */ |
|
| 71 | - private $injectedVariables; |
|
| 72 | - |
|
| 73 | - /** @var ICacheFactory */ |
|
| 74 | - private $cacheFactory; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param ILogger $logger |
|
| 78 | - * @param Factory $appDataFactory |
|
| 79 | - * @param IURLGenerator $urlGenerator |
|
| 80 | - * @param IConfig $config |
|
| 81 | - * @param \OC_Defaults $defaults |
|
| 82 | - * @param string $serverRoot |
|
| 83 | - * @param ICacheFactory $cacheFactory |
|
| 84 | - */ |
|
| 85 | - public function __construct(ILogger $logger, |
|
| 86 | - Factory $appDataFactory, |
|
| 87 | - IURLGenerator $urlGenerator, |
|
| 88 | - IConfig $config, |
|
| 89 | - \OC_Defaults $defaults, |
|
| 90 | - $serverRoot, |
|
| 91 | - ICacheFactory $cacheFactory) { |
|
| 92 | - $this->logger = $logger; |
|
| 93 | - $this->appData = $appDataFactory->get('css'); |
|
| 94 | - $this->urlGenerator = $urlGenerator; |
|
| 95 | - $this->config = $config; |
|
| 96 | - $this->defaults = $defaults; |
|
| 97 | - $this->serverRoot = $serverRoot; |
|
| 98 | - $this->cacheFactory = $cacheFactory; |
|
| 99 | - $this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl())); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Process the caching process if needed |
|
| 104 | - * |
|
| 105 | - * @param string $root Root path to the nextcloud installation |
|
| 106 | - * @param string $file |
|
| 107 | - * @param string $app The app name |
|
| 108 | - * @return boolean |
|
| 109 | - * @throws NotPermittedException |
|
| 110 | - */ |
|
| 111 | - public function process(string $root, string $file, string $app): bool { |
|
| 112 | - $path = explode('/', $root . '/' . $file); |
|
| 113 | - |
|
| 114 | - $fileNameSCSS = array_pop($path); |
|
| 115 | - $fileNameCSS = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)), $app); |
|
| 116 | - |
|
| 117 | - $path = implode('/', $path); |
|
| 118 | - $webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT); |
|
| 119 | - |
|
| 120 | - try { |
|
| 121 | - $folder = $this->appData->getFolder($app); |
|
| 122 | - } catch(NotFoundException $e) { |
|
| 123 | - // creating css appdata folder |
|
| 124 | - $folder = $this->appData->newFolder($app); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 129 | - return true; |
|
| 130 | - } |
|
| 131 | - return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @param $appName |
|
| 136 | - * @param $fileName |
|
| 137 | - * @return ISimpleFile |
|
| 138 | - */ |
|
| 139 | - public function getCachedCSS(string $appName, string $fileName): ISimpleFile { |
|
| 140 | - $folder = $this->appData->getFolder($appName); |
|
| 141 | - $cachedFileName = $this->prependVersionPrefix($this->prependBaseurlPrefix($fileName), $appName); |
|
| 142 | - return $folder->getFile($cachedFileName); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Check if the file is cached or not |
|
| 147 | - * @param string $fileNameCSS |
|
| 148 | - * @param ISimpleFolder $folder |
|
| 149 | - * @return boolean |
|
| 150 | - */ |
|
| 151 | - private function isCached(string $fileNameCSS, ISimpleFolder $folder) { |
|
| 152 | - try { |
|
| 153 | - $cachedFile = $folder->getFile($fileNameCSS); |
|
| 154 | - if ($cachedFile->getSize() > 0) { |
|
| 155 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 156 | - $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 157 | - if ($deps === null) { |
|
| 158 | - $depFile = $folder->getFile($depFileName); |
|
| 159 | - $deps = $depFile->getContent(); |
|
| 160 | - //Set to memcache for next run |
|
| 161 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 162 | - } |
|
| 163 | - $deps = json_decode($deps, true); |
|
| 164 | - |
|
| 165 | - foreach ((array)$deps as $file=>$mtime) { |
|
| 166 | - if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 167 | - return false; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - return true; |
|
| 171 | - } |
|
| 172 | - return false; |
|
| 173 | - } catch(NotFoundException $e) { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Check if the variables file has changed |
|
| 180 | - * @return bool |
|
| 181 | - */ |
|
| 182 | - private function variablesChanged(): bool { |
|
| 183 | - $injectedVariables = $this->getInjectedVariables(); |
|
| 184 | - if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 185 | - $this->resetCache(); |
|
| 186 | - $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
|
| 187 | - return true; |
|
| 188 | - } |
|
| 189 | - return false; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Cache the file with AppData |
|
| 194 | - * |
|
| 195 | - * @param string $path |
|
| 196 | - * @param string $fileNameCSS |
|
| 197 | - * @param string $fileNameSCSS |
|
| 198 | - * @param ISimpleFolder $folder |
|
| 199 | - * @param string $webDir |
|
| 200 | - * @return boolean |
|
| 201 | - * @throws NotPermittedException |
|
| 202 | - */ |
|
| 203 | - private function cache(string $path, string $fileNameCSS, string $fileNameSCSS, ISimpleFolder $folder, string $webDir) { |
|
| 204 | - $scss = new Compiler(); |
|
| 205 | - $scss->setImportPaths([ |
|
| 206 | - $path, |
|
| 207 | - $this->serverRoot . '/core/css/', |
|
| 208 | - ]); |
|
| 209 | - // Continue after throw |
|
| 210 | - $scss->setIgnoreErrors(true); |
|
| 211 | - if($this->config->getSystemValue('debug')) { |
|
| 212 | - // Debug mode |
|
| 213 | - $scss->setFormatter(Expanded::class); |
|
| 214 | - $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
|
| 215 | - } else { |
|
| 216 | - // Compression |
|
| 217 | - $scss->setFormatter(Crunched::class); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - try { |
|
| 221 | - $cachedfile = $folder->getFile($fileNameCSS); |
|
| 222 | - } catch(NotFoundException $e) { |
|
| 223 | - $cachedfile = $folder->newFile($fileNameCSS); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 227 | - try { |
|
| 228 | - $depFile = $folder->getFile($depFileName); |
|
| 229 | - } catch (NotFoundException $e) { |
|
| 230 | - $depFile = $folder->newFile($depFileName); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - // Compile |
|
| 234 | - try { |
|
| 235 | - $compiledScss = $scss->compile( |
|
| 236 | - '$webroot: \'' . \OC::$WEBROOT. '\';'. |
|
| 237 | - '@import "variables.scss";' . |
|
| 238 | - $this->getInjectedVariables() . |
|
| 239 | - '@import "'.$fileNameSCSS.'";'); |
|
| 240 | - } catch(ParserException $e) { |
|
| 241 | - $this->logger->error($e, ['app' => 'core']); |
|
| 242 | - return false; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - // Gzip file |
|
| 246 | - try { |
|
| 247 | - $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 248 | - } catch (NotFoundException $e) { |
|
| 249 | - $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - try { |
|
| 253 | - $data = $this->rebaseUrls($compiledScss, $webDir); |
|
| 254 | - $cachedfile->putContent($data); |
|
| 255 | - $deps = json_encode($scss->getParsedFiles()); |
|
| 256 | - $depFile->putContent($deps); |
|
| 257 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 258 | - $gzipFile->putContent(gzencode($data, 9)); |
|
| 259 | - $this->logger->debug('SCSSCacher: '.$webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
|
| 260 | - return true; |
|
| 261 | - } catch(NotPermittedException $e) { |
|
| 262 | - $this->logger->error('SCSSCacher: unable to cache: ' . $fileNameSCSS); |
|
| 263 | - return false; |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Reset scss cache by deleting all generated css files |
|
| 269 | - * We need to regenerate all files when variables change |
|
| 270 | - */ |
|
| 271 | - public function resetCache() { |
|
| 272 | - $this->injectedVariables = null; |
|
| 273 | - $this->cacheFactory->createDistributed('SCSS-')->clear(); |
|
| 274 | - $appDirectory = $this->appData->getDirectoryListing(); |
|
| 275 | - foreach ($appDirectory as $folder) { |
|
| 276 | - foreach ($folder->getDirectoryListing() as $file) { |
|
| 277 | - try { |
|
| 278 | - $file->delete(); |
|
| 279 | - } catch(NotPermittedException $e) { |
|
| 280 | - $this->logger->logException($e, ['message' => 'SCSSCacher: unable to delete file: ' . $file->getName()]); |
|
| 281 | - } |
|
| 282 | - } |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * @return string SCSS code for variables from OC_Defaults |
|
| 288 | - */ |
|
| 289 | - private function getInjectedVariables(): string { |
|
| 290 | - if ($this->injectedVariables !== null) { |
|
| 291 | - return $this->injectedVariables; |
|
| 292 | - } |
|
| 293 | - $variables = ''; |
|
| 294 | - foreach ($this->defaults->getScssVariables() as $key => $value) { |
|
| 295 | - $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // check for valid variables / otherwise fall back to defaults |
|
| 299 | - try { |
|
| 300 | - $scss = new Compiler(); |
|
| 301 | - $scss->compile($variables); |
|
| 302 | - $this->injectedVariables = $variables; |
|
| 303 | - } catch (ParserException $e) { |
|
| 304 | - $this->logger->error($e, ['app' => 'core']); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - return $variables; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Add the correct uri prefix to make uri valid again |
|
| 312 | - * @param string $css |
|
| 313 | - * @param string $webDir |
|
| 314 | - * @return string |
|
| 315 | - */ |
|
| 316 | - private function rebaseUrls(string $css, string $webDir): string { |
|
| 317 | - $re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x'; |
|
| 318 | - $subst = 'url(\''.$webDir.'/$1\')'; |
|
| 319 | - return preg_replace($re, $subst, $css); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Return the cached css file uri |
|
| 324 | - * @param string $appName the app name |
|
| 325 | - * @param string $fileName |
|
| 326 | - * @return string |
|
| 327 | - */ |
|
| 328 | - public function getCachedSCSS(string $appName, string $fileName): string { |
|
| 329 | - $tmpfileLoc = explode('/', $fileName); |
|
| 330 | - $fileName = array_pop($tmpfileLoc); |
|
| 331 | - $fileName = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)), $appName); |
|
| 332 | - |
|
| 333 | - return substr($this->urlGenerator->linkToRoute('core.Css.getCss', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Prepend hashed base url to the css file |
|
| 338 | - * @param string $cssFile |
|
| 339 | - * @return string |
|
| 340 | - */ |
|
| 341 | - private function prependBaseurlPrefix(string $cssFile): string { |
|
| 342 | - $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 343 | - return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 4) . '-' . $cssFile; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Prepend hashed app version hash |
|
| 348 | - * @param string $cssFile |
|
| 349 | - * @param string $appId |
|
| 350 | - * @return string |
|
| 351 | - */ |
|
| 352 | - private function prependVersionPrefix(string $cssFile, string $appId): string { |
|
| 353 | - $appVersion = \OC_App::getAppVersion($appId); |
|
| 354 | - if ($appVersion !== '0') { |
|
| 355 | - return substr(md5($appVersion), 0, 4) . '-' . $cssFile; |
|
| 356 | - } |
|
| 357 | - $coreVersion = \OC_Util::getVersionString(); |
|
| 358 | - return substr(md5($coreVersion), 0, 4) . '-' . $cssFile; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Get WebDir root |
|
| 363 | - * @param string $path the css file path |
|
| 364 | - * @param string $appName the app name |
|
| 365 | - * @param string $serverRoot the server root path |
|
| 366 | - * @param string $webRoot the nextcloud installation root path |
|
| 367 | - * @return string the webDir |
|
| 368 | - */ |
|
| 369 | - private function getWebDir(string $path, string $appName, string $serverRoot, string $webRoot): string { |
|
| 370 | - // Detect if path is within server root AND if path is within an app path |
|
| 371 | - if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) { |
|
| 372 | - // Get the file path within the app directory |
|
| 373 | - $appDirectoryPath = explode($appName, $path)[1]; |
|
| 374 | - // Remove the webroot |
|
| 375 | - return str_replace($webRoot, '', $appWebPath.$appDirectoryPath); |
|
| 376 | - } |
|
| 377 | - return $webRoot.substr($path, strlen($serverRoot)); |
|
| 378 | - } |
|
| 49 | + /** @var ILogger */ |
|
| 50 | + protected $logger; |
|
| 51 | + |
|
| 52 | + /** @var IAppData */ |
|
| 53 | + protected $appData; |
|
| 54 | + |
|
| 55 | + /** @var IURLGenerator */ |
|
| 56 | + protected $urlGenerator; |
|
| 57 | + |
|
| 58 | + /** @var IConfig */ |
|
| 59 | + protected $config; |
|
| 60 | + |
|
| 61 | + /** @var \OC_Defaults */ |
|
| 62 | + private $defaults; |
|
| 63 | + |
|
| 64 | + /** @var string */ |
|
| 65 | + protected $serverRoot; |
|
| 66 | + |
|
| 67 | + /** @var ICache */ |
|
| 68 | + protected $depsCache; |
|
| 69 | + |
|
| 70 | + /** @var null|string */ |
|
| 71 | + private $injectedVariables; |
|
| 72 | + |
|
| 73 | + /** @var ICacheFactory */ |
|
| 74 | + private $cacheFactory; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param ILogger $logger |
|
| 78 | + * @param Factory $appDataFactory |
|
| 79 | + * @param IURLGenerator $urlGenerator |
|
| 80 | + * @param IConfig $config |
|
| 81 | + * @param \OC_Defaults $defaults |
|
| 82 | + * @param string $serverRoot |
|
| 83 | + * @param ICacheFactory $cacheFactory |
|
| 84 | + */ |
|
| 85 | + public function __construct(ILogger $logger, |
|
| 86 | + Factory $appDataFactory, |
|
| 87 | + IURLGenerator $urlGenerator, |
|
| 88 | + IConfig $config, |
|
| 89 | + \OC_Defaults $defaults, |
|
| 90 | + $serverRoot, |
|
| 91 | + ICacheFactory $cacheFactory) { |
|
| 92 | + $this->logger = $logger; |
|
| 93 | + $this->appData = $appDataFactory->get('css'); |
|
| 94 | + $this->urlGenerator = $urlGenerator; |
|
| 95 | + $this->config = $config; |
|
| 96 | + $this->defaults = $defaults; |
|
| 97 | + $this->serverRoot = $serverRoot; |
|
| 98 | + $this->cacheFactory = $cacheFactory; |
|
| 99 | + $this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl())); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Process the caching process if needed |
|
| 104 | + * |
|
| 105 | + * @param string $root Root path to the nextcloud installation |
|
| 106 | + * @param string $file |
|
| 107 | + * @param string $app The app name |
|
| 108 | + * @return boolean |
|
| 109 | + * @throws NotPermittedException |
|
| 110 | + */ |
|
| 111 | + public function process(string $root, string $file, string $app): bool { |
|
| 112 | + $path = explode('/', $root . '/' . $file); |
|
| 113 | + |
|
| 114 | + $fileNameSCSS = array_pop($path); |
|
| 115 | + $fileNameCSS = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)), $app); |
|
| 116 | + |
|
| 117 | + $path = implode('/', $path); |
|
| 118 | + $webDir = $this->getWebDir($path, $app, $this->serverRoot, \OC::$WEBROOT); |
|
| 119 | + |
|
| 120 | + try { |
|
| 121 | + $folder = $this->appData->getFolder($app); |
|
| 122 | + } catch(NotFoundException $e) { |
|
| 123 | + // creating css appdata folder |
|
| 124 | + $folder = $this->appData->newFolder($app); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 129 | + return true; |
|
| 130 | + } |
|
| 131 | + return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @param $appName |
|
| 136 | + * @param $fileName |
|
| 137 | + * @return ISimpleFile |
|
| 138 | + */ |
|
| 139 | + public function getCachedCSS(string $appName, string $fileName): ISimpleFile { |
|
| 140 | + $folder = $this->appData->getFolder($appName); |
|
| 141 | + $cachedFileName = $this->prependVersionPrefix($this->prependBaseurlPrefix($fileName), $appName); |
|
| 142 | + return $folder->getFile($cachedFileName); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Check if the file is cached or not |
|
| 147 | + * @param string $fileNameCSS |
|
| 148 | + * @param ISimpleFolder $folder |
|
| 149 | + * @return boolean |
|
| 150 | + */ |
|
| 151 | + private function isCached(string $fileNameCSS, ISimpleFolder $folder) { |
|
| 152 | + try { |
|
| 153 | + $cachedFile = $folder->getFile($fileNameCSS); |
|
| 154 | + if ($cachedFile->getSize() > 0) { |
|
| 155 | + $depFileName = $fileNameCSS . '.deps'; |
|
| 156 | + $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 157 | + if ($deps === null) { |
|
| 158 | + $depFile = $folder->getFile($depFileName); |
|
| 159 | + $deps = $depFile->getContent(); |
|
| 160 | + //Set to memcache for next run |
|
| 161 | + $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 162 | + } |
|
| 163 | + $deps = json_decode($deps, true); |
|
| 164 | + |
|
| 165 | + foreach ((array)$deps as $file=>$mtime) { |
|
| 166 | + if (!file_exists($file) || filemtime($file) > $mtime) { |
|
| 167 | + return false; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + return true; |
|
| 171 | + } |
|
| 172 | + return false; |
|
| 173 | + } catch(NotFoundException $e) { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Check if the variables file has changed |
|
| 180 | + * @return bool |
|
| 181 | + */ |
|
| 182 | + private function variablesChanged(): bool { |
|
| 183 | + $injectedVariables = $this->getInjectedVariables(); |
|
| 184 | + if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 185 | + $this->resetCache(); |
|
| 186 | + $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
|
| 187 | + return true; |
|
| 188 | + } |
|
| 189 | + return false; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Cache the file with AppData |
|
| 194 | + * |
|
| 195 | + * @param string $path |
|
| 196 | + * @param string $fileNameCSS |
|
| 197 | + * @param string $fileNameSCSS |
|
| 198 | + * @param ISimpleFolder $folder |
|
| 199 | + * @param string $webDir |
|
| 200 | + * @return boolean |
|
| 201 | + * @throws NotPermittedException |
|
| 202 | + */ |
|
| 203 | + private function cache(string $path, string $fileNameCSS, string $fileNameSCSS, ISimpleFolder $folder, string $webDir) { |
|
| 204 | + $scss = new Compiler(); |
|
| 205 | + $scss->setImportPaths([ |
|
| 206 | + $path, |
|
| 207 | + $this->serverRoot . '/core/css/', |
|
| 208 | + ]); |
|
| 209 | + // Continue after throw |
|
| 210 | + $scss->setIgnoreErrors(true); |
|
| 211 | + if($this->config->getSystemValue('debug')) { |
|
| 212 | + // Debug mode |
|
| 213 | + $scss->setFormatter(Expanded::class); |
|
| 214 | + $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
|
| 215 | + } else { |
|
| 216 | + // Compression |
|
| 217 | + $scss->setFormatter(Crunched::class); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + try { |
|
| 221 | + $cachedfile = $folder->getFile($fileNameCSS); |
|
| 222 | + } catch(NotFoundException $e) { |
|
| 223 | + $cachedfile = $folder->newFile($fileNameCSS); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + $depFileName = $fileNameCSS . '.deps'; |
|
| 227 | + try { |
|
| 228 | + $depFile = $folder->getFile($depFileName); |
|
| 229 | + } catch (NotFoundException $e) { |
|
| 230 | + $depFile = $folder->newFile($depFileName); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + // Compile |
|
| 234 | + try { |
|
| 235 | + $compiledScss = $scss->compile( |
|
| 236 | + '$webroot: \'' . \OC::$WEBROOT. '\';'. |
|
| 237 | + '@import "variables.scss";' . |
|
| 238 | + $this->getInjectedVariables() . |
|
| 239 | + '@import "'.$fileNameSCSS.'";'); |
|
| 240 | + } catch(ParserException $e) { |
|
| 241 | + $this->logger->error($e, ['app' => 'core']); |
|
| 242 | + return false; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + // Gzip file |
|
| 246 | + try { |
|
| 247 | + $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 248 | + } catch (NotFoundException $e) { |
|
| 249 | + $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + try { |
|
| 253 | + $data = $this->rebaseUrls($compiledScss, $webDir); |
|
| 254 | + $cachedfile->putContent($data); |
|
| 255 | + $deps = json_encode($scss->getParsedFiles()); |
|
| 256 | + $depFile->putContent($deps); |
|
| 257 | + $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 258 | + $gzipFile->putContent(gzencode($data, 9)); |
|
| 259 | + $this->logger->debug('SCSSCacher: '.$webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
|
| 260 | + return true; |
|
| 261 | + } catch(NotPermittedException $e) { |
|
| 262 | + $this->logger->error('SCSSCacher: unable to cache: ' . $fileNameSCSS); |
|
| 263 | + return false; |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Reset scss cache by deleting all generated css files |
|
| 269 | + * We need to regenerate all files when variables change |
|
| 270 | + */ |
|
| 271 | + public function resetCache() { |
|
| 272 | + $this->injectedVariables = null; |
|
| 273 | + $this->cacheFactory->createDistributed('SCSS-')->clear(); |
|
| 274 | + $appDirectory = $this->appData->getDirectoryListing(); |
|
| 275 | + foreach ($appDirectory as $folder) { |
|
| 276 | + foreach ($folder->getDirectoryListing() as $file) { |
|
| 277 | + try { |
|
| 278 | + $file->delete(); |
|
| 279 | + } catch(NotPermittedException $e) { |
|
| 280 | + $this->logger->logException($e, ['message' => 'SCSSCacher: unable to delete file: ' . $file->getName()]); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * @return string SCSS code for variables from OC_Defaults |
|
| 288 | + */ |
|
| 289 | + private function getInjectedVariables(): string { |
|
| 290 | + if ($this->injectedVariables !== null) { |
|
| 291 | + return $this->injectedVariables; |
|
| 292 | + } |
|
| 293 | + $variables = ''; |
|
| 294 | + foreach ($this->defaults->getScssVariables() as $key => $value) { |
|
| 295 | + $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // check for valid variables / otherwise fall back to defaults |
|
| 299 | + try { |
|
| 300 | + $scss = new Compiler(); |
|
| 301 | + $scss->compile($variables); |
|
| 302 | + $this->injectedVariables = $variables; |
|
| 303 | + } catch (ParserException $e) { |
|
| 304 | + $this->logger->error($e, ['app' => 'core']); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + return $variables; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Add the correct uri prefix to make uri valid again |
|
| 312 | + * @param string $css |
|
| 313 | + * @param string $webDir |
|
| 314 | + * @return string |
|
| 315 | + */ |
|
| 316 | + private function rebaseUrls(string $css, string $webDir): string { |
|
| 317 | + $re = '/url\([\'"]([^\/][\.\w?=\/-]*)[\'"]\)/x'; |
|
| 318 | + $subst = 'url(\''.$webDir.'/$1\')'; |
|
| 319 | + return preg_replace($re, $subst, $css); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Return the cached css file uri |
|
| 324 | + * @param string $appName the app name |
|
| 325 | + * @param string $fileName |
|
| 326 | + * @return string |
|
| 327 | + */ |
|
| 328 | + public function getCachedSCSS(string $appName, string $fileName): string { |
|
| 329 | + $tmpfileLoc = explode('/', $fileName); |
|
| 330 | + $fileName = array_pop($tmpfileLoc); |
|
| 331 | + $fileName = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileName)), $appName); |
|
| 332 | + |
|
| 333 | + return substr($this->urlGenerator->linkToRoute('core.Css.getCss', ['fileName' => $fileName, 'appName' => $appName]), strlen(\OC::$WEBROOT) + 1); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Prepend hashed base url to the css file |
|
| 338 | + * @param string $cssFile |
|
| 339 | + * @return string |
|
| 340 | + */ |
|
| 341 | + private function prependBaseurlPrefix(string $cssFile): string { |
|
| 342 | + $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 343 | + return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 4) . '-' . $cssFile; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Prepend hashed app version hash |
|
| 348 | + * @param string $cssFile |
|
| 349 | + * @param string $appId |
|
| 350 | + * @return string |
|
| 351 | + */ |
|
| 352 | + private function prependVersionPrefix(string $cssFile, string $appId): string { |
|
| 353 | + $appVersion = \OC_App::getAppVersion($appId); |
|
| 354 | + if ($appVersion !== '0') { |
|
| 355 | + return substr(md5($appVersion), 0, 4) . '-' . $cssFile; |
|
| 356 | + } |
|
| 357 | + $coreVersion = \OC_Util::getVersionString(); |
|
| 358 | + return substr(md5($coreVersion), 0, 4) . '-' . $cssFile; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Get WebDir root |
|
| 363 | + * @param string $path the css file path |
|
| 364 | + * @param string $appName the app name |
|
| 365 | + * @param string $serverRoot the server root path |
|
| 366 | + * @param string $webRoot the nextcloud installation root path |
|
| 367 | + * @return string the webDir |
|
| 368 | + */ |
|
| 369 | + private function getWebDir(string $path, string $appName, string $serverRoot, string $webRoot): string { |
|
| 370 | + // Detect if path is within server root AND if path is within an app path |
|
| 371 | + if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) { |
|
| 372 | + // Get the file path within the app directory |
|
| 373 | + $appDirectoryPath = explode($appName, $path)[1]; |
|
| 374 | + // Remove the webroot |
|
| 375 | + return str_replace($webRoot, '', $appWebPath.$appDirectoryPath); |
|
| 376 | + } |
|
| 377 | + return $webRoot.substr($path, strlen($serverRoot)); |
|
| 378 | + } |
|
| 379 | 379 | } |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | $this->defaults = $defaults; |
| 97 | 97 | $this->serverRoot = $serverRoot; |
| 98 | 98 | $this->cacheFactory = $cacheFactory; |
| 99 | - $this->depsCache = $cacheFactory->createDistributed('SCSS-' . md5($this->urlGenerator->getBaseUrl())); |
|
| 99 | + $this->depsCache = $cacheFactory->createDistributed('SCSS-'.md5($this->urlGenerator->getBaseUrl())); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | * @throws NotPermittedException |
| 110 | 110 | */ |
| 111 | 111 | public function process(string $root, string $file, string $app): bool { |
| 112 | - $path = explode('/', $root . '/' . $file); |
|
| 112 | + $path = explode('/', $root.'/'.$file); |
|
| 113 | 113 | |
| 114 | 114 | $fileNameSCSS = array_pop($path); |
| 115 | 115 | $fileNameCSS = $this->prependVersionPrefix($this->prependBaseurlPrefix(str_replace('.scss', '.css', $fileNameSCSS)), $app); |
@@ -119,13 +119,13 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | try { |
| 121 | 121 | $folder = $this->appData->getFolder($app); |
| 122 | - } catch(NotFoundException $e) { |
|
| 122 | + } catch (NotFoundException $e) { |
|
| 123 | 123 | // creating css appdata folder |
| 124 | 124 | $folder = $this->appData->newFolder($app); |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | |
| 128 | - if(!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 128 | + if (!$this->variablesChanged() && $this->isCached($fileNameCSS, $folder)) { |
|
| 129 | 129 | return true; |
| 130 | 130 | } |
| 131 | 131 | return $this->cache($path, $fileNameCSS, $fileNameSCSS, $folder, $webDir); |
@@ -152,17 +152,17 @@ discard block |
||
| 152 | 152 | try { |
| 153 | 153 | $cachedFile = $folder->getFile($fileNameCSS); |
| 154 | 154 | if ($cachedFile->getSize() > 0) { |
| 155 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 156 | - $deps = $this->depsCache->get($folder->getName() . '-' . $depFileName); |
|
| 155 | + $depFileName = $fileNameCSS.'.deps'; |
|
| 156 | + $deps = $this->depsCache->get($folder->getName().'-'.$depFileName); |
|
| 157 | 157 | if ($deps === null) { |
| 158 | 158 | $depFile = $folder->getFile($depFileName); |
| 159 | 159 | $deps = $depFile->getContent(); |
| 160 | 160 | //Set to memcache for next run |
| 161 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 161 | + $this->depsCache->set($folder->getName().'-'.$depFileName, $deps); |
|
| 162 | 162 | } |
| 163 | 163 | $deps = json_decode($deps, true); |
| 164 | 164 | |
| 165 | - foreach ((array)$deps as $file=>$mtime) { |
|
| 165 | + foreach ((array) $deps as $file=>$mtime) { |
|
| 166 | 166 | if (!file_exists($file) || filemtime($file) > $mtime) { |
| 167 | 167 | return false; |
| 168 | 168 | } |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | return true; |
| 171 | 171 | } |
| 172 | 172 | return false; |
| 173 | - } catch(NotFoundException $e) { |
|
| 173 | + } catch (NotFoundException $e) { |
|
| 174 | 174 | return false; |
| 175 | 175 | } |
| 176 | 176 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | private function variablesChanged(): bool { |
| 183 | 183 | $injectedVariables = $this->getInjectedVariables(); |
| 184 | - if($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 184 | + if ($this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) { |
|
| 185 | 185 | $this->resetCache(); |
| 186 | 186 | $this->config->setAppValue('core', 'scss.variables', md5($injectedVariables)); |
| 187 | 187 | return true; |
@@ -204,11 +204,11 @@ discard block |
||
| 204 | 204 | $scss = new Compiler(); |
| 205 | 205 | $scss->setImportPaths([ |
| 206 | 206 | $path, |
| 207 | - $this->serverRoot . '/core/css/', |
|
| 207 | + $this->serverRoot.'/core/css/', |
|
| 208 | 208 | ]); |
| 209 | 209 | // Continue after throw |
| 210 | 210 | $scss->setIgnoreErrors(true); |
| 211 | - if($this->config->getSystemValue('debug')) { |
|
| 211 | + if ($this->config->getSystemValue('debug')) { |
|
| 212 | 212 | // Debug mode |
| 213 | 213 | $scss->setFormatter(Expanded::class); |
| 214 | 214 | $scss->setLineNumberStyle(Compiler::LINE_COMMENTS); |
@@ -219,11 +219,11 @@ discard block |
||
| 219 | 219 | |
| 220 | 220 | try { |
| 221 | 221 | $cachedfile = $folder->getFile($fileNameCSS); |
| 222 | - } catch(NotFoundException $e) { |
|
| 222 | + } catch (NotFoundException $e) { |
|
| 223 | 223 | $cachedfile = $folder->newFile($fileNameCSS); |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - $depFileName = $fileNameCSS . '.deps'; |
|
| 226 | + $depFileName = $fileNameCSS.'.deps'; |
|
| 227 | 227 | try { |
| 228 | 228 | $depFile = $folder->getFile($depFileName); |
| 229 | 229 | } catch (NotFoundException $e) { |
@@ -233,20 +233,20 @@ discard block |
||
| 233 | 233 | // Compile |
| 234 | 234 | try { |
| 235 | 235 | $compiledScss = $scss->compile( |
| 236 | - '$webroot: \'' . \OC::$WEBROOT. '\';'. |
|
| 237 | - '@import "variables.scss";' . |
|
| 238 | - $this->getInjectedVariables() . |
|
| 236 | + '$webroot: \''.\OC::$WEBROOT.'\';'. |
|
| 237 | + '@import "variables.scss";'. |
|
| 238 | + $this->getInjectedVariables(). |
|
| 239 | 239 | '@import "'.$fileNameSCSS.'";'); |
| 240 | - } catch(ParserException $e) { |
|
| 240 | + } catch (ParserException $e) { |
|
| 241 | 241 | $this->logger->error($e, ['app' => 'core']); |
| 242 | 242 | return false; |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | // Gzip file |
| 246 | 246 | try { |
| 247 | - $gzipFile = $folder->getFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 247 | + $gzipFile = $folder->getFile($fileNameCSS.'.gzip'); # Safari doesn't like .gz |
|
| 248 | 248 | } catch (NotFoundException $e) { |
| 249 | - $gzipFile = $folder->newFile($fileNameCSS . '.gzip'); # Safari doesn't like .gz |
|
| 249 | + $gzipFile = $folder->newFile($fileNameCSS.'.gzip'); # Safari doesn't like .gz |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | try { |
@@ -254,12 +254,12 @@ discard block |
||
| 254 | 254 | $cachedfile->putContent($data); |
| 255 | 255 | $deps = json_encode($scss->getParsedFiles()); |
| 256 | 256 | $depFile->putContent($deps); |
| 257 | - $this->depsCache->set($folder->getName() . '-' . $depFileName, $deps); |
|
| 257 | + $this->depsCache->set($folder->getName().'-'.$depFileName, $deps); |
|
| 258 | 258 | $gzipFile->putContent(gzencode($data, 9)); |
| 259 | 259 | $this->logger->debug('SCSSCacher: '.$webDir.'/'.$fileNameSCSS.' compiled and successfully cached', ['app' => 'core']); |
| 260 | 260 | return true; |
| 261 | - } catch(NotPermittedException $e) { |
|
| 262 | - $this->logger->error('SCSSCacher: unable to cache: ' . $fileNameSCSS); |
|
| 261 | + } catch (NotPermittedException $e) { |
|
| 262 | + $this->logger->error('SCSSCacher: unable to cache: '.$fileNameSCSS); |
|
| 263 | 263 | return false; |
| 264 | 264 | } |
| 265 | 265 | } |
@@ -276,8 +276,8 @@ discard block |
||
| 276 | 276 | foreach ($folder->getDirectoryListing() as $file) { |
| 277 | 277 | try { |
| 278 | 278 | $file->delete(); |
| 279 | - } catch(NotPermittedException $e) { |
|
| 280 | - $this->logger->logException($e, ['message' => 'SCSSCacher: unable to delete file: ' . $file->getName()]); |
|
| 279 | + } catch (NotPermittedException $e) { |
|
| 280 | + $this->logger->logException($e, ['message' => 'SCSSCacher: unable to delete file: '.$file->getName()]); |
|
| 281 | 281 | } |
| 282 | 282 | } |
| 283 | 283 | } |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | } |
| 293 | 293 | $variables = ''; |
| 294 | 294 | foreach ($this->defaults->getScssVariables() as $key => $value) { |
| 295 | - $variables .= '$' . $key . ': ' . $value . ';'; |
|
| 295 | + $variables .= '$'.$key.': '.$value.';'; |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | // check for valid variables / otherwise fall back to defaults |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | */ |
| 341 | 341 | private function prependBaseurlPrefix(string $cssFile): string { |
| 342 | 342 | $frontendController = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
| 343 | - return substr(md5($this->urlGenerator->getBaseUrl() . $frontendController), 0, 4) . '-' . $cssFile; |
|
| 343 | + return substr(md5($this->urlGenerator->getBaseUrl().$frontendController), 0, 4).'-'.$cssFile; |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | /** |
@@ -352,10 +352,10 @@ discard block |
||
| 352 | 352 | private function prependVersionPrefix(string $cssFile, string $appId): string { |
| 353 | 353 | $appVersion = \OC_App::getAppVersion($appId); |
| 354 | 354 | if ($appVersion !== '0') { |
| 355 | - return substr(md5($appVersion), 0, 4) . '-' . $cssFile; |
|
| 355 | + return substr(md5($appVersion), 0, 4).'-'.$cssFile; |
|
| 356 | 356 | } |
| 357 | 357 | $coreVersion = \OC_Util::getVersionString(); |
| 358 | - return substr(md5($coreVersion), 0, 4) . '-' . $cssFile; |
|
| 358 | + return substr(md5($coreVersion), 0, 4).'-'.$cssFile; |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | /** |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | */ |
| 369 | 369 | private function getWebDir(string $path, string $appName, string $serverRoot, string $webRoot): string { |
| 370 | 370 | // Detect if path is within server root AND if path is within an app path |
| 371 | - if ( strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) { |
|
| 371 | + if (strpos($path, $serverRoot) === false && $appWebPath = \OC_App::getAppWebPath($appName)) { |
|
| 372 | 372 | // Get the file path within the app directory |
| 373 | 373 | $appDirectoryPath = explode($appName, $path)[1]; |
| 374 | 374 | // Remove the webroot |
@@ -36,48 +36,48 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | $application = new Application(); |
| 38 | 38 | $application->registerRoutes($this, [ |
| 39 | - 'routes' => [ |
|
| 40 | - ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'], |
|
| 41 | - ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'], |
|
| 42 | - ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'], |
|
| 43 | - ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'], |
|
| 44 | - ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'], |
|
| 45 | - ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'], |
|
| 46 | - ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'], |
|
| 47 | - ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'], |
|
| 48 | - ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'], |
|
| 49 | - ['name' => 'CSRFToken#index', 'url' => '/csrftoken', 'verb' => 'GET'], |
|
| 50 | - ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'], |
|
| 51 | - ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'], |
|
| 52 | - ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'], |
|
| 53 | - ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'], |
|
| 54 | - ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'], |
|
| 55 | - ['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'], |
|
| 56 | - ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'], |
|
| 57 | - ['name' => 'ClientFlowLogin#grantPage', 'url' => '/login/flow/grant', 'verb' => 'GET'], |
|
| 58 | - ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'], |
|
| 59 | - ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'], |
|
| 60 | - ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'], |
|
| 61 | - ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'], |
|
| 62 | - ['name' => 'Preview#getPreviewByFileId', 'url' => '/core/preview', 'verb' => 'GET'], |
|
| 63 | - ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'], |
|
| 64 | - ['name' => 'Svg#getSvg', 'url' => '/svg/{fileName}/{color}', 'verb' => 'GET'], |
|
| 65 | - ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 66 | - ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 67 | - ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'], |
|
| 68 | - ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'], |
|
| 69 | - ['name' => 'WalledGarden#get', 'url' => '/204', 'verb' => 'GET'], |
|
| 70 | - ['name' => 'Search#search', 'url' => '/core/search', 'verb' => 'GET'], |
|
| 71 | - ], |
|
| 72 | - 'ocs' => [ |
|
| 73 | - ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'], |
|
| 74 | - ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'], |
|
| 75 | - ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'], |
|
| 76 | - ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'], |
|
| 77 | - ['root' => '/core', 'name' => 'Navigation#getAppsNavigation', 'url' => '/navigation/apps', 'verb' => 'GET'], |
|
| 78 | - ['root' => '/core', 'name' => 'Navigation#getSettingsNavigation', 'url' => '/navigation/settings', 'verb' => 'GET'], |
|
| 79 | - ['root' => '/core', 'name' => 'AutoComplete#get', 'url' => '/autocomplete/get', 'verb' => 'GET'], |
|
| 80 | - ], |
|
| 39 | + 'routes' => [ |
|
| 40 | + ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'], |
|
| 41 | + ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'], |
|
| 42 | + ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'], |
|
| 43 | + ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'], |
|
| 44 | + ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'], |
|
| 45 | + ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'], |
|
| 46 | + ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'], |
|
| 47 | + ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'], |
|
| 48 | + ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'], |
|
| 49 | + ['name' => 'CSRFToken#index', 'url' => '/csrftoken', 'verb' => 'GET'], |
|
| 50 | + ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'], |
|
| 51 | + ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'], |
|
| 52 | + ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'], |
|
| 53 | + ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'], |
|
| 54 | + ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'], |
|
| 55 | + ['name' => 'ClientFlowLogin#redirectPage', 'url' => '/login/flow/redirect', 'verb' => 'GET'], |
|
| 56 | + ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'], |
|
| 57 | + ['name' => 'ClientFlowLogin#grantPage', 'url' => '/login/flow/grant', 'verb' => 'GET'], |
|
| 58 | + ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'], |
|
| 59 | + ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'], |
|
| 60 | + ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'], |
|
| 61 | + ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'], |
|
| 62 | + ['name' => 'Preview#getPreviewByFileId', 'url' => '/core/preview', 'verb' => 'GET'], |
|
| 63 | + ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'], |
|
| 64 | + ['name' => 'Svg#getSvg', 'url' => '/svg/{fileName}/{color}', 'verb' => 'GET'], |
|
| 65 | + ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 66 | + ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'], |
|
| 67 | + ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'], |
|
| 68 | + ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'], |
|
| 69 | + ['name' => 'WalledGarden#get', 'url' => '/204', 'verb' => 'GET'], |
|
| 70 | + ['name' => 'Search#search', 'url' => '/core/search', 'verb' => 'GET'], |
|
| 71 | + ], |
|
| 72 | + 'ocs' => [ |
|
| 73 | + ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'], |
|
| 74 | + ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'], |
|
| 75 | + ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'], |
|
| 76 | + ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'], |
|
| 77 | + ['root' => '/core', 'name' => 'Navigation#getAppsNavigation', 'url' => '/navigation/apps', 'verb' => 'GET'], |
|
| 78 | + ['root' => '/core', 'name' => 'Navigation#getSettingsNavigation', 'url' => '/navigation/settings', 'verb' => 'GET'], |
|
| 79 | + ['root' => '/core', 'name' => 'AutoComplete#get', 'url' => '/autocomplete/get', 'verb' => 'GET'], |
|
| 80 | + ], |
|
| 81 | 81 | ]); |
| 82 | 82 | |
| 83 | 83 | // Post installation check |
@@ -86,12 +86,12 @@ discard block |
||
| 86 | 86 | // Core ajax actions |
| 87 | 87 | // Routing |
| 88 | 88 | $this->create('core_ajax_update', '/core/ajax/update.php') |
| 89 | - ->actionInclude('core/ajax/update.php'); |
|
| 89 | + ->actionInclude('core/ajax/update.php'); |
|
| 90 | 90 | |
| 91 | 91 | // File routes |
| 92 | 92 | $this->create('files.viewcontroller.showFile', '/f/{fileid}')->action(function($urlParams) { |
| 93 | - $app = new \OCA\Files\AppInfo\Application($urlParams); |
|
| 94 | - $app->dispatch('ViewController', 'index'); |
|
| 93 | + $app = new \OCA\Files\AppInfo\Application($urlParams); |
|
| 94 | + $app->dispatch('ViewController', 'index'); |
|
| 95 | 95 | }); |
| 96 | 96 | |
| 97 | 97 | // Call routes |
@@ -100,52 +100,52 @@ discard block |
||
| 100 | 100 | * @suppress PhanUndeclaredClassMethod |
| 101 | 101 | */ |
| 102 | 102 | $this->create('spreed.pagecontroller.showCall', '/call/{token}')->action(function($urlParams) { |
| 103 | - if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) { |
|
| 104 | - $app = new \OCA\Spreed\AppInfo\Application($urlParams); |
|
| 105 | - $app->dispatch('PageController', 'index'); |
|
| 106 | - } else { |
|
| 107 | - throw new \OC\HintException('App spreed is not enabled'); |
|
| 108 | - } |
|
| 103 | + if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) { |
|
| 104 | + $app = new \OCA\Spreed\AppInfo\Application($urlParams); |
|
| 105 | + $app->dispatch('PageController', 'index'); |
|
| 106 | + } else { |
|
| 107 | + throw new \OC\HintException('App spreed is not enabled'); |
|
| 108 | + } |
|
| 109 | 109 | }); |
| 110 | 110 | |
| 111 | 111 | // Sharing routes |
| 112 | 112 | $this->create('files_sharing.sharecontroller.showShare', '/s/{token}')->action(function($urlParams) { |
| 113 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 114 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 115 | - $app->dispatch('ShareController', 'showShare'); |
|
| 116 | - } else { |
|
| 117 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 118 | - } |
|
| 113 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 114 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 115 | + $app->dispatch('ShareController', 'showShare'); |
|
| 116 | + } else { |
|
| 117 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 118 | + } |
|
| 119 | 119 | }); |
| 120 | 120 | $this->create('files_sharing.sharecontroller.authenticate', '/s/{token}/authenticate/{redirect}')->post()->action(function($urlParams) { |
| 121 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 122 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 123 | - $app->dispatch('ShareController', 'authenticate'); |
|
| 124 | - } else { |
|
| 125 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 126 | - } |
|
| 121 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 122 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 123 | + $app->dispatch('ShareController', 'authenticate'); |
|
| 124 | + } else { |
|
| 125 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 126 | + } |
|
| 127 | 127 | }); |
| 128 | 128 | $this->create('files_sharing.sharecontroller.showAuthenticate', '/s/{token}/authenticate/{redirect}')->get()->action(function($urlParams) { |
| 129 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 130 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 131 | - $app->dispatch('ShareController', 'showAuthenticate'); |
|
| 132 | - } else { |
|
| 133 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 134 | - } |
|
| 129 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 130 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 131 | + $app->dispatch('ShareController', 'showAuthenticate'); |
|
| 132 | + } else { |
|
| 133 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 134 | + } |
|
| 135 | 135 | }); |
| 136 | 136 | $this->create('files_sharing.sharecontroller.downloadShare', '/s/{token}/download')->get()->action(function($urlParams) { |
| 137 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 138 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 139 | - $app->dispatch('ShareController', 'downloadShare'); |
|
| 140 | - } else { |
|
| 141 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 142 | - } |
|
| 137 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 138 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 139 | + $app->dispatch('ShareController', 'downloadShare'); |
|
| 140 | + } else { |
|
| 141 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 142 | + } |
|
| 143 | 143 | }); |
| 144 | 144 | $this->create('files_sharing.publicpreview.directLink', '/s/{token}/preview')->get()->action(function($urlParams) { |
| 145 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 146 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 147 | - $app->dispatch('PublicPreviewController', 'directLink'); |
|
| 148 | - } else { |
|
| 149 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
| 150 | - } |
|
| 145 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
| 146 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
| 147 | + $app->dispatch('PublicPreviewController', 'directLink'); |
|
| 148 | + } else { |
|
| 149 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
| 150 | + } |
|
| 151 | 151 | }); |
@@ -34,64 +34,64 @@ |
||
| 34 | 34 | |
| 35 | 35 | class SvgController extends Controller { |
| 36 | 36 | |
| 37 | - /** @var string */ |
|
| 38 | - protected $serverRoot; |
|
| 39 | - |
|
| 40 | - /** @var ITimeFactory */ |
|
| 41 | - protected $timeFactory; |
|
| 42 | - |
|
| 43 | - public function __construct(string $appName, |
|
| 44 | - IRequest $request, |
|
| 45 | - ITimeFactory $timeFactory) { |
|
| 46 | - parent::__construct($appName, $request); |
|
| 47 | - |
|
| 48 | - $this->serverRoot = \OC::$SERVERROOT; |
|
| 49 | - $this->timeFactory = $timeFactory; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @NoAdminRequired |
|
| 54 | - * @NoCSRFRequired |
|
| 55 | - * |
|
| 56 | - * Generate svg from filename with the requested color |
|
| 57 | - * |
|
| 58 | - * @param string $fileName |
|
| 59 | - * @param string $color |
|
| 60 | - * @return DataDisplayResponse|NotFoundException |
|
| 61 | - */ |
|
| 62 | - public function getSvg(string $fileName, $color = 'ffffff') { |
|
| 63 | - $path = $this->serverRoot . "/core/img/actions/$fileName.svg"; |
|
| 64 | - if (!file_exists($path)) { |
|
| 65 | - return new NotFoundResponse(); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $svg = file_get_contents($path); |
|
| 69 | - |
|
| 70 | - if (is_null($svg)) { |
|
| 71 | - return new NotFoundResponse(); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - // add fill (fill is not present on black elements) |
|
| 75 | - $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#])+)\/>/mi'; |
|
| 76 | - |
|
| 77 | - $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg); |
|
| 78 | - |
|
| 79 | - // replace any fill or stroke colors |
|
| 80 | - $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg); |
|
| 81 | - $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg); |
|
| 82 | - |
|
| 83 | - $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); |
|
| 84 | - |
|
| 85 | - // Set cache control |
|
| 86 | - $ttl = 31536000; |
|
| 87 | - $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable'); |
|
| 88 | - $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"'); |
|
| 89 | - $expires = new \DateTime(); |
|
| 90 | - $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 91 | - $expires->add(new \DateInterval('PT' . $ttl . 'S')); |
|
| 92 | - $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 93 | - $response->addHeader('Pragma', 'cache'); |
|
| 94 | - |
|
| 95 | - return $response; |
|
| 96 | - } |
|
| 37 | + /** @var string */ |
|
| 38 | + protected $serverRoot; |
|
| 39 | + |
|
| 40 | + /** @var ITimeFactory */ |
|
| 41 | + protected $timeFactory; |
|
| 42 | + |
|
| 43 | + public function __construct(string $appName, |
|
| 44 | + IRequest $request, |
|
| 45 | + ITimeFactory $timeFactory) { |
|
| 46 | + parent::__construct($appName, $request); |
|
| 47 | + |
|
| 48 | + $this->serverRoot = \OC::$SERVERROOT; |
|
| 49 | + $this->timeFactory = $timeFactory; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @NoAdminRequired |
|
| 54 | + * @NoCSRFRequired |
|
| 55 | + * |
|
| 56 | + * Generate svg from filename with the requested color |
|
| 57 | + * |
|
| 58 | + * @param string $fileName |
|
| 59 | + * @param string $color |
|
| 60 | + * @return DataDisplayResponse|NotFoundException |
|
| 61 | + */ |
|
| 62 | + public function getSvg(string $fileName, $color = 'ffffff') { |
|
| 63 | + $path = $this->serverRoot . "/core/img/actions/$fileName.svg"; |
|
| 64 | + if (!file_exists($path)) { |
|
| 65 | + return new NotFoundResponse(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $svg = file_get_contents($path); |
|
| 69 | + |
|
| 70 | + if (is_null($svg)) { |
|
| 71 | + return new NotFoundResponse(); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + // add fill (fill is not present on black elements) |
|
| 75 | + $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#])+)\/>/mi'; |
|
| 76 | + |
|
| 77 | + $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg); |
|
| 78 | + |
|
| 79 | + // replace any fill or stroke colors |
|
| 80 | + $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg); |
|
| 81 | + $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg); |
|
| 82 | + |
|
| 83 | + $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); |
|
| 84 | + |
|
| 85 | + // Set cache control |
|
| 86 | + $ttl = 31536000; |
|
| 87 | + $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable'); |
|
| 88 | + $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"'); |
|
| 89 | + $expires = new \DateTime(); |
|
| 90 | + $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 91 | + $expires->add(new \DateInterval('PT' . $ttl . 'S')); |
|
| 92 | + $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 93 | + $response->addHeader('Pragma', 'cache'); |
|
| 94 | + |
|
| 95 | + return $response; |
|
| 96 | + } |
|
| 97 | 97 | } |
| 98 | 98 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare (strict_types = 1); |
|
| 2 | +declare(strict_types=1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2018, John Molakvoæ ([email protected]) |
| 5 | 5 | * |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | * @return DataDisplayResponse|NotFoundException |
| 61 | 61 | */ |
| 62 | 62 | public function getSvg(string $fileName, $color = 'ffffff') { |
| 63 | - $path = $this->serverRoot . "/core/img/actions/$fileName.svg"; |
|
| 63 | + $path = $this->serverRoot."/core/img/actions/$fileName.svg"; |
|
| 64 | 64 | if (!file_exists($path)) { |
| 65 | 65 | return new NotFoundResponse(); |
| 66 | 66 | } |
@@ -74,21 +74,21 @@ discard block |
||
| 74 | 74 | // add fill (fill is not present on black elements) |
| 75 | 75 | $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#])+)\/>/mi'; |
| 76 | 76 | |
| 77 | - $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg); |
|
| 77 | + $svg = preg_replace($fillRe, '<$1 fill="#'.$color.'"/>', $svg); |
|
| 78 | 78 | |
| 79 | 79 | // replace any fill or stroke colors |
| 80 | - $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg); |
|
| 81 | - $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg); |
|
| 80 | + $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#'.$color.'"', $svg); |
|
| 81 | + $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#'.$color.'"', $svg); |
|
| 82 | 82 | |
| 83 | 83 | $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); |
| 84 | 84 | |
| 85 | 85 | // Set cache control |
| 86 | 86 | $ttl = 31536000; |
| 87 | - $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable'); |
|
| 88 | - $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"'); |
|
| 87 | + $response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable'); |
|
| 88 | + $response->addHeader('Content-Disposition', 'inline; filename="'.$fileName.'.svg"'); |
|
| 89 | 89 | $expires = new \DateTime(); |
| 90 | 90 | $expires->setTimestamp($this->timeFactory->getTime()); |
| 91 | - $expires->add(new \DateInterval('PT' . $ttl . 'S')); |
|
| 91 | + $expires->add(new \DateInterval('PT'.$ttl.'S')); |
|
| 92 | 92 | $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
| 93 | 93 | $response->addHeader('Pragma', 'cache'); |
| 94 | 94 | |
@@ -40,73 +40,73 @@ |
||
| 40 | 40 | |
| 41 | 41 | class JsController extends Controller { |
| 42 | 42 | |
| 43 | - /** @var IAppData */ |
|
| 44 | - protected $appData; |
|
| 43 | + /** @var IAppData */ |
|
| 44 | + protected $appData; |
|
| 45 | 45 | |
| 46 | - /** @var ITimeFactory */ |
|
| 47 | - protected $timeFactory; |
|
| 46 | + /** @var ITimeFactory */ |
|
| 47 | + protected $timeFactory; |
|
| 48 | 48 | |
| 49 | - public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) { |
|
| 50 | - parent::__construct($appName, $request); |
|
| 49 | + public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) { |
|
| 50 | + parent::__construct($appName, $request); |
|
| 51 | 51 | |
| 52 | - $this->appData = $appDataFactory->get('js'); |
|
| 53 | - $this->timeFactory = $timeFactory; |
|
| 54 | - } |
|
| 52 | + $this->appData = $appDataFactory->get('js'); |
|
| 53 | + $this->timeFactory = $timeFactory; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @PublicPage |
|
| 58 | - * @NoCSRFRequired |
|
| 59 | - * |
|
| 60 | - * @param string $fileName js filename with extension |
|
| 61 | - * @param string $appName js folder name |
|
| 62 | - * @return FileDisplayResponse|NotFoundResponse |
|
| 63 | - */ |
|
| 64 | - public function getJs(string $fileName, string $appName): Response { |
|
| 65 | - try { |
|
| 66 | - $folder = $this->appData->getFolder($appName); |
|
| 67 | - $gzip = false; |
|
| 68 | - $file = $this->getFile($folder, $fileName, $gzip); |
|
| 69 | - } catch(NotFoundException $e) { |
|
| 70 | - return new NotFoundResponse(); |
|
| 71 | - } |
|
| 56 | + /** |
|
| 57 | + * @PublicPage |
|
| 58 | + * @NoCSRFRequired |
|
| 59 | + * |
|
| 60 | + * @param string $fileName js filename with extension |
|
| 61 | + * @param string $appName js folder name |
|
| 62 | + * @return FileDisplayResponse|NotFoundResponse |
|
| 63 | + */ |
|
| 64 | + public function getJs(string $fileName, string $appName): Response { |
|
| 65 | + try { |
|
| 66 | + $folder = $this->appData->getFolder($appName); |
|
| 67 | + $gzip = false; |
|
| 68 | + $file = $this->getFile($folder, $fileName, $gzip); |
|
| 69 | + } catch(NotFoundException $e) { |
|
| 70 | + return new NotFoundResponse(); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']); |
|
| 74 | - if ($gzip) { |
|
| 75 | - $response->addHeader('Content-Encoding', 'gzip'); |
|
| 76 | - } |
|
| 73 | + $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']); |
|
| 74 | + if ($gzip) { |
|
| 75 | + $response->addHeader('Content-Encoding', 'gzip'); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - $ttl = 31536000; |
|
| 79 | - $response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable'); |
|
| 78 | + $ttl = 31536000; |
|
| 79 | + $response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable'); |
|
| 80 | 80 | |
| 81 | - $expires = new \DateTime(); |
|
| 82 | - $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 83 | - $expires->add(new \DateInterval('PT'.$ttl.'S')); |
|
| 84 | - $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 85 | - $response->addHeader('Pragma', 'cache'); |
|
| 86 | - return $response; |
|
| 87 | - } |
|
| 81 | + $expires = new \DateTime(); |
|
| 82 | + $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 83 | + $expires->add(new \DateInterval('PT'.$ttl.'S')); |
|
| 84 | + $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 85 | + $response->addHeader('Pragma', 'cache'); |
|
| 86 | + return $response; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @param ISimpleFolder $folder |
|
| 91 | - * @param string $fileName |
|
| 92 | - * @param bool $gzip is set to true if we use the gzip file |
|
| 93 | - * @return ISimpleFile |
|
| 94 | - * |
|
| 95 | - * @throws NotFoundException |
|
| 96 | - */ |
|
| 97 | - private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile { |
|
| 98 | - $encoding = $this->request->getHeader('Accept-Encoding'); |
|
| 89 | + /** |
|
| 90 | + * @param ISimpleFolder $folder |
|
| 91 | + * @param string $fileName |
|
| 92 | + * @param bool $gzip is set to true if we use the gzip file |
|
| 93 | + * @return ISimpleFile |
|
| 94 | + * |
|
| 95 | + * @throws NotFoundException |
|
| 96 | + */ |
|
| 97 | + private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile { |
|
| 98 | + $encoding = $this->request->getHeader('Accept-Encoding'); |
|
| 99 | 99 | |
| 100 | - if (strpos($encoding, 'gzip') !== false) { |
|
| 101 | - try { |
|
| 102 | - $gzip = true; |
|
| 103 | - return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
| 104 | - } catch (NotFoundException $e) { |
|
| 105 | - // continue |
|
| 106 | - } |
|
| 107 | - } |
|
| 100 | + if (strpos($encoding, 'gzip') !== false) { |
|
| 101 | + try { |
|
| 102 | + $gzip = true; |
|
| 103 | + return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz |
|
| 104 | + } catch (NotFoundException $e) { |
|
| 105 | + // continue |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - $gzip = false; |
|
| 110 | - return $folder->getFile($fileName); |
|
| 111 | - } |
|
| 109 | + $gzip = false; |
|
| 110 | + return $folder->getFile($fileName); |
|
| 111 | + } |
|
| 112 | 112 | } |