flux/fileRead.php
2015-03-31 07:04:11 +00:00

166 lines
4.5 KiB
PHP

<?php
/**
* Client => SD
* Attention le script doit s'executer avec l'utilisateur root
*/
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// 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 "Execute basic action when a customer send a file.\n";
echo $opts->getUsageMessage();
exit;
}
//Get the file
if ( isset($opts->file) )
{
$optionsNoLog = false;
$repositoryDir = 'recv';
$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'];
}
$client = basename(dirname($pathParts['dirname']));
// 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;
}
}
}
if ($prestation === null)
{
echo date('Y-m-d H:i:s')." - Prestation not found !\n";
}
$fluxBasePath .= '/'.$repositoryDir;
//Prepare mail
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');
$tr = new Zend_Mail_Transport_Smtp('smtp.celeste.fr');
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine');
$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('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";
}
}
}