Completed
Push — master ( 5ca5eb...afb5d4 )
by Lukas
16:52
created

ThemingDefaults::getColorPrimary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Bjoern Schiessle <[email protected]>
4
 * @copyright Copyright (c) 2017 Lukas Reschke <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\Theming;
24
25
26
use OCP\Files\IAppData;
27
use OCP\ICacheFactory;
28
use OCP\IConfig;
29
use OCP\IL10N;
30
use OCP\IURLGenerator;
31
use OCP\Util;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OCA\Theming\Util.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
32
33
class ThemingDefaults extends \OC_Defaults {
34
35
	/** @var IConfig */
36
	private $config;
37
	/** @var IL10N */
38
	private $l;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
39
	/** @var IURLGenerator */
40
	private $urlGenerator;
41
	/** @var IAppData */
42
	private $appData;
43
	/** @var ICacheFactory */
44
	private $cacheFactory;
45
	/** @var string */
46
	private $name;
47
	/** @var string */
48
	private $url;
49
	/** @var string */
50
	private $slogan;
51
	/** @var string */
52
	private $color;
53
54
	/**
55
	 * ThemingDefaults constructor.
56
	 *
57
	 * @param IConfig $config
58
	 * @param IL10N $l
59
	 * @param IURLGenerator $urlGenerator
60
	 * @param \OC_Defaults $defaults
61
	 * @param IAppData $appData
62
	 * @param ICacheFactory $cacheFactory
63
	 */
64
	public function __construct(IConfig $config,
65
								IL10N $l,
66
								IURLGenerator $urlGenerator,
67
								\OC_Defaults $defaults,
68
								IAppData $appData,
69
								ICacheFactory $cacheFactory
70
	) {
71
		parent::__construct();
72
		$this->config = $config;
73
		$this->l = $l;
74
		$this->urlGenerator = $urlGenerator;
75
		$this->appData = $appData;
76
		$this->cacheFactory = $cacheFactory;
77
78
		$this->name = $defaults->getName();
79
		$this->url = $defaults->getBaseUrl();
80
		$this->slogan = $defaults->getSlogan();
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaults->getSlogan() can also be of type object<OC_L10N_String>. However, the property $slogan is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
81
		$this->color = $defaults->getColorPrimary();
82
	}
83
84
	public function getName() {
85
		return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
86
	}
87
88
	public function getHTMLName() {
89
		return $this->config->getAppValue('theming', 'name', $this->name);
90
	}
91
92
	public function getTitle() {
93
		return $this->getName();
94
	}
95
96
	public function getEntity() {
97
		return $this->getName();
98
	}
99
100
	public function getBaseUrl() {
101
		return $this->config->getAppValue('theming', 'url', $this->url);
102
	}
103
104
	public function getSlogan() {
105
		return Util::sanitizeHTML($this->config->getAppValue('theming', 'slogan', $this->slogan));
0 ignored issues
show
Bug Compatibility introduced by
The expression \OCP\Util::sanitizeHTML(...ogan', $this->slogan)); of type array|string adds the type array to the return on line 105 which is incompatible with the return type of the parent method OC_Defaults::getSlogan of type string|OC_L10N_String.
Loading history...
106
	}
107
108
	public function getShortFooter() {
109
		$slogan = $this->getSlogan();
110
		$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
111
			' rel="noreferrer">' .$this->getEntity() . '</a>'.
112
			($slogan !== '' ? ' – ' . $slogan : '');
113
114
		return $footer;
115
	}
116
117
	/**
118
	 * Color that is used for the header as well as for mail headers
119
	 *
120
	 * @return string
121
	 */
122
	public function getColorPrimary() {
123
		return $this->config->getAppValue('theming', 'color', $this->color);
124
	}
125
126
	/**
127
	 * Themed logo url
128
	 *
129
	 * @return string
130
	 */
131
	public function getLogo() {
132
		$logo = $this->config->getAppValue('theming', 'logoMime');
133
134
		$logoExists = true;
135
		try {
136
			$this->appData->getFolder('images')->getFile('logo');
137
		} catch (\Exception $e) {
138
			$logoExists = false;
139
		}
140
141
		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
142
143
		if(!$logo || !$logoExists) {
144
			return $this->urlGenerator->imagePath('core','logo.svg') . '?v=' . $cacheBusterCounter;
145
		}
146
147
		return $this->urlGenerator->linkToRoute('theming.Theming.getLogo') . '?v=' . $cacheBusterCounter;
148
	}
149
150
	/**
151
	 * Themed background image url
152
	 *
153
	 * @return string
154
	 */
155
	public function getBackground() {
156
		$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime');
157
158
		$backgroundExists = true;
159
		try {
160
			$this->appData->getFolder('images')->getFile('background');
161
		} catch (\Exception $e) {
162
			$backgroundExists = false;
163
		}
164
165
		if(!$backgroundLogo || !$backgroundExists) {
166
			return $this->urlGenerator->imagePath('core','background.jpg');
167
		}
168
169
		return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground');
170
	}
171
172
	/**
173
	 * Check if Imagemagick is enabled and if SVG is supported
174
	 * otherwise we can't render custom icons
175
	 *
176
	 * @return bool
177
	 */
178
	public function shouldReplaceIcons() {
179
		$cache = $this->cacheFactory->create('theming');
180
		if($value = $cache->get('shouldReplaceIcons')) {
181
			return (bool)$value;
182
		}
183
		$value = false;
184
		if(extension_loaded('imagick')) {
185
			$checkImagick = new \Imagick();
186
			if (count($checkImagick->queryFormats('SVG')) >= 1) {
187
				$value = true;
188
			}
189
			$checkImagick->clear();
190
		}
191
		$cache->set('shouldReplaceIcons', $value);
192
		return $value;
193
	}
194
195
	/**
196
	 * Increases the cache buster key
197
	 */
198
	private function increaseCacheBuster() {
199
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
200
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
201
	}
202
203
	/**
204
	 * Update setting in the database
205
	 *
206
	 * @param string $setting
207
	 * @param string $value
208
	 */
209
	public function set($setting, $value) {
210
		$this->config->setAppValue('theming', $setting, $value);
211
		$this->increaseCacheBuster();
212
	}
213
214
	/**
215
	 * Revert settings to the default value
216
	 *
217
	 * @param string $setting setting which should be reverted
218
	 * @return string default value
219
	 */
220
	public function undo($setting) {
221
		$this->config->deleteAppValue('theming', $setting);
222
		$this->increaseCacheBuster();
223
224
		switch ($setting) {
225
			case 'name':
226
				$returnValue = $this->getEntity();
227
				break;
228
			case 'url':
229
				$returnValue = $this->getBaseUrl();
230
				break;
231
			case 'slogan':
232
				$returnValue = $this->getSlogan();
233
				break;
234
			case 'color':
235
				$returnValue = $this->getColorPrimary();
236
				break;
237
			default:
238
				$returnValue = '';
239
				break;
240
		}
241
242
		return $returnValue;
243
	}
244
245
}
246