From 67111ffb2ece15eb41aa56205b180cc588a72bd4 Mon Sep 17 00:00:00 2001 From: Conrad Vermeulen Date: Thu, 8 Nov 2007 17:01:32 +0000 Subject: [PATCH] WSA-1 "Problems with upload.php" Fixed. --- ktwebservice/upload.php | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ 1 file changed, 107 insertions(+), 42 deletions(-) diff --git a/ktwebservice/upload.php b/ktwebservice/upload.php index f13d0a6..c967de8 100644 --- a/ktwebservice/upload.php +++ b/ktwebservice/upload.php @@ -2,115 +2,110 @@ /** * - * $Id:$ + * $Id$ * * This uploads a file onto the file server. A web service method can later move the file to the correct location. * * KnowledgeTree Open Source Edition * Document Management Made Simple * Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited - * + * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * + * * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place, * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com. - * + * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. - * + * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by - * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices - * must display the words "Powered by KnowledgeTree" and retain the original - * copyright notice. + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. * Contributor( s): ______________________________________ * */ +$output = 'php'; +if (array_key_exists('output',$_POST)) +{ + $format = $_POST['output']; + switch($format) + { + case 'xml': + case 'json': + case 'php': + $output = $format; + break; + default: + // don't do anything - defaulting to php + } + unset($format); +} + // TODO: allow for linking of related documents. if (!array_key_exists('session_id',$_POST)) { - $msg = urlencode('Session not specified.'); - print "status_code=1&msg=$msg"; - exit; + respond(1, 'Session not specified.'); } - if (!array_key_exists('action',$_POST)) { - $msg = urlencode('Action not specified.'); - print "status_code=2&msg=$msg"; - exit; + respond(2, 'Action not specified.'); } $action = $_POST['action']; - if (!in_array($action,array('C','A'))) { - $msg = urlencode('Invalid action specified.'); - print "status_code=3&msg=$msg"; - exit; + respond(3, 'Invalid action specified.'); } - $session_id = $_POST['session_id']; - - - if (count($_FILES) == 0) { - $msg = urlencode('No files have been uploaded.'); - print "status_code=5&msg=$msg"; - exit; + respond(5, 'No files have been uploaded.'); } if ($action == 'C') { if (!array_key_exists('document_id',$_POST)) { - $msg = urlencode('document not specified.'); - print "status_code=6&msg=$msg"; - exit; + respond(6, 'Document ID not specified.'); } $document_id = $_POST['document_id']; } -//require_once('../config/dmsDefaults.php'); require_once('../ktapi/ktapi.inc.php'); require_once('KTUploadManager.inc.php'); - $ktapi = new KTAPI(); - $session = $ktapi->get_active_session($session_id); - if (PEAR::isError($session)) { - $msg = urlencode($session->getMessage()); - print "status_code=4&msg=$msg"; - exit; + respond(4, $session->getMessage()); } $upload_manager = new KTUploadManager(); - $upload_manager->cleanup(); - $upload_manager->set_session($session); +$failed = 0; $added=array(); +$lastMessage=''; foreach($_FILES as $key =>$file) { $filename=$file['name']; @@ -122,6 +117,9 @@ foreach($_FILES as $key =>$file) $result = $upload_manager->uploaded($filename, $tempfile, $action); if (PEAR::isError($result)) { + $lastMessage=$result->getMessage(); + $default->log->error("Cannot upload file '$filename'. Temp location: '$tempfile'. " . $lastMessage); + $failed++; continue; } if ($result !== false) @@ -129,11 +127,78 @@ foreach($_FILES as $key =>$file) $file['tmp_name'] = $result; $added[$key]=$file; } + else + { + $failed++; + } } } -$added=urlencode(serialize($added)); -print "status_code=0&upload_status=$added"; +if ($failed) +{ + respond(7, 'Could not add files to the system. Please inspect the log file. ' . $lastMessage); +} +else +{ + respond(0, '', $added); +} + +function respond($code, $msg, $uploads=array()) +{ + global $output; + + + $response =array( + 'status_code'=>$code, + 'msg'=>$msg, + 'upload_status'=>$uploads + ); + + switch($output) + { + case 'xml': + $xml = "\n"; + $xml .= "\t$code\n"; + $xml .= "\t$msg\n"; + $xml .= "\t\n"; + $i=0; + foreach($uploads as $key=>$value) + { + $servername = $value['tmp_name']; + $filesize = $value['size']; + $error = $value['error']; + $name = $value['name']; + $xml .= "\t\t\n"; + $xml .= "\t\t\t$i\n"; + $xml .= "\t\t\t$name\n"; + $xml .= "\t\t\t$servername\n"; + $xml .= "\t\t\t$filesize\n"; + $xml .= "\t\t\t$error\n"; + $xml .= "\t\t\n"; + $i++; + } + $xml .= "\t\n"; + $xml .= "\n"; + print $xml; + exit; + case 'json': + print json_encode($response); + exit; + case 'php': + default: + $msg = urlencode($msg); + $response['upload_status'] = serialize($response['upload_status']); + $str = ''; + $i=0; + foreach($response as $key=>$value) + { + if ($i++>0) $str .= '&'; + $str .= "$key=$value"; + } + print $str; + exit; + } +} ?> \ No newline at end of file -- libgit2 0.21.4