Compare commits

...

29 Commits
1.0 ... 1.1

Author SHA1 Message Date
Michael RICOIS
91da2b3658 Composer 2015-09-11 14:29:14 +00:00
Michael RICOIS
1b851647c3 TODO 2015-09-11 14:23:35 +00:00
Michael RICOIS
01ed73488c Ajout exemple crontab nettoyage 2015-09-11 14:10:04 +00:00
Michael RICOIS
7fd3c5df51 Nettoyage des fichiers 2015-09-11 14:01:04 +00:00
Michael RICOIS
6e646e364e Mise en place de la lecture du fichier 2015-09-11 13:58:40 +00:00
Michael RICOIS
01c88b1963 Petite correction après mise en prod 2015-07-03 06:21:00 +00:00
Michael RICOIS
769778cfc3 Correction 2015-07-02 14:57:29 +00:00
Michael RICOIS
3d20d60687 Configuration VWBANK 2015-06-30 09:33:30 +00:00
Michael RICOIS
7d3f435f2a Ajout option filtre par nom 2015-06-30 09:32:45 +00:00
Michael RICOIS
7751aa504a Remove application.ini from repository 2015-06-29 07:37:33 +00:00
Michael RICOIS
77fcfa4a9d Nettoyage 2015-06-29 07:35:26 +00:00
Michael RICOIS
0271f2e27c Pour le fichier reçu ajout d'une option de routage 2015-06-26 10:43:25 +00:00
Michael RICOIS
1d4a18065b Ajout la possibilité de configurer l'envoi de mail 2015-06-17 15:06:49 +00:00
Michael RICOIS
da3836d96e Mise à jour de la configuration 2015-06-10 06:14:37 +00:00
Michael RICOIS
2d1c282af5 Documentation 2015-05-19 14:01:49 +00:00
Michael RICOIS
9cc6a9af4f Date de lecture des fichiers 2015-03-31 07:04:11 +00:00
Michael RICOIS
3aa3c01931 incron file en lecture 2015-03-31 06:47:55 +00:00
Michael RICOIS
409758e4f5 Nouvelles options 2015-03-31 06:44:13 +00:00
Michael RICOIS
7ea58800b4 Detection prestation avec options 2015-03-30 13:54:20 +00:00
Michael RICOIS
756c1b68af Prestation jalpdfsed 2015-03-30 13:53:28 +00:00
Michael RICOIS
81f19f3ad3 Ajout de la taille du fichier et des options 2015-03-30 13:53:03 +00:00
Michael RICOIS
c41bb05a8b Fallback autoloader 2014-12-19 14:29:23 +00:00
Michael RICOIS
c47069460b Ajout Prestation RRG Hexavia 2014-12-18 16:27:24 +00:00
Michael RICOIS
bd96303de4 Utilisation de profil pour la configuration 2014-12-17 10:28:01 +00:00
Michael RICOIS
b525a49f5b Change incron.d with automatic configuration 2014-11-06 19:23:41 +00:00
Michael RICOIS
86cf862fc2 Good config dir definition 2014-11-06 19:19:07 +00:00
Michael RICOIS
6a4e805282 Add options NoLog to get file without save it in database 2014-10-07 08:30:01 +00:00
Michael RICOIS
19b6bf6fd9 Change user, group pour ftp et sftp 2014-10-02 09:45:58 +00:00
Michael RICOIS
85fcc8eaa5 SEND : Plus de souplesse dans la gestion 2014-10-01 08:59:58 +00:00
12 changed files with 1016 additions and 150 deletions

134
README Normal file
View File

