SvgController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare (strict_types = 1);
  3. /**
  4. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
  5. *
  6. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Controller;
  25. use OCP\AppFramework\Controller;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataDisplayResponse;
  28. use OCP\AppFramework\Http\NotFoundResponse;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\Files\NotFoundException;
  31. use OCP\App\IAppManager;
  32. use OCP\IRequest;
  33. class SvgController extends Controller {
  34. /** @var string */
  35. protected $serverRoot;
  36. /** @var ITimeFactory */
  37. protected $timeFactory;
  38. /** @var IAppManager */
  39. protected $appManager;
  40. public function __construct(string $appName,
  41. IRequest $request,
  42. ITimeFactory $timeFactory,
  43. IAppManager $appManager) {
  44. parent::__construct($appName, $request);
  45. $this->serverRoot = \OC::$SERVERROOT;
  46. $this->timeFactory = $timeFactory;
  47. $this->appManager = $appManager;
  48. }
  49. /**
  50. * @PublicPage
  51. * @NoCSRFRequired
  52. *
  53. * Generate svg from filename with the requested color
  54. *
  55. * @param string $folder
  56. * @param string $fileName
  57. * @param string $color
  58. * @return DataDisplayResponse|NotFoundResponse
  59. */
  60. public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') {
  61. $path = $this->serverRoot . "/core/img/$folder/$fileName.svg";
  62. return $this->getSvg($path, $color, $fileName);
  63. }
  64. /**
  65. * @PublicPage
  66. * @NoCSRFRequired
  67. *
  68. * Generate svg from filename with the requested color
  69. *
  70. * @param string $app
  71. * @param string $fileName
  72. * @param string $color
  73. * @return DataDisplayResponse|NotFoundResponse
  74. */
  75. public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') {
  76. if ($app === 'settings') {
  77. $path = $this->serverRoot . "/settings/img/$fileName.svg";
  78. return $this->getSvg($path, $color, $fileName);
  79. }
  80. $appRootPath = $this->appManager->getAppPath($app);
  81. $appPath = substr($appRootPath, strlen($this->serverRoot));
  82. if (!$appPath) {
  83. return new NotFoundResponse();
  84. }
  85. $path = $this->serverRoot . $appPath ."/img/$fileName.svg";
  86. return $this->getSvg($path, $color, $fileName);
  87. }
  88. /**
  89. * Generate svg from filename with the requested color
  90. *
  91. * @param string $path
  92. * @param string $color
  93. * @return DataDisplayResponse|NotFoundResponse
  94. */
  95. private function getSvg(string $path, string $color, string $fileName) {
  96. if (!file_exists($path)) {
  97. return new NotFoundResponse();
  98. }
  99. $svg = file_get_contents($path);
  100. if (is_null($svg)) {
  101. return new NotFoundResponse();
  102. }
  103. // add fill (fill is not present on black elements)
  104. $fillRe = '/<((circle|rect|path)((?!fill)[a-z0-9 =".\-#():;])+)\/>/mi';
  105. $svg = preg_replace($fillRe, '<$1 fill="#' . $color . '"/>', $svg);
  106. // replace any fill or stroke colors
  107. $svg = preg_replace('/stroke="#([a-z0-9]{3,6})"/mi', 'stroke="#' . $color . '"', $svg);
  108. $svg = preg_replace('/fill="#([a-z0-9]{3,6})"/mi', 'fill="#' . $color . '"', $svg);
  109. $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  110. // Set cache control
  111. $ttl = 31536000;
  112. $response->cacheFor($ttl);
  113. $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"');
  114. $expires = new \DateTime();
  115. $expires->setTimestamp($this->timeFactory->getTime());
  116. $expires->add(new \DateInterval('PT' . $ttl . 'S'));
  117. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  118. $response->addHeader('Pragma', 'cache');
  119. return $response;
  120. }
  121. }