Import older code
This commit is contained in:
parent
d7ec1a3a03
commit
9cf28665d1
@ -7,6 +7,7 @@ use Zend\Mvc\Controller\AbstractActionController;
|
||||
use Zend\Stdlib\RequestInterface;
|
||||
use Zend\Stdlib\ResponseInterface;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Application\Entity\FluxFilein;
|
||||
|
||||
class FileController extends AbstractActionController
|
||||
{
|
||||
@ -55,6 +56,7 @@ class FileController extends AbstractActionController
|
||||
/**
|
||||
* Fichier SD => Client
|
||||
* Depot d'un fichier dans le repertoire client du FTP (recv)
|
||||
* Traitement par lot
|
||||
*/
|
||||
public function recvAction()
|
||||
{
|
||||
@ -63,7 +65,10 @@ class FileController extends AbstractActionController
|
||||
$dryrun = $request->getParam('dry-run', false) || $request->getParam('n', false);
|
||||
|
||||
// Get config
|
||||
$globalConfig = array();
|
||||
$pathConfig = array();
|
||||
if (array_key_exists('path', $this->appConfig)) {
|
||||
$pathConfig = $this->appConfig['path'];
|
||||
}
|
||||
|
||||
// Get definition
|
||||
$reposConfig = array();
|
||||
@ -71,23 +76,137 @@ class FileController extends AbstractActionController
|
||||
$reposConfig = $this->appConfig['repository'];
|
||||
}
|
||||
|
||||
if (count($reposConfig) == 0 || count($globalConfig) == 0) {
|
||||
if (count($reposConfig) == 0 || count($pathConfig) == 0) {
|
||||
$this->output("Aucune configuration.");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// Get file not sent
|
||||
$results = $this->entityManager
|
||||
->getRepository('Application\Entity\FluxFileout')->getNotSent();
|
||||
$fluxM = $this->entityManager->getRepository('Application\Entity\FluxFileout');
|
||||
$results = $fluxM->getNotSent();
|
||||
if (count($results) == 0) {
|
||||
$this->output("Rien à traiter.", $verbose);
|
||||
}
|
||||
// Browse files to exec operation
|
||||
else {
|
||||
// Browse files to exec operation
|
||||
foreach($results as $result) {
|
||||
|
||||
$filename = $result->getFileout();
|
||||
$client = $result->getClient();
|
||||
|
||||
$source = $pathConfig['storage'] . '/' . $client . '/recv/' . $filename;
|
||||
|
||||
$fluxRepository = 'recv';
|
||||
if ($result->getDepotdir() != '') {
|
||||
$fluxRepository = $result->getDepotdir();
|
||||
}
|
||||
|
||||
echo $result->getFileout();
|
||||
switch ($result->getDepottype()) {
|
||||
case 'FTP':
|
||||
$dest = $pathConfig['ftp'];
|
||||
break;
|
||||
case 'SFTP':
|
||||
$dest = $pathConfig['sftp'];
|
||||
break;
|
||||
}
|
||||
$dest.= '/' . $client . '/' . $fluxRepository . '/' . $filename;
|
||||
|
||||
// Options
|
||||
$optionsCopyAddDate = false;
|
||||
$optionsCopyDeleteAfter = false;
|
||||
$optionsRunWithEndFile = false;
|
||||
$optionsLog = false;
|
||||
|
||||
// Match prestation
|
||||
$prestation = null;
|
||||
if (array_key_exists($client, $reposConfig)) {
|
||||
$clientPrestations = $reposConfig[$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 ($result->getDepottype() == $p['type'] && $fluxRepository == $repositoryDir) {
|
||||
$prestation = $p['name'];
|
||||
|
||||
// Set options
|
||||
if (array_key_exists('out', $p) && count($p['out']) > 0) {
|
||||
foreach ($p['out'] as $option => $value) {
|
||||
${'options'.$option} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copie fichier
|
||||
if ( copy($source, $dest) ) {
|
||||
// Detail du fichier
|
||||
$nbLines = 0;
|
||||
if ( strtolower(substr(basename($source), -3)) == 'csv' ) {
|
||||
$lines = file($source);
|
||||
$nbLines = count($lines);
|
||||
}
|
||||
$size = filesize($source);
|
||||
|
||||
// Permission du depot
|
||||
if ($result->getDepottype() == 'FTP') {
|
||||
chown($dest, 'ftpuser');
|
||||
chgrp($dest, 'ftpgroup');
|
||||
}
|
||||
elseif($result->getDepottype() == 'SFTP') {
|
||||
chown($dest, $client);
|
||||
}
|
||||
|
||||
// Mise à jour information de remise
|
||||
$result->setDepotfilesize($size);
|
||||
$result->setDepotdate(new \DateTime());
|
||||
$this->entityManager->flush();
|
||||
|
||||
// --- 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');
|
||||
// --- 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();
|
||||
}*/
|
||||
} else {
|
||||
$this->output("Impossible de copier le fichier $source vers $dest");
|
||||
}
|
||||
$this->output("", $verbose);
|
||||
}
|
||||
}
|
||||
@ -105,7 +224,152 @@ class FileController extends AbstractActionController
|
||||
$request = $this->getRequest();
|
||||
$verbose = $request->getParam('verbose', false) || $request->getParam('v', false);
|
||||
$dryrun = $request->getParam('dry-run', false) || $request->getParam('n', false);
|
||||
$filepath = $request->getParam('filepath');
|
||||
|
||||
// Get config
|
||||
$pathConfig = array();
|
||||
if (array_key_exists('path', $this->appConfig)) {
|
||||
$pathConfig = $this->appConfig['path'];
|
||||
}
|
||||
|
||||
// Get definition
|
||||
$reposConfig = array();
|
||||
if (array_key_exists('repository', $this->appConfig)) {
|
||||
$reposConfig = $this->appConfig['repository'];
|
||||
}
|
||||
|
||||
if (count($reposConfig) == 0 || count($pathConfig) == 0) {
|
||||
$this->output("Aucune configuration.");
|
||||
}
|
||||
else {
|
||||
// Get the main directory name in FTP and SFTP
|
||||
$pathParts = pathinfo($filepath);
|
||||
$filenameIn = $pathParts['basename'];
|
||||
$extension = '';
|
||||
if (array_key_exists('extension', $pathParts)) {
|
||||
$extension = $pathParts['extension'];
|
||||
}
|
||||
$client = basename(dirname($pathParts['dirname']));
|
||||
|
||||
// Base path, type and repository
|
||||
if ($pathConfig['sftp'] == substr($filepath, 0, strlen($pathConfig['sftp']))) {
|
||||
$type = 'SFTP';
|
||||
$fluxBasePath = $pathConfig['sftp'] . '/' . $client;
|
||||
}
|
||||
elseif ($pathConfig['ftp'] == substr($filepath, 0, strlen($pathConfig['ftp']))) {
|
||||
$type = 'FTP';
|
||||
$fluxBasePath = $pathConfig['ftp'] . '/' . $client;
|
||||
}
|
||||
$fluxRepository = str_replace(
|
||||
array($fluxBasePath.'/', '/'.$filenameIn),
|
||||
array('', ''),
|
||||
$filepath);
|
||||
|
||||
// Options
|
||||
$optionsCopyAddDate = false;
|
||||
$optionsCopyDeleteAfter = false;
|
||||
$optionsRunWithEndFile = false;
|
||||
$optionsLog = false;
|
||||
|
||||
// Match prestation
|
||||
$prestation = null;
|
||||
if (array_key_exists($client, $reposConfig)) {
|
||||
$clientPrestations = $reposConfig[$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 ($result->getDepottype() == $p['type'] && $fluxRepository == $repositoryDir) {
|
||||
$prestation = $p['name'];
|
||||
|
||||
// Set options
|
||||
if (array_key_exists('out', $p) && count($p['out']) > 0) {
|
||||
foreach ($p['out'] 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 or Debug mode
|
||||
/*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) {
|
||||
// Get file not sent
|
||||
$fluxM = $this->entityManager->getRepository('Application\Entity\FluxFileout');
|
||||
$result = $fluxM->getByFilename($filenameIn);
|
||||
if ($result !== null) {
|
||||
if ($result->getDepotdateread() == 0){
|
||||
try {
|
||||
$fluxM->setDepotDateRead(new \DateTime());
|
||||
$this->entityManager->flush();
|
||||
$this->output("Enregistrement client:$client fichier:$filenameIn");
|
||||
} catch (\Doctrine\ORM\ORMException $e) {
|
||||
$this->output("ERREUR Enregistrement client:$client fichier:$filenameIn");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Last line blank to always add a carrier return in console
|
||||
$this->output("");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,36 +380,527 @@ class FileController extends AbstractActionController
|
||||
$request = $this->getRequest();
|
||||
$verbose = $request->getParam('verbose', false) || $request->getParam('v', false);
|
||||
$dryrun = $request->getParam('dry-run', false) || $request->getParam('n', false);
|
||||
$filepath = $request->getParam('filepath');
|
||||
|
||||
// Get config
|
||||
$pathConfig = array();
|
||||
if (array_key_exists('path', $this->appConfig)) {
|
||||
$pathConfig = $this->appConfig['path'];
|
||||
}
|
||||
|
||||
// Get definition
|
||||
$reposConfig = array();
|
||||
if (array_key_exists('repository', $this->appConfig)) {
|
||||
$reposConfig = $this->appConfig['repository'];
|
||||
}
|
||||
|
||||
if (count($reposConfig) == 0 || count($pathConfig) == 0) {
|
||||
$this->output("Aucune configuration.");
|
||||
}
|
||||
else {
|
||||
// Get the main directory name in FTP and SFTP
|
||||
$pathParts = pathinfo($filepath);
|
||||
$filenameIn = $pathParts['basename'];
|
||||
$extension = '';
|
||||
if (array_key_exists('extension', $pathParts)) {
|
||||
$extension = $pathParts['extension'];
|
||||
}
|
||||
|
||||
// Only file with .tck
|
||||
if ($extension != 'tck') {
|
||||
exit;
|
||||
}
|
||||
|
||||
$client = basename(dirname($pathParts['dirname']));
|
||||
$filenameSearch = str_replace('.tck', '', $filenameIn);
|
||||
|
||||
// Base path, type and repository
|
||||
if ($pathConfig['sftp'] == substr($filepath, 0, strlen($pathConfig['sftp']))) {
|
||||
$type = 'SFTP';
|
||||
$fluxBasePath = $pathConfig['sftp'] . '/' . $client;
|
||||
}
|
||||
elseif ($pathConfig['ftp'] == substr($filepath, 0, strlen($pathConfig['ftp']))) {
|
||||
$type = 'FTP';
|
||||
$fluxBasePath = $pathConfig['ftp'] . '/' . $client;
|
||||
}
|
||||
$fluxRepository = str_replace(
|
||||
array($fluxBasePath.'/', '/'.$filenameIn),
|
||||
array('', ''),
|
||||
$filepath);
|
||||
|
||||
// Match prestation
|
||||
$prestation = null;
|
||||
if (array_key_exists($client, $reposConfig)) {
|
||||
$clientPrestations = $reposConfig[$client]['prestations'];
|
||||
foreach ($clientPrestations as $i => $p) {
|
||||
// Not default repository dir
|
||||
$repositoryDir = 'send';
|
||||
if (array_key_exists('directory', $p) && !empty($p['directory'])) {
|
||||
$repositoryDir = $p['directory'];
|
||||
}
|
||||
|
||||
// Match prestation
|
||||
if ($result->getDepottype() == $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;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$fluxBasePath .= '/' . $repositoryDir;
|
||||
|
||||
// --- Prepare mail or Debug mode
|
||||
/*if ($opts->mail || $opts->debug)
|
||||
{
|
||||
$subject = "[Flux] - Transfert fichier $client";
|
||||
$txt = "Transfert d'un fichier\n";
|
||||
$txt.= "Client : $client\n";
|
||||
$txt.= "Fichier : ".$filenameIn."\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) {
|
||||
// Mark entity
|
||||
$fluxM = $this->entityManager->getRepository('Application\Entity\FluxFilein');
|
||||
$result = $fluxM->getByFilename($filenameSearch);
|
||||
try {
|
||||
$fluxM->setTransfertdate(new \DateTime());
|
||||
$this->entityManager->flush();
|
||||
$this->output("Enregistrement client:$client fichier:$filenameSearch");
|
||||
unlink($fluxBasePath . '/' . $filenameIn);
|
||||
} catch (\Doctrine\ORM\ORMException $e) {
|
||||
$this->output("ERREUR Enregistrement client:$client fichier:$filenameSearch");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Last line blank to always add a carrier return in console
|
||||
$this->output("");
|
||||
}
|
||||
|
||||
/**
|
||||
* flux_fileout
|
||||
* Nettoyage des fichiers
|
||||
*/
|
||||
public function cleanAction()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
$verbose = $request->getParam('verbose', false) || $request->getParam('v', false);
|
||||
$dryrun = $request->getParam('dry-run', false) || $request->getParam('n', false);
|
||||
|
||||
// Get config
|
||||
$pathConfig = array();
|
||||
if (array_key_exists('path', $this->appConfig)) {
|
||||
$pathConfig = $this->appConfig['path'];
|
||||
}
|
||||
|
||||
// Get definition
|
||||
$reposConfig = array();
|
||||
if (array_key_exists('repository', $this->appConfig)) {
|
||||
$reposConfig = $this->appConfig['repository'];
|
||||
}
|
||||
|
||||
if (count($reposConfig) == 0 || count($pathConfig) == 0) {
|
||||
$this->output("Aucune configuration.");
|
||||
}
|
||||
// Suppression des fichiers de plus de 5 jours suivant la table
|
||||
else {
|
||||
|
||||
$dateEnd = new \DateTime();
|
||||
$dateEnd->sub('P5D');
|
||||
|
||||
$fluxM = $this->entityManager->getRepository('Application\Entity\FluxFileout');
|
||||
$results = $fluxM->getOld($dateEnd->format('YmdHis'));
|
||||
if (count($results) == 0) {
|
||||
$this->output("Aucun fichier à supprimer.", $verbose);
|
||||
}
|
||||
else {
|
||||
foreach ($results as $item) {
|
||||
$path = '';
|
||||
switch($item->getDepottype()) {
|
||||
case 'FTP':
|
||||
$path = $pathConfig['ftp'];
|
||||
break;
|
||||
case 'SFTP':
|
||||
$path = $pathConfig['sftp'];
|
||||
break;
|
||||
}
|
||||
$path.= '/' . $item->getClient();
|
||||
if ($item->getDepotdir() == '') {
|
||||
$path.= '/recv';
|
||||
} else {
|
||||
$path.= '/' . $item->getDepotdir();
|
||||
}
|
||||
$filename = $path . '/' . $item->getFileout();
|
||||
if (file_exists($filename) && is_file($filename)) {
|
||||
unlink($filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback pour suppression des fichiers de plus de 30 jours
|
||||
$path = array(
|
||||
$pathConfig['ftp'],
|
||||
$pathConfig['sftp']
|
||||
);
|
||||
|
||||
// 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 {} \;");
|
||||
}
|
||||
|
||||
// Last line blank to always add a carrier return in console
|
||||
$this->output("");
|
||||
}
|
||||
|
||||
/**
|
||||
* flux_fileout
|
||||
* Trigger all events and redirect
|
||||
*/
|
||||
public function triggerAction()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
$verbose = $request->getParam('verbose', false) || $request->getParam('v', false);
|
||||
$dryrun = $request->getParam('dry-run', false) || $request->getParam('n', false);
|
||||
$filepath = $request->getParam('filepath');
|
||||
$event = $request->getParam('event');
|
||||
|
||||
$pathParts = pathinfo($filepath);
|
||||
$filename = $pathParts['basename'];
|
||||
$extension = $pathParts['extension'];
|
||||
|
||||
switch($event) {
|
||||
case 'IN_CLOSE_WRITE':
|
||||
if ($extension == 'tck') {
|
||||
passthru(__DIR__ . '/cli tck '.$filepath.' >> /home/log/tck.log 2>&1');
|
||||
} else {
|
||||
passthru(__DIR__ . '/cli send '.$filepath.' >> /home/log/send.log 2>&1');
|
||||
}
|
||||
break;
|
||||
case 'IN_ACCESS':
|
||||
case 'IN_CLOSE_NOWRITE':
|
||||
passthru(__DIR__ . '/cli read '.$filepath.' >> /home/log/read.log 2>&1');
|
||||
break;
|
||||
case 'IN_DELETE':
|
||||
passthru(__DIR__ . '/cli read '.$filepath.' >> /home/log/read.log 2>&1');
|
||||
break;
|
||||
}
|
||||
|
||||
// Last line blank to always add a carrier return in console
|
||||
$this->output("");
|
||||
}
|
||||
|
||||
/**
|
||||
* flux_filein
|
||||
* Fichier Client => SD
|
||||
* Réception d'un fichier dans le repertoire client du FTP (send)
|
||||
*/
|
||||
public function sendAction()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
$verbose = $request->getParam('verbose', false) || $request->getParam('v', false);
|
||||
$dryrun = $request->getParam('dry-run', false) || $request->getParam('n', false);
|
||||
$filepath = $request->getParam('filepath');
|
||||
|
||||
// Get config
|
||||
$pathConfig = array();
|
||||
if (array_key_exists('path', $this->appConfig)) {
|
||||
$pathConfig = $this->appConfig['path'];
|
||||
}
|
||||
|
||||
// Get definition
|
||||
$reposConfig = array();
|
||||
if (array_key_exists('repository', $this->appConfig)) {
|
||||
$reposConfig = $this->appConfig['repository'];
|
||||
}
|
||||
|
||||
if (count($reposConfig) == 0 || count($pathConfig) == 0) {
|
||||
$this->output("Aucune configuration.");
|
||||
}
|
||||
else {
|
||||
|
||||
// Get the main directory name in FTP and SFTP
|
||||
$pathParts = pathinfo($filepath);
|
||||
$filenameIn = $pathParts['basename'];
|
||||
$extension = '';
|
||||
if (array_key_exists('extension', $pathParts)) {
|
||||
$extension = $pathParts['extension'];
|
||||
}
|
||||
// Don't play with *.tck files
|
||||
if ($extension == 'tck') {
|
||||
passthru(__DIR__ . '/cli tck '.$filepath.' >> /home/log/tck.log 2>&1');
|
||||
exit;
|
||||
}
|
||||
$client = basename(dirname($pathParts['dirname']));
|
||||
|
||||
// Base path, type and repository
|
||||
if ($pathConfig['sftp'] == substr($filepath, 0, strlen($pathConfig['sftp']))) {
|
||||
$type = 'SFTP';
|
||||
$fluxBasePath = $pathConfig['sftp'] . '/' . $client;
|
||||
}
|
||||
elseif ($pathConfig['ftp'] == substr($filepath, 0, strlen($pathConfig['ftp']))) {
|
||||
$type = 'FTP';
|
||||
$fluxBasePath = $pathConfig['ftp'] . '/' . $client;
|
||||
}
|
||||
$fluxRepository = str_replace(
|
||||
array($fluxBasePath.'/', '/'.$filenameIn),
|
||||
array('', ''),
|
||||
$filepath);
|
||||
|
||||
$optionsCopyAddDate = false;
|
||||
$optionsCopyDeleteAfter = false;
|
||||
$optionsRunWithEndFile = false;
|
||||
$optionsLog = true;
|
||||
$optionsRoute = array();
|
||||
$optionsFilterName = false;
|
||||
|
||||
// Match prestation
|
||||
$prestation = null;
|
||||
if (array_key_exists($client, $reposConfig)) {
|
||||
$clientPrestations = $reposConfig[$client]['prestations'];
|
||||
foreach ($clientPrestations as $i => $p) {
|
||||
// Not default repository dir
|
||||
$repositoryDir = 'send';
|
||||
if (array_key_exists('directory', $p) && !empty($p['directory'])) {
|
||||
$repositoryDir = $p['directory'];
|
||||
}
|
||||
|
||||
// Match prestation
|
||||
if ($result->getDepottype() == $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) {
|
||||
$this->output("Prestation not found !");
|
||||
}
|
||||
|
||||
// Set directory
|
||||
$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'];
|
||||
}
|
||||
else {
|
||||
$this->output("Fichier fin inexistant $filenameIn");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Do nothing
|
||||
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 {
|
||||
// Do nothing
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// Detail du fichier
|
||||
$nbLines = 0;
|
||||
if (strtolower(substr($filenameIn, -3)) == 'csv') {
|
||||
$lines = file($fluxBasePath . '/' . $filenameIn);
|
||||
$nbLines = count($lines);
|
||||
}
|
||||
$size = filesize($fluxBasePath . '/' . $filenameIn);
|
||||
$dateFile = date('YmdHis', filectime($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)) {
|
||||
$this->output("Copie du fichier $filenameIn dans $value");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Copy file
|
||||
$destDir = $pathConfig['storage'] . '/' . $client . '/' . 'send';
|
||||
|
||||
if (!is_dir($destDir)) {
|
||||
mkdir($destDir, 0755, true);
|
||||
}
|
||||
|
||||
if (copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut)) {
|
||||
$this->output("Copie du fichier $filenameIn dans $destDir");
|
||||
|
||||
// Execute
|
||||
if ($optionsLog === true) {
|
||||
try {
|
||||
$fluxM = new FluxFilein;
|
||||
$fluxM->setClient($client);
|
||||
$fluxM->setName($prestation);
|
||||
$fluxM->setDepottype($type);
|
||||
$fluxM->setDepotdate(\DateTime::createFromFormat('YmdHis', $dateFile));
|
||||
$fluxM->setDepotfile($filenameOut);
|
||||
$fluxM->setNblines($nbLines);
|
||||
$fluxM->setDepotfilesize($size);
|
||||
$fluxM->setDateinsert(new \DateTime());
|
||||
$this->entityManager->persist($fluxM);
|
||||
$this->entityManager->flush();
|
||||
$this->output("Enregistrement client:$client fichier:$filenameOut");
|
||||
} catch (\Doctrine\ORM\ORMException $e) {
|
||||
$this->output("ERREUR Enregistrement client:$client fichier:$filenameOut");
|
||||
}
|
||||
}
|
||||
|
||||
// Suppression des fichiers
|
||||
if ($optionsCopyDeleteAfter) {
|
||||
unlink( $fluxBasePath . '/' . $filenameIn );
|
||||
if ($optionsRunWithEndFile) {
|
||||
unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
$this->output("ERREUR Copie du fichier $filenameIn dans $destDir");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Last line blank to always add a carrier return in console
|
||||
$this->output("");
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user