1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2014 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_ADMIN_CONTEXT')) { return; } |
---|
13 | |
---|
14 | /** |
---|
15 | * @ingroup DC_CORE |
---|
16 | * @brief Helper for theme configurators. |
---|
17 | * @since 2.7 |
---|
18 | * |
---|
19 | * Provides helper tools for theme configurators. |
---|
20 | */ |
---|
21 | class dcThemeConfig |
---|
22 | { |
---|
23 | // Utilities |
---|
24 | |
---|
25 | public static function computeContrastRatio($color,$background) |
---|
26 | { |
---|
27 | // Compute contrast ratio between two colors |
---|
28 | |
---|
29 | $color = adjustColor($color); |
---|
30 | if (($color == '') || (strlen($color) != 7)) return 0; |
---|
31 | $background = adjustColor($background); |
---|
32 | if (($background == '') || (strlen($background) != 7)) return 0; |
---|
33 | |
---|
34 | $l1 = (0.2126 * pow(hexdec(substr($color,1,2))/255,2.2)) + |
---|
35 | (0.7152 * pow(hexdec(substr($color,3,2))/255,2.2)) + |
---|
36 | (0.0722 * pow(hexdec(substr($color,5,2))/255,2.2)); |
---|
37 | |
---|
38 | $l2 = (0.2126 * pow(hexdec(substr($background,1,2))/255,2.2)) + |
---|
39 | (0.7152 * pow(hexdec(substr($background,3,2))/255,2.2)) + |
---|
40 | (0.0722 * pow(hexdec(substr($background,5,2))/255,2.2)); |
---|
41 | |
---|
42 | if ($l1 > $l2) { |
---|
43 | $ratio = ($l1 + 0.05) / ($l2 + 0.05); |
---|
44 | } else { |
---|
45 | $ratio = ($l2 + 0.05) / ($l1 + 0.05); |
---|
46 | } |
---|
47 | return $ratio; |
---|
48 | } |
---|
49 | |
---|
50 | public static function contrastRatioLevel($ratio,$size,$bold=false) |
---|
51 | { |
---|
52 | if ($size == '') { |
---|
53 | return ''; |
---|
54 | } |
---|
55 | |
---|
56 | // Eval font size in em (assume base font size in pixels equal to 16) |
---|
57 | if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex)?$/',$size,$m)) { |
---|
58 | if (empty($m[2])) { |
---|
59 | $m[2] = 'em'; |
---|
60 | } |
---|
61 | } else { |
---|
62 | return ''; |
---|
63 | } |
---|
64 | switch ($m[2]) { |
---|
65 | case '%': |
---|
66 | $s = (float) $m[1] / 100; |
---|
67 | break; |
---|
68 | case 'pt': |
---|
69 | $s = (float) $m[1] / 12; |
---|
70 | break; |
---|
71 | case 'px': |
---|
72 | $s = (float) $m[1] / 16; |
---|
73 | break; |
---|
74 | case 'em': |
---|
75 | $s = (float) $m[1]; |
---|
76 | break; |
---|
77 | case 'ex': |
---|
78 | $s = (float) $m[1] / 2; |
---|
79 | break; |
---|
80 | default: |
---|
81 | return ''; |
---|
82 | } |
---|
83 | |
---|
84 | $large = ((($s > 1.5) && ($bold == false)) || (($s > 1.2) && ($bold == true))); |
---|
85 | |
---|
86 | // Check ratio |
---|
87 | if ($ratio > 7) { |
---|
88 | return 'AAA'; |
---|
89 | } elseif (($ratio > 4.5) && $large) { |
---|
90 | return 'AAA'; |
---|
91 | } elseif ($ratio > 4.5) { |
---|
92 | return 'AA'; |
---|
93 | } elseif (($ratio > 3) && $large) { |
---|
94 | return 'AA'; |
---|
95 | } |
---|
96 | return ''; |
---|
97 | } |
---|
98 | |
---|
99 | public static function contrastRatio($color,$background,$size='',$bold=false) |
---|
100 | { |
---|
101 | if (($color != '') && ($background != '')) { |
---|
102 | $ratio = computeContrastRatio($color,$background); |
---|
103 | $level = contrastRatioLevel($ratio,$size,$bold); |
---|
104 | return |
---|
105 | sprintf(__('ratio %.1f'),$ratio). |
---|
106 | ($level != '' ? ' '.sprintf(__('(%s)'),$level) : ''); |
---|
107 | } |
---|
108 | return ''; |
---|
109 | } |
---|
110 | |
---|
111 | public static function adjustFontSize($s) |
---|
112 | { |
---|
113 | if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex)?$/',$s,$m)) { |
---|
114 | if (empty($m[2])) { |
---|
115 | $m[2] = 'em'; |
---|
116 | } |
---|
117 | return $m[1].$m[2]; |
---|
118 | } |
---|
119 | return null; |
---|
120 | } |
---|
121 | |
---|
122 | public static function adjustPosition($p) |
---|
123 | { |
---|
124 | if (!preg_match('/^[0-9]+(:[0-9]+)?$/',$p)) { |
---|
125 | return null; |
---|
126 | } |
---|
127 | $p = explode(':',$p); |
---|
128 | |
---|
129 | return $p[0].(count($p) == 1 ? ':0' : ':'.$p[1]); |
---|
130 | } |
---|
131 | |
---|
132 | public static function adjustColor($c) |
---|
133 | { |
---|
134 | if ($c === '') { |
---|
135 | return ''; |
---|
136 | } |
---|
137 | |
---|
138 | $c = strtoupper($c); |
---|
139 | |
---|
140 | if (preg_match('/^[A-F0-9]{3,6}$/',$c)) { |
---|
141 | $c = '#'.$c; |
---|
142 | } |
---|
143 | if (preg_match('/^#[A-F0-9]{6}$/',$c)) { |
---|
144 | return $c; |
---|
145 | } |
---|
146 | if (preg_match('/^#[A-F0-9]{3,}$/',$c)) { |
---|
147 | return '#'.substr($c,1,1).substr($c,1,1).substr($c,2,1).substr($c,2,1).substr($c,3,1).substr($c,3,1); |
---|
148 | } |
---|
149 | |
---|
150 | return ''; |
---|
151 | } |
---|
152 | |
---|
153 | // CSS file management |
---|
154 | public static function cleanCSS($css) |
---|
155 | { |
---|
156 | // TODO ? |
---|
157 | return $css; |
---|
158 | } |
---|
159 | |
---|
160 | public static function cssPath($folder) |
---|
161 | { |
---|
162 | global $core; |
---|
163 | return path::real($core->blog->public_path).'/'.$folder; |
---|
164 | } |
---|
165 | |
---|
166 | public static function cssURL($folder) |
---|
167 | { |
---|
168 | global $core; |
---|
169 | return $core->blog->settings->system->public_url.'/'.$folder; |
---|
170 | } |
---|
171 | |
---|
172 | public static function canWriteCss($folder,$create=false) |
---|
173 | { |
---|
174 | global $core; |
---|
175 | |
---|
176 | $public = path::real($core->blog->public_path); |
---|
177 | $css = self::cssPath($folder); |
---|
178 | |
---|
179 | if (!is_dir($public)) { |
---|
180 | $core->error->add(__('The \'public\' directory does not exist.')); |
---|
181 | return false; |
---|
182 | } |
---|
183 | |
---|
184 | if (!is_dir($css)) { |
---|
185 | if (!is_writable($public)) { |
---|
186 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public')); |
---|
187 | return false; |
---|
188 | } |
---|
189 | if ($create) { |
---|
190 | files::makeDir($css); |
---|
191 | } |
---|
192 | return true; |
---|
193 | } |
---|
194 | |
---|
195 | if (!is_writable($css)) { |
---|
196 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/'.$folder)); |
---|
197 | return false; |
---|
198 | } |
---|
199 | |
---|
200 | return true; |
---|
201 | } |
---|
202 | |
---|
203 | public static function prop(&$css,$selector,$prop,$value) |
---|
204 | { |
---|
205 | if ($value) { |
---|
206 | $css[$selector][$prop] = $value; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | public static function backgroundImg($folder,&$css,$selector,$value,$image) |
---|
211 | { |
---|
212 | $file = self::imagesPath($folder).'/'.$image; |
---|
213 | if ($value && file_exists($file)){ |
---|
214 | $css[$selector]['background-image'] = 'url('.self::imagesURL($folder).'/'.$image.')'; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | public static function writeCss($folder,$theme,$css) |
---|
219 | { |
---|
220 | file_put_contents(self::cssPath($folder).'/'.$theme.'.css', $css); |
---|
221 | } |
---|
222 | |
---|
223 | public static function dropCss($folder,$theme) |
---|
224 | { |
---|
225 | $file = path::real(self::cssPath($folder).'/'.$theme.'.css'); |
---|
226 | if (is_writable(dirname($file))) { |
---|
227 | @unlink($file); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | public static function publicCssUrlHelper($folder) |
---|
232 | { |
---|
233 | $theme = $GLOBALS['core']->blog->settings->system->theme; |
---|
234 | $url = self::cssURL($folder); |
---|
235 | $path = self::cssPath($folder); |
---|
236 | |
---|
237 | if (file_exists($path.'/'.$theme.'.css')) { |
---|
238 | return $url.'/'.$theme.'.css'; |
---|
239 | } |
---|
240 | |
---|
241 | return null; |
---|
242 | } |
---|
243 | |
---|
244 | public static function imagesPath($folder) |
---|
245 | { |
---|
246 | global $core; |
---|
247 | return path::real($core->blog->public_path).'/'.$folder; |
---|
248 | } |
---|
249 | |
---|
250 | public static function imagesURL($folder) |
---|
251 | { |
---|
252 | global $core; |
---|
253 | return $core->blog->settings->system->public_url.'/'.$folder; |
---|
254 | } |
---|
255 | |
---|
256 | public static function canWriteImages($folder,$create=false) |
---|
257 | { |
---|
258 | global $core; |
---|
259 | |
---|
260 | $public = path::real($core->blog->public_path); |
---|
261 | $imgs = self::imagesPath($folder); |
---|
262 | |
---|
263 | if (!function_exists('imagecreatetruecolor') || !function_exists('imagepng') || !function_exists('imagecreatefrompng')) { |
---|
264 | $core->error->add(__('At least one of the following functions is not available: '. |
---|
265 | 'imagecreatetruecolor, imagepng & imagecreatefrompng.')); |
---|
266 | return false; |
---|
267 | } |
---|
268 | |
---|
269 | if (!is_dir($public)) { |
---|
270 | $core->error->add(__('The \'public\' directory does not exist.')); |
---|
271 | return false; |
---|
272 | } |
---|
273 | |
---|
274 | if (!is_dir($imgs)) { |
---|
275 | if (!is_writable($public)) { |
---|
276 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public')); |
---|
277 | return false; |
---|
278 | } |
---|
279 | if ($create) { |
---|
280 | files::makeDir($imgs); |
---|
281 | } |
---|
282 | return true; |
---|
283 | } |
---|
284 | |
---|
285 | if (!is_writable($imgs)) { |
---|
286 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/'.$folder)); |
---|
287 | return false; |
---|
288 | } |
---|
289 | |
---|
290 | return true; |
---|
291 | } |
---|
292 | |
---|
293 | public static function uploadImage($folder,$f) |
---|
294 | { |
---|
295 | if (!self::canWriteImages($folder,true)) { |
---|
296 | throw new Exception(__('Unable to create images.')); |
---|
297 | } |
---|
298 | |
---|
299 | $name = $f['name']; |
---|
300 | $type = files::getMimeType($name); |
---|
301 | |
---|
302 | if ($type != 'image/jpeg' && $type != 'image/png') { |
---|
303 | throw new Exception(__('Invalid file type.')); |
---|
304 | } |
---|
305 | |
---|
306 | $dest = self::imagesPath($folder).'/uploaded'.($type == 'image/png' ? '.png' : '.jpg'); |
---|
307 | |
---|
308 | if (@move_uploaded_file($f['tmp_name'],$dest) === false) { |
---|
309 | throw new Exception(__('An error occurred while writing the file.')); |
---|
310 | } |
---|
311 | |
---|
312 | $s = getimagesize($dest); |
---|
313 | if ($s[0] != 800) { |
---|
314 | throw new Exception(__('Uploaded image is not 800 pixels wide.')); |
---|
315 | } |
---|
316 | |
---|
317 | return $dest; |
---|
318 | } |
---|
319 | |
---|
320 | public static function dropImage($folder,$img) |
---|
321 | { |
---|
322 | $img = path::real(self::imagesPath($folder).'/'.$img); |
---|
323 | if (is_writable(dirname($img))) { |
---|
324 | @unlink($img); |
---|
325 | // Following lines should be replaced by a media thumbnails removal tool |
---|
326 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg'); |
---|
327 | @unlink(dirname($img).'/.'.basename($img,'.png').'_m.jpg'); |
---|
328 | @unlink(dirname($img).'/.'.basename($img,'.png').'_s.jpg'); |
---|
329 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.jpg'); |
---|
330 | @unlink(dirname($img).'/.'.basename($img,'.png').'_t.jpg'); |
---|
331 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.png'); |
---|
332 | @unlink(dirname($img).'/.'.basename($img,'.png').'_m.png'); |
---|
333 | @unlink(dirname($img).'/.'.basename($img,'.png').'_s.png'); |
---|
334 | @unlink(dirname($img).'/.'.basename($img,'.png').'_sq.png'); |
---|
335 | @unlink(dirname($img).'/.'.basename($img,'.png').'_t.png'); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | } |
---|