179 lines
5.2 KiB
PHP
179 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* Indicateur de transfert par un autre système
|
|
* 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');
|
|
|
|
// --- Options
|
|
$displayUsage = false;
|
|
try {
|
|
$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",
|
|
));
|
|
$opts->parse();
|
|
$optsNb = $opts->getOptions();
|
|
} catch (Zend_Console_Getopt_Exception $e) {
|
|
$displayUsage = true;
|
|
}
|
|
|
|
// --- Aide / Options
|
|
if ($optsNb == 0 || isset($opts->help)) {
|
|
$displayUsage = true;
|
|
}
|
|
|
|
// --- Usage
|
|
if ($displayUsage) {
|
|
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__ . '/config.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";
|
|
unlink($fluxBasePath.'/'.$filenameIn);
|
|
}
|
|
catch (Zend_Db_Exception $e)
|
|
{
|
|
echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filenameSearch\n";
|
|
}
|
|
}
|
|
} |