src/Aviatur/GeneralBundle/Services/AviaturLogSave.php line 56

Open in your IDE?
  1. <?php
  2. namespace Aviatur\GeneralBundle\Services;
  3. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  4. use MicrosoftAzure\Storage\Blob\BlobRestProxy;
  5. class AviaturLogSave
  6. {
  7.     private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
  8.     private $projectDir;
  9.     private $env;
  10.     private $transactionIdSessionName;
  11.     /**
  12.      * @param type $kernel
  13.      * @param type $session
  14.      * @param type $transactionIdSessionName
  15.      */
  16.     public function __construct(SessionInterface $session$projectDir$env$transactionIdSessionName)
  17.     {
  18.         $this->session $session;
  19.         $this->projectDir $projectDir;
  20.         $this->env $env;
  21.         $this->transactionIdSessionName $transactionIdSessionName;
  22.     }
  23.     /**
  24.      * @param string $xml
  25.      *   Xml to save.
  26.      * @param string $command
  27.      *   The name of the services that mpa consumer.
  28.      * @param string $suffix
  29.      *   Any suffix you want to put for example RQ or RS. default:DF.
  30.      * @param string $transactionId
  31.      *   Unique identfier id for a file (This is usefull when you are trying to group requests and responses).
  32.      */
  33.     public function logSave($xml$command$suffix 'DF'$transactionId '')
  34.     {
  35.         if(isset($_ENV['APP_TYPELOG']) && $_ENV['APP_TYPELOG'] == "1"){
  36.             $this->logSave_azure($xml$command$suffix$transactionId);
  37.         }else{
  38.             $this->logSaveLocal($xml$command$suffix$transactionId);
  39.         }
  40.     }
  41.     public function logSave_azure($xml$command$suffix 'DF'$transactionId '')
  42.     {
  43.         $conf $this->loadConf();
  44.         $options['http'] = ['verify' => false];
  45.         $blobClient BlobRestProxy::createBlobService($_ENV['LOG_AZURE_STRCONN'],$options);
  46.         $currentDate date("Y-m-d");
  47.         $transactionId null != $transactionId $transactionId $this->session->get($this->transactionIdSessionName);
  48.         $fileToUpload $currentDate.'/'.$command.'/'.$transactionId.'__'.$command.'__'.microtime().'__'.gethostname().'__'.$suffix.'.'.'xml.gz';
  49.         try {
  50.             $blobClient->createBlockBlob($_ENV['LOG_AZURE_CONTAINER'], $fileToUploadgzencode($xml,9));
  51.         } catch (\Exception $ex) {
  52.             //var_dump($ex->getMessage());die;
  53.             $this->logSaveLocal($xml$command$suffix$transactionId);
  54.         }
  55.     }
  56.     
  57.     public function logSaveLocal($xml$command$suffix 'DF'$transactionId null)
  58.     {
  59.         $Conf $this->loadConf();
  60.         $path $this->projectDir.'/app/serviceLogs';
  61.         $ServiceLogsFolder $Conf->Command->General->LogSource->{$this->env}->FolderName;
  62.         //$path = preg_match('/[a-zA-Z]{1}\:/', $ServiceLogsFolder) ? $ServiceLogsFolder : $this->projectDir . '\\' . $ServiceLogsFolder;
  63.         $transactionId null != $transactionId $transactionId $this->session->get($this->transactionIdSessionName);
  64.         if (!isset($command) || '' == $command) {
  65.             $command 'unknow';
  66.         }
  67.         //File name. Te time() function is excuted to avoid files with the same name
  68.         if (strlen((string) $this->get_client_ip()) > 16) {
  69.             $userIp 'IPv6';
  70.         } else {
  71.             $userIp $this->get_client_ip();
  72.         }
  73.         $file $path.'/'.$command.'/'.$command.'__'.$transactionId.'__'.microtime().'__'.$userIp.'__'.$suffix.'.'.'xml';
  74.         //    if (($command == 'Login') || ($command == 'AirOverride')) {
  75.         //        return $file;
  76.         //    }
  77.         //    elseif ((($command == 'AirLowFareSearch') && ($suffix == 'RS'))) {
  78.         //        $xml = gzcompress($xml, 9);
  79.         //    }
  80.         //set permissions
  81.         @chmod($path0777);
  82.         //Verify if exists a directory with the command name
  83.         if (!file_exists($path.'/'.$command)) {
  84.             //create the directory
  85.             mkdir($path.'/'.$command0777);
  86.             //create the security file
  87.             $secFile $path.'/'.$command.'/index.html';
  88.             $fh fopen($secFile'w');
  89.             fwrite($fh'<html><body bgcolor="#FFFFFF"></body></html>');
  90.             fclose($fh);
  91.             //Set permissions
  92.             //@chmod($secFile, 0644);
  93.         } else {
  94.             //change permissions to directory
  95.             @chmod($path.'/'.$command0777);
  96.         }
  97.         //create the current time
  98.         $curentDateAndTime date('Ymd_His'time());
  99.         //Write the xml
  100.         $fvalidation file_exists($file);
  101.         if ($fvalidation) {
  102.             $file $path.'/'.$command.'/'.$command.'__'.$transactionId.'__'.time().'__'.$userIp.'__'.$suffix.'.'.'xml';
  103.             $fh fopen($file'w');
  104.         } else {
  105.             $fh fopen($file'w');
  106.         }
  107.         fwrite($fh$xml);
  108.         //close the file
  109.         fclose($fh);
  110.         //Set permissions
  111.         //@chmod($file, 0644);
  112.         //@chmod($path . '/' . $command, 0644);
  113.         //@chmod($path, 0644);
  114.         return $file;
  115.     }
  116.     public function saveToFile($variable)
  117.     {
  118.         $command null;
  119.         $path $this->projectDir.'/app/serviceLogs';
  120.         $curentDateAndTime date('Ymd_His'time());
  121.         $file $path.'/_var_dump/'.$curentDateAndTime.'.txt';
  122.         if (is_file($file)) {
  123.             $fh fopen($file'w');
  124.         } else {
  125.             return false;
  126.         }
  127.         fwrite($fh$variable);
  128.         //close the file
  129.         fclose($fh);
  130.     }
  131.     final private function get_client_ip()
  132.     {
  133.         $ipaddress '';
  134.         if (getenv('HTTP_CLIENT_IP')) {
  135.             $ipaddress getenv('HTTP_CLIENT_IP');
  136.         } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
  137.             $ipaddress getenv('HTTP_X_FORWARDED_FOR');
  138.         } elseif (getenv('HTTP_X_FORWARDED')) {
  139.             $ipaddress getenv('HTTP_X_FORWARDED');
  140.         } elseif (getenv('HTTP_FORWARDED_FOR')) {
  141.             $ipaddress getenv('HTTP_FORWARDED_FOR');
  142.         } elseif (getenv('HTTP_FORWARDED')) {
  143.             $ipaddress getenv('HTTP_FORWARDED');
  144.         } elseif (getenv('REMOTE_ADDR')) {
  145.             $ipaddress getenv('REMOTE_ADDR');
  146.         } else {
  147.             $ipaddress 'UNKNOWN';
  148.         }
  149.         return (strpos($ipaddress',') !== false) ? trim(explode(','$ipaddress)[0]) : (substr_count($ipaddress':') > $ipaddress explode(':'$ipaddress)[0]);
  150.     }
  151.     private function loadConf()
  152.     {
  153.         $filename __DIR__.'/../Resources/config/log_purge.json';
  154.         $confFile '';
  155.         if (file_exists($filename)) {
  156.             $confFile fopen($filename'r');
  157.         }
  158.         $confContent fread($confFilefilesize($filename));
  159.         $conf json_decode($confContent);
  160.         return $conf;
  161.     }
  162. }