aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Vitkov <alexvitkov98@gmail.com>2021-02-13 14:10:46 +0200
committerAlex Vitkov <alexvitkov98@gmail.com>2021-02-13 14:10:46 +0200
commit820a552e7d69cdee9c040a4b683014f13b7ed6b2 (patch)
treef6c18c65b2cd5a5558f9309a40b8f420085a8f7b
parent0d02fe02d61bfc4db1418c9bfad2206bbfc2f3c5 (diff)
parent8b1393b3f06fdd86abc3dca396670965c42ba8c2 (diff)
downloadfileup-820a552e7d69cdee9c040a4b683014f13b7ed6b2.tar.gz
Merge branch 'master' of https://github.com/alexvitkov/india
-rw-r--r--php/configuration.php3
-rw-r--r--php/database.php78
-rw-r--r--php/delete.php6
-rw-r--r--php/node.php19
-rw-r--r--sql/fileshare.sql10
5 files changed, 101 insertions, 15 deletions
diff --git a/php/configuration.php b/php/configuration.php
index 313f0ad..09d1a06 100644
--- a/php/configuration.php
+++ b/php/configuration.php
@@ -21,7 +21,8 @@ $database_location="localhost";
$storage_root = "/srv/apache/testing/project/files/";
}
-
+/*if we save deleted files just in case of an error*/
+$has_trash=true;
$password_hash_algo=PASSWORD_BCRYPT;
$has_email_verification=false;
diff --git a/php/database.php b/php/database.php
index cb2697f..668490a 100644
--- a/php/database.php
+++ b/php/database.php
@@ -262,6 +262,72 @@ require_once "node.php";
}
}
+ /*returns NULL if directory or error*/
+ function get_code_of_node(int $node_id)
+ {
+ global $storage_root;
+
+ $prep=$this->pdo->prepare("select code
+ from nodes
+ where node_id=:id
+ ");
+ $prep->bindParam(':id',$node_id);
+ if($prep->execute()==false)
+ {
+ error_log("could not execute sql statement in get_file_location_of_node");
+ return NULL;
+ }
+ $hold=$prep->fetch(PDO::FETCH_ASSOC);
+ if(count($hold)!=1)
+ {
+ return NULL;
+ }else
+ {
+ /*BEWARE*/
+ return $hold["code"];
+ }
+ }
+ /*
+ we remove the node and
+ 1. move the file represented by the node to the trash folder
+ 2. remove the file
+ depends on the conf file
+ */
+ function delete_node_by_id(int $node_id)
+ {
+ global $has_trash;
+ global $storage_root;
+
+ $location=get_file_location_of_node($node_id);
+
+ /*actually delete the file*/
+ if($has_trash)
+ {
+ /*BEWARE*/
+ if(!copy($storage_root."/".$location,$storage_root."/trash/".$location))
+ {
+ error_log("could not copy file aborting node deletion in delete_node_by_id");
+ return;
+ }
+ }
+ unlink($storage_root,"/".$location);
+
+ if($location==NULL)
+ {
+ error_log("trying to delete a node that does not exist in delete_node_by_id!");
+ return;
+ }
+ $prep=$this->pdo->prepare("delete
+ from nodes
+ where node_id=:id
+ ");
+ $prep->bindParam(':id',$node_id);
+ if($prep->execute()==false)
+ {
+ error_log("sql statement in delete_node_by_id could not execute");
+ return NULL;
+ }
+ }
/*this is used to create seperate roots for the users*/
function create_dangling_directory(): int
@@ -291,6 +357,7 @@ require_once "node.php";
return $id["id"];
}
+
/*links source to target*/
function link_nodes(int $target_id,int $source_id,string $name,string $note)
{
@@ -307,6 +374,15 @@ require_once "node.php";
error_log("there was an error with the statement ni link_nodes");
}
}
+
+ function create_home_directory():int
+ {
+ $ret=$this->create_dangling_directory();
+ $trash_folder_id=$this->create_dangling_directory();
+ $this->link_nodes($ret,$trash_folder_id,"trash","trash folder");
+ return $ret;
+ }
+
function check_if_name_is_taken(string $filename,int $dir_id):bool
{
if($this->get_node_id($filename,$dir_id)!=NULL)
@@ -434,7 +510,7 @@ require_once "node.php";
}else
{
$hashed_pass=password_hash($password,$password_hash_algo);
- $home_dir=$this->create_dangling_directory();
+ $home_dir=$this->create_home_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);
diff --git a/php/delete.php b/php/delete.php
new file mode 100644
index 0000000..b50dbad
--- /dev/null
+++ b/php/delete.php
@@ -0,0 +1,6 @@
+<?php
+
+
+
+
+?>
diff --git a/php/node.php b/php/node.php
index d7c2a6c..5074082 100644
--- a/php/node.php
+++ b/php/node.php
@@ -1,25 +1,27 @@
<?php
require_once "database.php";
require_once "user.php";
-
/*path is in terms of the simulated filesystem*/
function get_directory(string $abstract_path,User $user)
{
+
global $database;
- if($abstract_path[0] != "/") {
+ if($abstract_path[0] != "/")
+ {
return NULL;
}
- $component = strtok($abstract_path,"/");
- $current_dir = $user->home_directory;
+ $component = strtok($abstract_path,"/");
+ $current_dir = $user->home_directory;
- while ($component) {
+ while($component)
+ {
$current_dir = $database->get_node_id($component, $current_dir);
- $component = strtok("/");
- };
+ $component = strtok("/");
+ }
- return $current_dir;
+ return $current_dir;
}
/*returns an assoc arrat of Node-s*/
@@ -39,6 +41,7 @@ require_once "user.php";
global $database;
$parent_dir_id=get_directory($abstract_path,$user);
+
if($database->check_if_name_is_taken($directory_name,$parent_dir_id))
{
return NULL;
diff --git a/sql/fileshare.sql b/sql/fileshare.sql
index 010b35d..8e69881 100644
--- a/sql/fileshare.sql
+++ b/sql/fileshare.sql
@@ -24,7 +24,7 @@ create table users (
email varchar(50),
home_directory int not null,
primary key (user_id),
- foreign key (home_directory) references nodes(node_id)
+ foreign key (home_directory) references nodes(node_id) on delete cascade
);
create table node_access (
@@ -33,8 +33,8 @@ create table node_access (
can_view boolean not null default true,
can_edit boolean not null default false,
- foreign key (node_id) references nodes(node_id),
- foreign key (user_id) references users(user_id)
+ foreign key (node_id) references nodes(node_id) on delete cascade,
+ foreign key (user_id) references users(user_id) on delete cascade
);
/*we can name a node in many different ways */
create table node_links (
@@ -43,8 +43,8 @@ create table node_links (
name varchar(100) not null default 'no name',
note varchar(200) not null default "",
check (directory_id != node_id),
- foreign key (directory_id) references nodes(node_id),
- foreign key (node_id) references nodes(node_id)
+ foreign key (directory_id) references nodes(node_id) on delete cascade,
+ foreign key (node_id) references nodes(node_id) on delete cascade
);