I have below code which is scheduled to run daily at midnight using task scheduler. First my code is connecting to the database (function __construct),then it comparing he files which are in my logs folder and files which are already uploaded(function uploadLogs), then function uploadLog is uploading the file to the database which are not uploaded.The file which is uploaded is written a file in order to duplicate data(function updateRegistery).
Now when the file is uploaded once, then it can't be uploaded again, since the name is already written in the registry file. However I want to run it on daily basis. I really cant think of a way to do that. Can someone please help me with the coding.
My code:
<?php
class logAgent
{
const CONFIG_FILENAME = "data_config.ini";
private $_dbConn;
private $_config;
function __construct()
{
$this->_loadConfig();
$this->_dbConn = oci_connect($this->_config['db_usrnm'],
$this->_config['db_pwd'],
$this->_config['hostnm_sid']);
}
public function uploadLogs(){
//Comparison of all the files present in the logs directory and all the files which has been already uploaded
$f = fopen($this->_config['uploadedRegistry'], 'r');
$contents = [];
while (FALSE !== ($row = fgetcsv($f, 1000, $this->_config['filenameTimeSeparator']))){
$contents[] = $row[0];
}
$files = array_map('basename',glob($this->_config['logspath'] . $this->_config['fileextension'],GLOB_BRACE));
$result = array_diff($files, $contents);
foreach($result as $r){
$this->uploadLog($r);
}
}
private function _loadConfig()
{
// Loads config
$path = dirname(__FILE__) . '/' . self::CONFIG_FILENAME;
$this->_config = parse_ini_file($path) ;
}
public function uploadLog($filename) {
//Uploads file to database
$filename = trim($this->_config['logspath'] . trim($filename));
if(file_exists($filename)){
$myfile = fopen($filename, "r");
while(!feof($myfile)) {
$content= fgets($myfile);
$carray=explode($this->_config['logFomatDelimiter'],$content);
list($REQUEST_TIME, $HOSTNAME, $IP_ADDRESS, $WORKFLOW_NAME, $USERID, $EVENT_MESSAGE , $OPTIONAL_DETAILS , $SESSION_COOKIES)=$carray;
$data = array_map(function($v) { return explode('=',trim($v));},explode(';',substr($carray[6],strpos($carray[6],'=')+1)));
foreach ($data as $datum) {
if ($datum[0] == 'TANID') {
$SESSION_COOKIES= $datum[0] . "=" . $datum[1];
break;
}
}
$statement = "INSERT INTO AUTH_LOGS(REQUEST_TIME, HOSTNAME, IP_ADDRESS, WORKFLOW_NAME, USERID, EVENT_MESSAGE , OPTIONAL_DETAILS , SESSION_COOKIES)
values(to_date(:REQUEST_TIME, 'YYYY/MM/DD HH24:MI:SS'), :HOSTNAME, :IP_ADDRESS, :WORKFLOW_NAME , :USERID ,:EVENT_MESSAGE ,
:OPTIONAL_DETAILS, :SESSION_COOKIES)";
//Preparing an Oracle statement for execution
$compiled = oci_parse($this->_dbConn, $statement);
//binding values to named parameters
oci_bind_by_name($compiled, ':REQUEST_TIME', $REQUEST_TIME);
oci_bind_by_name($compiled, ':HOSTNAME', $HOSTNAME);
oci_bind_by_name($compiled, ':IP_ADDRESS', $IP_ADDRESS);
oci_bind_by_name($compiled,':WORKFLOW_NAME', $WORKFLOW_NAME);
oci_bind_by_name($compiled, ':USERID', $USERID);
$EVENT_MESSAGE = str_replace('"', '', $EVENT_MESSAGE);
oci_bind_by_name($compiled, ':EVENT_MESSAGE', $EVENT_MESSAGE);
$OPTIONAL_DETAILS = str_replace('"', '', $OPTIONAL_DETAILS);
oci_bind_by_name($compiled, ':OPTIONAL_DETAILS', $OPTIONAL_DETAILS);
$SESSION_COOKIES = str_replace(')"', '', $SESSION_COOKIES);
oci_bind_by_name($compiled, ':SESSION_COOKIES', $SESSION_COOKIES);
//Executing statement
oci_execute($compiled, OCI_COMMIT_ON_SUCCESS);
}
//closing the file
fclose($myfile);
$this->updateRegistry($filename);
return TRUE;
}
else{
throw new Exception("File doesnot exist");
}
}
public function sendEmail(Exception $e){
$sent = mail($this->_config['recipients'], $this->_config['notificationSubject'], $e);
}
public function updateRegistry($filename)
{
$uploadedfilename = fopen($this->_config['uploadedRegistry'], "a");
fwrite($uploadedfilename, basename($filename . date($this->_config['filenameTimeSeparator'] . 'Ymdhi', time())) . PHP_EOL);
}
}
try {
$logAgent = new logAgent();
$logAgent->uploadLogs();
}
catch (Exception $e) {
$logAgent->sendEmail($e);
}
?>
</div>