Commit 67111ffb2ece15eb41aa56205b180cc588a72bd4
1 parent
dd96c9a2
WSA-1
"Problems with upload.php" Fixed. Committed By: Conrad Vermeulen Reviewed By: Kevin Fourie git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7648 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
107 additions
and
42 deletions
ktwebservice/upload.php
| ... | ... | @@ -2,115 +2,110 @@ |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | 4 | * |
| 5 | - * $Id:$ | |
| 5 | + * $Id$ | |
| 6 | 6 | * |
| 7 | 7 | * This uploads a file onto the file server. A web service method can later move the file to the correct location. |
| 8 | 8 | * |
| 9 | 9 | * KnowledgeTree Open Source Edition |
| 10 | 10 | * Document Management Made Simple |
| 11 | 11 | * Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited |
| 12 | - * | |
| 12 | + * | |
| 13 | 13 | * This program is free software; you can redistribute it and/or modify it under |
| 14 | 14 | * the terms of the GNU General Public License version 3 as published by the |
| 15 | 15 | * Free Software Foundation. |
| 16 | - * | |
| 16 | + * | |
| 17 | 17 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 | 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | 19 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 20 | 20 | * details. |
| 21 | - * | |
| 21 | + * | |
| 22 | 22 | * You should have received a copy of the GNU General Public License |
| 23 | 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | - * | |
| 24 | + * | |
| 25 | 25 | * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place, |
| 26 | 26 | * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com. |
| 27 | - * | |
| 27 | + * | |
| 28 | 28 | * The interactive user interfaces in modified source and object code versions |
| 29 | 29 | * of this program must display Appropriate Legal Notices, as required under |
| 30 | 30 | * Section 5 of the GNU General Public License version 3. |
| 31 | - * | |
| 31 | + * | |
| 32 | 32 | * In accordance with Section 7(b) of the GNU General Public License version 3, |
| 33 | 33 | * these Appropriate Legal Notices must retain the display of the "Powered by |
| 34 | - * KnowledgeTree" logo and retain the original copyright notice. If the display of the | |
| 34 | + * KnowledgeTree" logo and retain the original copyright notice. If the display of the | |
| 35 | 35 | * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices |
| 36 | - * must display the words "Powered by KnowledgeTree" and retain the original | |
| 37 | - * copyright notice. | |
| 36 | + * must display the words "Powered by KnowledgeTree" and retain the original | |
| 37 | + * copyright notice. | |
| 38 | 38 | * Contributor( s): ______________________________________ |
| 39 | 39 | * |
| 40 | 40 | */ |
| 41 | 41 | |
| 42 | +$output = 'php'; | |
| 43 | +if (array_key_exists('output',$_POST)) | |
| 44 | +{ | |
| 45 | + $format = $_POST['output']; | |
| 46 | + switch($format) | |
| 47 | + { | |
| 48 | + case 'xml': | |
| 49 | + case 'json': | |
| 50 | + case 'php': | |
| 51 | + $output = $format; | |
| 52 | + break; | |
| 53 | + default: | |
| 54 | + // don't do anything - defaulting to php | |
| 55 | + } | |
| 56 | + unset($format); | |
| 57 | +} | |
| 58 | + | |
| 42 | 59 | // TODO: allow for linking of related documents. |
| 43 | 60 | |
| 44 | 61 | if (!array_key_exists('session_id',$_POST)) |
| 45 | 62 | { |
| 46 | - $msg = urlencode('Session not specified.'); | |
| 47 | - print "status_code=1&msg=$msg"; | |
| 48 | - exit; | |
| 63 | + respond(1, 'Session not specified.'); | |
| 49 | 64 | } |
| 50 | 65 | |
| 51 | - | |
| 52 | 66 | if (!array_key_exists('action',$_POST)) |
| 53 | 67 | { |
| 54 | - $msg = urlencode('Action not specified.'); | |
| 55 | - print "status_code=2&msg=$msg"; | |
| 56 | - exit; | |
| 68 | + respond(2, 'Action not specified.'); | |
| 57 | 69 | } |
| 58 | 70 | |
| 59 | 71 | $action = $_POST['action']; |
| 60 | - | |
| 61 | 72 | if (!in_array($action,array('C','A'))) |
| 62 | 73 | { |
| 63 | - $msg = urlencode('Invalid action specified.'); | |
| 64 | - print "status_code=3&msg=$msg"; | |
| 65 | - exit; | |
| 74 | + respond(3, 'Invalid action specified.'); | |
| 66 | 75 | } |
| 67 | 76 | |
| 68 | - | |
| 69 | 77 | $session_id = $_POST['session_id']; |
| 70 | - | |
| 71 | - | |
| 72 | - | |
| 73 | 78 | if (count($_FILES) == 0) |
| 74 | 79 | { |
| 75 | - $msg = urlencode('No files have been uploaded.'); | |
| 76 | - print "status_code=5&msg=$msg"; | |
| 77 | - exit; | |
| 80 | + respond(5, 'No files have been uploaded.'); | |
| 78 | 81 | } |
| 79 | 82 | |
| 80 | 83 | if ($action == 'C') |
| 81 | 84 | { |
| 82 | 85 | if (!array_key_exists('document_id',$_POST)) |
| 83 | 86 | { |
| 84 | - $msg = urlencode('document not specified.'); | |
| 85 | - print "status_code=6&msg=$msg"; | |
| 86 | - exit; | |
| 87 | + respond(6, 'Document ID not specified.'); | |
| 87 | 88 | } |
| 88 | 89 | $document_id = $_POST['document_id']; |
| 89 | 90 | } |
| 90 | 91 | |
| 91 | -//require_once('../config/dmsDefaults.php'); | |
| 92 | 92 | require_once('../ktapi/ktapi.inc.php'); |
| 93 | 93 | require_once('KTUploadManager.inc.php'); |
| 94 | 94 | |
| 95 | - | |
| 96 | 95 | $ktapi = new KTAPI(); |
| 97 | - | |
| 98 | 96 | $session = $ktapi->get_active_session($session_id); |
| 99 | - | |
| 100 | 97 | if (PEAR::isError($session)) |
| 101 | 98 | { |
| 102 | - $msg = urlencode($session->getMessage()); | |
| 103 | - print "status_code=4&msg=$msg"; | |
| 104 | - exit; | |
| 99 | + respond(4, $session->getMessage()); | |
| 105 | 100 | } |
| 106 | 101 | |
| 107 | 102 | $upload_manager = new KTUploadManager(); |
| 108 | - | |
| 109 | 103 | $upload_manager->cleanup(); |
| 110 | - | |
| 111 | 104 | $upload_manager->set_session($session); |
| 112 | 105 | |
| 106 | +$failed = 0; | |
| 113 | 107 | $added=array(); |
| 108 | +$lastMessage=''; | |
| 114 | 109 | foreach($_FILES as $key =>$file) |
| 115 | 110 | { |
| 116 | 111 | $filename=$file['name']; |
| ... | ... | @@ -122,6 +117,9 @@ foreach($_FILES as $key =>$file) |
| 122 | 117 | $result = $upload_manager->uploaded($filename, $tempfile, $action); |
| 123 | 118 | if (PEAR::isError($result)) |
| 124 | 119 | { |
| 120 | + $lastMessage=$result->getMessage(); | |
| 121 | + $default->log->error("Cannot upload file '$filename'. Temp location: '$tempfile'. " . $lastMessage); | |
| 122 | + $failed++; | |
| 125 | 123 | continue; |
| 126 | 124 | } |
| 127 | 125 | if ($result !== false) |
| ... | ... | @@ -129,11 +127,78 @@ foreach($_FILES as $key =>$file) |
| 129 | 127 | $file['tmp_name'] = $result; |
| 130 | 128 | $added[$key]=$file; |
| 131 | 129 | } |
| 130 | + else | |
| 131 | + { | |
| 132 | + $failed++; | |
| 133 | + } | |
| 132 | 134 | } |
| 133 | 135 | } |
| 134 | 136 | |
| 135 | -$added=urlencode(serialize($added)); | |
| 136 | -print "status_code=0&upload_status=$added"; | |
| 137 | +if ($failed) | |
| 138 | +{ | |
| 139 | + respond(7, 'Could not add files to the system. Please inspect the log file. ' . $lastMessage); | |
| 140 | +} | |
| 141 | +else | |
| 142 | +{ | |
| 143 | + respond(0, '', $added); | |
| 144 | +} | |
| 145 | + | |
| 146 | +function respond($code, $msg, $uploads=array()) | |
| 147 | +{ | |
| 148 | + global $output; | |
| 149 | + | |
| 150 | + | |
| 151 | + $response =array( | |
| 152 | + 'status_code'=>$code, | |
| 153 | + 'msg'=>$msg, | |
| 154 | + 'upload_status'=>$uploads | |
| 155 | + ); | |
| 156 | + | |
| 157 | + switch($output) | |
| 158 | + { | |
| 159 | + case 'xml': | |
| 160 | + $xml = "<response>\n"; | |
| 161 | + $xml .= "\t<status_code>$code</status_code>\n"; | |
| 162 | + $xml .= "\t<msg>$msg</msg>\n"; | |
| 163 | + $xml .= "\t<upload_status>\n"; | |
| 164 | + $i=0; | |
| 165 | + foreach($uploads as $key=>$value) | |
| 166 | + { | |
| 167 | + $servername = $value['tmp_name']; | |
| 168 | + $filesize = $value['size']; | |
| 169 | + $error = $value['error']; | |
| 170 | + $name = $value['name']; | |
| 171 | + $xml .= "\t\t<file>\n"; | |
| 172 | + $xml .= "\t\t\t<offset>$i</offset>\n"; | |
| 173 | + $xml .= "\t\t\t<name>$name</name>\n"; | |
| 174 | + $xml .= "\t\t\t<filename>$servername</filename>\n"; | |
| 175 | + $xml .= "\t\t\t<filesize>$filesize</filesize>\n"; | |
| 176 | + $xml .= "\t\t\t<error>$error</error>\n"; | |
| 177 | + $xml .= "\t\t</file>\n"; | |
| 178 | + $i++; | |
| 179 | + } | |
| 180 | + $xml .= "\t</upload_status>\n"; | |
| 181 | + $xml .= "</response>\n"; | |
| 182 | + print $xml; | |
| 183 | + exit; | |
| 184 | + case 'json': | |
| 185 | + print json_encode($response); | |
| 186 | + exit; | |
| 187 | + case 'php': | |
| 188 | + default: | |
| 189 | + $msg = urlencode($msg); | |
| 190 | + $response['upload_status'] = serialize($response['upload_status']); | |
| 191 | + $str = ''; | |
| 192 | + $i=0; | |
| 193 | + foreach($response as $key=>$value) | |
| 194 | + { | |
| 195 | + if ($i++>0) $str .= '&'; | |
| 196 | + $str .= "$key=$value"; | |
| 197 | + } | |
| 198 | + print $str; | |
| 199 | + exit; | |
| 200 | + } | |
| 201 | +} | |
| 137 | 202 | |
| 138 | 203 | |
| 139 | 204 | ?> |
| 140 | 205 | \ No newline at end of file | ... | ... |