flux/fileSend.php
2013-10-24 14:11:59 +00:00

176 lines
4.4 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",
'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'];
$extension = '';
if (array_key_exists('extension', $pathParts) ){
$extension = $pathParts['extension'];
}
$client = basename(dirname($pathParts['dirname']));
$dateFile = date('YmdHis', filectime($opts->file));
$startpos = strlen( $c->profil->path->data . DIRECTORY_SEPARATOR );
if ( 'sftp' == substr($opts->file,$startpos,4) ) {
$type = 'SFTP';
} elseif ( 'ftp' == substr($opts->file,$startpos,3) ) {
$type = 'FTP';
}
$lines = file($opts->file);
$nbLines = count($lines);
$copyOptionsAddDate = false;
$copyOptionsDeleteAfter = false;
/**
* @todo : links to prestation table and file rules
*/
switch ($client) {
case 'sfrbtr' :
$prestation = 'FICH_RCE';
$filemask = array(
'FICH_RCE_SCORE_.*.csv',
);
break;
case 'gefacto':
$prestation = 'GEFACTO';
$copyOptionsAddDate = true;
break;
}
if ($copyOptionsAddDate) {
$extensionLength = 0;
if ($extension!='') {
$extensionLength = strlen($extension)+1;
$filename = substr($filename, 0, strlen($filename)-$extensionLength);
$filename = $filename . '_' . date('YmdHis') . '.' . $extension;
} else {
$filename = $filename . '_' . date('YmdHis');
}
}
//Prepare mail
if ($opts->mail || $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('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
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table::setDefaultAdapter($db);
try {
$fluxM = new Application_Model_Sdv1FluxFileIn();
$fluxM->insert(array(
'client' => $client,
'name' => $prestation,
'depotType' => $type,
'depotDate' => $dateFile,
'depotFile' => $filename,
'nbLines' => $nbLines,
'dateInsert' => date('YmdHis'),
'dateExecute' => '0000-00-00 00:00:00',
));
echo date('Y-m-d H:i:s')." - Enregistrement client:$client fichier:$filename\n";
} catch (Zend_Db_Exception $e) {
echo date('Y-m-d H:i:s')." - ERREUR Enregistrement client:$client fichier:$filename\n";
}
//Copie du fichier
$destDir = $c->profil->path->storage .
DIRECTORY_SEPARATOR . $client .
DIRECTORY_SEPARATOR . 'send';
if ( !is_dir($destDir) ) {
mkdir($destDir, 0755, true);
}
if ( copy($opts->file, $destDir. DIRECTORY_SEPARATOR . $filename) ) {
echo date('Y-m-d H:i:s')." - Copie du fichier $filename dans $destDir\n";
if ($copyOptionsDeleteAfter) {
unlink($opts->file);
}
} else {
echo date('Y-m-d H:i:s')." - ERREUR Copie du fichier $filename dans $destDir\n";
}
}