<?php
namespace Aviatur\GeneralBundle\Services;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
class AviaturLogSave
{
private \Symfony\Component\HttpFoundation\Session\SessionInterface $session;
private $projectDir;
private $env;
private $transactionIdSessionName;
/**
* @param type $kernel
* @param type $session
* @param type $transactionIdSessionName
*/
public function __construct(SessionInterface $session, $projectDir, $env, $transactionIdSessionName)
{
$this->session = $session;
$this->projectDir = $projectDir;
$this->env = $env;
$this->transactionIdSessionName = $transactionIdSessionName;
}
/**
* @param string $xml
* Xml to save.
* @param string $command
* The name of the services that mpa consumer.
* @param string $suffix
* Any suffix you want to put for example RQ or RS. default:DF.
* @param string $transactionId
* Unique identfier id for a file (This is usefull when you are trying to group requests and responses).
*/
public function logSave($xml, $command, $suffix = 'DF', $transactionId = '')
{
if(isset($_ENV['APP_TYPELOG']) && $_ENV['APP_TYPELOG'] == "1"){
$this->logSave_azure($xml, $command, $suffix, $transactionId);
}else{
$this->logSaveLocal($xml, $command, $suffix, $transactionId);
}
}
public function logSave_azure($xml, $command, $suffix = 'DF', $transactionId = '')
{
$conf = $this->loadConf();
$options['http'] = ['verify' => false];
$blobClient = BlobRestProxy::createBlobService($_ENV['LOG_AZURE_STRCONN'],$options);
$currentDate = date("Y-m-d");
$transactionId = null != $transactionId ? $transactionId : $this->session->get($this->transactionIdSessionName);
$fileToUpload = $currentDate.'/'.$command.'/'.$transactionId.'__'.$command.'__'.microtime().'__'.gethostname().'__'.$suffix.'.'.'xml.gz';
try {
$blobClient->createBlockBlob($_ENV['LOG_AZURE_CONTAINER'], $fileToUpload, gzencode($xml,9));
} catch (\Exception $ex) {
//var_dump($ex->getMessage());die;
$this->logSaveLocal($xml, $command, $suffix, $transactionId);
}
}
public function logSaveLocal($xml, $command, $suffix = 'DF', $transactionId = null)
{
$Conf = $this->loadConf();
$path = $this->projectDir.'/app/serviceLogs';
$ServiceLogsFolder = $Conf->Command->General->LogSource->{$this->env}->FolderName;
//$path = preg_match('/[a-zA-Z]{1}\:/', $ServiceLogsFolder) ? $ServiceLogsFolder : $this->projectDir . '\\' . $ServiceLogsFolder;
$transactionId = null != $transactionId ? $transactionId : $this->session->get($this->transactionIdSessionName);
if (!isset($command) || '' == $command) {
$command = 'unknow';
}
//File name. Te time() function is excuted to avoid files with the same name
if (strlen((string) $this->get_client_ip()) > 16) {
$userIp = 'IPv6';
} else {
$userIp = $this->get_client_ip();
}
$file = $path.'/'.$command.'/'.$command.'__'.$transactionId.'__'.microtime().'__'.$userIp.'__'.$suffix.'.'.'xml';
// if (($command == 'Login') || ($command == 'AirOverride')) {
// return $file;
// }
// elseif ((($command == 'AirLowFareSearch') && ($suffix == 'RS'))) {
// $xml = gzcompress($xml, 9);
// }
//set permissions
@chmod($path, 0777);
//Verify if exists a directory with the command name
if (!file_exists($path.'/'.$command)) {
//create the directory
mkdir($path.'/'.$command, 0777);
//create the security file
$secFile = $path.'/'.$command.'/index.html';
$fh = fopen($secFile, 'w');
fwrite($fh, '<html><body bgcolor="#FFFFFF"></body></html>');
fclose($fh);
//Set permissions
//@chmod($secFile, 0644);
} else {
//change permissions to directory
@chmod($path.'/'.$command, 0777);
}
//create the current time
$curentDateAndTime = date('Ymd_His', time());
//Write the xml
$fvalidation = file_exists($file);
if ($fvalidation) {
$file = $path.'/'.$command.'/'.$command.'__'.$transactionId.'__'.time().'__'.$userIp.'__'.$suffix.'.'.'xml';
$fh = fopen($file, 'w');
} else {
$fh = fopen($file, 'w');
}
fwrite($fh, $xml);
//close the file
fclose($fh);
//Set permissions
//@chmod($file, 0644);
//@chmod($path . '/' . $command, 0644);
//@chmod($path, 0644);
return $file;
}
public function saveToFile($variable)
{
$command = null;
$path = $this->projectDir.'/app/serviceLogs';
$curentDateAndTime = date('Ymd_His', time());
$file = $path.'/_var_dump/'.$curentDateAndTime.'.txt';
if (is_file($file)) {
$fh = fopen($file, 'w');
} else {
return false;
}
fwrite($fh, $variable);
//close the file
fclose($fh);
}
final private function get_client_ip()
{
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP')) {
$ipaddress = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('HTTP_X_FORWARDED')) {
$ipaddress = getenv('HTTP_X_FORWARDED');
} elseif (getenv('HTTP_FORWARDED_FOR')) {
$ipaddress = getenv('HTTP_FORWARDED_FOR');
} elseif (getenv('HTTP_FORWARDED')) {
$ipaddress = getenv('HTTP_FORWARDED');
} elseif (getenv('REMOTE_ADDR')) {
$ipaddress = getenv('REMOTE_ADDR');
} else {
$ipaddress = 'UNKNOWN';
}
return (strpos($ipaddress, ',') !== false) ? trim(explode(',', $ipaddress)[0]) : (substr_count($ipaddress, ':') > 1 ? $ipaddress : explode(':', $ipaddress)[0]);
}
private function loadConf()
{
$filename = __DIR__.'/../Resources/config/log_purge.json';
$confFile = '';
if (file_exists($filename)) {
$confFile = fopen($filename, 'r');
}
$confContent = fread($confFile, filesize($filename));
$conf = json_decode($confContent);
return $conf;
}
}