| 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 | |
|---|
| 24 | /** |
|---|
| 25 | * Compute contrast ratio between two colors |
|---|
| 26 | * |
|---|
| 27 | * @param string $color text color |
|---|
| 28 | * @param string $background background color |
|---|
| 29 | * |
|---|
| 30 | * @return float computed ratio |
|---|
| 31 | */ |
|---|
| 32 | public static function computeContrastRatio($color, $background) |
|---|
| 33 | { |
|---|
| 34 | // Compute contrast ratio between two colors |
|---|
| 35 | |
|---|
| 36 | $color = self::adjustColor($color); |
|---|
| 37 | if (($color == '') || (strlen($color) != 7)) { |
|---|
| 38 | return 0; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | $background = self::adjustColor($background); |
|---|
| 42 | if (($background == '') || (strlen($background) != 7)) { |
|---|
| 43 | return 0; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | $l1 = (0.2126 * pow(hexdec(substr($color, 1, 2)) / 255, 2.2)) + |
|---|
| 47 | (0.7152 * pow(hexdec(substr($color, 3, 2)) / 255, 2.2)) + |
|---|
| 48 | (0.0722 * pow(hexdec(substr($color, 5, 2)) / 255, 2.2)); |
|---|
| 49 | |
|---|
| 50 | $l2 = (0.2126 * pow(hexdec(substr($background, 1, 2)) / 255, 2.2)) + |
|---|
| 51 | (0.7152 * pow(hexdec(substr($background, 3, 2)) / 255, 2.2)) + |
|---|
| 52 | (0.0722 * pow(hexdec(substr($background, 5, 2)) / 255, 2.2)); |
|---|
| 53 | |
|---|
| 54 | if ($l1 > $l2) { |
|---|
| 55 | $ratio = ($l1 + 0.05) / ($l2 + 0.05); |
|---|
| 56 | } else { |
|---|
| 57 | $ratio = ($l2 + 0.05) / ($l1 + 0.05); |
|---|
| 58 | } |
|---|
| 59 | return $ratio; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Compute WCAG contrast ration level |
|---|
| 64 | * |
|---|
| 65 | * @param float $ratio computed ratio between foreground and backround color |
|---|
| 66 | * @param string $size font size as defined in CSS |
|---|
| 67 | * @param boolean $bold true if bold font |
|---|
| 68 | * |
|---|
| 69 | * @return string WCAG contrast ratio level (AAA, AA or <nothing>) |
|---|
| 70 | */ |
|---|
| 71 | public static function contrastRatioLevel($ratio, $size, $bold = false) |
|---|
| 72 | { |
|---|
| 73 | if ($size == '') { |
|---|
| 74 | return ''; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // Eval font size in em (assume base font size in pixels equal to 16) |
|---|
| 78 | if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex|rem)?$/', $size, $m)) { |
|---|
| 79 | if (empty($m[2])) { |
|---|
| 80 | $m[2] = 'em'; |
|---|
| 81 | } |
|---|
| 82 | } else { |
|---|
| 83 | return ''; |
|---|
| 84 | } |
|---|
| 85 | switch ($m[2]) { |
|---|
| 86 | case '%': |
|---|
| 87 | $s = (float) $m[1] / 100; |
|---|
| 88 | break; |
|---|
| 89 | case 'pt': |
|---|
| 90 | $s = (float) $m[1] / 12; |
|---|
| 91 | break; |
|---|
| 92 | case 'px': |
|---|
| 93 | $s = (float) $m[1] / 16; |
|---|
| 94 | break; |
|---|
| 95 | case 'em': |
|---|
| 96 | $s = (float) $m[1]; |
|---|
| 97 | break; |
|---|
| 98 | case 'ex': |
|---|
| 99 | $s = (float) $m[1] / 2; |
|---|
| 100 | break; |
|---|
| 101 | default: |
|---|
| 102 | return ''; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | $large = ((($s > 1.5) && ($bold == false)) || (($s > 1.2) && ($bold == true))); |
|---|
| 106 | |
|---|
| 107 | // Check ratio |
|---|
| 108 | if ($ratio > 7) { |
|---|
| 109 | return 'AAA'; |
|---|
| 110 | } elseif (($ratio > 4.5) && $large) { |
|---|
| 111 | return 'AAA'; |
|---|
| 112 | } elseif ($ratio > 4.5) { |
|---|
| 113 | return 'AA'; |
|---|
| 114 | } elseif (($ratio > 3) && $large) { |
|---|
| 115 | return 'AA'; |
|---|
| 116 | } |
|---|
| 117 | return ''; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Return full information about constrat ratio |
|---|
| 122 | * |
|---|
| 123 | * @param string $color text color |
|---|
| 124 | * @param string $background background color |
|---|
| 125 | * @param string $size font size |
|---|
| 126 | * @param boolean $bold bold font |
|---|
| 127 | * |
|---|
| 128 | * @return string contrast ratio including WCAG level |
|---|
| 129 | */ |
|---|
| 130 | public static function contrastRatio($color, $background, $size = '', $bold = false) |
|---|
| 131 | { |
|---|
| 132 | if (($color != '') && ($background != '')) { |
|---|
| 133 | $ratio = self::computeContrastRatio($color, $background); |
|---|
| 134 | $level = self::contrastRatioLevel($ratio, $size, $bold); |
|---|
| 135 | return |
|---|
| 136 | sprintf(__('ratio %.1f'), $ratio) . |
|---|
| 137 | ($level != '' ? ' ' . sprintf(__('(%s)'), $level) : ''); |
|---|
| 138 | } |
|---|
| 139 | return ''; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * Check font size |
|---|
| 144 | * |
|---|
| 145 | * @param string $s font size |
|---|
| 146 | * |
|---|
| 147 | * @return string checked font size |
|---|
| 148 | */ |
|---|
| 149 | public static function adjustFontSize($s) |
|---|
| 150 | { |
|---|
| 151 | if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex|rem)?$/', $s, $m)) { |
|---|
| 152 | if (empty($m[2])) { |
|---|
| 153 | $m[2] = 'em'; |
|---|
| 154 | } |
|---|
| 155 | return $m[1] . $m[2]; |
|---|
| 156 | } |
|---|
| 157 | return; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /** |
|---|
| 161 | * Check object position, should be x:y |
|---|
| 162 | * |
|---|
| 163 | * @param string $p position |
|---|
| 164 | * |
|---|
| 165 | * @return string checked position |
|---|
| 166 | */ |
|---|
| 167 | public static function adjustPosition($p) |
|---|
| 168 | { |
|---|
| 169 | if (!preg_match('/^[0-9]+(:[0-9]+)?$/', $p)) { |
|---|
| 170 | return; |
|---|
| 171 | } |
|---|
| 172 | $p = explode(':', $p); |
|---|
| 173 | |
|---|
| 174 | return $p[0] . (count($p) == 1 ? ':0' : ':' . $p[1]); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | /** |
|---|
| 178 | * Check a CSS color |
|---|
| 179 | * |
|---|
| 180 | * @param string $c CSS color |
|---|
| 181 | * |
|---|
| 182 | * @return string checked CSS color |
|---|
| 183 | */ |
|---|
| 184 | public static function adjustColor($c) |
|---|
| 185 | { |
|---|
| 186 | if ($c === '') { |
|---|
| 187 | return ''; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | $c = strtoupper($c); |
|---|
| 191 | |
|---|
| 192 | if (preg_match('/^[A-F0-9]{3,6}$/', $c)) { |
|---|
| 193 | $c = '#' . $c; |
|---|
| 194 | } |
|---|
| 195 | if (preg_match('/^#[A-F0-9]{6}$/', $c)) { |
|---|
| 196 | return $c; |
|---|
| 197 | } |
|---|
| 198 | if (preg_match('/^#[A-F0-9]{3,}$/', $c)) { |
|---|
| 199 | 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); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | return ''; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /** |
|---|
| 206 | * Check and clean CSS |
|---|
| 207 | * |
|---|
| 208 | * @param string $css CSS to be checked |
|---|
| 209 | * |
|---|
| 210 | * @return string checked CSS |
|---|
| 211 | */ |
|---|
| 212 | public static function cleanCSS($css) |
|---|
| 213 | { |
|---|
| 214 | // TODO ? |
|---|
| 215 | return $css; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | /** |
|---|
| 219 | * Return real path of a user defined CSS |
|---|
| 220 | * |
|---|
| 221 | * @param string $folder CSS folder |
|---|
| 222 | * |
|---|
| 223 | * @return string real path of CSS |
|---|
| 224 | */ |
|---|
| 225 | public static function cssPath($folder) |
|---|
| 226 | { |
|---|
| 227 | global $core; |
|---|
| 228 | return path::real($core->blog->public_path) . '/' . $folder; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | /** |
|---|
| 232 | * Retirn URL of a user defined CSS |
|---|
| 233 | * |
|---|
| 234 | * @param string $folder CSS folder |
|---|
| 235 | * |
|---|
| 236 | * @return string CSS URL |
|---|
| 237 | */ |
|---|
| 238 | public static function cssURL($folder) |
|---|
| 239 | { |
|---|
| 240 | global $core; |
|---|
| 241 | return $core->blog->settings->system->public_url . '/' . $folder; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /** |
|---|
| 245 | * Check if user defined CSS may be written |
|---|
| 246 | * |
|---|
| 247 | * @param string $folder CSS folder |
|---|
| 248 | * @param boolean $create create CSS folder if necessary |
|---|
| 249 | * |
|---|
| 250 | * @return boolean true if CSS folder exists and may be written, else false |
|---|
| 251 | */ |
|---|
| 252 | public static function canWriteCss($folder, $create = false) |
|---|
| 253 | { |
|---|
| 254 | global $core; |
|---|
| 255 | |
|---|
| 256 | $public = path::real($core->blog->public_path); |
|---|
| 257 | $css = self::cssPath($folder); |
|---|
| 258 | |
|---|
| 259 | if (!is_dir($public)) { |
|---|
| 260 | $core->error->add(__('The \'public\' directory does not exist.')); |
|---|
| 261 | return false; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | if (!is_dir($css)) { |
|---|
| 265 | if (!is_writable($public)) { |
|---|
| 266 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'), 'public')); |
|---|
| 267 | return false; |
|---|
| 268 | } |
|---|
| 269 | if ($create) { |
|---|
| 270 | files::makeDir($css); |
|---|
| 271 | } |
|---|
| 272 | return true; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | if (!is_writable($css)) { |
|---|
| 276 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'), 'public/' . $folder)); |
|---|
| 277 | return false; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | return true; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | /** |
|---|
| 284 | * Store CSS property value in associated array |
|---|
| 285 | * |
|---|
| 286 | * @param array $css CSS associated array |
|---|
| 287 | * @param string $selector selector |
|---|
| 288 | * @param string $prop property |
|---|
| 289 | * @param string $value value |
|---|
| 290 | */ |
|---|
| 291 | public static function prop(&$css, $selector, $prop, $value) |
|---|
| 292 | { |
|---|
| 293 | if ($value) { |
|---|
| 294 | $css[$selector][$prop] = $value; |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | /** |
|---|
| 299 | * Store background image property in CSS associated array |
|---|
| 300 | * |
|---|
| 301 | * @param string $folder image folder |
|---|
| 302 | * @param array $css CSS associated array |
|---|
| 303 | * @param string $selector selector |
|---|
| 304 | * @param boolean $value false for default, true if image should be set |
|---|
| 305 | * @param string $image image filename |
|---|
| 306 | */ |
|---|
| 307 | public static function backgroundImg($folder, &$css, $selector, $value, $image) |
|---|
| 308 | { |
|---|
| 309 | $file = self::imagesPath($folder) . '/' . $image; |
|---|
| 310 | if ($value && file_exists($file)) { |
|---|
| 311 | $css[$selector]['background-image'] = 'url(' . self::imagesURL($folder) . '/' . $image . ')'; |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | /** |
|---|
| 316 | * Write CSS file |
|---|
| 317 | * |
|---|
| 318 | * @param string $folder CSS folder |
|---|
| 319 | * @param string $theme CSS filename |
|---|
| 320 | * @param string $css CSS file content |
|---|
| 321 | */ |
|---|
| 322 | public static function writeCss($folder, $theme, $css) |
|---|
| 323 | { |
|---|
| 324 | file_put_contents(self::cssPath($folder) . '/' . $theme . '.css', $css); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /** |
|---|
| 328 | * Delete CSS file |
|---|
| 329 | * |
|---|
| 330 | * @param string $folder CSS folder |
|---|
| 331 | * @param string $theme CSS filename to be removed |
|---|
| 332 | */ |
|---|
| 333 | public static function dropCss($folder, $theme) |
|---|
| 334 | { |
|---|
| 335 | $file = path::real(self::cssPath($folder) . '/' . $theme . '.css'); |
|---|
| 336 | if (is_writable(dirname($file))) { |
|---|
| 337 | @unlink($file); |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | /** |
|---|
| 342 | * Return public URL of user defined CSS |
|---|
| 343 | * |
|---|
| 344 | * @param string $folder CSS folder |
|---|
| 345 | * |
|---|
| 346 | * @return string CSS file URL |
|---|
| 347 | */ |
|---|
| 348 | public static function publicCssUrlHelper($folder) |
|---|
| 349 | { |
|---|
| 350 | $theme = $GLOBALS['core']->blog->settings->system->theme; |
|---|
| 351 | $url = self::cssURL($folder); |
|---|
| 352 | $path = self::cssPath($folder); |
|---|
| 353 | |
|---|
| 354 | if (file_exists($path . '/' . $theme . '.css')) { |
|---|
| 355 | return $url . '/' . $theme . '.css'; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | return; |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * Return real path of folder images |
|---|
| 363 | * |
|---|
| 364 | * @param string $folder images folder |
|---|
| 365 | * |
|---|
| 366 | * @return string real path of folder |
|---|
| 367 | */ |
|---|
| 368 | public static function imagesPath($folder) |
|---|
| 369 | { |
|---|
| 370 | global $core; |
|---|
| 371 | return path::real($core->blog->public_path) . '/' . $folder; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | /** |
|---|
| 375 | * Return URL of images folder |
|---|
| 376 | * |
|---|
| 377 | * @param string $folder images folder |
|---|
| 378 | * |
|---|
| 379 | * @return string URL of images folder |
|---|
| 380 | */ |
|---|
| 381 | public static function imagesURL($folder) |
|---|
| 382 | { |
|---|
| 383 | global $core; |
|---|
| 384 | return $core->blog->settings->system->public_url . '/' . $folder; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | /** |
|---|
| 388 | * Check if images folder exists and may be written |
|---|
| 389 | * |
|---|
| 390 | * @param string $folder images folder |
|---|
| 391 | * @param boolean $create create the folder if not exists |
|---|
| 392 | * |
|---|
| 393 | * @return boolean true if folder exists and may be written |
|---|
| 394 | */ |
|---|
| 395 | public static function canWriteImages($folder, $create = false) |
|---|
| 396 | { |
|---|
| 397 | global $core; |
|---|
| 398 | |
|---|
| 399 | $public = path::real($core->blog->public_path); |
|---|
| 400 | $imgs = self::imagesPath($folder); |
|---|
| 401 | |
|---|
| 402 | if (!function_exists('imagecreatetruecolor') || !function_exists('imagepng') || !function_exists('imagecreatefrompng')) { |
|---|
| 403 | $core->error->add(__('At least one of the following functions is not available: ' . |
|---|
| 404 | 'imagecreatetruecolor, imagepng & imagecreatefrompng.')); |
|---|
| 405 | return false; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | if (!is_dir($public)) { |
|---|
| 409 | $core->error->add(__('The \'public\' directory does not exist.')); |
|---|
| 410 | return false; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | if (!is_dir($imgs)) { |
|---|
| 414 | if (!is_writable($public)) { |
|---|
| 415 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'), 'public')); |
|---|
| 416 | return false; |
|---|
| 417 | } |
|---|
| 418 | if ($create) { |
|---|
| 419 | files::makeDir($imgs); |
|---|
| 420 | } |
|---|
| 421 | return true; |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | if (!is_writable($imgs)) { |
|---|
| 425 | $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'), 'public/' . $folder)); |
|---|
| 426 | return false; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | return true; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | /** |
|---|
| 433 | * Upload an image in images folder |
|---|
| 434 | * |
|---|
| 435 | * @param string $folder images folder |
|---|
| 436 | * @param string $f selected image file (as $_FILES[<file input fieldname>]) |
|---|
| 437 | * @param int $width check accurate width of uploaded image if <> 0 |
|---|
| 438 | * |
|---|
| 439 | * @return string full pathname of uploaded image |
|---|
| 440 | */ |
|---|
| 441 | public static function uploadImage($folder, $f, $width = 0) |
|---|
| 442 | { |
|---|
| 443 | if (!self::canWriteImages($folder, true)) { |
|---|
| 444 | throw new Exception(__('Unable to create images.')); |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | $name = $f['name']; |
|---|
| 448 | $type = files::getMimeType($name); |
|---|
| 449 | |
|---|
| 450 | if ($type != 'image/jpeg' && $type != 'image/png') { |
|---|
| 451 | throw new Exception(__('Invalid file type.')); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | $dest = self::imagesPath($folder) . '/uploaded' . ($type == 'image/png' ? '.png' : '.jpg'); |
|---|
| 455 | |
|---|
| 456 | if (@move_uploaded_file($f['tmp_name'], $dest) === false) { |
|---|
| 457 | throw new Exception(__('An error occurred while writing the file.')); |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | if ($width) { |
|---|
| 461 | $s = getimagesize($dest); |
|---|
| 462 | if ($s[0] != $width) { |
|---|
| 463 | throw new Exception(sprintf(__('Uploaded image is not %s pixels wide.'), $width)); |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | return $dest; |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | /** |
|---|
| 471 | * Delete an image from images folder (with its thumbnails if any) |
|---|
| 472 | * |
|---|
| 473 | * @param string $folder images folder |
|---|
| 474 | * @param string $img image filename |
|---|
| 475 | */ |
|---|
| 476 | public static function dropImage($folder, $img) |
|---|
| 477 | { |
|---|
| 478 | global $core; |
|---|
| 479 | |
|---|
| 480 | $img = path::real(self::imagesPath($folder) . '/' . $img); |
|---|
| 481 | if (is_writable(dirname($img))) { |
|---|
| 482 | // Delete thumbnails if any |
|---|
| 483 | try { |
|---|
| 484 | $media = new dcMedia($core); |
|---|
| 485 | $media->imageThumbRemove($img); |
|---|
| 486 | } catch (Exception $e) { |
|---|
| 487 | $core->error->add($e->getMessage()); |
|---|
| 488 | } |
|---|
| 489 | // Delete image |
|---|
| 490 | @unlink($img); |
|---|
| 491 | } |
|---|
| 492 | } |
|---|
| 493 | } |
|---|