Application.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Accessibility\AppInfo;
  24. use OCP\AppFramework\App;
  25. use OCP\IConfig;
  26. use OCP\IUserSession;
  27. use OCP\IURLGenerator;
  28. class Application extends App {
  29. /** @var string */
  30. protected $appName = 'accessibility';
  31. /** @var IConfig */
  32. private $config;
  33. /** @var IUserSession */
  34. private $userSession;
  35. /** @var IURLGenerator */
  36. private $urlGenerator;
  37. public function __construct() {
  38. parent::__construct($this->appName);
  39. $this->config = \OC::$server->getConfig();
  40. $this->userSession = \OC::$server->getUserSession();
  41. $this->urlGenerator = \OC::$server->getURLGenerator();
  42. }
  43. public function injectCss() {
  44. // Inject the fake css on all pages if enabled and user is logged
  45. $loggedUser = $this->userSession->getUser();
  46. if (!is_null($loggedUser)) {
  47. $userValues = $this->config->getUserKeys($loggedUser->getUID(), $this->appName);
  48. // we want to check if any theme or font is enabled.
  49. if (count($userValues) > 0) {
  50. $hash = $this->config->getUserValue($loggedUser->getUID(), $this->appName, 'icons-css', md5(implode('-', $userValues)));
  51. $linkToCSS = $this->urlGenerator->linkToRoute($this->appName . '.accessibility.getCss', ['md5' => $hash]);
  52. \OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
  53. }
  54. }
  55. }
  56. public function injectJavascript() {
  57. $linkToJs = $this->urlGenerator->linkToRoute(
  58. $this->appName . '.accessibility.getJavascript',
  59. [
  60. 'v' => \OC::$server->getConfig()->getAppValue('accessibility', 'cachebuster', '0'),
  61. ]
  62. );
  63. \OCP\Util::addHeader(
  64. 'script',
  65. [
  66. 'src' => $linkToJs,
  67. 'nonce' => \OC::$server->getContentSecurityPolicyNonceManager()->getNonce()
  68. ],
  69. ''
  70. );
  71. }
  72. }