From cda0c33a5d752305dcdf7e14dfb0029771bdeaa2 Mon Sep 17 00:00:00 2001 From: Gabriel Carneiro Date: Tue, 6 Aug 2024 10:42:22 -0300 Subject: [PATCH] add json db to php --- php/library/json-db.class.php | 158 ++++++++++++++++++++++++++++++++++ php/upload.php | 118 +++++++++++++++++-------- 2 files changed, 238 insertions(+), 38 deletions(-) create mode 100644 php/library/json-db.class.php diff --git a/php/library/json-db.class.php b/php/library/json-db.class.php new file mode 100644 index 0000000..b1f6888 --- /dev/null +++ b/php/library/json-db.class.php @@ -0,0 +1,158 @@ +path = $path; + + if(!file_exists($path)){ + // If the .json extension is not provided, append it + if(strpos($path, '.json') === false){ $path .= '.json'; } + + $fp = fopen($path,"wb"); + fwrite($fp, "{}"); + fclose($fp); + } + + // Get the contect of the current path + $this->db = json_decode(file_get_contents($path), false, 512, JSON_UNESCAPED_UNICODE); + } + + /** + * SAVE + * Save the new db + */ + private function save(){ + $json = ($this->db === "{}") ? $this->db : json_encode($this->db, JSON_UNESCAPED_UNICODE); + + file_put_contents($this->path, $json); + } + + /** + * INSERT + * + * @param $data: array + * @param $key: string (optional) + * + * @return DB + */ + public function insert($data, $key = ""){ + if($key !== "") + $this->db[$key] = $data; + else + $this->db[] = $data; + + $this->save(); + + return $this; + } + + /** + * UPDATE + * + * @param $data: array + * @param $key: string + * + * @return DB + */ + public function update($data, $key){ + if($key !== "") + $update = array_merge($this->db[$key], $data); + $this->db[$key] = $update; + + $this->save(); + + return $this; + } + + /** + * DELETE + * + * @param $key: string + * + * @return DB + */ + public function delete($key){ + unset($this->db[$key]); + + $this->save(); + + return $this; + } + + /** + * GET SINGLE + * + * @param $key: string + * + * @return array + */ + public function getSingle($key){ + return $this->db[$key] ?? null; + } + + /** + * GET LIST + * + * @param $conditions: array (optional) + * @param $orderBy: array (optional) + * + * @return array + */ + public function getList($conditions = [], $orderBy = []){ + $result = []; + + if(empty($conditions)){ + $result = $this->db; + }else{ + foreach($this->db as $key => $value){ + $requirements = true; + + foreach($conditions as $k => $v){ + if($value[$k] !== $v){ + $requirements = false; + } + } + + if($requirements) $result[$key] = $value; + } + } + + if($orderBy['on'] !== '' && $orderBy['order'] !== ''){ + usort($result, function($first, $second) use($orderBy){ + if($orderBy['order'] === "ASC"){ + return strcmp($first[$orderBy['on']], $second[$orderBy['on']]) > 0; + }else{ + return strcmp($first[$orderBy['on']], $second[$orderBy['on']]) < 0; + } + }); + } + + return $result; + } + + /** + * CLEAR + * + * @return DB + */ + public function clear(){ + $this->db = "{}"; + + $this->save(); + + return $this; + } +} +?> diff --git a/php/upload.php b/php/upload.php index 57f4de2..bf8d5c9 100755 --- a/php/upload.php +++ b/php/upload.php @@ -1,52 +1,94 @@ $title, + "user" => $username, + "nome" => $username, + "description" => $desc, + "date" => $date, + "software" => $software, + "level" => $level + ); + + $db->insert($data); + + return 1; + } + + $username = $_SERVER['PHP_AUTH_USER']; $video_title = $_POST["videoTitle"]; $video_desc = $_POST["videoDescription"]; - $video_date = date('d/m/Y'); $video_level = $_POST["level"]; + $video_software = $_POST["software"]; + $video_date = date('d/m/Y'); - $brute_file = $target_dir . "/src/" . $video_title . $ext; + $orig_file = $_FILES["fileToUpload"]["tmp_name"]; + $target_dir = "/var/www/html/aliceclass_videos/" . $username; + $brute_file = $target_dir . "/src/" . $video_title . '.mkv'; $target_file = $target_dir . "/dest/" . $video_title . '.mp4'; - // Check if file already exists - if (file_exists($brute_file)) { - echo "Sorry, file already exists."; - $uploadOk = 0; - } + create_user_dir($target_dir) || exit; + check_file_extension($_FILES["fileToUpload"]["tmp_name"]) || exit; + !check_file_exists($brute_file) || exit; + upload($brute_file) || exit; + process_file($username, $video_title, $brute_file, $target_file) || exit; + insert_into_db($username, $video_title, $video_desc, $video_level, $video_software, $video_date); - if ($uploadOk == 1) { - if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $brute_file)) { - echo "Houve um problema com o upload do arquivo."; - return; - } - } - - // return; - - echo "The file ". htmlspecialchars(basename( $_FILES["videoTitle"]["name"])). " has been uploaded."; - $SCRIPT_PATH = "/var/www/src/AliceClass/script/script.sh"; - $AUTHOR = $username; - $NAME = $_POST["videoTitle"]; - $INPUT = $brute_file; - $OUTPUT = $target_file; - - $cmd = sprintf('%s "%s" "%s" "%s" "%s" "%s"', $SCRIPT_PATH, $NAME, $AUTHOR, $INPUT, 'aulateste', $OUTPUT); - $output = shell_exec($cmd); + echo "The file ". htmlspecialchars(basename($orig_file)). " has been uploaded."; ?>