]> git.p6c8.net - jirafeau/jirafeau.git/blobdiff - lib/functions.php
Jirafeau 4.8.0 is ready
[jirafeau/jirafeau.git] / lib / functions.php
index f9fbd9b72a92d79fa24e883db6f470bd30cb998a..e83108747ce19e6459ded436645a6c1fff1e0a4e 100644 (file)
@@ -229,6 +229,58 @@ function jirafeau_ini_to_bytes($value)
     return $bytes;
 }
 
+/**
+ * returns the configured disallowed extensions, lower-cased and without
+ * a leading dot
+ * @param $cfg jirafeau config
+ * @returns array of normalized extensions
+ */
+function jirafeau_disallowed_extensions($cfg)
+{
+    if (empty($cfg['disallowed_extensions'])) {
+        return array();
+    }
+
+    // tolerate a leading dot in the config, e.g. '.jpg' instead of 'jpg'
+    $extensions = array();
+    foreach ($cfg['disallowed_extensions'] as $extension) {
+        $extension = ltrim(strtolower(trim($extension)), '.');
+        if ($extension !== '') {
+            $extensions[] = $extension;
+        }
+    }
+
+    return $extensions;
+}
+
+/**
+ * checks if a file name's extension is allowed to be uploaded
+ * @param $cfg jirafeau config
+ * @param $filename the name of the file being uploaded
+ * @returns true if the extension is allowed
+ */
+function jirafeau_is_extension_allowed($cfg, $filename)
+{
+    $extensions = jirafeau_disallowed_extensions($cfg);
+    if (empty($extensions)) {
+        return true;
+    }
+
+    // trailing dots and whitespace are dropped on storage or by the client OS
+    $filename = strtolower(rtrim($filename, " \t\n\r\0\v."));
+
+    // suffix match so that compound extensions such as 'tar.gz' work too;
+    // 'gz' still covers 'archive.tar.gz'
+    foreach ($extensions as $extension) {
+        $suffix = '.' . $extension;
+        if (substr($filename, -strlen($suffix)) === $suffix) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
 /**
  * gets the maximum upload size according to php.ini
  * @returns the maximum upload size in bytes
@@ -464,9 +516,9 @@ function jirafeau_handle_add_file_encryption($crypt_module_enabled, $file_path)
         error_log("PHP extension sodium not loaded, won't encrypt in Jirafeau");
     }
     if ($crypt_module_enabled == true && extension_loaded('sodium') == true) {
-        $crypt_key = jirafeau_encrypt_file($file_path, $file_path.'crypt');
+        $crypt_key = jirafeau_encrypt_file($file_path, $file_path.'.crypt');
         if (strlen($crypt_key) > 0) {
-            if (rename($file_path.'crypt', $file_path) === true) {
+            if (rename($file_path.'.crypt', $file_path) === true) {
                 $crypted = true;
             }
         }
@@ -544,7 +596,7 @@ function jirafeau_add_file($file, $one_time_download, $key, $time, $ip, $crypt,
     /* hash password or empty. */
     $password = '';
     if (!empty($key)) {
-        $password = md5($key);
+        $password = '[SHA256]' . hash('sha256', $key);
     }
 
     /* create link file */
@@ -554,7 +606,7 @@ function jirafeau_add_file($file, $one_time_download, $key, $time, $ip, $crypt,
         $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 ? 'C' : 'O')
+            NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C2' : 'O')
     );
     fclose($handle);
     $hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length);
@@ -771,6 +823,9 @@ function jirafeau_admin_list($name, $file_hash, $link_hash)
                     if (!empty($link_hash) && $link_hash != $node) {
                         continue;
                     }
+
+                    /* Get download statistics */
+                    $ld = jirafeau_get_download_stats($node);
                     /* Print link information. */
                     echo '<tr>';
                     echo '<td><strong>';
@@ -793,6 +848,11 @@ function jirafeau_admin_list($name, $file_hash, $link_hash)
                     if (strlen($l['ip']) > 0) {
                         echo t('ORIGIN') . ': ' . $l['ip'] . '<br/>';
                     }
