diff options
| author | Horus3 | 2014-04-13 18:48:34 +0200 |
|---|---|---|
| committer | Horus3 | 2014-04-13 18:48:34 +0200 |
| commit | ab03a281b0e9fc34a370b6a1809e9f8ab52236eb (patch) | |
| tree | 4b68acbbcbc19161b49a1e8a17ffa0655c654cb8 /www | |
| parent | 087455a4825429a4fde186c4e82badcd20573c7b (diff) | |
| download | files.iamfabulous.de-ab03a281b0e9fc34a370b6a1809e9f8ab52236eb.tar.gz | |
Patch: Old files are now deleted if you overwrite it with a new one.
Diffstat (limited to 'www')
| -rw-r--r-- | www/functions/func_delete.php | 18 | ||||
| -rwxr-xr-x | www/functions/func_upload.php | 82 |
2 files changed, 72 insertions, 28 deletions
diff --git a/www/functions/func_delete.php b/www/functions/func_delete.php index 059bc3b..6220f8a 100644 --- a/www/functions/func_delete.php +++ b/www/functions/func_delete.php @@ -4,6 +4,24 @@ expected state: tested?; but broken */ +function check_if_deletable($db, $id, $hash){ + $check_db = $db->query("SELECT hash FROM files WHERE id!=".$id." AND folder='FILE';"); + + $count = 0; + while($check_ar = $check_db->fetchArray(SQLITE3_NUM)){ + if($check_ar[0] == $hash){ + $count = $count +1; + break; + } + } + + if($count == 0){ + return true; + } else { + return false; + } +} + function delete_file($user, $path){ if(!$_SESSION["login"]){ diff --git a/www/functions/func_upload.php b/www/functions/func_upload.php index 1178d9c..eb49486 100755 --- a/www/functions/func_upload.php +++ b/www/functions/func_upload.php @@ -15,7 +15,7 @@ function database_upload($db, $parentdir, $owner, $filename, $folder, $mime, $si function database_upload_update($db, $id, $mime, $size, $share, $filehash){ if($db->exec(" BEGIN TRANSACTION; - UPDATE files SET mime='".$mime."', size='".$size."', share='".$share."', hash='".$filehash"' WHERE id=".$id."; + UPDATE files SET mime='".$mime."', size='".$size."', share='".$share."', hash='".$filehash."' WHERE id=".$id."; COMMIT; ")){ return true; @@ -89,40 +89,52 @@ function upload($path){ $hashtest_db = $db->query("SELECT hash FROM files WHERE hash='" . $filehash ."';"); $hashtest_ar = $hashtest_db->fetchArray(SQLITE3_NUM); + + /* new file was uploaded */ if(empty($hashtest_ar[0])){ - if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])){ + if(!move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])){ + return UPLOAD_MOVING; + } - if($exists){ - if(!database_upload_update($db, $fileid, $mime, $size, $share, $filehash)){ - return UPLOAD_DATABASE; - } - } else { - if(!database_upload($db, $parentdir, $owner, $filename, $folder, $mime, $size, $share, $filehash)){ - return UPLOAD_DATABASE; - } - } + /* checks if filename exists in the same directory */ + if($exists){ + $delete_file_db = $db->query("SELECT hash FROM files WHERE name='".$filename."' AND parent='".$parentdir."';"); + $delete_file_ar = $delete_file_db->fetchArray(SQLITE3_NUM); - $gzfile = $uploaddir . $filehash . ".gz"; - $fp = gzopen($gzfile, 'w9'); + if(check_if_deletable($db, $fileid, $delete_file_ar[0])){ - if(!gzwrite($fp, file_get_contents($uploaddir . $filename))){ - return UPLOAD_FILE_HANDLING; + if(!unlink($uploaddir.$delete_file_ar[0].".gz")){ + return UPLOAD_FILE_HANDLING; + } } - - if(!gzclose($fp)){ - return UPLOAD_FILE_HANDLING; + if(!database_upload_update($db, $fileid, $mime, $size, $share, $filehash)){ + return UPLOAD_DATABASE; } + } else { + if(!database_upload($db, $parentdir, $owner, $filename, $folder, $mime, $size, $share, $filehash)){ + return UPLOAD_DATABASE; + } + } - if(!unlink($uploaddir . $filename)){ - return UPLOAD_FILE_HANDLING; - } + $gzfile = $uploaddir . $filehash . ".gz"; + $fp = gzopen($gzfile, 'w9'); + + if(!gzwrite($fp, file_get_contents($uploaddir . $filename))){ + return UPLOAD_FILE_HANDLING; + } - return UPLOAD_SUCCESS; + if(!gzclose($fp)){ + return UPLOAD_FILE_HANDLING; + } - } else { - return UPLOAD_MOVING; + if(!unlink($uploaddir . $filename)){ + return UPLOAD_FILE_HANDLING; } + + return UPLOAD_SUCCESS; + + /* new file already exists, only thing is a new entry in the database */ } else { $dupl_db = $db->query("SELECT parent FROM files WHERE hash='" . $filehash . "';"); @@ -134,6 +146,14 @@ function upload($path){ } if($exists){ + $delete_file_db = $db->query("SELECT hash FROM files WHERE name='".$filename."' AND parent='".$parentdir."';"); + $delete_file_ar = $delete_file_db->fetchArray(SQLITE3_NUM); + + if(check_if_deletable($db, $fileid, $delete_file_ar[0])){ + if(!unlink($uploaddir.$delete_file_ar[0].".gz")){ + return UPLOAD_FILE_HANDLING; + } + } if(!database_upload_update($db, $fileid, $mime, $size, $share, $filehash)){ return UPLOAD_DATABASE; } @@ -155,10 +175,16 @@ function upload($path){ //function web_upload($db){ // $url = $_POST["url"]; -// if(preg_match("/^((https?|ftp)?://|www\.|ftp\.)?([-a-z0-9+&@#/%?=~_|!:,.;]+\.)+[a-z]{2}[a-z]*/i", $url)){ -// echo "hyperlink detected"; -// } else { +// if(!preg_match("/^((https?|ftp)?://|www\.|ftp\.)?([-a-z0-9+&@#/%?=~_|!:,.;]+\.)+[a-z]{2}[a-z]*/i", $url)){ // echo "no hyperlink"; +// return false; +// } +// if(!preg_match("/^[a-zA-Z]+://", $url){ +// $url = "http://".$url; +// } +// $file = file_get_contents($url); +// if(!$file){ +// echo "Couldn't download ".$url; +// return false; // } //} - |
