2016-08-11 21:25:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Julius Haertl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @author Julius Haertl <jus@bitgrid.net>
|
|
|
|
*
|
|
|
|
* @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 OCA\Theming\Controller;
|
|
|
|
|
2016-08-30 07:03:06 +00:00
|
|
|
use OCA\Theming\IconBuilder;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCA\Theming\ImageManager;
|
2016-08-25 14:18:54 +00:00
|
|
|
use OCA\Theming\ThemingDefaults;
|
2016-08-11 21:25:41 +00:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
2016-11-04 17:55:00 +00:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2016-08-11 21:25:41 +00:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2016-08-11 21:25:41 +00:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-10-17 14:31:07 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-08-11 21:25:41 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCA\Theming\Util;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCP\IConfig;
|
2016-08-11 21:25:41 +00:00
|
|
|
|
|
|
|
class IconController extends Controller {
|
2016-08-25 14:18:54 +00:00
|
|
|
/** @var ThemingDefaults */
|
|
|
|
private $themingDefaults;
|
2016-08-11 21:25:41 +00:00
|
|
|
/** @var Util */
|
|
|
|
private $util;
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $timeFactory;
|
2016-09-18 18:15:06 +00:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2016-08-30 07:03:06 +00:00
|
|
|
/** @var IconBuilder */
|
|
|
|
private $iconBuilder;
|
2016-09-18 18:15:06 +00:00
|
|
|
/** @var ImageManager */
|
|
|
|
private $imageManager;
|
2016-08-29 09:53:44 +00:00
|
|
|
|
2016-08-11 21:25:41 +00:00
|
|
|
/**
|
|
|
|
* IconController constructor.
|
|
|
|
*
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
2016-08-25 14:18:54 +00:00
|
|
|
* @param ThemingDefaults $themingDefaults
|
2016-08-11 21:25:41 +00:00
|
|
|
* @param Util $util
|
|
|
|
* @param ITimeFactory $timeFactory
|
2016-09-18 18:15:06 +00:00
|
|
|
* @param IConfig $config
|
2016-08-30 09:51:48 +00:00
|
|
|
* @param IconBuilder $iconBuilder
|
2016-09-18 18:15:06 +00:00
|
|
|
* @param ImageManager $imageManager
|
2016-08-11 21:25:41 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$appName,
|
|
|
|
IRequest $request,
|
2016-08-25 14:18:54 +00:00
|
|
|
ThemingDefaults $themingDefaults,
|
2016-08-11 21:25:41 +00:00
|
|
|
Util $util,
|
|
|
|
ITimeFactory $timeFactory,
|
2016-09-18 18:15:06 +00:00
|
|
|
IConfig $config,
|
|
|
|
IconBuilder $iconBuilder,
|
|
|
|
ImageManager $imageManager
|
2016-08-11 21:25:41 +00:00
|
|
|
) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2016-08-25 14:18:54 +00:00
|
|
|
$this->themingDefaults = $themingDefaults;
|
2016-08-11 21:25:41 +00:00
|
|
|
$this->util = $util;
|
|
|
|
$this->timeFactory = $timeFactory;
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->config = $config;
|
2016-08-30 07:03:42 +00:00
|
|
|
$this->iconBuilder = $iconBuilder;
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->imageManager = $imageManager;
|
2016-08-11 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2016-08-30 09:51:48 +00:00
|
|
|
* @param $app string app name
|
|
|
|
* @param $image string image file name (svg required)
|
2016-09-18 18:15:06 +00:00
|
|
|
* @return FileDisplayResponse
|
2016-08-11 21:25:41 +00:00
|
|
|
*/
|
|
|
|
public function getThemedIcon($app, $image) {
|
2016-10-17 14:31:07 +00:00
|
|
|
try {
|
|
|
|
$iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image));
|
|
|
|
} catch (NotFoundException $exception) {
|
2016-09-18 18:15:06 +00:00
|
|
|
$icon = $this->iconBuilder->colorSvg($app, $image);
|
|
|
|
$iconFile = $this->imageManager->setCachedImage("icon-" . $app . '-' . str_replace("/","_",$image), $icon);
|
|
|
|
}
|
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
2016-08-16 08:30:56 +00:00
|
|
|
$response->cacheFor(86400);
|
2016-10-14 12:57:58 +00:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2016-08-30 07:03:06 +00:00
|
|
|
$response->addHeader('Pragma', 'cache');
|
2016-08-16 08:30:56 +00:00
|
|
|
return $response;
|
2016-08-11 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a 32x32 favicon as png
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2016-08-30 09:51:48 +00:00
|
|
|
* @param $app string app name
|
2016-09-18 18:15:06 +00:00
|
|
|
* @return FileDisplayResponse|DataDisplayResponse
|
2016-08-11 21:25:41 +00:00
|
|
|
*/
|
2016-09-18 18:15:06 +00:00
|
|
|
public function getFavicon($app = "core") {
|
|
|
|
if ($this->themingDefaults->shouldReplaceIcons()) {
|
2016-10-17 14:31:07 +00:00
|
|
|
try {
|
|
|
|
$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
|
|
|
|
} catch (NotFoundException $exception) {
|
2016-10-14 12:57:58 +00:00
|
|
|
$icon = $this->iconBuilder->getFavicon($app);
|
|
|
|
$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
|
|
|
|
}
|
2016-09-18 18:15:06 +00:00
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
2016-10-14 12:57:58 +00:00
|
|
|
$response->cacheFor(86400);
|
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
|
|
|
$response->addHeader('Pragma', 'cache');
|
2016-08-30 07:03:06 +00:00
|
|
|
} else {
|
2016-11-04 17:55:00 +00:00
|
|
|
$response = new Response();
|
|
|
|
$response->setStatus(Http::STATUS_NOT_FOUND);
|
2016-10-14 12:57:58 +00:00
|
|
|
$response->cacheFor(0);
|
|
|
|
$response->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
|
2016-08-30 07:03:06 +00:00
|
|
|
}
|
2016-08-30 07:03:42 +00:00
|
|
|
return $response;
|
2016-08-11 21:25:41 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 10:24:51 +00:00
|
|
|
/**
|
|
|
|
* Return a 512x512 icon for touch devices
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2016-08-30 09:51:48 +00:00
|
|
|
* @param $app string app name
|
2016-09-18 18:15:06 +00:00
|
|
|
* @return FileDisplayResponse|DataDisplayResponse
|
2016-08-14 10:24:51 +00:00
|
|
|
*/
|
2016-09-18 18:15:06 +00:00
|
|
|
public function getTouchIcon($app = "core") {
|
|
|
|
if ($this->themingDefaults->shouldReplaceIcons()) {
|
2016-10-17 14:31:07 +00:00
|
|
|
try {
|
|
|
|
$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
|
|
|
|
} catch (NotFoundException $exception) {
|
2016-10-14 12:57:58 +00:00
|
|
|
$icon = $this->iconBuilder->getTouchIcon($app);
|
|
|
|
$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
|
|
|
|
}
|
2016-09-18 18:15:06 +00:00
|
|
|
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
|
2016-10-14 12:57:58 +00:00
|
|
|
$response->cacheFor(86400);
|
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
|
|
|
$response->addHeader('Pragma', 'cache');
|
2016-08-14 10:24:51 +00:00
|
|
|
} else {
|
2016-11-04 17:55:00 +00:00
|
|
|
$response = new Response();
|
|
|
|
$response->setStatus(Http::STATUS_NOT_FOUND);
|
2016-10-14 12:57:58 +00:00
|
|
|
$response->cacheFor(0);
|
|
|
|
$response->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
|
2016-08-11 21:25:41 +00:00
|
|
|
}
|
2016-08-30 07:03:42 +00:00
|
|
|
return $response;
|
2016-08-11 21:25:41 +00:00
|
|
|
}
|
2016-10-17 14:31:07 +00:00
|
|
|
}
|