+                    echo t('DOWNLOAD_COUNT') . ': ' . $ld['count'] . '<br/>';
+                    if ($ld['count'] > 0) {
+                        echo t('DOWNLOAD_DATE') . ': ' . jirafeau_get_datetimefield($ld['date']) . '<br/>';
+                        echo t('DOWNLOAD_IP') . ': ' . $ld['ip'] . '<br/>';
+                    }
                     echo '</td><td>';
 
                     if (!$l['crypted'] && !$l['crypted_legacy']) {
@@ -854,7 +914,8 @@ function jirafeau_admin_clean()
                     continue;
                 }
                 $p = s2p($l['hash']);
-                if ($l['time'] > 0 && $l['time'] < time() || // expired
+                if (empty($l['time']) || // invalid
+                    $l['time'] > 0 && $l['time'] < time() || // expired
                     !file_exists(VAR_FILES . $p . $l['hash']) || // invalid
                     !file_exists(VAR_FILES . $p . $l['hash'] . '_count')) { // invalid
                     jirafeau_delete_link($node);
@@ -939,6 +1000,7 @@ function jirafeau_admin_bug_report($cfg)
     $out .= "- version: " . JIRAFEAU_VERSION . "<br/>";
     $jirafeau_options = [
         'debug',
+        'debug_enforce_classic_upload',
         'file_hash',
         'litespeed_workaround',
         'store_uploader_ip',
@@ -986,6 +1048,7 @@ function jirafeau_admin_bug_report($cfg)
 
     $out .= "# OS details<br/>";
     $out .= "- OS: " . php_uname() . "<br/>";
+    $out .= "- Docker: " . (is_file('/.dockerenv') ? 'yes' : 'no') . "<br/>";
     $out .= "<br/>";
 
     $out .= "# Browser details<br/>";
@@ -1013,11 +1076,20 @@ function jirafeau_get_async_ref($ref)
     $out = array();
     $refinfos = VAR_ASYNC . s2p("$ref") . "$ref";
 
-    if (!file_exists($refinfos)) {
+    if (!is_file($refinfos)) {
+        return $out;
+    }
+
+    if (filesize($refinfos) == 0) {
         return $out;
     }
 
     $c = file($refinfos);
+
+    if(empty($c)) {
+        return $out;
+    }
+
     $out['file_name'] = trim($c[0]);
     $out['mime_type'] = trim($c[1]);
     $out['key'] = trim($c[2], NL);
@@ -1066,9 +1138,9 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip)
     /* Create temporary folder. */
     $ref = '';
     $p = '';
-    $code = jirafeau_gen_random(4);
+    $code = jirafeau_gen_random(JIRAFEAU_ASYNC_CODE_LENGTH);
     do {
-        $ref = jirafeau_gen_random(32);
+        $ref = jirafeau_gen_random(JIRAFEAU_ASYNC_REF_LENGTH);
         $p = VAR_ASYNC . s2p($ref);
     } while (file_exists($p));
     @mkdir($p, 0755, true);
@@ -1080,10 +1152,10 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip)
     $w_path = $p . $ref . '_data';
     touch($w_path);
 
-    /* md5 password or empty */
+    /* sha256 password or empty */
     $password = '';
     if (!empty($key)) {
-        $password = md5($key);
+        $password = '[SHA256]' . hash('sha256', $key);
     }
 
     /* Store information. */
@@ -1111,15 +1183,29 @@ function jirafeau_async_init($filename, $type, $one_time, $key, $time, $ip)
   */
 function jirafeau_async_push($ref, $data, $code, $max_file_size)
 {
+    $ref = trim($ref);
+    $code = trim($code);
+
+    /* Validate ref string. */
+    if (!ctype_xdigit($ref) || strlen($ref) != JIRAFEAU_ASYNC_REF_LENGTH) {
+        return 'Error: Malformed ref string';
+    }
+
+    /* Validate code string. */
+    if (!ctype_xdigit($code) || strlen($code) != JIRAFEAU_ASYNC_CODE_LENGTH) {
+        return 'Error: Malformed code string';
+    }
+
     /* Get async infos. */
     $a = jirafeau_get_async_ref($ref);
 
     /* Check some errors. */
     if (count($a) == 0) {
-        return "Error: cannot find transfer";
+        return 'Error: cannot find transfer';
     }
-    if ($a['next_code'] != "$code") {
-        return "Error: bad transfer code";
+
+    if (!hash_equals($a['next_code'], "$code")) {
+        return 'Error: bad transfer code';
     }
     if ($data['error'] != UPLOAD_ERR_OK) {
         // Check error code in https://www.php.net/manual/en/features.file-upload.errors.php
@@ -1127,10 +1213,10 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size)
         return "Error: upload error: {$data_details}";
     }
     if (empty($data['tmp_name'])) {
-        return "Error: missing tmp_name";
+        return 'Error: missing tmp_name';
     }
     if (!is_uploaded_file($data['tmp_name'])) {
-        return "Error: tmp_name may not be uploaded";
+        return 'Error: tmp_name may not be uploaded';
     }
 
     $p = s2p($ref);
@@ -1143,7 +1229,7 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size)
     if ($max_file_size > 0 &&
         filesize($r_path) + filesize($w_path) > $max_file_size * 1024 * 1024) {
         jirafeau_async_delete($ref);
-        return "Error: file size is above upload limit";
+        return 'Error: file size is above upload limit';
     }
 
     /* Concatenate data. */
@@ -1154,7 +1240,7 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size)
             fclose($r);
             fclose($w);
             jirafeau_async_delete($ref);
-            return "Error: cannot write file";
+            return 'Error: cannot write file';
         }
     }
     fclose($r);
