1
0

IconController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Haertl <jus@bitgrid.net>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Haertl <jus@bitgrid.net>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Michael Weimann <mail@michael-weimann.eu>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Theming\Controller;
  29. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  30. use OCA\Theming\IconBuilder;
  31. use OCA\Theming\ImageManager;
  32. use OCA\Theming\ThemingDefaults;
  33. use OCP\App\IAppManager;
  34. use OCP\AppFramework\Controller;
  35. use OCP\AppFramework\Http;
  36. use OCP\AppFramework\Http\DataDisplayResponse;
  37. use OCP\AppFramework\Http\FileDisplayResponse;
  38. use OCP\AppFramework\Http\NotFoundResponse;
  39. use OCP\AppFramework\Http\Response;
  40. use OCP\Files\NotFoundException;
  41. use OCP\IRequest;
  42. class IconController extends Controller {
  43. /** @var ThemingDefaults */
  44. private $themingDefaults;
  45. /** @var IconBuilder */
  46. private $iconBuilder;
  47. /** @var ImageManager */
  48. private $imageManager;
  49. /** @var FileAccessHelper */
  50. private $fileAccessHelper;
  51. /** @var IAppManager */
  52. private $appManager;
  53. public function __construct(
  54. $appName,
  55. IRequest $request,
  56. ThemingDefaults $themingDefaults,
  57. IconBuilder $iconBuilder,
  58. ImageManager $imageManager,
  59. FileAccessHelper $fileAccessHelper,
  60. IAppManager $appManager
  61. ) {
  62. parent::__construct($appName, $request);
  63. $this->themingDefaults = $themingDefaults;
  64. $this->iconBuilder = $iconBuilder;
  65. $this->imageManager = $imageManager;
  66. $this->fileAccessHelper = $fileAccessHelper;
  67. $this->appManager = $appManager;
  68. }
  69. /**
  70. * @PublicPage
  71. * @NoCSRFRequired
  72. *
  73. * @param $app string app name
  74. * @param $image string image file name (svg required)
  75. * @return FileDisplayResponse|NotFoundResponse
  76. * @throws \Exception
  77. */
  78. public function getThemedIcon(string $app, string $image): Response {
  79. if ($app !== 'core' && !$this->appManager->isEnabledForUser($app)) {
  80. $app = 'core';
  81. $image = 'favicon.png';
  82. }
  83. $color = $this->themingDefaults->getColorPrimary();
  84. try {
  85. $iconFileName = $this->imageManager->getCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image));
  86. } catch (NotFoundException $exception) {
  87. $icon = $this->iconBuilder->colorSvg($app, $image);
  88. if ($icon === false || $icon === '') {
  89. return new NotFoundResponse();
  90. }
  91. $iconFileName = $this->imageManager->setCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image), $icon);
  92. }
  93. $response = new FileDisplayResponse($iconFileName, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  94. $response->cacheFor(86400, false, true);
  95. return $response;
  96. }
  97. /**
  98. * Return a 32x32 favicon as png
  99. *
  100. * @PublicPage
  101. * @NoCSRFRequired
  102. *
  103. * @param $app string app name
  104. * @return FileDisplayResponse|DataDisplayResponse|NotFoundResponse
  105. * @throws \Exception
  106. */
  107. public function getFavicon(string $app = 'core'): Response {
  108. if ($app !== 'core' && !$this->appManager->isEnabledForUser($app)) {
  109. $app = 'core';
  110. }
  111. $response = null;
  112. $iconFile = null;
  113. try {
  114. $iconFile = $this->imageManager->getImage('favicon', false);
  115. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  116. } catch (NotFoundException $e) {
  117. }
  118. if ($iconFile === null && $this->imageManager->shouldReplaceIcons()) {
  119. $color = $this->themingDefaults->getColorPrimary();
  120. try {
  121. $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app . $color);
  122. } catch (NotFoundException $exception) {
  123. $icon = $this->iconBuilder->getFavicon($app);
  124. if ($icon === false || $icon === '') {
  125. return new NotFoundResponse();
  126. }
  127. $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app . $color, $icon);
  128. }
  129. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  130. }
  131. if ($response === null) {
  132. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
  133. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  134. }
  135. $response->cacheFor(86400);
  136. return $response;
  137. }
  138. /**
  139. * Return a 512x512 icon for touch devices
  140. *
  141. * @PublicPage
  142. * @NoCSRFRequired
  143. *
  144. * @param $app string app name
  145. * @return DataDisplayResponse|FileDisplayResponse|NotFoundResponse
  146. * @throws \Exception
  147. */
  148. public function getTouchIcon(string $app = 'core'): Response {
  149. if ($app !== 'core' && !$this->appManager->isEnabledForUser($app)) {
  150. $app = 'core';
  151. }
  152. $response = null;
  153. try {
  154. $iconFile = $this->imageManager->getImage('favicon');
  155. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  156. } catch (NotFoundException $e) {
  157. }
  158. if ($this->imageManager->shouldReplaceIcons()) {
  159. $color = $this->themingDefaults->getColorPrimary();
  160. try {
  161. $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app . $color);
  162. } catch (NotFoundException $exception) {
  163. $icon = $this->iconBuilder->getTouchIcon($app);
  164. if ($icon === false || $icon === '') {
  165. return new NotFoundResponse();
  166. }
  167. $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app . $color, $icon);
  168. }
  169. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
  170. }
  171. if ($response === null) {
  172. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
  173. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
  174. }
  175. $response->cacheFor(86400);
  176. return $response;
  177. }
  178. }