--- /dev/null
+<?php
+
+class FrontController {
+
+ private $server = array();
+ private $get = array();
+ private $post = array();
+ private $session = array();
+ private $input_content = "";
+
+ public function setServer(array $server){
+ $this->server = $server;
+ }
+
+ public function setGet(array $get){
+ $this->get = $get;
+ }
+
+ public function setPost(array $post){
+ $this->post = $post;
+ }
+
+ public function setSession(array & $session){
+ $this->session = & $session;
+ }
+
+ public function setInput($input_content){
+ $this->input_content = $input_content;
+ }
+
+ public function go(){
+ try {
+ $this->goThrow();
+ } catch (Exception $e){
+ $result = array('retour'=>'KO','message'=>$e->getMessage());
+ echo json_encode($result);
+ }
+ }
+
+ private function goThrow(){
+ if (empty($this->server['REQUEST_METHOD'])){
+ throw new Exception("Impossible de lire la request_method.");
+ }
+
+ if (! in_array($this->server['REQUEST_METHOD'],array('GET','POST'))){
+ throw new Exception("L'action ".get_hecho($this->server['REQUEST_METHOD'])." n'est pas gérée par ce serveur.");
+ }
+
+ if (empty($this->get['action']) && empty($this->post['action'])){
+ throw new Exception("Le paramètre « action » n'a pas été fourni");
+ }
+
+ $action = $this->get['action'];
+ if (! $action){
+ $action = $this->post['action'];
+ }
+
+ $action_translate_name = array(
+ 'synchronize_customers_info' => 'customers_synchronize',
+ 'synchronize_contracts_info' => 'contracts_synchronize',
+ 'check_appliances_infos' => 'appliances_allinfo',
+ 'create_opportunity' => 'appliances_createopportunity',
+ 'upload_info' => 'appliances_updateFromAgent',
+ 'check_updates' => 'appliances_checkUpdateFromAgent',
+ 'get_patch_url' => 'appliances_getPatchURL',
+ );
+
+ if (isset($action_translate_name[$action])){
+ $action = $action_translate_name[$action];
+ }
+
+ list($controller,$function) = explode('_',$action);
+
+ $controllerName = ucfirst($controller)."Controller";
+
+ if (! file_exists(__DIR__."/../controller/{$controllerName}.class.php")) {
+ throw new Exception("L'action ".get_hecho($action)." est inconnue sur ce serveur");
+ }
+
+ /** @var ApplicationController $applicationController */
+ $applicationController = new $controllerName();
+ $applicationController->setGetParameters(new GetParameters($this->get));
+ $applicationController->setPostParameters(new GetParameters($this->post));
+ $applicationController->setInputContent($this->input_content);
+ $applicationController->setSession($this->session);
+
+ $applicationController->preAction();
+
+ $functionName = "{$function}Action";
+ if (! method_exists($applicationController,$functionName)){
+ throw new Exception("L'action ".get_hecho($action)." est inconnue sur ce serveur");
+ }
+
+ $result = $applicationController->$functionName();
+ $result = array("retour"=>"OK","message"=>"",'result'=>$result);
+
+ echo json_encode($result);
+ }
+
+
+
+}
\ No newline at end of file