Completed
Push — master ( d35b65...6a979a )
by Morris
24:58 queued 10s
created

Application::injectJavascript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 John Molakvoæ <[email protected]>
4
 *
5
 * @author John Molakvoæ <[email protected]>
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
24
namespace OCA\Accessibility\AppInfo;
25
26
use OCP\AppFramework\App;
27
use OCP\IConfig;
28
use OCP\IUserSession;
29
use OCP\IURLGenerator;
30
31
class Application extends App {
32
33
	/** @var string */
34
	protected $appName = 'accessibility';
35
36
	/** @var IConfig */
37
	private $config;
38
39
	/** @var IUserSession */
40
	private $userSession;
41
42
	/** @var IURLGenerator */
43
	private $urlGenerator;
44
45
	public function __construct() {
46
		parent::__construct($this->appName);
47
		$this->config       = \OC::$server->getConfig();
48
		$this->userSession  = \OC::$server->getUserSession();
49
		$this->urlGenerator = \OC::$server->getURLGenerator();
50
	}
51
52
	public function injectCss() {
53
		// Inject the fake css on all pages if enabled and user is logged
54
		$loggedUser = $this->userSession->getUser();
55
		if (!is_null($loggedUser)) {
56
			$userValues = $this->config->getUserKeys($loggedUser->getUID(), $this->appName);
57
			if (count($userValues) > 0) {
58
				$linkToCSS = $this->urlGenerator->linkToRoute($this->appName . '.accessibility.getCss', ['md5' => md5(implode('-', $userValues))]);
59
				\OCP\Util::addHeader('link', ['rel' => 'stylesheet', 'href' => $linkToCSS]);
60
			}
61
		}
62
	}
63
64
	public function injectJavascript() {
65
		$linkToJs = $this->urlGenerator->linkToRoute(
66
			$this->appName . '.accessibility.getJavascript',
67
			[
68
				'v' => \OC::$server->getConfig()->getAppValue('accessibility', 'cachebuster', '0'),
69
			]
70
		);
71
72
		\OCP\Util::addHeader(
73
			'script',
74
			[
75
				'src' => $linkToJs,
76
				'nonce' => \OC::$server->getContentSecurityPolicyNonceManager()->getNonce()
77
			],
78
			''
79
		);
80
	}
81
}
82