From: Jerome Jutteau Date: Wed, 5 Dec 2012 18:48:00 +0000 (+0000) Subject: add delete link added to each upload X-Git-Tag: 1.0~30 X-Git-Url: https://git.p6c8.net/jirafeau_mojo42.git/commitdiff_plain/8dc8b4bc884065eecec6ef0e544bf47734e69372 add delete link added to each upload --- diff --git a/file.php b/file.php index a8d42e4..7916fb8 100644 --- a/file.php +++ b/file.php @@ -27,6 +27,10 @@ require(JIRAFEAU_ROOT . 'lib/functions.php'); if(isset($_GET['h']) && !empty($_GET['h'])) { $link_name = $_GET['h']; + $delete_code = ''; + if(isset($_GET['d']) && !empty($_GET['d'])) + $delete_code = $_GET['d']; + if(!ereg('[0-9a-f]{32}$', $link_name)) { header("HTTP/1.0 404 Not Found"); @@ -46,28 +50,35 @@ if(isset($_GET['h']) && !empty($_GET['h'])) { $time = trim($content[4]); $md5 = trim($content[5]); $onetime = trim($content[6]); + $link_code = trim($content[9]); + + if(!file_exists(VAR_FILES . $md5)) { jirafeau_delete($link_name); - 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) { + if(!empty($delete_code) && $delete_code == $link_code) { jirafeau_delete($link_name); + require(JIRAFEAU_ROOT . 'lib/template/header.php'); + echo '

' . _('File has been deleted.') . '

'; + require(JIRAFEAU_ROOT . 'lib/template/footer.php'); + exit; + } + if($time != JIRAFEAU_INFINITY && time() > $time) { + jirafeau_delete($link_name); require(JIRAFEAU_ROOT . 'lib/template/header.php'); echo '

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

'; require(JIRAFEAU_ROOT . 'lib/template/footer.php'); exit; } - } - if(!empty($key)) { + if(!empty($key)) { if(!isset($_POST['key'])) { require(JIRAFEAU_ROOT . 'lib/template/header.php'); ?> diff --git a/index.php b/index.php index 9028143..9e1222c 100644 --- a/index.php +++ b/index.php @@ -80,11 +80,16 @@ if(!has_error() && !empty($res)) { add_error (_('An error occurred.'), $res['error']['why']); } else { $link = $cfg['web_root']; + $delete_link = $cfg['web_root']; + if($cfg['rewrite']) { $link .= 'file-' . $res['link']; + $delete_link .= 'file-' . $res['link'] . '-delete-' . $res['delete_link']; } else { $link .= 'file.php?h=' . $res['link']; // h because 'h' looks like a jirafeau ;) + $delete_link .= 'file.php?h=' . $res['link'] . '&d=' . $res['delete_link']; } + echo '
' . NL; echo '

' . _('File uploaded! Copy the following URL to get it:') . '
' . NL; echo '' . $link . '' . NL; @@ -94,6 +99,11 @@ if(!has_error() && !empty($res)) { } echo '

'; + + echo '
' . NL; + echo '

' . _('Keep the following URL to delete it:') . '
' . NL; + echo '' . $delete_link . '' . NL; + echo '

'; } } diff --git a/lib/functions.php b/lib/functions.php index 7a79840..b10207c 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -51,30 +51,6 @@ function jirafeau_get_max_upload_size() { return min(jirafeau_ini_to_bytes(ini_get('post_max_size')), jirafeau_ini_to_bytes(ini_get('upload_max_filesize'))); } -/** - * detects if a given filename is present in a directory and find an alternate filename - * @param $name the initial filename - * @param $dir the directory to explore (finishing with a '/') - * @returns an alternate filename, possibly the initial filename - */ -function jirafeau_detect_collision($name, $dir) { - if(!file_exists($dir . $name)) { - return $name; - } - - $dot = strpos($name, '.'); - $dot = ($dot === false) ? strlen($name) : $dot; - $first = substr($name, 0, $dot); - $second = substr($name, $dot); - $i = 1; - do { - $new_name = $first . '-' . $i . $second; - $i++; - } while(file_exists($dir . $new_name)); - - return $new_name; -} - /** * gets a string explaining the error * @param $code the error code @@ -146,10 +122,11 @@ function jirafeau_delete($link) { * @returns an array containing some information * 'error' => information on possible errors * 'link' => the link name of the uploaded file + * 'delete_link' => the link code to delete file */ function jirafeau_upload($file, $one_time_download, $key, $time, $cfg, $ip) { if(empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) { - return(array('error' => array('has_error' => true, 'why' => jirafeau_upload_errstr($file['error'])), 'link' => '')); + return(array('error' => array('has_error' => true, 'why' => jirafeau_upload_errstr($file['error'])), 'link' => '', 'delete_link' => '')); } /* array representing no error */ @@ -175,7 +152,8 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $cfg, $ip) { 'error' => array( 'has_error' => true, 'why' => _('Internal error during file creation.')), - 'link' => '') + 'link' => '', + 'delete_link' => '') ); } @@ -190,10 +168,15 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $cfg, $ip) { fwrite($handle, $counter); fclose($handle); + /* Create delete code. */ + $delete_link_code = 0; + for ($i = 0; $i < 8; $i++) + $delete_link_code .= dechex(rand(0,16)); + /* create link file */ $link_tmp_name = VAR_LINKS . $md5 . rand(0, 10000) . '.tmp'; $handle = fopen($link_tmp_name, 'w'); - fwrite($handle, $name . NL . $mime_type . NL . $size . NL . $key . NL . $time . NL . $md5 . NL . ($one_time_download ? 'O' : 'R') . NL . date('U') . NL . $ip . NL); + fwrite($handle, $name . NL . $mime_type . NL . $size . NL . $key . NL . $time . NL . $md5 . NL . ($one_time_download ? 'O' : 'R') . NL . date('U') . NL . $ip . NL . $delete_link_code . NL); fclose($handle); $md5_link = md5_file($link_tmp_name); if(!rename($link_tmp_name, VAR_LINKS . $md5_link)) { @@ -212,10 +195,11 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $cfg, $ip) { 'error' => array( 'has_error' => true, 'why' => _('Internal error during file creation.')), - 'link' => '') + 'link' => '', + 'delete_link' => '') ); } - return(array('error' => $noerr, 'link' => $md5_link)); + return(array('error' => $noerr, 'link' => $md5_link, 'delete_link' => $delete_link_code)); } /**