From fc25a8c591ab3efb8df6e472b207879fdbe68f5e Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Tue, 27 Aug 2013 10:27:03 +0000 Subject: [PATCH] Ajout d'options et documentation --- docs/README | 64 ++++++++++++++++++++++++++++ fileSend.php | 81 ++++++++++++++++++++++++++---------- scripts/build/incron/gefacto | 1 + scripts/build/incron/sfr | 1 + 4 files changed, 124 insertions(+), 23 deletions(-) create mode 100644 scripts/build/incron/gefacto create mode 100644 scripts/build/incron/sfr diff --git a/docs/README b/docs/README index e69de29..855eac5 100644 --- a/docs/README +++ b/docs/README @@ -0,0 +1,64 @@ + +INSTALLATION +============ + + +DOCUMENTATION +============= + + + + +INCRON DOCUMENTATION +==================== +Description +An incrontab file contains instructions to the incrond(8) daemon of the general form: "run this command on these file events". There are two categories of tables: system tables (with root privileges) and user tables (with user privileges). + +System tables are (by default) located in /etc/incron.d and may have any names. Each system table exists separately inside incron and their watches never collide. + +Each user has their own table, and commands in any given incrontab will be executed as the user who owns the incrontab. System users (such as apache, postfix, nobody etc.) may have their own incrontab. + +incrontab files are read when the incrond(8) daemon starts and after any change (incrontab file are being hooked when incrond is running). + +Blank lines are ignored. The general line format is the following: + + + +Where path is an absolute filesystem path, mask is an event mask (in symbolic or numeric form) and command is an executable file (or a script) with its arguments. See bellow for event mask symbols. The executable file may be noted as an absolute path or only as the name itself (PATH locations are examined). + +Please remember that the same path may occur only once per table (otherwise only the first occurrence takes effect and an error message is emitted to the system log). +Event Symbols +These basic event mask symbols are defined: + +IN_ACCESS File was accessed (read) (*) +IN_ATTRIB Metadata changed (permissions, timestamps, extended attributes, etc.) (*) +IN_CLOSE_WRITE File opened for writing was closed (*) +IN_CLOSE_NOWRITE File not opened for writing was closed (*) +IN_CREATE File/directory created in watched directory (*) +IN_DELETE File/directory deleted from watched directory (*) +IN_DELETE_SELF Watched file/directory was itself deleted +IN_MODIFY File was modified (*) +IN_MOVE_SELF Watched file/directory was itself moved +IN_MOVED_FROM File moved out of watched directory (*) +IN_MOVED_TO File moved into watched directory (*) +IN_OPEN File was opened (*) + +When monitoring a directory, the events marked with an asterisk (*) above can occur for files in the directory, in which case the name field in the returned event data identifies the name of the file within the directory. + +The IN_ALL_EVENTS symbol is defined as a bit mask of all of the above events. Two additional convenience symbols are IN_MOVE, which is a combination of IN_MOVED_FROM and IN_MOVED_TO, and IN_CLOSE which combines IN_CLOSE_WRITE and IN_CLOSE_NOWRITE. + +The following further symbols can be specified in the mask: +IN_DONT_FOLLOW Don't dereference pathname if it is a symbolic link +IN_ONESHOT Monitor pathname for only one event + +IN_ONLYDIR Only watch pathname if it is a directory + +Additionaly, there is a symbol which doesn't appear in the inotify symbol set. It it IN_NO_LOOP. This symbol disables monitoring events until the current one is completely handled (until its child process exits). +Wildcards +The following wildards may be used inside command specification: + +$$ dollar sign +$@ watched filesystem path (see above) +$# event-related file name +$% event flags (textually) +$& event flags (numerically) diff --git a/fileSend.php b/fileSend.php index 46e7ac6..09ab116 100644 --- a/fileSend.php +++ b/fileSend.php @@ -51,26 +51,55 @@ if( isset($opts->help) || count($opts->getOptions())==0 ) if ( isset($opts->file) ) { $c = new Zend_Config($application->getOptions()); - + //Get the main directory name in FTP and SFTP $pathParts = pathinfo($opts->file); - + $filename = $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("/home/data/"); if ( 'sftp' == substr($opts->file,$startpos,4) ) { - $type = 'SFTP'; + $type = 'SFTP'; } elseif ( 'ftp' == substr($opts->file,$startpos,3) ) { - $type = 'FTP'; + $type = 'FTP'; } - - $lines = file($filename); + + $lines = file($opts->file); $nbLines = count($lines); - + + $copyOptionsAddDate = false; + $copyOptionsDeleteAfter = false; + + /** + * @todo : links to prestation table and file rules + */ + switch ($client) { + case 'sfr' : + $prestation = 'FICH_RCE'; + break; + case 'gefacto': + $prestation = 'GEFACTO'; + $copyOptionsAddDate = true; + break; + } + + if ($copyOptionsAddDate) { + $extensionLength = 0; + if ($extension!='') { + $extensionLength = strlen($extension)+1; + } + $filename = substr($filename, 0, $extensionLength); + $filename = $filename . '_' . date('YmdHis') . '.' . $extension ; + } + //Prepare mail - if ($opts->onlyinfo || $opts->debug) { + if ($opts->mail || $opts->debug) { $subject = "[Flux] - Réception fichier $client"; $txt = "Réception d'un fichier pour traitement\n"; @@ -78,54 +107,60 @@ if ( isset($opts->file) ) $txt.= "Mode de transmission : $type\n"; $txt.= "Fichier : ".$opts->file."\n"; $txt.= "Nombre de Lignes : $nbLines\n"; - + $mail = new Zend_Mail(); $tr = new Zend_Mail_Transport_Sendmail(); - $mail->setDefaultTransport($tr); + $mail->setDefaultTransport($tr); $mail->setBodyText($txt); $mail->setFrom('production@scores-decisions.com', 'Production'); $mail->addTo('support@scores-decisions.com', 'Support'); $mail->setSubject($subject); $mail->send(); - + //Stop - if ($opts->onlyinfo) { - exit; + if ($opts->mail) { + exit; } - - } + + } //Execute - $db = Zend_Db::factory($c->profil->metier); + $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, 'typeDepot' => $type, 'dateDepot' => $dateFile, 'fileDepot' => $filename, 'nbLines' => $nbLines, 'dateInsert' => date('YmdHis'), 'dateExecute' => '0000-00-00 00:00:00', - )); + )); echo date('Y-m-dTH:i:s')." - Enregistrement client:$client fichier:$filename\n"; } catch (Zend_Db_Exception $e) { echo date('Y-m-dTH:i:s')." - ERREUR Enregistrement client:$client fichier:$filename\n"; } //Copie du fichier - $destDir = $c->profil->storage . - DIRECTORY_SEPARATOR . $client . + $destDir = $c->profil->path->storage . + DIRECTORY_SEPARATOR . $client . DIRECTORY_SEPARATOR . 'send'; if ( !is_dir($destDir) ) { mkdir($destDir, null, true); } - - if ( copy($opts->file, $destDir. DIRECTORY_SEPARATOR . $filename) ) { + + if ( copy($opts->file, $destDir. DIRECTORY_SEPARATOR . $filename) ) { echo date('Y-m-dTH:i:s')." - Copie du fichier $filename dans $destDir\n"; + + if ($copyOptionsDeleteAfter) { + unlink($opts->file); + } + } else { echo date('Y-m-dTH:i:s')." - ERREUR Copie du fichier $filename dans $destDir\n"; - } + } } \ No newline at end of file diff --git a/scripts/build/incron/gefacto b/scripts/build/incron/gefacto new file mode 100644 index 0000000..7517121 --- /dev/null +++ b/scripts/build/incron/gefacto @@ -0,0 +1 @@ +/home/data/sftp/gefacto/send IN_CLOSE_WRITE /home/scores/sftp-mail.sh $# \ No newline at end of file diff --git a/scripts/build/incron/sfr b/scripts/build/incron/sfr new file mode 100644 index 0000000..c34004a --- /dev/null +++ b/scripts/build/incron/sfr @@ -0,0 +1 @@ +/home/data/sftp/sfrbtr/send IN_CLOSE_WRITE php /home/batchFlux/fileSend.php --file $@/$# \ No newline at end of file