X-Git-Url: https://git.p6c8.net/jirafeau_project.git/blobdiff_plain/6eca3aa915782488ab0d688d7397c42e3fdf30b4..f90fb205aba25e0f599e3140a1e6b3ebcb1972a2:/lib/functions.php
diff --git a/lib/functions.php b/lib/functions.php
index 4dcf5de..8a82b2a 100644
--- a/lib/functions.php
+++ b/lib/functions.php
@@ -101,6 +101,19 @@ function jirafeau_gen_random($l)
return $code;
}
+function jirafeau_gen_download_pass($length, $allowed_chars)
+{
+ if ($length <= 0) {
+ return false;
+ }
+ $pass="";
+ for ($i = 0; $i < $length; $i++) {
+ $pass .= $allowed_chars[rand(0, strlen($allowed_chars) - 1)];
+ }
+
+ return $pass;
+}
+
function is_ssl()
{
if (isset($_SERVER['HTTPS'])) {
@@ -130,8 +143,11 @@ function jirafeau_human_size($octets)
// Convert UTC timestamp to a datetime field
function jirafeau_get_datetimefield($timestamp)
{
- $content = ''
- . strftime('%Y-%m-%d %H:%M', $timestamp) . ' (GMT)';
+
+ $ts = date_create("@" . $timestamp);
+ $content = ''
+ . date_format($ts, 'Y-m-d H:i') . ' (GMT)';
+
return $content;
}
@@ -148,6 +164,9 @@ function jirafeau_clean_rm_link($link)
if (file_exists(VAR_LINKS . $p . $link)) {
unlink(VAR_LINKS . $p . $link);
}
+ if (file_exists(VAR_LINKS . $p . $link . '_download')) {
+ unlink(VAR_LINKS . $p . $link . '_download');
+ }
$parse = VAR_LINKS . $p;
$scan = array();
while (file_exists($parse)
@@ -190,20 +209,23 @@ function jirafeau_ini_to_bytes($value)
$modifier = substr($value, -1);
$bytes = substr($value, 0, -1);
switch (strtoupper($modifier)) {
- case 'P':
- $bytes *= 1024;
- // no break
- case 'T':
- $bytes *= 1024;
- // no break
- case 'G':
- $bytes *= 1024;
- // no break
- case 'M':
- $bytes *= 1024;
- // no break
- case 'K':
- $bytes *= 1024;
+ default:
+ return intval($value);
+ break;
+ case 'P':
+ $bytes *= 1024;
+ // no break
+ case 'T':
+ $bytes *= 1024;
+ // no break
+ case 'G':
+ $bytes *= 1024;
+ // no break
+ case 'M':
+ $bytes *= 1024;
+ // no break
+ case 'K':
+ $bytes *= 1024;
}
return $bytes;
}
@@ -235,13 +257,22 @@ function jirafeau_get_max_upload_size()
*/
function jirafeau_get_max_upload_chunk_size_bytes($max_upload_chunk_size_bytes = 0)
{
- if ($max_upload_chunk_size_bytes > 0) {
- return min(
- jirafeau_get_max_upload_size_bytes(),
- $max_upload_chunk_size_bytes
- );
+ if ($max_upload_chunk_size_bytes == 0) {
+ $size = jirafeau_get_max_upload_size_bytes();
+ // Jirafeau must choose an arbitrary number as PHP config does not give any limit nor $max_upload_chunk_size_bytes
+ if ($size == 0) {
+ return 10000000; // 10MB
+ }
+ return $size;
}
- return jirafeau_get_max_upload_size_bytes();
+ $size = min(
+ jirafeau_get_max_upload_size_bytes(),
+ $max_upload_chunk_size_bytes
+ );
+ if ($size == 0) {
+ return $max_upload_chunk_size_bytes;
+ }
+ return $size;
}
/**
@@ -252,19 +283,19 @@ function jirafeau_get_max_upload_chunk_size_bytes($max_upload_chunk_size_bytes =
function jirafeau_upload_errstr($code)
{
switch ($code) {
- case UPLOAD_ERR_INI_SIZE:
- case UPLOAD_ERR_FORM_SIZE:
- return t('Your file exceeds the maximum authorized file size. ');
+ case UPLOAD_ERR_INI_SIZE:
+ case UPLOAD_ERR_FORM_SIZE:
+ return t('Your file exceeds the maximum authorized file size. ');
- case UPLOAD_ERR_PARTIAL:
- case UPLOAD_ERR_NO_FILE:
- return
- t('Your file was not uploaded correctly. You may succeed in retrying. ');
+ case UPLOAD_ERR_PARTIAL:
+ case UPLOAD_ERR_NO_FILE:
+ return
+ t('Your file was not uploaded correctly. You may succeed in retrying. ');
- case UPLOAD_ERR_NO_TMP_DIR:
- case UPLOAD_ERR_CANT_WRITE:
- case UPLOAD_ERR_EXTENSION:
- return t('Internal error. You may not succeed in retrying. ');
+ case UPLOAD_ERR_NO_TMP_DIR:
+ case UPLOAD_ERR_CANT_WRITE:
+ case UPLOAD_ERR_EXTENSION:
+ return t('Internal error. You may not succeed in retrying. ');
}
return t('Unknown error. ');
}
@@ -416,23 +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('mcrypt') == true)) {
- error_log("PHP extension mcrypt not loaded, won't encrypt in Jirafeau");
+ 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('mcrypt') == true) {
- $crypt_key = jirafeau_encrypt_file($file['tmp_name'], $file['tmp_name']);
+ if ($crypt_module_enabled == true && extension_loaded('sodium') == true) {
+ $crypt_key = jirafeau_encrypt_file($file_path, $file_path.'crypt');
if (strlen($crypt_key) > 0) {
- $crypted = 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']));
@@ -445,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) {
@@ -516,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
@@ -579,13 +684,15 @@ function show_errors()
function check_errors($cfg)
{
- if (file_exists(JIRAFEAU_ROOT . 'install.php')
- && !($cfg['installation_done'] === true)) {
- header('Location: install.php');
- exit;
+ if (!($cfg['installation_done'] === true)) {
+ if (file_exists(JIRAFEAU_ROOT . 'install.php')) {
+ header('Location: install.php');
+ exit;
+ } else {
+ add_error(t('INSTALL_FILE_NOT_FOUND_TITLE'), t('INSTALL_FILE_NOT_FOUND_DESC'));
+ }
}
- /* Checking for errors. */
if (!is_writable(VAR_FILES)) {
add_error(t('FILE_DIR_W'), VAR_FILES);
}
@@ -631,103 +738,86 @@ function jirafeau_get_link($hash)
$out['upload_date'] = trim($c[7]);
$out['ip'] = trim($c[8]);
$out['link_code'] = trim($c[9]);
- $out['crypted'] = trim($c[10]) == 'C';
+ $out['crypted'] = trim($c[10]) == 'C2';
+ $out['crypted_legacy'] = trim($c[10]) == 'C';
return $out;
}
/**
- * 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);
}
/**
@@ -837,7 +927,7 @@ function jirafeau_fileperms($path)
function jirafeau_admin_bug_report($cfg)
{
$out = "