From 85fcc8eaa55774e3e8bbbbfcf721472ecf8896e0 Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Wed, 1 Oct 2014 08:59:58 +0000 Subject: [PATCH] SEND : Plus de souplesse dans la gestion --- README | 26 +++++++ fileConfig.php | 36 ++++++++++ fileSend.php | 151 ++++++++++++++++++++++++++------------- scripts/build/incron.php | 101 ++++++++++++++++++++++++++ 4 files changed, 263 insertions(+), 51 deletions(-) create mode 100644 README create mode 100644 fileConfig.php create mode 100644 scripts/build/incron.php diff --git a/README b/README new file mode 100644 index 0000000..22af4c1 --- /dev/null +++ b/README @@ -0,0 +1,26 @@ +Mise à disposition fichier de télétransmission +---------------------------------------------- + + +Configuration pour les fichiers en entrées +------------------------------------------ + +'client' => array( + 'prestations' => array( + array( + 'name' => Nom de la prestation pour référence (UPPERCASE) + 'type' => Type du dépot SFTP|FTP + 'directory' => Répertoire de dépot (par defaut "send") + 'filemask' => Masque du fichier à récupérer ! not working + 'options' => array( + --See the list of options-- + ), + ), + ), +), + +Options list +------------ +- CopyAddDate - Copy file and add a timestamp to the end of the filename +- CopyDeleteAfter - After copying the file delete it in repository +- RunWithEndFile - Only execute action when we receive file with extension .fin or .end diff --git a/fileConfig.php b/fileConfig.php new file mode 100644 index 0000000..c16e83f --- /dev/null +++ b/fileConfig.php @@ -0,0 +1,36 @@ + array( + 'prestations' => array( + array( + 'name' => 'FICH_RCE', + 'type' => 'SFTP', + 'options' => array(), + ), + ), + ), + 'gefacto' => array( + 'prestations' => array( + array( + 'name' => 'GEFACTO', + 'type' => 'SFTP', + 'options' => array( + 'RunWithEndFile' => true, + 'CopyAddDate' => true, + 'CopyDeleteAfter' => true, + ), + ), + ), + ), + 'fransbonhomme' => array( + 'prestations' => array( + array( + 'name' => 'RAPPORT', + 'type' => 'FTP', + 'options' => array( + 'CopyDeleteAfter' => true, + ), + ), + ), + ), +); diff --git a/fileSend.php b/fileSend.php index 80c4f15..b28305f 100644 --- a/fileSend.php +++ b/fileSend.php @@ -14,8 +14,8 @@ defined('APPLICATION_ENV') // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( - realpath(APPLICATION_PATH . '/../library'), - get_include_path(), + realpath(APPLICATION_PATH . '/../library'), + get_include_path(), ))); /** Zend_Application */ @@ -54,6 +54,11 @@ if( isset($opts->help) || count($opts->getOptions())==0 ) //Get the file if ( isset($opts->file) ) { + $optionsCopyAddDate = false; + $optionsCopyDeleteAfter = false; + $optionsRunWithEndFile = false; + $repositoryDir = 'send'; + $c = new Zend_Config($application->getOptions()); $dateFile = date('YmdHis', filectime($opts->file)); @@ -62,72 +67,105 @@ if ( isset($opts->file) ) $pathParts = pathinfo($opts->file); $filenameIn = $pathParts['basename']; $extension = ''; - if (array_key_exists('extension', $pathParts) ){ + if (array_key_exists('extension', $pathParts)) + { $extension = $pathParts['extension']; } $client = basename(dirname($pathParts['dirname'])); + // Base path, type and repository $startpos = strlen( $c->profil->path->data . DIRECTORY_SEPARATOR ); - if ( 'sftp' == substr($opts->file, $startpos, 4) ) { + if ('sftp' == substr($opts->file, $startpos, 4)) + { $type = 'SFTP'; $fluxBasePath = $c->profil->path->sftp . '/' . $client; - } elseif ( 'ftp' == substr($opts->file, $startpos, 3) ) { + } + elseif ('ftp' == substr($opts->file, $startpos, 3)) + { $type = 'FTP'; $fluxBasePath = $c->profil->path->ftp . '/' . $client; } + $fluxRepository = str_replace(array($fluxBasePath.'/', '/'.$filenameIn), array('', ''), $opts->file); - $fluxBasePath .= '/send'; + // Match prestation + $prestations = include __DIR__ . '/fileConfig.php'; + $prestation = null; + if (array_key_exists($client, $prestations)) + { + $clientPrestations = $prestations[$client]['prestations']; + foreach ($clientPrestations as $i => $item) + { + // Not default repository dir + if (array_key_exists('directory', $item) && !empty($item['directory'])) + { + $repositoryDir = $item['directory']; + } - $OptionsCopyAddDate = false; - $OptionsCopyDeleteAfter = false; - $OptionsRunWithEndFile = false; + if ($type == $item['type'] && $fluxRepository == $repositoryDir) + { + $prestation = $item['name']; - /** - * @todo : links to prestation table and file rules - * Prestation => Repertoire reception, etc - */ - switch ($client) { - case 'sfrbtr' : - $prestation = 'FICH_RCE'; - $filemask = array( - 'FICH_RCE_SCORE_.*.csv', - ); - break; - case 'gefacto': - $prestation = 'GEFACTO'; - $OptionsCopyAddDate = true; - $OptionsRunWithEndFile = true; - break; - } + // Set option + if (array_key_exists('options', $item) && count($item['options']) > 0) + { + foreach ($item['options'] as $option => $value) + { + ${'options'.$option} = $value; + } + } + + break; + } + } + } + + if ($prestation === null) + { + echo date('Y-m-d H:i:s')." - Prestation not found !\n"; + } + + $fluxBasePath .= '/'.$repositoryDir; //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)) { + 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) ){ + if (array_key_exists('extension', $pathParts)) + { $extension = $pathParts['extension']; } - } else { + } + else + { echo "Fichier inexistant $filenameIn\n"; exit; } - } else { + } + else + { exit; } } //Get the realname of file IN or exit - if ( $OptionsRunWithEndFile ) { - if ( in_array( $extension, $runExtensions) ) { + if ($optionsRunWithEndFile) + { + if (in_array( $extension, $runExtensions)) + { $extensionLength = strlen($extension)+1; $filenameIn = substr($filenameIn, 0, strlen($filenameIn) - $extensionLength); $extension = ''; - } else { + } + else + { exit; } } @@ -139,20 +177,24 @@ if ( isset($opts->file) ) $filenameOut = $filenameIn; //Add date to filename - if ( $OptionsCopyAddDate ) { + if ($optionsCopyAddDate) + { $extensionLength = 0; - if ( $extension != '' ) { + if ($extension != '') + { $extensionLength = strlen($extension)+1; $filenameOut = substr($filenameIn, 0, strlen($filenameIn) - $extensionLength); $filenameOut = $filenameOut . '_' . date('YmdHis') . '.' . $extension; - } else { + } + else + { $filenameOut = $filenameIn . '_' . date('YmdHis'); } } //Prepare mail - if ($opts->mail || $opts->debug) { - + if ($opts->mail || $opts->debug) + { $subject = "[Flux] - Réception fichier $client"; $txt = "Réception d'un fichier pour traitement\n"; $txt.= "Client : $client\n"; @@ -170,20 +212,22 @@ if ( isset($opts->file) ) $mail->send(); //Stop - if ($opts->mail) { + if ($opts->mail) + { exit; } - } - //Copie du fichier + // Copy file $destDir = $c->profil->path->storage . '/' . $client . '/' . 'send'; - if ( !is_dir($destDir) ) { + if (!is_dir($destDir)) + { mkdir($destDir, 0755, true); } - if ( copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut) ) { + if (copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut)) + { echo date('Y-m-d H:i:s')." - Copie du fichier $filenameIn dans $destDir\n"; //Execute @@ -202,21 +246,26 @@ if ( isset($opts->file) ) '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) { + } + 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 ) { + if ($optionsCopyDeleteAfter) + { unlink( $fluxBasePath . '/' . $filenameIn ); - if ( $OptionsRunWithEndFile ) { + if ($optionsRunWithEndFile) + { unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete ); } } - } else { + } + else + { echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filenameIn dans $destDir\n"; } - } \ No newline at end of file diff --git a/scripts/build/incron.php b/scripts/build/incron.php new file mode 100644 index 0000000..d38f4e7 --- /dev/null +++ b/scripts/build/incron.php @@ -0,0 +1,101 @@ + "Display usage information.", + 'list' => "List client and prestations.", + 'generate-s' => "Generate incron file [client]/[name]", + ) + ); + $opts->parse(); +} catch (Zend_Console_Getopt_Exception $e) { + echo $e->getUsageMessage(); + exit; +} + +//Usage +if(isset($opts->help)) +{ + echo $opts->getUsageMessage(); + exit; +} + +$prestations = include APPLICATION_PATH . '/../fileConfig.php'; + +if ($opts->list) +{ + if (count($prestations)>0) + { + foreach ($prestations as $client => $params) + { + echo "Client : ".$client."\n"; + foreach ($params['prestations'] as $i => $item) + { + echo " "; + echo $item['name'] . " : ".$item['type']; + if (array_key_exists('directory', $item)) { + echo " - " .$item['directory']; + } + echo "\n"; + } + echo "\n"; + } + } +} + +if ($opts->generate) +{ + echo "Generate.\n"; + + $input = explode('/', $opts->generate); + $client = $input[0]; + $name = $input[1]; + if (array_key_exists($client, $prestations)) + { + foreach ($prestations[$client]['prestations'] as $item) + { + if ($item['name'] == $name) + { + $directory = 'send'; + if (array_key_exists('directory', $item)) + { + $directory = $item['directory']; + } + $fluxBasePath = '/home/data/' . strtolower($item['type']) . '/' . $client . '/' . $directory; + $file = __DIR__ . '/incron/' . strtolower($item['type']) . '_' . $client; + file_put_contents($file, "$fluxBasePath IN_CLOSE_WRITE php /home/batchFlux/fileSend.php --file $@/$#"); + echo "File created $file\n"; + } + } + } + + echo "Put the file to /etc/incron.d and restart incron.\n"; + echo "Generate Ended.\n"; +} + + +