Example #1
0
 /**
  * ATTENTION AU NAME DE L'INPUT
  * ==> FORM[INPUTNAME] <==
  *
  * ATTENTION A LA TECHNIQUE DE RENVOIE D'INFORMATION !M&eacute;thode priv&eacute;e
  *
  * @return void
  */
 public function upload_async()
 {
     $datas = null;
     $response = array("error" => "");
     if (!isset($_POST["form_name"]) || empty($_POST["form_name"])) {
         $response["error"] = '$_POST["form_name"] require';
         $this->response($response);
     }
     if (!isset($_POST["input_name"]) || empty($_POST["input_name"])) {
         $response["error"] = '$_POST["input_name"] require';
         $this->response($response);
     }
     $file = $_FILES[$_POST["input_name"]];
     if (!isset($file) || empty($file)) {
         $response["error"] = "Aucun fichier n'a été transmis";
     }
     if (empty($response["error"])) {
         Configuration::$site_application = $_POST["application"];
         $path_to_form = "includes/applications/" . $_POST["application"] . "/modules/";
         if ($_POST["is_backoffice"]) {
             $path_to_form .= "back/";
         } else {
             $path_to_form .= "front/";
         }
         $form_name = $_POST["form_name"];
         $path_to_form .= "forms/form." . $form_name . ".json";
         if (!file_exists($path_to_form)) {
             $path_to_form = preg_replace("/_[0-9]+\\.json\$/", ".json", $path_to_form);
         }
         try {
             $datas = SimpleJSON::import($path_to_form);
         } catch (Exception $e) {
             $response["error"] = "Formulaire introuvable " . $path_to_form;
             $this->response($response);
         }
         if (!is_array($datas[$_POST["input_name"]])) {
             $response["error"] = "Champs cibl&eacute; introuvable";
             $this->response($response);
         }
         $input = $datas[$_POST["input_name"]];
         if ($input["tag"] != Form::TAG_UPLOAD && ($input["tag"] != "input" && $input["attributes"]["type"] != "file")) {
             $response["error"] = "Le champ ciblé n'est pas un input type 'file'";
             $this->response($response);
         }
         $fileName = "";
         if (isset($input["fileName"])) {
             $fileName = "file" . rand(0, 999999);
         }
         $folderName = Form::PATH_TO_UPLOAD_FOLDER;
         if (isset($input["folder"])) {
             $folderName .= $input["folder"];
         }
         $upload = new Upload($file, $folderName, $fileName);
         if (isset($input["resize"]) && is_array($input["resize"])) {
             $upload->resizeImage($input["resize"][0], $input["resize"][1]);
         }
         if (!$upload->isMimeType($input["fileType"])) {
             $response["error"] = "Type de fichier non-autorisé (" . $input["fileType"] . ")";
             $this->response($response);
         }
         try {
             $upload->send();
         } catch (Exception $e) {
             $response["error"] = "Upload impossible";
             $this->response($response);
         }
         if (isset($input["fileName"]) && !empty($input["fileName"])) {
             $fileName = preg_replace("/(\\{id\\})/", $upload->id_upload, $input["fileName"]);
             $upload->renameFile($fileName);
         }
         $response["path_upload"] = (Configuration::$site_application != "main" ? "../" : "") . $upload->pathFile;
         $response["id_upload"] = $upload->id_upload;
     }
     $this->response($response);
 }