flux/fileSend.php

222 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2013-08-19 13:30:59 +02:00
<?php
/**
* Client => SD
* 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'));
2013-08-19 13:30:59 +02:00
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
2013-08-19 13:30:59 +02:00
)));
/** 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",
'mail' => "Only send a mail when a file is write",
'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) )
{
$c = new Zend_Config($application->getOptions());
$dateFile = date('YmdHis', filectime($opts->file));
2013-08-27 12:27:03 +02:00
2013-08-19 13:30:59 +02:00
//Get the main directory name in FTP and SFTP
$pathParts = pathinfo($opts->file);
$filenameIn = $pathParts['basename'];
2013-08-27 12:27:03 +02:00
$extension = '';
if (array_key_exists('extension', $pathParts) ){
$extension = $pathParts['extension'];
}
2013-08-19 13:30:59 +02:00
$client = basename(dirname($pathParts['dirname']));
2013-08-27 12:27:03 +02:00
2013-08-28 12:23:01 +02:00
$startpos = strlen( $c->profil->path->data . DIRECTORY_SEPARATOR );
if ( 'sftp' == substr($opts->file, $startpos, 4) ) {
2013-08-27 12:27:03 +02:00
$type = 'SFTP';
$fluxBasePath = $c->profil->path->sftp . '/' . $client;
} elseif ( 'ftp' == substr($opts->file, $startpos, 3) ) {
2013-08-27 12:27:03 +02:00
$type = 'FTP';
$fluxBasePath = $c->profil->path->ftp . '/' . $client;
2013-08-19 13:30:59 +02:00
}
2013-08-27 12:27:03 +02:00
2014-03-26 19:01:33 +01:00
$fluxBasePath .= '/send';
$OptionsCopyAddDate = false;
$OptionsCopyDeleteAfter = false;
$OptionsRunWithEndFile = false;
2013-08-27 12:27:03 +02:00
/**
* @todo : links to prestation table and file rules
2014-05-05 17:32:48 +02:00
* Prestation => Repertoire reception, etc
2013-08-27 12:27:03 +02:00
*/
switch ($client) {
case 'sfrbtr' :
2013-08-27 12:27:03 +02:00
$prestation = 'FICH_RCE';
$filemask = array(
'FICH_RCE_SCORE_.*.csv',
);
2013-08-27 12:27:03 +02:00
break;
case 'gefacto':
$prestation = 'GEFACTO';
$OptionsCopyAddDate = true;
2014-05-05 17:32:48 +02:00
$OptionsRunWithEndFile = true;
2013-08-27 12:27:03 +02:00
break;
}
//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;
}
}
2014-06-09 10:37:32 +02:00
//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 {
exit;
}
}
$lines = file($fluxBasePath . '/' . $filenameIn);
$nbLines = count($lines);
2014-03-26 19:01:33 +01:00
//Define default out filename
$filenameOut = $filenameIn;
//Add date to filename
if ( $OptionsCopyAddDate ) {
2013-08-27 12:27:03 +02:00
$extensionLength = 0;
if ( $extension != '' ) {
2013-08-27 12:27:03 +02:00
$extensionLength = strlen($extension)+1;
2014-06-09 10:37:32 +02:00
$filenameOut = substr($filenameIn, 0, strlen($filenameIn) - $extensionLength);
$filenameOut = $filenameOut . '_' . date('YmdHis') . '.' . $extension;
} else {
$filenameOut = $filenameIn . '_' . date('YmdHis');
2013-08-27 12:27:03 +02:00
}
}
2013-08-19 13:30:59 +02:00
//Prepare mail
2013-08-27 12:27:03 +02:00
if ($opts->mail || $opts->debug) {
2013-08-19 13:30:59 +02:00
$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";
2013-08-19 13:30:59 +02:00
$txt.= "Nombre de Lignes : $nbLines\n";
2013-08-27 12:27:03 +02:00
$mail = new Zend_Mail('UTF-8');
$tr = new Zend_Mail_Transport_Smtp('smtp.celeste.fr');
2013-08-27 12:27:03 +02:00
$mail->setDefaultTransport($tr);
2013-08-19 13:30:59 +02:00
$mail->setBodyText($txt);
2013-08-28 21:58:09 +02:00
$mail->setFrom('supportdev@scores-decisions.com', 'Machine');
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
2013-08-19 13:30:59 +02:00
$mail->setSubject($subject);
$mail->send();
2013-08-27 12:27:03 +02:00
2013-08-19 13:30:59 +02:00
//Stop
2013-08-27 12:27:03 +02:00
if ($opts->mail) {
exit;
2013-08-19 13:30:59 +02:00
}
2013-08-27 12:27:03 +02:00
}
2013-08-19 13:30:59 +02:00
//Copie du fichier
$destDir = $c->profil->path->storage . '/' . $client . '/' . 'send';
2013-08-19 13:30:59 +02:00
if ( !is_dir($destDir) ) {
mkdir($destDir, 0755, true);
2013-08-19 13:30:59 +02:00
}
2013-08-27 12:27:03 +02:00
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";
}
//Suppression des fichiers
if ( $OptionsCopyDeleteAfter ) {
unlink( $fluxBasePath . '/' . $filenameIn );
if ( $OptionsRunWithEndFile ) {
unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete );
}
}
2013-08-27 12:27:03 +02:00
2013-08-19 13:30:59 +02:00
} else {
echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filenameIn dans $destDir\n";
2013-08-27 12:27:03 +02:00
}
2013-08-19 13:30:59 +02:00
}