- * @return array
- */
-function uploadFile($file)
-{
- global $db;
- global $FILTER_MODE;
- global $FILTER_MIME;
-
- // Handle file errors
- if ($file->error) {
- throw new UploadException($file->error);
- }
-
- //fixes a bug
- $lol = $file->getSha1();
-
- // Check if a file with the same hash and size (a file which is the same)
- // does already exist in the database; if it does, return the proper link
- // and data. PHP deletes the temporary file just uploaded automatically.
- if(ANTI_DUPE){
- $q = $db->prepare('SELECT filename, COUNT(*) AS count FROM files WHERE hash = (:hash) AND size = (:size)');
- $q->bindValue(':hash', $file->getSha1(), PDO::PARAM_STR);
- $q->bindValue(':size', $file->size, PDO::PARAM_INT);
- $q->execute();
- $result = $q->fetch();
- if ($result['count'] > 0) {
- return [
- 'hash' => $file->getSha1(),
- 'name' => $file->name,
- 'url' => UGUU_URL.rawurlencode($result['filename']),
- 'size' => $file->size,
- ];
- }
-}
-
- // Generate a name for the file
- $newname = generateName($file);
-
- // Store the file's full file path in memory
- $uploadFile = UGUU_FILES_ROOT.$newname;
-
- // Attempt to move it to the static directory
- if (!move_uploaded_file($file->tempfile, $uploadFile)) {
- http_response_code(500);
- throw new Exception(
- 'Failed to move file to destination',
- 500
- ); // HTTP status code "500 Internal Server Error"
- }
-
- // Need to change permissions for the new file to make it world readable
- if (!chmod($uploadFile, 0644)) {
- http_response_code(500);
- throw new Exception(
- 'Failed to change file permissions',
- 500
- ); // HTTP status code "500 Internal Server Error"
- }
-
- // Log IP
- if(LOG_IP){
- $ip = $_SERVER['REMOTE_ADDR'];
- } else {
- $ip = null;
- }
-
- // Common parameters binding
- $q = $db->prepare('INSERT INTO files (hash, originalname, filename, size, date, ip) VALUES (:hash, :orig, :name, :size, :date, :ip)');
- $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_STR);
- $q->bindValue(':ip', $ip, PDO::PARAM_STR);
- $q->execute();
-
- return [
- 'hash' => $file->getSha1(),
- 'name' => $file->name,
- 'url' => UGUU_URL.rawurlencode($newname),
- 'size' => $file->size,
- ];
-}
-
-/**
- * Reorder files array by file.