lib/Common/Common.php line 182

Open in your IDE?
  1. <?php
  2. namespace OlaSoft;
  3. use JetBrains\PhpStorm\ArrayShape;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\Config\Definition\Exception\Exception;
  6. use Symfony\Component\Intl\Countries;
  7. use Symfony\Component\Intl\Languages;
  8. use Symfony\Component\Yaml\Yaml;
  9. class Common{
  10.     public static function downloadPDF($file,$name null): void
  11.     {
  12.         header('Content-type: application/pdf');
  13.         header('Content-Disposition: attachment; '. ($name 'filename="'.self::slug($name).'.pdf"' ''));
  14.         readfile($file);
  15.     }
  16.     public static function generatePassword($n 10): string
  17.     {
  18.         $r "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  19.         $r str_shuffle($r);
  20.         $r uniqid($r,true);
  21.         $r substr($r,0,$n);
  22.         return $r;
  23.     }
  24.     public static function generateToken($n 30): string
  25.     {
  26.         return bin2hex(random_bytes($n));
  27.     }
  28.     public static function generateCode($n 5): string
  29.     {
  30.         $r "0123456789";
  31.         $r str_shuffle($r);
  32.         $r uniqid($r,true);
  33.         $r substr($r,0,$n);
  34.         return $r;
  35.     }
  36.     public static function generateName(): string
  37.     {
  38.         $generateName microtime();
  39.         $generateNameTable preg_split('/[\s,\.]/',$generateName);
  40.         $generateName $generateNameTable[0].$generateNameTable[1].$generateNameTable[2];
  41.         return $generateName;
  42.     }
  43.     public static function transliterateString($txt): array|string
  44.     {
  45.         $transliterationTable = array('œ' => 'oe''á' => 'a''Á' => 'A''à' => 'a''À' => 'A''ă' => 'a''Ă' => 'A''â' => 'a''Â' => 'A''å' => 'a''Å' => 'A''ã' => 'a''Ã' => 'A''ą' => 'a''Ą' => 'A''ā' => 'a''Ā' => 'A''ä' => 'ae''Ä' => 'AE''æ' => 'ae''Æ' => 'AE''ḃ' => 'b''Ḃ' => 'B''ć' => 'c''Ć' => 'C''ĉ' => 'c''Ĉ' => 'C''č' => 'c''Č' => 'C''ċ' => 'c''Ċ' => 'C''ç' => 'c''Ç' => 'C''ď' => 'd''Ď' => 'D''ḋ' => 'd''Ḋ' => 'D''đ' => 'd''Đ' => 'D''ð' => 'dh''Ð' => 'Dh''é' => 'e''É' => 'E''è' => 'e''È' => 'E''ĕ' => 'e''Ĕ' => 'E''ê' => 'e''Ê' => 'E''ě' => 'e''Ě' => 'E''ë' => 'e''Ë' => 'E''ė' => 'e''Ė' => 'E''ę' => 'e''Ę' => 'E''ē' => 'e''Ē' => 'E''ḟ' => 'f''Ḟ' => 'F''ƒ' => 'f''Ƒ' => 'F''ğ' => 'g''Ğ' => 'G''ĝ' => 'g''Ĝ' => 'G''ġ' => 'g''Ġ' => 'G''ģ' => 'g''Ģ' => 'G''ĥ' => 'h''Ĥ' => 'H''ħ' => 'h''Ħ' => 'H''í' => 'i''Í' => 'I''ì' => 'i''Ì' => 'I''î' => 'i''Î' => 'I''ï' => 'i''Ï' => 'I''ĩ' => 'i''Ĩ' => 'I''į' => 'i''Į' => 'I''ī' => 'i''Ī' => 'I''ĵ' => 'j''Ĵ' => 'J''ķ' => 'k''Ķ' => 'K''ĺ' => 'l''Ĺ' => 'L''ľ' => 'l''Ľ' => 'L''ļ' => 'l''Ļ' => 'L''ł' => 'l''Ł' => 'L''ṁ' => 'm''Ṁ' => 'M''ń' => 'n''Ń' => 'N''ň' => 'n''Ň' => 'N''ñ' => 'n''Ñ' => 'N''ņ' => 'n''Ņ' => 'N''ó' => 'o''Ó' => 'O''ò' => 'o''Ò' => 'O''ô' => 'o''Ô' => 'O''ő' => 'o''Ő' => 'O''õ' => 'o''Õ' => 'O''ø' => 'oe''Ø' => 'OE''ō' => 'o''Ō' => 'O''ơ' => 'o''Ơ' => 'O''ö' => 'oe''Ö' => 'OE''ṗ' => 'p''Ṗ' => 'P''ŕ' => 'r''Ŕ' => 'R''ř' => 'r''Ř' => 'R''ŗ' => 'r''Ŗ' => 'R''ś' => 's''Ś' => 'S''ŝ' => 's''Ŝ' => 'S''š' => 's''Š' => 'S''ṡ' => 's''Ṡ' => 'S''ş' => 's''Ş' => 'S''ș' => 's''Ș' => 'S''ß' => 'SS''ť' => 't''Ť' => 'T''ṫ' => 't''Ṫ' => 'T''ţ' => 't''Ţ' => 'T''ț' => 't''Ț' => 'T''ŧ' => 't''Ŧ' => 'T''ú' => 'u''Ú' => 'U''ù' => 'u''Ù' => 'U''ŭ' => 'u''Ŭ' => 'U''û' => 'u''Û' => 'U''ů' => 'u''Ů' => 'U''ű' => 'u''Ű' => 'U''ũ' => 'u''Ũ' => 'U''ų' => 'u''Ų' => 'U''ū' => 'u''Ū' => 'U''ư' => 'u''Ư' => 'U''ü' => 'ue''Ü' => 'UE''ẃ' => 'w''Ẃ' => 'W''ẁ' => 'w''Ẁ' => 'W''ŵ' => 'w''Ŵ' => 'W''ẅ' => 'w''Ẅ' => 'W''ý' => 'y''Ý' => 'Y''ỳ' => 'y''Ỳ' => 'Y''ŷ' => 'y''Ŷ' => 'Y''ÿ' => 'y''Ÿ' => 'Y''ź' => 'z''Ź' => 'Z''ž' => 'z''Ž' => 'Z''ż' => 'z''Ż' => 'Z''þ' => 'th''Þ' => 'Th''µ' => 'u''а' => 'a''А' => 'a''б' => 'b''Б' => 'b''в' => 'v''В' => 'v''г' => 'g''Г' => 'g''д' => 'd''Д' => 'd''е' => 'e''Е' => 'E''ё' => 'e''Ё' => 'E''ж' => 'zh''Ж' => 'zh''з' => 'z''З' => 'z''и' => 'i''И' => 'i''й' => 'j''Й' => 'j''к' => 'k''К' => 'k''л' => 'l''Л' => 'l''м' => 'm''М' => 'm''н' => 'n''Н' => 'n''о' => 'o''О' => 'o''п' => 'p''П' => 'p''р' => 'r''Р' => 'r''с' => 's''С' => 's''т' => 't''Т' => 't''у' => 'u''У' => 'u''ф' => 'f''Ф' => 'f''х' => 'h''Х' => 'h''ц' => 'c''Ц' => 'c''ч' => 'ch''Ч' => 'ch''ш' => 'sh''Ш' => 'sh''щ' => 'sch''Щ' => 'sch''ъ' => '''Ъ' => '''ы' => 'y''Ы' => 'y''ь' => '''Ь' => '''э' => 'e''Э' => 'e''ю' => 'ju''Ю' => 'ju''я' => 'ja''Я' => 'ja');
  46.         return str_replace(array_keys($transliterationTable), array_values($transliterationTable), $txt);
  47.     }
  48.     public static function slug($text){
  49.         $t1 "/\,|;|\?|!|:|\(|\)|\[|\]|\{|\}|'|’| |«|»|°|\*|\+|=|\\|\//";
  50.         $t2 "/-+((.{1,3}|pour)-+((.{1,3}|pour)-+)*)/";
  51.         $slug preg_replace(["/\s+/",$t1,$t2,"/-+/"],"-",strtolower(Common::transliterateString($text)));
  52.         return $slug;
  53.     }
  54.     public static function isMobile(){
  55.         $isMobile $_SESSION['isMobile'] ?? null;
  56.         if($isMobile !== null && !isset($_GET['force']))
  57.             return $isMobile;
  58.         $useragent $_SERVER['HTTP_USER_AGENT'];
  59.         $_SESSION['isMobile'] = (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)
  60.         ||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)));
  61.         return $_SESSION['isMobile'];
  62.     }
  63.     public static function isDesktop(){
  64.         return !self::isMobile();
  65.     }
  66.     public  function mobile($s){
  67.         return self::isMobile() ? $s "";
  68.     }
  69.     public  function desktop($s){
  70.         return self::isMobile() ? "" $s;
  71.     }
  72.     public static function getBrowser()
  73.     {
  74.         $u_agent $_SERVER['HTTP_USER_AGENT'];
  75.         $bname 'Unknown';
  76.         $platform 'Unknown';
  77.         $version"";
  78.         $ub "";
  79.         //First get the platform?
  80.         if (preg_match('/linux/i'$u_agent)) {
  81.             $platform 'linux';
  82.         }
  83.         elseif (preg_match('/macintosh|mac os x/i'$u_agent)) {
  84.             $platform 'mac';
  85.         }
  86.         elseif (preg_match('/windows|win32/i'$u_agent)) {
  87.             $platform 'windows';
  88.         }
  89.         elseif (preg_match('/android/i'$u_agent)) {
  90.             $platform 'android';
  91.         }
  92.         elseif (preg_match('/ios/i'$u_agent)) {
  93.             $platform 'ios';
  94.         }
  95.         // Next get the name of the useragent yes seperately and for good reason
  96.         if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
  97.         {
  98.             $bname 'Internet Explorer';
  99.             $ub "MSIE";
  100.         }
  101.         elseif(preg_match('/Firefox/i',$u_agent))
  102.         {
  103.             $bname 'Mozilla Firefox';
  104.             $ub "Firefox";
  105.         }
  106.         elseif(preg_match('/Chrome/i',$u_agent))
  107.         {
  108.             $bname 'Google Chrome';
  109.             $ub "Chrome";
  110.         }
  111.         elseif(preg_match('/Safari/i',$u_agent))
  112.         {
  113.             $bname 'Apple Safari';
  114.             $ub "Safari";
  115.         }
  116.         elseif(preg_match('/Opera/i',$u_agent))
  117.         {
  118.             $bname 'Opera';
  119.             $ub "Opera";
  120.         }
  121.         elseif(preg_match('/Netscape/i',$u_agent))
  122.         {
  123.             $bname 'Netscape';
  124.             $ub "Netscape";
  125.         }
  126.         // finally get the correct version number
  127.         $known = array('Version'$ub'other');
  128.         $pattern '#(?<browser>' join('|'$known) .
  129.         ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
  130.         if (!preg_match_all($pattern$u_agent$matches)) {
  131.             // we have no matching number just continue
  132.         }
  133.         // see how many we have
  134.         $i count($matches['browser']);
  135.         if ($i != 1) {
  136.             //we will have two since we are not using 'other' argument yet
  137.             //see if version is before or after the name
  138.             if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
  139.                 $version$matches['version'][0];
  140.             }
  141.             else {
  142.                 if(count$matches['version'])>1)
  143.                     $version$matches['version'][1];
  144.             }
  145.         }
  146.         else {
  147.             $version$matches['version'][0];
  148.         }
  149.         // check if we have a number
  150.         if ($version==null || $version=="") {$version="?";}
  151.         return array(
  152.             'userAgent' => $u_agent,
  153.             'name'      => $bname,
  154.             'version'   => $version,
  155.             'platform'  => $platform,
  156.             'pattern'    => $pattern
  157.         );
  158.     }
  159.     public static function FrDate(\DateTimeInterface $date$isLong false){
  160.         setlocale(LC_TIME"fr_FR.utf8");
  161.         return strftime($isLong "%d %B %Y" "%d %b %Y"$date->getTimestamp());
  162.     }
  163.     public static function FrDateTime(\DateTimeInterface $date$isLong false){
  164.         setlocale(LC_TIME"fr_FR.utf8");
  165.         return strftime($isLong "%d %B %Y à %H:%M" "%d %b %Y à %H:%M"$date->getTimestamp());
  166.     }
  167.     public static function strftime(\DateTimeInterface $date$format){
  168.         setlocale(LC_TIME"fr_FR.utf8");
  169.         return strftime($format$date->getTimestamp());
  170.     }
  171.     public static function FrMonth(\DateTimeInterface $date$isLong false){
  172.         setlocale(LC_TIME"fr_FR.utf8");
  173.         return strftime($isLong "%B" "%b"$date->getTimestamp());
  174.     }
  175.     public static function FrDay(\DateTimeInterface $date$isLong false){
  176.         setlocale(LC_TIME"fr_FR.utf8");
  177.         return strftime($isLong "%A" "%a"$date->getTimestamp());
  178.     }
  179.     public function prettyDate(\DateTimeInterface $date$isLongDate false){
  180.         $now = new \DateTime;
  181.         $now->setTime(0,0,0);
  182.         $i = ($now $date ? -1) * $date->diff($now)->days;
  183.         return $i >= -7
  184.             $i >= -6
  185.             $i >= -2
  186.             $i >= -1
  187.             $i >= 0
  188.             $i >= 1
  189.             $i >= 2
  190.             $i >= 3
  191.             $i >= 5
  192.             self::FrDate($date,$isLongDate)
  193.             : 'Dans 3 jours'
  194.             'Après-demain'
  195.             'Demain'
  196.             'Aujourd\'hui'
  197.             'Hier'
  198.             'Avant-hier'
  199.             'Il y a '.-$i.' jours'
  200.             'Il y a une semaine'
  201.             :  self::FrDate($date,$isLongDate)
  202.         ;
  203.     }
  204.     public function prettyDateTime(\DateTime $date$isLongDate false){
  205.         $now = new \DateTime;
  206.         return $date >= $now->modify('-1 day')
  207.             ? $date >= $now->modify('-1 day')
  208.             ? $date $now
  209.             'Aujourd\'hui à '.$date->format('H:i')
  210.             : 'Hier à '.$date->format('H:i')
  211.             : 'Avant-hier à '.$date->format('H:i')
  212.             : self::FrDateTime($date,$isLongDate)
  213.         ;
  214.     }
  215.     public static function resizeImage($file$width 1520$height 1520){
  216.         $file urldecode($file);
  217.         if(is_file($file)) {
  218.             $size = ['width'=>0,'height'=>0];
  219.             list($size['width'], $size['height']) = getimagesize(realpath($file));
  220.             if($size['width'] > 600 || $size['height'] > 600){
  221.                 //check if Imagick is available
  222.                 if(!class_exists('Imagick')){
  223.                     return $size;
  224.                 }
  225.                 $imagick = new \Imagick(realpath($file));
  226.                 $imagick->setImageCompressionQuality(75);
  227.                 $imagick->thumbnailImage($width$width1false);
  228.                 $imagick->writeimage(realpath($file));
  229.                 $size = [$imagick->getImageWidth(), $imagick->getImageHeight()];
  230.                 $imagick->clear();
  231.                 $imagick->destroy();
  232.             }
  233.             return $size;
  234.         }
  235.         else
  236.             throw new Exception("Veuillez choisir une image valide et réessayer => ".$file);
  237.     }
  238.     public static function thumbnailImage($file$target$quality 60){
  239.         $file urldecode($file);
  240.         $extension pathinfo($filePATHINFO_EXTENSION);
  241.         $filename pathinfo($filePATHINFO_FILENAME);
  242.         if (is_file($file)) {
  243.             if(!class_exists('Imagick')){
  244.                 return false;
  245.             }
  246.             $imagick = new \Imagick(($file));
  247.             if($imagick->getImageWidth() > 600 || $imagick->getImageHeight() > 600){
  248.                 $imagick->setImageFormat(strtolower($extension));
  249.                 $imagick->setImageCompressionQuality($quality);
  250.                 $imagick->thumbnailImage(6006001false);
  251.             }
  252.             $imagick->writeimage(realpath($target).'/'.$filename.'.'.$extension);
  253.             $imagick->clear();
  254.             $imagick->destroy();
  255.             return true;
  256.         }
  257.         else
  258.             throw new Exception("Veuillez choisir une image valide et réessayer.");
  259.     }
  260.     public static function thumbnailImage2($file$target){
  261.         $extension strtolower(pathinfo($filePATHINFO_EXTENSION));
  262.         $filename strtolower(pathinfo($filePATHINFO_FILENAME));
  263.         $realSize getimagesize($file);
  264.         $image null;
  265.         if($extension == 'jpg' || $extension == 'jpeg'$image imagecreatefromjpeg($file);
  266.         else if($extension == 'png'$image imagecreatefrompng($file);
  267.         else if ($extension == 'gif'$image imagecreatefromgif($file);
  268.         else return false;
  269.         $target $target $target './upload/images/articles/thumbnails/';
  270.         $width 600;
  271.         $height = ($width $realSize[0]) * $realSize[1];
  272.         $top $left $coef 0;
  273.         $newImage imagecreatetruecolor($width,$height);
  274.         $coef min($realSize[0]/$width,$realSize[1]/$height);
  275.         $deltax $realSize[0]-($coef $width);
  276.         $deltay $realSize[1]-($coef $height);
  277.         imagecopyresampled($newImage,$image,0,0,$deltax/2,$deltay/2,$width,$height,$realSize[0]-$deltax,$realSize[1]-$deltay);
  278.         $fullpath $target.$filename.'.'.$extension;
  279.         if($extension == 'jpg' || $extension == 'jpeg'imagejpeg($newImage,$fullpath);
  280.         else if($extension == 'png'imagepng($newImage,$fullpath);
  281.         else if ($extension == 'gif'imagegif($newImage,$fullpath);
  282.         $imagevariable ob_get_contents();
  283.         imagedestroy($image);
  284.         imagedestroy($newImage);
  285.         return $fullpath;
  286.     }
  287.     public static function prettySize($size){
  288.         $bytes null;
  289.         if ($size >= 1073741824)
  290.             $bytes round($size 1073741824) . ' Go';
  291.         elseif ($size >= 1048576)
  292.             $bytes round($size 1048576) . ' Mo';
  293.         elseif ($size >= 1000)
  294.             $bytes round($size 1024) . ' Ko';
  295.         else
  296.             $bytes $size ' O';
  297.         return $bytes;
  298.     }
  299.     public static function idToTweet($username,$id){
  300.         return "https://twitter.com/".$username."/status/".$id;
  301.     }
  302.     public static function idToPost($username,$id){
  303.         return "https://www.facebook.com/".$username."/posts/".$id;
  304.     }
  305.     public static function postToId($post){
  306.         $post explode('/',$post);
  307.         if(preg_match('/^\d+$/i',$post[count($post)-1]))
  308.             return $post[count($post)-1];
  309.         else
  310.             return $post[count($post)-2];
  311.     }
  312.     // Code from https://stackoverflow.com/a/6121972
  313.     public static function getYoutubeId($link){
  314.         preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#"$link$id);
  315.         return $id[0];
  316.     }
  317.     public static function getYoutubeEmbed($id){
  318.         return 'https://youtube.com/embed/'.$id;
  319.     }
  320.     public static function getYoutubeThumbnail($link$quality 'l') {
  321.         $video_id getYoutubeId($link);
  322.         if (!isset($video_id[1]))
  323.             $video_id explode("/embed/"$link);
  324.         if (!isset($video_id[1]) || empty($video_id[1]))
  325.             $video_id explode("/v/"$link);
  326.         if (!isset($video_id[1]) || empty($video_id[1]))
  327.             return false;
  328.         $video_id explode("&"$video_id[1]);
  329.         $id $video_id[0];
  330.         if ($id) {
  331.             switch (strtolower(substr($quality01)))
  332.             {
  333.                 case 'l'//low
  334.                 return 'https://img.youtube.com/vi/'.$id.'/sddefault.jpg';
  335.                 case 'h'//high
  336.                 return 'https://img.youtube.com/vi/'.$id.'/hqdefault.jpg';
  337.                 case 'm'//maximum
  338.                 return 'https://img.youtube.com/vi/'.$id.'/maxresdefault.jpg';
  339.                 default:
  340.                 return 'https://img.youtube.com/vi/'.$id.'/mqdefault.jpg';
  341.             }
  342.         }
  343.         return false;
  344.     }
  345.     #[ArrayShape(['width' => "int|mixed"'height' => "int|mixed"])] public function imagesize($file){
  346.         $size = ['width'=>0,'height'=>0];
  347.         list($size['width'], $size['height']) = getimagesize($file);
  348.         return $size ;
  349.     }
  350.     public function filesize($file): bool|int
  351.     {
  352.         return file_exists($file) ? filesize($file) : false ;
  353.     }
  354.     public function country($countryCode,$locale "fr"): string
  355.     {
  356.         return Countries::getName($countryCode$locale);
  357.     }
  358.     public function lang($langCode$locale "fr"): string
  359.     {
  360.         return Languages::getName($langCode$locale);
  361.     }
  362.     public function filterArray($list$property$s$subProperty null): array
  363.     {
  364.         if (is_array($list)){
  365.             $m "get".ucfirst($property);
  366.             if($subProperty !== null){
  367.                 $subM "get".ucfirst($subProperty);
  368.                 return array_filter($list,function($o) use(&$property, &$s, &$m, &$subProperty, &$subM){
  369.                     $sub is_object($o) && property_exists($o$property) ? $o->$m() : null;
  370.                     return is_object($sub) && property_exists($sub$subProperty) ? $sub->$subM() == $s false;
  371.                 });
  372.             }
  373.             else
  374.                 return array_filter($list,function($o) use(&$property, &$s, &$m){
  375.                     if(is_object($o))
  376.                         return property_exists($o$property) ? $o->$m() == $s false;
  377.                     elseif(is_array($o))
  378.                         return array_key_exists($property,$o) ? $o[$property] == $s false;
  379.                 });
  380.         }
  381.         return [];
  382.     }
  383.     public function imageColor($file$default="rgb(255, 255, 255)"){
  384.         $explode explode('.'$file);
  385.         $extension end($explode);
  386.         if($extension){
  387.             $img=null;
  388.             switch (strtolower($extension)){
  389.                 case 'png':
  390.                     $img imagecreatefrompng($file);
  391.                     break;
  392.                 case 'jpg':
  393.                 case 'jpeg':
  394.                     $img imagecreatefromjpeg($file);
  395.                     break;
  396.                 case 'gif':
  397.                     $img imagecreatefromgif($file);
  398.                     break;
  399.                 default:
  400.                     break;
  401.             }
  402.             if($img){
  403.                 $rgb imagecolorat($img11);
  404.                 $r = ($rgb >> 16) & 0xFF;
  405.                 $g = ($rgb >> 8) & 0xFF;
  406.                 $b $rgb 0xFF;
  407.                 return "rgb(".$r.", ".$g.", ".$b.")";
  408.             }
  409.         }
  410.         return $default;
  411.     }
  412.     public function getParameter($prop){
  413.         $params Yaml::parseFile("../config/packages/olasoft.yaml")["parameters"];
  414.         return in_array($prop$params) ? "" $params[$prop];
  415.     }
  416.     // Code from https://stackoverflow.com/questions/13076480/
  417.     // Returns a file size limit in bytes based on the PHP upload_max_filesize
  418.     // and post_max_size
  419.     function getUploadMaxFileSize() {
  420.         static $max_size = -1;
  421.         if ($max_size 0) {
  422.             // Start with post_max_size.
  423.             $post_max_size self::parseSize(ini_get('post_max_size'));
  424.             if ($post_max_size 0) {
  425.                 $max_size $post_max_size;
  426.             }
  427.             // If upload_max_size is less, then reduce. Except if upload_max_size is
  428.             // zero, which indicates no limit.
  429.             $upload_max self::parseSize(ini_get('upload_max_filesize'));
  430.             if ($upload_max && $upload_max $max_size) {
  431.               $max_size $upload_max;
  432.             }
  433.         }
  434.         // if max_size is greater than 4Mo, return 4Mo
  435.         if ($max_size 4194304) {
  436.             return 4194304;
  437.         }
  438.         return $max_size;
  439.     }
  440.     function parseSize($size): float
  441.     {
  442.         $unit preg_replace('/[^bkmgtpezy]/i'''$size); // Remove the non-unit characters from the size.
  443.         $size preg_replace('/[^0-9\.]/'''$size); // Remove the non-numeric characters from the size.
  444.         if ($unit) {
  445.             // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
  446.             return round($size pow(1024stripos('bkmgtpezy'$unit[0])));
  447.         }
  448.         else {
  449.             return round($size);
  450.         }
  451.     }
  452. }