| 3 | jpm | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | /**
 | 
        
           |  |  | 4 | * Description :
 | 
        
           |  |  | 5 | * Classe Aide.php fournit des informations sur les services des projets : leur état (up ou down), une description,
 | 
        
           |  |  | 6 | * le lien vers le wikini
 | 
        
           |  |  | 7 | * La ressource /projets donne la liste de tous les projets concernés par cette api.
 | 
        
           |  |  | 8 | *
 | 
        
           |  |  | 9 | * Encodage en entrée : utf8
 | 
        
           |  |  | 10 | * Encodage en sortie : utf8
 | 
        
           |  |  | 11 | * @package framework-v3
 | 
        
           |  |  | 12 | * @author Jennifer Dhé <jennifer.dhe@tela-botanica.org>
 | 
        
           |  |  | 13 | * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
 | 
        
           |  |  | 14 | * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
 | 
        
           |  |  | 15 | * @version 1.0
 | 
        
           |  |  | 16 | * @copyright 1999-${year} Tela Botanica (accueil@tela-botanica.org)
 | 
        
           |  |  | 17 | */
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | class Aide extends RestService {
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | 	protected $retour_format = 'max';
 | 
        
           |  |  | 22 | 	protected $url_wikini = 'http://www.tela-botanica.org/wikini/eflore/wakka.php?wiki=';
 | 
        
           |  |  | 23 | 	protected $url_liste_projet = '';
 | 
        
           |  |  | 24 | 	protected $table_retour = array();
 | 
        
           |  |  | 25 | 	protected $corps_http = '';
 | 
        
           |  |  | 26 | 	protected $entete_http = '';
 | 
        
           |  |  | 27 | 	protected $projet = '';
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | 	public function consulter($ressources, $parametres) {
 | 
        
           |  |  | 31 | 		$this->url_liste_projet = Config::get('url_service_base').'commun/aide/projets';
 | 
        
           |  |  | 32 | 		$this->projet = Config::get('nom_projet');
 | 
        
           |  |  | 33 | 		$this->traiterRessources($ressources);
 | 
        
           | 236 | delphine | 34 | 		return $this->formerReponseHTTP($this->table_retour);
 | 
        
           | 3 | jpm | 35 | 	}
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | //-----------------------------------------traiter reponse http-------------------------------------------------------------
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 | 	public function formerReponseHTTP($resultat_formate, $mime = 'application/json', $encodage= 'utf-8') {
 | 
        
           |  |  | 40 | 		// Si aucune erreur n'a été établie (donc un tableau de résultat a bien ete renvoyé...)
 | 
        
           |  |  | 41 | 		if ($this->corps_http == '' && $this->entete_http == '') {
 | 
        
           |  |  | 42 | 			$this->entete_http = RestServeur::HTTP_CODE_OK;
 | 
        
           |  |  | 43 | 			$this->corps_http  = $resultat_formate;
 | 
        
           |  |  | 44 | 		}
 | 
        
           |  |  | 45 | 		if ($this->entete_http != RestServeur::HTTP_CODE_OK) {
 | 
        
           |  |  | 46 | 			$mime = 'text/html';
 | 
        
           |  |  | 47 | 		}
 | 
        
           |  |  | 48 | 		// Gestion du type de contenu
 | 
        
           |  |  | 49 | 		if (!is_null($mime) && !is_null($encodage)) {
 | 
        
           | 952 | raphael | 50 | 			if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) header("Content-Type: $mime; charset=$encodage");
 | 
        
           | 3 | jpm | 51 | 		} else if (!is_null($mime) && is_null($encodage)) {
 | 
        
           | 952 | raphael | 52 | 			if(strpos($_SERVER['SCRIPT_NAME'], 'phpunit') === FALSE) header("Content-Type: $mime");
 | 
        
           | 3 | jpm | 53 | 		}
 | 
        
           |  |  | 54 | 		// Envoie de l'entête
 | 
        
           |  |  | 55 | 		RestServeur::envoyerEnteteStatutHttp($this->entete_http);
 | 
        
           |  |  | 56 | 		// Envoie du corps
 | 
        
           | 236 | delphine | 57 | 		return $this->corps_http;
 | 
        
           | 3 | jpm | 58 | 	}
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | 	public function renvoyerErreur($e, $c) {
 | 
        
           |  |  | 61 | 		$this->entete_http = $e;
 | 
        
           |  |  | 62 | 		$this->corps_http  = $c;
 | 
        
           |  |  | 63 | 	}
 | 
        
           |  |  | 64 |   | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 | //---------------------------------traiter Ressources------------------------------------------------------------------
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 | 	public function traiterRessources($ressources) {
 | 
        
           |  |  | 69 | 		if (isset($ressources) && !empty($ressources)) {
 | 
        
           |  |  | 70 | 			//-----------------------service /aide/projets------------------------------------
 | 
        
           |  |  | 71 | 			if ($ressources[0] == 'projets') {
 | 
        
           |  |  | 72 | 				$this->afficherInfosTousProjets();
 | 
        
           |  |  | 73 | 			} else {
 | 
        
           |  |  | 74 | 				$r = 'Erreur dans votre requête </br> Ressources disponibles :
 | 
        
           |  |  | 75 | 					 <br/><li> /aide </li><li> /aide/projets </li>';
 | 
        
           |  |  | 76 | 				$this->renvoyerErreur(RestServeur::HTTP_CODE_MAUVAISE_REQUETE, $r);
 | 
        
           |  |  | 77 | 			}
 | 
        
           |  |  | 78 | 		} else {
 | 
        
           |  |  | 79 | 			//-----------------------service /aide------------------------------------
 | 
        
           |  |  | 80 | 			$this->afficherInfosProjet();
 | 
        
           |  |  | 81 | 			$this->formaterAideListeProjetEtRessources();
 | 
        
           |  |  | 82 | 		}
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 | 	}
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 | 	public function afficherInfosTousProjets() {
 | 
        
           |  |  | 88 | 		$req = 'show tables';
 | 
        
           |  |  | 89 | 		$tables = $this->getBdd()->recupererTous($req);
 | 
        
           |  |  | 90 | 		foreach ($tables as $table) {
 | 
        
           |  |  | 91 | 			if (preg_match('/^(.+)_meta$/', array_shift($table), $projet)) {
 | 
        
           |  |  | 92 | 				if (isset($projet[1])) {
 | 
        
           |  |  | 93 | 					$projets[] = $projet[1];
 | 
        
           |  |  | 94 | 				} else {
 | 
        
           |  |  | 95 | 					$e = "erreur.";
 | 
        
           |  |  | 96 | 					trigger_error($e, E_USER_ERROR);
 | 
        
           |  |  | 97 | 				}
 | 
        
           |  |  | 98 | 			}
 | 
        
           |  |  | 99 | 		}
 | 
        
           |  |  | 100 | 		foreach ($projets as $projet) {
 | 
        
           |  |  | 101 | 			$this->table_retour[$projet] = $this->recupererInfoProjet($projet);
 | 
        
           |  |  | 102 | 		}
 | 
        
           |  |  | 103 | 	}
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |   | 
        
           |  |  | 106 | 	public function recupererInfoProjet($projet) {
 | 
        
           |  |  | 107 | 		$res_return = null;
 | 
        
           |  |  | 108 | 		$projet = str_replace('_', '-', $projet);
 | 
        
           |  |  | 109 | 		$url = Config::get('url_service_base').$projet.'/aide';
 | 
        
           |  |  | 110 | 		$intitule = 'PROJET '.strtoupper($projet);
 | 
        
           |  |  | 111 | 		$res = $this->consulterHref($url);
 | 
        
           |  |  | 112 | 		if ($res) {
 | 
        
           |  |  | 113 | 			$res_return = $res->$intitule;
 | 
        
           |  |  | 114 | 		}
 | 
        
           |  |  | 115 | 		return $res_return;
 | 
        
           |  |  | 116 | 	}
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 | 	public function formaterAideListeProjetEtRessources() {
 | 
        
           |  |  | 120 | 		$projets = 'LISTE DES PROJETS';
 | 
        
           |  |  | 121 | 		$services = 'Ressources';
 | 
        
           |  |  | 122 | 		$this->table_retour[$projets]['liste des projets en place']['href'] = $this->url_liste_projet;
 | 
        
           |  |  | 123 | 		$this->table_retour[$projets]['liste de tous les projets']['wikini'] = $this->url_wikini.'EfloreIntegrationProjets';
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 | 	}
 | 
        
           |  |  | 126 |   | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |   | 
        
           |  |  | 129 | 	public function afficherInfosProjet() {
 | 
        
           |  |  | 130 | 		$res = array();
 | 
        
           |  |  | 131 | 		$ressources = $this->recupererListeDesRessources();
 | 
        
           |  |  | 132 | 		if (isset($ressources['href']) && $ressources['href'] != '') {
 | 
        
           |  |  | 133 | 			$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.href'] = $ressources['href'];
 | 
        
           |  |  | 134 | 		}
 | 
        
           |  |  | 135 | 		$etats = $this->verifierEtatDesRessources($ressources['WS']);
 | 
        
           |  |  | 136 | 		$this->table_retour['PROJET '.strtoupper($this->projet)]['projet.services'] = $etats['projet.services'];
 | 
        
           |  |  | 137 | 	}
 | 
        
           |  |  | 138 |   | 
        
           |  |  | 139 |   | 
        
           |  |  | 140 |   | 
        
           |  |  | 141 | 	public function recupererListeDesRessources() {
 | 
        
           |  |  | 142 | 		$resultat = null;
 | 
        
           |  |  | 143 | 		$req = 'SELECT url_projet, version, web_services FROM '.Config::get('bdd_table_meta').' ORDER BY CAST(version as DECIMAL)';
 | 
        
           |  |  | 144 | 		$res = $this->getBdd()->recuperer($req);
 | 
        
           |  |  | 145 | 		if ($res == '') {
 | 
        
           |  |  | 146 | 			$r = 'La requête SQL formée comporte une erreur !!';
 | 
        
           |  |  | 147 | 			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $r);
 | 
        
           |  |  | 148 | 			Debug::printr($req);
 | 
        
           |  |  | 149 | 		} elseif ($res) {
 | 
        
           |  |  | 150 | 			$resultat['WS'] = $res['web_services'];
 | 
        
           |  |  | 151 | 			$resultat['href'] = $res['url_projet'];
 | 
        
           |  |  | 152 | 		} else {
 | 
        
           |  |  | 153 | 			$d = 'Les données recherchées sont introuvables dans la version '.$res[0]['version'].'de la table de méta données';
 | 
        
           |  |  | 154 | 			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $d);
 | 
        
           |  |  | 155 | 			Debug::printr($req);
 | 
        
           |  |  | 156 | 		}
 | 
        
           |  |  | 157 | 		return $resultat;
 | 
        
           |  |  | 158 | 	}
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 | 	public function verifierEtatDesRessources($ressources) {
 | 
        
           |  |  | 162 | 		$ressources = explode(';', $ressources);
 | 
        
           |  |  | 163 | 		$etats = array();
 | 
        
           |  |  | 164 | 		foreach ($ressources as $key => $ressApi) {
 | 
        
           |  |  | 165 | 			list($ress, $api) = explode(':', $ressApi);
 | 
        
           |  |  | 166 | 			$this->translitererRess($ress);
 | 
        
           |  |  | 167 | 			$url = Config::get('url_service').'/'.$ress;
 | 
        
           | 899 | raphael | 168 | 			$wikini = $this->url_wikini.'EfloreApi'.str_replace('.', '', $api).$this->creerChaMot($ress);
 | 
        
           | 3 | jpm | 169 | 			$url_service = Config::get('url_service').'/'.$ress;
 | 
        
           |  |  | 170 | 			$etat = array(
 | 
        
           |  |  | 171 | 					'service.nom' => $ress,
 | 
        
           |  |  | 172 | 					'service.href' => $url_service,
 | 
        
           |  |  | 173 | 					'service.wikini' => $wikini,
 | 
        
           |  |  | 174 | 					'service.etat' => 'UP');
 | 
        
           | 830 | raphael | 175 |   | 
        
           | 3 | jpm | 176 | 			$res = $this->consulterHref($url);
 | 
        
           |  |  | 177 | 			if (!$res) {
 | 
        
           | 830 | raphael | 178 | 				$etat['service.etat'] = 'DOWN';
 | 
        
           | 3 | jpm | 179 | 			}
 | 
        
           |  |  | 180 | 			$etats['projet.services'][] = $etat;
 | 
        
           |  |  | 181 | 		}
 | 
        
           |  |  | 182 | 		return $etats;
 | 
        
           |  |  | 183 | 	}
 | 
        
           |  |  | 184 |   | 
        
           |  |  | 185 |   | 
        
           |  |  | 186 | 	/**Permet de consulter une url et retourne le résultat ou une erreur
 | 
        
           |  |  | 187 | 	 * @param $url	 */
 | 
        
           |  |  | 188 | 	public function consulterHref($url) {
 | 
        
           |  |  | 189 | 		$res = $this->getRestClient()->consulter($url);
 | 
        
           |  |  | 190 | 		$entete = $this->getRestClient()->getReponseEntetes();
 | 
        
           |  |  | 191 | 		//Si le service meta-donnees fonctionne correctement, l'entete comprend la clé wrapper_data
 | 
        
           |  |  | 192 | 		if (isset($entete['wrapper_data'])) {
 | 
        
           |  |  | 193 | 			$res = json_decode($res);
 | 
        
           |  |  | 194 | 			return $res;
 | 
        
           |  |  | 195 | 		} else {
 | 
        
           |  |  | 196 | 			$u = 'L\'url <a href="'.$url.'">'.$url.'</a> lancée via RestClient renvoie une erreur';
 | 
        
           |  |  | 197 | 			$this->renvoyerErreur(RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE, $u);
 | 
        
           |  |  | 198 | 		}
 | 
        
           |  |  | 199 | 	}
 | 
        
           |  |  | 200 |   | 
        
           |  |  | 201 |   | 
        
           |  |  | 202 | 	public function translitererRess(&$ress) {
 | 
        
           |  |  | 203 | 		$ress = strtolower($ress);
 | 
        
           |  |  | 204 | 		if ($ress == 'metadonnees') {
 | 
        
           |  |  | 205 | 			$ress = 'meta-donnees';
 | 
        
           |  |  | 206 | 		}
 | 
        
           |  |  | 207 | 	}
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 | 	public function creerChaMot($nom) {
 | 
        
           |  |  | 211 | 		$chaine = new Chaine();
 | 
        
           |  |  | 212 | 		$nom = $chaine->supprimerAccents($nom);
 | 
        
           |  |  | 213 | 		$nom_modif = '';
 | 
        
           |  |  | 214 | 		$nom_dec = preg_split('/-/', $nom);
 | 
        
           |  |  | 215 | 		foreach ($nom_dec as $dec) {
 | 
        
           |  |  | 216 | 			$nom_modif .= ucfirst($dec);
 | 
        
           |  |  | 217 | 		}
 | 
        
           |  |  | 218 |         return trim($nom_modif);
 | 
        
           |  |  | 219 |     }
 | 
        
           |  |  | 220 |   | 
        
           |  |  | 221 | }
 | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 | ?>
 |