2016-08-12 00:25:41 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Julius Haertl <jus@bitgrid.net>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-08-12 00:25:41 +03:00
|
|
|
* @author Julius Haertl <jus@bitgrid.net>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Michael Weimann <mail@michael-weimann.eu>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-08-12 00:25:41 +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/>.
|
2016-08-12 00:25:41 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-08-12 00:25:41 +03:00
|
|
|
namespace OCA\Theming\Controller;
|
|
|
|
|
2017-05-15 00:48:51 +03:00
|
|
|
use OC\IntegrityCheck\Helpers\FileAccessHelper;
|
2016-08-30 10:03:06 +03:00
|
|
|
use OCA\Theming\IconBuilder;
|
2016-09-18 21:15:06 +03:00
|
|
|
use OCA\Theming\ImageManager;
|
2017-04-20 14:10:34 +03:00
|
|
|
use OCA\Theming\ThemingDefaults;
|
2016-08-12 00:25:41 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
2017-05-15 00:48:51 +03:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2018-02-26 15:54:00 +03:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2016-10-17 17:31:07 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-08-12 00:25:41 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class IconController extends Controller {
|
2017-04-20 14:10:34 +03:00
|
|
|
/** @var ThemingDefaults */
|
2016-08-25 17:18:54 +03:00
|
|
|
private $themingDefaults;
|
2016-08-30 10:03:06 +03:00
|
|
|
/** @var IconBuilder */
|
|
|
|
private $iconBuilder;
|
2016-09-18 21:15:06 +03:00
|
|
|
/** @var ImageManager */
|
|
|
|
private $imageManager;
|
2017-05-18 11:45:42 +03:00
|
|
|
/** @var FileAccessHelper */
|
|
|
|
private $fileAccessHelper;
|
2016-08-29 12:53:44 +03:00
|
|
|
|
2016-08-12 00:25:41 +03:00
|
|
|
/**
|
|
|
|
* IconController constructor.
|
|
|
|
*
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
2017-04-20 14:10:34 +03:00
|
|
|
* @param ThemingDefaults $themingDefaults
|
2016-08-30 12:51:48 +03:00
|
|
|
* @param IconBuilder $iconBuilder
|
2016-09-18 21:15:06 +03:00
|
|
|
* @param ImageManager $imageManager
|
2018-02-26 15:54:00 +03:00
|
|
|
* @param FileAccessHelper $fileAccessHelper
|
2016-08-12 00:25:41 +03:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$appName,
|
|
|
|
IRequest $request,
|
2017-04-20 14:10:34 +03:00
|
|
|
ThemingDefaults $themingDefaults,
|
2016-09-18 21:15:06 +03:00
|
|
|
IconBuilder $iconBuilder,
|
2017-05-18 11:45:42 +03:00
|
|
|
ImageManager $imageManager,
|
|
|
|
FileAccessHelper $fileAccessHelper
|
2016-08-12 00:25:41 +03:00
|
|
|
) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2016-08-25 17:18:54 +03:00
|
|
|
$this->themingDefaults = $themingDefaults;
|
2016-08-30 10:03:42 +03:00
|
|
|
$this->iconBuilder = $iconBuilder;
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->imageManager = $imageManager;
|
2017-05-18 11:45:42 +03:00
|
|
|
$this->fileAccessHelper = $fileAccessHelper;
|
2016-08-12 00:25:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2016-08-30 12:51:48 +03:00
|
|
|
* @param $app string app name
|
|
|
|
* @param $image string image file name (svg required)
|
2016-11-05 22:11:49 +03:00
|
|
|
* @return FileDisplayResponse|NotFoundResponse
|
2018-02-26 15:54:00 +03:00
|
|
|
* @throws \Exception
|
2016-08-12 00:25:41 +03:00
|
|
|
*/
|
2018-02-26 15:54:00 +03:00
|
|
|
public function getThemedIcon(string $app, string $image): Response {
|
2016-10-17 17:31:07 +03:00
|
|
|
try {
|
2018-02-26 15:54:00 +03:00
|
|
|
$iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image));
|
2016-10-17 17:31:07 +03:00
|
|
|
} catch (NotFoundException $exception) {
|
2016-09-18 21:15:06 +03:00
|
|
|
$icon = $this->iconBuilder->colorSvg($app, $image);
|
2018-02-26 15:54:00 +03:00
|
|
|
if ($icon === false || $icon === '') {
|
2016-12-09 14:44:01 +03:00
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
2018-02-26 15:54:00 +03:00
|
|
|
$iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
|
2016-09-18 21:15:06 +03:00
|
|
|
}
|
2016-11-05 22:11:49 +03:00
|
|
|
if ($iconFile !== false) {
|
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
|
|
|
$response->cacheFor(86400);
|
|
|
|
return $response;
|
|
|
|
}
|
2018-02-26 15:54:00 +03:00
|
|
|
|
|
|
|
return new NotFoundResponse();
|
2016-08-12 00:25:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 32x32 favicon as png
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2016-08-30 12:51:48 +03:00
|
|
|
* @param $app string app name
|
2017-05-15 00:48:51 +03:00
|
|
|
* @return FileDisplayResponse|DataDisplayResponse
|
2018-02-26 15:54:00 +03:00
|
|
|
* @throws \Exception
|
2016-08-12 00:25:41 +03:00
|
|
|
*/
|
2018-02-26 15:54:00 +03:00
|
|
|
public function getFavicon(string $app = 'core'): Response {
|
2017-05-15 00:48:51 +03:00
|
|
|
$response = null;
|
2018-02-26 15:54:00 +03:00
|
|
|
$iconFile = null;
|
|
|
|
try {
|
2018-09-02 15:17:17 +03:00
|
|
|
$iconFile = $this->imageManager->getImage('favicon', false);
|
2018-02-26 15:54:00 +03:00
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
}
|
2018-06-05 17:59:05 +03:00
|
|
|
if ($iconFile === null && $this->imageManager->shouldReplaceIcons()) {
|
2016-10-17 17:31:07 +03:00
|
|
|
try {
|
|
|
|
$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
|
|
|
|
} catch (NotFoundException $exception) {
|
2016-10-14 15:57:58 +03:00
|
|
|
$icon = $this->iconBuilder->getFavicon($app);
|
|
|
|
$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
|
|
|
|
}
|
2016-11-05 22:11:49 +03:00
|
|
|
if ($iconFile !== false) {
|
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
|
|
|
}
|
2016-08-30 10:03:06 +03:00
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($response === null) {
|
2017-05-15 00:48:51 +03:00
|
|
|
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
|
2017-05-18 11:45:42 +03:00
|
|
|
$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
2017-05-15 00:48:51 +03:00
|
|
|
}
|
|
|
|
$response->cacheFor(86400);
|
|
|
|
return $response;
|
2016-08-12 00:25:41 +03:00
|
|
|
}
|
|
|
|
|
2016-08-14 13:24:51 +03:00
|
|
|
/**
|
|
|
|
* Return a 512x512 icon for touch devices
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2016-08-30 12:51:48 +03:00
|
|
|
* @param $app string app name
|
2016-11-05 22:11:49 +03:00
|
|
|
* @return FileDisplayResponse|NotFoundResponse
|
2018-02-26 15:54:00 +03:00
|
|
|
* @throws \Exception
|
2016-08-14 13:24:51 +03:00
|
|
|
*/
|
2018-02-26 15:54:00 +03:00
|
|
|
public function getTouchIcon(string $app = 'core'): Response {
|
2017-05-15 00:48:51 +03:00
|
|
|
$response = null;
|
2018-02-26 15:54:00 +03:00
|
|
|
try {
|
|
|
|
$iconFile = $this->imageManager->getImage('favicon');
|
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
}
|
2018-06-05 17:59:05 +03:00
|
|
|
if ($this->imageManager->shouldReplaceIcons()) {
|
2016-10-17 17:31:07 +03:00
|
|
|
try {
|
|
|
|
$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
|
|
|
|
} catch (NotFoundException $exception) {
|
2016-10-14 15:57:58 +03:00
|
|
|
$icon = $this->iconBuilder->getTouchIcon($app);
|
|
|
|
$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
|
|
|
|
}
|
2016-11-05 22:11:49 +03:00
|
|
|
if ($iconFile !== false) {
|
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
|
|
|
|
}
|
2016-08-12 00:25:41 +03:00
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($response === null) {
|
2017-05-15 00:48:51 +03:00
|
|
|
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
|
2017-05-18 11:45:42 +03:00
|
|
|
$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
|
2017-05-15 00:48:51 +03:00
|
|
|
}
|
|
|
|
$response->cacheFor(86400);
|
|
|
|
return $response;
|
2016-08-12 00:25:41 +03:00
|
|
|
}
|
2016-10-17 17:31:07 +03:00
|
|
|
}
|