206 lines
6.1 KiB
PHP
206 lines
6.1 KiB
PHP
<?php
|
|
/**
|
|
* SD => Client
|
|
* Attention le script doit s'executer avec l'utilisateur root, afin de lire le fichier même
|
|
* s'il n'a pas les droits
|
|
*/
|
|
|
|
// --- 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.",
|
|
'cron' => "Mandatory option in crontab",
|
|
'file=s' => "Manually define the file to process",
|
|
'client=s' => "Define the client name for getting the file manually",
|
|
'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 "Place files in right directory for sending to the customer.\n";
|
|
echo $opts->getUsageMessage();
|
|
exit;
|
|
}
|
|
|
|
$c = new Zend_Config($application->getOptions());
|
|
$db = Zend_Db::factory($c->profil->db->metier);
|
|
Zend_Db_Table::setDefaultAdapter($db);
|
|
|
|
$fluxM = new Application_Model_Sdv1FluxFileOut();
|
|
$sql = $fluxM->select()->where('dateEnd!=?','0000-00-00 00:00:00');
|
|
|
|
//Get specific file
|
|
if ( isset($opts->file) && isset($opts->client) ) {
|
|
$sql->where('client=?', $opts->client)
|
|
->where('fileOut=?', $opts->file);
|
|
} else {
|
|
$sql->where('dateEnd!=?', '0000-00-00 00:00:00');
|
|
$sql->where('depotDate=?', '0000-00-00 00:00:00');
|
|
}
|
|
|
|
$result = $fluxM->fetchAll($sql);
|
|
|
|
if ( $result->count() > 0 ) {
|
|
foreach ( $result as $item ) {
|
|
|
|
$source = $c->profil->path->storage .
|
|
'/' . $item->client .
|
|
'/' . 'recv' .
|
|
'/' . $item->fileOut;
|
|
|
|
$fluxRepository = 'recv';
|
|
if ($item->depotDir != '') {
|
|
$fluxRepository = $item->depotDir;
|
|
}
|
|
|
|
switch ($item->depotType) {
|
|
case 'FTP':
|
|
$dest = $c->profil->path->ftp .
|
|
'/' . $item->client .
|
|
'/' . $fluxRepository .
|
|
'/' . $item->fileOut;
|
|
break;
|
|
case 'SFTP':
|
|
$dest = $c->profil->path->sftp .
|
|
'/' . $item->client .
|
|
'/' . $fluxRepository .
|
|
'/' . $item->fileOut;
|
|
|
|
break;
|
|
}
|
|
|
|
// --- Options
|
|
$optionsCopyAddDate = false;
|
|
$optionsCopyDeleteAfter = false;
|
|
$optionsRunWithEndFile = false;
|
|
$optionsLog = false;
|
|
|
|
// Match prestation
|
|
$prestations = include __DIR__ . '/config.php';
|
|
$prestation = null;
|
|
if (array_key_exists($item->client, $prestations))
|
|
{
|
|
$clientPrestations = $prestations[$item->client]['prestations'];
|
|
foreach ($clientPrestations as $i => $p)
|
|
{
|
|
// Not default repository dir
|
|
$repositoryDir = 'recv';
|
|
if (array_key_exists('directory', $p) && !empty($p['directory']))
|
|
{
|
|
$repositoryDir = $p['directory'];
|
|
}
|
|
|
|
if ($item->depotType == $p['type'] && $fluxRepository == $repositoryDir)
|
|
{
|
|
$prestation = $p['name'];
|
|
|
|
// Set option
|
|
if (array_key_exists('out', $p) && count($p['out']) > 0)
|
|
{
|
|
foreach ($p['out'] as $option => $value)
|
|
{
|
|
${'options'.$option} = $value;
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Copy du fichier
|
|
exec("cp $source $dest", $output, $result);
|
|
if ($result == 0) {
|
|
|
|
// --- Detail du fichier
|
|
$nbLines = 0;
|
|
if ( strtolower(substr(basename($source), -3)) == 'csv' ) {
|
|
$lines = file($source);
|
|
$nbLines = count($lines);
|
|
}
|
|
$size = filesize($source);
|
|
|
|
// --- Permission du depot
|
|
if ($item->depotType == 'FTP') {
|
|
chown($dest, 'ftpuser');
|
|
chgrp($dest, 'ftpgroup');
|
|
}
|
|
elseif ($item->depotType == 'SFTP') {
|
|
chown($dest, $item->client);
|
|
}
|
|
|
|
// --- Mise à jour information de remise
|
|
$fluxM->update(array(
|
|
'depotFileSize' => $size,
|
|
'depotDate' => date('YmdHis'),
|
|
), 'id='.$item->id);
|
|
|
|
// --- Envoi email
|
|
if ($optionsLog === true) {
|
|
$subject = "[Flux] - Envoi d'un fichier " . $item->client;
|
|
$txt = "Envoi d'un fichier après traitement\n";
|
|
$txt.= "Client : ".$item->client."\n";
|
|
$txt.= "Mode de transmission : ".$item->depotType."\n";
|
|
$txt.= "Fichier : ".$item->fileOut."\n";
|
|
$txt.= "Nombre de Lignes : $nbLines\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();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|