src/Utils/Util.php line 33
- <?php
- namespace App\Utils;
- class Util {
- static public function encode_afcode($str) {
- $out = "";
- $str = (string)sprintf("%04d", $str);
- //primeros 4 digitos
- for ($i=0; $i<strlen($str); $i++) {
- $out .= chr(97+$str[$i]);
- }
- for ($i=0; $i<strlen($str); $i++) {
- $out .= chr(107+$str[$i]);
- }
- return $out;
- }
- /**
- * Función que devuelve los shortcodes que se busquen en un texto
- *
- * @param array $listashorts shotcodes a procesar
- * @param string $texto donde buscar
- *
- * @return array() de shortcodes encontrados
- */
- public static function shortcodes($listashorts, $texto)
- {
- $salida = array();
- $texto = html_entity_decode($texto);
- foreach($listashorts as $shortcode)
- {
- preg_match_all("~\[\[$shortcode(.*?)/\]\]?~", $texto, $matches);
- $salida[$shortcode] = array();
- $i=0;
- foreach ($matches[1] as $m) {
- preg_match_all("#(\w+)=['\"]{1}([^'\"]*)#", $m, $matches2);
- //Array atributes?
- foreach($matches2[2] as &$mm)
- {
- $temp2 = explode(",",$mm);
- if (count($temp2) > 1)
- $mm = $temp2;
- }
- $tempArray = array_combine($matches2[1],$matches2[2]);
- $salida[$shortcode][$matches[0][$i++]] = $tempArray;
- }
- }
- // var_dump($salida);
- return $salida;
- }
- public static function getCountryByIp($ip = null, $all = false)
- {
- if ($ip == null)
- {
- $client = @$_SERVER['HTTP_CLIENT_IP'];
- $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
- $remote = @$_SERVER['REMOTE_ADDR'];
- $result = array('country'=>'', 'city'=>'');
- if(filter_var($client, FILTER_VALIDATE_IP)){
- $ip = $client;
- }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
- $ip = $forward;
- }else{
- $ip = $remote;
- }
- }
- $ip_data = @json_decode
- (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
- if($ip_data && $ip_data->geoplugin_countryName != null){
- $result['country'] = $ip_data->geoplugin_countryCode;
- $result['city'] = $ip_data->geoplugin_city;
- if ($all)
- return $ip_data;
- else
- return $result['country'];
- }
- else {
- return null;
- }
- }
- public static function mobileDevice($useragent)
- {
- if (!$useragent) {
- return "-";
- }
- if (
- 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) ||
- 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))
- )
- return "Movil";
- return "PC";
- }
- /**
- * Funcion para cambiar extension del path
- */
- static function replaceExtension($filename, $new_extension) {
- $info = pathinfo($filename);
- return ($info['dirname'] ? $info['dirname'] . DIRECTORY_SEPARATOR : '')
- . $info['filename']
- . '.'
- . $new_extension;
- }
- /*public static function getRuta($tipo, PersistenceManagerRegistry $em)
- {
- return $em->getRepository('App\Entity\Ruta')->findOneById($tipo."_url");
- }*/
- //FUNCION PARA CAMBIAR LA RESOLUCION DE LAS IMAGENES SUBIDAS
- static public function smart_resize_image($file,
- $string = null,
- $width = 0,
- $height = 0,
- $proportional = true,
- $output = 'file',
- $delete_original = true,
- $use_linux_commands = false,
- $quality = 100,
- $grayscale = false
- ) {
- if ( $height <= 0 && $width <= 0 ) return false;
- if ( $file === null && $string === null ) return false;
- # Setting defaults and meta
- $info = $file !== null ? getimagesize($file) : getimagesizefromstring($string);
- $image = '';
- $final_width = 0;
- $final_height = 0;
- list($width_old, $height_old) = $info;
- $cropHeight = $cropWidth = 0;
- # Calculating proportionality
- if ($proportional) {
- if ($width == 0) $factor = $height/$height_old;
- elseif ($height == 0) $factor = $width/$width_old;
- else $factor = min( $width / $width_old, $height / $height_old );
- $final_width = round( $width_old * $factor );
- $final_height = round( $height_old * $factor );
- }
- else {
- $final_width = ( $width <= 0 ) ? $width_old : $width;
- $final_height = ( $height <= 0 ) ? $height_old : $height;
- $widthX = $width_old / $width;
- $heightX = $height_old / $height;
- $x = min($widthX, $heightX);
- $cropWidth = (int) ($width_old - $width * $x) / 2;
- $cropHeight = (int) ($height_old - $height * $x) / 2;
- }
- # Loading image to memory according to type
- switch ( $info[2] ) {
- case IMAGETYPE_JPEG: $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string); break;
- case IMAGETYPE_GIF: $file !== null ? $image = imagecreatefromgif($file) : $image = imagecreatefromstring($string); break;
- case IMAGETYPE_PNG: $file !== null ? $image = imagecreatefrompng($file) : $image = imagecreatefromstring($string); break;
- default: return false;
- }
- # Making the image grayscale, if needed
- if ($grayscale) {
- imagefilter($image, IMG_FILTER_GRAYSCALE);
- }
- # This is the resizing/resampling/transparency-preserving magic
- $image_resized = imagecreatetruecolor( $final_width, $final_height );
- if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
- $transparency = imagecolortransparent($image);
- $palletsize = imagecolorstotal($image);
- if ($transparency >= 0 && $transparency < $palletsize) {
- $transparent_color = imagecolorsforindex($image, $transparency);
- $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
- imagefill($image_resized, 0, 0, $transparency);
- imagecolortransparent($image_resized, $transparency);
- }
- elseif ($info[2] == IMAGETYPE_PNG) {
- imagealphablending($image_resized, false);
- $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
- imagefill($image_resized, 0, 0, $color);
- imagesavealpha($image_resized, true);
- }
- }
- imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);
- # Taking care of original, if needed
- if ( $delete_original ) {
- if ( $use_linux_commands ) exec('rm '.$file);
- else @unlink($file);
- }
- # Preparing a method of providing result
- switch ( strtolower($output) ) {
- case 'browser':
- $mime = image_type_to_mime_type($info[2]);
- header("Content-type: $mime");
- $output = NULL;
- break;
- case 'file':
- $output = $file;
- break;
- case 'return':
- return $image_resized;
- break;
- default:
- break;
- }
- # Writing image according to type to the output destination and image quality
- switch ( $info[2] ) {
- case IMAGETYPE_GIF: imagegif($image_resized, $output); break;
- case IMAGETYPE_JPEG: imagejpeg($image_resized, $output, $quality); break;
- case IMAGETYPE_PNG:
- $quality = 9 - (int)((0.9*$quality)/10.0);
- imagepng($image_resized, $output, $quality);
- break;
- default: return false;
- }
- return true;
- }
- }