2954 |
mathias |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* @author Mathias Chouet <mathias@tela-botanica.org>
|
|
|
4 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
5 |
* @copyright Copyright (c) 2016, Tela Botanica (accueil@tela-botanica.org)
|
|
|
6 |
*/
|
|
|
7 |
class Savapa extends WidgetCommun {
|
|
|
8 |
|
|
|
9 |
const DS = DIRECTORY_SEPARATOR;
|
|
|
10 |
const SERVICE_DEFAUT = 'savapa';
|
|
|
11 |
/** Si spécifié, on ajoute une barre de navigation inter-applications */
|
|
|
12 |
private $bar;
|
|
|
13 |
/** liste des requetes (dans requetes.json) */
|
|
|
14 |
protected $requetes;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Méthode appelée par défaut pour charger ce widget.
|
|
|
18 |
*/
|
|
|
19 |
public function executer() {
|
|
|
20 |
$retour = null;
|
|
|
21 |
//extract($this->parametres); // aaaargh mon pauvre cœur :'(
|
|
|
22 |
$this->bar = (isset($bar)) ? $bar : false;
|
|
|
23 |
|
|
|
24 |
// chargement de la config
|
|
|
25 |
$fichierConfig = dirname(__FILE__) . self::DS . "config.json";
|
|
|
26 |
try {
|
|
|
27 |
$this->configJson = json_decode(file_get_contents($fichierConfig), true);
|
|
|
28 |
} catch (Exception $ex) {
|
|
|
29 |
return "Veuillez remplir un fichier [config.json] à la racine du widget";
|
|
|
30 |
}
|
|
|
31 |
//var_dump($this->config);
|
|
|
32 |
|
|
|
33 |
// connexion à la base
|
|
|
34 |
// @TODO remplaer par un WS mais le cas ne s'y prête pas
|
|
|
35 |
$dsn = "mysql:dbname=" . $this->configJson['bd']['base']
|
|
|
36 |
. ";host=" . $this->configJson['bd']['hote']
|
|
|
37 |
. ";port=" . $this->configJson['bd']['port'];
|
|
|
38 |
$bd = new PDO($dsn, $this->configJson['bd']['utilisateur'], $this->configJson['bd']['mdp']);
|
|
|
39 |
|
|
|
40 |
// exécution en file indienne (attention au temps)
|
|
|
41 |
foreach ($this->configJson['requetes'] as &$req) {
|
|
|
42 |
$this->filtrerRequete($req['sql']); // TRÈS important
|
|
|
43 |
$r = $bd->query($req['sql']);
|
|
|
44 |
$donneesBrutes = $r->fetchAll(PDO::FETCH_ASSOC);
|
|
|
45 |
$labels = array();
|
|
|
46 |
$donnees = array();
|
|
|
47 |
foreach($donneesBrutes as $e) {
|
|
|
48 |
if ($e["periode"] != null) {
|
|
|
49 |
$labels[] = $e["periode"];
|
|
|
50 |
$donnees[] = $e["nombre"];
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
$req['resultat'] = array(
|
|
|
54 |
'labels' => $labels,
|
|
|
55 |
'donnees' => $donnees
|
|
|
56 |
);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// données
|
|
|
60 |
$retour['donnees']['url_base'] = sprintf($this->config['chemins']['baseURLAbsoluDyn'], '');
|
|
|
61 |
$retour['donnees']['url_export'] = sprintf($this->config['chemins']['baseURLServicesCelTpl'], 'CelWidgetExport');
|
|
|
62 |
$retour['donnees']['url_script_navigation'] = sprintf($this->config['chemins']['baseURLRessources'], 'tb/reseau/navigation.js');
|
|
|
63 |
|
|
|
64 |
$retour['donnees']['bar'] = $this->bar;
|
|
|
65 |
$retour['donnees']['requetes'] = $this->configJson['requetes'];
|
|
|
66 |
//$retour['donnees']['prod'] = ($this->config['parametres']['modeServeur'] == "prod");
|
|
|
67 |
|
|
|
68 |
// rendu
|
|
|
69 |
$squelette = dirname(__FILE__).self::DS.'squelettes'.self::DS.'savapa.tpl.html';
|
|
|
70 |
$contenu = $this->traiterSquelettePhp($squelette, $retour['donnees']);
|
|
|
71 |
$this->envoyer($contenu);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// empêche de tout péter
|
|
|
75 |
protected function filtrerRequete($r) {
|
|
|
76 |
$instructions = array("INSERT", "UPDATE", "DELETE", "TRUNCATE", "DROP", "ADD");
|
|
|
77 |
foreach ($instructions as $i) {
|
|
|
78 |
if (stripos($r, $i) !== false) {
|
|
|
79 |
throw new Exception('Les instructions ' . implode(', ', $instructions) . 'sont interdites, galopin !');
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
?>
|