@ -0,0 +1,134 @@
Mise à disposition fichier de télétransmission
----------------------------------------------
Utilisation des événements INOTIFY pour la surveillance des dossiers
The command may contain these wildcards:
$$ - a dollar sign
$@ - the watched filesystem path (see above)
$# - the event-related file name
$% - the event flags (textually)
$& - the event flags (numerically)
Events
IN_ACCESS
File was accessed (read) (*).
IN_ATTRIB
Metadata changed, e.g., permissions, timestamps, extended attributes, link count (since Linux 2.6.25), UID, GID, 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 inotify_event structure identifies the name of the file within the
directory.
The IN_ALL_EVENTS macro is defined as a bit mask of all of the above events. This macro can be used as the mask
argument when calling inotify_add_watch(2).
Two additional convenience macros are IN_MOVE, which equates to IN_MOVED_FROM|IN_MOVED_TO, and IN_CLOSE, which
equates to IN_CLOSE_WRITE|IN_CLOSE_NOWRITE.
The following further bits can be specified in mask when calling inotify_add_watch(2):
IN_DONT_FOLLOW (since Linux 2.6.15)
Don't dereference pathname if it is a symbolic link.
IN_EXCL_UNLINK (since Linux 2.6.36)
By default, when watching events on the children of a directory, events are generated for children even after they have been unlinked from the directory. This can result in large numbers of uninteresting events for some applications (e.g., if watching /tmp, in which many applications create temporary files whose names are immediately unlinked). Specifying IN_EXCL_UNLINK changes the default behavior, so that events are not generated for children after they have been unlinked from the watched directory.
IN_MASK_ADD
Add (OR) events to watch mask for this pathname if it already exists (instead of replacing mask).
IN_ONESHOT
Monitor pathname for one event, then remove from watch list.
IN_ONLYDIR (since Linux 2.6.15)
Only watch pathname if it is a directory.
The following bits may be set in the mask field returned by read(2):
IN_IGNORED
Watch was removed explicitly (inotify_rm_watch(2)) or automatically (file was deleted, or file system was unmounted).
IN_ISDIR
Subject of this event is a directory.
IN_Q_OVERFLOW
Event queue overflowed (wd is -1 for this event).
IN_UNMOUNT
File system containing watched object was unmounted.
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
'in' => array(
--See the list of options--
),
'out' => array(
--See the list of options--
),
),
),
),
Send Options list (in)
----------------------
- 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
- Log - Log transfert
- Route - For each Route options (cp, ftp, sftp, mail)
cp => realpath of dir
ftp
sftp
mail
- FilterName - Filtre par nom de prestation (true) ou par nom défini
Recv Options list (out)
-----------------------
- Log - Log read disable by default
TODO
----
Créer fileMail => Recupération element par mail, enregistrement
Filtre sur email et/ou sujet

97
fileClean.php Normal file
View File

@ -0,0 +1,97 @@
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Displays usage information.",
'cron' => "Mandatory option with cron",
'verbose|v' => "Affichage de ce qui est fait."
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
echo "Clean directories.\n";
echo $opts->getUsageMessage();
exit;
}
$c = new Zend_Config($application->getOptions());
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db);
// --- Parcours des éléments lu dans la table flux_fileout pour suppression des fichiers de plus de 5 jours
$dateEnd = new Zend_Date();
$dateEnd->subDay(5);
$fluxM = new Application_Model_Sdv1FluxFileOut();
$deleteSql = $fluxM->select()
->where('depotDate BETWEEN "'.date('Ym').'01000000" AND "'.date('Ym').'31235959"')
->where('depotDateRead!=0')
->where('depotDateRead < ?', $dateEnd->toString('YMdHms'));
$deleteResult = $fluxM->fetchAll($deleteSql);
if (count($deleteResult) > 0) {
foreach ($deleteResult as $item) {
$path = '';
switch($item->depotType) {
case 'FTP':
$path = $c->profil->path->ftp;
break;
case 'SFTP':
$path = $c->profil->path->sftp;
break;
}
$path.= '/' . $item->client;
if ($item->depotDir == '') {
$path.= '/recv';
} else {
$path.= '/' . $item->depotDir;
}
$filename = $path . '/' . $item->fileOut;
if (file_exists($filename) && is_file($filename)) {
unlink($filename);
}
}
}
// --- Parcours des fichiers pour suppression des fichiers de plus de 30 jours
$path = array(
$c->profil->path->sftp,
$c->profil->path->ftp,
);
// find $p/ -mindepth 2 -maxdepth 4 -type f ! -regex '.*/\..*' -ctime +30 -exec echo {} \;
foreach ($path as $p) {
passthru("find $p/ -mindepth 2 -maxdepth 4 -type f ! -regex '.*/\..*' -ctime +30 -exec rm -f {} \;");
}

115
fileConfig.php Normal file
View File

