Completed
Push — master ( bcf587...5a9224 )
by Morris
19:50 queued 04:43
created

ThemingDefaults::getTitle()   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
32
class ThemingDefaults extends \OC_Defaults {
33
34
	/** @var IConfig */
35
	private $config;
36
	/** @var IL10N */
37
	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...
38
	/** @var IURLGenerator */
39
	private $urlGenerator;
40
	/** @var IAppData */
41
	private $appData;
42
	/** @var ICacheFactory */
43
	private $cacheFactory;
44
	/** @var string */
45
	private $name;
46
	/** @var string */
47
	private $url;
48
	/** @var string */
49
	private $slogan;
50
	/** @var string */
51
	private $color;
52
	/** @var Util */
53
	private $util;
54
55
	/**
56
	 * ThemingDefaults constructor.
57
	 *
58
	 * @param IConfig $config
59
	 * @param IL10N $l
60
	 * @param IURLGenerator $urlGenerator
61
	 * @param \OC_Defaults $defaults
62
	 * @param IAppData $appData
63
	 * @param ICacheFactory $cacheFactory
64
	 * @param Util $util
65
	 */
66
	public function __construct(IConfig $config,
67
								IL10N $l,
68
								IURLGenerator $urlGenerator,
69
								\OC_Defaults $defaults,
70
								IAppData $appData,
71
								ICacheFactory $cacheFactory,
72
								Util $util
73
	) {
74
		$this->config = $config;
75
		$this->l = $l;
76
		$this->urlGenerator = $urlGenerator;
77
		$this->appData = $appData;
78
		$this->cacheFactory = $cacheFactory;
79
		$this->util = $util;
80
81
		$this->name = $defaults->getName();
82
		$this->url = $defaults->getBaseUrl();
83
		$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...
84
		$this->color = $defaults->getColorPrimary();
85
	}
86
87
	public function getName() {
88
		return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
89
	}
90
91
	public function getHTMLName() {
92
		return $this->config->getAppValue('theming', 'name', $this->name);
93
	}
94
95
	public function getTitle() {
96
		return $this->getName();
97
	}
98
99
	public function getEntity() {
100
		return $this->getName();
101
	}
102
103
	public function getBaseUrl() {
104
		return $this->config->getAppValue('theming', 'url', $this->url);
105
	}
106
107
	public function getSlogan() {
108
		return \OCP\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 108 which is incompatible with the return type of the parent method OC_Defaults::getSlogan of type string|OC_L10N_String.
Loading history...
109
	}
110
111
	public function getShortFooter() {
112
		$slogan = $this->getSlogan();
113
		$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
114
			' rel="noreferrer">' .$this->getEntity() . '</a>'.
115
			($slogan !== '' ? ' – ' . $slogan : '');
116
117
		return $footer;
118
	}
119
120
	/**
121
	 * Color that is used for the header as well as for mail headers
122
	 *
123
	 * @return string
124
	 */
125
	public function getColorPrimary() {
126
		return $this->config->getAppValue('theming', 'color', $this->color);
127
	}
128
129
	/**
130
	 * Themed logo url
131
	 *
132
	 * @return string
133
	 */
134
	public function getLogo() {
135
		$logo = $this->config->getAppValue('theming', 'logoMime', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
137
		$logoExists = true;
138
		try {
139
			$this->appData->getFolder('images')->getFile('logo');
140
		} catch (\Exception $e) {
141
			$logoExists = false;
142
		}
143
144
		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
145
146
		if(!$logo || !$logoExists) {
147
			return $this->urlGenerator->imagePath('core','logo.svg') . '?v=' . $cacheBusterCounter;
148
		}
149
150
		return $this->urlGenerator->linkToRoute('theming.Theming.getLogo') . '?v=' . $cacheBusterCounter;
151
	}
152
153
	/**
154
	 * Themed background image url
155
	 *
156
	 * @return string
157
	 */
158
	public function getBackground() {
159
		$backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime',false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
160
161
		$backgroundExists = true;
162
		try {
163
			$this->appData->getFolder('images')->getFile('background');
164
		} catch (\Exception $e) {
165
			$backgroundExists = false;
166
		}
167
168
		if(!$backgroundLogo || !$backgroundExists) {
169
			return $this->urlGenerator->imagePath('core','background.jpg');
170
		}
171
172
		return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground');
173
	}
174
175
176
	/**
177
	 * @return array scss variables to overwrite
178
	 */
179
	public function getScssVariables() {
180
		$cache = $this->cacheFactory->create('theming');
181
		if ($value = $cache->get('getScssVariables')) {
182
			return $value;
183
		}
184
185
		$variables = [
186
			'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'",
187
		];
188
189
		$variables['image-logo'] = "'".$this->urlGenerator->getAbsoluteURL($this->getLogo())."'";
190
		$variables['image-login-background'] = "'".$this->urlGenerator->getAbsoluteURL($this->getBackground())."'";
191
192
		if ($this->config->getAppValue('theming', 'color', null) !== null) {
193
			if ($this->util->invertTextColor($this->getColorPrimary())) {
194
				$colorPrimaryText = '#000000';
195
			} else {
196
				$colorPrimaryText = '#ffffff';
197
			}
198
			$variables['color-primary'] = $this->getColorPrimary();
199
			$variables['color-primary-text'] = $colorPrimaryText;
200
		}
201
		$cache->set('getScssVariables', $variables);
202
		return $variables;
203
	}
204
205
	/**
206
	 * Check if Imagemagick is enabled and if SVG is supported
207
	 * otherwise we can't render custom icons
208
	 *
209
	 * @return bool
210
	 */
211
	public function shouldReplaceIcons() {
212
		$cache = $this->cacheFactory->create('theming');
213
		if($value = $cache->get('shouldReplaceIcons')) {
214
			return (bool)$value;
215
		}
216
		$value = false;
217
		if(extension_loaded('imagick')) {
218
			$checkImagick = new \Imagick();
219
			if (count($checkImagick->queryFormats('SVG')) >= 1) {
220
				$value = true;
221
			}
222
			$checkImagick->clear();
223
		}
224
		$cache->set('shouldReplaceIcons', $value);
225
		return $value;
226
	}
227
228
	/**
229
	 * Increases the cache buster key
230
	 */
231
	private function increaseCacheBuster() {
232
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
233
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
234
		$this->cacheFactory->create('theming')->clear('getScssVariables');
235
	}
236
237
	/**
238
	 * Update setting in the database
239
	 *
240
	 * @param string $setting
241
	 * @param string $value
242
	 */
243
	public function set($setting, $value) {
244
		$this->config->setAppValue('theming', $setting, $value);
245
		$this->increaseCacheBuster();
246
	}
247
248
	/**
249
	 * Revert settings to the default value
250
	 *
251
	 * @param string $setting setting which should be reverted
252
	 * @return string default value
253
	 */
254
	public function undo($setting) {
255
		$this->config->deleteAppValue('theming', $setting);
256
		$this->increaseCacheBuster();
257
258
		switch ($setting) {
259
			case 'name':
260
				$returnValue = $this->getEntity();
261
				break;
262
			case 'url':
263
				$returnValue = $this->getBaseUrl();
264
				break;
265
			case 'slogan':
266
				$returnValue = $this->getSlogan();
267
				break;
268
			case 'color':
269
				$returnValue = $this->getColorPrimary();
270
				break;
271
			default:
272
				$returnValue = '';
273
				break;
274
		}
275
276
		return $returnValue;
277
	}
278
279
}
280