X-Git-Url: https://git.p6c8.net/jirafeau.git/blobdiff_plain/6bb6cfda2f5cbc5c7c9cc25cd228970ce2b582cd..f89474388394d7a7fd7dbb41f089956454e4957f:/lib/functions.php?ds=sidebyside
diff --git a/lib/functions.php b/lib/functions.php
old mode 100644
new mode 100755
index 6d7cb34..87d2cff
--- a/lib/functions.php
+++ b/lib/functions.php
@@ -31,6 +31,65 @@ s2p ($s)
return $p;
}
+/**
+ * Convert base 16 to base 64
+ * @returns A string based on 64 characters (0-9, a-z, A-Z, "-" and "_")
+ */
+function
+base_16_to_64 ($num)
+{
+ $m = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
+ $hex2bin = array ('0000', # 0
+ '0001', # 1
+ '0010', # 2
+ '0011', # 3
+ '0100', # 4
+ '0101', # 5
+ '0110', # 6
+ '0111', # 7
+ '1000', # 8
+ '1001', # 9
+ '1010', # a
+ '1011', # b
+ '1100', # c
+ '1101', # d
+ '1110', # e
+ '1111'); #Â f
+ $o = '';
+ $b = '';
+ $i = 0;
+ # Convert long hex string to bin.
+ $size = strlen ($num);
+ for ($i = 0; $i < $size; $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;
+ # Some few bits remaining ?
+ if ($i < 0 && $i > -6)
+ $o = $m{bindec (substr ($b, 0, $i + 6))} . $o;
+ return $o;
+}
+
+/**
+ * Generate a random code.
+ * @param $l code length
+ * @return random code.
+ */
+function
+jirafeau_gen_random ($l)
+{
+ if ($l <= 0)
+ return 42;
+
+ $code="";
+ for ($i = 0; $i < $l; $i++)
+ $code .= dechex (rand (0, 15));
+
+ return $code;
+}
+
function
jirafeau_human_size ($octets)
{
@@ -106,6 +165,17 @@ function jirafeau_ini_to_bytes ($value)
return $bytes;
}
+/**
+ * gets the maximum upload size according to php.ini
+ * @returns the maximum upload size in bytes
+ */
+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')));
+}
+
/**
* gets the maximum upload size according to php.ini
* @returns the maximum upload size string
@@ -259,7 +329,7 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
/* file informations */
$md5 = md5_file ($file['tmp_name']);
- $name = trim ($file['name']);
+ $name = str_replace (NL, '', trim ($file['name']));
$mime_type = $file['type'];
$size = $file['size'];
@@ -285,7 +355,7 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
'delete_link' => ''));
}
- /* increment or create count file */
+ /* Increment or create count file. */
$counter = 0;
if (file_exists (VAR_FILES . $p . $md5 . '_count'))
{
@@ -298,9 +368,7 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
fclose ($handle);
/* Create delete code. */
- $delete_link_code = 0;
- for ($i = 0; $i < 8; $i++)
- $delete_link_code .= dechex (rand (0, 16));
+ $delete_link_code = jirafeau_gen_random (8);
/* md5 password or empty */
$password = '';
@@ -312,10 +380,10 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
$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);
+ NL . $md5. NL . ($one_time_download ? 'O' : 'R') . NL . date ('U') .
+ NL . $ip . NL. $delete_link_code . NL);
fclose ($handle);
- $md5_link = md5_file ($link_tmp_name);
+ $md5_link = base_16_to_64 (md5_file ($link_tmp_name));
$l = s2p ("$md5_link");
if (!@mkdir (VAR_LINKS . $l, 0755, true) ||
!rename ($link_tmp_name, VAR_LINKS . $l . $md5_link))
@@ -364,7 +432,6 @@ jirafeau_is_viewable ($mime)
return false;
}
-
// Error handling functions.
//! Global array that contains all registered errors.
$error_list = array ();
@@ -410,6 +477,35 @@ show_errors ()
}
}
+function check_errors ()
+{
+ if (file_exists (JIRAFEAU_ROOT . 'install.php')
+ && !file_exists (JIRAFEAU_ROOT . 'lib/config.local.php'))
+ {
+ header('Location: install.php');
+ exit;
+ }
+
+ /* check if the destination dirs are writable */
+ $writable = is_writable (VAR_FILES) && is_writable (VAR_LINKS);
+
+ /* Checking for errors. */
+ if (!is_writable (VAR_FILES))
+ add_error (t('The file directory is not writable!'), VAR_FILES);
+
+ if (!is_writable (VAR_LINKS))
+ add_error (t('The link directory is not writable!'), VAR_LINKS);
+
+ if (!is_writable (VAR_ASYNC))
+ add_error (t('The async directory is not writable!'), VAR_ASYNC);
+
+ /* 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.'));
+}
+
/**
* Read link informations
* @return array containing informations.
@@ -446,11 +542,11 @@ jirafeau_admin_list ($name, $file_hash, $link_hash)
{
echo '