@ -0,0 +1,115 @@
<?php
return array(
'sfrbtr' => array(
'prestations' => array(
array(
'name' => 'FICH_RCE',
'type' => 'SFTP',
'in' => array(
'CopyDeleteAfter' => true,
),
'out' => array(),
),
),
),
'gefacto' => array(
'prestations' => array(
array(
'name' => 'GEFACTO',
'type' => 'SFTP',
'in' => array(
'RunWithEndFile' => true,
'CopyAddDate' => true,
'CopyDeleteAfter' => true,
),
'out' => array(),
),
),
),
'fransbonhomme' => array(
'prestations' => array(
array(
'name' => 'RAPPORT',
'type' => 'FTP',
'in' => array(
'CopyDeleteAfter' => true,
),
'out' => array(),
),
),
),
'bilansext' => array(
'prestations' => array(
array(
'name' => 'EXTRANET',
'type' => 'FTP',
'in' => array(
'CopyDeleteAfter' => true,
),
'out' => array(),
),
),
),
'rrg' => array(
'prestations' => array(
array(
'name' => 'HEXAVIA',
'type' => 'FTP',
'in' => array(),
'out' => array(),
),
),
),
'jalpdfsed' => array(
'prestations' => array(
array(
'name' => 'PDF',
'type' => 'FTP',
'in' => array(
'CopyDeleteAfter' => true,
),
'out' => array(),
),
),
),
'orone' => array(
'prestations' => array(
array(
'name' => 'XML',
'type' => 'FTP',
'in' => array(),
'out' => array(),
),
array(
'name' => 'PDF',
'type' => 'FTP',
'in' => array(),
'out' => array(),
),
),
),
'vwbank' => array(
'prestations' => array(
array(
'name' => 'SURBODPRDCFTVWBANK19300',
'type' => 'FTP',
'in' => array(
'FilterName' => true,
'Route' => array( 'cp' => '/home/data/clients-ftp/vwbank/send' ),
'CopyDeleteAfter' => true,
),
'out' => array(),
),
array(
'name' => 'SURBODPRDCFTVWBENG',
'type' => 'FTP',
'in' => array(
'FilterName' => true,
'Route' => array( 'cp' => '/home/data/clients-ftp/vwbank/send' ),
'CopyDeleteAfter' => true,
),
'out' => array(),
),
),
),
);

186
fileRead.php Normal file
View File

