From 3e47e7c2308813736cb9461d2d4a3164fbffc849 Mon Sep 17 00:00:00 2001 From: Jerome Jutteau Date: Wed, 5 Dec 2012 18:23:54 +0000 Subject: [PATCH] delete files instead of moving them to trash --- file.php | 54 ++++++++--------------------------------------- index.php | 6 +----- install.php | 2 +- lib/functions.php | 41 ++++++++++++++++++++++++++++++----- lib/settings.php | 1 - 5 files changed, 47 insertions(+), 57 deletions(-) diff --git a/file.php b/file.php index 31c16da..a8d42e4 100644 --- a/file.php +++ b/file.php @@ -47,40 +47,18 @@ if(isset($_GET['h']) && !empty($_GET['h'])) { $md5 = trim($content[5]); $onetime = trim($content[6]); - if(!file_exists(VAR_FILES . $md5)) { - if (file_exists(VAR_FILES . $md5 . '_count')) { - unlink(VAR_FILES . $md5 . '_count'); - } - unlink($link_file); - require(JIRAFEAU_ROOT . 'lib/template/header.php'); - echo '

' . _('File not available.') . '

'; - require(JIRAFEAU_ROOT . 'lib/template/footer.php'); - exit; - } + if(!file_exists(VAR_FILES . $md5)) { + jirafeau_delete($link_name); - $counter = 1; - if (file_exists(VAR_FILES . $md5 . '_count')) { - $content = file(VAR_FILES . $md5 . '_count'); - $counter = trim($content[0], NL); - } + require(JIRAFEAU_ROOT . 'lib/template/header.php'); + echo '

' . _('File not available.') . '

'; + require(JIRAFEAU_ROOT . 'lib/template/footer.php'); + exit; + } if($time != JIRAFEAU_INFINITY) { if(time() > $time) { - unlink($link_file); - - $counter--; - if ($counter >= 1) { - $handle = fopen(VAR_FILES . $md5 . '_count', 'w'); - fwrite($handle, $counter); - fclose($handle); - } - elseif ($counter == 0) { - if (file_exists(VAR_FILES . $md5 . '_count')) { - unlink(VAR_FILES . $md5 . '_count'); - } - $new_name = jirafeau_detect_collision($md5 . '_' . $file_name, VAR_TRASH); - rename(VAR_FILES . $md5, VAR_TRASH . $new_name); - } + jirafeau_delete($link_name); require(JIRAFEAU_ROOT . 'lib/template/header.php'); echo '

' . _('The time limit of this file has expired. It has been deleted.') . '

'; @@ -132,21 +110,7 @@ if(isset($_GET['h']) && !empty($_GET['h'])) { readfile(VAR_FILES . $md5); if($onetime == 'O') { - unlink($link_file); - - $counter--; - if ($counter >= 1) { - $handle = fopen(VAR_FILES . $md5 . '_count', 'w'); - fwrite($handle, $counter); - fclose($handle); - } - elseif ($counter == 0) { - if (file_exists(VAR_FILES . $md5 . '_count')) { - unlink(VAR_FILES . $md5 . '_count'); - } - $new_name = jirafeau_detect_collision($md5 . '_' . $file_name, VAR_TRASH); - rename(VAR_FILES . $md5, VAR_TRASH . $new_name); - } + jirafeau_delete($link_name); } exit; } else { diff --git a/index.php b/index.php index 7ee8ea2..9028143 100644 --- a/index.php +++ b/index.php @@ -26,7 +26,7 @@ require(JIRAFEAU_ROOT . 'lib/settings.php'); require(JIRAFEAU_ROOT . 'lib/functions.php'); /* check if the destination dirs are writable */ -$writable = is_writable(VAR_FILES) && is_writable(VAR_LINKS) && is_writable(VAR_TRASH); +$writable = is_writable(VAR_FILES) && is_writable(VAR_LINKS); $res = array(); if($writable && isset($_POST['jirafeau'])) { @@ -69,10 +69,6 @@ if(!is_writable(VAR_LINKS)) { add_error (_('The link directory is not writable!'), VAR_LINKS); } -if(!is_writable(VAR_TRASH)) { - add_error (_('The trash directory is not writable!'), VAR_TRASH); -} - /* Check if the install.php script is still in the directory. */ if (file_exists(JIRAFEAU_ROOT . 'install.php')) { add_error (_('Installer script still present'), diff --git a/install.php b/install.php index dfe094c..d313684 100644 --- a/install.php +++ b/install.php @@ -89,7 +89,7 @@ function jirafeau_check_var_dir($path) { ); } - foreach(array('files', 'links', 'trash') as $subdir) { + foreach(array('files', 'links') as $subdir) { $subpath = $path . $subdir; if(!jirafeau_mkdir($subpath)) { diff --git a/lib/functions.php b/lib/functions.php index 48bdefd..7a79840 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -104,6 +104,37 @@ function jirafeau_upload_errstr($code) { return _('Unknown error.'); } +/** Remove link and it's file + * @param $link the link's name (hash) + */ + +function jirafeau_delete($link) { + if(!file_exists(VAR_LINKS . $link)) + return; + + $content = file(VAR_LINKS . $link); + $md5 = trim($content[5]); + unlink(VAR_LINKS . $link); + + $counter = 1; + if (file_exists(VAR_FILES . $md5 . '_count')) { + $content = file(VAR_FILES . $md5 . '_count'); + $counter = trim($content[0]); + } + $counter--; + + if ($counter >= 1) { + $handle = fopen(VAR_FILES . $md5 . '_count', 'w'); + fwrite($handle, $counter); + fclose($handle); + } + + if ($counter == 0 && file_exists(VAR_FILES. $md5)) { + unlink (VAR_FILES . $md5); + unlink (VAR_FILES . $md5 . '_count'); + } +} + /** * handles an uploaded file * @param $file the file struct given by $_FILE[] @@ -166,16 +197,16 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $cfg, $ip) { fclose($handle); $md5_link = md5_file($link_tmp_name); if(!rename($link_tmp_name, VAR_LINKS . $md5_link)) { - if ($counter > 1) { - $counter--; + unlink($link_tmp_name); + $counter--; + if ($counter >= 1) { $handle = fopen(VAR_FILES . $md5 . '_count', 'w'); fwrite($handle, $counter); fclose($handle); } else { - unlink($link_tmp_name); - unlink(VAR_FILE . $md5 . '_count'); - unlink(VAR_FILE . $md5); + unlink(VAR_FILES . $md5 . '_count'); + unlink(VAR_FILES . $md5); } return(array( 'error' => array( diff --git a/lib/settings.php b/lib/settings.php index a54a3a7..353a8d6 100644 --- a/lib/settings.php +++ b/lib/settings.php @@ -26,7 +26,6 @@ define('JIRAFEAU_VERSION', '0.4'); define('VAR_FILES', $cfg['var_root'] . 'files/'); define('VAR_LINKS', $cfg['var_root'] . 'links/'); -define('VAR_TRASH', $cfg['var_root'] . 'trash/'); // i18n -- 2.34.1