flux/read.php

185 lines
5.6 KiB
PHP
Raw Permalink Normal View History

2015-03-31 09:04:11 +02:00
<?php
/**
2015-09-11 15:58:40 +02:00
* Indicateur de récupération par le client
2015-03-31 09:04:11 +02:00
* Attention le script doit s'executer avec l'utilisateur root
*/
2015-10-12 10:10:22 +02:00
// --- Define path to application directory
2015-03-31 09:04:11 +02:00
defined('APPLICATION_PATH')
2015-10-12 10:10:22 +02:00
|| define('APPLICATION_PATH', realpath(__DIR__ . '/application'));
2015-03-31 09:04:11 +02:00
2015-10-12 10:10:22 +02:00
// --- Define application environment
2015-03-31 09:04:11 +02:00
defined('APPLICATION_ENV')
2015-10-12 10:10:22 +02:00
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
2015-03-31 09:04:11 +02:00
2015-10-12 10:10:22 +02:00
// --- Composer autoload
require_once realpath(__DIR__ . '/vendor/autoload.php');
2015-03-31 09:04:11 +02:00
2015-10-12 10:10:22 +02:00
// --- Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
2015-03-31 09:04:11 +02:00
2017-03-02 14:17:33 +01:00
// --- Options
$displayUsage = false;
2015-03-31 09:04:11 +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",
'debug' => "Send a mail for debug",
));
2015-03-31 09:04:11 +02:00
$opts->parse();
2017-03-02 14:17:33 +01:00
$optsNb = $opts->getOptions();
2015-03-31 09:04:11 +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;
2015-03-31 09:04:11 +02:00
}
2015-10-09 16:57:43 +02:00
// --- Usage
2017-03-02 14:17:33 +01:00
if ($displayUsage) {
2015-11-19 16:25:31 +01:00
echo "Execute basic action when a customer read a file.\n";
2015-03-31 09:04:11 +02:00
echo $opts->getUsageMessage();
exit;
}
2015-10-09 16:57:43 +02:00
// --- Get the file
2015-03-31 09:04:11 +02:00
if ( isset($opts->file) )
{
$optionsNoLog = false;
$repositoryDir = 'recv';
$c = new Zend_Config($application->getOptions());
2015-10-09 16:57:43 +02:00
// --- Get the main directory name in FTP and SFTP
2015-03-31 09:04:11 +02:00
$pathParts = pathinfo($opts->file);
$filenameIn = $pathParts['basename'];
$extension = '';
if (array_key_exists('extension', $pathParts))
{
$extension = $pathParts['extension'];
}
$client = basename(dirname($pathParts['dirname']));
2015-10-09 16:57:43 +02:00
// --- Base path, type and repository
2015-03-31 09:04:11 +02:00
$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);
2015-10-09 16:57:43 +02:00
// --- Match prestation
2016-06-17 11:08:25 +02:00
$prestations = include __DIR__ . '/config.php';
2015-03-31 09:04:11 +02:00
$prestation = null;
if (array_key_exists($client, $prestations))
{
$clientPrestations = $prestations[$client]['prestations'];
foreach ($clientPrestations as $i => $p)
{
2015-10-09 16:57:43 +02:00
// --- Not default repository dir
2015-03-31 09:04:11 +02:00
if (array_key_exists('directory', $p) && !empty($p['directory']))
{
$repositoryDir = $p['directory'];
}
if ($type == $p['type'] && $fluxRepository == $repositoryDir)
{
$prestation = $p['name'];
2015-10-09 16:57:43 +02:00
// --- Set option
2015-03-31 09:04:11 +02:00
if (array_key_exists('in', $p) && count($p['in']) > 0)
{
foreach ($p['in'] as $option => $value)
{
${'options'.$option} = $value;
}
}
break;
}
}
}
2015-10-09 16:57:43 +02:00
if ($prestation === null) {
2015-03-31 09:04:11 +02:00
echo date('Y-m-d H:i:s')." - Prestation not found !\n";
}
$fluxBasePath .= '/'.$repositoryDir;
2015-10-09 16:57:43 +02:00
// --- Prepare mail or Debug mode
2015-03-31 09:04:11 +02:00
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();
}
2015-03-31 09:04:11 +02:00
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine Flux');
2015-03-31 09:04:11 +02:00
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
$mail->setSubject($subject);
$mail->send();
2015-10-09 16:57:43 +02:00
// --- Stop
if ($opts->mail) {
2015-03-31 09:04:11 +02:00
exit;
}
}
// --- Execute
if ($optionsNoLog === false)
{
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db);
$fluxM = new Application_Model_Sdv1FluxFileOut();
// --- Vérifier si l'indicateur de lecture n'a pas déjà été ecrit
$readSql = $fluxM->select()->where("fileOut='".$filenameIn."'");
$readResult = $fluxM->fetchRow($readSql);
if ($readResult !== null) {
if ($readResult->depotDateRead == 0) {
// --- Ecrire l'indicateur de lecture
try {
$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";
}
}
}
2015-03-31 09:04:11 +02:00
}
}