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) && $oldFile["path"] == $file["path"]) { $found = true; break; } } if (!$found) { File::delete(static::storagePath() . $oldFile["path"]); } } } else { if ($flag == "D") { //Delete all files foreach ($oldFiles as $oldFile) { File::delete(static::storagePath() . $oldFile["path"]); } 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)) { $unique = static::unique($destPath . $file["_name"]); File::move(static::tmpPath() . $file["_tmpName"], $unique); $files[$k] = array("path" => $path . File::fileName($unique), "caption" => array_key_exists('caption', $file) ? $file['caption'] : ""); } } $this->module->orm->setField($this->name, json_encode($files)); } }
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)); } }
break; } } if (!$allowed) { Response::code(403); return; } Response::download(J_PATH . $path, Request::get("name")); }); Router::register("GET", "thumb/", function () { $pieces = explode("/", trim(Request::get("path"), "/")); $path = implode(DS, $pieces); $allowedPaths = Config::item("application", "thumbPaths", array("app/storage/")); $allowed = false; foreach ($allowedPaths as $dir) { if (Str::startsWith($path, File::formatDir($dir))) { $allowed = true; break; } } if (!$allowed || count($pieces) == 0) { return Response::code(403); } $path = implode(DS, $pieces); if (!File::exists(J_PATH . $path) || is_dir(J_PATH . $path)) { return Response::code(404); } $im = new Image(J_PATH . $path); $im->resize((int) Request::get("width"), (int) Request::get("height"), Request::get("method", "fit"), Request::get("background", 0xffffff)); $im->header(); });