Completed
Push — master ( 8ef4fc...ba2e1c )
by Morris
13:20
created

ThemingDefaults::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\App\AppPathNotFoundException;
27
use OCP\App\IAppManager;
28
use OCP\Files\IAppData;
29
use OCP\ICacheFactory;
30
use OCP\IConfig;
31
use OCP\IL10N;
32
use OCP\IURLGenerator;
33
34
class ThemingDefaults extends \OC_Defaults {
35
36
	/** @var IConfig */
37
	private $config;
38
	/** @var IL10N */
39
	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...
40
	/** @var IURLGenerator */
41
	private $urlGenerator;
42
	/** @var IAppData */
43
	private $appData;
44
	/** @var ICacheFactory */
45
	private $cacheFactory;
46
	/** @var Util */
47
	private $util;
48
	/** @var IAppManager */
49
	private $appManager;
50
	/** @var string */
51
	private $name;
52
	/** @var string */
53
	private $title;
54
	/** @var string */
55
	private $entity;
56
	/** @var string */
57
	private $url;
58
	/** @var string */
59
	private $slogan;
60
	/** @var string */
61
	private $color;
62
63
	/** @var string */
64
	private $iTunesAppId;
65
	/** @var string */
66
	private $iOSClientUrl;
67
	/** @var string */
68
	private $AndroidClientUrl;
69
70
	/**
71
	 * ThemingDefaults constructor.
72
	 *
73
	 * @param IConfig $config
74
	 * @param IL10N $l
75
	 * @param IURLGenerator $urlGenerator
76
	 * @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...
77
	 * @param IAppData $appData
78
	 * @param ICacheFactory $cacheFactory
79
	 * @param Util $util
80
	 * @param IAppManager $appManager
81
	 */
82
	public function __construct(IConfig $config,
83
								IL10N $l,
84
								IURLGenerator $urlGenerator,
85
								IAppData $appData,
86
								ICacheFactory $cacheFactory,
87
								Util $util,
88
								IAppManager $appManager
89
	) {
90
		parent::__construct();
91
		$this->config = $config;
92
		$this->l = $l;
93
		$this->urlGenerator = $urlGenerator;
94
		$this->appData = $appData;
95
		$this->cacheFactory = $cacheFactory;
96
		$this->util = $util;
97
		$this->appManager = $appManager;
98
99
		$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...
100
		$this->title = parent::getTitle();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getTitle() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->getTitle().

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

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

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

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

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...
108
	}
109
110
	public function getName() {
111
		return strip_tags($this->config->getAppValue('theming', 'name', $this->name));
112
	}
113
114
	public function getHTMLName() {
115
		return $this->config->getAppValue('theming', 'name', $this->name);
116
	}
117
118
	public function getTitle() {
119
		return strip_tags($this->config->getAppValue('theming', 'name', $this->title));
120
	}
121
122
	public function getEntity() {
123
		return strip_tags($this->config->getAppValue('theming', 'name', $this->entity));
124
	}
125
126
	public function getBaseUrl() {
127
		return $this->config->getAppValue('theming', 'url', $this->url);
128
	}
129
130
	public function getSlogan() {
131
		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 131 which is incompatible with the return type of the parent method OC_Defaults::getSlogan of type string.
Loading history...
132
	}
133
134
	public function getShortFooter() {
135
		$slogan = $this->getSlogan();
136
		$footer = '<a href="'. $this->getBaseUrl() . '" target="_blank"' .
137
			' rel="noreferrer">' .$this->getEntity() . '</a>'.
138
			($slogan !== '' ? ' – ' . $slogan : '');
139
140
		return $footer;
141
	}
142
143
	/**
144
	 * Color that is used for the header as well as for mail headers
145
	 *
146
	 * @return string
147
	 */
148
	public function getColorPrimary() {
149
		return $this->config->getAppValue('theming', 'color', $this->color);
150
	}
151
152
	/**
153
	 * Themed logo url
154
	 *
155
	 * @param bool $useSvg Whether to point to the SVG image or a fallback
156
	 * @return string
157
	 */
158
	public function getLogo($useSvg = true) {
159
		$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...
160
161
		$logoExists = true;
162
		try {
163
			$this->appData->getFolder('images')->getFile('logo');
164
		} catch (\Exception $e) {
165
			$logoExists = false;
166
		}
167
168
		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
169
170
		if(!$logo || !$logoExists) {
171
			if($useSvg) {
172
				$logo = $this->urlGenerator->imagePath('core', 'logo.svg');
173
			} else {
174
				$logo = $this->urlGenerator->imagePath('core', 'logo.png');
175
			}
176
			return $logo . '?v=' . $cacheBusterCounter;
177
		}
178
179
		return $this->urlGenerator->linkToRoute('theming.Theming.getLogo') . '?v=' . $cacheBusterCounter;
180
	}
181
182
	/**
183
	 * Themed background image url
184
	 *
185
	 * @return string
186
	 */
187
	public function getBackground() {
188
		$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...
189
190
		$backgroundExists = true;
191
		try {
192
			$this->appData->getFolder('images')->getFile('background');
193
		} catch (\Exception $e) {
194
			$backgroundExists = false;
195
		}
196
197
		$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
198
199
		if(!$backgroundLogo || !$backgroundExists) {
200
			return $this->urlGenerator->imagePath('core','background.png') . '?v=' . $cacheBusterCounter;
201
		}
202
203
		return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground') . '?v=' . $cacheBusterCounter;
204
	}
205
206
	/**
207
	 * @return string
208
	 */
209
	public function getiTunesAppId() {
210
		return $this->config->getAppValue('theming', 'iTunesAppId', $this->iTunesAppId);
211
	}
212
213
	/**
214
	 * @return string
215
	 */
216
	public function getiOSClientUrl() {
217
		return $this->config->getAppValue('theming', 'iOSClientUrl', $this->iOSClientUrl);
218
	}
219
220
	/**
221
	 * @return string
222
	 */
223
	public function getAndroidClientUrl() {
224
		return $this->config->getAppValue('theming', 'AndroidClientUrl', $this->AndroidClientUrl);
225
	}
226
227
228
	/**
229
	 * @return array scss variables to overwrite
230
	 */
231
	public function getScssVariables() {
232
		$cache = $this->cacheFactory->create('theming');
233
		if ($value = $cache->get('getScssVariables')) {
234
			return $value;
235
		}
236
237
		$variables = [
238
			'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'",
239
			'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime', '') . "'",
240
			'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime', '') . "'"
241
		];
