. */ /** * @defgroup mcc Maarch Capture Connector */ /** * mcc_server = server for Maarch Capture Connector * * @file * @author Laurent Giovanonni/ Yves Christian Kpakpo * @date $date$ * @version $Revision$ * @ingroup mcc * @brief mcc_server, server for Maarch Capture Connector */ //Error mode and function error_reporting(E_ERROR); set_error_handler(errorHandler); //Global var of the program /** * maarch capture connector path */ $_ENV['mccPath'] = ""; /** * maarch tmp path */ $_ENV['maarchTmpPath'] = ""; /** * target path for preprocess */ $_ENV['targetPath'] = ""; /** * 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'] = ""; /** * path to the log file */ $_ENV['log'] = ""; $_ENV['tagFields'] = array(); /** * Creation of the log file */ function loginCreation() { if(!is_dir($_ENV['mccPath'].DIRECTORY_SEPARATOR."logs".DIRECTORY_SEPARATOR)) { mkdir($_ENV['mccPath'].DIRECTORY_SEPARATOR."logs".DIRECTORY_SEPARATOR,0777); } $_ENV['log'] = $_ENV['mccPath'].DIRECTORY_SEPARATOR."logs".DIRECTORY_SEPARATOR."mcc_server.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 */ function manageError($eventInfo, $errorLevel) { $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); echo $errorLevel." ".$eventInfo; 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 index'"; 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; } } /** * Return the file's extention of a file * @param $sFullPath string path of the file */ function extractFileExt($sFullPath) { $sName = $sFullPath; if($sFullPath == "") { $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(!ereg(DIRECTORY_SEPARATOR, $sFullPath)) { return $sFullPath; } else { return substr($sFullPath, strrpos($sFullPath, DIRECTORY_SEPARATOR) +1); } } else { if(!ereg("\\".DIRECTORY_SEPARATOR, $sFullPath)) { return $sFullPath; } else { return substr($sFullPath, strrpos($sFullPath, DIRECTORY_SEPARATOR) +1); } } } /** * Give the number of files in a folder * @param $folder string destination folder */ function scanDestinationFile($folder) { $CountFiles = -1; $ClassScan = dir($folder); while(($filescan=$ClassScan->read())!=false) { if($filescan=='.'||$filescan=='..' ||$filescan=='backup' ||$filescan=='failed' || $filescan == '.svn' || strtoupper(extractFileExt($filescan)) == "XML") { continue; } elseif(!is_dir($folder.$filescan)) { $CountFiles++; } } $CountFiles++; return $CountFiles; } /** * Convert xml object to multidimensional array * @param $xmlObject object XML object */ function xml2array($xmlObject) { $return = NULL; $var = NUll; if(is_array($xmlObject)) { foreach($xmlObject as $key => $value) $return[$key] = xml2array($value); } else { if(is_object($xmlObject)) $var = get_object_vars($xmlObject); if($var) { foreach($var as $key => $value) $return[$key] = xml2array($value); } else return strval($xmlObject); // strval and everything is fine } return $return; } /** * Create xml index file */ function buildXmlIndex() { $Insert_line_value = ''; $Insert_line_value .= "\n\n"; foreach($_ENV['tagFields'] as $key => $value) { if (empty($value)) { $Insert_line_value .= "<".$key.">\n"; } else { if(strstr($value, "'") <> false) { $cleanedValue = str_replace("'", "", $value); switch($cleanedValue) { case '@date' : $today = date('d-m-Y'); $cleanedValue = $today; break; } $Insert_line_value .= "<".$key.">".utf8_encode($cleanedValue)."\n"; } else { if(isset($_REQUEST[$value]) && !empty($_REQUEST[$value])) { $cleanedValue = htmlentities($_REQUEST[$value], ENT_QUOTES); $Insert_line_value .= "<".$key.">".utf8_encode($cleanedValue)."\n"; } else { $Insert_line_value .= "<".$key.">\n"; } } } } $Insert_line_value .= "\n"; return $Insert_line_value; } /** * Loading of the xml config file * @param $conf string path to the config file */ function loadConfig($conf) { //loading of the xml config file. $xmlConfig = simplexml_load_file($conf); $CONFIG =$xmlConfig -> CONFIG; $_ENV['mccPath'] = $CONFIG -> MCC_PATH; loginCreation(); writeLog("Loading xml config file : ".$conf); writeLog("mccPath : ".$_ENV['mccPath']); $_ENV['maarchTmpPath'] = $CONFIG -> MAARCH_TMP_PATH; writeLog("maarchTmpPath : ".$_ENV['maarchTmpPath']); $_ENV['targetPath'] = $CONFIG -> TARGET_PATH; writeLog("targetPath : ".$_ENV['targetPath']); $_ENV['createIndex'] = $CONFIG -> CREATE_INDEX; writeLog("createIndex : ".$_ENV['createIndex']); if ($_ENV['createIndex'] == "true") { $xmlStructureFile = $CONFIG->STRUCTURE_FILE; writeLog("Loading structure xml file : ".$xmlStructureFile); if(!file_exists($xmlStructureFile)) { die ("The read of the structure file has been stopped by Maarch Capture Connector Server (file not exist)\n"); } $xmlStructure = simplexml_load_file($xmlStructureFile); //Get the fields of the xml index file $_ENV['tagFields'] = xml2array($xmlStructure->FIELDS); } $DATABASE =$xmlConfig -> DATABASE; $_ENV['databaseserver'] = $DATABASE->LOCATION; //writeLog("databaseserver : ".$_ENV['databaseserver']); $_ENV['databaseport'] = $DATABASE->DATABASE_PORT; //writeLog("databaseport : ".$_ENV['databaseport']); $_ENV['database'] = $DATABASE ->DATABASE; //writeLog("database : ".$_ENV['database']); $_ENV['databasetype'] = $DATABASE->DATABASETYPE; //writeLog("databasetype : ".$_ENV['databasetype']); $_ENV['databaseuser'] = $DATABASE -> USER_NAME; //writeLog("databaseuser : ".$_ENV['databaseuser']); $_ENV['databasepwd'] = $DATABASE->PASSWORD; //writeLog("databasepwd : ".$_ENV['databasepwd']); $_ENV['databaseworkspace'] = $DATABASE->DATABASEWORKSPACE; //writeLog("databaseworkspace : ".$_ENV['databaseworkspace']); } //begin date_default_timezone_set('Etc/GMT'); $conf = "config".DIRECTORY_SEPARATOR."config_".$_REQUEST['configName'].".xml"; if(!file_exists($conf)) { die ("The read of the configuration file has been stopped by Maarch Capture Connector Server (Xml Error)"); } loadConfig($conf); $debug = false; //date_default_timezone_set($_ENV['timeZone']); if ($debug) { writeLog("DEBUG"); writeLog("configFileName : ".$conf); writeLog("FileName : ".$_FILES['images']['name']); writeLog("FileType : ".$_FILES['images']['type']); writeLog("FileTmpName : ".$_FILES['images']['tmp_name']); writeLog("FileError : ".$_FILES['images']['error']); writeLog("FileSize : ".$_FILES['images']['size']); writeLog("Md5 : ".$_REQUEST['md5']); writeLog("MLB : ".$_REQUEST['mlb']); } //print_r($_FILES); //print_r($_REQUEST); //print_r($_ENV['tagFields']); if(!empty($_FILES['images']['name'])) { if(!is_uploaded_file($_FILES['images']['tmp_name'])) { manageError("No file uploaded !", 1); } //ext $extension = extractFileExt($_FILES['images']['name']); //Mass scan if($_REQUEST['massScan'] == "true") { $nb_file = scanDestinationFile($_ENV['targetPath']); //writeLog("NB files : ".$nb_file); $docName = str_pad($nb_file,4,"0",STR_PAD_LEFT); //nom du fichier $filename = $_ENV['targetPath'].$docName.".".$extension; //writeLog("Filename : ".$filename); if(!move_uploaded_file($_FILES['images']['tmp_name'], $filename)) { manageError("No copy to destination dir : ".$filename, 1); } else { writeLog("Copy to destination dir : ".$filename); //Create Index if ($_ENV['createIndex'] == "true") { writeLog("Creation of the xml file : ".$_ENV['targetPath'].$docName.".xml"); $fichierXml = fopen($_ENV['targetPath'].$docName.".xml","a"); //Creation du fichier xml $xmlValues = buildXmlIndex(); //On genère les index xml fputs($fichierXml, $xmlValues); // on écrit les valeurs récupèrées dans le fichier xml fclose($fichierXml); //On ferme le fichier xml } exit(0); } } else { if($_REQUEST['mlb'] == "true") //name is tmp_file4559d6d2333d4f26f4bb17cf7ab3ca00.pdf { if(!move_uploaded_file($_FILES['images']['tmp_name'], $_ENV['maarchTmpPath']."tmp_file".$_REQUEST['md5'].".".$extension)) { manageError("No copy to tmp dir : ".$_ENV['maarchTmpPath']."tmp_file".$_REQUEST['md5'].'.'.$extension, 1); } else { writeLog("Copy to tmp dir : ".$_ENV['maarchTmpPath']."tmp_file".$_REQUEST['md5'].'.'.$extension); exit(0); } } else { //The name is different if it was for entreprise (tmp_file_4559d6d2333d4f26f4bb17cf7ab3ca00.pdf) if(!move_uploaded_file($_FILES['images']['tmp_name'], $_ENV['maarchTmpPath']."tmp_file_".$_REQUEST['md5'].".".$extension)) { manageError("No copy to tmp dir : ".$_ENV['maarchTmpPath']."tmp_file_".$_REQUEST['md5'].'.'.$extension, 1); } else { writeLog("Copy to tmp dir : ".$_ENV['maarchTmpPath']."tmp_file_".$_REQUEST['md5'].'.'.$extension); exit(0); } } } } exit(0); ?>