. */ /** * @defgroup AutoImport Massive importation of files in Maarch DataBase */ /** * * Massive importation of files in Maarch DataBase * All known errors are classified by a return code. * In case of a known error #3, #5, #6, the file in the course of treatment is moved in the failed directory. * * List of known errors : * 2 : The free space available in the docserver is not sufficient to continue : put all the loading batch in failed directory and stop application, * 3 : Copy of a file failed during treatment of it : put the file in failed directory, * 4 : Maarch Auto Import cannot execute query on database : put all the loading batch in failed directory and stop application, * 5 : Image of a xml file not found : put the xml file in failed directory, * 6 : Xml of an image file not found : put the xml file in failed directory, * 7 : Move imported files in backup failed : don't delete file in incoming directory and stop application, * 8 : Unable to move file in CreateLockFile function : put all the loading batch in failed directory and stop application, * 9 : Error DATE_FORMAT tag : wrong description of the date in the mapping file : put all the loading batch in failed directory and stop application, * 10 : Error XML format (DATE_FORMAT tag) ! Please check your xml transfert file : put all the loading batch in failed directory and stop application, * 11 : Workbatch already exist : put all the loading batch in failed directory and stop application, * 12 : Path of the docserver not exists : put all the loading batch in failed directory and stop application, * In case of a known error #2, #4, #7, #8, #9, #10, #11, #12 all the loading batch is moved in failed directory. * @file * @author Loïc Vinet * @author Laurent Giovanonni * @author Claire Figueras * @date $date$ * @version $Revision$ * @ingroup dispatch * @brief Massive importation of files in Maarch DataBase */ //Error mode and function error_reporting(E_ERROR); set_error_handler(errorHandler); //Global var of the program /** * Name of the configuration of the AutoImport */ $_ENV['configName'] = ""; /** * Path to the log file */ $_ENV['log'] = ""; /** * Batch of folder to load in Maarch */ $_ENV['folderLoaded'] = array(); /** * Batch of Files to load in Maarch */ $_ENV['fileNameLoaded'] = array(); /** * Flag for the without xml mod */ $_ENV['withoutXml'] = "false"; /** * Size of the transfert */ $_ENV['currentTransfertSize'] = 0; /** * Connection object to database 1 */ $_ENV['db'] = ""; /** * Connection object to database 2 */ $_ENV['db2'] = ""; /** * Scan import directory */ $_ENV['sid'] = ""; /** * Work batch number of AutoImport */ $_ENV['wbai'] = 0; /** * AutoImport directory */ $_ENV['aid'] = ""; /** * Location of the database server */ $_ENV['databaseserver'] = ""; /** * Port of the database server */ $_ENV['databaseport'] = 0; /** * Name of the database */ $_ENV['database'] = ""; /** * Type of the database */ $_ENV['databasetype'] = ""; /** * User name of the database */ $_ENV['databaseuser'] = ""; /** * Password of the database */ $_ENV['databasepwd'] = ""; /** * Workspace of the database, only for Oracle */ $_ENV['databaseworkspace'] = ""; /** * OS */ $_ENV['osName'] = ""; /** * Limit size of the docserver */ $_ENV['sizeLimitConfig'] = ""; /** * Name of the resource table */ $_ENV['tableName'] = ""; /** * Fields of the mapping file */ $_ENV['tabMaarchDefaultValue'] = array(); /** * Field to view in debug mode */ $_ENV['tabMaarchField'] = array(); /** * Date fields of the mapping file */ $_ENV['tabMaarchDate'] = array(); /** * Format of date fields of the mapping file */ $_ENV['tabFormatDate'] = array(); /** * Format of date fields of the config file */ $_ENV['dateTimeFormat'] = ""; /** * String fields of the mapping file */ $_ENV['tabMaarchString'] = array(); /** * Integer fields of the mapping file */ $_ENV['tabMaarchInt'] = array(); /** * Default value if empty of the mapping file */ $_ENV['tabMaarchDefaultIfTagEmpty'] = array(); /** * Total nb files processed */ $_ENV['totalNbfile'] = 0; /** * User exit of the program, contains 1 if any problem appears */ $_ENV['errorLevel'] = 0; /** * Nb of errors */ $_ENV['nbErrors'] = 0; /** * Infos of errors */ $_ENV['infoErrors'] = ""; /** * Insert sql mode */ $_ENV['insertMode'] = "false"; /** * Backup batch mode */ $_ENV['backupBatch'] = "true"; /** * Exclude existing documents mode */ $_ENV['excludeExistingDocs'] = "false"; /** * Exclude existing documents folder */ $_ENV['excludeExistingDocsFolder'] = "duplicate"; /** * Controle complete files mode */ $_ENV['controlCompleteFiles'] = "false"; /** * fingerprint mode of the docserver */ $_ENV['docserverFingerprintMode'] = "NONE"; /** * Creation of the log file */ function loginCreation() { if(!is_dir($_ENV['aid'].DIRECTORY_SEPARATOR."log".DIRECTORY_SEPARATOR)) { mkdir($_ENV['aid'].DIRECTORY_SEPARATOR."log".DIRECTORY_SEPARATOR,0777); } $_ENV['log'] = $_ENV['aid'].DIRECTORY_SEPARATOR."log".DIRECTORY_SEPARATOR.$_ENV['configName'].".log"; writeLog("Application start with : ".$_SERVER['SCRIPT_FILENAME']); } /** * Write on the log file * @param $eventInfo string text which is written in the log file */ function writeLog($eventInfo) { $logFileOpened = fopen($_ENV['log'], "a"); fwrite($logFileOpened, "[".date("d")."/".date("m")."/".date("Y")." ".date("H").":".date("i").":".date("s")."] ".$eventInfo."\r\n"); fclose($logFileOpened); } /** * Write on the log file errors * @param $eventInfo string text which is written in the log file * @param $errorLevel integer text return code of an error * @param $filePath string path of the file to put in error */ function manageError($eventInfo, $errorLevel, $filePath = "") { $logFileOpened = fopen($_ENV['log'], "a"); fwrite($logFileOpened, "[".date("d")."/".date("m")."/".date("Y")." ".date("H").":".date("i").":".date("s")."][ERRNO]".$errorLevel."[ERRNO][ERROR]".$eventInfo."[ERROR]\r\n"); fclose($logFileOpened); if($errorLevel == 2 || $errorLevel == 4 || $errorLevel == 7 || $errorLevel == 8 || $errorLevel == 9 || $errorLevel == 10 || $errorLevel == 11) { createLockFile($errorLevel); writeLog("End of application, return code : ".$errorLevel); logInDataBase(0, 1, "[ERRNO]".$errorLevel."[ERRNO][ERROR]".$eventInfo."[ERROR]
"); exit($errorLevel); } elseif($errorLevel == 3 || $errorLevel == 5 || $errorLevel == 6) { //put file in error, no exit if(file_exists($filePath)) { $_ENV['nbErrors']++; $_ENV['infoErrors'] .= "[ERRNO]".$errorLevel."[ERRNO][ERROR]".$eventInfo."[ERROR]
"; putFileInError($filePath); } } else { createLockFile($errorLevel); logInDataBase(0, 1, "[ERRNO]".$errorLevel."[ERRNO][ERROR]".$eventInfo."[ERROR]
"); exit($errorLevel); } } /** * Managing of errors * @param $errno integer number of the error * @param $errstr string text of the error * @param $errfile string file concerned with the error * @param $errline integer line of the error * @param $errcontext string context of the error */ function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) { $string = $errstr; $search1="'xml_file_loaded'"; $search2="'Undefined offset: 2'"; preg_match($search1,$string,$out); $count=count($out[0]); if($count == 1) { $find1 = true; } preg_match($search2,$string,$out); $count=count($out[0]); if($count == 1) { $find2 = true; } if(!$find1 && !$find2) { manageError("from line ".$errline." : ". $errstr, 1); $_ENV['errorLevel'] = 1; } } /** * Insert in the database the report of AI * @param $workBatchAutoimport */ function logInDataBase($totalProcessed = 0, $totalErrors = 0, $info = "") { if($_ENV['databasetype'] == "SQLSERVER") { $dateNow = "getdate()"; } elseif($_ENV['databasetype'] == "MYSQL" || $_ENV['databasetype'] == "POSTGRESQL") { $dateNow = "now()"; } elseif($_ENV['databasetype'] == "ORACLE") { $dateNow = "SYSDATE"; } $req = "insert into history_batch(module_name, batch_id, event_date, total_processed, total_errors, info) values('AutoImport', ".$_ENV['wbai'].", ".$dateNow.", ".$totalProcessed.", ".$totalErrors.", '".protectStringDb(substr($info,0, 999))."')"; //$_ENV['db']->query($req); } /** * Control in DB if the document already exists with the fingerprint * @param $fingerprint */ function controlExistingDocsInDb($fingerprint) { $flagExists = false; if($_ENV['excludeExistingDocs'] == "false") { return $flagExists; } if($_ENV['dateTimeFormat'] == "YYYY-MM-DD") { $myDate = date('Y/m/d', strtotime('-3 month')); } elseif($_ENV['dateTimeFormat'] == "MM/DD/YYYY") { $myDate = date('m/d/Y', strtotime('-3 month')); } else { $myDate = date('d/m/Y', strtotime('-3 month')); } if($_ENV['databasetype'] == 'ORACLE') { $req = "select fingerprint from ".$_ENV['tableName']." where fingerprint = '".$fingerprint."' and creation_date > to_date('".$myDate."','".$_ENV['dateTimeFormat']."')"; } else { $req = "select fingerprint from ".$_ENV['tableName']." where fingerprint = '".$fingerprint."' and creation_date > '".$myDate."'"; } //echo $req."\r\n"; $_ENV['db']->query($req); while($reqResult = $_ENV['db']->fetch_array()) { if($reqResult[0] <> "") { $flagExists = true; break; } } return $flagExists; } /** * Browse each file and folder in the import folder and return true if the process have to continue * @param $folder path string of the incoming folder */ function scanIncoming($folder) { $foundDoc = false; $classScan= dir($folder); while(($fileScan=$classScan->read())!=false) { if($foundDoc) { return $foundDoc; } if($fileScan=='.'||$fileScan=='..' ||$fileScan=='backup' ||$fileScan=='failed' || $fileScan == '.svn' || $fileScan == $_ENV['excludeExistingDocsFolder']) { continue; } elseif(is_dir($folder.DIRECTORY_SEPARATOR.$fileScan)) { //Looking in the sub directories $subClassScan = dir($folder.DIRECTORY_SEPARATOR.$fileScan); while(($subFileScan=$subClassScan->read())!=false) { if($subFileScan=='.' || $subFileScan=='..' || $subFileScan == '.svn') { continue; } else { $foundDoc = true; } } } else { $foundDoc = true; } } return $foundDoc; } /** * Browse each file and folder in the import folder and return true if true when the all files are completed * @param $folder path string of the incoming folder */ function isCompleteBatch($folder, $delay=500, $pointer=0) { $complete = true; $classScan= dir($folder); while(($fileScan=$classScan->read())!=false) { if($fileScan=='.'||$fileScan=='..' ||$fileScan=='backup' ||$fileScan=='failed' || $fileScan == '.svn' || $fileScan == $_ENV['excludeExistingDocsFolder']) { continue; } elseif(is_dir($folder.DIRECTORY_SEPARATOR.$fileScan)) { //Looking in the sub directories $subClassScan = dir($folder.DIRECTORY_SEPARATOR.$fileScan); echo "dir : ".$folder.DIRECTORY_SEPARATOR.$fileScan."\r\n"; while(($subFileScan=$subClassScan->read())!=false) { if($subFileScan=='.' || $subFileScan=='..' || $subFileScan == '.svn') { continue; } else { echo "control of ".$folder.$fileScan.DIRECTORY_SEPARATOR.$subFileScan."\r\n"; $ofile = @fopen($folder.$fileScan.DIRECTORY_SEPARATOR.$subFileScan, "r"); if(isCompleteFile($ofile)) { //continue fclose($ofile); } else { $complete = false; } } } } else { echo "control of ".$folder.$fileScan."\r\n"; $ofile = @fopen($folder.$fileScan, "r"); if(isCompleteFile($ofile)) { //continue fclose($ofile); } else { $complete = false; } } } return $complete; } /** * Return true when the file is completed * @param $file * @param $delay * @param $pointer position in the file */ function isCompleteFile($file, $delay=500, $pointer=0) { if($file == null) { return false; } fseek($file, $pointer); $currentLine = fgets($file); while (!feof($file)) { $currentLine = fgets($file); } $currentPos = ftell($file); //Wait $delay ms usleep($delay * 1000); if( $currentPos == $pointer ) { return true; } else { return isCompleteFile($file, $delay, $currentPos); } } /** * Browse each file and folder in the import folder * @param $folder path string of the incoming folder * @param $availableSpace string available space on the docserver * @param $docServer string path of the docserver * @param $docServerName string name of the docserver * @param $tabMaarchValue array list of tags in xml * @param $subDocServer string * @param $docServerMiniPath string * @param $initFile string */ function scan($folder, $availableSpace, $docServer, $docServerName, $tabMaarchValue, $subDocServer, $docServerMiniPath, $initFile) { $currentTransfertSize = 0; $classScan= dir($folder); $pass=true; $tmpFileTri = array(); while(($fileScan=$classScan->read())!=false) { if(($initFile == 0) && ($pass==true)) { $folderName = mkdir($docServer.str_pad($initFile,4,"0",STR_PAD_LEFT).DIRECTORY_SEPARATOR,0777); $pass = false; $nbFiles = 0; } if(strstr($nbFiles, "000")==true) { $initFile++; $folderName = mkdir($docServer.str_pad($initFile,4,"0",STR_PAD_LEFT).DIRECTORY_SEPARATOR,0777); $nbFiles = 0; } $currentInitFolder = str_pad($initFile,4,"0",STR_PAD_LEFT).DIRECTORY_SEPARATOR; if($fileScan=='.'||$fileScan=='..' ||$fileScan=='backup' ||$fileScan=='failed' || $fileScan == '.svn' || $fileScan == $_ENV['excludeExistingDocsFolder']) { continue; } elseif(is_dir($folder.DIRECTORY_SEPARATOR.$fileScan)) { //echo 'dir '.$folder.DIRECTORY_SEPARATOR.$fileScan." \n"; //Looking in the sub directories array_push($_ENV['folderLoaded'], $fileScan); $SubClassScan= dir($folder.DIRECTORY_SEPARATOR.$fileScan); while(($subFileScan=$SubClassScan->read())!=false) { if($subFileScan=='.' || $subFileScan=='..' || $subFileScan == '.svn') { continue; } else { if(strtoupper(extractFileExt($subFileScan)) <> "XML") { if(!controlExistingDocsInDb(doFingerprint($folder . $fileScan . DIRECTORY_SEPARATOR . $subFileScan, $_ENV['docserverFingerprintMode']))) { array_push($tmpFileTri, $fileScan.DIRECTORY_SEPARATOR.$subFileScan); array_push($_ENV['fileNameLoaded'], $fileScan.DIRECTORY_SEPARATOR.$subFileScan); if(file_exists($folder.$fileScan.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($subFileScan), "", $subFileScan)."xml"))) { array_push($tmpFileTri, $fileScan.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($subFileScan), "", $subFileScan)."xml")); array_push($_ENV['fileNameLoaded'], $fileScan.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($subFileScan), "", $subFileScan)."xml")); } elseif(file_exists($folder.$fileScan.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($subFileScan), "", $subFileScan)."XML"))) { array_push($tmpFileTri, $fileScan.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($subFileScan), "", $subFileScan)."XML")); array_push($_ENV['fileNameLoaded'], $fileScan.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($subFileScan), "", $subFileScan)."XML")); } } else { writeLog("File already Exists in DB ".$folder.$fileScan.DIRECTORY_SEPARATOR.$subFileScan); putFileInAlreadyExistsDirectory($folder.$fileScan.DIRECTORY_SEPARATOR.$subFileScan, $folder, $fileScan); } } else { //array_push($tmpFileTri, $fileScan.DIRECTORY_SEPARATOR.$subFileScan); //array_push($_ENV['fileNameLoaded'], $fileScan.DIRECTORY_SEPARATOR.$subFileScan); } } } } else { if(strtoupper(extractFileExt($fileScan)) <> "XML") { if(!controlExistingDocsInDb(doFingerprint($folder . DIRECTORY_SEPARATOR . $fileScan, $_ENV['docserverFingerprintMode']))) { array_push($tmpFileTri, $fileScan); array_push($_ENV['fileNameLoaded'], $fileScan); if(file_exists($folder.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($fileScan), "", $fileScan)."xml"))) { array_push($tmpFileTri, extractFileName(str_replace(extractFileExt($fileScan), "", $fileScan)."xml")); array_push($_ENV['fileNameLoaded'], extractFileName(str_replace(extractFileExt($fileScan), "", $fileScan)."xml")); } elseif(file_exists($folder.DIRECTORY_SEPARATOR.extractFileName(str_replace(extractFileExt($fileScan), "", $fileScan)."XML"))) { array_push($tmpFileTri, extractFileName(str_replace(extractFileExt($fileScan), "", $fileScan)."XML")); array_push($_ENV['fileNameLoaded'], extractFileName(str_replace(extractFileExt($fileScan), "", $fileScan)."XML")); } } else { writeLog("File already Exists in DB ".$folder.DIRECTORY_SEPARATOR.$fileScan); putFileInAlreadyExistsDirectory($folder.DIRECTORY_SEPARATOR.$fileScan, $folder); } } else { //array_push($tmpFileTri, $fileScan); //array_push($_ENV['fileNameLoaded'], $fileScan); } } } //Sorting tab for compatibilites with Unix system sort($tmpFileTri); $totalNbfile = 0; $sqlQueryBuilded = ''; $sqlQueryErrorBuilded = ''; for($l=0;$lquery($reqCwbai); while($reqResult = $_ENV['db']->fetch_array()) { $workBatchAutoImport = $reqResult[0] + 1; } return $workBatchAutoImport; } /** * Create a log file in the application folder * @param $path string path of the file to backup */ function putFileInError($path) { $directory = $_ENV['sid']; if(!is_dir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR, 0777); } if(!is_dir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR, 0777); } if(!copy($path, $directory."failed".DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR.extractFileName($path))) { echo "Unable to move file in putFileInError function. AutoImport has been stopped!\r\n"; manageError("Unable to move file in putFileInError function. AutoImport has been stopped", 8); } else { unlink($path); } } /** * Put already exists file in a tmp dir * @param $path string path of the file to backup */ function putFileInAlreadyExistsDirectory($path, $shortPath, $additionnalPath="") { $directory = $_ENV['sid']; if(!is_dir($directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR, 0777); } if(!is_dir($directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR, 0777); } if($additionnalPath <> "" ) { if(!is_dir($directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR.$additionnalPath.DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR.$additionnalPath.DIRECTORY_SEPARATOR, 0777); } $initPath = $shortPath.DIRECTORY_SEPARATOR.$additionnalPath.DIRECTORY_SEPARATOR; $targetPath = $directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR.$additionnalPath.DIRECTORY_SEPARATOR; } else { $initPath = $shortPath.DIRECTORY_SEPARATOR; $targetPath = $directory.DIRECTORY_SEPARATOR.$_ENV['excludeExistingDocsFolder'].DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR; } if($_ENV['withoutXml'] == "false") { if(file_exists($initPath.extractFileName(str_replace(extractFileExt($path), "", extractFileName($path))."xml"))) { if(!rename($initPath.extractFileName(str_replace(extractFileExt($path), "", extractFileName($path))."xml"), $targetPath.extractFileName(str_replace(extractFileExt($path), "", extractFileName($path))."xml"))) { echo "Unable to move file in putFileInAlreadyExistsDirectory function. AutoImport has been stopped!\r\n"; manageError("Unable to move file in putFileInAlreadyExistsDirectory function. AutoImport has been stopped", 8); } } elseif(file_exists($initPath.extractFileName(str_replace(extractFileExt($path), "", extractFileName($path))."XML"))) { if(!rename($initPath.extractFileName(str_replace(extractFileExt($path), "", extractFileName($path))."XML"), $targetPath.extractFileName(str_replace(extractFileExt($path), "", extractFileName($path))."XML"))) { echo "Unable to move file in putFileInAlreadyExistsDirectory function. AutoImport has been stopped!\r\n"; manageError("Unable to move file in putFileInAlreadyExistsDirectory function. AutoImport has been stopped", 8); } } } if(!rename($path, $targetPath.extractFileName($path))) { echo "Unable to move file in putFileInAlreadyExistsDirectory function. AutoImport has been stopped!\r\n"; manageError("Unable to move file in putFileInAlreadyExistsDirectory function. AutoImport has been stopped", 8); } } /** * Lock the AutoImport and moved the all batch in the failed directory */ function createLockFile() { $directory = $_ENV['sid']; $lockFile = $_ENV['aid'].$_ENV['configName']."_error.lck"; $lockFileInit = $_ENV['aid'].$_ENV['configName'].".lck"; if(file_exists($lockFile)) { unlink($lockFile); } if(file_exists($lockFileInit)) { unlink($lockFileInit); } $lockFileOpened = fopen($lockFile, "a"); fwrite($lockFileOpened, "1"); fclose($lockFileOpened); echo "\r\nMoving imported files in subfolder : ".$directory."failed".DIRECTORY_SEPARATOR.$_ENV['wbai']."\r\n"; writeLog("Moving imported files in subfolder : ".$directory."failed".DIRECTORY_SEPARATOR.$_ENV['wbai']); if(!is_dir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR, 0777); } if(!is_dir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR."failed".DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR, 0777); } if(!empty($_ENV['folderLoaded'])) { for($fo=0;$foread())!=false) { if($fileScanmove=='.'||$fileScanmove=='..') { continue; } else { $filesToMove++; } } if($filesToMove == 0) { rmdir($directory.$_ENV['folderLoaded'][$vo]); } } } /** * Protects string to insert in the database * * @param $string string String to format * @return Formated date */ function protectStringDb($string) { if($_ENV['databasetype'] == "SQLSERVER") { $string = str_replace("'", "''", $string); $string = str_replace("\\", "", $string); } elseif($_ENV['databasetype'] == "ORACLE") { $string = str_replace("'", "''", $string); $string = str_replace("\\", "", $string); } elseif($_ENV['databasetype'] == "MYSQL" || $_ENV['databasetype'] == "POSTGRESQL") { $string = addslashes($string); $string = str_replace("\\", "#", $string); } return $string; } /** * Move batch load in the database into a backup directory * @param $directory string path of the file to backup */ function moveImportedFiles($directory) { $classScan= dir($directory); if($_ENV['backupBatch'] == "true") { if(!is_dir($directory.DIRECTORY_SEPARATOR."backup".DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR."backup".DIRECTORY_SEPARATOR, 0777); } if(!is_dir($directory.DIRECTORY_SEPARATOR."backup".DIRECTORY_SEPARATOR.date("Y").date("m").date("d").DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR."backup".DIRECTORY_SEPARATOR.date("Y").date("m").date("d").DIRECTORY_SEPARATOR, 0777); } if(!is_dir($directory.DIRECTORY_SEPARATOR."backup".DIRECTORY_SEPARATOR.date("Y").date("m").date("d").DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR)) { mkdir($directory.DIRECTORY_SEPARATOR."backup".DIRECTORY_SEPARATOR.date("Y").date("m").date("d").DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR, 0777); } //Create the subfolders if subfolders exists in the scan folder if(!empty($_ENV['folderLoaded'])) { for($fo=0;$foread())!=false) { if($fileScanmove=='.'||$fileScanmove=='..') { continue; } else { $filesToMove++; } } if($filesToMove == 0) { rmdir($directory.$_ENV['folderLoaded'][$vo]); } } } /** * Return physical location of the docserver * @param $docServerName string name of tjhe docserver */ function getDocSeverLocation($docServerName) { $reqGdsl = "select path_template from docservers where docserver_id ='".$docServerName."'"; $_ENV['db']->query($reqGdsl); while($reqResult=$_ENV['db']->fetch_array()) { $docServerLocation = $reqResult[0]; } return $docServerLocation; } /** * Update the database with the new batch AutoImport id * @param $workBatchAutoImport long name of tjhe docserver */ function updateWorkingBatchAutoImport($workBatchAutoImport) { $req_Uwbai = "update parameters set param_value_int = '".$workBatchAutoImport."' where id = 'work_batch_autoimport_id'"; $_ENV['db']->query($req_Uwbai); } /** * Allows to know if there is an image associate with the current xml * @param $file string path of the file * @param $xml string * @param $extension string extension of the file */ function searchImageOfXml($file, $xml, $extension) { $searchedFilename = str_replace($xml, $extension, $file); if($searchedFilename == $file) { $searchedFilename = str_replace(strtolower($xml), strtolower($extension), $file); } if(file_exists($searchedFilename) == true) { return true; } else { return false; /*$searchedFilename = str_replace(strtoupper($xml), strtoupper($extension), $file); if(file_exists($searchedFilename) == true) { return true; } else { return false; }*/ } } /** * Return the file's extention of a file * @param $sFullPath string path of the file */ function extractFileExt($sFullPath) { $sName = $sFullPath; if(strpos($sName, ".")==0) { $extractFileExt = ""; } else { $extractFileExt = explode(".", $sName); } return $extractFileExt[count($extractFileExt) - 1]; } /** * Return only name of the file path * @param $sFullPath string path of the file */ function extractFileName($sFullPath) { //echo "chemin :".$sFullPath."\n slh : ".DIRECTORY_SEPARATOR."\n position : ".strrpos($sFullPath, DIRECTORY_SEPARATOR)."\n file : ".substr($sFullPath, strrpos($sFullPath, DIRECTORY_SEPARATOR) +1)."\n"; if($_ENV['osName'] == "UNIX") { if(!preg_match('@'.DIRECTORY_SEPARATOR.'@', $sFullPath)) { return $sFullPath; } else { return substr($sFullPath, strrpos($sFullPath, DIRECTORY_SEPARATOR) +1); } } else { if(!preg_match("/\\".DIRECTORY_SEPARATOR."/", $sFullPath)) { return $sFullPath; } else { return substr($sFullPath, strrpos($sFullPath, DIRECTORY_SEPARATOR) +1); } } } /** * Return the actual size of the DocServer * @param $docServerId string id of the docserver */ function docServerSizeinit($docServerId) { $req = "SELECT size_limit_number, actual_size_number FROM docservers WHERE docserver_id = '".$docServerId."'"; $_ENV['db']->query($req); while($result = $_ENV['db']->fetch_array()) { $_ENV['sizeLimitConfig'] = $result[0]; $actualDocserverSize = $result[1]; $availableSpace = $result[0] - $result[1]; } return $availableSpace; } /** * Update the actual size of the DocServer * @param $docServerId string id of the docserver * @param $theSize long size of the docserver */ function docserverSizeUpdate($docServerId, $theSize) { $req1 = "Select actual_size_number from docservers where docserver_id='".$docServerId."'"; $_ENV['db']->query($req1); while($result=$_ENV['db']->fetch_array()) { $actualSize = $result[0]; $size = ($actualSize + $theSize); $req2 = "update docservers SET actual_size_number = '".$size."' where docserver_id = '".$docServerId."'"; $_ENV['db2']->query($req2); } } /** * Return the fingerprint mode of the DocServer * @param $docServerId string id of the docserver */ function docserverFingerprintMode($docServerId) { $req = "SELECT fingerprint_mode FROM docserver_types WHERE docserver_type_id in (SELECT docserver_type_id FROM docservers WHERE docserver_id = '".$docServerId."')"; $_ENV['db']->query($req); while($result = $_ENV['db']->fetch_array()) { $_ENV['docserverFingerprintMode'] = $result[0]; } } function doFingerprint($path, $fingerprintMode) { if ($fingerprintMode == "NONE" || $fingerprintMode == "") { return '0'; } else { return hash_file(strtolower($fingerprintMode), $path); } } /** * Update the actual size of the DocServer * @param $docServerId string id of the docserver * @param $theSize long size of the docserver */ function checkingAvailableSpace($availableSpace, $ActualTransferSize) { if($_ENV['sizeLimitConfig'] <> 0) { if(($availableSpace - $ActualTransferSize) > 0) { //echo "\nThe free space available in the docserver is sufficient to continue..."; } else { echo "\nThe free space available in the docserver is not sufficient to continue..."; manageError("The free space available in the docserver is not sufficient to continue. End of application.", 2); writeLog("Available space in bytes : ".$availableSpace); writeLog("Data size : ".$ActualTransferSize); } } } /** * Copy the file in the DoServer * @param $docServer string path of the docserver * @param $fileName string name of the file * @param $filePath string path of the file * @param $nbFiles long */ function buildDirectory($docServer, $fileName, $filePath, $nbFiles) { if(file_exists($docServer)) { $docName = str_pad($nbFiles, 4, "0", STR_PAD_LEFT); if($filePath <> "" && $fileName <> "") { $extTab = explode(".",$fileName); if(!copy($filePath.$fileName, $docServer.$docName.".".$extTab[count($extTab) - 1])) { manageError("Copy of a file failed during treatment of it : put the file in failed directory ".$filePath.$fileName, 3, $filePath.$fileName); } writeLog("New file created : ".$docServer.$docName.".".$extTab[count($extTab) - 1]); return $docServer.$docName.".".$extTab[count($extTab) - 1]; } } } /** * Compares date format in XML between mapping file * @param $docServer string path of the docserver * @param $fileName string name of the file * @param $filePath string path of the file * @param $nbFiles long */ function checkFormatDate($value, $format) { if($value <> "") { if(!preg_match('/YYYY/', $format)) { if(!preg_match('/YY/', $format)) { echo "\nError DATE_FORMAT tag : wrong description of the year"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file(year) : put all the loading batch in failed directory and stop application", 9); } } if(!preg_match('/MM/', $format)) { echo "\nError DATE_FORMAT tag : wrong description of the month"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file(month) : put all the loading batch in failed directory and stop application", 9); } if(!preg_match('/DD/', $format)) { echo "\nError DATE_FORMAT tag : wrong description of the day"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file(day) : put all the loading batch in failed directory and stop application", 9); } if((preg_match('/H/', $format) && !preg_match('/HH/', $format)) || (preg_match('/HHH/', $format))) // if One H or more than HH { echo "\nError DATE_FORMAT tag : wrong description of the hour"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (hour) : put all the loading batch in failed directory and stop application", 9); } if((preg_match('/I/', $format) && !preg_match('/II/', $format)) || (preg_match('/III/', $format))) // if One I or more than two I { echo "\nError DATE_FORMAT tag : wrong description of the minutes"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (minutes) : put all the loading batch in failed directory and stop application", 9); } if((preg_match('/S/', $format) && !preg_match('/SS/', $format)) || (preg_match('/SSS/', $format))) // if One S or more than two S { echo "\nError DATE_FORMAT tag : wrong description of the seconds"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (seconds) : put all the loading batch in failed directory and stop application", 9); } //Check value $tmp = $format; $tmp = preg_replace('/YY/', '[0-9][0-9]', $tmp); $tmp = preg_replace('/YY/', '[0-9][0-9]', $tmp); $tmp = preg_replace('/MM/', '[0-9][0-9]', $tmp); $tmp = preg_replace('/DD/', '[0-9][0-9]', $tmp); $tmp = preg_replace('/HH/', '[0-9][0-9]', $tmp); $tmp = preg_replace('/II/', '[0-9][0-9]', $tmp); $tmp = preg_replace('/SS/', '[0-9][0-9]', $tmp); if(!preg_match("#^".$tmp."$#", trim($value))) { echo "\n Error XML File : wrong date compared to the mapping file \n"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file : put all the loading batch in failed directory and stop application", 9); } else { return true; } } else { return true; } } /** * Convert datetime with a specific format * @param $value string date to convert * @param $format string format to convert * @param $valueDescArr string */ function dateTimeConvert($value, $format, $valueDescArr) { if($value <> "") { $year = ''; $month = ''; $day = ''; $hour = '00'; $minutes = '00'; $seconds = '00'; $year2 = ''; $year4 = ''; $tmp = (string) $value; for($i=$valueDescArr['year']['startPos'];$i<$valueDescArr['year']['endPos']+1;$i++) { $year .= $tmp[$i]; } // Microsoft excel 2001 rule : year with 2 digits > 30 = XX century and year < 30 = XXI century if(strlen($year) == 2) { $year2 = $year; if((integer) $year > 30) { $year4 = '19'.$year; } else { $year4 = '20'.$year; } } else { $year4 = $year; $year2 = $year[2].$year[3]; } $month = $tmp[$valueDescArr['month']['startPos']].$tmp[$valueDescArr['month']['endPos']]; $day = $tmp[$valueDescArr['day']['startPos']].$tmp[$valueDescArr['day']['endPos']]; if(isset($valueDescArr['hour']['startPos']) && isset($valueDescArr['hour']['endPos'])) { $hour = $tmp[$valueDescArr['hour']['startPos']].$tmp[$valueDescArr['hour']['endPos']]; } if(isset($valueDescArr['minutes']['startPos']) && isset($valueDescArr['minutes']['endPos'])) { $minutes = $tmp[$valueDescArr['minutes']['startPos']].$tmp[$valueDescArr['minutes']['endPos']]; } if(isset($valueDescArr['seconds']['startPos']) && isset($valueDescArr['seconds']['endPos'])) { $seconds = $tmp[$valueDescArr['seconds']['startPos']].$tmp[$valueDescArr['seconds']['endPos']]; } $res = (string) $format; if(preg_match('/YYYY/', $res)) { $res = preg_replace('/YYYY/', $year4, $res); } else { $res = preg_replace('/YY/', $year2, $res); } $res = preg_replace('/MM/', $month, $res); $res = preg_replace('/DD/', $day, $res); $res = preg_replace('/HH/', $hour, $res); $res = preg_replace('/II/', $minutes, $res); $res = preg_replace('/SS/', $seconds, $res); //echo "\n".$res."\n"; return $res; } else { return ""; } } /** * Write the SQL query in a csv file * @param $sqlImportFile string path to the file * @param $query string sql query * @param $autoImportDirectory string AutoImport directory */ function writeInSqlFile($sqlImportFile, $query, $SqlDirectory) { $sqlImportFileOpened = fopen($SqlDirectory.$sqlImportFile, "a"); fwrite($sqlImportFileOpened, $query); fclose($sqlImportFileOpened); } /** * Create the CSV for the massive import * @param $filePath string * @param $docServerName string * @param $fileDestination string * @param $tabMaarchValue string * @param $subDocServer string * @param $docServer string */ function buildSqlQuery($filePath, $docServerName, $fileDestination, $tabMaarchValue, $subDocServer, $docServer) { $onlyFilePath = explode(".",$filePath); $insertLineValue = ""; $sqlColumns = ""; $sqlValues = ""; $sqlInsertQuery = ""; $onlyFilePathTmp = ""; if(count($onlyFilePath) > 2) { for($cptExp=0;$cptExp < count($onlyFilePath) - 1;$cptExp++) { $onlyFilePathTmp = $onlyFilePathTmp.".".$onlyFilePath[$cptExp]; } $onlyFilePathTmp = substr($onlyFilePathTmp, 1, strlen($onlyFilePathTmp)); if(file_exists($onlyFilePathTmp.".xml")) { $xmlFile = ($onlyFilePathTmp.".xml"); } else { $xmlFile = ($onlyFilePathTmp.".XML"); } } else { if(file_exists($onlyFilePath[0].".xml")) { $xmlFile = ($onlyFilePath[0].".xml"); } else { $xmlFile = ($onlyFilePath[0].".XML"); } //$xmlFile = ($onlyFilePath[0].".xml"); } echo "\n\n".$xmlFile."\n\n"; if($_ENV['withoutXml'] == "false") { $xmlFileLoaded = simplexml_load_file($xmlFile); } if($_ENV['insertMode'] == "true") { //typist $sqlValues = "'AUTOIMPORT', "; //creation_date if($_ENV['databasetype'] == 'ORACLE') { $sqlValues .= "Date(sysdate), "; } elseif($_ENV['databasetype'] == 'SQLSERVER') { $sqlValues .= "'".date("Ymd")." ".date("H:i:s").".000', "; } else { $sqlValues .= "'".date("Y-m-d")." ".date("H:i:s")."', "; } //docserver_id $sqlValues .= "'".$docServerName."', "; //path $tmp = str_replace("/", "#", $subDocServer); $tmp = str_replace("\\", "#", $tmp); $sqlValues .= "'".$tmp."', "; $tmp = str_replace("#", DIRECTORY_SEPARATOR, $tmp); //filename $sqlValues .= "'".str_replace($docServer.$tmp, "",$fileDestination)."', "; //offset_doc = '' $sqlValues .= "'', "; //fingerprint $sqlValues .= "'".doFingerprint($filePath, $_ENV['docserverFingerprintMode'])."', "; //filesize $sqlValues .= "".filesize($filePath).", "; //format $format = extractFileExt($filePath); $sqlValues .= "'".$format."', "; //workbatch $sqlValues .= "'".$_ENV['wbai']."', "; } else { $tabMaarchValueBis = $tabMaarchValue; $j=0; //typist $insertLineValue .= "AUTOIMPORT"; //creation_date if($_ENV['databasetype'] == 'ORACLE') { //$insertLineValue .= ";".date("d/m/Y")." ".date("H:i:s"); } else if($_ENV['databasetype'] == 'SQLSERVER') { $insertLineValue .= ";".date("Ymd")." ".date("H:i:s").".000"; } else { $insertLineValue .= ";".date("Y-m-d")." ".date("H:i:s"); } //docserver_id $insertLineValue .= ";".$docServerName; //path $tmp = str_replace("/", "#", $subDocServer); $tmp = str_replace("\\", "#", $tmp); $insertLineValue .= ";".$tmp; $tmp = str_replace("#", DIRECTORY_SEPARATOR, $tmp); //filename $insertLineValue .= ";".str_replace($docServer.$tmp, "",$fileDestination); //offset_doc = '' $insertLineValue .= ";"; //fingerprint $insertLineValue .= ";".doFingerprint($filePath, $_ENV['docserverFingerprintMode']); //filesize $insertLineValue .= ";".filesize($filePath); //format $format = extractFileExt($filePath); $insertLineValue .= ";".$format; //workbatch $insertLineValue .= ";".$_ENV['wbai']; } //write the xml value in the good sql file to import $xmlValueContent = array(); for($j=1;$j<=sizeof($tabMaarchValue);$j++) { $var = 0; if(isset($_ENV['tabMaarchDefaultValue'][$j])) { if($_ENV['insertMode'] == "true") { if($_ENV['tabMaarchInt'][$j] == true) { $sqlValues .= "".$_ENV['tabMaarchDefaultValue'][$j].", "; } else { $sqlValues .= "'".$_ENV['tabMaarchDefaultValue'][$j]."', "; } $xmlValueContent[$j] = ""; //echo $_ENV['tabMaarchField'][$j]." : ".$_ENV['tabMaarchDefaultValue'][$j]."\r\n"; } else { $xmlValueContent[$j] = $_ENV['tabMaarchDefaultValue'][$j]; } } elseif($_ENV['tabMaarchDate'][$j] == true && count($_ENV['tabFormatDate']) > 1) { if(!checkFormatDate($xmlFileLoaded->$tabMaarchValue[$j], $_ENV['tabFormatDate'][$j]['dateFormat'])) { //echo "ERROR XML format (DATE_FORMAT tag) ! End of Application"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file : put all the loading batch in failed directory and stop application", 9); } if($_ENV['insertMode'] == "true") { $sqlValues .= "'".dateTimeConvert($xmlFileLoaded->$tabMaarchValue[$j], $_ENV['dateTimeFormat'], $_ENV['tabFormatDate'][$j])."', "; $xmlValueContent[$j] = ""; //echo $_ENV['tabMaarchField'][$j]." : ".dateTimeConvert($xmlFileLoaded->$tabMaarchValue[$j], $_ENV['dateTimeFormat'], $_ENV['tabFormatDate'][$j])."\r\n"; } else { $xmlValueContent[$j] = dateTimeConvert($xmlFileLoaded->$tabMaarchValue[$j], $_ENV['dateTimeFormat'], $_ENV['tabFormatDate'][$j]); } } elseif($_ENV['tabMaarchString'][$j] == true) { if($_ENV['insertMode'] == "true") { $sqlValues .= "'".protectStringDb($xmlFileLoaded -> $tabMaarchValue[$j])."', "; $xmlValueContent[$j] = ""; //echo $_ENV['tabMaarchField'][$j]." : ".protectStringDb($xmlFileLoaded -> $tabMaarchValue[$j])."\r\n"; } else { $xmlValueContent[$j] = protectStringDb($xmlFileLoaded -> $tabMaarchValue[$j]); } } elseif($_ENV['tabMaarchInt'][$j] == true) { if($_ENV['insertMode'] == "true") { if($xmlFileLoaded -> $tabMaarchValue[$j] == "") { $xmlFileLoaded -> $tabMaarchValue[$j] = 0; } $sqlValues .= "".$xmlFileLoaded -> $tabMaarchValue[$j].", "; $xmlValueContent[$j] = ""; //echo $_ENV['tabMaarchField'][$j]." : ".$xmlFileLoaded -> $tabMaarchValue[$j]."\r\n"; } else { $xmlValueContent[$j] = $xmlFileLoaded -> $tabMaarchValue[$j]; } } else { if($_ENV['insertMode'] == "true") { if($xmlFileLoaded -> $tabMaarchValue[$j] == "") { $xmlFileLoaded -> $tabMaarchValue[$j] = 0; } $sqlValues .= "".$xmlFileLoaded -> $tabMaarchValue[$j].", "; $xmlValueContent[$j] = ""; //echo $_ENV['tabMaarchField'][$j]." : ".$xmlFileLoaded -> $tabMaarchValue[$j]."\r\n"; } else { $xmlValueContent[$j] = $xmlFileLoaded -> $tabMaarchValue[$j]; } } if($xmlValueContent[$j] == "") { if($_ENV['insertMode'] == "true") { //$sqlValues .= "'".$_ENV['tabMaarchDefaultIfTagEmpty'][$j]."', "; } else { $xmlValueContent[$j] = $_ENV['tabMaarchDefaultIfTagEmpty'][$j]; } } $insertLineValue .= ";".str_replace("'", "", $xmlValueContent[$j]); } $sqlValues = substr($sqlValues, 0, -2); if($_ENV['insertMode'] == "true") { if($_ENV['databasetype'] == "ORACLE") { $sqlColumns = 'typist, creation_date sysdate, docserver_id, path, filename, offset_doc, fingerprint, filesize, format, work_batch, '; } else { $sqlColumns = "typist, creation_date, docserver_id, path, filename, offset_doc, fingerprint, filesize, format, work_batch, "; } $j = 0; for($j=1;$j<=sizeof($_ENV['tabMaarchField']);$j++) { if(count($_ENV['tabFormatDate'][$j]) > 0 && $_ENV['databasetype'] == "ORACLE") { $sqlColumns .= " ".$_ENV['tabMaarchField'][$j].= " date \"".$_ENV['dateTimeFormat']."\", "; } else { $sqlColumns .= " ".$_ENV['tabMaarchField'][$j].", "; } } $sqlColumns = substr($sqlColumns, 0, -2); $sqlInsertQuery = "insert into ".$_ENV['tableName']." (".$sqlColumns.") values(".$sqlValues.")"; $_ENV['db']->query($sqlInsertQuery); echo $sqlInsertQuery; } $insertLineValue .= "\r\n"; return $insertLineValue; } /** * Slice a date to a format * @param $dateFormat string format of the date */ function sliceDateFormat($dateFormat) { $arr = array(); if(empty($dateFormat) || !isset($dateFormat)) { echo "Empty DATE_FORMAT tag in the mapping file ! End of Application"; manageError("Error DATE_FORMAT tag : Empty DATE_FORMAT tag in the mapping file : Please modify your mapping file, put all the loading batch in failed directory and stop application", 9); } $arr['dateFormat'] = $dateFormat; //Year check $arr['year']['startPos'] = strpos($dateFormat,'Y'); $arr['year']['endPos'] = strrpos($dateFormat,'Y'); if(($arr['year']['startPos'] == FALSE && $arr['year']['startPos'] <> 0) || $arr['year']['endPos'] == FALSE) { echo "Error of the DATE_FORMAT tag in the mapping file (YEAR)! End of Application"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (year) : put all the loading batch in failed directory and stop application", 9); } //Month check $arr['month']['startPos'] = strpos($dateFormat,'M'); $arr['month']['endPos'] = strrpos($dateFormat,'M'); if(($arr['month']['startPos'] == FALSE && $arr['month']['startPos'] <> 0) || $arr['month']['endPos'] == FALSE) { echo "Error of the DATE_FORMAT tag in the mapping file (MONTH)! End of Application"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (month) : put all the loading batch in failed directory and stop application", 9); } //Day check $arr['day']['startPos'] = strpos($dateFormat,'D'); $arr['day']['endPos'] = strrpos($dateFormat,'D'); if(($arr['day']['startPos'] == FALSE && $arr['day']['startPos'] <> 0)|| $arr['day']['endPos'] == FALSE) { echo "Error of the DATE_FORMAT tag in the mapping file (DAY)! End of Application"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (day) : put all the loading batch in failed directory and stop application", 9); } if(preg_match('/H/', $dateFormat)) { $arr['hour']['startPos'] = strpos($dateFormat,'H'); $arr['hour']['endPos'] = strrpos($dateFormat,'H'); if(($arr['hour']['startPos'] == FALSE && $arr['hour']['startPos'] <> 0) || $arr['hour']['endPos'] == FALSE) { echo "Error of the DATE_FORMAT tag in the mapping file (HOUR)! End of Application"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (hour) : put all the loading batch in failed directory and stop application", 9); } } if(preg_match('/I/', $dateFormat)) { $arr['minutes']['startPos'] = strpos($dateFormat,'I'); $arr['minutes']['endPos'] = strrpos($dateFormat,'I'); if(( $arr['minutes']['startPos'] == FALSE && $arr['minutes']['startPos'] <> 0)|| $arr['minutes']['endPos'] == FALSE) { echo "Error of the DATE_FORMAT tag in the mapping file (MINUTES)! End of Application"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (minutes) : put all the loading batch in failed directory and stop application", 9); } } if(preg_match('/S/', $dateFormat)) { $arr['seconds']['startPos'] = strpos($dateFormat,'S'); $arr['seconds']['endPos'] = strrpos($dateFormat,'S'); if( ($arr['seconds']['startPos'] == FALSE && $arr['seconds']['startPos'] <> 0) || $arr['seconds']['endPos'] == FALSE) { echo "Error of the DATE_FORMAT tag in the mapping file (SECONDS)! End of Application"; manageError("Error DATE_FORMAT tag : wrong description of the date in the mapping file (seconds) : put all the loading batch in failed directory and stop application", 9); } } return $arr; } function createPathOnDocServer($docServer) { if(!is_dir($docServer.date("Y").DIRECTORY_SEPARATOR)) { mkdir($docServer.date("Y").DIRECTORY_SEPARATOR, 0777); } if(!is_dir($docServer.date("Y").DIRECTORY_SEPARATOR.date("m").DIRECTORY_SEPARATOR)) { mkdir($docServer.date("Y").DIRECTORY_SEPARATOR.date("m").DIRECTORY_SEPARATOR, 0777); } if(!is_dir($docServer.date("Y").DIRECTORY_SEPARATOR.date("m").DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR)) { $folder_wbai = mkdir($docServer.date("Y").DIRECTORY_SEPARATOR.date("m").DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR, 0777); } else { echo "\Folder Already exist... Software stopped!"; manageError("Workbatch already exist! Unable to send file in the Docserver. End of application.", 11); } } //begin date_default_timezone_set('Europe/Paris'); if($argc < 2) { echo "You must specify the configuration file."; exit; } else { $conf = $argv[1]; if(!file_exists($conf)) { echo "The read of the configuration file has been stopped by Maarch Auto Import (Xml Error)."; exit; } //loading of the xml config file. $xmlConfig = simplexml_load_file($conf); $CONFIG =$xmlConfig -> CONFIG; $mappingFile = (string)$CONFIG -> MAPPING_FILE; $scanImportDirectory = (string)$CONFIG -> SCAN_IMPORT_DIRECTORY; $_ENV['sid']= (string)$scanImportDirectory; $_ENV['configName'] = (string) $CONFIG -> CONFIG_NAME; $_ENV['databaseserver'] = (string) $CONFIG->LOCATION; $_ENV['databaseport'] = (string) $CONFIG->DATABASE_PORT; $_ENV['database'] = (string) $CONFIG ->DATABASE; $_ENV['databasetype'] = (string) $CONFIG->DATABASETYPE; $_ENV['databaseuser'] = (string) $CONFIG -> USER_NAME; $_ENV['databasepwd'] = (string) $CONFIG->PASSWORD; $_ENV['databaseworkspace'] = (string) $CONFIG->DATABASEWORKSPACE; $tabTableMaarch[1] = (string) $CONFIG -> TABLE_NAME; $_ENV['insertMode'] = (string)$CONFIG ->INSERT_MODE; $_ENV['tableName'] =(string) $tabTableMaarch[1]; $docServerName =(string) $CONFIG -> DOCSERVER_NAME; $_ENV['dateTimeFormat'] = $CONFIG -> DATE_TIME_FORMAT; $autoImportDirectory = $CONFIG -> AUTO_IMPORT_DIRECTORY; $_ENV['withoutXml'] = $CONFIG -> WITHOUT_XML; $_ENV['backupBatch'] = $CONFIG -> BACKUP_BATCH; $_ENV['excludeExistingDocs'] = $CONFIG -> EXCLUDE_EXISTING_DOCS; $_ENV['excludeExistingDocsFolder'] = $CONFIG -> EXCLUDE_EXISTING_DOCS_FOLDER; $_ENV['controlCompleteFiles'] = $CONFIG -> CONTROL_COMPLETE_FILES; $_ENV['aid'] = $autoImportDirectory; //look if there is files to process //print_r($_ENV);exit(); if(isset($argv[2])) { $path_sql = $argv[2]; } else { $path_sql = $autoImportDirectory; } if(scanIncoming($scanImportDirectory)) { if($_ENV['controlCompleteFiles'] == "true" && !isCompleteBatch($scanImportDirectory)) { echo "batch not complete"; exit(0); } loginCreation(); writeLog("Loading xml config file : ".$conf); writeLog("Database type : ".$_ENV['databasetype']); if($_ENV['insertMode'] == "true") { writeLog("Insert SQL Mode"); } if(DIRECTORY_SEPARATOR == "/") { $_ENV['osName'] = "UNIX"; } else { $_ENV['osName'] = "WINDOWS"; } /*if(file_exists($autoImportDirectory.$_ENV['configName'].".lck") || file_exists($autoImportDirectory.$_ENV['configName']."_error.lck")) { echo "An error has been found while the last batch. AutoImport stopped!\r\n Please check you configuration file\r\n. You have to delete the lock file in AutoImport folder to restart this application"; writeLog("An error has been found while the last batch. AutoImport stopped!Please check you configuration file.You have to delete the lock file in AutoImport folder to restart this application"); exit; }*/ $lockFile = $_ENV['aid'].$_ENV['configName'].".lck"; $lockFileOpened = fopen($lockFile, "a"); fwrite($lockFileOpened, 1); fclose($lockFileOpened); require_once("class_db.php"); $_ENV['db'] = new dbquery(); $_ENV['db']->connect(); $_ENV['db2'] = new dbquery(); $_ENV['db2']->connect(); //Return location of docserver $docServer = getDocSeverLocation($docServerName); docserverFingerprintMode($docServerName); echo "\n".$docServer ."\n"; //The if docserver exists if(!is_dir($docServer)) { manageError("Path of the docserver not exists", 12); } //Caculing the work batch autoimport number $wbai = getWorkBatchAutoImport(); $_ENV['wbai'] = $wbai; $wbai = str_pad($_ENV['wbai'],8,"0", STR_PAD_LEFT); writeLog("AutoImport WorkBatch is : ".$_ENV['wbai']); //Updating the work batch autoimport number updateWorkingBatchAutoImport($_ENV['wbai']); //Calcul of the init size avaible on docserver $availableSpace = docServerSizeinit($docServerName); writeLog("Target OS : ".$_ENV['osName']); //Loading the mapping xml file writeLog("Loading mapping xml file"); $xmlmapping = simplexml_load_file($mappingFile); $iMap = 1; //Loading MAPPING_FILE tag $MAPPING_FILE = $xmlmapping->MAPPING_FILE; foreach($MAPPING_FILE->ELEMENT as $ELEMENT) { $tabMaarchField[$iMap] = $ELEMENT->COLUMN; if(strstr($ELEMENT->VALUE, "'") <> false) { $_ENV['tabMaarchDefaultValue'][$iMap] = str_replace("'", "", $ELEMENT->VALUE); $tabMaarchValue[$iMap] = "DEFAULT_VALUE_MAARCH"; } else { $tabMaarchValue[$iMap] = $ELEMENT->VALUE; } //if the value tag is empty $_ENV['tabMaarchDefaultIfTagEmpty'][$iMap] = $ELEMENT->DEFAULT_IF_TAG_EMPTY; $_ENV['tabMaarchDate'][$iMap] = false; $_ENV['tabMaarchString'][$iMap] = false; $_ENV['tabMaarchInt'][$iMap] = false; $_ENV['tabFormatDate'][$iMap] = array(); if($ELEMENT->FORMAT) { if($ELEMENT->FORMAT == "DATE" || $ELEMENT->FORMAT == "date" ) { $_ENV['tabMaarchDate'][$iMap] = true; if(!isset($ELEMENT->DATE_FORMAT) || empty($ELEMENT->DATE_FORMAT)) { echo "A DATE_FORMAT tag is missing in the mapping file! End of Application"; manageError("A DATE_FORMAT tag is missing in the mapping file! Please modify your mapping file", 9); } else { $_ENV['tabFormatDate'][$iMap] = sliceDateFormat($ELEMENT->DATE_FORMAT); } } elseif($ELEMENT->FORMAT == "STRING" || $ELEMENT->FORMAT == "string" ) { $_ENV['tabMaarchString'][$iMap] = true; } elseif($ELEMENT->FORMAT == "INTEGER" || $ELEMENT->FORMAT == "integer" ) { $_ENV['tabMaarchInt'][$iMap] = true; } } $iMap++; } $_ENV['tabMaarchField'] = $tabMaarchField; //Scan the import directory $folder = $scanImportDirectory; $folder2 = $docServer; writeLog("Find current directory and file of the docserver"); //creation of the current directory of the docserver to use $nbfile = 0; $nbfolder = 0; $initFile = 0; $subDocServer = date("Y").DIRECTORY_SEPARATOR.date("m").DIRECTORY_SEPARATOR.$_ENV['wbai'].DIRECTORY_SEPARATOR; $initDirectory = $docServer.$subDocServer; createPathOnDocServer($docServer); writeLog("Scanning import folder"); writeLog("Current target docserver location for import : ".$initDirectory); $request = scan($folder, $availableSpace, $initDirectory, $docServerName, $tabMaarchValue, $subDocServer, $docServer, $initFile); if($request <> "") { $theSize = $_ENV['currentTransfertSize']; docserverSizeUpdate($docServerName, $theSize); //Create the file with the SQL query for($z=1;$z==1;$z++) { $tabSqlFile[$z] = "sql_import_file_".$_ENV['configName']."_".$tabTableMaarch[$z].".sql"; //delete existant import sql file if(file_exists($path_sql.$tabSqlFile[$z])) { unlink($path_sql.$tabSqlFile[$z]); } writeLog("Creation of SQL command file in : ".$tabSqlFile[$z]); writeInSqlFile($tabSqlFile[$z], $request, $path_sql); } if($_ENV['databasetype'] == "ORACLE") { $maarchField = 'typist, creation_date sysdate, docserver_id, path, filename, offset_doc, fingerprint, filesize, format, work_batch '; } else { $maarchField = "typist, creation_date, docserver_id, path, filename, offset_doc, fingerprint, filesize, format, work_batch "; } if(sizeof($tabMaarchField) <> 0) { $maarchField .= ","; } for($j=1;$j<=sizeof($tabMaarchField);$j++) { if(count($_ENV['tabFormatDate'][$j]) > 0 && $_ENV['databasetype'] == "ORACLE") { $maarchField .= " ".$tabMaarchField[$j].= " date \"".$_ENV['dateTimeFormat']."\","; } else { $maarchField .= " ".$tabMaarchField[$j].","; } } $maarchField = substr($maarchField, 0, -1); if($_ENV['insertMode'] == "false") { for($z=1;$z==1;$z++) { $tabSqlFile[$z] = str_replace(DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,$tabSqlFile[$z]); if($_ENV['databasetype'] == "MYSQL") { $req = "LOAD DATA LOCAL INFILE '".$path_sql.$tabSqlFile[$z]."' INTO TABLE ".$tabTableMaarch[$z]." FIELDS TERMINATED BY ';' LINES TERMINATED BY '\\n' (".$maarchField.");"; } elseif($_ENV['databasetype'] == "POSTGRESQL") { $tabSqlFile[$z] = str_replace(DIRECTORY_SEPARATOR,DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR,$tabSqlFile[$z]); $req = "COPY ".$tabTableMaarch[$z]." (".$maarchField.") FROM '".$path_sql.$tabSqlFile[$z]."' WITH DELIMITER AS ';';"; } elseif($_ENV['databasetype'] == "SQLSERVER") { $req = "bulk insert ".$_ENV['database'].".dbo.".$tabTableMaarch[1]." from '".$path_sql.$tabSqlFile[$z]."' with( DATAFILETYPE = 'widechar', formatfile='".$autoImportDirectory."maarch_autoimport".DIRECTORY_SEPARATOR.$tabTableMaarch[1].".xml' );"; } if($_ENV['databasetype'] == "ORACLE") { $req = "LOAD DATA INFILE '".$path_sql.$tabSqlFile[$z]."' APPEND INTO TABLE ".$tabTableMaarch[$z]." FIELDS TERMINATED BY ';' (".$maarchField.")"; //echo $req."\r\n"; $Fnm = $_ENV['configName']."_control.txt"; $inF = fopen($Fnm,"w"); fwrite($inF,$req); fclose($inF); if(isset($_ENV['osName']) && $_ENV['osName'] == 'UNIX') { $oracleQueryResult = exec("sqlldr userid=".$_ENV['databaseuser']."/".$_ENV['databasepwd']." control=".$_ENV['configName']."_control.txt log=log/".$_ENV['configName']."_log_oracle.txt parallel=true bad=log/".$_ENV['configName']."_bad_oracle.txt discard=log/".$_ENV['configName']."_discard_oracle.txt errors=0", $returnTab, $oracleReturn); } else { $oracleQueryResult = exec("sqlldr ".$_ENV['databaseuser']."/".$_ENV['databasepwd']."@".$_ENV['database']." control=".$_ENV['configName']."_control.txt log=log/".$_ENV['configName']."_log_oracle.txt parallel=true bad=log/".$_ENV['configName']."_bad_oracle.txt discard=log/".$_ENV['configName']."_discard_oracle.txt errors=0", $returnTab, $oracleReturn); } //print_r($oracleReturn); } writeLog($req); if($_ENV['databasetype'] <> "ORACLE") { //Execute the SQL query. $reqExecute = $_ENV['db']->query($req, true); if(!$reqExecute) { echo "\n".$_ENV['databasetype']." Error!: ".$_ENV['db']->error(true); manageError("Maarch Auto Import cannot execute query on database".$_ENV['db']->error(true), 4); } } elseif($oracleReturn <> 0) { manageError("Maarch Auto Import cannot execute query on database".$_ENV['db']->error(true), 4); } } } writeLog("Import files moved to backup of ".$_ENV['wbai']); moveImportedFiles($folder); } writeLog("End of application"); if(file_exists($lockFile)) { unlink($lockFile); } logInDataBase($_ENV['totalNbfile'], $_ENV['nbErrors'], $_ENV['infoErrors']); exit($_ENV['errorLevel']); } else { echo "no file to process"; exit(0); } } ?>