src/Utils/Util.php line 36

  1. <?php
  2. namespace App\Utils;
  3. class Util {
  4.     static public function encode_afcode($str) {
  5.         $out "";
  6.         $str = (string)sprintf("%04d"$str);
  7.         //primeros 4 digitos
  8.         for ($i=0$i<strlen($str); $i++) {
  9.             $out .= chr(97+$str[$i]);
  10.         }
  11.         for ($i=0$i<strlen($str); $i++) {
  12.             $out .= chr(107+$str[$i]);
  13.         }
  14.         return $out;
  15.     }
  16.      /**
  17.      * Función que devuelve los shortcodes que se busquen en un texto
  18.      * 
  19.      * @param array $listashorts shotcodes a procesar
  20.      * @param string $texto donde buscar
  21.      * 
  22.      * @return array() de shortcodes encontrados
  23.      */    
  24.     public static function shortcodes($listashorts$texto)
  25.     {
  26.         $salida = array();
  27.         $texto html_entity_decode($texto);
  28.         
  29.         foreach($listashorts as $shortcode)
  30.         {
  31.             preg_match_all("~\[\[$shortcode(.*?)/\]\]?~"$texto$matches);
  32.             
  33.             $salida[$shortcode] = array();
  34.             $i=0;
  35.             foreach ($matches[1] as $m) {
  36.                 preg_match_all("#(\w+)=['\"]{1}([^'\"]*)#"$m$matches2);
  37.                 
  38.                 //Array atributes?
  39.                 foreach($matches2[2] as &$mm)
  40.                 {
  41.                     $temp2 explode(",",$mm);
  42.                     if (count($temp2) > 1)
  43.                         $mm $temp2;
  44.                 }
  45.                 $tempArray array_combine($matches2[1],$matches2[2]);
  46.                 $salida[$shortcode][$matches[0][$i++]] = $tempArray;
  47.             }
  48.         }
  49.         
  50. //      var_dump($salida);
  51.         return $salida;
  52.     }   
  53.     
  54.     
  55.     public static function getCountryByIp($ip null$all false)
  56.     {
  57.         if ($ip == null)
  58.         {
  59.             $client = @$_SERVER['HTTP_CLIENT_IP'];
  60.             $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  61.             $remote = @$_SERVER['REMOTE_ADDR'];
  62.             $result = array('country'=>'''city'=>'');
  63.             if(filter_var($clientFILTER_VALIDATE_IP)){
  64.             $ip $client;
  65.             }elseif(filter_var($forwardFILTER_VALIDATE_IP)){
  66.             $ip $forward;
  67.             }else{
  68.             $ip $remote;
  69.             }
  70.         }
  71.         $ip_data = @json_decode
  72.         (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
  73.         if($ip_data && $ip_data->geoplugin_countryName != null){
  74.             $result['country'] = $ip_data->geoplugin_countryCode;
  75.             $result['city'] = $ip_data->geoplugin_city;
  76.             
  77.             if ($all)
  78.                 return $ip_data;
  79.             else
  80.                 return $result['country'];
  81.         }
  82.         else {
  83.             return null;
  84.         }
  85.     }  
  86.     
  87.     public static function mobileDevice($useragent)
  88.     {
  89.         
  90.         if (!$useragent) {
  91.             return "-";
  92.         }
  93.         if (
  94.             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|mobile.+firefox|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|xda|xiino/i'$useragent) ||
  95.             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))
  96.         )
  97.             return "Movil";
  98.         
  99.         return "PC";
  100.     }
  101.     
  102.     /**
  103.      * Funcion para cambiar extension del path
  104.      */
  105.     static function replaceExtension($filename$new_extension) {
  106.         $info pathinfo($filename);
  107.         return ($info['dirname'] ? $info['dirname'] . DIRECTORY_SEPARATOR ''
  108.         . $info['filename'
  109.         . '.' 
  110.         $new_extension;
  111.    }   
  112.    
  113.    /*public static function getRuta($tipo, PersistenceManagerRegistry $em)
  114.    {
  115.         return $em->getRepository('App\Entity\Ruta')->findOneById($tipo."_url");
  116.    }*/
  117.    
  118.    
  119.    //FUNCION PARA CAMBIAR LA RESOLUCION DE LAS IMAGENES SUBIDAS
  120.     static public function smart_resize_image($file,
  121.                                  $string             null,
  122.                                  $width              0,
  123.                                  $height             0,
  124.                                  $proportional       true,
  125.                                  $output             'file',
  126.                                  $delete_original    true,
  127.                                  $use_linux_commands false,
  128.                                  $quality            100,
  129.                                  $grayscale          false
  130.         ) {
  131.         if ( $height <= && $width <= ) return false;
  132.         if ( $file === null && $string === null ) return false;
  133.         # Setting defaults and meta
  134.         $info                         $file !== null getimagesize($file) : getimagesizefromstring($string);
  135.         $image                        '';
  136.         $final_width                  0;
  137.         $final_height                 0;
  138.         list($width_old$height_old) = $info;
  139.         $cropHeight $cropWidth 0;
  140.         # Calculating proportionality
  141.         if ($proportional) {
  142.             if      ($width  == 0)  $factor $height/$height_old;
  143.             elseif  ($height == 0)  $factor $width/$width_old;
  144.             else                    $factor min$width $width_old$height $height_old );
  145.             $final_width  round$width_old $factor );
  146.             $final_height round$height_old $factor );
  147.         }
  148.         else {
  149.             $final_width = ( $width <= ) ? $width_old $width;
  150.             $final_height = ( $height <= ) ? $height_old $height;
  151.             $widthX $width_old $width;
  152.             $heightX $height_old $height;
  153.             $x min($widthX$heightX);
  154.             $cropWidth = (int) ($width_old $width $x) / 2;
  155.             $cropHeight = (int) ($height_old $height $x) / 2;
  156.         }
  157.         # Loading image to memory according to type
  158.         switch ( $info[2] ) {
  159.             case IMAGETYPE_JPEG:  $file !== null $image imagecreatefromjpeg($file) : $image imagecreatefromstring($string);  break;
  160.             case IMAGETYPE_GIF:   $file !== null $image imagecreatefromgif($file)  : $image imagecreatefromstring($string);  break;
  161.             case IMAGETYPE_PNG:   $file !== null $image imagecreatefrompng($file)  : $image imagecreatefromstring($string);  break;
  162.             default: return false;
  163.         }
  164.         # Making the image grayscale, if needed
  165.         if ($grayscale) {
  166.             imagefilter($imageIMG_FILTER_GRAYSCALE);
  167.         }
  168.         # This is the resizing/resampling/transparency-preserving magic
  169.         $image_resized imagecreatetruecolor$final_width$final_height );
  170.         if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  171.             $transparency imagecolortransparent($image);
  172.             $palletsize imagecolorstotal($image);
  173.             if ($transparency >= && $transparency $palletsize) {
  174.                 $transparent_color  imagecolorsforindex($image$transparency);
  175.                 $transparency       imagecolorallocate($image_resized$transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  176.                 imagefill($image_resized00$transparency);
  177.                 imagecolortransparent($image_resized$transparency);
  178.             }
  179.             elseif ($info[2] == IMAGETYPE_PNG) {
  180.                 imagealphablending($image_resizedfalse);
  181.                 $color imagecolorallocatealpha($image_resized000127);
  182.                 imagefill($image_resized00$color);
  183.                 imagesavealpha($image_resizedtrue);
  184.             }
  185.         }
  186.         imagecopyresampled($image_resized$image00$cropWidth$cropHeight$final_width$final_height$width_old $cropWidth$height_old $cropHeight);
  187.         # Taking care of original, if needed
  188.         if ( $delete_original ) {
  189.             if ( $use_linux_commands exec('rm '.$file);
  190.             else @unlink($file);
  191.         }
  192.         # Preparing a method of providing result
  193.         switch ( strtolower($output) ) {
  194.             case 'browser':
  195.                 $mime image_type_to_mime_type($info[2]);
  196.                 header("Content-type: $mime");
  197.                 $output NULL;
  198.                 break;
  199.             case 'file':
  200.                 $output $file;
  201.                 break;
  202.             case 'return':
  203.                 return $image_resized;
  204.                 break;
  205.             default:
  206.                 break;
  207.         }
  208.         # Writing image according to type to the output destination and image quality
  209.         switch ( $info[2] ) {
  210.             case IMAGETYPE_GIF:   imagegif($image_resized$output);    break;
  211.             case IMAGETYPE_JPEG:  imagejpeg($image_resized$output$quality);   break;
  212.             case IMAGETYPE_PNG:
  213.                 $quality - (int)((0.9*$quality)/10.0);
  214.                 imagepng($image_resized$output$quality);
  215.                 break;
  216.             default: return false;
  217.         }
  218.         return true;
  219.      }
  220. }