1593 |
samuel |
1 |
<?php
|
1795 |
jpm |
2 |
// declare(encoding='UTF-8');
|
1593 |
samuel |
3 |
/**
|
1795 |
jpm |
4 |
* Classe principale de chargement des sous-services de Plantnet.
|
1593 |
samuel |
5 |
*
|
1795 |
jpm |
6 |
* @category DEL
|
|
|
7 |
* @package Services
|
|
|
8 |
* @subpackage Plantnet
|
|
|
9 |
* @version 0.1
|
|
|
10 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
11 |
* @author Samuel DUFOUR-KOWALSKI <samuel.dufour@cirad.fr>
|
|
|
12 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
13 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
14 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
15 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
16 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
1593 |
samuel |
17 |
*/
|
2222 |
julien |
18 |
/*
|
|
|
19 |
* Paramètres: date.debut et date.fin (unixtime en secondes), navigation.limite (10 par défaut), navigation.depart, ordre (DESC par défaut)
|
|
|
20 |
* Ne pas Oublier l'api key dans le header $_SERVER['HTTP_API_KEY']
|
|
|
21 |
*/
|
1593 |
samuel |
22 |
class PlantNet extends RestService {
|
|
|
23 |
|
|
|
24 |
private $parametres = array();
|
|
|
25 |
private $ressources = array();
|
|
|
26 |
private $methode = null;
|
1881 |
jpm |
27 |
private $projetNom = 'plantnet';
|
|
|
28 |
private $serviceNom = 'changements';
|
1593 |
samuel |
29 |
private $cheminCourant = null;
|
|
|
30 |
|
|
|
31 |
private $conteneur;
|
1795 |
jpm |
32 |
|
1593 |
samuel |
33 |
/** Indique si oui (true) ou non (false), on veut utiliser les paramètres bruts. */
|
|
|
34 |
protected $utilisationParametresBruts = true;
|
|
|
35 |
|
|
|
36 |
public function __construct() {
|
|
|
37 |
$this->cheminCourant = dirname(__FILE__).DS;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function consulter($ressources, $parametres) {
|
|
|
41 |
$this->methode = 'consulter';
|
|
|
42 |
$reponseHttp = new ReponseHttp();
|
|
|
43 |
try {
|
1881 |
jpm |
44 |
$this->ressources = $ressources;
|
|
|
45 |
$this->parametres = $parametres;
|
1593 |
samuel |
46 |
|
|
|
47 |
$this->conteneur = new Conteneur($this->parametres);
|
1795 |
jpm |
48 |
|
1881 |
jpm |
49 |
$resultat = $this->initialiserService();
|
1593 |
samuel |
50 |
$reponseHttp->setResultatService($resultat);
|
|
|
51 |
} catch (Exception $e) {
|
|
|
52 |
$reponseHttp->ajouterErreur($e);
|
|
|
53 |
}
|
|
|
54 |
$reponseHttp->emettreLesEntetes();
|
1881 |
jpm |
55 |
return $reponseHttp->getCorps();
|
1593 |
samuel |
56 |
}
|
|
|
57 |
|
|
|
58 |
/*------------------------------------------------------------------------------------------------------------------
|
|
|
59 |
CONFIGURATION DU SERVICE
|
|
|
60 |
------------------------------------------------------------------------------------------------------------------*/
|
|
|
61 |
private function initialiserService() {
|
|
|
62 |
$classe = $this->obtenirNomClasseService($this->serviceNom);
|
|
|
63 |
$chemins = array();
|
|
|
64 |
$chemins[] = $this->cheminCourant.$this->projetNom.DS.$classe.'.php';
|
|
|
65 |
$retour = '';
|
|
|
66 |
$service = null;
|
|
|
67 |
foreach ($chemins as $chemin) {
|
1881 |
jpm |
68 |
if (file_exists($chemin)) {
|
2222 |
julien |
69 |
$authorized_key = $this->conteneur->getParametre('api.key');
|
|
|
70 |
// Vérification de la clé API (et oui je sais, c'est relou, mais il faut bien commencer à sécuriser nos webservices)
|
|
|
71 |
if ($_SERVER['HTTP_API_KEY'] == $authorized_key){
|
|
|
72 |
require_once $chemin;
|
|
|
73 |
$service = new $classe($this->conteneur);
|
|
|
74 |
$retour = $service->consulter($this->ressources, $this->parametres);
|
|
|
75 |
} else {
|
|
|
76 |
$message = "Accès au service '{$this->projetNom}' refusé, veuillez vérifier votre clé API.";
|
|
|
77 |
$code = RestServeur::HTTP_CODE_ERREUR;
|
|
|
78 |
throw new Exception($message, $code);
|
|
|
79 |
}
|
|
|
80 |
|
1881 |
jpm |
81 |
}
|
1593 |
samuel |
82 |
}
|
1795 |
jpm |
83 |
|
1593 |
samuel |
84 |
if (is_null($service)) {
|
|
|
85 |
$message = "Le service demandé '{$this->serviceNom}' n'existe pas dans le projet {$this->projetNom} !";
|
|
|
86 |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;
|
|
|
87 |
throw new Exception($message, $code);
|
|
|
88 |
}
|
|
|
89 |
return $retour;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function obtenirNomClasseService($mot) {
|
|
|
93 |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot))));
|
|
|
94 |
return $classeNom;
|
|
|
95 |
}
|
1881 |
jpm |
96 |
}
|