From f2bf4d23d5be9b4d24a830ddaecabe2e87a0e93c Mon Sep 17 00:00:00 2001 From: adam Date: Fri, 12 Feb 2021 10:46:45 +0200 Subject: introducing directories --- php/configuration.php | 3 -- php/database.php | 92 +++++++++++++++++++++++++++++++++++++++++++++++++-- php/user.php | 2 +- sql/fileshare.sql | 58 ++++++++++++++++++-------------- 4 files changed, 124 insertions(+), 31 deletions(-) diff --git a/php/configuration.php b/php/configuration.php index 425dd10..5510131 100644 --- a/php/configuration.php +++ b/php/configuration.php @@ -3,14 +3,11 @@ $domain_name="localhost"; -<<<<<<< HEAD -======= if (file_exists("/home/alex")) { $database_name="alex"; $database_username="alex"; $database_password="lol"; $database_location="127.0.0.1"; ->>>>>>> f1cd0085cd8dba8b25818fc998d315b28e6c13a5 $storage_root = "/home/alex/fileup_storage"; } diff --git a/php/database.php b/php/database.php index f472cbf..a5a8c20 100644 --- a/php/database.php +++ b/php/database.php @@ -37,6 +37,7 @@ require_once "misc.php"; $ret->user_id=$hold["user_id"]; $ret->username=$hold["username"]; $ret->email_address=$hold["email"]; + $ret->current_directory=$hold["home_directory"]; return $ret; }else { @@ -71,6 +72,86 @@ require_once "misc.php"; return false; } } + function get_node_id($name,$directory_id) + { + $hold=NULL; + $statement=NULL; + $ret=[]; + if($name != NULL) + { + if($directory_id!=NULL) + { + $statement=$this->pdo->prepare( + "select nl.node_id as id from node_links nl + inner join nodes n on n.node_id=nl.node_id + where name=:name and directory_id=:directory_id)"); + $statement->bindParam(':name',$name); + $statement->bindParam(':directory_id',$directory_id); + }else + { + /*get all node_ids with the name name*/ + $statement=$this->pdo->prepare("select node_id as id from nodes where name=:name"); + $statement->bindParam(':name',$name); + } + if($statement==NULL) + { + error_log("statement is null"); + exit(1); + } + }else { + $statement=$this->pdo->prepare("select node_id as id from node_links where directory_id=:dir_id"); + $statement->bindParam(':dir_id',$directory_id); + } + if($statement->execute()==false) + { + error_log("there is an error in the sql statement in get_node_id"); + exit(1); + } + + while($hold=$statement->fetch(PDO::FETCH_ASSOC)) + { + print_r($hold); + array_push($ret,$hold["id"]); + } + return $ret; + + } + function get_random_node_name(string $prefix) + { + do{ + $proposal=uniqid($prefix,true); + }while($this->get_node_id($proposal,NULL)!=NULL); + return $proposal; + } + /*returns NULL if node doesn't exist*/ + /*if name is NULL return all node ids in the directory*/ + /*if directory is NULL return all node ids with the name name*/ + /*if both are null return NULL*/ + /*returns node id*/ + function create_dangling_directory(): int + { + $dir_name=$this->get_random_node_name(""); + global $storage_root; + + $prep=$this->pdo->prepare("insert into nodes(is_directory,relative_path,name) values(true,:root,:name)"); + $prep->bindParam(':name',$dir_name); + $prep->bindParam(':root',$storage_root); + if($prep->execute()==false) + { + error_log("tried to create a dangling directory but sql statement failed. Fatal error!"); + exit(1); + } + + $id=$this->get_node_id($dir_name,NULL); + if(count($id)!=1) + { + error_log("created a dangling directory but couldn't find it afterward. Fatal error!"); + exit(1); + } + + //print count($id); + return $id[0]; + } /*returns false if username is taken, email is not checked here*/ function register_user(string $user,string $password,string $email) : bool { @@ -91,11 +172,18 @@ require_once "misc.php"; }else { $hashed_pass=password_hash($password,$password_hash_algo); - $prep=$this->pdo->prepare("insert into users(username,password,email) values(:username,:password,:email)"); + $home_dir=$this->create_dangling_directory(); + $prep=$this->pdo->prepare("insert into users(username,password,email,home_directory) values(:username,:password,:email,:dir)"); $prep->bindParam(':username',$user); $prep->bindParam(':password',$hashed_pass); $prep->bindParam(':email',$email); - $prep->execute(); + $prep->bindParam(':dir',$home_dir); + if($prep->execute()==false) + { + error_log("can't create user because there was an error in the sql statement"); + /*todo make an error page*/ + exit(1); + } } return true; } diff --git a/php/user.php b/php/user.php index 1ef3083..3cfcc89 100644 --- a/php/user.php +++ b/php/user.php @@ -1,10 +1,10 @@ diff --git a/sql/fileshare.sql b/sql/fileshare.sql index dfb9010..5740a48 100644 --- a/sql/fileshare.sql +++ b/sql/fileshare.sql @@ -1,32 +1,40 @@ +create table nodes ( + node_id int not null auto_increment, + is_directory boolean default false, + relative_path varchar(500) not null, + type varchar(20) not null default 'data', + name varchar(100) not null default 'no name', + primary key (node_id) +); + /*base user information*/ create table users ( - user_id int not null auto_increment, - username varchar(50) not null unique, - password varchar(255) not null, - email varchar(50), - primary key (user_id) + user_id int not null auto_increment, + username varchar(50) not null unique, + password varchar(255) not null, + email varchar(50), + home_directory int not null, + primary key (user_id), + foreign key (home_directory) references nodes(node_id) ); -/*table has only one owner and is identifyed by a number*/ -create table files ( - file_id int not null auto_increment, - owner_id int default null, - relative_path varchar(500) not null, - type varchar(20) not null default 'data', - primary key (file_id), - foreign key (owner_id) references users(user_id) +create table node_access ( + node_id int not null, + user_id int not null, + + can_view boolean not null default true, + can_edit boolean not null default false, + check (can_view=true or can_edit=true) , + foreign key (node_id) references nodes(node_id), + foreign key (user_id) references users(user_id) ); -/*the user with userid is given some kind of access to the file with fileid*/ -/*there is no edit bit because it will be too dificult to implement prehaps a change bit is in order (but not an edit bit)*/ -/*might be beneficial to even go full minimalist and remove the remove bit and only have the view bit*/ -create table access ( - file_id int not null, - user_id int not null, - - can_view boolean not null default true, - can_remove boolean not null default false, - check (can_view=true or can_remove=true) , - foreign key (file_id) references files(file_id), - foreign key (user_id) references users(user_id) +create table node_links ( + directory_id int not null, + node_id int not null, + check (directory_id != node_id), + foreign key (directory_id) references nodes(node_id), + foreign key (node_id) references nodes(node_id) ); + + -- cgit v1.2.3