flux/fileTck.php

176 lines
5.1 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Indicateur de transfert par un autre système
* Attention le script doit s'executer avec l'utilisateur root
*/
2015-10-12 10:10:22 +02:00
// --- Define path to application directory
defined('APPLICATION_PATH')
2015-10-12 10:10:22 +02:00
|| define('APPLICATION_PATH', realpath(__DIR__ . '/application'));
2015-10-12 10:10:22 +02:00
// --- Define application environment
defined('APPLICATION_ENV')
2015-10-12 10:10:22 +02:00
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
2015-10-12 10:10:22 +02:00
// --- Composer autoload
require_once realpath(__DIR__ . '/vendor/autoload.php');
2015-10-12 10:10:22 +02:00
// --- 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",
'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 "Mark as transfert.\n";
echo $opts->getUsageMessage();
exit;
}
// --- Get the file
if ( isset($opts->file) )
{
$optionsNoLog = false;
$repositoryDir = 'send';
$c = new Zend_Config($application->getOptions());
// --- 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'];
}
// --- Only file with .tck
if ($extension != 'tck') {
exit;
}
$client = basename(dirname($pathParts['dirname']));
$filenameSearch = str_replace('.tck', '', $filenameIn);
// --- Base path, type and repository
$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);
// --- 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'];
}
if ($type == $p['type'] && $fluxRepository == $repositoryDir)
{
$prestation = $p['name'];
// --- Set option
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)
{
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db);
try {
$fluxM = new Application_Model_Sdv1FluxFileOut();
$fluxM->update(array('transfertDate' => date('YmdHis')), "fileOut='".$filenameSearch."'");
echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filenameSearch\n";
2015-10-13 11:44:36 +02:00
unlink($fluxBasePath.'/'.$filenameIn);
}
catch (Zend_Db_Exception $e)
{
echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filenameSearch\n";
}
}
}