olasoft/Common.php line 181

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\Yaml\Yaml;
  8. class Common{
  9.     public static function downloadPDF($file,$name null): void
  10.     {
  11.         header('Content-type: application/pdf');
  12.         header('Content-Disposition: attachment; '. ($name 'filename="'.self::slug($name).'.pdf"' ''));
  13.         readfile($file);
  14.     }
  15.     public static function generatePassword($n 10): string
  16.     {
  17.         $r "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  18.         $r str_shuffle($r);
  19.         $r uniqid($r,true);
  20.         $r substr($r,0,$n);
  21.         return $r;
  22.     }
  23.     public static function generateToken($n 30): string
  24.     {
  25.         return bin2hex(random_bytes($n));
  26.     }
  27.     public static function generateCode($n 5): string
  28.     {
  29.         $r "0123456789";
  30.         $r str_shuffle($r);
  31.         $r uniqid($r,true);
  32.         $r substr($r,0,$n);
  33.         return $r;
  34.     }
  35.     public static function generateName(): string
  36.     {
  37.         $generateName microtime();
  38.         $generateNameTable preg_split('/[\s,\.]/',$generateName);
  39.         $generateName $generateNameTable[0].$generateNameTable[1].$generateNameTable[2];
  40.         return $generateName;
  41.     }
  42.     public static function transliterateString($txt): array|string
  43.     {
  44.         $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');
  45.         return str_replace(array_keys($transliterationTable), array_values($transliterationTable), $txt);
  46.     }
  47.     public static function slug($text){
  48.         $t1 "/\,|;|\?|!|:|\(|\)|\[|\]|\{|\}|'|’| |«|»|°|\*|\+|=|\\|\//";
  49.         $t2 "/-+((.{1,3}|pour)-+((.{1,3}|pour)-+)*)/";
  50.         $slug preg_replace(["/\s+/",$t1,$t2,"/-+/"],"-",strtolower(Common::transliterateString($text)));
  51.         return $slug;
  52.     }
  53.     public static function isMobile(){
  54.         $isMobile $_SESSION['isMobile'] ?? null;
  55.         if($isMobile !== null && !isset($_GET['force']))
  56.             return $isMobile;
  57.         $useragent $_SERVER['HTTP_USER_AGENT'];
  58.         $_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)
  59.         ||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)));
  60.         return $_SESSION['isMobile'];
  61.     }
  62.     public static function isDesktop(){
  63.         return !self::isMobile();
  64.     }
  65.     public  function mobile($s){
  66.         return self::isMobile() ? $s "";
  67.     }
  68.     public  function desktop($s){
  69.         return self::isMobile() ? "" $s;
  70.     }
  71.     public static function getBrowser()
  72.     {
  73.         $u_agent $_SERVER['HTTP_USER_AGENT'];
  74.         $bname 'Unknown';
  75.         $platform 'Unknown';
  76.         $version"";
  77.         $ub "";
  78.         //First get the platform?
  79.         if (preg_match('/linux/i'$u_agent)) {
  80.             $platform 'linux';
  81.         }
  82.         elseif (preg_match('/macintosh|mac os x/i'$u_agent)) {
  83.             $platform 'mac';
  84.         }
  85.         elseif (preg_match('/windows|win32/i'$u_agent)) {
  86.             $platform 'windows';
  87.         }
  88.         elseif (preg_match('/android/i'$u_agent)) {
  89.             $platform 'android';
  90.         }
  91.         elseif (preg_match('/ios/i'$u_agent)) {
  92.             $platform 'ios';
  93.         }
  94.         // Next get the name of the useragent yes seperately and for good reason
  95.         if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
  96.         {
  97.             $bname 'Internet Explorer';
  98.             $ub "MSIE";
  99.         }
  100.         elseif(preg_match('/Firefox/i',$u_agent))
  101.         {
  102.             $bname 'Mozilla Firefox';
  103.             $ub "Firefox";
  104.         }
  105.         elseif(preg_match('/Chrome/i',$u_agent))
  106.         {
  107.             $bname 'Google Chrome';
  108.             $ub "Chrome";
  109.         }
  110.         elseif(preg_match('/Safari/i',$u_agent))
  111.         {
  112.             $bname 'Apple Safari';
  113.             $ub "Safari";
  114.         }
  115.         elseif(preg_match('/Opera/i',$u_agent))
  116.         {
  117.             $bname 'Opera';
  118.             $ub "Opera";
  119.         }
  120.         elseif(preg_match('/Netscape/i',$u_agent))
  121.         {
  122.             $bname 'Netscape';
  123.             $ub "Netscape";
  124.         }
  125.         // finally get the correct version number
  126.         $known = array('Version'$ub'other');
  127.         $pattern '#(?<browser>' join('|'$known) .
  128.         ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
  129.         if (!preg_match_all($pattern$u_agent$matches)) {
  130.             // we have no matching number just continue
  131.         }
  132.         // see how many we have
  133.         $i count($matches['browser']);
  134.         if ($i != 1) {
  135.             //we will have two since we are not using 'other' argument yet
  136.             //see if version is before or after the name
  137.             if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
  138.                 $version$matches['version'][0];
  139.             }
  140.             else {
  141.                 if(count$matches['version'])>1)
  142.                     $version$matches['version'][1];
  143.             }
  144.         }
  145.         else {
  146.             $version$matches['version'][0];
  147.         }
  148.         // check if we have a number
  149.         if ($version==null || $version=="") {$version="?";}
  150.         return array(
  151.             'userAgent' => $u_agent,
  152.             'name'      => $bname,
  153.             'version'   => $version,
  154.             'platform'  => $platform,
  155.             'pattern'    => $pattern
  156.         );
  157.     }
  158.     public static function FrDate(\DateTimeInterface $date$isLong false){
  159.         setlocale(LC_TIME"fr_FR.utf8");
  160.         return strftime($isLong "%d %B %Y" "%d %b %Y"$date->getTimestamp());
  161.     }
  162.     public static function FrDateTime(\DateTimeInterface $date$isLong false){
  163.         setlocale(LC_TIME"fr_FR.utf8");
  164.         return strftime($isLong "%d %B %Y à %H:%M" "%d %b %Y à %H:%M"$date->getTimestamp());
  165.     }
  166.     public static function strftime(\DateTimeInterface $date$format){
  167.         setlocale(LC_TIME"fr_FR.utf8");
  168.         return strftime($format$date->getTimestamp());
  169.     }
  170.     public static function FrMonth(\DateTimeInterface $date$isLong false){
  171.         setlocale(LC_TIME"fr_FR.utf8");
  172.         return strftime($isLong "%B" "%b"$date->getTimestamp());
  173.     }
  174.     public static function FrDay(\DateTimeInterface $date$isLong false){
  175.         setlocale(LC_TIME"fr_FR.utf8");
  176.         return strftime($isLong "%A" "%a"$date->getTimestamp());
  177.     }
  178.     public function prettyDate(\DateTimeInterface $date$isLongDate false){
  179.         $now = new \DateTime;
  180.         $now->setTime(0,0,0);
  181.         $i = ($now $date ? -1) * $date->diff($now)->days;
  182.         return $i >= -7
  183.             $i >= -6
  184.             $i >= -2
  185.             $i >= -1
  186.             $i >= 0
  187.             $i >= 1
  188.             $i >= 2
  189.             $i >= 3
  190.             $i >= 5
  191.             self::FrDate($date,$isLongDate)
  192.             : 'Dans 3 jours'
  193.             'Après-demain'
  194.             'Demain'
  195.             'Aujourd\'hui'
  196.             'Hier'
  197.             'Avant-hier'
  198.             'Il y a '.-$i.' jours'
  199.             'Il y a une semaine'
  200.             :  self::FrDate($date,$isLongDate)
  201.         ;
  202.     }
  203.     public function prettyDateTime(\DateTime $date$isLongDate false){
  204.         $now = new \DateTime;
  205.         return $date >= $now->modify('-1 day')
  206.             ? $date >= $now->modify('-1 day')
  207.             ? $date $now
  208.             'Aujourd\'hui à '.$date->format('H:i')
  209.             : 'Hier à '.$date->format('H:i')
  210.             : 'Avant-hier à '.$date->format('H:i')
  211.             : self::FrDateTime($date,$isLongDate)
  212.         ;
  213.     }
  214.     public static function resizeImage($file$width 1520$height 1520){
  215.         $file urldecode($file);
  216.         if(is_file($file)) {
  217.             $size = ['width'=>0,'height'=>0];
  218.             list($size['width'], $size['height']) = getimagesize(realpath($file));
  219.             if($size['width'] > 600 || $size['height'] > 600){
  220.                 $imagick = new \Imagick(realpath($file));
  221.                 $imagick->setImageCompressionQuality(75);
  222.                 $imagick->thumbnailImage($width$width1false);
  223.                 $imagick->writeimage(realpath($file));
  224.                 $size = [$imagick->getImageWidth(), $imagick->getImageHeight()];
  225.                 $imagick->clear();
  226.                 $imagick->destroy();
  227.             }
  228.             return $size;
  229.         }
  230.         else
  231.             throw new Exception("Veuillez choisir une image valide et réessayer => ".$file);
  232.     }
  233.     public static function thumbnailImage($file$target$quality 60){
  234.         $file urldecode($file);
  235.         $extension pathinfo($filePATHINFO_EXTENSION);
  236.         $filename pathinfo($filePATHINFO_FILENAME);
  237.         if (is_file($file)) {
  238.             $imagick = new \Imagick(($file));
  239.             if($imagick->getImageWidth() > 600 || $imagick->getImageHeight() > 600){
  240.                 $imagick->setImageFormat(strtolower($extension));
  241.                 $imagick->setImageCompressionQuality($quality);
  242.                 $imagick->thumbnailImage(6006001false);
  243.             }
  244.             $imagick->writeimage(realpath($target).'/'.$filename.'.'.$extension);
  245.             $imagick->clear();
  246.             $imagick->destroy();
  247.             return true;
  248.         }
  249.         else
  250.             throw new Exception("Veuillez choisir une image valide et réessayer.");
  251.     }
  252.     public static function thumbnailImage2($file$target){
  253.         $extension strtolower(pathinfo($filePATHINFO_EXTENSION));
  254.         $filename strtolower(pathinfo($filePATHINFO_FILENAME));
  255.         $realSize getimagesize($file);
  256.         $image null;
  257.         if($extension == 'jpg' || $extension == 'jpeg'$image imagecreatefromjpeg($file);
  258.         else if($extension == 'png'$image imagecreatefrompng($file);
  259.         else if ($extension == 'gif'$image imagecreatefromgif($file);
  260.         else return false;
  261.         $target $target $target './upload/images/articles/thumbnails/';
  262.         $width 600;
  263.         $height = ($width $realSize[0]) * $realSize[1];
  264.         $top $left $coef 0;
  265.         $newImage imagecreatetruecolor($width,$height);
  266.         $coef min($realSize[0]/$width,$realSize[1]/$height);
  267.         $deltax $realSize[0]-($coef $width);
  268.         $deltay $realSize[1]-($coef $height);
  269.         imagecopyresampled($newImage,$image,0,0,$deltax/2,$deltay/2,$width,$height,$realSize[0]-$deltax,$realSize[1]-$deltay);
  270.         $fullpath $target.$filename.'.'.$extension;
  271.         if($extension == 'jpg' || $extension == 'jpeg'imagejpeg($newImage,$fullpath);
  272.         else if($extension == 'png'imagepng($newImage,$fullpath);
  273.         else if ($extension == 'gif'imagegif($newImage,$fullpath);
  274.         $imagevariable ob_get_contents();
  275.         imagedestroy($image);
  276.         imagedestroy($newImage);
  277.         return $fullpath;
  278.     }
  279.     public static function prettySize($size){
  280.         $bytes null;
  281.         if ($size >= 1073741824)
  282.             $bytes round($size 1073741824) . ' Go';
  283.         elseif ($size >= 1048576)
  284.             $bytes round($size 1048576) . ' Mo';
  285.         elseif ($size >= 1000)
  286.             $bytes round($size 1024) . ' Ko';
  287.         else
  288.             $bytes $size ' O';
  289.         return $bytes;
  290.     }
  291.     public static function idToTweet($username,$id){
  292.         return "https://twitter.com/".$username."/status/".$id;
  293.     }
  294.     public static function idToPost($username,$id){
  295.         return "https://www.facebook.com/".$username."/posts/".$id;
  296.     }
  297.     public static function postToId($post){
  298.         $post explode('/',$post);
  299.         if(preg_match('/^\d+$/i',$post[count($post)-1]))
  300.             return $post[count($post)-1];
  301.         else
  302.             return $post[count($post)-2];
  303.     }
  304.     // Code from https://stackoverflow.com/a/6121972
  305.     public static function getYoutubeId($link){
  306.         preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#"$link$id);
  307.         return $id[0];
  308.     }
  309.     public static function getYoutubeEmbed($id){
  310.         return 'https://youtube.com/embed/'.$id;
  311.     }
  312.     public static function getYoutubeThumbnail($link$quality 'l') {
  313.         $video_id getYoutubeId($link);
  314.         if (!isset($video_id[1]))
  315.             $video_id explode("/embed/"$link);
  316.         if (!isset($video_id[1]) || empty($video_id[1]))
  317.             $video_id explode("/v/"$link);
  318.         if (!isset($video_id[1]) || empty($video_id[1]))
  319.             return false;
  320.         $video_id explode("&"$video_id[1]);
  321.         $id $video_id[0];
  322.         if ($id) {
  323.             switch (strtolower(substr($quality01)))
  324.             {
  325.                 case 'l'//low
  326.                 return 'https://img.youtube.com/vi/'.$id.'/sddefault.jpg';
  327.                 case 'h'//high
  328.                 return 'https://img.youtube.com/vi/'.$id.'/hqdefault.jpg';
  329.                 case 'm'//maximum
  330.                 return 'https://img.youtube.com/vi/'.$id.'/maxresdefault.jpg';
  331.                 default:
  332.                 return 'https://img.youtube.com/vi/'.$id.'/mqdefault.jpg';
  333.             }
  334.         }
  335.         return false;
  336.     }
  337.     #[ArrayShape(['width' => "int|mixed"'height' => "int|mixed"])] public function imagesize($file){
  338.         $size = ['width'=>0,'height'=>0];
  339.         list($size['width'], $size['height']) = getimagesize($file);
  340.         return $size ;
  341.     }
  342.     public function filesize($file): bool|int
  343.     {
  344.         return file_exists($file) ? filesize($file) : false ;
  345.     }
  346.     public function country($countryCode,$locale "fr"): string
  347.     {
  348.         return Countries::getName($countryCode$locale);
  349.     }
  350.     public function filterArray($list$property$s$subProperty null): array
  351.     {
  352.         if (is_array($list)){
  353.             $m "get".ucfirst($property);
  354.             if($subProperty !== null){
  355.                 $subM "get".ucfirst($subProperty);
  356.                 return array_filter($list,function($o) use(&$property, &$s, &$m, &$subProperty, &$subM){
  357.                     $sub is_object($o) && property_exists($o$property) ? $o->$m() : null;
  358.                     return is_object($sub) && property_exists($sub$subProperty) ? $sub->$subM() == $s false;
  359.                 });
  360.             }
  361.             else
  362.                 return array_filter($list,function($o) use(&$property, &$s, &$m){
  363.                     if(is_object($o))
  364.                         return property_exists($o$property) ? $o->$m() == $s false;
  365.                     elseif(is_array($o))
  366.                         return array_key_exists($property,$o) ? $o[$property] == $s false;
  367.                 });
  368.         }
  369.         return [];
  370.     }
  371.     public function imageColor($file$default="rgb(255, 255, 255)"){
  372.         $explode explode('.'$file);
  373.         $extension end($explode);
  374.         if($extension){
  375.             $img=null;
  376.             switch (strtolower($extension)){
  377.                 case 'png':
  378.                     $img imagecreatefrompng($file);
  379.                     break;
  380.                 case 'jpg':
  381.                 case 'jpeg':
  382.                     $img imagecreatefromjpeg($file);
  383.                     break;
  384.                 case 'gif':
  385.                     $img imagecreatefromgif($file);
  386.                     break;
  387.                 default:
  388.                     break;
  389.             }
  390.             if($img){
  391.                 $rgb imagecolorat($img11);
  392.                 $r = ($rgb >> 16) & 0xFF;
  393.                 $g = ($rgb >> 8) & 0xFF;
  394.                 $b $rgb 0xFF;
  395.                 return "rgb(".$r.", ".$g.", ".$b.")";
  396.             }
  397.         }
  398.         return $default;
  399.     }
  400.     public function getParameter($prop){
  401.         $params Yaml::parseFile("../config/packages/olasoft.yaml")["parameters"];
  402.         return in_array($prop$params) ? "" $params[$prop];
  403.     }
  404.     // Code from https://stackoverflow.com/questions/13076480/
  405.     // Returns a file size limit in bytes based on the PHP upload_max_filesize
  406.     // and post_max_size
  407.     function getUploadMaxFileSize() {
  408.         static $max_size = -1;
  409.         if ($max_size 0) {
  410.             // Start with post_max_size.
  411.             $post_max_size self::parseSize(ini_get('post_max_size'));
  412.             if ($post_max_size 0) {
  413.                 $max_size $post_max_size;
  414.             }
  415.             // If upload_max_size is less, then reduce. Except if upload_max_size is
  416.             // zero, which indicates no limit.
  417.             $upload_max self::parseSize(ini_get('upload_max_filesize'));
  418.             if ($upload_max && $upload_max $max_size) {
  419.               $max_size $upload_max;
  420.             }
  421.         }
  422.         return $max_size;
  423.     }
  424.     function parseSize($size): float
  425.     {
  426.         $unit preg_replace('/[^bkmgtpezy]/i'''$size); // Remove the non-unit characters from the size.
  427.         $size preg_replace('/[^0-9\.]/'''$size); // Remove the non-numeric characters from the size.
  428.         if ($unit) {
  429.             // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by.
  430.             return round($size pow(1024stripos('bkmgtpezy'$unit[0])));
  431.         }
  432.         else {
  433.             return round($size);
  434.         }
  435.     }
  436. }