242
243
		$variables['image-logo'] = "'".$this->urlGenerator->getAbsoluteURL($this->getLogo())."'";
244
		$variables['image-login-background'] = "'".$this->urlGenerator->getAbsoluteURL($this->getBackground())."'";
245
		$variables['image-login-plain'] = 'false';
246
247
		if ($this->config->getAppValue('theming', 'color', null) !== null) {
248
			if ($this->util->invertTextColor($this->getColorPrimary())) {
249
				$colorPrimaryText = '#000000';
250
			} else {
251
				$colorPrimaryText = '#ffffff';
252
			}
253
			$variables['color-primary'] = $this->getColorPrimary();
254
			$variables['color-primary-text'] = $colorPrimaryText;
255
			$variables['color-primary-element'] = $this->util->elementColor($this->getColorPrimary());
256
		}
257
258
		if ($this->config->getAppValue('theming', 'backgroundMime', null) === 'backgroundColor') {
259
			$variables['image-login-plain'] = 'true';
260
		}
261
		$cache->set('getScssVariables', $variables);
262
		return $variables;
263
	}
264
265
	/**
266
	 * Check if the image should be replaced by the theming app
267
	 * and return the new image location then
268
	 *
269
	 * @param string $app name of the app
270
	 * @param string $image filename of the image
271
	 * @return bool|string false if image should not replaced, otherwise the location of the image
272
	 */
273
	public function replaceImagePath($app, $image) {
274
		if($app==='') {
275
			$app = 'core';
276
		}
277
		$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
278
279
		if ($image === 'favicon.ico' && $this->shouldReplaceIcons()) {
280
			return $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]) . '?v=' . $cacheBusterValue;
281
		}
282
		if ($image === 'favicon-touch.png' && $this->shouldReplaceIcons()) {
283
			return $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]) . '?v=' . $cacheBusterValue;
284
		}
285
		if ($image === 'manifest.json') {
286
			try {
287
				$appPath = $this->appManager->getAppPath($app);
288
				if (file_exists($appPath . '/img/manifest.json')) {
289
					return false;
290
				}
291
			} catch (AppPathNotFoundException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
292
			return $this->urlGenerator->linkToRoute('theming.Theming.getManifest') . '?v=' . $cacheBusterValue;
293
		}
294
		return false;
295
	}
296
297
	/**
298
	 * Check if Imagemagick is enabled and if SVG is supported
299
	 * otherwise we can't render custom icons
300
	 *
301
	 * @return bool
302
	 */
303
	public function shouldReplaceIcons() {
304
		$cache = $this->cacheFactory->create('theming');
305
		if($value = $cache->get('shouldReplaceIcons')) {
306
			return (bool)$value;
307
		}
308
		$value = false;
309
		if(extension_loaded('imagick')) {
310
			$checkImagick = new \Imagick();
311
			if (count($checkImagick->queryFormats('SVG')) >= 1) {
312
				$value = true;
313
			}
314
			$checkImagick->clear();
315
		}
316
		$cache->set('shouldReplaceIcons', $value);
317
		return $value;
318
	}
319
320
	/**
321
	 * Increases the cache buster key
322
	 */
323
	private function increaseCacheBuster() {
324
		$cacheBusterKey = $this->config->getAppValue('theming', 'cachebuster', '0');
325
		$this->config->setAppValue('theming', 'cachebuster', (int)$cacheBusterKey+1);
326
		$this->cacheFactory->create('theming')->clear('getScssVariables');
327
	}
328
329
	/**
330
	 * Update setting in the database
331
	 *
332
	 * @param string $setting
333
	 * @param string $value
334
	 */
335
	public function set($setting, $value) {
336
		$this->config->setAppValue('theming', $setting, $value);
337
		$this->increaseCacheBuster();
338
	}
339
340
	/**
341
	 * Revert settings to the default value
342
	 *
343
	 * @param string $setting setting which should be reverted
344
	 * @return string default value
345
	 */
346
	public function undo($setting) {
347
		$this->config->deleteAppValue('theming', $setting);
348
		$this->increaseCacheBuster();
349
350
		switch ($setting) {
351
			case 'name':
352
				$returnValue = $this->getEntity();
353
				break;
354
			case 'url':
355
				$returnValue = $this->getBaseUrl();
356
				break;
357
			case 'slogan':
358
				$returnValue = $this->getSlogan();
359
				break;
360
			case 'color':
361
				$returnValue = $this->getColorPrimary();
362
				break;
363
			default:
364
				$returnValue = '';
365
				break;
366
		}
367
368
		return $returnValue;
369
	}
370
}
371