Completed
Push — master ( 45fcfc...6e6327 )
by Lukas
13:40
created

Util::getAppIcon()   C

Complexity

Conditions 7
Paths 22

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 22
nop 1
dl 0
loc 25
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Haertl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Theming;
25
26
use OCP\App\AppPathNotFoundException;
27
use OCP\App\IAppManager;
28
use OCP\Files\IAppData;
29
use OCP\Files\NotFoundException;
30
use OCP\Files\SimpleFS\ISimpleFile;
31
use OCP\IConfig;
32
use OCP\Files\IRootFolder;
33
34
class Util {
35
36
	/** @var IConfig */
37
	private $config;
38
39
	/** @var IAppManager */
40
	private $appManager;
41
42
	/** @var IAppData */
43
	private $appData;
44
45
	/**
46
	 * Util constructor.
47
	 *
48
	 * @param IConfig $config
49
	 * @param IAppManager $appManager
50
	 * @param IAppData $appData
51
	 */
52
	public function __construct(IConfig $config, IAppManager $appManager, IAppData $appData) {
53
		$this->config = $config;
54
		$this->appManager = $appManager;
55
		$this->appData = $appData;
56
	}
57
58
	/**
59
	 * @param string $color rgb color value
60
	 * @return bool
61
	 */
62
	public function invertTextColor($color) {
63
		$l = $this->calculateLuminance($color);
64
		if($l>0.5) {
65
			return true;
66
		} else {
67
			return false;
68
		}
69
	}
70
71
	/**
72
	 * get color for on-page elements:
73
	 * theme color by default, grey if theme color is to bright
74
	 * @param $color
75
	 * @return string
76
	 */
77
	public function elementColor($color) {
78
		$l = $this->calculateLuminance($color);
79
		if($l>0.8) {
80
			return '#555555';
81
		} else {
82
			return $color;
83
		}
84
	}
85
86
	/**
87
	 * @param string $color rgb color value
88
	 * @return float
89
	 */
90
	public function calculateLuminance($color) {
91
		$hex = preg_replace("/[^0-9A-Fa-f]/", '', $color);
92
		if (strlen($hex) === 3) {
93
			$hex = $hex{0} . $hex{0} . $hex{1} . $hex{1} . $hex{2} . $hex{2};
94
		}
95
		if (strlen($hex) !== 6) {
96
			return 0;
97
		}
98
		$r = hexdec(substr($hex, 0, 2));
99
		$g = hexdec(substr($hex, 2, 2));
100
		$b = hexdec(substr($hex, 4, 2));
101
		return (0.299 * $r + 0.587 * $g + 0.114 * $b)/255;
102
	}
103
104
	/**
105
	 * @param $color
106
	 * @return string base64 encoded radio button svg
107
	 */
108
	public function generateRadioButton($color) {
109
		$radioButtonIcon = '<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16">' .
110
			'<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>';
111
		return base64_encode($radioButtonIcon);
112
	}
113
114
115
	/**
116
	 * @param $app string app name
117
	 * @return string|ISimpleFile path to app icon / file of logo
118
	 */
119
	public function getAppIcon($app) {
120
		$app = str_replace(array('\0', '/', '\\', '..'), '', $app);
121
		try {
122
			$appPath = $this->appManager->getAppPath($app);
123
			$icon = $appPath . '/img/' . $app . '.svg';
124
			if (file_exists($icon)) {
125
				return $icon;
126
			}
127
			$icon = $appPath . '/img/app.svg';
128
			if (file_exists($icon)) {
129
				return $icon;
130
			}
131
		} catch (AppPathNotFoundException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
132
133
		if ($this->config->getAppValue('theming', 'logoMime', '') !== '') {
134
			$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...
135
			try {
136
				$folder = $this->appData->getFolder('images');
137
				if ($folder !== null) {
138
					return $folder->getFile('logo');
139
				}
140
			} catch (NotFoundException $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
141
		}
142
		return \OC::$SERVERROOT . '/core/img/logo.svg';
143
	}
144
145
	/**
146
	 * @param $app string app name
147
	 * @param $image string relative path to image in app folder
148
	 * @return string|false absolute path to image
149
	 */
150
	public function getAppImage($app, $image) {
151
		$app = str_replace(array('\0', '/', '\\', '..'), '', $app);
152
		$image = str_replace(array('\0', '\\', '..'), '', $image);
153
		if ($app === "core") {
154
			$icon = \OC::$SERVERROOT . '/core/img/' . $image;
155
			if (file_exists($icon)) {
156
				return $icon;
157
			}
158
		}
159
160
		try {
161
			$appPath = $this->appManager->getAppPath($app);
162
		} catch (AppPathNotFoundException $e) {
163
			return false;
164
		}
165
166
		$icon = $appPath . '/img/' . $image;
167
		if (file_exists($icon)) {
168
			return $icon;
169
		}
170
		$icon = $appPath . '/img/' . $image . '.svg';
171
		if (file_exists($icon)) {
172
			return $icon;
173
		}
174
		$icon = $appPath . '/img/' . $image . '.png';
175
		if (file_exists($icon)) {
176
			return $icon;
177
		}
178
		$icon = $appPath . '/img/' . $image . '.gif';
179
		if (file_exists($icon)) {
180
			return $icon;
181
		}
182
		$icon = $appPath . '/img/' . $image . '.jpg';
183
		if (file_exists($icon)) {
184
			return $icon;
185
		}
186
187
		return false;
188
	}
189
190
	/**
191
	 * replace default color with a custom one
192
	 *
193
	 * @param $svg string content of a svg file
194
	 * @param $color string color to match
195
	 * @return string
196
	 */
197
	public function colorizeSvg($svg, $color) {
198
		$svg = preg_replace('/#0082c9/i', $color, $svg);
199
		return $svg;
200
	}
201
202
}
203