diff options
-rw-r--r-- | inc/media.php | 41 | ||||
-rw-r--r-- | lib/exe/ajax.php | 63 | ||||
-rw-r--r-- | lib/scripts/fileuploaderextended.js | 9 |
3 files changed, 56 insertions, 57 deletions
diff --git a/inc/media.php b/inc/media.php index 768026b54..abdb33bcb 100644 --- a/inc/media.php +++ b/inc/media.php @@ -243,19 +243,56 @@ function media_delete($id,$auth){ } /** + * Handle file uploads via XMLHttpRequest + * + * @return mixed false on error, id of the new file on success + */ +function media_upload_xhr($ns,$auth){ + $id = $_GET['qqfile']; + list($ext,$mime,$dl) = mimetype($id); + $input = fopen("php://input", "r"); + $temp = tmpfile(); + $realSize = stream_copy_to_stream($input, $temp); + fclose($input); + if ($realSize != (int)$_SERVER["CONTENT_LENGTH"]) return false; + if (!($tmp = io_mktmpdir())) return false; + $path = $tmp.'/'.$id; + $target = fopen($path, "w"); + fseek($temp, 0, SEEK_SET); + stream_copy_to_stream($temp, $target); + fclose($target); + $res = media_save( + array('name' => $path, + 'mime' => $mime, + 'ext' => $ext), + $ns.':'.$id, + (($_REQUEST['ow'] == 'true') ? true : false), + $auth, + 'copy' + ); + unlink($path); + if ($tmp) dir_delete($tmp); + if (is_array($res)) { + msg($res[0], $res[1]); + return false; + } + return $res; +} + +/** * Handles media file uploads * * @author Andreas Gohr <andi@splitbrain.org> * @author Michael Klier <chi@chimeric.de> * @return mixed false on error, id of the new file on success */ -function media_upload($ns,$auth){ +function media_upload($ns,$auth,$file=false){ if(!checkSecurityToken()) return false; global $lang; // get file and id $id = $_POST['mediaid']; - $file = $_FILES['upload']; + if (!$file) $file = $_FILES['upload']; if(empty($id)) $id = $file['name']; // check for errors (messages are done in lib/exe/mediamanager.php) diff --git a/lib/exe/ajax.php b/lib/exe/ajax.php index 7a4cea360..aa07fe7b0 100644 --- a/lib/exe/ajax.php +++ b/lib/exe/ajax.php @@ -249,65 +249,24 @@ function ajax_mediadiff(){ } function ajax_mediaupload(){ - global $NS; + global $NS, $MSG; + $NS = $_REQUEST['ns']; $AUTH = auth_quickaclcheck("$NS:*"); if($AUTH >= AUTH_UPLOAD) { io_createNamespace("$NS:xxx", 'media'); } - if($_FILES['qqfile']['error']){ - unset($_FILES['qqfile']); - } - - if($_FILES['qqfile']['tmp_name']){ - $id = $_REQUEST['file_name']; - if (!$id) $id = $_FILES['qqfile']['name']; - $file = $_FILES['qqfile']['tmp_name']; - list($ext,$mime,$dl) = mimetype($id); - - $res = media_save( - array('name' => $file, - 'mime' => $mime, - 'ext' => $ext), - $NS.':'.$id, - $_REQUEST['ow'], - $AUTH, - 'move_uploaded_file' - ); - if (!is_array($res)) { - $result = array('success'=>true); - } - } + if ($_FILES['qqfile']['error']) unset($_FILES['qqfile']); + if ($_FILES['qqfile']['tmp_name']) $res = media_upload($NS, $AUTH, $_FILES['qqfile']); + if (isset($_GET['qqfile'])) $res = media_upload_xhr($NS, $AUTH); + if ($res) $result = array('success'=>true); - if (isset($_GET['qqfile'])) { - $id = $_GET['qqfile']; - list($ext,$mime,$dl) = mimetype($id); - $input = fopen("php://input", "r"); - $temp = tmpfile(); - $realSize = stream_copy_to_stream($input, $temp); - fclose($input); - if ($realSize != (int)$_SERVER["CONTENT_LENGTH"]) return false; - if (!($tmp = io_mktmpdir())) return false; - $path = $tmp.'/'.$id; - $target = fopen($path, "w"); - fseek($temp, 0, SEEK_SET); - stream_copy_to_stream($temp, $target); - fclose($target); - $res = media_save( - array('name' => $path, - 'mime' => $mime, - 'ext' => $ext), - $NS.':'.$id, - (($_REQUEST['ow'] == 'true') ? true : false), - $AUTH, - 'copy' - ); - unlink($path); - if ($tmp) dir_delete($tmp); - if (!is_array($res)) { - $result = array('success'=>true); + if (!$result) { + $error = ''; + if (isset($MSG)) { + foreach($MSG as $msg) $error .= $msg['msg']; } + $result = array('error'=> $msg['msg']); } - if (!$result) $result = array('error'=> 'Could not save uploaded file.'); echo htmlspecialchars(json_encode($result), ENT_NOQUOTES); } diff --git a/lib/scripts/fileuploaderextended.js b/lib/scripts/fileuploaderextended.js index cbe7304c2..fd2104e8e 100644 --- a/lib/scripts/fileuploaderextended.js +++ b/lib/scripts/fileuploaderextended.js @@ -107,7 +107,8 @@ qq.FileUploaderExtended = function(o){ // added to list item when upload completes // used in css to hide progress spinner success: 'qq-upload-success', - fail: 'qq-upload-fail' + fail: 'qq-upload-fail', + failedText : 'qq-upload-failed-text' } }); @@ -143,7 +144,7 @@ qq.extend(qq.FileUploaderExtended.prototype, { }, _onComplete: function(id, fileName, result){ - qq.FileUploaderBasic.prototype._onComplete.apply(this, arguments); + this._filesInProgress--; // mark completed var item = this._getItemByFileId(id); @@ -159,6 +160,8 @@ qq.extend(qq.FileUploaderExtended.prototype, { qq.addClass(item, this._classes.success); } else { qq.addClass(item, this._classes.fail); + var fail = this._find(item, 'failedText'); + qq.setText(fail, result.error); } } @@ -207,7 +210,7 @@ qq.extend(qq.UploadHandlerForm.prototype, { var form = this._createForm(iframe, params); form.appendChild(input); - var nameInput = qq.toElement('<input name="file_name" value="' + fileName + '" type="text">'); + var nameInput = qq.toElement('<input name="mediaid" value="' + fileName + '" type="text">'); form.appendChild(nameInput); var owCheckbox = document.getElementById('dw__ow').cloneNode(true); |