X-Git-Url: https://git.p6c8.net/jirafeau_project.git/blobdiff_plain/1203cc4e9094fa86f7978c9da56f454d4c21ca32..ec8af7800c2e654b60f21400d3bcb6a7842e8e8a:/lib/functions.php diff --git a/lib/functions.php b/lib/functions.php index 34c7df8..b87f6c3 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -20,7 +20,7 @@ */ /** - * Transform a string in a path by seperating each letters by a '/'. + * Transform a string in a path by separating each letters by a '/'. * @return path finishing with a '/' */ function s2p($s) @@ -28,7 +28,7 @@ function s2p($s) $block_size = 8; $p = ''; for ($i = 0; $i < strlen($s); $i++) { - $p .= $s{$i}; + $p .= $s[$i]; if (($i + 1) % $block_size == 0) { $p .= '/'; } @@ -68,16 +68,16 @@ function base_16_to_64($num) # Convert long hex string to bin. $size = strlen($num); for ($i = 0; $i < $size; $i++) { - $b .= $hex2bin{hexdec($num{$i})}; + $b .= $hex2bin[hexdec($num[$i])]; } # Convert long bin to base 64. $size *= 4; for ($i = $size - 6; $i >= 0; $i -= 6) { - $o = $m{bindec(substr($b, $i, 6))} . $o; + $o = $m[bindec(substr($b, $i, 6))] . $o; } # Some few bits remaining ? if ($i < 0 && $i > -6) { - $o = $m{bindec(substr($b, 0, $i + 6))} . $o; + $o = $m[bindec(substr($b, 0, $i + 6))] . $o; } return $o; } @@ -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,16 +209,23 @@ function jirafeau_ini_to_bytes($value) $modifier = substr($value, -1); $bytes = substr($value, 0, -1); switch (strtoupper($modifier)) { - case 'P': - $bytes *= 1024; - case 'T': - $bytes *= 1024; - case 'G': - $bytes *= 1024; - case 'M': - $bytes *= 1024; - 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; } @@ -210,8 +236,10 @@ function jirafeau_ini_to_bytes($value) */ function jirafeau_get_max_upload_size_bytes() { - return min(jirafeau_ini_to_bytes(ini_get('post_max_size')), - jirafeau_ini_to_bytes(ini_get('upload_max_filesize'))); + return min( + jirafeau_ini_to_bytes(ini_get('post_max_size')), + jirafeau_ini_to_bytes(ini_get('upload_max_filesize')) + ); } /** @@ -223,6 +251,30 @@ function jirafeau_get_max_upload_size() return jirafeau_human_size(jirafeau_get_max_upload_size_bytes()); } +/** + * get the maximal upload size for a data chunk in async uploads + * @param max_upload_chunk_size_bytes + */ +function jirafeau_get_max_upload_chunk_size_bytes($max_upload_chunk_size_bytes = 0) +{ + 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; + } + $size = min( + jirafeau_get_max_upload_size_bytes(), + $max_upload_chunk_size_bytes + ); + if ($size == 0) { + return $max_upload_chunk_size_bytes; + } + return $size; +} + /** * gets a string explaining the error * @param $code the error code @@ -231,19 +283,19 @@ function jirafeau_get_max_upload_size() 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. '); } @@ -402,17 +454,19 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l /* 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 == 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 == true && extension_loaded('sodium') == true) { + $crypt_key = jirafeau_encrypt_file($file['tmp_name'], $file['tmp_name'].'crypt'); if (strlen($crypt_key) > 0) { - $crypted = true; + if (rename($file['tmp_name'].'crypt', $file['tmp_name']) === true) { + $crypted = true; + } } } - /* file informations */ + /* file information */ $hash = jirafeau_hash_file($file_hash_method, $file['tmp_name']); $name = str_replace(NL, '', trim($file['name'])); $mime_type = $file['type']; @@ -459,10 +513,12 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l /* create link file */ $link_tmp_name = VAR_LINKS . $hash . rand(0, 10000) . '.tmp'; $handle = fopen($link_tmp_name, 'w'); - fwrite($handle, - $name . NL. $mime_type . NL. $size . NL. $password . NL. $time . + fwrite( + $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); $l = s2p("$hash_link"); @@ -501,10 +557,15 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l function jirafeau_is_viewable($mime) { if (!empty($mime)) { - /* Actually, verify if mime-type is an image or a text. */ - $viewable = array('image', 'text', 'video', 'audio'); + $viewable = array('image', 'video', 'audio'); $decomposed = explode('/', $mime); - return in_array($decomposed[0], $viewable); + if (in_array($decomposed[0], $viewable) && strpos($mime, 'image/svg+xml') === false) { + return true; + } + $viewable = array('text/plain'); + if (in_array($mime, $viewable)) { + return true; + } } return false; } @@ -551,13 +612,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); } @@ -580,8 +643,8 @@ function check_errors($cfg) } /** - * Read link informations - * @return array containing informations. + * Read link information + * @return array containing information. */ function jirafeau_get_link($hash) { @@ -603,7 +666,8 @@ 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; } @@ -627,15 +691,9 @@ function jirafeau_admin_list($name, $file_hash, $link_hash) echo t('LS_FILES'); } echo ''; - echo '
' . t('FILENAME') . ' | '; - echo '' . t('TYPE') . ' | '; - echo '' . t('SIZE') . ' | '; - echo '' . t('EXPIRE') . ' | '; - echo '' . t('ONETIME') . ' | '; - echo '' . t('UPLOAD_DATE') . ' | '; - echo '' . t('ORIGIN') . ' | '; + echo ''; echo ' | ' . t('ACTION') . ' | '; echo '
---|---|---|---|---|---|---|---|---|
' . '' . jirafeau_escape($l['file_name']) . ''; - echo ' | '; - echo '' . jirafeau_escape($l['mime_type']) . ' | '; - echo '' . jirafeau_human_size($l['file_size']) . ' | '; - echo '' . ($l['time'] == -1 ? 'â' : jirafeau_get_datetimefield($l['time'])) . ' | '; - echo '';
- if ($l['onetime'] == 'O') {
- echo 'Y';
- } else {
- echo 'N';
+ t('DL_PAGE') . '">' . jirafeau_escape($l['file_name']) . ' '; + echo t('TYPE') . ': ' . jirafeau_escape($l['mime_type']) . ' '; + echo t('SIZE') . ': ' . jirafeau_human_size($l['file_size']) . ' '; + echo t('EXPIRE') . ': ' . ($l['time'] == -1 ? 'â' : jirafeau_get_datetimefield($l['time'])) . ' '; + echo t('ONETIME') . ': ' . ($l['onetime'] == 'O' ? 'Yes' : 'No') . ' '; + echo t('UPLOAD_DATE') . ': ' . jirafeau_get_datetimefield($l['upload_date']) . ' '; + if (strlen($l['ip']) > 0) { + echo t('ORIGIN') . ': ' . $l['ip'] . ' '; } - echo ' | ';
- echo '' . jirafeau_get_datetimefield($l['upload_date']) . ' | '; - echo '' . $l['ip'] . ' | '; - echo '' . - ' | '; + echo ' |