Completed
Push — master ( 157be4...5213c5 )
by Lukas
27:14 queued 14:51
created

ThemingDefaults::getLogo()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 6
nop 1
dl 0
loc 23
rs 8.5906
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
0 ignored issues
show
Bug introduced by
There is no parameter named $defaults. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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
								IAppData $appData,
70
								ICacheFactory $cacheFactory,
71
								Util $util
72
	) {
73
		parent::__construct();
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 = parent::getName();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getName() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->getName().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
82
		$this->url = parent::getBaseUrl();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getBaseUrl() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->getBaseUrl().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
83
		$this->slogan = parent::getSlogan();
0 ignored issues
show
Documentation Bug introduced by
It seems like parent::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...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getSlogan() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->getSlogan().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
84
		$this->color = parent::getColorPrimary();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getColorPrimary() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->getColorPrimary().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

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