X-Git-Url: https://git.p6c8.net/jirafeau_project.git/blobdiff_plain/33bb6cfe3111b316082336edae43846aaef84f17..17d5977bf8d24a1b0abb5f52a8453322f21a994d:/lib/functions.php diff --git a/lib/functions.php b/lib/functions.php index a4f9ec6..850386b 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,20 +190,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; } @@ -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 @@ -237,19 +264,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. '); } @@ -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. */ @@ -1431,7 +1476,7 @@ function jirafeau_admin_session_start() $_SESSION['admin_csrf'] = md5(uniqid(mt_rand(), true)); } -function jirafeau_admin_session_end() +function jirafeau_session_end() { $_SESSION = array(); session_destroy(); @@ -1451,6 +1496,17 @@ function jirafeau_admin_csrf_field() return ""; } +function jirafeau_user_session_start() +{ + $_SESSION['user_auth'] = true; +} + +function jirafeau_user_session_logged() +{ + return isset($_SESSION['user_auth']) && + $_SESSION['user_auth'] === true; +} + function jirafeau_dir_size($dir) { $size = 0; @@ -1529,5 +1585,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']); }