]> jfr.im git - uguu.git/blobdiff - static/php/upload.php
fixed empty db when not logging ip
[uguu.git] / static / php / upload.php
index 87190763be82e46bf89f41cc7cff3b7e0c1d2d9e..6c6786b691cbedbbd0106f4ec401e6664e4c73ec 100644 (file)
@@ -1,15 +1,20 @@
 <?php
 /**
- * Require the settings and DB files.
+ * Handles POST uploads, generates filenames, moves files around and commits
+ * uploaded metadata to database.
  */
+
 require_once 'classes/Response.class.php';
 require_once 'classes/UploadException.class.php';
 require_once 'classes/UploadedFile.class.php';
 require_once 'includes/database.inc.php';
 
 /**
- * Generates name and checks in DB
- * Also adds to DB.
+ * Generates a random name for the file, retrying until we get an unused one.
+ *
+ * @param UploadedFile $file
+ *
+ * @return string
  */
 function generateName($file)
 {
@@ -37,7 +42,8 @@ function generateName($file)
     do {
         // Iterate until we reach the maximum number of retries
         if ($tries-- === 0) {
-            throw new Exception(
+               http_response_code(500);
+        throw new Exception(
                 'Gave up trying to find an unused name',
                 500
             ); // HTTP status code "500 Internal Server Error"
@@ -54,17 +60,17 @@ function generateName($file)
             $name .= '.'.$ext;
         }
 
-        //Check if mime is blacklisted
-        if (in_array($type_mime, unserialize(CONFIG_BLOCKED_MIME))) {
-            http_response_code(415);
-            throw new Exception('Filetype not allowed!');
+       //Check if mime is blacklisted
+       if (in_array($type_mime, unserialize(CONFIG_BLOCKED_MIME))) {
+               http_response_code(415);
+        throw new Exception ('Extension type not allowed.');
             exit(0);
-        }
+          }
 
         //Check if EXT is blacklisted
         if (in_array($ext, unserialize(CONFIG_BLOCKED_EXTENSIONS))) {
-            http_response_code(415);
-            throw new Exception('Filetype not allowed!');
+            http_response_code(415);           
+        throw new Exception ('Extension type not allowed.');
             exit(0);
         }
 
@@ -74,10 +80,9 @@ function generateName($file)
         $q->execute();
         $result = $q->fetchColumn();
         // If it does, generate a new name
-    } while ($result > 0);
-
-    return $name;
-}
+        } while ($result > 0);
+            return $name;
+        }
 
 /**
  * Handles the uploading and db entry for a file.
@@ -100,12 +105,16 @@ function uploadFile($file)
     // Generate a name for the file
     $newname = generateName($file);
 
+    // Get IP
+    $ip = $_SERVER['REMOTE_ADDR'];
+
     // Store the file's full file path in memory
-    $uploadFile = UGUU_FILES_ROOT.$newname;
+    $uploadFile = UGUU_FILES_ROOT . $newname;
 
     // Attempt to move it to the static directory
     if (!move_uploaded_file($file->tempfile, $uploadFile)) {
-        throw new Exception(
+            http_response_code(500);        
+    throw new Exception(
             'Failed to move file to destination',
             500
         ); // HTTP status code "500 Internal Server Error"
@@ -113,39 +122,47 @@ function uploadFile($file)
 
     // Need to change permissions for the new file to make it world readable
     if (!chmod($uploadFile, 0644)) {
-        throw new Exception(
+            http_response_code(500);       
+    throw new Exception(
             'Failed to change file permissions',
             500
         ); // HTTP status code "500 Internal Server Error"
     }
 
     // Add it to the database
-    $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date) VALUES (:hash, :orig, :name, :size, :date)');
-
+    if(LOG_IP == 'yes'){
+        $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ip) VALUES (:hash, :orig, :name, :size, :date, :ip)');
+    }else{
+        $ip = '0';
+        $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ip) VALUES (:hash, :orig, :name, :size, :date, :ip)');
+    }
     // Common parameters binding
     $q->bindValue(':hash', $file->getSha1(), PDO::PARAM_STR);
     $q->bindValue(':orig', strip_tags($file->name), PDO::PARAM_STR);
     $q->bindValue(':name', $newname, PDO::PARAM_STR);
     $q->bindValue(':size', $file->size, PDO::PARAM_INT);
     $q->bindValue(':date', time(), PDO::PARAM_INT);
+    $q->bindValue(':ip', $ip, PDO::PARAM_STR);
     $q->execute();
 
-    return [
+    return array(
         'hash' => $file->getSha1(),
         'name' => $file->name,
         'url' => UGUU_URL.rawurlencode($newname),
         'size' => $file->size,
-    ];
+    );
 }
 
 /**
  * Reorder files array by file.
  *
+ * @param  $_FILES
+ *
  * @return array
  */
 function diverseArray($files)
 {
-    $result = [];
+    $result = array();
 
     foreach ($files as $key1 => $value1) {
         foreach ($value1 as $key2 => $value2) {
@@ -159,11 +176,13 @@ function diverseArray($files)
 /**
  * Reorganize the $_FILES array into something saner.
  *
+ * @param  $_FILES
+ *
  * @return array
  */
 function refiles($files)
 {
-    $result = [];
+    $result = array();
     $files = diverseArray($files);
 
     foreach ($files as $file) {