Commit 86e039aa authored by Sébastien RENOU's avatar Sébastien RENOU

init

parents
/composer.phar
/utils/conf.php
/nbproject/
/Niveaux/Commandes/ImportDrealHauteurs.class.php
/Niveaux/Commandes/ImportDrealPluies.class.php
/Niveaux/Commandes/ImportRibou.class.php
/Niveaux/Commandes/ImportVerdon.class.php
/Niveaux/Commandes/ImportVigicrue.class.php
\ No newline at end of file
This diff is collapsed.
<?php
require_once __DIR__ . '/../Niveaux/CommonConf.php';
require_once __DIR__ . '/../Niveaux/AppConf.php';
require_once __DIR__ . '/../Niveaux/Controllers/Controllers.php';
<?php
/*
* partie de la conf spécifique à l'application web
*/
// TWIG
$app->register(new Silex\Provider\HttpFragmentServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/../views'
));
// TRANSLATION
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale_fallbacks' => array('fr'),
));
// VALIDATOR
use Symfony\Component\Validator\Constraints as Assert;
$app->register(new Silex\Provider\ValidatorServiceProvider());
// FORM
use Silex\Provider\FormServiceProvider;
$app->register(new FormServiceProvider());
// SESSION & COOKIES
$app->register(new Silex\Provider\SessionServiceProvider());
// session par defaut sur xx heures
$app['session.storage.options'] = [
'cookie_lifetime' => (3600 * 24 * 7)
];
use Symfony\Component\HttpFoundation\Cookie;
// URL MANAGER
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
// REQUEST & RESPONSE & FLASH
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
// SECURITY
$app['security.firewalls'] = array(
// 'security' => $app['debug'] ? false : true,
'secured' => array(
'pattern' => '^/',
'anonymous' => true,
'form' => array('login_path' => '/login', 'check_path' => '/control', 'default_target_path' => '/control', 'always_use_default_target_path' => true, 'csrf_token_generator' => 'security.csrf.token_manager'),
'logout' => array('logout_path' => '/logout', 'invalidate_session' => true, 'target' => '/'),
'remember_me' => array(
'key' => REMEMBERKEY,
'always_remember_me' => true,
),
'users' => function () use ($app) {
return new UserProviderNiveaux($app['pdo']);
},
),
);
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => $app['security.firewalls']
));
$app['security.access_rules'] = array(
array('^/admin', 'ROLE_ADMIN')
);
$app->register(new Silex\Provider\RememberMeServiceProvider(), array('lifetime' => 3600 * 24 * 7));
$app['security.default_encoder'] = function ($app) {
return $app['security.encoder.bcrypt'];
};
// SMS OVH
use \Ovh\Sms\SmsApi;
<?php
/**
* Description of Notification
*
* @author srenou
*/
class Notification {
public function __construct($pdo, $id = null) {
if ($id !== null) {
$qry = $pdo->prepare('select * from ' . SCHEMA . '.notification_infos where id_notification = :id');
$qry->bindParam(':id', $id, PDO::PARAM_INT);
$qry->execute();
$res = $qry->fetch(PDO::FETCH_ASSOC);
foreach ($res as $key => $value) {
$this->$key = $value;
}
return true;
}
return false;
}
public function insert($pdo, $request) {
$pp = prepInsertQuery($request);
$qry = $pdo->prepare("insert into " . SCHEMA . ".notification (" . $pp['reqFields'] . ") VALUES (" . $pp['reqParams'] . ")");
foreach ($pp['tabD'] as $key => $val) {
$qry->bindValue($key, $val);
}
return returnResultQuery($qry, $pdo, $pp['tabD']);
}
public function update($pdo, $request) {
$pp = prepUpdateQuery($request, 'id_notification');
$qry = $pdo->prepare("update " . SCHEMA . ".notification set " . $pp['req'] . " where id_notification = :id_notification");
foreach ($pp['tabD'] as $key => $val) {
$qry->bindValue($key, $val);
echo $key . $val . '<br>';
}
return returnResultQuery($qry, $pdo, $pp['tabD']);
}
public function delete($pdo) {
$qry = $pdo->prepare('delete from ' . SCHEMA . '.notification where id_notification = :id_notification');
$qry->bindParam(':id_notification', $this->id_notification, PDO::PARAM_INT);
return $qry->execute();
}
/*
* function envoie
*
* @param integer id_releve
* @param string valeur
* @param DateTime date_valeur,
* @param array Dern
* @param pdo
*/
public function envoie($id_releve, $valeur, $date_valeur, $Dern, $app) {
$pdo = $app['pdo'];
$R = new Releve($app, $id_releve);
$dtdern = new DateTime($Dern['date_valeur'], new DateTimeZone($app['TIMEZONE']));
if ($this->type_notification === 'mail') {
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Notification ' . $app['BRAND'] . '</title>
<style>
body {
font-family:arial,sans-serif;
}
</style>
</head>
<body>';
$message .= '<h2>Notification ' . $app['BRAND'] . ' ' . $app['BRANDSTRUCTURE'] . '</h2>';
$message .= '<h1>Point de suivi ' . $this->point_suivi . ' / ' . $this->element_mesure . ' : ' . $valeur . ' ' . $this->unite . ' à ' . $date_valeur->format('H:i \l\e d/m/Y') . ' (' . $this->montant_descendant . ')</h1>';
$message .= '<p>Cette notification a été déclenchée automatiquement suite au dernier relevé effetué sur la station de ' . $this->point_suivi . '</p>';
$message .= '<p>Informations complémentaires : </p>';
$message .= '<ul>';
$message .= '<li>Nouvelle valeur de ' . $valeur . ' ' . $this->unite . ' issue du relevé ' . $R->type_releve . ' du ' . $R->datetime_releve->format('d/m/Y \à H:i') . ' effectué par ' . $R->prenom . ' ' . $R->nom . '</li>';
$message .= '<li>Valeur précédente : ' . $Dern['valeur'] . ' ' . $this->unite . ' le ' . $dtdern->format('d/m/Y \à H:i') . '</li>';
$message .= '</ul>';
$message .= '<p>Paramétrage de votre notification : </p>';
$message .= '<ul>';
$message .= '<li>' . $this->element_mesure . ' de déclenchement : ' . $this->seuil . ' ' . $this->unite . ' (' . $this->montant_descendant . ')</li>';
$message .= '</ul>';
$message .= '</body></html>';
$object = '' . $this->point_suivi . ' / ' . $this->type_point . ' à ' . $date_valeur->format('H:i \l\e d/m/Y') . ' : ' . $valeur;
$senderMail = $app['MAILEXP'];
$sender = $app['BRAND'] . ' ' . $app['BRANDSTRUCTURE'];
if (envoieMail($this->mail, $senderMail, $sender, $message, $object)) {
$this->traceEnvoi($id_releve, 'succes', array('dest' => $this->mail, 'sender-mail' => $senderMail, 'sender' => $sender, 'message' => $message, 'object' => $object), $valeur, $date_valeur->format('Y-m-d H:i:s'), $pdo);
return true;
} else {
$this->traceEnvoi($id_releve, 'echec', array($this->mail, $senderMail, $sender, $message, $object), $valeur, $date_valeur->format('Y-m-d H:i:s'), $pdo);
return false;
}
}
if ($this->type_notification === 'sms') {
if ($this->telephone != null) {
$message = $this->point_suivi . ' : ' . $this->element_mesure . ' de ' . $valeur . ' ' . $this->unite . ' à ' . $date_valeur->format('H:i \l\e d/m/Y') . ' (' . $this->montant_descendant . '). Nouvelle valeur issue du relevé ' . $R->type_releve . ' du ' . $R->datetime_releve->format('d/m/Y \à H:i') . ' effectué par ' . $R->prenom . ' ' . $R->nom . '.';
if (envoiSMS($this->username, $this->telephone, $message, $app)) {
$this->traceEnvoi($id_releve, 'succes', array('dest' => $this->telephone, 'message' => $message), $valeur, $date_valeur->format('Y-m-d H:i:s'), $pdo);
return true;
} else {
$this->traceEnvoi($id_releve, 'echec', array('dest' => $this->telephone, 'message' => $message), $valeur, $date_valeur->format('Y-m-d H:i:s'), $pdo);
return false;
}
}
}
}
private function traceEnvoi($id_releve, $statut, $contenu, $valeur, $date_valeur, $pdo) {
$qry = $pdo->prepare('insert into ' . SCHEMA . '.notification_envoi (id_notification,id_releve,statut,contenu_notification,releve_valeur,releve_valeur_date) VALUES (:id_notification,:id_releve,:statut,:contenu,:releve_valeur,to_timestamp(:releve_valeur_date, \'yyyy-mm-dd hh24:mi:ss\'))');
$qry->bindParam(':id_notification', $this->id_notification, PDO::PARAM_INT);
$qry->bindParam(':id_releve', $id_releve, PDO::PARAM_INT);
$qry->bindParam(':statut', $statut, PDO::PARAM_STR);
$cont = json_encode($contenu);
$qry->bindParam(':contenu', $cont, PDO::PARAM_STR);
$qry->bindParam(':releve_valeur', $valeur, PDO::PARAM_STR);
$qry->bindParam(':releve_valeur_date', $date_valeur, PDO::PARAM_STR);
return $qry->execute();
}
public function chargeEnvois($pdo, $limit = 5) {
$qry = $pdo->prepare('select * from ' . SCHEMA . '.notification_envoi_infos where id_notification = ' . $this->id_notification . ' order by date_envoi_notification desc LIMIT ' . $limit);
$qry->execute();
return $qry->fetchAll(PDO::FETCH_ASSOC);
}
public function desactive($pdo) {
$qry = $pdo->prepare('update ' . SCHEMA . '.notification set active = 0 where id_notification = :id');
$qry->bindParam(':id', $this->id_notification, PDO::PARAM_INT);
return $qry->execute();
}
}
<?php
/**
* Description of PointSuivi
*
* @author srenou
*/
class PointSuivi {
public function __construct($pdo, $id = null, $alias = null) {
if ($id !== null or $alias !== null) {
$condId = $condAlias = null;
if ($id !== null)
$condId = ' and id_point_suivi = :id';
if ($alias !== null)
$condAlias = ' and alias = :alias';
$qry = $pdo->prepare('select * from ' . SCHEMA . '.point_suivi_infos where 1=1 ' . $condId . $condAlias);
if ($alias !== null)
$qry->bindParam(':alias', $alias, PDO::PARAM_STR);
if ($id !== null)
$qry->bindParam(':id', $id, PDO::PARAM_INT);
$qry->execute();
if ($qry->rowCount() === 1) {
$res = $qry->fetch(PDO::FETCH_ASSOC);
foreach ($res as $key => $value) {
$this->$key = $value;
}
$this->tabSeuils = array();
}
}
}
public function insert($pdo, $request) {
$pp = prepInsertQuery($request);
$qry = $pdo->prepare("insert into " . SCHEMA . ".point_suivi (" . $pp['reqFields'] . ") VALUES (" . $pp['reqParams'] . ")");
foreach ($pp['tabD'] as $key => $val) {
$qry->bindValue($key, $val);
}
return returnResultQuery($qry, $pdo, $pp['tabD']);
}
public function update($pdo, $request) {
$pp = prepUpdateQuery($request, 'id_point_suivi');
$qry = $pdo->prepare("update " . SCHEMA . ".point_suivi set " . $pp['req'] . " where id_point_suivi = :id_point_suivi");
foreach ($pp['tabD'] as $key => $val) {
$qry->bindValue($key, $val);
echo $key . $val . '<br>';
}
return returnResultQuery($qry, $pdo, $pp['tabD']);
}
/**
* @param string type_releve : manuel ou auto
* @param string type_suivi : valeur ou seuil
*/
public function chargeValeurs($pdo, $date_debut, $date_fin = null, $type_releve = null, $type_suivi = null, $heures_slmt = 0) {
$condDateFin = $condTypeReleve = $condTypeSuivi = $condHeureSlmt = null;
$condDateDebut = ' and date_valeur >= :date_debut ';
if ($date_fin !== null)
$condDateFin = ' and date_valeur <= :date_fin ';
if ($type_releve !== null)
$condTypeReleve = ' and type_releve = :type_releve ';
if ($type_suivi !== null)
$condTypeSuivi = ' and type_suivi = :type_suivi ';
if ($heures_slmt == '1')
$condHeureSlmt = ' and extract(\'minute\' from date_valeur) = 0 ';
$i = 'select * from ' . SCHEMA . '.valeur_infos where id_point_suivi = :id ' . $condDateDebut . $condDateFin . $condTypeReleve . $condTypeSuivi . $condHeureSlmt . ' order by date_valeur asc';
$qry = $pdo->prepare($i);
$qry->bindParam(':id', $this->id_point_suivi, PDO::PARAM_STR);
$dt = date_format($date_debut, 'Y-m-d H:i:s');
$qry->bindParam(':date_debut', $dt, PDO::PARAM_STR);
if ($date_fin !== null)
$qry->bindParam(':date_fin', $date_fin, PDO::PARAM_STR);
if ($type_releve !== null)
$qry->bindParam(':type_releve', $type_releve, PDO::PARAM_STR);
if ($type_suivi !== null)
$qry->bindParam(':type_suivi', $type_suivi, PDO::PARAM_STR);
$qry->execute();
return $qry->fetchAll(PDO::FETCH_ASSOC);
}
/*
* recherche des seuils
* tous ou en dessous du max + 1m
*
*/
public function chargeSeuils($pdo, $max = null) {
$condM = null;
if ($max != null) {
$condM = ' and valeur <= :max';
}
$qry = $pdo->prepare('select * from ' . SCHEMA . '.seuil where id_point_suivi = :id ' . $condM . ' order by valeur');
$qry->bindParam(':id', $this->id_point_suivi, PDO::PARAM_INT);
if ($max != null) {
$max = $max + 1;
$qry->bindParam(':max', $max, PDO::PARAM_INT);
}
$qry->execute();
$this->tabSeuils = $qry->fetchAll(PDO::FETCH_ASSOC);
}
}
<?php
/**
* Description of Releve
*
* @author srenou
*/
class Releve {
public function __construct($app, $id = null) {
$pdo = $app['pdo'];
if ($id !== null) {
$qry = $pdo->prepare('select * from ' . SCHEMA . '.releve_infos where id_releve = :id');
$qry->bindParam(':id', $id, PDO::PARAM_INT);
$qry->execute();
$res = $qry->fetch(PDO::FETCH_ASSOC);
foreach ($res as $key => $value) {
$this->$key = $value;
}
$this->datetime_releve = new DateTime($this->date_releve, new DateTimeZone($app['TIMEZONE']));
}
}
/**
* insert
*
* @param array $infosReleve sur le modèle ['id_user'],['date_releve'],['type_releve'],['type_suivi']
* @param array valeursReleve sur le modèle ('0'=> array('id_point_suivi'=>xxx, 'valeurs'=>array('date_valeur'=> Y-m-d H:i, 'valeur' => valeur )))
*/
public function insert($infosReleve, $valeursReleve, $pdo) {
$erreurs = 0;
$qry = $pdo->prepare('insert into ' . SCHEMA . '.releve (id_user,type_releve,type_suivi,id_tache) VALUES (:id_user,:type_releve,:type_suivi,:id_tache) RETURNING id_releve as id_releve');
$qry->bindParam(':id_user', $infosReleve['id_user'], PDO::PARAM_INT);
$qry->bindParam(':type_releve', $infosReleve['type_releve'], PDO::PARAM_STR);
$qry->bindParam(':type_suivi', $infosReleve['type_suivi'], PDO::PARAM_STR);
$qry->bindParam(':id_tache', $infosReleve['id_tache'], PDO::PARAM_INT);
if ($qry->execute()) {
$r = $qry->fetchObject();
// boucle sur les stations du tableau de valeur
foreach ($valeursReleve as $v) {
$compt = 0;
foreach ($v['valeurs'] as $valeur) {
// VALEUR N'EST PAS UN DOUBLON, N EST PAS VIDE et EST NUMERIQUE
if (!estUnDoublon($valeur['valeur'], $v['id_point_suivi'], $valeur['date_valeur']->format('Y-m-d H:i:s'), $infosReleve['type_releve'], $infosReleve['type_suivi'], $pdo) and !estVide($valeur['valeur']) and is_numeric($valeur['valeur'])) {
// A DEJA UNE INFO MAIS DE VALEUR DIFFERENTE
if (aUneValeurDifferente($valeur['valeur'], $v['id_point_suivi'], $valeur['date_valeur']->format('Y-m-d H:i:s'), $infosReleve['type_releve'], $infosReleve['type_suivi'], $pdo)) {
$V = valeurActuelleSiDifferente($valeur['valeur'], $v['id_point_suivi'], $valeur['date_valeur']->format('Y-m-d H:i:s'), $infosReleve['type_releve'], $infosReleve['type_suivi'], $pdo);
if ($compt === 0) {
$compt++;
}
// UPDATE
$qry2 = $pdo->prepare('update ' . SCHEMA . '.releve_valeur SET id_releve = :id_releve, valeur = :valeur where date_valeur = to_timestamp(:date_valeur, \'yyyy-mm-dd hh24:mi:ss\') and id_point_suivi = :id_point_suivi');
$qry2->bindParam(':id_releve', $r->id_releve, PDO::PARAM_INT);
$qry2->bindParam(':valeur', $valeur['valeur'], PDO::PARAM_STR);
$date_v = $valeur['date_valeur']->format('Y-m-d H:i:s');
$qry2->bindParam(':date_valeur', $date_v, PDO::PARAM_STR);
$qry2->bindParam(':id_point_suivi', $v['id_point_suivi'], PDO::PARAM_INT);
}
// NOUVELLE VALEUR
else {
// INSERT
$qry2 = $pdo->prepare('insert into ' . SCHEMA . '.releve_valeur (id_releve,valeur,date_valeur,id_point_suivi) VALUES (:id_releve,:valeur,to_timestamp(:date_valeur, \'yyyy-mm-dd hh24:mi:ss\'),:id_point_suivi)');
$qry2->bindParam(':id_releve', $r->id_releve, PDO::PARAM_INT);
$qry2->bindParam(':valeur', $valeur['valeur'], PDO::PARAM_STR);
$date_v = $valeur['date_valeur']->format('Y-m-d H:i:s');
$qry2->bindParam(':date_valeur', $date_v, PDO::PARAM_STR);
$qry2->bindParam(':id_point_suivi', $v['id_point_suivi'], PDO::PARAM_INT);
}
if (!$qry2->execute()) {
$erreurs++;
} else {
// verifieNotifications($r->id_releve, $v['id_point_suivi'], $valeur['valeur'], $valeur['date_valeur'], $pdo);
}
} else {
// echo 'valeur KO' ;
}
}
}
} else {
die('erreur');
}
if ($erreurs === 0) {
return true;
} else {
return false;
}
}
}
<?php
/**
* Description of Seuil
*
* @author srenou
*/
class Seuil {
public function __construct($pdo, $id = null) {
if ($id !== null) {
$qry = $pdo->prepare('select * from ' . SCHEMA . '.seuil where id_seuil = :id');
$qry->bindParam(':id', $id, PDO::PARAM_INT);
$qry->execute();
if ($qry->rowCount() === 1) {
$res = $qry->fetch(PDO::FETCH_ASSOC);
foreach ($res as $key => $value) {
$this->$key = $value;
}
}
}
}
public function insert($pdo, $request) {
$pp = prepInsertQuery($request);
$qry = $pdo->prepare("insert into " . SCHEMA . ".seuil (" . $pp['reqFields'] . ") VALUES (" . $pp['reqParams'] . ")");
foreach ($pp['tabD'] as $key => $val) {
$qry->bindValue($key, $val);
}
return returnResultQuery($qry, $pdo, $pp['tabD']);
}
public function update($pdo, $request) {
$pp = prepUpdateQuery($request, 'id_seuil');
$qry = $pdo->prepare("update " . SCHEMA . ".seuil set " . $pp['req'] . " where id_seuil = :id_seuil");
foreach ($pp['tabD'] as $key => $val) {
$qry->bindValue($key, $val);
echo $key . $val . '<br>';
}
return returnResultQuery($qry, $pdo, $pp['tabD']);
}
public function delete($pdo) {
$qry = $pdo->prepare('delete from ' . SCHEMA . '.seuil where id_seuil = :id_seuil');
$qry->bindParam(':id_seuil', $this->id_seuil, PDO::PARAM_INT);
return $qry->execute();
}
}
<?php
/**
* Description of TachePlanifiee
*
* @author srenou
*/
class TachePlanifiee {
public function __construct($pdo, $id = null) {
if ($id !== null) {
$qry = $pdo->prepare('select * from ' . SCHEMA . '.tache_planifiee where id_tache = :id');
$qry->bindParam(':id', $id, PDO::PARAM_INT);
$qry->execute();
$res = $qry->fetch(PDO::FETCH_ASSOC);
foreach ($res as $key => $value) {
$this->$key = $value;
}
}
}
public function update($col, $val, $pdo) {
$req = 'update ' . SCHEMA . '.tache_planifiee set ' . $col . ' = :val where id_tache =:id';
$qry = $pdo->prepare($req);
$qry->bindParam(':val', $val);
$qry->bindParam(':id', $this->id_tache);
return $qry->execute();
}
public function traceExecution($statut, $date_execution, $pdo) {
$this->update('statut_derniere_execution', $statut, $pdo);
$qry = $pdo->prepare('insert into ' . SCHEMA . '.tache_executee (statut,date_execution,id_tache) VALUES (:st,:dt,:id)');
$qry->bindParam(':id', $this->id_tache, PDO::PARAM_INT);
$qry->bindParam(':st', $statut);
$qry->bindParam(':dt', $date_execution);
return $qry->execute();
}
}
<?php
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
class UserProviderNiveaux implements UserProviderInterface {
private $pdo;
public function __construct($pdo) {
$this->pdo = $pdo;
}
public function loadUserByUsername($username) {
$qry = $this->pdo->prepare('SELECT * FROM ' . SCHEMA . '.utilisateur WHERE username = :username');
$username_low = strtolower($username);
$qry->bindParam(':username', $username_low);
$qry->execute();
if ($qry->rowCount()<1) {
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
}
$user = $qry->fetch(PDO::FETCH_ASSOC) ;
$U = new User($user['username'], $user['password'], explode(',', $user['roles']), true, true, true, true, array('prenom'=>$user['prenom'],'nom'=>$user['nom']));
return $U;
}
public function refreshUser(UserInterface $user) {
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class) {
return $class === 'Symfony\Component\Security\Core\User\User';