Example #1
0
 public function save($value)
 {
     $flag = $this->module->flag;
     $path = File::formatDir($this->path);
     $destPath = static::storagePath() . File::formatDir($this->path);
     File::mkdir($destPath);
     if (!is_writable($destPath)) {
         Response::code(500);
         return Response::json(array("error" => true, "error_description" => "Directory '" . $destPath . "' is not writtable."));
     }
     $files = json_decode(Session::get($this->sessionKey), true);
     if ($flag == "U" || $flag == "D") {
         $oldFiles = $this->module->orm->field($this->name);
         if (!is_array(@json_decode($oldFiles, true))) {
             $oldFiles = json_encode(array());
         }
         $oldFiles = json_decode($oldFiles, true);
         if ($flag == "U") {
             //Delete files that are on our table but not on our session list
             foreach ($oldFiles as $oldFile) {
                 $found = false;
                 foreach ($files as $file) {
                     if (!array_key_exists("_tmpName", $file) && current($oldFile) == current($file)) {
                         $found = true;
                         break;
                     }
                 }
                 if (!$found) {
                     foreach ($oldFile as $sample) {
                         File::delete(static::storagePath() . $sample);
                     }
                 }
             }
         } else {
             if ($flag == "D") {
                 //Delete all files
                 foreach ($oldFiles as $oldFile) {
                     foreach ($oldFile as $sample) {
                         File::delete(static::storagePath() . $sample);
                     }
                 }
                 if (count(File::lsdir($destPath)) == 0) {
                     File::rmdir($destPath);
                 }
             }
         }
     }
     if ($flag == "C" || $flag == "U") {
         //Copy tmp files to it's target place and save
         foreach ($files as $k => $file) {
             if (array_key_exists("_tmpName", $file)) {
                 $samples = array();
                 $im = new Image();
                 $i = 0;
                 $tmpPath = static::tmpPath() . $file["_tmpName"];
                 foreach ($this->samples as $k2 => $sample) {
                     //if ($k2 == 0)
                     //{
                     $im->load($tmpPath);
                     //}
                     if (!array_key_exists("width", $sample)) {
                         $sample["width"] = 0;
                     }
                     if (!array_key_exists("height", $sample)) {
                         $sample["height"] = 0;
                     }
                     if ($sample["width"] != 0 || $sample["height"] != 0) {
                         $im->resize($sample["width"], $sample["height"], $sample["resizeMethod"], $sample["background"]);
                     }
                     if ($this->customFilters) {
                         foreach ($this->customFilters as $filter) {
                             call_user_func($filter, $im);
                         }
                     }
                     $fileName = $file["_name"];
                     if ($sample["key"] != "original") {
                         $fileName = File::removeExtension($file["_name"]) . "-" . $sample["key"] . "." . File::extension($file["_name"]);
                     }
                     $unique = static::unique($destPath . $fileName);
                     $im->save($unique);
                     $samples[$sample["key"]] = $path . File::fileName($unique);
                     $samples["caption"] = array_key_exists('caption', $file) ? $file['caption'] : "";
                     $i++;
                 }
                 File::delete($tmpPath);
                 $files[$k] = $samples;
             }
         }
         $this->module->orm->setField($this->name, json_encode($files));
     }
 }
Example #2
0
 public function upload($flag, $id)
 {
     if (isset($_FILES["attachment"])) {
         $info = $_FILES["attachment"];
         $num = count($info["name"]);
         $files = json_decode(Session::get($this->sessionKey), true);
         $remaining = $this->limit - count($files);
         $num = min($num, $remaining);
         if ($remaining <= 0) {
             return array("error" => true, "error_description" => "Limite de arquivos excedido.");
         }
         $ok = 0;
         $acceptError = 0;
         $permissionError = 0;
         for ($i = 0; $i < $num; $i++) {
             $ext = File::extension($info["name"][$i]);
             if (count($this->accepts) > 0) {
                 $mime = File::mime($ext);
                 if (array_search($mime, $this->accepts) === false) {
                     // echo $mime . "\n";
                     // print_r($this->accepts);
                     $acceptError++;
                     continue;
                 }
             }
             $fileName = File::removeExtension($info["name"][$i]);
             $destFile = $fileName . static::tmpKey() . "." . $ext;
             if (@move_uploaded_file($info["tmp_name"][$i], static::tmpPath() . $destFile)) {
                 $files[] = array("_name" => $fileName . "." . $ext, "caption" => "", "_tmpName" => $destFile);
                 $ok++;
             } else {
                 $permissionError++;
             }
         }
         Session::set($this->sessionKey, json_encode($files));
         if ($permissionError > 0 && $ok == 0) {
             return array("error" => true, "error_description" => "Erro de permissão de arquivo. Contate o desenvolvedor.");
         } else {
             if ($acceptError > 0 && $ok == 0) {
                 return array("error" => true, "error_description" => "Extensão não permitida.");
             }
         }
         return array("error" => false, "items" => $this->items());
     }
     return array("error" => true, "error_description" => "O arquivo é maior que o limite de " . ini_get('upload_max_filesize') . " do servidor");
 }