X-Git-Url: https://git.p6c8.net/jirafeau.git/blobdiff_plain/0a5462c3311b4541a858b8e719ae8548877c7a62..6c49ea194c030a04ce8b303aae2a51dce01c7382:/lib/functions.php diff --git a/lib/functions.php b/lib/functions.php old mode 100755 new mode 100644 index c07b6c9..1e5b083 --- a/lib/functions.php +++ b/lib/functions.php @@ -90,6 +90,18 @@ jirafeau_gen_random ($l) return $code; } +function is_ssl() { + if ( isset($_SERVER['HTTPS']) ) { + if ( 'on' == strtolower($_SERVER['HTTPS']) ) + return true; + if ( '1' == $_SERVER['HTTPS'] ) + return true; + } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { + return true; + } + return false; +} + function jirafeau_human_size ($octets) { @@ -122,10 +134,11 @@ function jirafeau_clean_rm_file ($md5) { $p = s2p ("$md5"); - if (file_exists (VAR_FILES . $p . $md5)) - unlink (VAR_FILES . $p . $md5); - if (file_exists (VAR_FILES . $p . $md5 . '_count')) - unlink (VAR_FILES . $p . $md5 . '_count'); + $f = VAR_FILES . $p . $md5; + if (file_exists ($f) && is_file ($f)) + unlink ($f); + if (file_exists ($f . '_count') && is_file ($f . '_count')) + unlink ($f . '_count'); $parse = VAR_FILES . $p; $scan = array(); while (file_exists ($parse) @@ -307,13 +320,14 @@ jirafeau_delete_file ($md5) * @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 * @returns 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_upload ($file, $one_time_download, $key, $time, $ip, $crypt) +jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length) { if (empty ($file['tmp_name']) || !is_uploaded_file ($file['tmp_name'])) { @@ -379,7 +393,7 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt) fclose ($handle); /* Create delete code. */ - $delete_link_code = jirafeau_gen_random (8); + $delete_link_code = jirafeau_gen_random (5); /* md5 password or empty. */ $password = ''; @@ -387,14 +401,14 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt) $password = md5 ($key); /* create link file */ - $link_tmp_name = VAR_LINKS . $md5 . rand (0, 10000) . ' .tmp'; + $link_tmp_name = VAR_LINKS . $md5 . rand (0, 10000) . '.tmp'; $handle = fopen ($link_tmp_name, 'w'); fwrite ($handle, $name . NL. $mime_type . NL. $size . NL. $password . NL. $time . NL . $md5. NL . ($one_time_download ? 'O' : 'R') . NL . date ('U') . NL . $ip . NL. $delete_link_code . NL . ($crypted ? 'C' : 'O')); fclose ($handle); - $md5_link = base_16_to_64 (md5_file ($link_tmp_name)); + $md5_link = substr(base_16_to_64 (md5_file ($link_tmp_name)), 0, $link_name_length); $l = s2p ("$md5_link"); if (!@mkdir (VAR_LINKS . $l, 0755, true) || !rename ($link_tmp_name, VAR_LINKS . $l . $md5_link)) @@ -489,10 +503,10 @@ show_errors () } } -function check_errors () +function check_errors ($cfg) { if (file_exists (JIRAFEAU_ROOT . 'install.php') - && !file_exists (JIRAFEAU_ROOT . 'lib/config.local.php')) + && !($cfg['installation_done'] === true)) { header('Location: install.php'); exit; @@ -513,12 +527,6 @@ function check_errors () if (!is_writable (VAR_BLOCK)) add_error (t('The block directory is not writable!'), VAR_BLOCK); - - /* Check if the install.php script is still in the directory. */ - if (file_exists (JIRAFEAU_ROOT . 'install.php')) - add_error (t('Installer script still present'), - t('Please make sure to delete the installer script ' . - '"install.php" before continuing.')); } /** @@ -547,6 +555,8 @@ jirafeau_get_link ($hash) $out['link_code'] = trim ($c[9]); if (trim ($c[10]) == 'C') $out['crypted'] = true; + else + $out['crypted'] = false; return $out; } @@ -882,10 +892,11 @@ jirafeau_async_push ($ref, $data, $code) * @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 lenght * @return a string containing the download reference followed by a delete code or the string "Error" */ function -jirafeau_async_end ($ref, $code, $crypt) +jirafeau_async_end ($ref, $code, $crypt, $link_name_length) { /* Get async infos. */ $a = jirafeau_get_async_ref ($ref); @@ -902,7 +913,7 @@ jirafeau_async_end ($ref, $code, $crypt) $crypt_key = ''; if ($crypt == true && extension_loaded('mcrypt')) { - $cypt_key = jirafeau_encrypt_file ($p, $p); + $crypt_key = jirafeau_encrypt_file ($p, $p); if (strlen($crypt_key) > 0) $crypted = true; } @@ -910,7 +921,7 @@ jirafeau_async_end ($ref, $code, $crypt) $md5 = md5_file ($p); $size = filesize($p); $np = s2p ($md5); - $delete_link_code = jirafeau_gen_random (8); + $delete_link_code = jirafeau_gen_random (5); /* File already exist ? */ if (!file_exists (VAR_FILES . $np)) @@ -931,14 +942,14 @@ jirafeau_async_end ($ref, $code, $crypt) fclose ($handle); /* Create link. */ - $link_tmp_name = VAR_LINKS . $md5 . rand (0, 10000) . ' .tmp'; + $link_tmp_name = VAR_LINKS . $md5 . rand (0, 10000) . '.tmp'; $handle = fopen ($link_tmp_name, 'w'); fwrite ($handle, $a['file_name'] . NL . $a['mime_type'] . NL . $size . NL . $a['key'] . NL . $a['time'] . NL . $md5 . NL . $a['onetime'] . NL . date ('U') . NL . $a['ip'] . NL . $delete_link_code . NL . ($crypted ? 'C' : 'O')); fclose ($handle); - $md5_link = base_16_to_64 (md5_file ($link_tmp_name)); + $md5_link = substr(base_16_to_64 (md5_file ($link_tmp_name)), 0, $link_name_length); $l = s2p ("$md5_link"); if (!@mkdir (VAR_LINKS . $l, 0755, true) || !rename ($link_tmp_name, VAR_LINKS . $l . $md5_link)) @@ -1289,6 +1300,16 @@ jirafeau_admin_clean_block () return $count; } +function +jirafeau_crypt_create_iv($base, $size) +{ + $iv = ''; + while (strlen ($iv) < $size) + $iv = $iv . $base; + $iv = substr($iv, 0, $size); + return $iv; +} + /** * Crypt file and returns decrypt key. * @param $fp_src file path to the file to crypt. @@ -1304,12 +1325,12 @@ jirafeau_encrypt_file ($fp_src, $fp_dst) /* Prepare module. */ $m = mcrypt_module_open('rijndael-256', '', 'ofb', ''); - $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($m), MCRYPT_DEV_URANDOM); /* Generate key. */ - $ks = mcrypt_enc_get_key_size($m); - $key = substr(md5(jirafeau_gen_random(12)), 0, $ks); + $crypt_key = jirafeau_gen_random (10); + $md5_key = md5($crypt_key); + $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m)); /* Init module. */ - mcrypt_generic_init($m, $key, $iv); + mcrypt_generic_init($m, $md5_key, $iv); /* Crypt file. */ $r = fopen ($fp_src, 'r'); $w = fopen ($fp_dst, 'c'); @@ -1324,7 +1345,7 @@ jirafeau_encrypt_file ($fp_src, $fp_dst) /* Cleanup. */ mcrypt_generic_deinit($m); mcrypt_module_close($m); - return $key . "." . base64_encode($iv); + return $crypt_key; } /** @@ -1341,13 +1362,12 @@ jirafeau_decrypt_file ($fp_src, $fp_dst, $k) if ($fs === false || $fs == 0 || !extension_loaded('mcrypt')) return false; - /* Extract key and iv. */ - $ex = explode (".", $k); - $key = $ex[0]; - $iv = base64_decode($ex[1]); /* Init module */ $m = mcrypt_module_open('rijndael-256', '', 'ofb', ''); - mcrypt_generic_init($m, $key, $iv); + /* Extract key and iv. */ + $crypt_key = $k; + $md5_key = md5($crypt_key); + $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m)); /* Decrypt file. */ $r = fopen ($fp_src, 'r'); $w = fopen ($fp_dst, 'c'); @@ -1365,4 +1385,28 @@ jirafeau_decrypt_file ($fp_src, $fp_dst, $k) return true; } -?> +/** + * Check if Jirafeau is password protected for visitors. + * @return true if Jirafeau is password protected, false otherwise. + */ +function jirafeau_has_upload_password ($cfg) +{ + return count ($cfg['upload_password']) > 0; +} + +/** + * Challenge password for a visitor. + * @param $password password to be challenged + * @return true if password is valid, false otherwise. + */ +function jirafeau_challenge_upload_password ($cfg, $password) +{ + if (!jirafeau_has_upload_password($cfg)) + return false; + forEach ($cfg['upload_password'] as $p) + if ($password == $p) + return true; + error_log("password not found $password"); + return false; +} +