From 2d6cc64fdb73879f54aa25b2122f36631c654e3c Mon Sep 17 00:00:00 2001 From: Kate Arzamastseva Date: Fri, 12 Aug 2011 12:50:34 +0300 Subject: issue #44, #52 media_upload reused, error handling --- inc/media.php | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'inc') diff --git a/inc/media.php b/inc/media.php index 768026b54..abdb33bcb 100644 --- a/inc/media.php +++ b/inc/media.php @@ -242,6 +242,43 @@ function media_delete($id,$auth){ return $data['unl'] ? DOKU_MEDIA_DELETED : 0; } +/** + * 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 * @@ -249,13 +286,13 @@ function media_delete($id,$auth){ * @author Michael Klier * @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) -- cgit v1.2.3