--- /dev/null
+<?php
+
+class AlloServeur {
+
+ private $allo_serveur_url;
+ private $cookie;
+
+ private $last_http_response_header;
+
+ public function __construct($allo_serveur_url){
+ $this->allo_serveur_url = $allo_serveur_url;
+ }
+
+ public function login($login,$password){
+ if (! $login){
+ return;
+ }
+ $result = $this->callPost('session_login',array('login'=>$login,'password'=>$password));
+ if ($result['is_loggued'] != true){
+ throw new Exception("Mauvais login ou mot de passe");
+ }
+ foreach($this->last_http_response_header as $response){
+ if (preg_match("#^Set-Cookie: ([^;]*);#i",$response,$matches)){
+ $this->cookie = $matches[1];
+ }
+ }
+ }
+
+ private function call($action,array $parameters = array()){
+ $url = "{$this->allo_serveur_url}?action={$action}";
+ $content = $this->fileGetContentsWithGet($url,$parameters);
+ return $this->traitementContent($url,$content);
+ }
+
+ private function callPost($action, array $parameters = array()){
+ $url = "{$this->allo_serveur_url}?action={$action}";
+ $content = $this->fileGetContentsWithPost($url,$parameters);
+ return $this->traitementContent($url,$content);
+ }
+
+ private function traitementContent($url,$content){
+ if (! $content){
+ throw new Exception("Impossible d'appeller le serveur ALLO ($url failed)");
+ }
+ $content = json_decode($content,true);
+ if (! $content){
+ throw new Exception("La réponse du serveur ALLO n'est pas compréhensible ($url)");
+ }
+ if ($content['retour'] != 'OK'){
+ throw new Exception("[$url] Le sereveur ALLO a répondu : {$content['retour']} : {$content['message']}");
+ }
+ return $content['result'];
+ }
+
+ private function fileGetContentsWithGet($url,$parameters){
+ foreach($parameters as $key => $value){
+ $url.="&$key=$value";
+ }
+ $opts = array(
+ 'http'=>array(
+ 'method'=> "GET",
+ 'header'=> "Cookie: {$this->cookie}\r\n"
+ )
+ );
+ $context = stream_context_create($opts);
+ return file_get_contents($url,false,$context);
+ }
+
+ private function fileGetContentsWithPost($url,$parameters){
+ $postdata = http_build_query($parameters);
+
+ $opts = array('http' =>
+ array(
+ 'method' => 'POST',
+ 'header' => 'Content-type: application/x-www-form-urlencoded',
+ 'content' => $postdata,
+ )
+ );
+
+ $context = stream_context_create($opts);
+
+ $result = file_get_contents($url, false, $context);
+ $this->last_http_response_header = $http_response_header;
+ return $result;
+ }
+
+ public function customerExists($ap_id){
+ return $this->call("customers_exists",array('ap_id'=>$ap_id));
+ }
+
+ public function listContract($ap_id){
+ return $this->call("contracts_listByCustomer",array("ap_id"=>$ap_id));
+ }
+
+ public function productExitsByName($name){
+ return $this->call("anonymous_productExistsByName",array("name"=>$name));
+ }
+
+ public function versionExistsByValue($product_name,$version_value){
+ return $this->call("anonymous_versionExistsByValue",array("product_name"=>$product_name,"version_value"=>$version_value));
+ }
+
+ public function applianceCreate($contract_number,$fqdn,$channel_key,$produit,$type_hebergement_key){
+ return $this->call("appliances_create",
+ array(
+ "contract_number"=>$contract_number,
+ "fqdn" => $fqdn,
+ "channel_key" => $channel_key,
+ "produit" => $produit,
+ "type_hebergement_key" => $type_hebergement_key,
+ )
+ );
+ }
+
+ public function applianceInfo($uuid){
+ $info = $this->call("anonymous_getInfoByUUID",array("uuid"=>$uuid));
+ if(! $info){
+ throw new Exception("L'instance n'existe pas sur le seveur Allo");
+ }
+ $info['uuid'] = array('libelle'=>'UUID','value'=>$uuid);
+ return $info;
+ }
+
+ public function updateInstance($uuid,$produit,$version,$os,$disques){
+ $info = $this->callPost("anonymous_update",
+ array(
+ 'uuid' => $uuid,
+ 'produit' => $produit,
+ 'version' => $version,
+ 'os' => $os,
+ 'disques' => $disques
+ )
+ );
+ return $info;
+ }
+
+ public function patchExists($uuid){
+ return $this->call('anonymous_patchExists',array('uuid'=>$uuid));
+ }
+
+ public function deleteAppliance($uuid){
+ return $this->call('appliances_deleteByUUID',array('uuid'=>$uuid));
+ }
+
+ public function listChannel(){
+ return $this->call('channel_list');
+ }
+
+ public function listTypeHebergement(){
+ return $this->call("typeHebergement_list");
+ }
+
+}
\ No newline at end of file