flux/fileSend.php

131 lines
3.4 KiB
PHP
Raw Normal View History

2013-08-19 13:30:59 +02:00
<?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'));
// 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",
'mail' => "Only send a mail when a file is write",
'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) )
{
$c = new Zend_Config($application->getOptions());
//Get the main directory name in FTP and SFTP
$pathParts = pathinfo($opts->file);
$filename = $pathParts['basename'];
$client = basename(dirname($pathParts['dirname']));
$dateFile = date('YmdHis', filectime($opts->file));
$startpos = strlen("/home/data/");
if ( 'sftp' == substr($opts->file,$startpos,4) ) {
$type = 'SFTP';
} elseif ( 'ftp' == substr($opts->file,$startpos,3) ) {
$type = 'FTP';
}
$lines = file($filename);
$nbLines = count($lines);
//Prepare mail
if ($opts->onlyinfo || $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 : ".$opts->file."\n";
$txt.= "Nombre de Lignes : $nbLines\n";
$mail = new Zend_Mail();
$tr = new Zend_Mail_Transport_Sendmail();
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('production@scores-decisions.com', 'Production');
$mail->addTo('support@scores-decisions.com', 'Support');
$mail->setSubject($subject);
$mail->send();
//Stop
if ($opts->onlyinfo) {
exit;
}
}
//Execute
$db = Zend_Db::factory($c->profil->metier);
Zend_Db_Table::setDefaultAdapter($db);
try {
$fluxM = new Application_Model_Sdv1FluxFileIn();
$fluxM->insert(array(
'client' => $client,
'typeDepot' => $type,
'dateDepot' => $dateFile,
'fileDepot' => $filename,
'nbLines' => $nbLines,
'dateInsert' => date('YmdHis'),
'dateExecute' => '0000-00-00 00:00:00',
));
echo date('Y-m-dTH:i:s')." - Enregistrement client:$client fichier:$filename\n";
} catch (Zend_Db_Exception $e) {
echo date('Y-m-dTH:i:s')." - ERREUR Enregistrement client:$client fichier:$filename\n";
}
//Copie du fichier
$destDir = $c->profil->storage .
DIRECTORY_SEPARATOR . $client .
DIRECTORY_SEPARATOR . 'send';
if ( !is_dir($destDir) ) {
mkdir($destDir, null, true);
}
if ( copy($opts->file, $destDir. DIRECTORY_SEPARATOR . $filename) ) {
echo date('Y-m-dTH:i:s')." - Copie du fichier $filename dans $destDir\n";
} else {
echo date('Y-m-dTH:i:s')." - ERREUR Copie du fichier $filename dans $destDir\n";
}
}