328 lines
9.5 KiB
PHP
328 lines
9.5 KiB
PHP
<?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(__DIR__ . '/application'));
|
|
|
|
// --- Define application environment
|
|
defined('APPLICATION_ENV')
|
|
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
|
|
|
|
// --- Composer autoload
|
|
require_once realpath(__DIR__ . '/vendor/autoload.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) )
|
|
{
|
|
$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
|
|
$pathParts = pathinfo($opts->file);
|
|
$filenameIn = $pathParts['basename'];
|
|
$extension = '';
|
|
if (array_key_exists('extension', $pathParts))
|
|
{
|
|
$extension = $pathParts['extension'];
|
|
}
|
|
// --- Don't play with *.tck files
|
|
if ($extension == 'tck') {
|
|
passthru(__DIR__ . '/fileTck.php --file '.$opts->file.' >> /home/log/tck.log 2>&1');
|
|
exit;
|
|
}
|
|
$client = basename(dirname($pathParts['dirname']));
|
|
|
|
// --- 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))
|
|
{
|
|
$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'];
|
|
}
|
|
|
|
// --- Match prestation
|
|
if ($type == $p['type'] && $fluxRepository == $repositoryDir)
|
|
{
|
|
$prestation = $p['name'];
|
|
|
|
// --- Set options
|
|
if (array_key_exists('in', $p) && count($p['in']) > 0)
|
|
{
|
|
foreach ($p['in'] as $option => $value)
|
|
{
|
|
${'options'.$option} = $value;
|
|
}
|
|
}
|
|
|
|
// --- 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))
|
|
{
|
|
$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;
|
|
}
|
|
}
|
|
|
|
// --- 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;
|
|
}
|
|
}
|
|
|
|
// ---
|
|
|
|
// --- 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)
|
|
{
|
|
$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
|
|
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');
|
|
// --- 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;
|
|
}
|
|
}
|
|
|
|
// --- 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))
|
|
{
|
|
mkdir($destDir, 0755, true);
|
|
}
|
|
|
|
if (copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut))
|
|
{
|
|
echo date('Y-m-d H:i:s')." - Copie du fichier $filenameIn dans $destDir\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)
|
|
{
|
|
unlink( $fluxBasePath . '/' . $filenameIn );
|
|
|
|
if ($optionsRunWithEndFile)
|
|
{
|
|
unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete );
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filenameIn dans $destDir\n";
|
|
}
|
|
} |