X-Git-Url: https://git.p6c8.net/jirafeau_project.git/blobdiff_plain/17431c71e3da077f11da9b10a9598bf7bd90918c..48c710744cbabd2ddc812fc50b6944328461747c:/lib/functions.php?ds=sidebyside diff --git a/lib/functions.php b/lib/functions.php index 2378d76..8a82b2a 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -447,25 +447,62 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l 'link' => '', 'delete_link' => '')); } + jirafeau_add_file($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length, $file_hash_method); +} - /* array representing no error */ - $noerr = array('has_error' => false, 'why' => ''); - +/** + * + * @param bool $crypt_module_enabled + * @param string $file_path + * @return array [bool, string] + */ +function jirafeau_handle_add_file_encryption($crypt_module_enabled, $file_path) { /* Crypt file if option is enabled. */ $crypted = false; $crypt_key = ''; - if ($crypt == true && !(extension_loaded('sodium') == true)) { + if ($crypt_module_enabled == true && !(extension_loaded('sodium') == true)) { error_log("PHP extension sodium not loaded, won't encrypt in Jirafeau"); } - if ($crypt == true && extension_loaded('sodium') == true) { - $crypt_key = jirafeau_encrypt_file($file['tmp_name'], $file['tmp_name'].'crypt'); + if ($crypt_module_enabled == true && extension_loaded('sodium') == true) { + $crypt_key = jirafeau_encrypt_file($file_path, $file_path.'crypt'); if (strlen($crypt_key) > 0) { - if (rename($file['tmp_name'].'crypt', $file['tmp_name']) === true) { + if (rename($file_path.'crypt', $file_path) === true) { $crypted = true; } } } + return [$crypted, $crypt_key]; +} + +/** + * adds an uploaded or copy/linked local file + * @param $file the file struct given by $_FILE[] + * @param $one_time_download is the file a one time download ? + * @param $key if not empty, protect the file with this key + * @param $time the time of validity of the file + * @param $ip uploader's ip + * @param $crypt boolean asking to crypt or not + * @param $link_name_length size of the link name + * @param $is_upload, determines if the file is uploaded or local - it controls which file-functions are used + * @return array 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_add_file($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length, $file_hash_method, $is_upload = true) +{ + // TODO needs to be adapted + $move_operation = $is_upload ? 'move_uploaded_file' : 'symlink'; + + /* array representing no error */ + $noerr = array('has_error' => false, 'why' => ''); + + $crypted = false; + $crypt_key = ''; + list($crypted, $crypt_key) = jirafeau_handle_add_file_encryption($crypt, $file['tmp_name']); + + /* file information */ $hash = jirafeau_hash_file($file_hash_method, $file['tmp_name']); $name = str_replace(NL, '', trim($file['name'])); @@ -478,7 +515,11 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l if (file_exists(VAR_FILES . $p . $hash)) { $rc = unlink($file['tmp_name']); } elseif ((file_exists(VAR_FILES . $p) || @mkdir(VAR_FILES . $p, 0755, true)) - && move_uploaded_file($file['tmp_name'], VAR_FILES . $p . $hash)) { + && + //move_uploaded_file($file['tmp_name'], VAR_FILES . $p . $hash)) + $move_operation($file['tmp_name'], VAR_FILES . $p . $hash)) + { + $rc = true; } if (!$rc) { @@ -517,7 +558,7 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l $handle, $name . NL. $mime_type . NL. $size . NL. $password . NL. $time . NL . $hash. NL . ($one_time_download ? 'O' : 'R') . NL . time() . - NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C2' : 'O') + NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C' : 'O') ); fclose($handle); $hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length); @@ -549,6 +590,37 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l 'crypt_key' => $crypt_key); } + +function jirafeau_admin_list_table ($name, $file_hash, $link_hash, $visitor_function = null) { + echo '
'; +} + + + + + /** * Tells if a mime-type is viewable in a browser * @param $mime the mime type @@ -673,103 +745,79 @@ function jirafeau_get_link($hash) } /** - * List files in admin interface. + * List files ii folder in admin interface. */ function jirafeau_admin_list($name, $file_hash, $link_hash) { - echo ''; + }; + jirafeau_admin_list_table($name, $file_hash, $link_hash, $function); } /** @@ -1010,8 +1058,8 @@ function jirafeau_async_delete($ref) function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip) { /* Create temporary folder. */ - $ref; - $p; + $ref = ''; + $p = ''; $code = jirafeau_gen_random(4); do { $ref = jirafeau_gen_random(32); @@ -1843,3 +1891,106 @@ function jirafeau_get_expiration_time_options() { echo ''; } + function jirafeau_datestr_to_int ($time_str) { + $time = time(); + switch ($time_str) { + case 'minute': + $time += JIRAFEAU_MINUTE; + break; + case 'hour': + $time += JIRAFEAU_HOUR; + break; + case 'day': + $time += JIRAFEAU_DAY; + break; + case 'week': + $time += JIRAFEAU_WEEK; + break; + case 'fortnight': + $time += JIRAFEAU_FORTNIGHT; + break; + case 'month': + $time += JIRAFEAU_MONTH; + break; + case 'quarter': + $time += JIRAFEAU_QUARTER; + break; + case 'year': + $time += JIRAFEAU_YEAR; + break; + default: + $time = JIRAFEAU_INFINITY; + break; + } + return $time; +} + + + + +/** + * links or copy a local file + * TODO: boolean in config for linking + * @param string $filepath + * @param $one_time_download is the file a one time download ? + * @param $key if not empty, protect the file with this key + * @param $time the time of validity of the file + * @param $ip uploader's ip + * @param $crypt boolean asking to crypt or not + * @param $link_name_length size of the link name + * @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_copy_local_file($local_file_path, $one_time_download, $key, $time, $ip, $crypt, $link_name_length, $file_hash_method) { + + if (!file_exists($local_file_path)) { + return (array( + 'error' => + array('has_error' => true, + 'why' => t('INTERNAL_ERROR_FILE_NOT_EXIST')), + 'link' =>'', + 'delete_link' => '')); + } + if( + // sanity check if file can be opened + $file = fopen($local_file_path, "r") + ) + { + // close file pointer - it's not needed here + fclose($file); + $time_in_int = jirafeau_datestr_to_int($time); + return jirafeau_add_file( + jirafeau_create_file_array($local_file_path), + $one_time_download, + $key, + $time_in_int, + $ip, + $crypt, + $link_name_length, + $file_hash_method, + false + ); + } + else { + return (array( + 'error' => + array('has_error' => true, + 'why' => t('INTERNAL_ERROR_FP_OPEN_LOCAL')), + 'link' =>'', + 'delete_link' => '')); + } + +} + + +function jirafeau_create_file_array($file_path) { + return + [ + 'type' => mime_content_type($file_path), + 'tmp_name' => $file_path, + 'name' => basename($file_path), + 'size' => filesize($file_path), + ]; +}