src/Util/Util.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Util;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Twig\Extension\ExtensionInterface;
  7. use App\Entity\Idioma;
  8. class Util extends AbstractController
  9. {
  10.     private $em;
  11.     // Definir un array amb les planes a comprovar per canviar de domini
  12.     public $aValors = [
  13.         'escola_esqui_alpi_snowboard',
  14.         'escuela_esqui_alpino_snowboard',
  15.         'ski_snowboard_school',
  16.         'ecole_ski_snowboard',
  17.         'escola_esqui_snowboard',
  18.         'preus_escola',
  19.         'precios_escuela',
  20.         'rates_school',
  21.         'tarifs',
  22.         'precos_escola',
  23.         'consells-per-als-mes-petits',
  24.         'consejos-para-los-mas-pequenos',
  25.         'advice-for-children',
  26.         'conseils-pour-les-enfants',
  27.         'conselhos-para-os-mais-pequenos',
  28.         'grups_escolars',
  29.         'grupos_escolares',
  30.         'school_groups',
  31.         'groupes_scolaires',
  32.         'grupos_escolares'
  33.     ];
  34.     public function __construct(EntityManagerInterface $em)
  35.     {
  36.         $this->em $em;
  37.     }
  38.     public function checkDateBetweenPeriod($dDate$iPediod '-99')
  39.     {
  40.         //$dDateFormat = date('Y-m-d');
  41.         //$dDateFormat = date('Y-m-d', strtotime($dDate));
  42.         $dDateFormat $dDate;
  43.         if (strpos($dDate,'/')  !== false) {
  44.             $aData explode('/'$dDate);
  45.             $dDateFormat $aData[2] . '-' $aData[1] . '-' $aData[0];
  46.         }
  47.         $dDateBegin date('Y-m-d'strtotime($iPediod ' year'));
  48.         $dDateEnd date('Y-m-d'strtotime('-1 year'));
  49.         if (($dDateFormat >= $dDateBegin)
  50.             && ($dDateFormat <= $dDateEnd))
  51.         {
  52.             return true;
  53.         } else {
  54.             return false;
  55.         }
  56.     }
  57.     public function getIdiomes()
  58.     {
  59.         $entities $this->em->getRepository(Idioma::class)->findAll();
  60.         
  61.         return $entities;
  62.     }
  63.     
  64.     public function logAction($request$user)
  65.     {
  66.         ini_set('max_execution_time'300);
  67.         
  68.         $sDataHora date("Y_m_d");
  69.         
  70.         $sNameLog "userLog_" $sDataHora ".txt";
  71.         switch ($_SERVER['SERVER_NAME']) {
  72.             case 'www.masella.com':
  73.             case 'beta.masella.com':
  74.                 $sBase '/var/www/vhosts/masella.com/httpdocs/';
  75.             break;
  76.             case 'dev22.masella.com':
  77.                 $sBase '/var/www/vhosts/dev22.masella.com/httpdocs/';
  78.                 break;
  79.             case 'dev23.masella.com':
  80.                 $sBase '/var/www/vhosts/dev23.masella.com/httpdocs/';
  81.                 break;
  82.             case 'masella015.developer.entorno.es':
  83.                 $sBase '/home/jramon/projects/masella2015/';
  84.                 break;
  85.             default:
  86.                 $sBase '/var/www/vhosts/' $_SERVER['HTTP_HOST'] . '/httpdocs/';
  87.                 break;
  88.         }
  89.         $sDir $sBase 'userLog/';
  90.         $aUserLog fopen($sDir $sNameLog'a');
  91.         $antigua umask(0);
  92.         umask($antigua);
  93.         fputs($aUserLogpreg_replace("/\r|\n/"""$this->processLogInfo($request$user)) . PHP_EOL);
  94.         fclose($aUserLog);
  95.         return true;
  96.     }
  97.     
  98.     private function processLogInfo($request$user)
  99.     {
  100.         $module 'Módulo: ' $request->attributes->get('_controller');
  101.         $action 'Action: ' $request->get('_route');
  102.         
  103.         $sLinea '->' date('Y-m-d H:i:s') . ', ' $module ', ' $action;
  104.         
  105.         $session_params = (array)$user;
  106.         
  107.         if ($session_params)
  108.             $sLinea .= ", sesión: ";
  109.         //dump ($session_params);
  110.         $allowed_keys = array('id','nom','email');
  111.         $sComa '';
  112.         foreach ($session_params as $key => $value)
  113.         {
  114.             $key trim(str_replace('App\Entity\Usuari','',$key));
  115.             if (in_array($key$allowed_keys))
  116.             {
  117.                 if ($key == 'en_culture')
  118.                     $key 'idioma';
  119.                 
  120.                 $sLinea .= $sComa ' ' $key '="' strip_tags(substr($value050)) . '"';
  121.                 $sComa ',';
  122.             }
  123.         }
  124.         $param '';
  125.         
  126.         //dump ($request);
  127.         //die();        
  128.         
  129.         $request_params array_merge((array)$request->query->all(), (array)$request->request->all(), (array)$request->attributes->all());
  130.         //dump ($request->request->all());
  131.         //dump ($request_params);
  132.         //die();
  133.         
  134.         if ($request_params)
  135.         {
  136.             $sLinea .= ", parámetros:  ";
  137.         }
  138.         $not_allowed_keys = array(  'input_x'
  139.                                     'input_y'
  140.                                     'PHPSESSID'
  141.                                     'input'
  142.                                     'pagina',
  143.                                     'user'
  144.                                     '__utmc'
  145.                                     '__utma'
  146.                                     '__utmz'
  147.                                     '__utmb',
  148.                                     'password'
  149.                                     'Usuario'
  150.                                     'Clave'
  151.                                     'locale'
  152.                                     'psaContext',
  153.                                     'BarExpanded'
  154.                                     'phpMyAdmin');
  155.         foreach ($request_params as $key => $value)
  156.         {
  157.             //echo '-' . $key . '-';
  158. //            dump($value);
  159. //            die();
  160.             if (!in_array($key$not_allowed_keys
  161.                     && $value)
  162.             {
  163.                 if ($key == 'en_culture')
  164.                     $key 'idioma';
  165.                 if (is_array($value))
  166.                 {
  167.                     $sLinea .= ' ' $key '=(';
  168.                     foreach ($value as $key => $val)
  169.                     {
  170.                         if (is_array($val)) $val implode(","$val);
  171.                         $sLinea .= ' ' $key '="' strip_tags(substr($val050)) . '" , ';
  172.                     }
  173.                     $sLinea .= ')';
  174.                 } else {
  175.                     $sLinea .= ' ' $key '="' strip_tags(substr($value050)) . '" , ';
  176.                 }
  177.                 //$param .= ' ' . $key . '="' . $value . '" , ';
  178.             }
  179.         }
  180.         //if (!$param)
  181.         //    $sLinea  .= ' sin parámetros, sólo acceso a la página ';
  182.         //else
  183.         //   $sLinea .= $param;
  184.         
  185.         return $sLinea;
  186.     }
  187.     
  188.     public function encrypt($string$key)
  189.     {
  190. //        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  191. //        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_URANDOM );
  192. //        mcrypt_generic_init($td, $key, $iv);
  193. //        $encrypted_data_bin = mcrypt_generic($td, $string);
  194. //        mcrypt_generic_deinit($td);
  195. //        mcrypt_module_close($td);
  196. //        $encrypted_data_hex = bin2hex($iv).bin2hex($encrypted_data_bin);
  197. //        return $encrypted_data_hex;
  198.         
  199.         //$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
  200.         $secret_iv '5fgf5HJ5g27';
  201.         $iv substr(hash('sha256'$secret_iv), 016);
  202.         $encrypted openssl_encrypt($string'aes-256-cbc'$key0$iv);
  203.         return urlencode(str_replace('/''**'base64_encode($encrypted '::' $iv)));
  204.         
  205.     }
  206.     
  207.     public function decrypt($encrypted_data_hex$key)
  208.     {
  209. //        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
  210. //        $iv_size_hex = mcrypt_enc_get_iv_size($td)*2;
  211. //        $iv = pack("H*", substr($encrypted_data_hex, 0, $iv_size_hex));
  212. //        $encrypted_data_bin = pack("H*", substr($encrypted_data_hex, $iv_size_hex));
  213. //        mcrypt_generic_init($td, $key, $iv);
  214. //        $decrypted = mdecrypt_generic($td, $encrypted_data_bin);
  215. //        mcrypt_generic_deinit($td);
  216. //        mcrypt_module_close($td);
  217. //        return $decrypted;
  218.         
  219.         $string str_replace('**''/'urldecode($encrypted_data_hex));
  220.         list($encrypted_data$iv) = explode('::'base64_decode($string), 2);
  221.         return rtrim(openssl_decrypt($encrypted_data'aes-256-cbc'$key0$iv), "\0");
  222.  
  223.     }
  224.     
  225.     public function createLocalitzador($id)
  226.     {
  227.         return 'ED' date('ym') . $id;
  228.     }
  229.     public function createLocalitzadorBaixadaTorxes($id)
  230.     {
  231.         return 'BT' date('ym') . str_pad($id4'0'STR_PAD_LEFT);
  232.     }
  233.     public function getNegativeNumber($iNumber)
  234.     {
  235.         if (!is_null($iNumber))
  236.         {
  237.             $iNumber $iNumber * (-1);
  238.         }
  239.         return $iNumber;
  240.     }
  241.     
  242.     /**
  243.     * Funció per substituir les lletres o els dígits per -
  244.     *
  245.     * @param $text
  246.     * @return unknown_type slugified $text
  247.     */
  248.     public function slugifyFile($text$cReplace "-")
  249.     {
  250.         if (empty($text
  251.                 || is_null($text
  252.                 || $text == '')
  253.         {
  254.             return '';
  255.         }
  256.         $text strtolower($text.'');
  257.         $text preg_replace("/[^a-z0-9.\s-]/"$cReplace$text);
  258.         $text trim(preg_replace("/\s+/"""$text));
  259.         $text preg_replace("/\s/"$cReplace$text);
  260.         return $text;
  261.     }
  262.     
  263.     public function texto_aleatorio ($long 8$letras_min true$letras_max true$num true)
  264.     {
  265.     $salt $letras_min?'abchefghknpqrstuvwxyz':'';
  266.     $salt .= $letras_max?'ACDEFHKNPRSTUVWXYZ':'';
  267.     $salt .= $num?(strlen($salt)?'2345679':'0123456789'):'';
  268.     if (strlen($salt) == 0)
  269.         {
  270.             return '';
  271.     }
  272.     $i 0;
  273.     $str '';
  274.     srand((double)microtime()*1000000);
  275.     while ($i $long)
  276.         {
  277.             $num rand(0strlen($salt)-1);
  278.             $str .= substr($salt$num1);
  279.             $i++;
  280.     }
  281.     return $str;
  282.     }
  283.     
  284.     public function separarCognoms ($sCognoms)
  285.     {
  286.         $aCognom explode(' '$sCognoms);
  287.         $sCognom $aCognom[0];
  288.         unset($aCognom[0]);
  289.         $sCognom2 implode(' ',$aCognom);
  290.         
  291.         $aResposta = array( 'cognom1'   => $sCognom,
  292.                             'cognom2'   => $sCognom2);
  293.         return new JsonResponse($aResposta);
  294.     }
  295.     
  296.     public function giraData($sData$sSeparado '/')
  297.     {
  298.         return implode($sSeparadoarray_reverse(explode($sSeparado$sData)));
  299.     } 
  300.     /**
  301.      * Guardem una imatge base64 a un arxiu
  302.      * @param type $sPath
  303.      * @param type $sBase64
  304.      * @return boolean
  305.      */
  306.     public function guardaImatge($sPath$sBase64)
  307.     {
  308.         $f fopen($sPath"wb") or die("Unable to open file!");
  309.         //////////////////////////////////////////////////////////////
  310.         // Guardem en l'arxiu el contenido que hi ha despés de la coma
  311.         //////////////////////////////////////////////////////////////
  312.         //fwrite($f, base64_decode(explode(",", $sBase64, 2)[1]));
  313.         fwrite($fbase64_decode($sBase64));
  314.         fclose($f);
  315.         return true;
  316.     }
  317.     /**
  318.      * Whether ip is from share internet
  319.      *
  320.      * @return mixed
  321.      */
  322.     public function getIpConsumer()
  323.     {
  324.         $ip_address '';
  325.         if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  326.             $ip_address $_SERVER['HTTP_CLIENT_IP'];
  327.         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  328.             ///////////////////////////
  329.             // Whether ip is from proxy
  330.             ///////////////////////////
  331.             $ip_address $_SERVER['HTTP_X_FORWARDED_FOR'];
  332.         } else {
  333.             ////////////////////////////////////
  334.             // Whether ip is from remote address
  335.             ////////////////////////////////////
  336.             $ip_address $_SERVER['REMOTE_ADDR'];
  337.         }
  338.         return $ip_address;
  339.     }
  340.     public function dataFormatYYYYMMDD($data$sSeparador '/')
  341.     {
  342.         $aData explode($sSeparador$data);
  343.         return $aData[2] . '-' $aData[1] . '-' $aData[0];
  344.     }
  345.     public function getPDO()
  346.     {
  347.         $conn = new \PDO('sqlsrv:Server=192.168.20.179;Database=TT_AXESS_MAS_Integracions;Encrypt = false;''Entorno''Etn@2537');
  348.         $conn->setAttributePDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION );
  349.         return $conn;
  350.     }
  351. }