flux/fileRecv.php

211 lines
6.1 KiB
PHP
Raw Normal View History

2013-08-19 13:30:59 +02:00
<?php
/**
* SD => Client
2013-08-28 12:23:01 +02:00
* Attention le script doit s'executer avec l'utilisateur root, afin de lire le fichier même
2013-08-19 13:30:59 +02:00
* s'il n'a pas les droits
*/
// Define path to application directory
defined('APPLICATION_PATH')
2015-07-02 16:57:29 +02:00
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
defined('APPLICATION_ENV')
2015-07-02 16:57:29 +02:00
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
2013-08-19 13:30:59 +02:00
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
2015-07-02 16:57:29 +02:00
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
2013-08-19 13:30:59 +02:00
)));
/** 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.",
2013-09-29 19:33:46 +02:00
'cron' => "Mandatory option in crontab",
2013-08-19 13:30:59 +02:00
'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();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if( isset($opts->help) || count($opts->getOptions())==0 )
{
echo "Place files in right directory for sending to the customer.\n";
echo $opts->getUsageMessage();
exit;
}
$c = new Zend_Config($application->getOptions());
2013-09-29 19:33:46 +02:00
$db = Zend_Db::factory($c->profil->db->metier);
2013-08-19 13:30:59 +02:00
Zend_Db_Table::setDefaultAdapter($db);
$fluxM = new Application_Model_Sdv1FluxFileOut();
2013-08-19 13:30:59 +02:00
$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)
2013-08-28 12:23:01 +02:00
->where('fileOut=?', $opts->file);
2013-08-19 13:30:59 +02:00
} else {
$sql->where('dateEnd!=?', '0000-00-00 00:00:00');
2013-08-28 12:23:01 +02:00
$sql->where('depotDate=?', '0000-00-00 00:00:00');
2013-08-19 13:30:59 +02:00
}
$result = $fluxM->fetchAll($sql);
if ( $result->count() > 0 ) {
foreach ( $result as $item ) {
$source = $c->profil->path->storage .
'/' . $item->client .
'/' . 'recv' .
'/' . $item->fileOut;
2015-07-02 16:57:29 +02:00
$fluxRepository = 'recv';
if ($item->depotDir != '') {
2015-07-02 16:57:29 +02:00
$fluxRepository = $item->depotDir;
}
2013-08-28 12:23:01 +02:00
2013-10-03 09:25:29 +02:00
switch ($item->depotType) {
2013-08-19 13:30:59 +02:00
case 'FTP':
2013-08-28 12:23:01 +02:00
$dest = $c->profil->path->ftp .
'/' . $item->client .
2015-07-02 16:57:29 +02:00
'/' . $fluxRepository .
'/' . $item->fileOut;
2013-08-19 13:30:59 +02:00
break;
case 'SFTP':
2013-08-28 12:23:01 +02:00
$dest = $c->profil->path->sftp .
'/' . $item->client .
2015-07-02 16:57:29 +02:00
'/' . $fluxRepository .
'/' . $item->fileOut;
2013-08-28 12:23:01 +02:00
break;
2013-08-19 13:30:59 +02:00
}
2015-03-30 15:54:20 +02:00
// --- Options
$optionsCopyAddDate = false;
$optionsCopyDeleteAfter = false;
$optionsRunWithEndFile = false;
2015-06-10 08:14:37 +02:00
$optionsLog = false;
2015-03-31 08:44:13 +02:00
// Match prestation
$prestations = include __DIR__ . '/fileConfig.php';
$prestation = null;
if (array_key_exists($item->client, $prestations))
{
$clientPrestations = $prestations[$item->client]['prestations'];
foreach ($clientPrestations as $i => $p)
{
// Not default repository dir
2015-07-02 16:57:29 +02:00
$repositoryDir = 'recv';
2015-03-31 08:44:13 +02:00
if (array_key_exists('directory', $p) && !empty($p['directory']))
{
$repositoryDir = $p['directory'];
}
2015-06-10 08:14:37 +02:00
2015-03-31 08:44:13 +02:00
if ($item->depotType == $p['type'] && $fluxRepository == $repositoryDir)
{
$prestation = $p['name'];
2015-06-10 08:14:37 +02:00
2015-03-31 08:44:13 +02:00
// Set option
if (array_key_exists('out', $p) && count($p['out']) > 0)
{
foreach ($p['out'] as $option => $value)
{
${'options'.$option} = $value;
}
}
2015-06-10 08:14:37 +02:00
2015-03-31 08:44:13 +02:00
break;
}
}
}
2015-06-10 08:14:37 +02:00
2015-03-30 15:54:20 +02:00
// --- Copy du fichier
2013-08-19 13:30:59 +02:00
if ( copy($source, $dest) ) {
2015-06-10 08:14:37 +02:00
2015-03-30 15:54:20 +02:00
// --- Detail du fichier
$nbLines = 0;
if ( strtolower(substr(basename($source), -3)) == 'csv' ) {
$lines = file($source);
$nbLines = count($lines);
}
$size = filesize($source);
2013-08-28 21:58:09 +02:00
2015-03-30 15:54:20 +02:00
// --- Permission du depot
if ($item->depotType == 'FTP') {
2014-10-02 11:45:58 +02:00
chown($dest, 'ftpuser');
chgrp($dest, 'ftpgroup');
}
2015-03-30 15:54:20 +02:00
elseif($item->depotType == 'SFTP') {
2014-10-02 11:45:58 +02:00
chown($dest, $item->client);
}
2015-06-10 08:14:37 +02:00
2015-03-30 15:54:20 +02:00
// --- Mise à jour information de remise
2013-08-19 13:30:59 +02:00
$fluxM->update(array(
2015-03-30 15:54:20 +02:00
'depotFileSize' => $size,
2013-08-28 12:23:01 +02:00
'depotDate' => date('YmdHis'),
), 'id='.$item->id);
2013-08-28 21:58:09 +02:00
2015-03-30 15:54:20 +02:00
// --- Envoi email
2015-06-10 08:14:37 +02:00
if ($optionsLog === true) {
2015-03-30 15:54:20 +02:00
$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";
2015-06-10 08:14:37 +02:00
2015-03-30 15:54:20 +02:00
$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();
}
2015-03-30 15:54:20 +02:00
$mail->setDefaultTransport($tr);
$mail->setBodyText($txt);
$mail->setFrom('supportdev@scores-decisions.com', 'Machine Flux');
2015-03-30 15:54:20 +02:00
$mail->addTo('suivi@scores-decisions.com', 'Suivi');
$mail->setSubject($subject);
$mail->send();
}
2013-08-28 21:58:09 +02:00
2013-08-19 13:30:59 +02:00
}
2013-08-28 12:23:01 +02:00
}
2013-08-19 13:30:59 +02:00
}