@ -0,0 +1,186 @@
<?php
/**
* Indicateur de récupération par le client
* Attention le script doit s'executer avec l'utilisateur root
*/
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Displays usage information.",
'file|f=s' => "Give the full file path to integrate",
'debug' => "Send a mail for debug",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
echo "Execute basic action when a customer send a file.\n";
echo $opts->getUsageMessage();
exit;
}
//Get the file
if ( isset($opts->file) )
{
$optionsNoLog = false;
$repositoryDir = 'recv';
$c = new Zend_Config($application->getOptions());
//Get the main directory name in FTP and SFTP
$pathParts = pathinfo($opts->file);
$filenameIn = $pathParts['basename'];
$extension = '';
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))
{
$type = 'SFTP';
$fluxBasePath = $c->profil->path->sftp . '/' . $client;
}
elseif ('ftp' == substr($opts->file, $startpos, 3))
{
$type = 'FTP';
$fluxBasePath = $c->profil->path->ftp . '/' . $client;
}
$fluxRepository = str_replace(array($fluxBasePath.'/', '/'.$filenameIn), array('', ''), $opts->file);
// Match prestation
$prestations = include __DIR__ . '/fileConfig.php';
$prestation = null;
if (array_key_exists($client, $prestations))
{
$clientPrestations = $prestations[$client]['prestations'];
foreach ($clientPrestations as $i => $p)
{
// Not default repository dir
if (array_key_exists('directory', $p) && !empty($p['directory']))
{
$repositoryDir = $p['directory'];
}
if ($type == $p['type'] && $fluxRepository == $repositoryDir)
{
$prestation = $p['name'];
// Set option
if (array_key_exists('in', $p) && count($p['in']) > 0)
{
foreach ($p['in'] as $option => $value)
{
${'options'.$option} = $value;
}
}
break;
}
}
}
if ($prestation === null)
{
echo date('Y-m-d H:i:s')." - Prestation not found !\n";
}
$fluxBasePath .= '/'.$repositoryDir;
//Prepare mail
if ($opts->mail || $opts->debug)
{
$subject = "[Flux] - Lecture fichier $client";
$txt = "Lecture d'un fichier\n";
$txt.= "Client : $client\n";
$txt.= "Mode de transmission : $type\n";
$txt.= "Fichier : ".$filenameIn."\n";
$txt.= "Nombre de Lignes : $nbLines\n";
$txt.= "Taille du fichier : $size\n";
$mail = new Zend_Mail('UTF-8');
// --- Configuration du transport SMTP
if ( $c->profil->mail->method == 'smtp' ) {
$config = array();
if ( isset($this->config->auth) ) {
$config['auth'] = $this->config->auth;
if ( isset($this->config->username) ) {
$config['username'] = $c->profil->mail->username;
}
if ( isset($this->config->password) ) {
$config['password'] = $c->profil->mail->password;
}
}
if ( isset($this->config->port) ) {
$config['port'] = $c->profil->mail->port;
}
$tr = new Zend_Mail_Transport_Smtp($c->profil->mail->host, $config);
}
// --- Configuration transport Sendmail
if ( $this->config->mail->method == 'sendmail' ) {
$tr = new Zend_Mail_Transport_Sendmail();
}
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine Flux');
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
$mail->setSubject($subject);
$mail->send();
//Stop
if ($opts->mail)
{
exit;
}
}
// --- Execute
if ($optionsNoLog === false)
{
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db);
try {
$fluxM = new Application_Model_Sdv1FluxFileOut();
$fluxM->update(array('depotDateRead' => date('YmdHis')), "fileOut='".$filenameIn."'");
echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filenameIn\n";
}
catch (Zend_Db_Exception $e)
{
echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filenameIn\n";
}
}
}

View File

