. */ /** * @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, * 13 : Could not load plugin, * 14 : Fingerprint control failed between incoming and docserver. * In case of a known error #2, #4, #7, #8, #9, #10, #11, #12, #14 all the * loading batch is moved in failed directory. * @file * @author Loïc Vinet * @author Laurent Giovanonni * @author Claire Figueras * @author Bruno Carlin * @author Gael de Villeblanche * @date $date$ * @version $Revision$ * @ingroup dispatch * @brief Massive importation of files in Maarch DataBase */ //require_once 'Maarch/AutoImport/PluginManager.php'; //require_once 'tools/log4php/Logger.php'; //Error mode and function error_reporting(E_ERROR); set_error_handler(Maarch_AutoImport_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"; /** * Exclude all existing documents folder */ $_ENV['excludeFolder'] = "false"; /** * Controle complete files mode */ $_ENV['controlCompleteFiles'] = "false"; /** * fingerprint mode of the docserver */ $_ENV['docserverFingerprintMode'] = "NONE"; /** * Folders management */ //Fields of the folders mapping file $_ENV['tabMaarchFoldersDefaultValue'] = array(); //Available folder fields (can be viewed in debug mode) $_ENV['tabMaarchFoldersField'] = array(); $_ENV['createFolders'] = "false"; //Create folders? $_ENV['foldersTableName'] = ""; //Folders table name in database $_ENV['totalNbfolders'] = 0; //Number of folders created /** * * write a log entry with a specific log level * @param object $logger Log4php logger * @param string $logLine Line we want to trace * @param enum $level Log level */ function Maarch_AutoImport_writeLog4php($logger, $logLine, $level) { $logLine = wd_remove_accents($logLine); switch ($level) { case 'DEBUG': $logger->debug($logLine); break; case 'INFO': $logger->info($logLine); break; case 'WARN': $logger->warn($logLine); break; case 'ERROR': $logger->error($logLine); break; case 'FATAL': $logger->fatal($logLine); break; } } /** * Creation of the log file */ function Maarch_AutoImport_loginCreation() { $logDirPath = $_ENV['aid'] . DIRECTORY_SEPARATOR . "log" . DIRECTORY_SEPARATOR; if (!is_dir($logDirPath)) { mkdir($logDirPath, 0777); } $_ENV['log'] = $logDirPath . $_ENV['configName'] . ".log"; Maarch_AutoImport_writeLog("Application start with : " . $_SERVER['SCRIPT_FILENAME'] , 'OK', 'INFO' ); } /** * Delete accents * * @param String $str * @param String $charset */ function wd_remove_accents($str, $charset ='utf-8') { $str = htmlentities($str, ENT_NOQUOTES, "utf-8"); $str = preg_replace( '#\&([A-za-z])(?:uml|circ|tilde|acute|grave|cedil|ring)\;#', '\1', $str ); $str = preg_replace('#\&([A-za-z]{2})(?:lig)\;#', '\1', $str); $str = preg_replace('#\&[^;]+\;#','', $str); return $str; } /** * Write on the log file * @param $eventInfo string text which is written in the log file */ function Maarch_AutoImport_writeLog($message, $result = 'OK', $level = 'INFO') { $remote_ip = '127.0.0.1'; Logger::configure($_ENV['logConfigPath']); $logger = Logger::getLogger($_ENV['logger']); $searchPatterns = array( '%ACCESS_METHOD%', '%RESULT%', '%CODE_METIER%', '%HOW%', '%WHAT%', '%REMOTE_IP%' ); $replacePatterns = array( 'Script', $result, $_ENV['appName'], 'ADD', $message, $remote_ip ); $logLine = str_replace($searchPatterns, $replacePatterns, $_ENV['logFormat']); Maarch_AutoImport_writeLog4php($logger, $logLine, $level); } /** * 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 Maarch_AutoImport_manageError($eventInfo, $errorLevel, $filePath="") { global $plugins; $logLine = $errorLevel.':'.$eventInfo; if ($errorLevel == 5 || $errorLevel == 6) { Maarch_AutoImport_writeLog($errorLevel.":".$eventInfo , 'KO', 'WARN'); } else { Maarch_AutoImport_writeLog($errorLevel.":".$eventInfo , 'KO', 'ERROR'); } if (in_array($errorLevel, array(2, 4, 7, 8, 9, 10, 11, 14))) { Maarch_AutoImport_createLockFile($errorLevel); Maarch_AutoImport_writeLog( "End of application, return code : " . $errorLevel , 'KO', 'ERROR' ); Maarch_AutoImport_logInDataBase(0, 1, $logLine . "
"); if (isset($plugins) && is_object($plugins) && get_class($plugins) === "Maarch_AutoImport_PluginManager") { $plugins->executeCallbacks('on_error'); } exit($errorLevel); // we put 12 and not 1 to not exit } elseif (in_array($errorLevel, array(1, 3, 5, 6, 12, 13))) { //put file in error, no exit if (file_exists($filePath)) { $_ENV['nbErrors']++; $_ENV['infoErrors'] .= $logLine . "
"; Maarch_AutoImport_putFileInError($filePath); return; } } else { Maarch_AutoImport_createLockFile($errorLevel); // it works with no quote Maarch_AutoImport_logInDataBase(0, 1, $logLine . "
"); // WARNING PHP Fatal error: // Call to a member function executeCallbacks() on a non-object // in /var/www/autoimport/maarch_autoimport/maarch_auto_import.php // on line 308 if (isset($plugins) && is_object($plugins) && get_class($plugins) === "Maarch_AutoImport_PluginManager") { $plugins->executeCallbacks('on_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 Maarch_AutoImport_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) { Maarch_AutoImport_manageError( "from line " . $errline . " : " . $errstr, 1 ); $_ENV['errorLevel'] = 1; } } /** * Insert in the database the report of AI * @param $workBatchAutoimport */ function Maarch_AutoImport_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', ?, CURRENT_TIMESTAMP, ?, ?, ?" . ")"; $_ENV['db']->query($req, array($_ENV['wbai'], $totalProcessed, $totalErrors, substr($info, 0, 999))); } /** * Control in DB if the document already exists with the fingerprint * @param $fingerprint */ function Maarch_AutoImport_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 = ?" . " and creation_date > to_date('" . $myDate . "','" . $_ENV['dateTimeFormat'] . "')"; } else { $req = "SELECT fingerprint FROM " . $_ENV['tableName'] . " WHERE fingerprint = ? and creation_date > '" . $myDate . "'"; } //echo $req."\r\n"; $stmt = $_ENV['db']->query($req, array($fingerprint)); while ($reqResult = $stmt->fetch(PDO::FETCH_ASSOC)) { if ($reqResult[0] <> "") { $flagExists = true; break; } } return $flagExists; } /** * Browse each file and folder in the import folder and return true * if the process has to continue * @param $folder path string of the incoming folder */ function Maarch_AutoImport_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'] || (is_dir($folder . DIRECTORY_SEPARATOR . $fileScan) && $_ENV['excludeFolder'] == 'true') || (is_file($folder . DIRECTORY_SEPARATOR . $fileScan) && strpos($fileScan, ".") === false) ) { 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 Maarch_AutoImport_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 { $subFileScanPath = $folder . $fileScan . DIRECTORY_SEPARATOR . $subFileScan; echo "control of " . $subFileScanPath . "\r\n"; $ofile = @fopen($subFileScanPath, "r"); if (Maarch_AutoImport_isCompleteFile($ofile)) { //continue fclose($ofile); } else { $complete = false; } } } } else { echo "control of " . $folder . $fileScan . "\r\n"; $ofile = @fopen($folder . $fileScan, "r"); if (Maarch_AutoImport_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 Maarch_AutoImport_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 Maarch_AutoImport_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 Maarch_AutoImport_scan( $folder, $availableSpace, $docServer, $docServerName, $tabMaarchValue, $subDocServer, $docServerMiniPath, $initFile ) { global $plugins; $currentTransfertSize = 0; $classScan = dir($folder); $pass = true; $tmpFileTri = array(); while (($fileScan = $classScan->read()) != false) { if (($initFile == 0) && ($pass == true)) { mkdir( $docServer . str_pad($initFile, 4, "0", STR_PAD_LEFT) . DIRECTORY_SEPARATOR, 0777 ); $pass = false; $nbFiles = 0; } if (strstr($nbFiles, "000") == true) { $initFile++; 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'] || (is_dir($folder . DIRECTORY_SEPARATOR . $fileScan) && $_ENV['excludeFolder'] == 'true') || (is_file($folder . DIRECTORY_SEPARATOR . $fileScan) && strpos($fileScan, ".") === false) ) { continue; } elseif (is_dir($folder . DIRECTORY_SEPARATOR . $fileScan)) { //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( Maarch_AutoImport_extractFileExt($subFileScan) ) <> "XML" ) { if ( !Maarch_AutoImport_controlExistingDocsInDb( Maarch_AutoImport_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 . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt( $subFileScan ), "", $subFileScan ) . "xml" ) ) ) { array_push( $tmpFileTri, $fileScan . DIRECTORY_SEPARATOR . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt( $subFileScan ), "", $subFileScan ) . "xml" ) ); array_push( $_ENV['fileNameLoaded'], $fileScan . DIRECTORY_SEPARATOR . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt( $subFileScan ), "", $subFileScan ) . "xml" ) ); } elseif ( file_exists( $folder . $fileScan . DIRECTORY_SEPARATOR . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt( $subFileScan ), "", $subFileScan ) . "XML" ) ) ) { array_push( $tmpFileTri, $fileScan . DIRECTORY_SEPARATOR . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt( $subFileScan ), "", $subFileScan ) . "XML" ) ); array_push( $_ENV['fileNameLoaded'], $fileScan . DIRECTORY_SEPARATOR . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt( $subFileScan ), "", $subFileScan ) . "XML" ) ); } } else { Maarch_AutoImport_writeLog("File already Exists in DB " . $folder . $fileScan . DIRECTORY_SEPARATOR . $subFileScan, 'KO', 'ERROR'); Maarch_AutoImport_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(Maarch_AutoImport_extractFileExt($fileScan)) <> "XML" ) { if ( !Maarch_AutoImport_controlExistingDocsInDb( Maarch_AutoImport_doFingerprint( $folder . DIRECTORY_SEPARATOR . $fileScan, $_ENV['docserverFingerprintMode'] ) ) ) { array_push($tmpFileTri, $fileScan); array_push($_ENV['fileNameLoaded'], $fileScan); if ( file_exists( $folder . DIRECTORY_SEPARATOR . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($fileScan), "", $fileScan ) . "xml" ) ) ) { array_push( $tmpFileTri, Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($fileScan), "", $fileScan ) . "xml" ) ); array_push( $_ENV['fileNameLoaded'], Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($fileScan), "", $fileScan ) . "xml" ) ); } elseif ( file_exists( $folder . DIRECTORY_SEPARATOR . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($fileScan), "", $fileScan ) . "XML" ) ) ) { array_push( $tmpFileTri, Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($fileScan), "", $fileScan ) . "XML" ) ); array_push( $_ENV['fileNameLoaded'], Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($fileScan), "", $fileScan ) . "XML" ) ); } } else { Maarch_AutoImport_writeLog("File already Exists in DB " . $folder . DIRECTORY_SEPARATOR . $fileScan , 'KO', 'ERROR'); Maarch_AutoImport_putFileInAlreadyExistsDirectory( $folder . DIRECTORY_SEPARATOR . $fileScan, $folder ); } } else { //array_push($tmpFileTri, $fileScan); //array_push($_ENV['fileNameLoaded'], $fileScan); } } } $plugins->executeCallbacks('filenames_loaded_end'); //Sorting tab for compatibilites with Unix system $tmpFileTri = $_ENV['fileNameLoaded']; sort($tmpFileTri); $totalNbfile = 0; $sqlQueryBuilded = ''; $sqlFoldersQueryBuilded = ''; for ($l = 0; $l < count($tmpFileTri); $l++) { $supremVerif = true; $fileExtension = Maarch_AutoImport_extractFileExt( $folder . $tmpFileTri[$l] ); if (strtoupper($fileExtension) == "XML" && $_ENV['withoutXml'] == "false" ) { if ( Maarch_AutoImport_searchImageOfXml( $folder . $tmpFileTri[$l], ".XML", ".PDF" ) == false && Maarch_AutoImport_searchImageOfXml( $folder . $tmpFileTri[$l], ".XML", ".TIF" ) == false && Maarch_AutoImport_searchImageOfXml( $folder . $tmpFileTri[$l], ".XML", ".TIFF" ) == false && Maarch_AutoImport_searchImageOfXml( $folder . $tmpFileTri[$l], ".XML", ".ZIP" ) == false && Maarch_AutoImport_searchImageOfXml( $folder.$tmpFileTri[$l], ".XML", ".DAT" ) == false && Maarch_AutoImport_searchImageOfXml( $folder.$tmpFileTri[$l], ".XML", ".DOC" ) == false && Maarch_AutoImport_searchImageOfXml( $folder.$tmpFileTri[$l], ".XML", ".PPT" ) == false && Maarch_AutoImport_searchImageOfXml( $folder.$tmpFileTri[$l], ".XML", ".7Z" ) == false && Maarch_AutoImport_searchImageOfXml( $folder.$tmpFileTri[$l], ".XML", ".NST" ) == false && Maarch_AutoImport_searchImageOfXml( $folder.$tmpFileTri[$l], ".XML", ".NIST" ) == false && Maarch_AutoImport_searchImageOfXml( $folder.$tmpFileTri[$l], ".XML", ".TXT" ) == false ) { Maarch_AutoImport_manageError( "Image not found : " . $folder . $tmpFileTri[$l], 5, $folder . $tmpFileTri[$l] ); for ($n = 0; $n < count($_ENV['fileNameLoaded']); $n++) { if ($_ENV['fileNameLoaded'][$n] == $tmpFileTri[$l]) { unset($_ENV['fileNameLoaded'][$n]); $_ENV['fileNameLoaded'] = array_values( $_ENV['fileNameLoaded'] ); } } } } else { if ( ( Maarch_AutoImport_searchImageOfXml( $folder . $tmpFileTri[$l], "." . $fileExtension, ".xml" ) == false && Maarch_AutoImport_searchImageOfXml( $folder . $tmpFileTri[$l], ".".$fileExtension, ".XML" ) == false ) && $_ENV['withoutXml'] == "false" ) { Maarch_AutoImport_manageError( "Xml not found : " . $folder . $tmpFileTri[$l], 6, $folder . $tmpFileTri[$l] ); for ($n = 0; $n < count($_ENV['fileNameLoaded']); $n++) { if ($_ENV['fileNameLoaded'][$n] == $tmpFileTri[$l]) { unset($_ENV['fileNameLoaded'][$n]); $_ENV['fileNameLoaded'] = array_values( $_ENV['fileNameLoaded'] ); } } } if ($supremVerif == true) { if (file_exists($folder.$tmpFileTri[$l])) { Maarch_AutoImport_writeLog( "Scan in progress on : " . $folder . $tmpFileTri[$l] , 'OK', 'INFO' ); $tempFileSize = (filesize($folder.$tmpFileTri[$l])); $_ENV['currentTransfertSize'] += $tempFileSize; Maarch_AutoImport_checkingAvailableSpace( $availableSpace, $currentTransfertSize ); $filePath = Maarch_AutoImport_buildDirectory( $docServer . $currentInitFolder, $tmpFileTri[$l], $folder, $nbFiles ); if ($_ENV['createFolders'] == "true") { $sqlArray = array(); $sqlArray = Maarch_AutoImport_buildSqlQuery( $folder . $tmpFileTri[$l], $docServerName, $filePath, $tabMaarchValue, $subDocServer . str_pad($initFile, 4, "0", STR_PAD_LEFT) . "\\", $docServerMiniPath ); $sqlQueryBuilded .= $sqlArray[0]; $sqlFoldersQueryBuilded .= $sqlArray[1]; } else { $sqlQueryBuilded .= Maarch_AutoImport_buildSqlQuery( $folder . $tmpFileTri[$l], $docServerName, $filePath, $tabMaarchValue, $subDocServer . str_pad($initFile, 4, "0", STR_PAD_LEFT) . "\\", $docServerMiniPath ); $plugins->executeCallbacks('insert_end', $tmpFileTri); } $nbFiles++; $totalNbfile = $totalNbfile + 1; } } } } Maarch_AutoImport_writeLog("Number of imported files : " . $totalNbfile , 'OK', 'INFO' ); $_ENV['totalNbfile'] = $totalNbfile; if ($_ENV['createFolders'] == "true") { Maarch_AutoImport_writeLog( "Number of created folders : " . $_ENV['totalNbfolders'] , 'OK', 'INFO' ); $strSqlTab = array(); $strSqlTab[0] = $sqlQueryBuilded; $strSqlTab[1] = $sqlFoldersQueryBuilded; return $strSqlTab; } else { return $sqlQueryBuilded; } } /** * Get the batch id of the AutoImport */ function Maarch_AutoImport_getWorkBatchAutoImport() { $workBatchAutoImport = ''; $reqCwbai = "SELECT param_value_int FROM parameters " . "WHERE id = 'work_batch_autoimport_id'"; $stmt = $_ENV['db']->query($reqCwbai); while ($reqResult = $stmt->fetchObject()) { $workBatchAutoImport = $reqResult->param_value_int + 1; } return $workBatchAutoImport; } /** * Create a log file in the application folder * @param $path string path of the file to backup */ function Maarch_AutoImport_putFileInError($path) { if ($_ENV['backupFailed'] == "true") { $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 . Maarch_AutoImport_extractFileName($path) ) ) { echo "Unable to move file in putFileInError function." . "AutoImport has been stopped!\r\n"; Maarch_AutoImport_manageError( "Unable to move file in putFileInError function. " . "AutoImport has been stopped", 8 ); } else { unlink($path); } } else { unlink($path); } } /** * Put already exists file in a tmp dir * @param $path string path of the file to backup */ function Maarch_AutoImport_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 . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($path), "", Maarch_AutoImport_extractFileName($path) ) . "xml" ) ) ) { if ( !rename( $initPath . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($path), "", Maarch_AutoImport_extractFileName($path) ) . "xml" ), $targetPath . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($path), "", Maarch_AutoImport_extractFileName($path) ) . "xml" ) ) ) { echo "Unable to move file in putFileInAlreadyExistsDirectory " . "function. AutoImport has been stopped!\r\n"; Maarch_AutoImport_manageError( "Unable to move file in putFileInAlreadyExistsDirectory " . "function. AutoImport has been stopped", 8 ); } } elseif ( file_exists( $initPath . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($path), "", Maarch_AutoImport_extractFileName($path) ) . "XML" ) ) ) { if ( !rename( $initPath . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($path), "", Maarch_AutoImport_extractFileName($path) ) . "XML" ), $targetPath . Maarch_AutoImport_extractFileName( str_replace( Maarch_AutoImport_extractFileExt($path), "", Maarch_AutoImport_extractFileName($path) ) . "XML" ) ) ) { echo "Unable to move file in putFileInAlreadyExistsDirectory " . "function. AutoImport has been stopped!\r\n"; Maarch_AutoImport_manageError( "Unable to move file in putFileInAlreadyExistsDirectory" . "function. AutoImport has been stopped", 8 ); } } } if (!rename($path, $targetPath.Maarch_AutoImport_extractFileName($path))) { echo "Unable to move file in putFileInAlreadyExistsDirectory " . "function. AutoImport has been stopped!\r\n"; Maarch_AutoImport_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 Maarch_AutoImport_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"; Maarch_AutoImport_writeLog("Moving imported files in subfolder : " . $directory . " failed" . DIRECTORY_SEPARATOR . $_ENV['wbai'] , 'OK', 'INFO'); 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; $fo < count($_ENV['folderLoaded']); $fo++) { if ( !is_dir( $directory . DIRECTORY_SEPARATOR . "failed" . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR . $_ENV['folderLoaded'][$fo] . DIRECTORY_SEPARATOR ) ) { mkdir( $directory . DIRECTORY_SEPARATOR . "failed" . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR . $_ENV['folderLoaded'][$fo] . DIRECTORY_SEPARATOR, 0777 ); } } } for ($i = 0; $i < count($_ENV['fileNameLoaded']); $i++) { if ( !copy( $directory . $_ENV['fileNameLoaded'][$i], $directory . "failed" . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR . $_ENV['fileNameLoaded'][$i] ) ) { echo "Unable to move file in createLockFile function. " . "AutoImport has been stopped!\r\n"; Maarch_AutoImport_manageError( "Unable to move file in createLockFile function. " . "AutoImport has been stopped", 8 ); } else { unlink($directory . $_ENV['fileNameLoaded'][$i]); } } //To delete folder in scan folder for ($vo = 0; $vo < count($_ENV['folderLoaded']); $vo++) { $filesToMove = 0; $classMoveScan = dir( $directory . DIRECTORY_SEPARATOR . $_ENV['folderLoaded'][$vo] . DIRECTORY_SEPARATOR ); while (($fileScanmove = $classMoveScan->read()) !== 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 Maarch_AutoImport_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") { $string = addslashes($string); $string = str_replace("\\", "#", $string); } elseif ($_ENV['databasetype'] == "POSTGRESQL") { $string = pg_escape_string($string); $string = str_replace("\\", "#", $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 Maarch_AutoImport_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("Ymd").DIRECTORY_SEPARATOR ) ) { mkdir( $directory . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . date("Ymd").DIRECTORY_SEPARATOR, 0777 ); } if ( !is_dir( $directory . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . date("Ymd") . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR ) ) { mkdir( $directory . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . date("Ymd") . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR, 0777 ); } //Create the subfolders if subfolders exists in the scan folder if (!empty($_ENV['folderLoaded'])) { for ($fo = 0; $fo < count($_ENV['folderLoaded']); $fo++) { if ( !is_dir( $directory . DIRECTORY_SEPARATOR . "backup". DIRECTORY_SEPARATOR . date("Ymd") . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR . $_ENV['folderLoaded'][$fo] . DIRECTORY_SEPARATOR ) ) { mkdir( $directory . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . date("Ymd") . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR . $_ENV['folderLoaded'][$fo] . DIRECTORY_SEPARATOR, 0777 ); } } } Maarch_AutoImport_writeLog("Moving imported files in subfolder : " . $directory . "backup" . DIRECTORY_SEPARATOR . date("Ymd") . DIRECTORY_SEPARATOR . $_ENV['wbai'], 'OK', 'INFO'); } for ($i = 0; $i < count($_ENV['fileNameLoaded']); $i++) { if ($_ENV['backupBatch'] == "true") { if ( !copy( $directory . $_ENV['fileNameLoaded'][$i], $directory . "backup" . DIRECTORY_SEPARATOR . date("Ymd") . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR . $_ENV['fileNameLoaded'][$i] ) ) { Maarch_AutoImport_manageError( "Move imported files in backup failed", 7 ); } } unlink($directory . $_ENV['fileNameLoaded'][$i]); } //To delete folder in scan folder for ($vo = 0; $vo < count($_ENV['folderLoaded']); $vo++) { $filesToMove = 0; $classMoveScan = dir( $directory . DIRECTORY_SEPARATOR . $_ENV['folderLoaded'][$vo] . DIRECTORY_SEPARATOR ); while (($fileScanmove = $classMoveScan->read()) != 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 Maarch_AutoImport_getDocSeverLocation($docServerName) { $docServerLocation = ''; $reqGdsl = "SELECT path_template FROM docservers WHERE docserver_id = ?"; $stmt = $_ENV['db']->query($reqGdsl, array($docServerName)); while ($reqResult = $stmt->fetchObject()) { $docServerLocation = $reqResult->path_template; } return $docServerLocation; } /** * Update the database with the new batch AutoImport id * @param $workBatchAutoImport long name of tjhe docserver */ function Maarch_AutoImport_updateWorkingBatchAutoImport($workBatchAutoImport) { $reqUwbai = "UPDATE parameters SET param_value_int = ? WHERE id = 'work_batch_autoimport_id'"; $_ENV['db']->query($reqUwbai, array($workBatchAutoImport)); } /** * 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 Maarch_AutoImport_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 Maarch_AutoImport_extractFileExt($sFullPath) { $sName = $sFullPath; if (strpos($sName, ".") == 0) { return $extractFileExt = ""; exit; } else { $extractFileExt = explode(".", $sName); return $extractFileExt[count($extractFileExt) - 1]; exit; } } /** * Return only name of the file path * @param $sFullPath string path of the file */ function Maarch_AutoImport_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 Maarch_AutoImport_docServerSizeinit($docServerId) { $req = "SELECT size_limit_number, actual_size_number " . "FROM docservers WHERE docserver_id = ?"; $stmt = $_ENV['db']->query($req, array($docServerId)); while ($result = $stmt->fetchObject()) { $_ENV['sizeLimitConfig'] = $result->size_limit_number; $actualDocserverSize = $result->actual_size_number; $availableSpace = $result->size_limit_number - $result->actual_size_number; } return $availableSpace; } /** * Update the actual size of the DocServer * @param $docServerId string id of the docserver * @param $theSize long size of the docserver */ function Maarch_AutoImport_docserverSizeUpdate($docServerId, $theSize) { $req1 = "SELECT actual_size_number " . "FROM docservers WHERE docserver_id= ?"; $stmt = $_ENV['db']->query($req1, array($docServerId)); while ($result = $stmt->fetchObject()) { $actualSize = $result->actual_size_number; $size = ($actualSize + $theSize); $req2 = "UPDATE docservers " . "SET actual_size_number = ? " . "WHERE docserver_id = ?"; $_ENV['db']->query($req2, array($size, $docServerId)); } } /** * Return the fingerprint mode of the DocServer * @param $docServerId string id of the docserver */ function Maarch_AutoImport_docserverFingerprintMode($docServerId) { $req = "SELECT fingerprint_mode FROM docserver_types " . "WHERE docserver_type_id in (" . " SELECT docserver_type_id FROM docservers " . " WHERE docserver_id = ?" . ")"; $stmt = $_ENV['db']->query($req, array($docServerId)); while ($result = $stmt->fetchObject()) { $_ENV['docserverFingerprintMode'] = $result->fingerprint_mode; } } function Maarch_AutoImport_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 Maarch_AutoImport_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..."; Maarch_AutoImport_manageError( "The free space available in the docserver is not " . "sufficient to continue. End of application.", 2 ); Maarch_AutoImport_writeLog("Available space in bytes : " . $availableSpace, 'OK', 'INFO' ); Maarch_AutoImport_writeLog("Data size : " . $actualTransferSize, 'OK', 'INFO' ); } } } /** * 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 Maarch_AutoImport_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] ) ) { Maarch_AutoImport_manageError( "Copy of a file failed during treatment of it :" . "put the file in failed directory " . $filePath . $fileName, 3, $filePath.$fileName ); } $fingerprintControl = array(); $fingerprintControl = Maarch_AutoImport_controlFingerprint( $filePath . $fileName, $docServer . $docName . '.' . $extTab[count($extTab) - 1], $_ENV['docserverFingerprintMode'] ); if ($fingerprintControl['status'] == 'ko') { Maarch_AutoImport_manageError( 'Fingerprint control failed between incoming and docserver ' . $filePath . $fileName . ' error : ' . $fingerprintControl['error'] , 14, $filePath . $fileName ); } Maarch_AutoImport_writeLog("New file created : " . $docServer . $docName . "." . $extTab[count($extTab) - 1], 'OK', 'INFO'); return $docServer.$docName.".".$extTab[count($extTab) - 1]; } } } /** * Control fingerprint between two resources * @param string $pathInit path of the resource 1 * @param string $pathTarget path of the resource 2 * @param string $fingerprintMode (md5, sha512, ...) * @return array ok or ko with error */ function Maarch_AutoImport_controlFingerprint( $pathInit, $pathTarget, $fingerprintMode='NONE' ) { $result = array(); if (Maarch_AutoImport_doFingerprint($pathInit,$fingerprintMode) <> Maarch_AutoImport_doFingerprint($pathTarget, $fingerprintMode) ) { $result = array( 'status' => 'ko', 'error' => 'pb with fingerprint of document' . ' ' . $pathInit . ' and ' . $pathTarget, ); } else { $result = array( 'status' => 'ok', 'error' => '', ); } return $result; } /** * 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 Maarch_AutoImport_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"; Maarch_AutoImport_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"; Maarch_AutoImport_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"; Maarch_AutoImport_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 One H or more than HH if ((preg_match('/H/', $format) && !preg_match('/HH/', $format)) || (preg_match('/HHH/', $format)) ) { echo "\nError DATE_FORMAT tag : wrong description of the hour"; Maarch_AutoImport_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 One I or more than two I if ((preg_match('/I/', $format) && !preg_match('/II/', $format)) || (preg_match('/III/', $format)) ) { echo "\nError DATE_FORMAT tag : wrong description of the minutes"; Maarch_AutoImport_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 One S or more than two S if ((preg_match('/S/', $format) && !preg_match('/SS/', $format)) || (preg_match('/SSS/', $format)) ) { echo "\nError DATE_FORMAT tag : wrong description of the seconds"; Maarch_AutoImport_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"; Maarch_AutoImport_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 Maarch_AutoImport_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 Maarch_AutoImport_writeInSqlFile( $sqlImportFile, $query, $autoImportDirectory ) { $sqlImportFileOpened = fopen($autoImportDirectory.$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 Maarch_AutoImport_buildSqlQuery( $filePath, $docServerName, $fileDestination, $tabMaarchValue, $subDocServer, $docServer ) { global $plugins; $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 ($xmlFileLoaded == false) { $xmlFileLoaded = simplexml_load_file(urlencode($xmlFile)); } } if ($_ENV['insertMode'] == "true") { //typist $sqlValues = "'AUTOIMPORT', "; //creation_date if ($_ENV['databasetype'] == 'ORACLE') { $sqlValues .= "Date(sysdate), "; } elseif ($_ENV['databasetype'] == 'SQLSERVER') { $sqlValues .= "'" . date("Ymd H:i:s.000") . "', "; } else { $sqlValues .= "'" . date("Y-m-d 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 .= "'" . Maarch_AutoImport_doFingerprint( $filePath, $_ENV['docserverFingerprintMode'] ) . "', "; //filesize $sqlValues .= "" . filesize($filePath) . ", "; //format $format = Maarch_AutoImport_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 H:i:s.000"); } else { $insertLineValue .= ";" . date("Y-m-d 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 .= ";" . Maarch_AutoImport_doFingerprint( $filePath, $_ENV['docserverFingerprintMode'] ); //filesize $insertLineValue .= ";" . filesize($filePath); //format $format = Maarch_AutoImport_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 ( !Maarch_AutoImport_checkFormatDate( $xmlFileLoaded->{$tabMaarchValue[$j]}, $_ENV['tabFormatDate'][$j]['dateFormat'] ) ) { //echo "ERROR XML format (DATE_FORMAT tag) ! // . "End of Application"; Maarch_AutoImport_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 .= "'" . Maarch_AutoImport_dateTimeConvert( $xmlFileLoaded->{$tabMaarchValue[$j]}, $_ENV['dateTimeFormat'], $_ENV['tabFormatDate'][$j] ) . "', "; $xmlValueContent[$j] = ""; //echo $_ENV['tabMaarchField'][$j] . " : " // . Maarch_AutoImport_dateTimeConvert( // $xmlFileLoaded->{$tabMaarchValue[$j]}, // $_ENV['dateTimeFormat'], // $_ENV['tabFormatDate'][$j] // ) . "\r\n"; } else { $xmlValueContent[$j] = Maarch_AutoImport_dateTimeConvert( $xmlFileLoaded->{$tabMaarchValue[$j]}, $_ENV['dateTimeFormat'], $_ENV['tabFormatDate'][$j] ); } } elseif ($_ENV['tabMaarchString'][$j] == true) { if ($_ENV['insertMode'] == "true") { $sqlValues .= "'" . Maarch_AutoImport_protectStringDb( $xmlFileLoaded->{$tabMaarchValue[$j]} )."', "; $xmlValueContent[$j] = ""; //LGI1 /*echo "string " . $_ENV['tabMaarchField'][$j] . " : " . Maarch_AutoImport_protectStringDb( $xmlFileLoaded -> {$tabMaarchValue[$j]} ) . "\r\n";*/ } else { $xmlValueContent[$j] = Maarch_AutoImport_protectStringDb( $xmlFileLoaded->{$tabMaarchValue[$j]} ); } } elseif ($_ENV['tabMaarchInt'][$j] == true) { if ($_ENV['insertMode'] == "true") { if ($xmlFileLoaded->{$tabMaarchValue[$j]} == "" && $_ENV['tabMaarchDefaultIfTagEmpty'][$j] <> '') { $xmlFileLoaded->{$tabMaarchValue[$j]} = $_ENV['tabMaarchDefaultIfTagEmpty'][$j]; $sqlValues .= "".$_ENV['tabMaarchDefaultIfTagEmpty'][$j].", "; } else { $sqlValues .= "".$xmlFileLoaded -> {$tabMaarchValue[$j]}.", "; } //LGI2 $xmlValueContent[$j] = ""; //echo "int " . $_ENV['tabMaarchField'][$j] . " : " // . $xmlFileLoaded->{$tabMaarchValue[$j]} . "\r\n"; } else { $xmlValueContent[$j] = $xmlFileLoaded->{$tabMaarchValue[$j]}; } } else { if ($_ENV['insertMode'] == "true") { if ($xmlFileLoaded->{$tabMaarchValue[$j]} == "" && $_ENV['tabMaarchDefaultIfTagEmpty'][$j] <> '') { $xmlFileLoaded->{$tabMaarchValue[$j]} = 0; } //LGI3 $sqlValues .= $xmlFileLoaded->{$tabMaarchValue[$j]} . ", "; $xmlValueContent[$j] = ""; //echo "other " . $_ENV['tabMaarchField'][$j] . " : " // . $xmlFileLoaded->{$tabMaarchValue[$j]} . "\r\n"; } else { $xmlValueContent[$j] = $xmlFileLoaded->{$tabMaarchValue[$j]}; } } if ($xmlValueContent[$j] == "") { if ($_ENV['insertMode'] == "true") { //LGI /*if ($_ENV['tabMaarchDefaultIfTagEmpty'][$j] <> '') { $sqlValues .= "'" . $_ENV['tabMaarchDefaultIfTagEmpty'][$j] . "', "; echo "empty " . $xmlValueContent[$j] . " : " . $_ENV['tabMaarchDefaultIfTagEmpty'][$j] . "\r\n"; }*/ } 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); } $insertLineValue .= "\r\n"; if ($_ENV['createFolders'] == "true") { $sqlArray = array(); $sqlArray[0] = $insertLineValue; $sqlArray[1] = Maarch_AutoImport_buildFolderSqlQuery($xmlFileLoaded); return $sqlArray; } else { return $insertLineValue; } } /** * Slice a date to a format * @param $dateFormat string format of the date */ function Maarch_AutoImport_sliceDateFormat($dateFormat) { $arr = array(); if (empty($dateFormat) || !isset($dateFormat)) { echo "Empty DATE_FORMAT tag in the mapping file ! End of Application"; Maarch_AutoImport_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"; Maarch_AutoImport_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"; Maarch_AutoImport_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"; Maarch_AutoImport_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"; Maarch_AutoImport_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"; Maarch_AutoImport_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"; Maarch_AutoImport_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 Maarch_AutoImport_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 ) ) { mkdir( $docServer . date("Y") . DIRECTORY_SEPARATOR . date("m") . DIRECTORY_SEPARATOR . $_ENV['wbai'] . DIRECTORY_SEPARATOR, 0777 ); } else { echo "\Folder Already exist... Software stopped!"; Maarch_AutoImport_manageError( "Workbatch already exist! Unable to send file " . "in the Docserver. End of application.", 11 ); } } /** * create the ENV array for folder mapping field * @param folder mapping file */ function Maarch_AutoImport_initFolders($foldersMappingFile) { if ($_ENV['createFolders'] == "true") { Maarch_AutoImport_writeLog("Create folders is enabled", 'OK', 'DEBUG'); $xmlmapping = simplexml_load_file($foldersMappingFile); $tabMaarchFoldersField = array(); $iMap = 1; //Loading MAPPING_FILE tag $MAPPING_FILE = $xmlmapping->MAPPING_FILE; foreach ($MAPPING_FILE->ELEMENT as $ELEMENT) { $tabMaarchFoldersField[$iMap] = $ELEMENT->COLUMN; if ($ELEMENT->KEY && $ELEMENT->KEY == "true") { $_ENV['tabMaarchFoldersKey'][$iMap] = true; } else { $_ENV['tabMaarchFoldersKey'][$iMap] = false; } // if (strstr($ELEMENT->VALUE, "'") <> false) { $_ENV['tabMaarchFoldersDefaultValue'][$iMap] = str_replace( "'", "", $ELEMENT->VALUE ); $_ENV['tabMaarchFoldersValue'][$iMap] = "DEFAULT_VALUE_MAARCH"; } else { $_ENV['tabMaarchFoldersValue'][$iMap] = $ELEMENT->VALUE; } //if the value tag is empty $_ENV['tabMaarchFoldersDefaultIfTagEmpty'][$iMap] = $ELEMENT->DEFAULT_IF_TAG_EMPTY; $_ENV['tabMaarchFoldersDate'][$iMap] = false; $_ENV['tabMaarchFoldersString'][$iMap] = false; $_ENV['tabMaarchFoldersInt'][$iMap] = false; $_ENV['tabFoldersFormatDate'][$iMap] = array(); if ($ELEMENT->FORMAT) { if ($ELEMENT->FORMAT == "DATE" || $ELEMENT->FORMAT == "date" ) { $_ENV['tabMaarchFoldersDate'][$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"; Maarch_AutoImport_manageError( "A DATE_FORMAT tag is missing in the " . "mapping file! Please modify your " . "mapping file", 9 ); } else { $_ENV['tabFoldersFormatDate'][$iMap] = Maarch_AutoImport_sliceDateFormat( $ELEMENT->DATE_FORMAT ); } } elseif ( $ELEMENT->FORMAT == "STRING" || $ELEMENT->FORMAT == "string" ) { $_ENV['tabMaarchFoldersString'][$iMap] = true; } elseif ( $ELEMENT->FORMAT == "INTEGER" || $ELEMENT->FORMAT == "integer" ) { $_ENV['tabMaarchFoldersInt'][$iMap] = true; } } $iMap++; } $_ENV['tabMaarchFoldersField'] = $tabMaarchFoldersField; } } /** * Check if folder already exists in database and in folder array * @param folder id */ function Maarch_AutoImport_isFolderExist($folderId) { $folderExist = false; static $foldersTab = array(); //The folder is in database $sql = "SELECT folders_system_id FROM " . $_ENV['foldersTableName'] . " WHERE folder_id = ?"; $stmt = $_ENV['db']->query($sql, array($folderId)); if ($stmt->rowCount > 0) { $folderExist = true; } else { //The folder is already in the array to generate sql_file_folders if (in_array($folderId, $foldersTab)) { $folderExist = true; } else { //Put the folder in the array $foldersTab[] = $folderId; } } return $folderExist; } /** * build folder sql query * @param xml index file */ function Maarch_AutoImport_buildFolderSqlQuery($xmlFileLoaded) { $sqlString = ""; if ($_ENV['createFolders'] == "true") { //print_r($xmlFileLoaded); $sqlValues = array(); if ($_ENV['insertMode'] == "true") { //typist $sqlValues[] = "'AUTOIMPORT'"; //creation_date and last_modified_date if ($_ENV['databasetype'] == 'ORACLE') { $sqlValues[] = "Date(sysdate)"; $sqlValues[] = "Date(sysdate)"; } elseif ($_ENV['databasetype'] == 'SQLSERVER') { $sqlValues[] = "'" . date("Ymd H:i:s.000"); $sqlValues[] = "'" . date("Ymd H:i:s.000"); } else { $sqlValues[] = "'" . date("Y-m-d H:i:s") . "'"; $sqlValues[] = "'" . date("Y-m-d H:i:s") . "'"; } } else { //typist $sqlValues[] = "AUTOIMPORT"; //creation_date and last_modified_date if ($_ENV['databasetype'] == 'ORACLE') { // $sqlValues[] = 'Date(sysdate)'; // $sqlValues[] = 'Date(sysdate)'; } else if ($_ENV['databasetype'] == 'SQLSERVER') { $sqlValues[] = date("Ymd H:i:s.000"); $sqlValues[] = date("Ymd H:i:s.000"); } else { $sqlValues[] = date("Y-m-d H:i:s"); $sqlValues[] = date("Y-m-d H:i:s"); } } //write the xml value in the good sql file to import $xmlFoldersValueContent = array(); for ($j = 1; $j <= sizeof($_ENV['tabMaarchFoldersValue']); $j++) { //echo $_ENV['tabMaarchFoldersValue'][$j]."\r\n"; //debug //Default value if (isset($_ENV['tabMaarchFoldersDefaultValue'][$j])) { if ($_ENV['insertMode'] == "true") { if ($_ENV['tabMaarchFoldersInt'][$j] === true) { $sqlValues[] = $_ENV['tabMaarchFoldersDefaultValue'][$j]; } else { $sqlValues[] = "'" . $_ENV['tabMaarchFoldersDefaultValue'][$j] . "'"; } } else { $sqlValues[] = $_ENV['tabMaarchFoldersDefaultValue'][$j]; } } elseif ( $_ENV['tabMaarchFoldersDate'][$j] === true && count($_ENV['tabFoldersFormatDate']) > 1 ) { //Date value if ( !Maarch_AutoImport_checkFormatDate( $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]}, $_ENV['tabFoldersFormatDate'][$j]['dateFormat'] ) ) { Maarch_AutoImport_manageError( "Error DATE_FORMAT tag : wrong description of " . "the date in the folder mapping file : put " . "all the loading batch in failed directory " . "and stop application", 9 ); } if ($_ENV['insertMode'] == "true") { $sqlValues[] = "'" . Maarch_AutoImport_dateTimeConvert( $xmlFileLoaded ->{$_ENV['tabMaarchFoldersValue'][$j]}, $_ENV['dateTimeFormat'], $_ENV['tabFoldersFormatDate'][$j] ) . "'"; } else { $sqlValues[] = Maarch_AutoImport_dateTimeConvert( $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]}, $_ENV['dateTimeFormat'], $_ENV['tabFoldersFormatDate'][$j] ); } } elseif ($_ENV['tabMaarchFoldersString'][$j] === true) { //String value if ($_ENV['insertMode'] == "true") { $sqlValues[] = "'" . Maarch_AutoImport_protectStringDb( $xmlFileLoaded ->{$_ENV['tabMaarchFoldersValue'][$j]} ) . "'"; //echo $_ENV['tabMaarchFoldersField'][$j] . " : " // . Maarch_AutoImport_protectStringDb( // $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]} // ) . "\r\n"; } else { $sqlValues[] = Maarch_AutoImport_protectStringDb( $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]} ); } } elseif ($_ENV['tabMaarchFoldersInt'][$j] === true) { //Integer value } else { //Default value (if none of them) if ($_ENV['insertMode'] == "true") { if ( $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]} == "" ) { $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]} = 0; } $sqlValues[] = $xmlFileLoaded ->{$_ENV['tabMaarchFoldersValue'][$j]}.""; //echo $_ENV['tabMaarchFoldersField'][$j] . " : " // . $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]} // . "\r\n"; } else { $sqlValues[] = $xmlFileLoaded ->{$_ENV['tabMaarchFoldersValue'][$j]}; } } if ($_ENV['tabMaarchFoldersKey'][$j] === true) { $folderId = Maarch_AutoImport_protectStringDb( $xmlFileLoaded->{$_ENV['tabMaarchFoldersValue'][$j]} ); } } if (Maarch_AutoImport_isFolderExist($folderId) === false) { //Go!! if ($_ENV['insertMode'] == "true") { $sqlColumns = array(); if ($_ENV['databasetype'] == "ORACLE") { $sqlColumns[] = "typist"; $sqlColumns[] = "creation_date sysdate"; $sqlColumns[] = "last_modified_date sysdate"; } else { $sqlColumns[] = "typist"; $sqlColumns[] = "creation_date"; $sqlColumns[] = "last_modified_date"; } //print_r($sqlValues); //debug //Get the fields for folders $j = 0; for ( $j = 1; $j <= sizeof($_ENV['tabMaarchFoldersField']); $j++ ) { $sqlColumns[] = $_ENV['tabMaarchFoldersField'][$j]; } $sqlString = "INSERT INTO " . $_ENV['foldersTableName'] . " (" . implode(', ', $sqlColumns) . ") " . "values(" . implode(', ', $sqlValues) . ")"; } else { $sqlString = implode(';', $sqlValues); $sqlString = str_replace("'", "", $sqlString); $sqlString = $sqlString . "\r\n"; } //echo "\n Folder: ".$folderId."\n"; $_ENV['totalNbfolders']++; } //echo $sqlString; } return $sqlString; } function Maarch_AutoImport_loadPlugins($includePath, $rootNode) { if ($rootNode->plugin <> '') { foreach ($rootNode->plugin as $plugin) { $res = include_once $includePath.'/../Maarch/AutoImport/Plugins/' . ((string) $plugin) . '.php'; print '$res = '.($res === False ? "KO" : "OK").' for plugin '. $plugin. "\n"; if ($res === false) { Maarch_AutoImport_manageError( "Could not load plugin '$plugin'", 13 ); } } } } //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 AutoImport (Xml Error)."; exit; } //loading of the xml config file. $xmlConfig = simplexml_load_file($conf); $CONFIG = $xmlConfig->CONFIG; $CONFIG_BASE = $xmlConfig->CONFIG_BASE; $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_BASE->databaseserver; $_ENV['databaseport'] = (string) $CONFIG_BASE->databaseserverport; $_ENV['database'] = (string) $CONFIG_BASE ->databasename; $_ENV['databasetype'] = (string) $CONFIG_BASE->databasetype; $_ENV['databaseuser'] = (string) $CONFIG_BASE ->databaseuser; $_ENV['databasepwd'] = (string) $CONFIG_BASE->databasepassword; $_ENV['databaseworkspace'] = (string) $CONFIG_BASE->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['backupFailed'] = $CONFIG -> BACKUP_FAILED; $_ENV['excludeExistingDocs'] = $CONFIG -> EXCLUDE_EXISTING_DOCS; $_ENV['excludeExistingDocsFolder'] = $CONFIG->EXCLUDE_EXISTING_DOCS_FOLDER; $_ENV['excludeFolder'] = $CONFIG->EXCLUDE_FOLDER; $_ENV['controlCompleteFiles'] = $CONFIG -> CONTROL_COMPLETE_FILES; $_ENV['aid'] = $autoImportDirectory; $_ENV['createFolders'] = (string) $CONFIG -> CREATE_FOLDERS; $_ENV['foldersTableName'] = (string) $CONFIG -> FOLDERS_TABLE_NAME; $_ENV['appName'] = (string) $CONFIG -> APP_NAME; $_ENV['logger'] = (string) $CONFIG -> LOGGER; $_ENV['logFormat'] = (string) $CONFIG -> LOG_FORMAT; $_ENV['logConfigPath'] = (string) $CONFIG -> LOG_CONFIG_PATH; $foldersMappingFile = (string) $CONFIG -> FOLDERS_MAPPING_FILE; require_once $autoImportDirectory.'/../Maarch/AutoImport/PluginManager.php'; require_once $autoImportDirectory.'/../tools/log4php/Logger.php'; $plugins = new Maarch_AutoImport_PluginManager(); if (isset($CONFIG->PLUGINS)) { Maarch_AutoImport_loadPlugins($autoImportDirectory, $CONFIG->PLUGINS); } //look if there is files to process //print_r($_ENV);exit(); if (Maarch_AutoImport_scanIncoming($scanImportDirectory)) { /*if ($_ENV['controlCompleteFiles'] == "true" && !Maarch_AutoImport_isCompleteBatch($scanImportDirectory) ) { echo "batch not complete"; exit(0); }*/ Maarch_AutoImport_loginCreation(); Maarch_AutoImport_writeLog("Loading xml config file : ".$conf, 'OK', 'INFO'); Maarch_AutoImport_writeLog("Database type : ".$_ENV['databasetype'], 'OK', 'DEBUG'); if ($_ENV['insertMode'] == "true") { Maarch_AutoImport_writeLog("Insert SQL Mode", 'OK', 'DEBUG'); } 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"; Maarch_AutoImport_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", 'OK', 'FATAL'); exit; } $lockFile = $_ENV['aid'] . $_ENV['configName'] . ".lck"; $lockFileOpened = fopen($lockFile, "a"); fwrite($lockFileOpened, 1); fclose($lockFileOpened); require_once("class_db_pdo.php"); $_ENV['db'] = new Database($conf); $plugins->executeCallbacks('program_start'); //Return location of docserver $docServer = Maarch_AutoImport_getDocSeverLocation($docServerName); Maarch_AutoImport_docserverFingerprintMode($docServerName); echo "\n" . $docServer . "\n"; //The if docserver exists $errorDocserver = false; if (!is_dir($docServer)) { Maarch_AutoImport_manageError( "Path of the docserver not exists", 12 ); $errorDocserver = true; } //Caculing the work batch autoimport number $wbai = Maarch_AutoImport_getWorkBatchAutoImport(); $_ENV['wbai'] = $wbai; $wbai = str_pad($_ENV['wbai'], 8, "0", STR_PAD_LEFT); Maarch_AutoImport_writeLog("AutoImport WorkBatch is : " . $_ENV['wbai'], 'OK', 'DEBUG'); //Updating the work batch autoimport number Maarch_AutoImport_updateWorkingBatchAutoImport($_ENV['wbai']); //Calcul of the init size avaible on docserver $availableSpace = Maarch_AutoImport_docServerSizeinit($docServerName); Maarch_AutoImport_writeLog("Target OS : " . $_ENV['osName'], 'OK', 'DEBUG'); //Loading the mapping xml file Maarch_AutoImport_writeLog("Loading mapping xml file", 'OK', 'DEBUG'); $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"; Maarch_AutoImport_manageError( "A DATE_FORMAT tag is missing in the " . "mapping file! Please modify your " . "mapping file", 9 ); } else { $_ENV['tabFormatDate'][$iMap] = Maarch_AutoImport_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; Maarch_AutoImport_writeLog( "Find current directory and file of the docserver", 'OK', 'INFO' ); //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; Maarch_AutoImport_createPathOnDocServer($docServer); Maarch_AutoImport_writeLog("Scanning import folder", 'OK', 'INFO'); Maarch_AutoImport_writeLog("Current target docserver location for import : " . $initDirectory, 'OK', 'DEBUG'); //Intit folders options Maarch_AutoImport_initFolders($foldersMappingFile); $request = ""; if ($_ENV['createFolders'] == "true") { $tabRequest = array(); $tabRequest = Maarch_AutoImport_scan( $folder, $availableSpace, $initDirectory, $docServerName, $tabMaarchValue, $subDocServer, $docServer, $initFile ); $request = $tabRequest[0]; $requestFolders = $tabRequest[1]; } else { $request = Maarch_AutoImport_scan( $folder, $availableSpace, $initDirectory, $docServerName, $tabMaarchValue, $subDocServer, $docServer, $initFile ); } if ($errorDocserver == true) { $plugins->executeCallbacks("error_docserver_end"); } if ($request <> "") { //Processing Folders if ($_ENV['createFolders'] == "true" && $_ENV['insertMode'] == "false" ) { $sqlFileFolders = "sql_import_file_" . $_ENV['configName'] . "_" . $_ENV['foldersTableName'] . ".sql"; if (file_exists($autoImportDirectory.$sqlFileFolders)) { unlink($autoImportDirectory.$sqlFileFolders); } Maarch_AutoImport_writeLog("Creation of SQL command file in : ".$sqlFileFolders, 'OK', 'DEBUG'); Maarch_AutoImport_writeInSqlFile( $sqlFileFolders, $requestFolders, $autoImportDirectory ); if ($_ENV['databasetype'] == "ORACLE") { $maarchFoldersField = 'typist, creation_date sysdate, ' . 'last_modified_date sysdate '; } else { $maarchFoldersField = "typist, creation_date, " . "last_modified_date "; } if (sizeof($_ENV['tabMaarchFoldersField']) <> 0) { $maarchFoldersField .= ","; } for ( $j = 1; $j <= sizeof($_ENV['tabMaarchFoldersField']); $j++ ) { if (count($_ENV['tabFoldersDate'][$j]) > 0 && $_ENV['databasetype'] == "ORACLE" ) { $maarchFoldersField .= " " . $_ENV['tabMaarchFoldersField'][$j] . " date \"" . $_ENV['dateTimeFormat'] . "\","; } else { $maarchFoldersField .= " " .$_ENV['tabMaarchFoldersField'][$j] . ","; } } $maarchFoldersField = substr($maarchFoldersField, 0, -1); if ($_ENV['insertMode'] == "false") { $sqlFileFolders = str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $sqlFileFolders ); } //Create the sql statements if ($_ENV['databasetype'] == "MYSQL") { $reqFolders = "LOAD DATA LOCAL INFILE '" . str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory ) . $sqlFileFolders . "' INTO TABLE " . $_ENV['foldersTableName'] . " FIELDS TERMINATED BY ';' " . "LINES TERMINATED BY '\\n' " . "(" . $maarchFoldersField . ");"; } elseif ($_ENV['databasetype'] == "POSTGRESQL") { $sqlFileFolders = str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $sqlFileFolders ); $reqFolders = "COPY " . $_ENV['foldersTableName'] . " (" . $maarchFoldersField . ") " . "FROM '" . str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory ) . $sqlFileFolders . "' WITH DELIMITER AS ';';"; } elseif ($_ENV['databasetype'] == "ORACLE") { $reqFolders = "LOAD DATA INFILE '" . str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory ) . $sqlFileFolders . "' APPEND INTO TABLE " . $_ENV['foldersTableName'] . " FIELDS TERMINATED BY ';' " . "(" . $maarchFoldersField . ")"; } elseif ($_ENV['databasetype'] == "SQLSERVER") { $reqFolders = "bulk insert " . $_ENV['database'] . ".dbo." . $_ENV['foldersTableName'] . " from '" . str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory ) . $sqlFileFolders . "' with( DATAFILETYPE = 'widechar', formatfile='" . $autoImportDirectory . "maarch_autoimport" . DIRECTORY_SEPARATOR . $_ENV['foldersTableName'] . ".xml' );"; } Maarch_AutoImport_writeLog($reqFolders, 'OK', 'DEBUG'); //Execution! if ($_ENV['databasetype'] == "ORACLE") { $fnm = $_ENV['configName']."_control.txt"; $inF = fopen($fnm, "w"); fwrite($inF, $reqFolders); 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); if ($oracleReturn <> 0) { Maarch_AutoImport_manageError( "Maarch Auto Import cannot execute query " . "on database", 4 ); } } else { $reqExecute = $_ENV['db']->query($reqFolders); if (!$reqExecute) { echo "\n" . $_ENV['databasetype'] . " Error!: " ; Maarch_AutoImport_manageError( "Maarch Auto Import cannot execute query" . " on database", 4 ); } } } //End of processing folders $theSize = $_ENV['currentTransfertSize']; Maarch_AutoImport_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($autoImportDirectory . $tabSqlFile[$z])) { unlink($autoImportDirectory . $tabSqlFile[$z]); } Maarch_AutoImport_writeLog("Creation of SQL command file in : ".$tabSqlFile[$z], 'OK', 'DEBUG'); Maarch_AutoImport_writeInSqlFile( $tabSqlFile[$z], $request, $autoImportDirectory ); } 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 '" . str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory ) . $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 '" . str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory ) . $tabSqlFile[$z] . "' WITH DELIMITER AS ';';"; } elseif ($_ENV['databasetype'] == "SQLSERVER") { $req = "bulk insert " . $_ENV['database'] . ".dbo." . $tabTableMaarch[1] . " from '".str_replace(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory) . $tabSqlFile[$z] . "' with( DATAFILETYPE = 'widechar', formatfile='" . $autoImportDirectory . "maarch_autoimport" . DIRECTORY_SEPARATOR . $tabTableMaarch[1] . ".xml' );"; } if ($_ENV['databasetype'] == "ORACLE") { $req = "LOAD DATA INFILE '" . str_replace( DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, $autoImportDirectory ) . $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' ) { /*echo "sqlldr userid=" . $_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\r\n";*/ $oracleQueryResult = exec( "sqlldr userid=" . $_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 ); } 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); } Maarch_AutoImport_writeLog($req, 'OK', 'DEBUG'); if ($_ENV['databasetype'] <> "ORACLE") { //Execute the SQL query. $reqExecute = $_ENV['db']->query($req); if (!$reqExecute) { $plugins->executeCallbacks("error_database_begin"); echo "\n" . $_ENV['databasetype'] . " Error!: "; Maarch_AutoImport_manageError( "Maarch Auto Import cannot execute " . "query on database", 4 ); } } elseif ($oracleReturn <> 0) { Maarch_AutoImport_manageError( "Maarch Auto Import cannot execute " . "query on database", 4 ); } } } //$plugins->executeCallbacks('insert_end'); Maarch_AutoImport_writeLog("Import files moved to backup of " . $_ENV['wbai'], 'OK', 'INFO' ); $plugins->executeCallbacks('move_imported_files_begin'); Maarch_AutoImport_moveImportedFiles($folder); $plugins->executeCallbacks('move_imported_files_end'); } Maarch_AutoImport_writeLog("End of application", 'OK', 'INFO'); if (file_exists($lockFile)) { unlink($lockFile); } Maarch_AutoImport_logInDataBase( $_ENV['totalNbfile'], $_ENV['nbErrors'], $_ENV['infoErrors'] ); $plugins->executeCallbacks('program_end'); exit($_ENV['errorLevel']); } else { echo "no file to process"; exit(0); } }