From fbbd02e749e786464e407743b9d43590c925e044 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Fri, 7 Mar 2014 16:44:18 +0000 Subject: [PATCH] Traitement avec les fichiers .fin ou .end --- fileSend.php | 136 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 84 insertions(+), 52 deletions(-) diff --git a/fileSend.php b/fileSend.php index 9eb6a84..df95bb0 100644 --- a/fileSend.php +++ b/fileSend.php @@ -54,31 +54,31 @@ if( isset($opts->help) || count($opts->getOptions())==0 ) //Get the file if ( isset($opts->file) ) { - $c = new Zend_Config($application->getOptions()); + $c = new Zend_Config($application->getOptions()); + + $dateFile = date('YmdHis', filectime($opts->file)); //Get the main directory name in FTP and SFTP $pathParts = pathinfo($opts->file); - - $filename = $pathParts['basename']; + $filenameIn = $pathParts['basename']; $extension = ''; if (array_key_exists('extension', $pathParts) ){ $extension = $pathParts['extension']; } $client = basename(dirname($pathParts['dirname'])); - $dateFile = date('YmdHis', filectime($opts->file)); $startpos = strlen( $c->profil->path->data . DIRECTORY_SEPARATOR ); - if ( 'sftp' == substr($opts->file,$startpos,4) ) { + if ( 'sftp' == substr($opts->file, $startpos, 4) ) { $type = 'SFTP'; - } elseif ( 'ftp' == substr($opts->file,$startpos,3) ) { + $fluxBasePath = $c->profil->path->sftp . '/' . $client; + } elseif ( 'ftp' == substr($opts->file, $startpos, 3) ) { $type = 'FTP'; + $fluxBasePath = $c->profil->path->ftp . '/' . $client; } - $lines = file($opts->file); - $nbLines = count($lines); - - $copyOptionsAddDate = false; - $copyOptionsDeleteAfter = false; + $OptionsCopyAddDate = false; + $OptionsCopyDeleteAfter = false; + $OptionsRunWithEndFile = false; /** * @todo : links to prestation table and file rules @@ -92,20 +92,48 @@ if ( isset($opts->file) ) break; case 'gefacto': $prestation = 'GEFACTO'; - $copyOptionsAddDate = true; + $OptionsCopyAddDate = true; break; } - if ($copyOptionsAddDate) { - $extensionLength = 0; - if ($extension!='') { - $extensionLength = strlen($extension)+1; - $filename = substr($filename, 0, strlen($filename)-$extensionLength); - $filename = $filename . '_' . date('YmdHis') . '.' . $extension; - } else { - $filename = $filename . '_' . date('YmdHis'); - } + //Use ".fin" or ".end" files to do something + $runExtensions = array('fin', 'end'); + if ( in_array( $extension, $runExtensions) ) { + if ( $OptionsRunWithEndFile ) { + $extToDelete = $extension; + if (file_exists($fluxBasePath . '/' . $filenameIn)) { + $pathParts = pathinfo($fluxBasePath . '/' . $filenameIn); + $filenameIn = $pathParts['basename']; + $extension = ''; + if (array_key_exists('extension', $pathParts) ){ + $extension = $pathParts['extension']; + } + } else { + echo "Fichier inexistant $filenameIn\n"; + exit; + } + } else { + exit; + } + } + if ( $OptionsRunWithEndFile && !in_array( $extension, $runExtensions) ) { + exit; + } + + $lines = file($fluxBasePath . '/' . $filenameIn); + $nbLines = count($lines); + + //Add date to filename + if ( $OptionsCopyAddDate ) { + $extensionLength = 0; + if ( $extension != '' ) { + $extensionLength = strlen($extension)+1; + $filenameOut = substr($filenameIn, 0, strlen($filenameIn)-$extensionLength); + $filenameOut = $filenameOut . '_' . date('YmdHis') . '.' . $extension; + } else { + $filenameOut = $filenameIn . '_' . date('YmdHis'); + } } //Prepare mail @@ -115,7 +143,7 @@ if ( isset($opts->file) ) $txt = "Réception d'un fichier pour traitement\n"; $txt.= "Client : $client\n"; $txt.= "Mode de transmission : $type\n"; - $txt.= "Fichier : ".$opts->file."\n"; + $txt.= "Fichier : ".$filenameIn."\n"; $txt.= "Nombre de Lignes : $nbLines\n"; $mail = new Zend_Mail('UTF-8'); @@ -134,43 +162,47 @@ if ( isset($opts->file) ) } - //Execute - $db = Zend_Db::factory($c->profil->db->metier); - Zend_Db_Table::setDefaultAdapter($db); - try { - $fluxM = new Application_Model_Sdv1FluxFileIn(); - $fluxM->insert(array( - 'client' => $client, - 'name' => $prestation, - 'depotType' => $type, - 'depotDate' => $dateFile, - 'depotFile' => $filename, - 'nbLines' => $nbLines, - 'dateInsert' => date('YmdHis'), - 'dateExecute' => '0000-00-00 00:00:00', - )); - echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filename\n"; - } catch (Zend_Db_Exception $e) { - echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filename\n"; - } - //Copie du fichier - $destDir = $c->profil->path->storage . - DIRECTORY_SEPARATOR . $client . - DIRECTORY_SEPARATOR . 'send'; + $destDir = $c->profil->path->storage . '/' . $client . '/' . 'send'; if ( !is_dir($destDir) ) { - mkdir($destDir, 0755, true); + mkdir($destDir, 0755, true); } - if ( copy($opts->file, $destDir. DIRECTORY_SEPARATOR . $filename) ) { - echo date('Y-m-d H:i:s')." - Copie du fichier $filename dans $destDir\n"; + if ( copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut) ) { + echo date('Y-m-d H:i:s')." - Copie du fichier $filenameIn dans $destDir\n"; - if ($copyOptionsDeleteAfter) { - unlink($opts->file); - } + //Execute + $db = Zend_Db::factory($c->profil->db->metier); + Zend_Db_Table::setDefaultAdapter($db); + try { + $fluxM = new Application_Model_Sdv1FluxFileIn(); + $fluxM->insert(array( + 'client' => $client, + 'name' => $prestation, + 'depotType' => $type, + 'depotDate' => $dateFile, + 'depotFile' => $filenameOut, + 'nbLines' => $nbLines, + 'dateInsert' => date('YmdHis'), + 'dateExecute' => '0000-00-00 00:00:00', + )); + echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filenameOut\n"; + } catch (Zend_Db_Exception $e) { + echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filenameOut\n"; + } + + //Suppression des fichiers + if ( $OptionsCopyDeleteAfter ) { + unlink( $fluxBasePath . '/' . $filenameIn ); + + if ( $OptionsRunWithEndFile ) { + unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete ); + } + } } else { - echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filename dans $destDir\n"; + echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filenameIn dans $destDir\n"; } + } \ No newline at end of file