@@ -1184,17 +1270,34 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size)
   */
 function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_method)
 {
+    $ref = trim($ref);
+    $code = trim($code);
+
+    /* Validate ref string. */
+    if (!ctype_xdigit($ref) || strlen($ref) != JIRAFEAU_ASYNC_REF_LENGTH) {
+        return 'Error: Malformed ref string';
+    }
+
+    /* Validate code string. */
+    if (!ctype_xdigit($code) || strlen($code) != JIRAFEAU_ASYNC_CODE_LENGTH) {
+        return 'Error: Malformed code string';
+    }
+
     /* Get async infos. */
     $a = jirafeau_get_async_ref($ref);
-    if (count($a) == 0
-        || $a['next_code'] != "$code") {
-        return "Error: bad code for ending transfer";
+
+    /* Check some errors. */
+    if (count($a) == 0) {
+        return 'Error: cannot find transfer';
     }
 
+    if (!hash_equals($a['next_code'], "$code")) {
+        return 'Error: bad code for ending transfer';
+    }
     /* Generate link infos. */
-    $p = VAR_ASYNC . s2p($ref) . $ref . "_data";
+    $p = VAR_ASYNC . s2p($ref) . $ref . '_data';
     if (!file_exists($p)) {
-        return "Error: referenced file does not exist";
+        return 'Error: referenced file does not exist';
     }
 
     /* Store filesize before encrypting the file */
@@ -1249,10 +1352,10 @@ function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_m
     $hash_link = substr(base_16_to_64(md5_file($link_tmp_name)), 0, $link_name_length);
     $l = s2p("$hash_link");
     if (!@mkdir(VAR_LINKS . $l, 0755, true)) {
-        return "Error: cannot create folder in LINKS";
+        return 'Error: cannot create folder in LINKS';
     }
     if (!rename($link_tmp_name, VAR_LINKS . $l . $hash_link)) {
-        return "Error: cannot rename file in LINKS";
+        return 'Error: cannot rename file in LINKS';
     }
 
     /* Clean async upload. */
@@ -1634,7 +1737,7 @@ function jirafeau_escape($string)
 function jirafeau_admin_session_start()
 {
     $_SESSION['admin_auth'] = true;
-    $_SESSION['admin_csrf'] = md5(uniqid(mt_rand(), true));
+    $_SESSION['admin_csrf'] = hash('sha256', uniqid(mt_rand(), true));
 }
 
 function jirafeau_session_end()
@@ -1779,7 +1882,7 @@ function jirafeau_write_download_stats($hash, $ip)
     fclose($handle);
 }
 
-function jirafeau_create_upload_finished_box($preview = true)
+function jirafeau_create_upload_finished_box($preview = true, $download_pass = null)
 {
     ?>
 
@@ -1796,6 +1899,21 @@ function jirafeau_create_upload_finished_box($preview = true)
     </p>
     </div>
 
+    <?php if (!is_null($download_pass)) { ?>
+    <div id="show_password">
+    <p><?php echo t('PSW') ?></p>
+
+    <div id="download_password">
+    <p>
+        <?php echo '<input id="output_key" value="' . $download_pass . '" readonly/>'?>
+        <button id="password_copy_button">&#128203;</button>
+    </p>
+    </div>
+    </div>
+    <?php
+    }?>
+
+
     <?php
     if ($preview == true) { ?>
     <div id="upload_finished_preview">

patrick-canterino.de