@ -7,16 +7,16 @@
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// 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 */
@ -79,53 +79,130 @@ if ( $result->count() > 0 ) {
'/' . 'recv' .
'/' . $item->fileOut;
$depotDir = 'recv';
$fluxRepository = 'recv';
if ($item->depotDir != '') {
$depotDir = $item->depotDir;
$fluxRepository = $item->depotDir;
}
switch ($item->depotType) {
case 'FTP':
$dest = $c->profil->path->ftp .
'/' . $item->client .
'/' . $depotDir .
'/' . $fluxRepository .
'/' . $item->fileOut;
break;
case 'SFTP':
$dest = $c->profil->path->sftp .
'/' . $item->client .
'/' . $depotDir .
'/' . $fluxRepository .
'/' . $item->fileOut;
break;
}
// --- Options
$optionsCopyAddDate = false;
$optionsCopyDeleteAfter = false;
$optionsRunWithEndFile = false;
$optionsLog = false;
// Match prestation
$prestations = include __DIR__ . '/fileConfig.php';
$prestation = null;
if (array_key_exists($item->client, $prestations))
{
$clientPrestations = $prestations[$item->client]['prestations'];
foreach ($clientPrestations as $i => $p)
{
// Not default repository dir
$repositoryDir = 'recv';
if (array_key_exists('directory', $p) && !empty($p['directory']))
{
$repositoryDir = $p['directory'];
}
if ($item->depotType == $p['type'] && $fluxRepository == $repositoryDir)
{
$prestation = $p['name'];
// Set option
if (array_key_exists('out', $p) && count($p['out']) > 0)
{
foreach ($p['out'] as $option => $value)
{
${'options'.$option} = $value;
}
}
break;
}
}
}
// --- Copy du fichier
if ( copy($source, $dest) ) {
$lines = file($source);
$nbLines = count($lines);
// --- Detail du fichier
$nbLines = 0;
if ( strtolower(substr(basename($source), -3)) == 'csv' ) {
$lines = file($source);
$nbLines = count($lines);
}
$size = filesize($source);
chown($dest, $item->client);
// --- Permission du depot
if ($item->depotType == 'FTP') {
chown($dest, 'ftpuser');
chgrp($dest, 'ftpgroup');
}
elseif($item->depotType == 'SFTP') {
chown($dest, $item->client);
}
// --- Mise à jour information de remise
$fluxM->update(array(
'depotFileSize' => $size,
'depotDate' => date('YmdHis'),
), 'id='.$item->id);
$subject = "[Flux] - Envoi d'un fichier " . $item->client;
$txt = "Envoi d'un fichier après traitement\n";
$txt.= "Client : ".$item->client."\n";
$txt.= "Mode de transmission : ".$item->depotType."\n";
$txt.= "Fichier : ".$item->fileOut."\n";
$txt.= "Nombre de Lignes : $nbLines\n";
// --- Envoi email
if ($optionsLog === true) {
$subject = "[Flux] - Envoi d'un fichier " . $item->client;
$txt = "Envoi d'un fichier après traitement\n";
$txt.= "Client : ".$item->client."\n";
$txt.= "Mode de transmission : ".$item->depotType."\n";
$txt.= "Fichier : ".$item->fileOut."\n";
$txt.= "Nombre de Lignes : $nbLines\n";
$mail = new Zend_Mail('UTF-8');
$tr = new Zend_Mail_Transport_Sendmail();
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine');
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
$mail->setSubject($subject);
$mail->send();
$mail = new Zend_Mail('UTF-8');
// --- Configuration du transport SMTP
if ( $c->profil->mail->method == 'smtp' ) {
$config = array();
if ( isset($this->config->auth) ) {
$config['auth'] = $this->config->auth;
if ( isset($this->config->username) ) {
$config['username'] = $c->profil->mail->username;
}
if ( isset($this->config->password) ) {
$config['password'] = $c->profil->mail->password;
}
}
if ( isset($this->config->port) ) {
$config['port'] = $c->profil->mail->port;
}
$tr = new Zend_Mail_Transport_Smtp($c->profil->mail->host, $config);
}
// --- Configuration transport Sendmail
if ( $this->config->mail->method == 'sendmail' ) {
$tr = new Zend_Mail_Transport_Sendmail();
}
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine Flux');
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
$mail->setSubject($subject);
$mail->send();
}
}

View File

@ -4,37 +4,37 @@
* Attention le script doit s'executer avec l'utilisateur root
*/
// Define path to application directory
// --- Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
// --- Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
// --- 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 */
// --- Zend_Application
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
// --- Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
// --- Options
array(
'help|?' => "Displays usage information.",
'file|f=s' => "Give the full file path to integrate",
'mail' => "Only send a mail when a file is write",
'debug' => "Send a mail for debug",
'help|?' => "Displays usage information.",
'file|f=s' => "Give the full file path to integrate",
'mail' => "Only send a mail when a file is write",
'debug' => "Send a mail for debug",
)
);
$opts->parse();
@ -43,7 +43,7 @@ try {
exit;
}
//Usage
// --- Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
echo "Execute basic action when a customer send a file.\n";
@ -51,172 +51,282 @@ if( isset($opts->help) || count($opts->getOptions())==0 )
exit;
}
//Get the file
// --- Get the file
if ( isset($opts->file) )
{
$optionsCopyAddDate = false;
$optionsCopyDeleteAfter = false;
$optionsRunWithEndFile = false;
$optionsLog = true;
$optionsRoute = array();
$optionsFilterName = false;
$repositoryDir = 'send';
$c = new Zend_Config($application->getOptions());
$dateFile = date('YmdHis', filectime($opts->file));
//Get the main directory name in FTP and SFTP
// --- Get the main directory name in FTP and SFTP
$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']));
$startpos = strlen( $c->profil->path->data . DIRECTORY_SEPARATOR );
if ( 'sftp' == substr($opts->file, $startpos, 4) ) {
// --- Base path, type and repository
$startpos = strlen( $c->profil->path->data . '/' );
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 => $p)
{
// --- Not default repository dir
if (array_key_exists('directory', $p) && !empty($p['directory']))
{
$repositoryDir = $p['directory'];
}
$OptionsCopyAddDate = false;
$OptionsCopyDeleteAfter = false;
$OptionsRunWithEndFile = false;
// --- Match prestation
if ($type == $p['type'] && $fluxRepository == $repositoryDir)
{
$prestation = $p['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 options
if (array_key_exists('in', $p) && count($p['in']) > 0)
{
foreach ($p['in'] as $option => $value)
{
${'options'.$option} = $value;
}
}
//Use ".fin" or ".end" files to do something
// --- Filters
if ($optionsFilterName === true) {
if ( strpos($filenameIn, $prestation) === false ) {
$prestation = null; continue;
}
}
if (is_string($optionsFilterName) && strlen($optionsFilterName) > 0) {
if (strpos($filenameIn, $optionsFilterName) === false) {
$prestation = null; continue;
}
}
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) ) {
// --- Get the realname of file IN or exit
if ($optionsRunWithEndFile)
{
if (in_array( $extension, $runExtensions))
{
$extensionLength = strlen($extension)+1;
$filenameIn = substr($filenameIn, 0, strlen($filenameIn) - $extensionLength);
$extension = '';
} else {
}
else
{
exit;
}
}
$lines = file($fluxBasePath . '/' . $filenameIn);
$nbLines = count($lines);
// ---
//Define default out filename
// --- Detail du fichier
$nbLines = 0;
if ( strtolower(substr($filenameIn, -3)) == 'csv' ) {
$lines = file($fluxBasePath . '/' . $filenameIn);
$nbLines = count($lines);
}
$size = filesize($fluxBasePath . '/' . $filenameIn);
// --- Define default out filename
$filenameOut = $filenameIn;
//Add date to filename
if ( $OptionsCopyAddDate ) {
// --- Add date to filename
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) {
// --- Prepare mail
if ($opts->mail || $opts->debug)
{
$subject = "[Flux] - Réception fichier $client";
$txt = "Réception d'un fichier pour traitement\n";
$txt.= "Client : $client\n";
$txt.= "Mode de transmission : $type\n";
$txt.= "Fichier : ".$filenameIn."\n";
$txt.= "Nombre de Lignes : $nbLines\n";
$txt.= "Taille du fichier : $size\n";
$mail = new Zend_Mail('UTF-8');
$tr = new Zend_Mail_Transport_Smtp('smtp.celeste.fr');
// --- Configuration du transport SMTP
if ( $c->profil->mail->method == 'smtp' ) {
$config = array();
if ( isset($this->config->auth) ) {
$config['auth'] = $this->config->auth;
if ( isset($this->config->username) ) {
$config['username'] = $c->profil->mail->username;
}
if ( isset($this->config->password) ) {
$config['password'] = $c->profil->mail->password;
}
}
if ( isset($this->config->port) ) {
$config['port'] = $c->profil->mail->port;
}
$tr = new Zend_Mail_Transport_Smtp($c->profil->mail->host, $config);
}
// --- Configuration transport Sendmail
if ( $this->config->mail->method == 'sendmail' ) {
$tr = new Zend_Mail_Transport_Sendmail();
}
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine');
$mail->setFrom('supportdev@scores-decisions.com', 'Machine Flux');
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
$mail->setSubject($subject);
$mail->send();
//Stop
if ($opts->mail) {
// --- Stop
if ($opts->mail)
{
exit;
}
}
//Copie du fichier
// --- Before store the file send to another repository
if (count($optionsRoute) > 0) {
foreach ($optionsRoute as $tr => $value) {
switch($tr) {
case 'cp':
if (copy($fluxBasePath . '/' . $filenameIn, $value . '/' . $filenameOut)) {
echo date('Y-m-d H:i:s')." - Copie du fichier $filenameIn dans $value\n";
}
break;
}
}
}
// --- 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
$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";
// --- Execute
if ($optionsLog === true)
{
$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,
'depotFileSize' => $size,
'dateInsert' => date('YmdHis'),
'dateExecute' => '0000-00-00 00:00:00', // @todo : dateExecute
));
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 ) {
// --- Suppression des fichiers
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";
}
}

View File

@ -13,36 +13,25 @@ set_include_path(implode(PATH_SEPARATOR, array(
get_include_path(),
)));
//Copy configuration
$configDir = realpath(dirname(__FILE__)).'/config';
$appconfigDir = APPLICATION_PATH.'/configs';
if (stristr(PHP_OS, 'WIN')) {
$hostname = 'development';
} else {
$hostname = exec('echo $(hostname)');
}
if ( !copy($configDir.'/'.$hostname.'/application.ini', $appconfigDir.'/application.ini') ) {
echo "application.ini could not be copied !\n";
exit;
}
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
),
'fallback_autoloader' => true
),
));
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Display usage information.",
'install' => "Make install operation.",
'install=s' => "Make install operation.",
)
);
$opts->parse();
@ -52,17 +41,34 @@ try {
}
//Usage
if(isset($opts->help))
if (isset($opts->help))
{
echo $opts->getUsageMessage();
exit;
}
if(isset($opts->install))
if ( ! empty($opts->install) )
{
if (stristr(PHP_OS, 'WIN')) {
echo date('Y-m-d H:i:s')." - Démarrage de la configuration.\n";
//Copy configuration
$configDir = realpath(dirname(__FILE__)).'/profil';
$appconfigDir = APPLICATION_PATH.'/configs';
$profil = $opts->install;
if ( !file_exists( $appconfigDir.'/application.ini') ) {
$result = copy($configDir.'/'.$profil.'/application.ini', $appconfigDir.'/application.ini');
if ($result !== true) {
echo date('Y-m-d H:i:s')." - Impossible de copier la configuration.\n";
exit(1);
}
} else {
echo date('Y-m-d H:i:s')." - Le profil de configuration existe déja.\n";
exit(1);
}
echo date('Y-m-d H:i:s')." - Fin de la configuration.\n";
} else {
echo date('Y-m-d H:i:s')." - Je n'ai rien fait !\n";
}

View File

@ -0,0 +1,6 @@
# Gestion des flux a envoyer au client
*/15 4-18 * * * root php /home/batchFlux/fileRecv.php --cron >> /home/log/recv.log 2>&1
# Nettoyage
01 18 * * * root php /home/batchFlux/fileClean.php --cron >> /home/log/clean.log 2>&1

134
scripts/build/incron.php Normal file
View File

@ -0,0 +1,134 @@
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Display usage information.",
'list' => "List client and prestations.",
'generate' => "Generate",
'send=s' => "Generate send incron file [client]/[name]",
'read=s' => "Generate read 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";
if ($opts->send) {
$input = explode('/', $opts->send);
$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.d/' . strtolower($item['type']) . '_' . $client;
file_put_contents($file, "$fluxBasePath IN_CLOSE_WRITE php /home/batchFlux/fileSend.php --file $@/$# >> /home/log/send.log 2>&1");
echo "File created $file\n";
}
}
}
}
if ($opts->read) {
$input = explode('/', $opts->read);
$client = $input[0];
$name = $input[1];
if (array_key_exists($client, $prestations))
{
foreach ($prestations[$client]['prestations'] as $item)
{
if ($item['name'] == $name)
{
$directory = 'recv';
if (array_key_exists('directory', $item))
{
$directory = $item['directory'];
}
$fluxBasePath = '/home/data/' . strtolower($item['type']) . '/' . $client. '/' . $directory;
$file = __DIR__ . '/incron.d/' . strtolower($item['type']) . '_' . $client . '_recv';
file_put_contents($file, "$fluxBasePath IN_ACCESS php /home/batchFlux/fileRead.php --file $@/$# >> /home/log/read.log 2>&1");
echo "File created $file\n";
}
}
}
}
echo "Put the file to /etc/incron.d and restart incron.\n";
echo "Generate Ended.\n";
}

View File

@ -1 +0,0 @@
/home/data/sftp/gefacto/send IN_CLOSE_WRITE php /home/batchFlux/fileSend.php --debug --file $@/$#

View File

@ -1 +0,0 @@
/home/data/sftp/sfrbtr/send IN_CLOSE_WRITE php /home/batchFlux/fileSend.php --debug --file $@/$#

View File

@ -27,6 +27,9 @@ profil.db.metier.params.password=2XQTpKatBVRezXE9
profil.db.metier.params.dbname=sdv1
profil.db.metier.params.driver_options.MYSQLI_INIT_COMMAND = "SET NAMES utf8"
; Mail configuration
profil.db.method = sendmail
[staging : production]
resources.frontController.params.displayExceptions = 1