173 lines
4.8 KiB
PHP
173 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* @todo :
|
|
* Faire en sorte que chaque extranet récupére la liste des bilans à traiter, depuis le webservice
|
|
* pour envoyer sur le ftp
|
|
*/
|
|
|
|
// 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(),
|
|
)));
|
|
|
|
//Use classmap autoloader - useful with opcode and realpath cache
|
|
require_once 'Zend/Loader/AutoloaderFactory.php';
|
|
require_once 'Zend/Loader/ClassMapAutoloader.php';
|
|
Zend_Loader_AutoloaderFactory::factory(array(
|
|
'Zend_Loader_ClassMapAutoloader' => array(
|
|
__DIR__ . '/../../library/Zend/autoload_classmap.php',
|
|
__DIR__ . '/../../library/Application/autoload_classmap.php',
|
|
__DIR__ . '/../../library/Scores/autoload_classmap.php',
|
|
__DIR__ . '/../../application/autoload_classmap.php',
|
|
),
|
|
'Zend_Loader_StandardAutoloader' => array(
|
|
'prefixes' => array(
|
|
'Zend' => __DIR__ . '/../../library/Zend',
|
|
'Application' => __DIR__ . '/../../library/Application',
|
|
'Scores' => __DIR__ . '/../../library/Scores',
|
|
'Metier' => __DIR__ . '/../../library/Metier',
|
|
),
|
|
'fallback_autoloader' => true
|
|
)
|
|
));
|
|
|
|
// Zend_Application - Use it if you don't have autoloaders
|
|
//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|?' => "Affiche l'aide.",
|
|
'send-s' => "Envoi tous les fichiers non traité par FTP (si nom du fichier fourni force l'envoi).",
|
|
'delete' => "Supprime les anciens fichiers et leurs références",
|
|
'save=s' => "Enregistre la présence d'un fichier"
|
|
));
|
|
$opts->parse();
|
|
} catch (Zend_Console_Getopt_Exception $e) {
|
|
echo $e->getUsageMessage();
|
|
exit;
|
|
}
|
|
|
|
//Usage
|
|
if(count($opts->getOptions())==0 || isset($opts->help))
|
|
{
|
|
echo "Bilan en commande saisi.";
|
|
echo "\n\n";
|
|
echo $opts->getUsageMessage();
|
|
echo "\n";
|
|
exit;
|
|
}
|
|
|
|
$test = false;
|
|
if ( isset($opts->list) ) {
|
|
$test = true;
|
|
}
|
|
|
|
$c = new Zend_Config($application->getOptions());
|
|
Zend_Registry::set('config', $c);
|
|
|
|
define ('PATH_DATA', $c->profil->path->data);
|
|
|
|
define('FTP_HOST', 'ftp.scores-decisions.com');
|
|
define('FTP_USER', 'bilansext');
|
|
define('FTP_PASS', 'j12azt78');
|
|
define('FTP_DIR', 'send');
|
|
|
|
// --- Functions
|
|
function sendToFtp($localFile, $remoteFile)
|
|
{
|
|
$conn_id = ftp_connect(FTP_HOST);
|
|
$login_result = ftp_login($conn_id, FTP_USER, FTP_PASS);
|
|
ftp_chdir($conn_id, FTP_DIR);
|
|
$return = false;
|
|
if (ftp_put($conn_id, $remoteFile, $localFile, FTP_BINARY)) {
|
|
$return = true;
|
|
}
|
|
ftp_close($conn_id);
|
|
return $return;
|
|
}
|
|
|
|
// --- Connexion à la base de données
|
|
$db = Zend_Db::factory($c->profil->db->sdv1);
|
|
Zend_Db_Table_Abstract::setDefaultAdapter($db);
|
|
|
|
$model = new Application_Model_BilanInput();
|
|
|
|
// --- Suppression des fichiers traités
|
|
if ( $opts->delete ) {
|
|
echo date('Y-m-d H:i:s')." - Suppression fichier.\n";
|
|
try {
|
|
$sql = $model->select()->where('sent=?', 1)->where('DATE_SUB(CURDATE(),INTERVAL 30 DAY) >= dateUpdate');
|
|
$result = $model->fetchAll($sql);
|
|
if (count($result) > 0) {
|
|
foreach ($result as $item) {
|
|
if( unlink(PATH_DATA . '/bilanclient/' . $item->file) ){
|
|
$model->delete("file='".$item->file."'");
|
|
}
|
|
}
|
|
}
|
|
} catch(Zend_Db_Exception $e) {
|
|
echo date('Y-m-d H:i:s')." - ".$e->getMessage().".\n";
|
|
}
|
|
echo date('Y-m-d H:i:s')." - Suppression fichier FIN.\n";
|
|
}
|
|
|
|
if ( $opts->save ) {
|
|
echo date('Y-m-d H:i:s')." - Enregistrement fichier.\n";
|
|
$model->insert(array('file' => $opts->save, 'sent' => 0));
|
|
echo date('Y-m-d H:i:s')." - Enregistrement fichier FIN.\n";
|
|
}
|
|
|
|
// --- Envoi
|
|
if ( $opts->send ) {
|
|
|
|
echo date('Y-m-d H:i:s')." - Envoi fichier.\n";
|
|
|
|
$tabFichier = array();
|
|
|
|
// --- Selection fichier
|
|
if ( $opts->send !== true ) {
|
|
$tabFichier[] = basename($opts->send);
|
|
} else {
|
|
$sql = $model->select()->where('sent=?', 0);
|
|
$result = $model->fetchAll($sql);
|
|
if (count($result) > 0) {
|
|
foreach($result as $item) {
|
|
$tabFichier[] = $item->file;
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Envoi sur le FTP
|
|
if (count($tabFichier) > 0) {
|
|
foreach ($tabFichier as $fichier) {
|
|
echo date('Y-m-d H:i:s')." - Envoi fichier ";
|
|
if ( sendToFtp(PATH_DATA.'/bilanclient/'.$fichier, $fichier) ) {
|
|
$model->setFlagSent($fichier);
|
|
echo $fichier." - OK";
|
|
} else {
|
|
echo $fichier." - Erreur";
|
|
}
|
|
echo "\n";
|
|
}
|
|
}
|
|
|
|
echo date('Y-m-d H:i:s')." - Envoi fichier FIN.\n";
|
|
}
|