X-Git-Url: https://git.p6c8.net/jirafeau.git/blobdiff_plain/ba6f06965a4d9846e23d99a0e9f74b1361c18c40..ab330a749db8c75b9a38a77536358be60a3e5163:/lib/functions.php diff --git a/lib/functions.php b/lib/functions.php index cb2226a..d4c1b20 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 @@ -104,6 +80,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[] @@ -111,13 +118,15 @@ function jirafeau_upload_errstr($code) { * @param $key if not empty, protect the file with this key * @param $time the time of validity of the file * @param $cfg the current configuration + * @param $ip uploader's ip * @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) { +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 */ @@ -143,7 +152,8 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $cfg) { 'error' => array( 'has_error' => true, 'why' => _('Internal error during file creation.')), - 'link' => '') + 'link' => '', + 'delete_link' => '') ); } @@ -158,32 +168,43 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $cfg) { 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)); + + /* md5 password or empty */ + $password = ''; + if (!empty($key)) + $password = md5($key); + /* 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); + fwrite($handle, $name . NL . $mime_type . NL . $size . NL . $password . 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)) { - 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( '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)); } /**