X-Git-Url: https://git.p6c8.net/jirafeau_project.git/blobdiff_plain/03d1001bce1a6d3ba0cce77199a6e1ad686e8273..d9647e1afea29401470efd68730d2562659be006:/lib/functions.php?ds=sidebyside diff --git a/lib/functions.php b/lib/functions.php index 459587d..77acae1 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -2,7 +2,7 @@ /* * Jirafeau, your web file repository * Copyright (C) 2008 Julien "axolotl" BERNARD - * Copyright (C) 2012 Jerome Jutteau + * Copyright (C) 2015 Jerome Jutteau * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -98,6 +98,9 @@ function is_ssl() { return true; } elseif ( isset($_SERVER['SERVER_PORT']) && ( '443' == $_SERVER['SERVER_PORT'] ) ) { return true; + } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) { + if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') + return true; } return false; } @@ -345,7 +348,9 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt, $link_name /* Crypt file if option is enabled. */ $crypted = false; $crypt_key = ''; - if ($crypt == true && extension_loaded('mcrypt')) + if ($crypt == true && !(extension_loaded('mcrypt') == true)) + error_log ("PHP extension mcrypt 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 (strlen($crypt_key) > 0) @@ -441,7 +446,7 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt, $link_name } /** - * tells if a mime-type is viewable in a browser + * Tells if a mime-type is viewable in a browser * @param $mime the mime type * @returns a boolean telling if a mime type is viewable */ @@ -451,7 +456,7 @@ jirafeau_is_viewable ($mime) if (!empty ($mime)) { /* Actually, verify if mime-type is an image or a text. */ - $viewable = array ('image', 'text'); + $viewable = array ('image', 'text', 'video', 'audio'); $decomposed = explode ('/', $mime); return in_array ($decomposed[0], $viewable); } @@ -839,10 +844,11 @@ jirafeau_async_init ($filename, $type, $one_time, $key, $time, $ip) * @param $ref asynchronous upload reference * @param $file piece of data * @param $code client code for this operation + * @param $max_file_size maximum allowed file size * @return a string containing a next code to use or the string "Error" */ function -jirafeau_async_push ($ref, $data, $code) +jirafeau_async_push ($ref, $data, $code, $max_file_size) { /* Get async infos. */ $a = jirafeau_get_async_ref ($ref); @@ -856,9 +862,21 @@ jirafeau_async_push ($ref, $data, $code) $p = s2p ($ref); + /* File path. */ + $r_path = $data['tmp_name']; + $w_path = VAR_ASYNC . $p . $ref . '_data'; + + /* Check that file size is not above upload limit. */ + if ($max_file_size > 0 && + filesize ($r_path) + filesize ($w_path) > $max_file_size * 1024 * 1024) + { + jirafeau_async_delete ($ref); + return "Error"; + } + /* Concatenate data. */ - $r = fopen ($data['tmp_name'], 'r'); - $w = fopen (VAR_ASYNC . $p . $ref . '_data', 'a'); + $r = fopen ($r_path, 'r'); + $w = fopen ($w_path, 'a'); while (!feof ($r)) { if (fwrite ($w, fread ($r, 1024)) === false) @@ -871,7 +889,7 @@ jirafeau_async_push ($ref, $data, $code) } fclose ($r); fclose ($w); - unlink ($data['tmp_name']); + unlink ($r_path); /* Update async file. */ $code = jirafeau_gen_random (4); @@ -889,7 +907,7 @@ 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 + * @param $link_name_length link name length * @return a string containing the download reference followed by a delete code or the string "Error" */ function @@ -908,7 +926,7 @@ jirafeau_async_end ($ref, $code, $crypt, $link_name_length) $crypted = false; $crypt_key = ''; - if ($crypt == true && extension_loaded('mcrypt')) + if ($crypt == true && extension_loaded('mcrypt') == true) { $crypt_key = jirafeau_encrypt_file ($p, $p); if (strlen($crypt_key) > 0) @@ -977,7 +995,7 @@ function jirafeau_encrypt_file ($fp_src, $fp_dst) { $fs = filesize ($fp_src); - if ($fs === false || $fs == 0 || !extension_loaded('mcrypt')) + if ($fs === false || $fs == 0 || !(extension_loaded('mcrypt') == true)) return ''; /* Prepare module. */ @@ -1016,7 +1034,7 @@ function jirafeau_decrypt_file ($fp_src, $fp_dst, $k) { $fs = filesize ($fp_src); - if ($fs === false || $fs == 0 || !extension_loaded('mcrypt')) + if ($fs === false || $fs == 0 || !(extension_loaded('mcrypt') == true)) return false; /* Init module */ @@ -1063,7 +1081,56 @@ function jirafeau_challenge_upload_password ($cfg, $password) forEach ($cfg['upload_password'] as $p) if ($password == $p) return true; - error_log("password not found $password"); return false; } +/** + * Test if visitor's IP is authorized to upload. + * @param $ip IP to be challenged + * @return true if IP is authorized, false otherwise. + */ +function jirafeau_challenge_upload_ip ($cfg, $ip) +{ + if (count ($cfg['upload_ip']) == 0) + return true; + forEach ($cfg['upload_ip'] as $i) + { + if ($i == $ip) + return true; + // CIDR test for IPv4 only. + if (strpos ($i, '/') !== false) + { + list ($subnet, $mask) = explode('/', $i); + if ((ip2long ($ip) & ~((1 << (32 - $mask)) - 1) ) == ip2long ($subnet)) + return true; + } + } + return false; +} + +/** + * Get the ip address of the client from REMOTE_ADDR + * or from HTTP_X_FORWARDED_FOR if behind a proxy + * @returns an the client ip address + */ +function get_ip_address($cfg) { + if (count ($cfg['proxy_ip']) == 0 || + empty ($_SERVER['HTTP_X_FORWARDED_FOR'])) + return $_SERVER['REMOTE_ADDR']; + + $iplist = explode (',', $_SERVER['HTTP_X_FORWARDED_FOR']); + if (count ($iplist) == 0) + return $_SERVER['REMOTE_ADDR']; + + foreach ($cfg['proxy_ip'] as $proxy_ip) + { + if ($_SERVER['REMOTE_ADDR'] != $proxy_ip) + continue; + + // Take the last IP (the one which has been set by our proxy). + $ip = end($iplist); + $ip = preg_replace ('/\s+/', '', $ip); + return $ip; + } + return $_SERVER['REMOTE_ADDR']; +}