2013-08-19 13:30:59 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Client => SD
|
|
|
|
* Attention le script doit s'executer avec l'utilisateur root
|
|
|
|
*/
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Define path to application directory
|
2013-08-19 13:30:59 +02:00
|
|
|
defined('APPLICATION_PATH')
|
2015-10-12 10:10:22 +02:00
|
|
|
|| define('APPLICATION_PATH', realpath(__DIR__ . '/application'));
|
2013-08-19 13:30:59 +02:00
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Define application environment
|
2013-09-16 12:28:50 +02:00
|
|
|
defined('APPLICATION_ENV')
|
2015-10-12 10:10:22 +02:00
|
|
|
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
|
2013-09-16 12:28:50 +02:00
|
|
|
|
2015-10-12 10:10:22 +02:00
|
|
|
// --- Composer autoload
|
|
|
|
require_once realpath(__DIR__ . '/vendor/autoload.php');
|
2013-08-19 13:30:59 +02:00
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Create application, bootstrap, and run
|
2015-10-12 10:10:22 +02:00
|
|
|
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
|
2013-08-19 13:30:59 +02:00
|
|
|
|
2017-03-02 14:17:33 +01:00
|
|
|
// --- Options
|
|
|
|
$displayUsage = false;
|
2013-08-19 13:30:59 +02:00
|
|
|
try {
|
2017-03-02 14:17:33 +01:00
|
|
|
$opts = new Zend_Console_Getopt(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",
|
|
|
|
));
|
2013-08-19 13:30:59 +02:00
|
|
|
$opts->parse();
|
2017-03-02 14:17:33 +01:00
|
|
|
$optsNb = $opts->getOptions();
|
2013-08-19 13:30:59 +02:00
|
|
|
} catch (Zend_Console_Getopt_Exception $e) {
|
2017-03-02 14:17:33 +01:00
|
|
|
$displayUsage = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Aide / Options
|
|
|
|
if ($optsNb == 0 || isset($opts->help)) {
|
|
|
|
$displayUsage = true;
|
2013-08-19 13:30:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Usage
|
2017-03-02 14:17:33 +01:00
|
|
|
if ($displayUsage) {
|
2013-08-19 13:30:59 +02:00
|
|
|
echo "Execute basic action when a customer send a file.\n";
|
|
|
|
echo $opts->getUsageMessage();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:37:05 +01:00
|
|
|
$logPath = realpath(__DIR__ . '/../shared');
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Get the file
|
2013-08-19 13:30:59 +02:00
|
|
|
if ( isset($opts->file) )
|
|
|
|
{
|
2014-10-01 10:59:58 +02:00
|
|
|
$optionsCopyAddDate = false;
|
|
|
|
$optionsCopyDeleteAfter = false;
|
|
|
|
$optionsRunWithEndFile = false;
|
2015-07-02 16:57:29 +02:00
|
|
|
$optionsLog = true;
|
2015-06-26 12:43:25 +02:00
|
|
|
$optionsRoute = array();
|
2015-06-30 11:32:45 +02:00
|
|
|
$optionsFilterName = false;
|
2014-10-01 10:59:58 +02:00
|
|
|
$repositoryDir = 'send';
|
|
|
|
|
2014-03-07 17:44:18 +01:00
|
|
|
$c = new Zend_Config($application->getOptions());
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Get the main directory name in FTP and SFTP
|
2013-08-19 13:30:59 +02:00
|
|
|
$pathParts = pathinfo($opts->file);
|
2014-03-07 17:44:18 +01:00
|
|
|
$filenameIn = $pathParts['basename'];
|
2013-08-27 12:27:03 +02:00
|
|
|
$extension = '';
|
2016-04-01 15:01:00 +02:00
|
|
|
if (array_key_exists('extension', $pathParts)) {
|
2013-08-27 12:27:03 +02:00
|
|
|
$extension = $pathParts['extension'];
|
|
|
|
}
|
2015-10-09 17:20:18 +02:00
|
|
|
// --- Don't play with *.tck files
|
|
|
|
if ($extension == 'tck') {
|
2017-03-02 14:37:05 +01:00
|
|
|
passthru(__DIR__ . '/tck.php --file '.$opts->file.' >> '.$logPath.'/tck.log 2>&1');
|
2015-10-09 17:20:18 +02:00
|
|
|
exit;
|
|
|
|
}
|
2013-08-19 13:30:59 +02:00
|
|
|
$client = basename(dirname($pathParts['dirname']));
|
2013-08-27 12:27:03 +02:00
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Base path, type and repository
|
|
|
|
$startpos = strlen( $c->profil->path->data . '/' );
|
2016-04-01 15:01:00 +02:00
|
|
|
if ('sftp' == substr($opts->file, $startpos, 4)) {
|
2013-08-27 12:27:03 +02:00
|
|
|
$type = 'SFTP';
|
2014-03-07 17:44:18 +01:00
|
|
|
$fluxBasePath = $c->profil->path->sftp . '/' . $client;
|
2014-10-01 10:59:58 +02:00
|
|
|
}
|
2016-04-01 15:01:00 +02:00
|
|
|
elseif ('ftp' == substr($opts->file, $startpos, 3)) {
|
2013-08-27 12:27:03 +02:00
|
|
|
$type = 'FTP';
|
2014-03-07 17:44:18 +01:00
|
|
|
$fluxBasePath = $c->profil->path->ftp . '/' . $client;
|
2013-08-19 13:30:59 +02:00
|
|
|
}
|
2014-10-01 10:59:58 +02:00
|
|
|
$fluxRepository = str_replace(array($fluxBasePath.'/', '/'.$filenameIn), array('', ''), $opts->file);
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Match prestation
|
2016-06-17 11:08:25 +02:00
|
|
|
$prestations = include __DIR__ . '/config.php';
|
2014-10-01 10:59:58 +02:00
|
|
|
$prestation = null;
|
2016-04-01 15:01:00 +02:00
|
|
|
if (array_key_exists($client, $prestations)) {
|
2014-10-01 10:59:58 +02:00
|
|
|
$clientPrestations = $prestations[$client]['prestations'];
|
2016-04-01 15:01:00 +02:00
|
|
|
foreach ($clientPrestations as $i => $p) {
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Not default repository dir
|
2016-04-01 15:01:00 +02:00
|
|
|
if (array_key_exists('directory', $p) && !empty($p['directory'])) {
|
2015-03-31 08:44:13 +02:00
|
|
|
$repositoryDir = $p['directory'];
|
2014-10-01 10:59:58 +02:00
|
|
|
}
|
2013-08-27 12:27:03 +02:00
|
|
|
|
2015-06-30 11:32:45 +02:00
|
|
|
// --- Match prestation
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($type == $p['type'] && $fluxRepository == $repositoryDir) {
|
2015-03-31 08:44:13 +02:00
|
|
|
$prestation = $p['name'];
|
2014-10-01 10:59:58 +02:00
|
|
|
|
2015-06-30 11:32:45 +02:00
|
|
|
// --- Set options
|
2016-04-01 15:01:00 +02:00
|
|
|
if (array_key_exists('in', $p) && count($p['in']) > 0) {
|
|
|
|
foreach ($p['in'] as $option => $value) {
|
2014-10-01 10:59:58 +02:00
|
|
|
${'options'.$option} = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 11:32:45 +02:00
|
|
|
// --- Filters
|
|
|
|
if ($optionsFilterName === true) {
|
|
|
|
if ( strpos($filenameIn, $prestation) === false ) {
|
2015-07-03 08:21:00 +02:00
|
|
|
$prestation = null; continue;
|
2015-06-30 11:32:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_string($optionsFilterName) && strlen($optionsFilterName) > 0) {
|
|
|
|
if (strpos($filenameIn, $optionsFilterName) === false) {
|
2015-07-03 08:21:00 +02:00
|
|
|
$prestation = null; continue;
|
2015-06-30 11:32:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 10:59:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($prestation === null) {
|
2014-10-01 10:59:58 +02:00
|
|
|
echo date('Y-m-d H:i:s')." - Prestation not found !\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$fluxBasePath .= '/'.$repositoryDir;
|
2013-08-27 12:27:03 +02:00
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Use ".fin" or ".end" files to do something
|
2014-03-07 17:44:18 +01:00
|
|
|
$runExtensions = array('fin', 'end');
|
2016-04-01 15:01:00 +02:00
|
|
|
if (in_array($extension, $runExtensions)) {
|
|
|
|
if ($optionsRunWithEndFile) {
|
2014-10-01 10:59:58 +02:00
|
|
|
$extToDelete = $extension;
|
2016-04-01 15:01:00 +02:00
|
|
|
if (file_exists($fluxBasePath . '/' . $filenameIn)) {
|
2014-03-07 17:44:18 +01:00
|
|
|
$pathParts = pathinfo($fluxBasePath . '/' . $filenameIn);
|
|
|
|
$filenameIn = $pathParts['basename'];
|
2014-10-01 10:59:58 +02:00
|
|
|
}
|
2016-04-01 15:01:00 +02:00
|
|
|
else {
|
|
|
|
echo "Fichier fin inexistant $filenameIn\n";
|
2014-03-07 17:44:18 +01:00
|
|
|
exit;
|
|
|
|
}
|
2014-10-01 10:59:58 +02:00
|
|
|
}
|
2016-04-01 15:01:00 +02:00
|
|
|
else {
|
2014-03-07 17:44:18 +01:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Get the realname of file IN or exit
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($optionsRunWithEndFile) {
|
|
|
|
if (in_array($extension, $runExtensions)) {
|
2014-06-09 10:37:32 +02:00
|
|
|
$extensionLength = strlen($extension)+1;
|
|
|
|
$filenameIn = substr($filenameIn, 0, strlen($filenameIn) - $extensionLength);
|
|
|
|
$extension = '';
|
2014-10-01 10:59:58 +02:00
|
|
|
}
|
2016-04-01 15:01:00 +02:00
|
|
|
else {
|
2014-06-09 10:37:32 +02:00
|
|
|
exit;
|
|
|
|
}
|
2014-03-07 17:44:18 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 11:32:45 +02:00
|
|
|
// ---
|
|
|
|
|
2015-03-30 15:53:03 +02:00
|
|
|
// --- Detail du fichier
|
|
|
|
$nbLines = 0;
|
|
|
|
if ( strtolower(substr($filenameIn, -3)) == 'csv' ) {
|
|
|
|
$lines = file($fluxBasePath . '/' . $filenameIn);
|
|
|
|
$nbLines = count($lines);
|
|
|
|
}
|
|
|
|
$size = filesize($fluxBasePath . '/' . $filenameIn);
|
2016-04-01 15:01:00 +02:00
|
|
|
$dateFile = date('YmdHis', filectime($fluxBasePath . '/' . $filenameIn));
|
2014-03-07 17:44:18 +01:00
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Define default out filename
|
2014-03-26 19:01:33 +01:00
|
|
|
$filenameOut = $filenameIn;
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Add date to filename
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($optionsCopyAddDate) {
|
2013-08-27 12:27:03 +02:00
|
|
|
$extensionLength = 0;
|
2016-04-01 15:01:00 +02:00
|
|
|
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);
|
2014-03-07 17:44:18 +01:00
|
|
|
$filenameOut = $filenameOut . '_' . date('YmdHis') . '.' . $extension;
|
2014-10-01 10:59:58 +02:00
|
|
|
}
|
2016-04-01 15:01:00 +02:00
|
|
|
else {
|
2014-03-07 17:44:18 +01:00
|
|
|
$filenameOut = $filenameIn . '_' . date('YmdHis');
|
2013-08-27 12:27:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Prepare mail
|
2016-04-01 15:01:00 +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";
|
2014-03-07 17:44:18 +01:00
|
|
|
$txt.= "Fichier : ".$filenameIn."\n";
|
2013-08-19 13:30:59 +02:00
|
|
|
$txt.= "Nombre de Lignes : $nbLines\n";
|
2015-03-30 15:53:03 +02:00
|
|
|
$txt.= "Taille du fichier : $size\n";
|
2013-08-27 12:27:03 +02:00
|
|
|
|
2013-09-18 13:43:40 +02:00
|
|
|
$mail = new Zend_Mail('UTF-8');
|
2015-06-17 17:06:49 +02:00
|
|
|
// --- 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();
|
|
|
|
}
|
2013-08-27 12:27:03 +02:00
|
|
|
$mail->setDefaultTransport($tr);
|
2013-08-19 13:30:59 +02:00
|
|
|
$mail->setBodyText($txt);
|
2015-06-17 17:06:49 +02:00
|
|
|
$mail->setFrom('supportdev@scores-decisions.com', 'Machine Flux');
|
2013-08-28 21:58:09 +02:00
|
|
|
$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
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Stop
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($opts->mail) {
|
2013-08-27 12:27:03 +02:00
|
|
|
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
|
|
|
|
2015-06-26 12:43:25 +02:00
|
|
|
// --- Before store the file send to another repository
|
|
|
|
if (count($optionsRoute) > 0) {
|
|
|
|
foreach ($optionsRoute as $tr => $value) {
|
|
|
|
switch($tr) {
|
|
|
|
case 'cp':
|
2015-06-30 11:32:45 +02:00
|
|
|
if (copy($fluxBasePath . '/' . $filenameIn, $value . '/' . $filenameOut)) {
|
2015-06-26 12:43:25 +02:00
|
|
|
echo date('Y-m-d H:i:s')." - Copie du fichier $filenameIn dans $value\n";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Copy file
|
2014-03-07 17:44:18 +01:00
|
|
|
$destDir = $c->profil->path->storage . '/' . $client . '/' . 'send';
|
2013-08-19 13:30:59 +02:00
|
|
|
|
2016-04-01 15:01:00 +02:00
|
|
|
if (!is_dir($destDir)) {
|
2014-03-07 17:44:18 +01:00
|
|
|
mkdir($destDir, 0755, true);
|
2013-08-19 13:30:59 +02:00
|
|
|
}
|
2013-08-27 12:27:03 +02:00
|
|
|
|
2016-04-01 15:01:00 +02:00
|
|
|
if (copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut)) {
|
2014-03-07 17:44:18 +01:00
|
|
|
echo date('Y-m-d H:i:s')." - Copie du fichier $filenameIn dans $destDir\n";
|
|
|
|
|
2015-03-31 08:44:13 +02:00
|
|
|
// --- Execute
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($optionsLog === true) {
|
2014-10-07 10:30:01 +02:00
|
|
|
$db = Zend_Db::factory($c->profil->db->metier);
|
|
|
|
Zend_Db_Table::setDefaultAdapter($db);
|
|
|
|
try {
|
|
|
|
$fluxM = new Application_Model_Sdv1FluxFileIn();
|
|
|
|
$fluxM->insert(array(
|
2015-06-30 11:32:45 +02:00
|
|
|
'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
|
2014-10-07 10:30:01 +02:00
|
|
|
));
|
|
|
|
echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filenameOut\n";
|
2016-04-01 15:01:00 +02:00
|
|
|
} catch (Zend_Db_Exception $e) {
|
2014-10-07 10:30:01 +02:00
|
|
|
echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filenameOut\n";
|
|
|
|
}
|
2014-03-07 17:44:18 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 08:44:13 +02:00
|
|
|
// --- Suppression des fichiers
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($optionsCopyDeleteAfter) {
|
2014-03-07 17:44:18 +01:00
|
|
|
unlink( $fluxBasePath . '/' . $filenameIn );
|
2016-04-01 15:01:00 +02:00
|
|
|
if ($optionsRunWithEndFile) {
|
2014-03-07 17:44:18 +01:00
|
|
|
unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete );
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 12:27:03 +02:00
|
|
|
|
2014-10-01 10:59:58 +02:00
|
|
|
}
|
2016-04-01 15:01:00 +02:00
|
|
|
else {
|
2014-03-07 17:44:18 +01:00
|
|
|
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
|
|
|
}
|