Completed
Push — master ( f3eb9e...aab39b )
by Morris
47:51 queued 28:25
created

Util::isBackgroundThemed()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 0
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Julius Haertl <[email protected]>
7
 * @author Julius Härtl <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\Theming;
27
28
use OCP\App\AppPathNotFoundException;
29
use OCP\App\IAppManager;
30
use OCP\Files\IAppData;
31
use OCP\Files\NotFoundException;
32
use OCP\Files\SimpleFS\ISimpleFile;
33
use OCP\IConfig;
34
use OCP\Files\IRootFolder;
35
use Leafo\ScssPhp\Compiler;
36
37
class Util {
38
39
	/** @var IConfig */
40
	private $config;
41
42
	/** @var IAppManager */
43
	private $appManager;
44
45
	/** @var IAppData */
46
	private $appData;
47
48
	/**
49
	 * Util constructor.
50
	 *
51
	 * @param IConfig $config
52
	 * @param IAppManager $appManager
53
	 * @param IAppData $appData
54
	 */
55
	public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData) {
56
		$this->config = $config;
57
		$this->appManager = $appManager;
58
		$this->appData = $appData;
59
	}
60
61
	/**
62
	 * @param string $color rgb color value
63
	 * @return bool
64
	 */
65
	public function invertTextColor($color) {
66
		$l = $this->calculateLuma($color);
67
		if($l>0.6) {
68
			return true;
69
		} else {
70
			return false;
71
		}
72
	}
73
74
	/**
75
	 * get color for on-page elements:
76
	 * theme color by default, grey if theme color is to bright
77
	 * @param $color
78
	 * @return string
79
	 */
80
	public function elementColor($color) {
81
		$l = $this->calculateLuminance($color);
82
		if($l>0.8) {
83
			return '#555555';
84
		}
85
		return $color;
86
	}
87
88
	/**
89
	 * @param string $color rgb color value
90
	 * @return float
91
	 */
92
	public function calculateLuminance($color) {
93
		list($red, $green, $blue) = $this->hexToRGB($color);
94
		$compiler = new Compiler();
95
		$hsl = $compiler->toHSL($red, $green, $blue);
96
		return $hsl[3]/100;
97
	}
98
99
	/**
100
	 * @param string $color rgb color value
101
	 * @return float
102
	 */
103
	public function calculateLuma($color) {
104
		list($red, $green, $blue) = $this->hexToRGB($color);
105
		return (0.2126 * $red  + 0.7152 * $green + 0.0722 * $blue) / 255;
106
	}
107
108
	/**
109
	 * @param string $color rgb color value
110
	 * @return int[]
111
	 */
112
	public function hexToRGB($color) {
113
		$hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
114
		if (strlen($hex) === 3) {
115
			$hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
116
		}
117
		if (strlen($hex) !== 6) {
118
			return 0;
119
		}
120
		return [
121
			hexdec(substr($hex, 0, 2)),
122
			hexdec(substr($hex, 2, 2)),
123
			hexdec(substr($hex, 4, 2))
124
		];
125
	}
126
127
	/**
128
	 * @param $color
129
	 * @return string base64 encoded radio button svg
130
	 */
131
	public function generateRadioButton($color) {
132
		$radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
133
			'<path d="M8 1a7 7 0 0 0-7 7 7 7 0 0 0 7 7 7 7 0 0 0 7-7 7 7 0 0 0-7-7zm0 1a6 6 0 0 1 6 6 6 6 0 0 1-6 6 6 6 0 0 1-6-6 6 6 0 0 1 6-6zm0 2a4 4 0 1 0 0 8 4 4 0 0 0 0-8z" fill="'.$color.'"/></svg>';
134
		return base64_encode($radioButtonIcon);
135
	}
136
137
138
	/**
139
	 * @param $app string app name
140
	 * @return string|ISimpleFile path to app icon / file of logo
141
	 */
142
	public function getAppIcon($app) {
143
		$app = str_replace(array('\0', '/', '\\', '..'), '', $app);
144
		try {
145
			$appPath = $this->appManager->getAppPath($app);
146
			$icon = $appPath . '/img/' . $app . '.svg';
147
			if (file_exists($icon)) {
148
				return $icon;
149
			}
150
			$icon = $appPath . '/img/app.svg';
151
			if (file_exists($icon)) {
152
				return $icon;
153
			}
154
		} catch (AppPathNotFoundException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
155
156
		if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
157
			$logoFile = null;
0 ignored issues
show
Unused Code introduced by
$logoFile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
			try {
159
				$folder = $this->appData->getFolder('images');
160
				if ($folder !== null) {
161
					return $folder->getFile('logo');
162
				}
163
			} catch (NotFoundException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
164
		}
165
		return \OC::$SERVERROOT . '/core/img/logo.svg';
166
	}
167
168
	/**
169
	 * @param $app string app name
170
	 * @param $image string relative path to image in app folder
171
	 * @return string|false absolute path to image
172
	 */
173
	public function getAppImage($app, $image) {
174
		$app = str_replace(array('\0', '/', '\\', '..'), '', $app);
175
		$image = str_replace(array('\0', '\\', '..'), '', $image);
176
		if ($app === "core") {
177
			$icon = \OC::$SERVERROOT . '/core/img/' . $image;
178
			if (file_exists($icon)) {
179
				return $icon;
180
			}
181
		}
182
183
		try {
184
			$appPath = $this->appManager->getAppPath($app);
185
		} catch (AppPathNotFoundException $e) {
186
			return false;
187
		}
188
189
		$icon = $appPath . '/img/' . $image;
190
		if (file_exists($icon)) {
191
			return $icon;
192
		}
193
		$icon = $appPath . '/img/' . $image . '.svg';
194
		if (file_exists($icon)) {
195
			return $icon;
196
		}
197
		$icon = $appPath . '/img/' . $image . '.png';
198
		if (file_exists($icon)) {
199
			return $icon;
200
		}
201
		$icon = $appPath . '/img/' . $image . '.gif';
202
		if (file_exists($icon)) {
203
			return $icon;
204
		}
205
		$icon = $appPath . '/img/' . $image . '.jpg';
206
		if (file_exists($icon)) {
207
			return $icon;
208
		}
209
210
		return false;
211
	}
212
213
	/**
214
	 * replace default color with a custom one
215
	 *
216
	 * @param $svg string content of a svg file
217
	 * @param $color string color to match
218
	 * @return string
219
	 */
220
	public function colorizeSvg($svg, $color) {
221
		$svg = preg_replace('/#0082c9/i', $color, $svg);
222
		return $svg;
223
	}
224
225
	/**
226
	 * Check if a custom theme is set in the server configuration
227
	 * 
228
	 * @return bool
229
	 */
230
	public function isAlreadyThemed() {
231
		$theme = $this->config->getSystemValue('theme', '');
232
		if ($theme !== '') {
233
			return true;
234
		}
235
		return false;
236
	}
237
238
	public function isBackgroundThemed() {
239
		$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...
240
241
		$backgroundExists = true;
242
		try {
243
			$this->appData->getFolder('images')->getFile('background');
244
		} catch (\Exception $e) {
245
			$backgroundExists = false;
246
		}
247
		return $backgroundLogo && $backgroundLogo !== 'backgroundColor' && $backgroundExists;
248
	}
249
250
}
251