2018-06-25 12:17:00 +03:00
|
|
|
<?php
|
|
|
|
declare (strict_types = 1);
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
|
|
|
|
*
|
|
|
|
* @author John Molakvoæ (skjnldsv) <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\Core\Controller;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\Files\NotFoundException;
|
2018-07-19 23:46:32 +03:00
|
|
|
use OCP\App\IAppManager;
|
2018-06-25 12:17:00 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class SvgController extends Controller {
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $serverRoot;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
protected $timeFactory;
|
|
|
|
|
2018-07-19 23:46:32 +03:00
|
|
|
/** @var IAppManager */
|
|
|
|
protected $appManager;
|
|
|
|
|
2018-06-25 12:17:00 +03:00
|
|
|
public function __construct(string $appName,
|
|
|
|
IRequest $request,
|
2018-07-19 23:46:32 +03:00
|
|
|
ITimeFactory $timeFactory,
|
|
|
|
IAppManager $appManager) {
|
2018-06-25 12:17:00 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
|
|
|
$this->serverRoot = \OC::$SERVERROOT;
|
|
|
|
$this->timeFactory = $timeFactory;
|
2018-07-19 23:46:32 +03:00
|
|
|
$this->appManager = $appManager;
|
2018-06-25 12:17:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-25 13:05:18 +03:00
|
|
|
* @PublicPage
|
2018-06-25 12:17:00 +03:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Generate svg from filename with the requested color
|
|
|
|
*
|
2018-06-29 17:10:53 +03:00
|
|
|
* @param string $folder
|
2018-06-25 12:17:00 +03:00
|
|
|
* @param string $fileName
|
|
|
|
* @param string $color
|
2018-07-19 23:46:32 +03:00
|
|
|
* @return DataDisplayResponse|NotFoundResponse
|
2018-06-25 12:17:00 +03:00
|
|
|
*/
|
2018-06-29 17:10:53 +03:00
|
|
|
public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') {
|
|
|
|
$path = $this->serverRoot . "/core/img/$folder/$fileName.svg";
|
2018-07-04 11:04:38 +03:00
|
|
|
return $this->getSvg($path, $color, $fileName);
|
2018-06-26 11:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-25 13:05:18 +03:00
|
|
|
* @PublicPage
|
2018-06-26 11:10:49 +03:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Generate svg from filename with the requested color
|
|
|
|
*
|
2018-06-29 17:10:53 +03:00
|
|
|
* @param string $app
|
2018-06-26 11:10:49 +03:00
|
|
|
* @param string $fileName
|
|
|
|
* @param string $color
|
2018-07-19 23:46:32 +03:00
|
|
|
* @return DataDisplayResponse|NotFoundResponse
|
2018-06-26 11:10:49 +03:00
|
|
|
*/
|
|
|
|
public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') {
|
2018-06-29 17:10:53 +03:00
|
|
|
|
|
|
|
if ($app === 'settings') {
|
|
|
|
$path = $this->serverRoot . "/settings/img/$fileName.svg";
|
2018-07-18 20:06:06 +03:00
|
|
|
return $this->getSvg($path, $color, $fileName);
|
2018-06-29 17:10:53 +03:00
|
|
|
}
|
|
|
|
|
2018-07-19 23:46:32 +03:00
|
|
|
$appRootPath = $this->appManager->getAppPath($app);
|
|
|
|
$appPath = substr($appRootPath, strlen($this->serverRoot));
|
|
|
|
|
2018-06-26 11:10:49 +03:00
|
|
|
if (!$appPath) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
$path = $this->serverRoot . $appPath ."/img/$fileName.svg";
|
2018-07-04 11:04:38 +03:00
|
|
|
return $this->getSvg($path, $color, $fileName);
|
2018-06-26 11:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate svg from filename with the requested color
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $color
|
2018-07-19 23:46:32 +03:00
|
|
|
* @return DataDisplayResponse|NotFoundResponse
|
2018-06-26 11:10:49 +03:00
|
|
|
*/
|
2018-07-04 11:04:38 +03:00
|
|
|
private function getSvg(string $path, string $color, string $fileName) {
|
2018-06-25 12:17:00 +03:00
|
|
|
if (!file_exists($path)) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$svg = file_get_contents($path);
|
|
|
|
|
|
|
|
if (is_null($svg)) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add fill (fill is not present on black elements)
|
2018-07-09 12:43:06 +03:00
|
|
|
$fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi';
|
2018-06-25 12:17:00 +03:00
|
|
|
|
|
|
|
$svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
|
|
|
|
|
|
|
|
// replace any fill or stroke colors
|
|
|
|
$svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
|
|
|
|
$svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
|
|
|
|
|
|
|
|
$response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
|
|
|
|
|
|
|
// Set cache control
|
|
|
|
$ttl = 31536000;
|
2018-06-29 17:12:47 +03:00
|
|
|
$response->cacheFor($ttl);
|
2018-06-25 12:17:00 +03:00
|
|
|
$response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"');
|
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT' . $ttl . 'S'));
|
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
|
|
|
|
$response->addHeader('Pragma', 'cache');
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2018-07-25 13:05:18 +03:00
|
|
|
}
|