X-Git-Url: https://git.p6c8.net/jirafeau_mojo42.git/blobdiff_plain/33bb6cfe3111b316082336edae43846aaef84f17..0b8bc44d7afc8c7249c697a7584fc9d1646e7c4d:/lib/functions.php?ds=sidebyside diff --git a/lib/functions.php b/lib/functions.php index a4f9ec6..1e084fb 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; } @@ -190,6 +190,9 @@ function jirafeau_ini_to_bytes($value) $modifier = substr($value, -1); $bytes = substr($value, 0, -1); switch (strtoupper($modifier)) { + default: + return intval($value); + break; case 'P': $bytes *= 1024; // no break @@ -229,6 +232,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 @@ -418,7 +445,7 @@ function jirafeau_upload($file, $one_time_download, $key, $time, $ip, $crypt, $l } } - /* 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']; @@ -509,10 +536,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; } @@ -559,13 +591,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); } @@ -588,8 +622,8 @@ function check_errors($cfg) } /** - * Read link informations - * @return array containing informations. + * Read link information + * @return array containing information. */ function jirafeau_get_link($hash) { @@ -654,7 +688,7 @@ function jirafeau_admin_list($name, $file_hash, $link_hash) /* Push new found directory. */ $stack[] = $d . $node . '/'; } elseif (is_file($d . $node)) { - /* Read link informations. */ + /* Read link information. */ $l = jirafeau_get_link($node); if (!count($l)) { continue; @@ -670,7 +704,7 @@ function jirafeau_admin_list($name, $file_hash, $link_hash) if (!empty($link_hash) && $link_hash != $node) { continue; } - /* Print link informations. */ + /* Print link information. */ echo ''; echo '' . ' 0 && filesize($r_path) + filesize($w_path) > $max_file_size * 1024 * 1024) { jirafeau_async_delete($ref); - return 'Error'; + return "Error: file size is above upload limit"; } /* Concatenate data. */ @@ -1028,7 +1071,7 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size) fclose($r); fclose($w); jirafeau_async_delete($ref); - return 'Error'; + return "Error: cannot write file"; } } fclose($r); @@ -1049,12 +1092,12 @@ function jirafeau_async_push($ref, $data, $code, $max_file_size) } /** - * Finalyze an asynchronous upload. + * Finalize an asynchronous upload. * @param $ref asynchronous upload reference * @param $code client code for this operation * @param $crypt boolean asking to crypt or not * @param $link_name_length link name length - * @return a string containing the download reference followed by a delete code or the string 'Error' + * @return a string containing the download reference followed by a delete code or a string starting with 'Error' */ function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_method) { @@ -1062,13 +1105,13 @@ function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_m $a = jirafeau_get_async_ref($ref); if (count($a) == 0 || $a['next_code'] != "$code") { - return "Error"; + return "Error: bad code for ending transfer"; } /* Generate link infos. */ $p = VAR_ASYNC . s2p($ref) . $ref . "_data"; if (!file_exists($p)) { - return 'Error'; + return "Error: referenced file does not exist"; } $crypted = false; @@ -1116,9 +1159,11 @@ function jirafeau_async_end($ref, $code, $crypt, $link_name_length, $file_hash_m fclose($handle); $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) || - !rename($link_tmp_name, VAR_LINKS . $l . $hash_link)) { - return 'Error'; + if (!@mkdir(VAR_LINKS . $l, 0755, true)) { + return "Error: cannot create folder in LINKS"; + } + if (!rename($link_tmp_name, VAR_LINKS . $l . $hash_link)) { + return "Error: cannot rename file in LINKS"; } /* Clean async upload. */ @@ -1529,5 +1574,5 @@ function jirafeau_add_ending_slash($path) function jirafeau_default_web_root() { - return $_SERVER['HTTP_HOST'] . str_replace(basename(__FILE__), '', $_SERVER['REQUEST_URI']); + return $_SERVER['HTTP_HOST'] . str_replace('install.php', '', $_SERVER['REQUEST_URI']); }