From 71f17ac4602c2b6b1e3b46130c4c6cb7c3f8702e Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sun, 29 Jul 2012 12:25:32 +0200
Subject: [PATCH] copy not move uploaded files FS#2465

This fixes problems with setgid bits on the media directory
---
 inc/media.php | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/inc/media.php b/inc/media.php
index 794aab8aa..6941aa61a 100644
--- a/inc/media.php
+++ b/inc/media.php
@@ -296,7 +296,7 @@ function media_upload($ns,$auth,$file=false){
     $res = media_save(array('name' => $file['tmp_name'],
                             'mime' => $imime,
                             'ext'  => $iext), $ns.':'.$id,
-                      $INPUT->post->bool('ow'), $auth, 'move_uploaded_file');
+                      $INPUT->post->bool('ow'), $auth, 'copy_uploaded_file');
     if (is_array($res)) {
         msg($res[0], $res[1]);
         return false;
@@ -304,6 +304,23 @@ function media_upload($ns,$auth,$file=false){
     return $res;
 }
 
+/**
+ * An alternative to move_uploaded_file that copies
+ *
+ * Using copy, makes sure any setgid bits on the media directory are honored
+ *
+ * @see   move_uploaded_file()
+ * @param string $from
+ * @param string $to
+ * @return bool
+ */
+function copy_uploaded_file($from, $to){
+    if(!is_uploaded_file($from)) return false;
+    $ok = copy($from, $to);
+    @unlink($from);
+    return $ok;
+}
+
 /**
  * This generates an action event and delegates to _media_upload_action().
  * Action plugins are allowed to pre/postprocess the uploaded file.
-- 
GitLab