flux/fileRecv.php

134 lines
3.4 KiB
PHP
Raw Permalink 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')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
// Define application environment
defined('APPLICATION_ENV')
|| 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(
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;
$depotDir = 'recv';
if ($item->depotDir != '') {
$depotDir = $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 .
'/' . $depotDir .
'/' . $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 .
'/' . $depotDir .
'/' . $item->fileOut;
2013-08-28 12:23:01 +02:00
break;
2013-08-19 13:30:59 +02:00
}
if ( copy($source, $dest) ) {
2013-08-28 21:58:09 +02:00
$lines = file($source);
$nbLines = count($lines);
2013-10-03 09:25:29 +02:00
chown($dest, $item->client);
2013-08-19 13:30:59 +02:00
$fluxM->update(array(
2013-08-28 12:23:01 +02:00
'depotDate' => date('YmdHis'),
), 'id='.$item->id);
2013-08-28 21:58:09 +02:00
$subject = "[Flux] - Envoi d'un fichier " . $item->client;
$txt = "Envoi d'un fichier après traitement\n";
$txt.= "Client : ".$item->client."\n";
2013-10-03 09:25:29 +02:00
$txt.= "Mode de transmission : ".$item->depotType."\n";
2013-08-28 21:58:09 +02:00
$txt.= "Fichier : ".$item->fileOut."\n";
$txt.= "Nombre de Lignes : $nbLines\n";
2013-08-28 21:58:09 +02:00
$mail = new Zend_Mail('UTF-8');
2013-08-28 21:58:09 +02:00
$tr = new Zend_Mail_Transport_Sendmail();
$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();
2013-08-19 13:30:59 +02:00
}
2013-08-28 12:23:01 +02:00
}
2013-08-19 13:30:59 +02:00
}