X-Git-Url: https://git.p6c8.net/jirafeau/mojo42.git/blobdiff_plain/6bb6cfda2f5cbc5c7c9cc25cd228970ce2b582cd..3b097eba6400cac6e9f5d6b230a33f2783b38e5b:/lib/functions.php
diff --git a/lib/functions.php b/lib/functions.php
old mode 100644
new mode 100755
index 6d7cb34..a8851aa
--- 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
@@ -236,13 +306,15 @@ jirafeau_delete_file ($md5)
  * @param $key if not empty, protect the file with this key
  * @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)
+jirafeau_upload ($file, $one_time_download, $key, $time, $ip, $crypt, $link_name_length)
 {
     if (empty ($file['tmp_name']) || !is_uploaded_file ($file['tmp_name']))
     {
@@ -257,9 +329,19 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
     /* array representing no error */
     $noerr = array ('has_error' => false, 'why' => '');
 
+    /* Crypt file if option is enabled. */
+    $crypted = false;
+    $crypt_key = '';
+    if ($crypt == true && extension_loaded('mcrypt'))
+    {
+        $crypt_key = jirafeau_encrypt_file ($file['tmp_name'], $file['tmp_name']);
+        if (strlen($crypt_key) > 0)
+            $crypted = true;
+    }
+
     /* 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 +367,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,11 +380,9 @@ 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 (5);
 
-    /* md5 password or empty */
+    /* md5 password or empty. */
     $password = '';
     if (!empty ($key))
         $password = md5 ($key);
@@ -312,10 +392,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 . ($crypted ? 'C' : 'O'));
     fclose ($handle);
-    $md5_link = 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))
@@ -343,7 +423,8 @@ jirafeau_upload ($file, $one_time_download, $key, $time, $ip)
     }
    return (array ('error' => $noerr,
                   'link' => $md5_link,
-                  'delete_link' => $delete_link_code));
+                  'delete_link' => $delete_link_code,
+                  'crypt_key' => $crypt_key));
 }
 
 /**
@@ -364,7 +445,6 @@ jirafeau_is_viewable ($mime)
     return false;
 }
 
-
 // Error handling functions.
 //! Global array that contains all registered errors.
 $error_list = array ();
@@ -410,6 +490,38 @@ 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);
+
+     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.'));
+}
+
 /**
  * Read link informations
  * @return array containing informations.
@@ -434,6 +546,8 @@ jirafeau_get_link ($hash)
     $out['upload_date'] = trim ($c[7]);
     $out['ip'] = trim ($c[8]);
     $out['link_code'] = trim ($c[9]);
+    if (trim ($c[10]) == 'C')
+	    $out['crypted'] = true;
     
     return $out;
 }
@@ -446,11 +560,11 @@ jirafeau_admin_list ($name, $file_hash, $link_hash)
 {
     echo '