Changeset 3730:5c45a5df9a59 for inc/admin/lib.themeconfig.php
- Timestamp:
- 03/08/18 17:58:39 (7 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/admin/lib.themeconfig.php
r3340 r3730 10 10 # 11 11 # -- END LICENSE BLOCK ----------------------------------------- 12 if (!defined('DC_ADMIN_CONTEXT')) { return;}12 if (!defined('DC_ADMIN_CONTEXT')) {return;} 13 13 14 14 /** … … 30 30 * @return float computed ratio 31 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)) return 0; 38 $background = self::adjustColor($background); 39 if (($background == '') || (strlen($background) != 7)) return 0; 40 41 $l1 = (0.2126 * pow(hexdec(substr($color,1,2))/255,2.2)) + 42 (0.7152 * pow(hexdec(substr($color,3,2))/255,2.2)) + 43 (0.0722 * pow(hexdec(substr($color,5,2))/255,2.2)); 44 45 $l2 = (0.2126 * pow(hexdec(substr($background,1,2))/255,2.2)) + 46 (0.7152 * pow(hexdec(substr($background,3,2))/255,2.2)) + 47 (0.0722 * pow(hexdec(substr($background,5,2))/255,2.2)); 48 49 if ($l1 > $l2) { 50 $ratio = ($l1 + 0.05) / ($l2 + 0.05); 51 } else { 52 $ratio = ($l2 + 0.05) / ($l1 + 0.05); 53 } 54 return $ratio; 55 } 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 } 56 61 57 62 /** … … 64 69 * @return string WCAG contrast ratio level (AAA, AA or <nothing>) 65 70 */ 66 public static function contrastRatioLevel($ratio,$size,$bold=false)67 68 69 70 71 72 73 if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex|rem)?$/',$size,$m)) {74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 } 114 119 115 120 /** … … 123 128 * @return string contrast ratio including WCAG level 124 129 */ 125 public static function contrastRatio($color,$background,$size='',$bold=false)126 127 128 $ratio = self::computeContrastRatio($color,$background);129 $level = self::contrastRatioLevel($ratio,$size,$bold);130 131 sprintf(__('ratio %.1f'),$ratio).132 ($level != '' ? ' '.sprintf(__('(%s)'),$level) : '');133 134 135 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 } 136 141 137 142 /** … … 142 147 * @return string checked font size 143 148 */ 144 145 146 if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex|rem)?$/',$s,$m)) {147 148 149 150 return $m[1].$m[2];151 152 return null;153 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 } 154 159 155 160 /** … … 160 165 * @return string checked position 161 166 */ 162 163 164 if (!preg_match('/^[0-9]+(:[0-9]+)?$/',$p)) {165 return null;166 167 $p = explode(':',$p);168 169 return $p[0].(count($p) == 1 ? ':0' : ':'.$p[1]);170 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 } 171 176 172 177 /** … … 177 182 * @return string checked CSS color 178 183 */ 179 180 181 182 183 184 185 186 187 if (preg_match('/^[A-F0-9]{3,6}$/',$c)) {188 $c = '#'.$c;189 190 if (preg_match('/^#[A-F0-9]{6}$/',$c)) {191 192 193 if (preg_match('/^#[A-F0-9]{3,}$/',$c)) {194 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);195 196 197 198 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 } 199 204 200 205 /** … … 205 210 * @return string checked CSS 206 211 */ 207 208 209 210 211 212 public static function cleanCSS($css) 213 { 214 // TODO ? 215 return $css; 216 } 212 217 213 218 /** … … 218 223 * @return string real path of CSS 219 224 */ 220 221 222 223 return path::real($core->blog->public_path).'/'.$folder;224 225 public static function cssPath($folder) 226 { 227 global $core; 228 return path::real($core->blog->public_path) . '/' . $folder; 229 } 225 230 226 231 /** … … 231 236 * @return string CSS URL 232 237 */ 233 234 235 236 return $core->blog->settings->system->public_url.'/'.$folder;237 238 public static function cssURL($folder) 239 { 240 global $core; 241 return $core->blog->settings->system->public_url . '/' . $folder; 242 } 238 243 239 244 /** … … 245 250 * @return boolean true if CSS folder exists and may be written, else false 246 251 */ 247 public static function canWriteCss($folder,$create=false)248 249 250 251 252 $css= self::cssPath($folder);253 254 255 256 257 258 259 260 261 $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public'));262 263 264 265 266 267 268 269 270 271 $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/'.$folder));272 273 274 275 276 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 } 277 282 278 283 /** … … 284 289 * @param string $value value 285 290 */ 286 public static function prop(&$css,$selector,$prop,$value)287 288 289 290 291 291 public static function prop(&$css, $selector, $prop, $value) 292 { 293 if ($value) { 294 $css[$selector][$prop] = $value; 295 } 296 } 292 297 293 298 /** … … 300 305 * @param string $image image filename 301 306 */ 302 public static function backgroundImg($folder,&$css,$selector,$value,$image)303 304 $file = self::imagesPath($folder).'/'.$image;305 if ($value && file_exists($file)){306 $css[$selector]['background-image'] = 'url('.self::imagesURL($folder).'/'.$image.')';307 308 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 } 309 314 310 315 /** … … 315 320 * @param string $css CSS file content 316 321 */ 317 public static function writeCss($folder,$theme,$css)318 319 file_put_contents(self::cssPath($folder).'/'.$theme.'.css', $css);320 322 public static function writeCss($folder, $theme, $css) 323 { 324 file_put_contents(self::cssPath($folder) . '/' . $theme . '.css', $css); 325 } 321 326 322 327 /** … … 326 331 * @param string $theme CSS filename to be removed 327 332 */ 328 public static function dropCss($folder,$theme)329 330 $file = path::real(self::cssPath($folder).'/'.$theme.'.css');331 332 333 334 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 } 335 340 336 341 /** … … 341 346 * @return string CSS file URL 342 347 */ 343 344 345 346 $url= self::cssURL($folder);347 $path= self::cssPath($folder);348 349 if (file_exists($path.'/'.$theme.'.css')) {350 return $url.'/'.$theme.'.css';351 352 353 return null;354 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 } 355 360 356 361 /** … … 361 366 * @return string real path of folder 362 367 */ 363 364 365 366 return path::real($core->blog->public_path).'/'.$folder;367 368 public static function imagesPath($folder) 369 { 370 global $core; 371 return path::real($core->blog->public_path) . '/' . $folder; 372 } 368 373 369 374 /** … … 374 379 * @return string URL of images folder 375 380 */ 376 377 378 379 return $core->blog->settings->system->public_url.'/'.$folder;380 381 public static function imagesURL($folder) 382 { 383 global $core; 384 return $core->blog->settings->system->public_url . '/' . $folder; 385 } 381 386 382 387 /** … … 388 393 * @return boolean true if folder exists and may be written 389 394 */ 390 public static function canWriteImages($folder,$create=false)391 392 393 394 395 $imgs= self::imagesPath($folder);396 397 398 $core->error->add(__('At least one of the following functions is not available: '.399 400 401 402 403 404 405 406 407 408 409 410 $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public'));411 412 413 414 415 416 417 418 419 420 $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/'.$folder));421 422 423 424 425 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 } 426 431 427 432 /** … … 434 439 * @return string full pathname of uploaded image 435 440 */ 436 public static function uploadImage($folder,$f,$width=0)437 438 if (!self::canWriteImages($folder,true)) {439 440 441 442 443 444 445 446 447 448 449 $dest = self::imagesPath($folder).'/uploaded'.($type == 'image/png' ? '.png' : '.jpg');450 451 if (@move_uploaded_file($f['tmp_name'],$dest) === false) {452 453 454 455 456 457 458 throw new Exception(sprintf(__('Uploaded image is not %s pixels wide.'),$width));459 460 461 462 463 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 } 464 469 465 470 /** … … 469 474 * @param string $img image filename 470 475 */ 471 public static function dropImage($folder,$img)472 473 474 475 $img = path::real(self::imagesPath($folder).'/'.$img);476 477 478 479 480 481 482 483 484 485 486 487 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 } 488 493 }
Note: See TracChangeset
for help on using the changeset viewer.