Dotclear


Ignore:
Timestamp:
03/08/18 17:58:39 (7 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Code formatting (PSR-2)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/admin/lib.themeconfig.php

    r3340 r3730  
    1010# 
    1111# -- END LICENSE BLOCK ----------------------------------------- 
    12 if (!defined('DC_ADMIN_CONTEXT')) { return; } 
     12if (!defined('DC_ADMIN_CONTEXT')) {return;} 
    1313 
    1414/** 
     
    3030 * @return float             computed ratio 
    3131 */ 
    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    } 
    5661 
    5762/** 
     
    6469 * @return string         WCAG contrast ratio level (AAA, AA or <nothing>) 
    6570 */ 
    66      public static function contrastRatioLevel($ratio,$size,$bold=false) 
    67      { 
    68           if ($size == '') { 
    69                return ''; 
    70           } 
    71  
    72           // Eval font size in em (assume base font size in pixels equal to 16) 
    73           if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex|rem)?$/',$size,$m)) { 
    74                if (empty($m[2])) { 
    75                     $m[2] = 'em'; 
    76                } 
    77           } else { 
    78                return ''; 
    79           } 
    80           switch ($m[2]) { 
    81                case '%': 
    82                     $s = (float) $m[1] / 100; 
    83                     break; 
    84                case 'pt': 
    85                     $s = (float) $m[1] / 12; 
    86                     break; 
    87                case 'px': 
    88                     $s = (float) $m[1] / 16; 
    89                     break; 
    90                case 'em': 
    91                     $s = (float) $m[1]; 
    92                     break; 
    93                case 'ex': 
    94                     $s = (float) $m[1] / 2; 
    95                     break; 
    96                default: 
    97                     return ''; 
    98           } 
    99  
    100           $large = ((($s > 1.5) && ($bold == false)) || (($s > 1.2) && ($bold == true))); 
    101  
    102           // Check ratio 
    103           if ($ratio > 7) { 
    104                return 'AAA'; 
    105           } elseif (($ratio > 4.5) && $large) { 
    106                return 'AAA'; 
    107           } elseif ($ratio > 4.5) { 
    108                return 'AA'; 
    109           } elseif (($ratio > 3) && $large) { 
    110                return 'AA'; 
    111           } 
    112           return ''; 
    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    } 
    114119 
    115120/** 
     
    123128 * @return string              contrast ratio including WCAG level 
    124129 */ 
    125      public static function contrastRatio($color,$background,$size='',$bold=false) 
    126      { 
    127           if (($color != '') && ($background != '')) { 
    128                $ratio = self::computeContrastRatio($color,$background); 
    129                $level = self::contrastRatioLevel($ratio,$size,$bold); 
    130                return 
    131                     sprintf(__('ratio %.1f'),$ratio). 
    132                     ($level != '' ? ' '.sprintf(__('(%s)'),$level) : ''); 
    133           } 
    134           return ''; 
    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    } 
    136141 
    137142/** 
     
    142147 * @return string    checked font size 
    143148 */ 
    144      public static function adjustFontSize($s) 
    145      { 
    146           if (preg_match('/^([0-9.]+)\s*(%|pt|px|em|ex|rem)?$/',$s,$m)) { 
    147                if (empty($m[2])) { 
    148                     $m[2] = 'em'; 
    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    } 
    154159 
    155160/** 
     
    160165 * @return string    checked position 
    161166 */ 
    162      public static function adjustPosition($p) 
    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    } 
    171176 
    172177/** 
     
    177182 * @return string    checked CSS color 
    178183 */ 
    179      public static function adjustColor($c) 
    180      { 
    181           if ($c === '') { 
    182                return ''; 
    183           } 
    184  
    185           $c = strtoupper($c); 
    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                return $c; 
    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           return ''; 
    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    } 
    199204 
    200205/** 
     
    205210 * @return string      checked CSS 
    206211 */ 
    207      public static function cleanCSS($css) 
    208      { 
    209           // TODO ? 
    210           return $css; 
    211      } 
     212    public static function cleanCSS($css) 
     213    { 
     214        // TODO ? 
     215        return $css; 
     216    } 
    212217 
    213218/** 
     
    218223 * @return string         real path of CSS 
    219224 */ 
    220      public static function cssPath($folder) 
    221      { 
    222           global $core; 
    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    } 
    225230 
    226231/** 
     
    231236 * @return string         CSS URL 
    232237 */ 
    233      public static function cssURL($folder) 
    234      { 
    235           global $core; 
    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    } 
    238243 
    239244/** 
     
    245250 * @return boolean          true if CSS folder exists and may be written, else false 
    246251 */ 
    247      public static function canWriteCss($folder,$create=false) 
    248      { 
    249           global $core; 
    250  
    251           $public = path::real($core->blog->public_path); 
    252           $css = self::cssPath($folder); 
    253  
    254           if (!is_dir($public)) { 
    255                $core->error->add(__('The \'public\' directory does not exist.')); 
    256                return false; 
    257           } 
    258  
    259           if (!is_dir($css)) { 
    260                if (!is_writable($public)) { 
    261                     $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public')); 
    262                     return false; 
    263                } 
    264                if ($create) { 
    265                     files::makeDir($css); 
    266                } 
    267                return true; 
    268           } 
    269  
    270           if (!is_writable($css)) { 
    271                $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/'.$folder)); 
    272                return false; 
    273           } 
    274  
    275           return true; 
    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    } 
    277282 
    278283/** 
     
    284289 * @param  string $value    value 
    285290 */ 
    286      public static function prop(&$css,$selector,$prop,$value) 
    287      { 
    288           if ($value) { 
    289                $css[$selector][$prop] = $value; 
    290           } 
    291      } 
     291    public static function prop(&$css, $selector, $prop, $value) 
     292    { 
     293        if ($value) { 
     294            $css[$selector][$prop] = $value; 
     295        } 
     296    } 
    292297 
    293298/** 
     
    300305 * @param  string $image    image filename 
    301306 */ 
    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    } 
    309314 
    310315/** 
     
    315320 * @param  string $css    CSS file content 
    316321 */ 
    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    } 
    321326 
    322327/** 
     
    326331 * @param  string $theme  CSS filename to be removed 
    327332 */ 
    328      public static function dropCss($folder,$theme) 
    329      { 
    330           $file = path::real(self::cssPath($folder).'/'.$theme.'.css'); 
    331           if (is_writable(dirname($file))) { 
    332                @unlink($file); 
    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    } 
    335340 
    336341/** 
     
    341346 * @return string         CSS file URL 
    342347 */ 
    343      public static function publicCssUrlHelper($folder) 
    344      { 
    345           $theme = $GLOBALS['core']->blog->settings->system->theme; 
    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    } 
    355360 
    356361/** 
     
    361366 * @return string         real path of folder 
    362367 */ 
    363      public static function imagesPath($folder) 
    364      { 
    365           global $core; 
    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    } 
    368373 
    369374/** 
     
    374379 * @return string         URL of images folder 
    375380 */ 
    376      public static function imagesURL($folder) 
    377      { 
    378           global $core; 
    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    } 
    381386 
    382387/** 
     
    388393 * @return boolean          true if folder exists and may be written 
    389394 */ 
    390      public static function canWriteImages($folder,$create=false) 
    391      { 
    392           global $core; 
    393  
    394           $public = path::real($core->blog->public_path); 
    395           $imgs = self::imagesPath($folder); 
    396  
    397           if (!function_exists('imagecreatetruecolor') || !function_exists('imagepng') || !function_exists('imagecreatefrompng')) { 
    398                $core->error->add(__('At least one of the following functions is not available: '. 
    399                     'imagecreatetruecolor, imagepng & imagecreatefrompng.')); 
    400                return false; 
    401           } 
    402  
    403           if (!is_dir($public)) { 
    404                $core->error->add(__('The \'public\' directory does not exist.')); 
    405                return false; 
    406           } 
    407  
    408           if (!is_dir($imgs)) { 
    409                if (!is_writable($public)) { 
    410                     $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public')); 
    411                     return false; 
    412                } 
    413                if ($create) { 
    414                     files::makeDir($imgs); 
    415                } 
    416                return true; 
    417           } 
    418  
    419           if (!is_writable($imgs)) { 
    420                $core->error->add(sprintf(__('The \'%s\' directory cannot be modified.'),'public/'.$folder)); 
    421                return false; 
    422           } 
    423  
    424           return true; 
    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    } 
    426431 
    427432/** 
     
    434439 * @return string         full pathname of uploaded image 
    435440 */ 
    436      public static function uploadImage($folder,$f,$width=0) 
    437      { 
    438           if (!self::canWriteImages($folder,true)) { 
    439                throw new Exception(__('Unable to create images.')); 
    440           } 
    441  
    442           $name = $f['name']; 
    443           $type = files::getMimeType($name); 
    444  
    445           if ($type != 'image/jpeg' && $type != 'image/png') { 
    446                throw new Exception(__('Invalid file type.')); 
    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                throw new Exception(__('An error occurred while writing the file.')); 
    453           } 
    454  
    455           if ($width) { 
    456                $s = getimagesize($dest); 
    457                if ($s[0] != $width) { 
    458                     throw new Exception(sprintf(__('Uploaded image is not %s pixels wide.'),$width)); 
    459                } 
    460           } 
    461  
    462           return $dest; 
    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    } 
    464469 
    465470/** 
     
    469474 * @param  string $img    image filename 
    470475 */ 
    471      public static function dropImage($folder,$img) 
    472      { 
    473           global $core; 
    474  
    475           $img = path::real(self::imagesPath($folder).'/'.$img); 
    476           if (is_writable(dirname($img))) { 
    477                // Delete thumbnails if any 
    478                try { 
    479                     $media = new dcMedia($core); 
    480                     $media->imageThumbRemove($img); 
    481                } catch (Exception $e) { 
    482                     $core->error->add($e->getMessage()); 
    483                } 
    484                // Delete image 
    485                @unlink($img); 
    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    } 
    488493} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map