. */ /** * @defgroup mcc Maarch Capture Connector */ /** * mcc_client = client for Maarch Capture Connector * * @file * @author Laurent Giovanonni * @date $date$ * @version $Revision$ * @ingroup mcc * @brief mcc_client, client for Maarch Capture Connector */ if (!class_exists('gtk')) { die("Please load the php-gtk2 module in your php.ini\r\n"); } //Error mode and function error_reporting(E_ERROR); set_error_handler(errorHandler); //Global var of the program /** * maarch capture connector path */ $_ENV['mccPath'] = ""; /** * scan import path */ $_ENV['scanImportPath'] = ""; /** * core name of maarch */ $_ENV['maarchCoreName'] = ""; /** * url of maarch */ $_ENV['maarchUrl'] = ""; /** * url of upload file */ $_ENV['uploadUrl'] = ""; /** * url to the ws url */ $_ENV['getDatasUrl'] = ""; /** * form mode flag */ $_ENV['formMode'] = "false"; /** * maarch user name */ $_ENV['userId'] = ""; /** * mass scan flag */ $_ENV['massScan'] = "false"; /** * image format */ $_ENV['imageFormat'] = ""; /** * is for MLB flag */ $_ENV['isForMlb'] = "false"; /** * 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'] = ""; /** * path to the file to process in no form mode */ $_ENV['fileToProcess'] = ""; /** * ssl certificate */ $_ENV['sslCert'] = ""; /** * web port */ $_ENV['webPort'] = ""; /** * path of the browser */ $_ENV['browserPath'] = ""; /** * url proxy */ $_ENV['urlProxy'] = ""; /** * user proxy */ $_ENV['userProxy'] = ""; /** * proxy authentification type */ $_ENV['authTypeProxy'] = ""; /** * proxy type */ $_ENV['typeProxy'] = ""; /** * 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_client.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 offset: 2'"; preg_match($search1,$string,$out); $count=count($out[0]); if($count == 1) { $find1 = true; } preg_match($search2,$string,$out); $count=count($out[0]); if($count == 1) { $find2 = true; } if(!$find1 && !$find2) { manageError("from line ".$errline." : ". $errstr, 1); $_ENV['errorLevel'] = 1; } } /** * 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) { 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); } } } /** * 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['scanImportPath'] = $CONFIG -> SCAN_IMPORT_PATH; writeLog("scanImportPath : ".$_ENV['scanImportPath']); $_ENV['maarchCoreName'] = $CONFIG -> MAARCH_CORENAME; writeLog("maarchCoreName : ".$_ENV['maarchCoreName']); $_ENV['maarchUrl'] = str_replace("&", "&", $CONFIG -> MAARCH_URL); writeLog("maarchUrl : ".$_ENV['maarchUrl']); $_ENV['uploadUrl'] = $CONFIG -> UPLOAD_URL; writeLog("uploadUrl : ".$_ENV['uploadUrl']); $_ENV['getDatasUrl'] = $CONFIG -> GET_DATAS_URL; writeLog("getDatasUrl : ".$_ENV['getDatasUrl']); $_ENV['sslCert'] = $CONFIG -> SSL_CERT; writeLog("sslCert : ".$_ENV['sslCert']); $_ENV['webPort'] = $CONFIG -> WEB_PORT; writeLog("webPort : ".$_ENV['webPort']); $_ENV['browserPath'] = $CONFIG -> BROWSER_PATH; writeLog("browserPath : ".$_ENV['browserPath']); $_ENV['urlProxy'] = $CONFIG -> URL_PROXY; writeLog("urlProxy : ".$_ENV['urlProxy']); $_ENV['userProxy'] = $CONFIG -> USER_PROXY; writeLog("userProxy : ".$_ENV['userProxy']); $_ENV['authTypeProxy'] = $CONFIG -> AUTH_TYPE_PROXY; writeLog("authTypeProxy : ".$_ENV['authTypeProxy']); $_ENV['typeProxy'] = $CONFIG -> TYPE_PROXY; writeLog("typeProxy : ".$_ENV['typeProxy']); $FORM =$xmlConfig -> FORM; $_ENV['formMode'] = $FORM -> FORM_MODE; writeLog("formMode : ".$_ENV['formMode']); $_ENV['userId'] = $FORM -> USER_ID; writeLog("userId : ".$_ENV['userId']); $_ENV['massScan'] = $FORM -> MASS_SCAN; writeLog("massScan : ".$_ENV['massScan']); $_ENV['imageFormat'] = $FORM -> IMAGE_FORMAT; writeLog("imageFormat : ".$_ENV['imageFormat']); $_ENV['isForMlb'] = $FORM -> IS_FOR_MLB; writeLog("isForMlb : ".$_ENV['isForMlb']); } /** * Browse each file and folder in the import folder and return true if the process have to continue */ function verifInputFolder() { $folder = $_ENV['scanImportPath']; $foundDoc = false; $classScan= dir($folder); while(($fileScan=$classScan->read())!=false) { if($foundDoc) { return $foundDoc; } if($fileScan=='.'||$fileScan=='..' ||$fileScan=='backup' ||$fileScan=='failed' || $fileScan == '.svn') { 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 { if(strtoupper(extractFileExt($subFileScan)) == $_ENV['imageFormat'] || $_ENV['imageFormat'] == "ALL") { $foundDoc = true; } else { continue; } } } } else { if(strtoupper(extractFileExt($fileScan)) == $_ENV['imageFormat'] || $_ENV['imageFormat'] == "ALL") { $foundDoc = true; } else { continue; } } } return $foundDoc; } /** * Browse each file and folder in the import folder and process each document * @param str $arg1 The comboEntity combo, used to get the entity * @param str $arg2 The comboPriority combo to retrieve the priority */ function processInputFolder($arg1 = "", $arg2 = "") { $folder = $_ENV['scanImportPath']; $classScan= dir($folder); while(($fileScan=$classScan->read())!=false) { if($fileScan=='.'||$fileScan=='..' ||$fileScan=='backup' ||$fileScan=='failed' || $fileScan == '.svn') { 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 { if(strtoupper(extractFileExt($subFileScan)) == $_ENV['imageFormat'] || $_ENV['imageFormat'] == "ALL") { //Process doc $_ENV['fileToProcess'] = $_ENV['scanImportPath'].DIRECTORY_SEPARATOR.$fileScan.DIRECTORY_SEPARATOR.$subFileScan; sendDocument($arg1, $arg2); } else { continue; } } } } else { if(strtoupper(extractFileExt($fileScan)) == $_ENV['imageFormat'] || $_ENV['imageFormat'] == "ALL") { //Process doc $_ENV['fileToProcess'] = $_ENV['scanImportPath'].DIRECTORY_SEPARATOR.$fileScan; sendDocument($arg1, $arg2); } else { continue; } } } } /** * This function gets called as soon as the user clicks on the launch Maarch button. * @param GtkWindow $wnd The login window, needed to close it when all is ok * @param GtkComboBox $comboEntity The comboEntity combo, used to get the entity * @param GtkEntry $comboPriority The comboPriority combo to retrieve the priority */ function launchMaarchWithForm(GtkWindow $wnd, GtkComboBox $comboEntity, GtkComboBox $comboPriority) { //fetch the values from the widgets into variables $strEntity = $comboEntity->get_active_text(); $strPriority = $comboPriority->get_active_text(); //Do some error checking $errors = null; if(strlen($strEntity) == 0) { $errors .= "Entity is missing.\r\n"; } if(strlen($strPriority) == 0) { $errors .= "No priority given.\r\n"; } if($errors !== null) { //There was at least one error. //We show a message box with the errors $dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $errors); $dialog->set_markup( "The following errors occured:\r\n" . "" . $errors . "" ); $dialog->run(); $dialog->destroy(); } else { if($_ENV['massScan'] == "true") { if(verifInputFolder()) { processInputFolder($strEntity, $strPriority); } else { //There was at least one error. //We show a message box with the errors $dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, $errors); $dialog->set_markup( "The following errors occured:\r\n" . "No file to process in ".$_ENV['scanImportPath']."" ); $dialog->run(); $dialog->destroy(); } } elseif($_ENV['massScan'] == "false") { sendDocument($strEntity, $strPriority); } $wnd->destroy(); } } /** * This function gets called as soon as the user * clicks on the launch Maarch button. * @param GtkWindow $wnd The login window, needed to close it when all is ok */ function launchMaarch(GtkWindow $wnd) { $wnd->destroy(); } /** * Creation of a window with a form */ function createWindowFormMode() { //Create the login window $wnd = new GtkWindow(); $wnd->set_title('Maarch Capture Connector'); //Close the main loop when the window is destroyed $wnd->connect_simple('destroy', array('gtk', 'main_quit')); //Set up all the widgets we need $lblCredit = new GtkLabel('Please provide your data'); //The second parameter says that the underscore should be parsed as underline $lblEntity = new GtkLabel('_Entity', true); $lbPriority = new GtkLabel('_Priority', true); $comboEntity = new GtkComboBox(); $comboPriority = new GtkComboBox(); $btnLaunchMaarch = new GtkButton('_Launch Maarch'); $btnCancel = new GtkButton('_Cancel'); //add an image $img = GtkImage::new_from_file($_ENV['mccPath'].DIRECTORY_SEPARATOR.'mcc_logo.gif'); //Create a model $listStoreEntity = new GtkListStore(Gobject::TYPE_STRING); $listStorePriority = new GtkListStore(Gobject::TYPE_STRING); $tabEntities = array(); $tabEntities = retrieveEntities(); //Add some values for($cptEntities=0;$cptEntitiesappend(array(utf8_decode($tabEntities[$cptEntities]))); } $listStorePriority->append(array('Priorité trés haute [0]')); $listStorePriority->append(array('Priorité haute [1]')); $listStorePriority->append(array('Priorité normale [2]')); $listStorePriority->append(array('Priorité basse [3]')); //Set the model for the combo $comboEntity->set_model($listStoreEntity); $comboPriority->set_model($listStorePriority); //Create a cell renderer $cellRenderer = new GtkCellRendererText(); //Pack the cell renderer into the combo $comboEntity->pack_start($cellRenderer); $comboPriority->pack_start($cellRenderer); //Tell the combo where to get the text value of the cell renderer $comboEntity->set_attributes($cellRenderer, 'text', 0); $comboPriority->set_attributes($cellRenderer, 'text', 0); //Which widget should be activated when the //mnemonic (Alt+U or Alt+P) is pressed? //$lblEntity->set_mnemonic_widget($comboEntity); $lblEntity->set_mnemonic_widget($comboEntity); $lbPriority->set_mnemonic_widget($comboPriority); //Destroy the window when the user clicks Cancel $btnCancel->connect_simple('clicked', array($wnd, 'destroy')); //Call the launchMaarchWithForm function when the user clicks on click $btnLaunchMaarch->connect_simple('clicked', 'launchMaarchWithForm', $wnd, $comboEntity, $comboPriority); //Lay out all the widgets in the table $tbl = new GtkTable(3, 2); $tbl->attach($img, 0, 2, 0, 1); $tbl->attach($lblEntity, 0, 1, 1, 2); $tbl->attach($comboEntity, 1, 2, 1, 2); $tbl->attach($lbPriority, 0, 1, 2, 3); $tbl->attach($comboPriority, 1, 2, 2, 3); //Add the buttons to a button box $bbox = new GtkHButtonBox(); $bbox->set_layout(Gtk::BUTTONBOX_EDGE); $bbox->add($btnCancel); $bbox->add($btnLaunchMaarch); //Add the table and the button box to a vbox $vbox = new GtkVBox(); $vbox->pack_start($tbl); $vbox->pack_start($bbox); //Add the vbox to the window $wnd->add($vbox); //Show all widgets $wnd->show_all(); } /** * Creation of a simple window */ function createWindow() { //Create the login window $wnd = new GtkWindow(); $wnd->set_title('Maarch Capture Connector'); //Close the main loop when the window is destroyed $wnd->connect_simple('destroy', array('gtk', 'main_quit')); //Set up all the widgets we need $btnLaunchMaarch = new GtkButton('_Launch Maarch'); $btnCancel = new GtkButton('_Cancel'); //add an image $img = GtkImage::new_from_file($_ENV['mccPath'].DIRECTORY_SEPARATOR.'mcc_logo.gif'); //Destroy the window when the user clicks Cancel $btnCancel->connect_simple('clicked', array($wnd, 'destroy')); //Call the launchMaarch function when the user clicks on click $btnLaunchMaarch->connect_simple('clicked', 'launchMaarch', $wnd); //Lay out all the widgets in the table $tbl = new GtkTable(3, 2); $tbl->attach($img, 0, 2, 0, 1); //Add the buttons to a button box $bbox = new GtkHButtonBox(); $bbox->set_layout(Gtk::BUTTONBOX_EDGE); $bbox->add($btnCancel); $bbox->add($btnLaunchMaarch); //Add the table and the button box to a vbox $vbox = new GtkVBox(); $vbox->pack_start($tbl); $vbox->pack_start($bbox); //Add the vbox to the window $wnd->add($vbox); //Show all widgets $wnd->show_all(); } /** * Retrieve entities with curl api */ function retrieveEntities() { require_once('class_http_request.php'); try { $http = new HTTPQuery($_ENV['getDatasUrl']); //fields of the form $http->addPostData('isForMlb[0]', $_ENV['isForMlb']); if($_ENV['webPort'] <> "") { $http->CURLOPT_PORT = "'".$_ENV['webPort']."'"; } if($_ENV['userId'] <> "") { $http->addPostData('userId[0]', $_ENV['userId']); } if(file_exists($_ENV['sslCert'])) { //SSL verif with certificate on the server writeLog("Use certif : ".$_ENV['sslCert']); $http->CURLOPT_CAINFO = $_ENV['sslCert']; $http->CURLOPT_SSL_VERIFYPEER = FALSE; } if($_ENV['urlProxy'] <> "" ) { $http->CURLOPT_RETURNTRANSFER = 1; $http->CURLOPT_HTTPPROXYTUNNEL = 1; $http->CURLOPT_PROXY = "'".$_ENV['urlProxy']."'"; if($_ENV['userProxy'] <> "") { $http->CURLOPT_PROXYUSERPWD = "'".$_ENV['userProxy']."'"; } if($_ENV['authTypeProxy'] <> "") { $http->CURLOPT_PROXYAUTH = "'".$_ENV['authTypeProxy']."'"; } if($_ENV['typeProxy'] <> "") { $http->CURLOPT_PROXYTYPE = "'".$_ENV['typeProxy']."'"; } } //the request $response = $http->doRequest(); //echo $response; $entities = array(); $entities = explode("$$$$", $response); //print_r($entities); return $entities; } catch (Exception $e) { die($e->getMessage()); } } function washValue($strValue) { $result = ""; $result = explode("[", $strValue); return substr($result[count($result) - 1], 0, strlen($result[count($result) - 1]) -1); } /** * Sending document with curl api */ function sendDocument($arg1 = "", $arg2 = "") { require_once('class_http_request.php'); try { $http = new HTTPQuery($_ENV['uploadUrl']); //retrieve on server with $_FILES['images']['name'][0] $http->addPostFile('images[0]', $_ENV['fileToProcess']); //fields of the form $http->addPostData('md5[0]', md5_file($_ENV['fileToProcess'])); if($_ENV['isForMlb'] == "true") { $http->addPostData('mlb[0]', "true"); } else { $http->addPostData('mlb[0]', "false"); } if($arg1 <> "") { $arg1 = washValue($arg1); $http->addPostData('arg1[0]', $arg1); } if($arg2 <> "") { $arg2 = washValue($arg2); $http->addPostData('arg2[0]', $arg2); } if($_ENV['webPort'] <> "") { $http->CURLOPT_PORT = "'".$_ENV['webPort']."'"; } if(file_exists($_ENV['sslCert'])) { //SSL verif with certificate on the server $http->CURLOPT_CAINFO = $_ENV['sslCert']; $http->CURLOPT_SSL_VERIFYPEER = FALSE; } if($_ENV['urlProxy'] <> "" ) { $http->CURLOPT_RETURNTRANSFER = 1; $http->CURLOPT_HTTPPROXYTUNNEL = 1; $http->CURLOPT_PROXY = "'".$_ENV['urlProxy']."'"; if($_ENV['userProxy'] <> "") { $http->CURLOPT_PROXYUSERPWD = "'".$_ENV['userProxy']."'"; } if($_ENV['authTypeProxy'] <> "") { $http->CURLOPT_PROXYAUTH = "'".$_ENV['authTypeProxy']."'"; } if($_ENV['typeProxy'] <> "") { $http->CURLOPT_PROXYTYPE = "'".$_ENV['typeProxy']."'"; } } //the request echo $http->doRequest(); unlink($_ENV['fileToProcess']); } catch (Exception $e) { manageError($e->getMessage(), 1); die($e->getMessage()); } } /** * Retrieve properties of the sending file and prepare the url */ function retrieveProperties() { $extension = extractFileExt($_ENV['fileToProcess']); $taille_fichier = filesize($_ENV['fileToProcess']); $md5 = md5_file($_ENV['fileToProcess']); $scan_user = "user"; $scan_wkstation = "pc"; $tmp_file = "tmp_file.".$extension; $index_type = "num"; $Corename = $_ENV['maarchCoreName']; if($_ENV['isForMlb'] == "true") { $Ftp_File = "tmp_file.".$extension; $params = "?extension=".$extension."&taille_fichier=".$taille_fichier."&Ftp_File=".$Ftp_File."&md5=".$md5."&scan_user=".$scan_user."&scan_wkstation=".$scan_wkstation."&tmp_file=".$tmp_file."&index_type=".$index_type."&Corename=".$Corename; } else { $Ftp_File = "tmp_file_".md5_file($_ENV['fileToProcess']).".".$extension; $params = "&extension=".$extension."&taille_fichier=".$taille_fichier."&Ftp_File=".$Ftp_File."&md5=".$md5."&scan_user=".$scan_user."&scan_wkstation=".$scan_wkstation."&tmp_file=".$tmp_file."&index_type=".$index_type."&Corename=".$Corename; } return $params; } //begin date_default_timezone_set('Europe/Paris'); if($argc < 2 ) { die("You must specify the configuration file\r\n"); } else { $conf = $argv[1]; if(!file_exists($conf)) { die ("The read of the configuration file has been stopped by Maarch Auto Import (Xml Error)"); } loadConfig($conf); if($_ENV['formMode'] == "true") { if($_ENV['massScan'] == "false") { if($argv[2] <> "") { $_ENV['fileToProcess'] = $argv[2]; writeLog("fileToProcess : ".$_ENV['fileToProcess']); if(file_exists($_ENV['fileToProcess'])) { createWindowFormMode(); //Start the main loop Gtk::main(); } else { manageError("File not exists : ".$_ENV['fileToProcess'], 1); } } else { manageError("No file to process in argument", 1); } } elseif($_ENV['massScan'] == "true") { sleep(2); if(is_dir($_ENV['scanImportPath'])) { createWindowFormMode(); //Start the main loop Gtk::main(); } else { manageError("No folder to process", 1); } } } else { if($argv[2] <> "") { $_ENV['fileToProcess'] = $argv[2]; writeLog("fileToProcess : ".$_ENV['fileToProcess']); if(file_exists($_ENV['fileToProcess'])) { $urlParams = retrieveProperties($_ENV['fileToProcess']); sendDocument(); writeLog("\"".$_ENV['browserPath']."\" \"".$_ENV['maarchUrl'].$urlParams."\""); $command = " start \"".$_ENV['browserPath']."\" \"".$_ENV['maarchUrl'].$urlParams."\""; $batFile = $_ENV["mccPath"].DIRECTORY_SEPARATOR."os_scripts".DIRECTORY_SEPARATOR."tmp_script.bat"; $inF = fopen($batFile,"w"); fwrite($inF,$command); fclose($inF); $converter_result = exec($batFile, $return_var); //unlink($batFile); } else { manageError("File not exists : ".$_ENV['fileToProcess'], 1); } } else { manageError("No file to process in argument", 1); } } } exit(0); ?>