Commit 1ed9ca498f9fa9eb51c648c32513feaf7843a20e

Authored by Megan Watson
1 parent 9f554638

Updated the file upload to allow a unique file id to identify the upload

PT: 1940148

Committed by: Megan Watson
config/dmsDefaults.php
... ... @@ -7,7 +7,7 @@
7 7 * KnowledgeTree Community Edition
8 8 * Document Management Made Simple
9 9 * Copyright (C) 2008, 2009 KnowledgeTree Inc.
10   - *
  10 + *
11 11 *
12 12 * This program is free software; you can redistribute it and/or modify it under
13 13 * the terms of the GNU General Public License version 3 as published by the
... ... @@ -47,7 +47,7 @@ if (defined('DMS_DEFAULTS_INCLUDED'))
47 47 }
48 48  
49 49 define('DMS_DEFAULTS_INCLUDED',1);
50   -define('LATEST_WEBSERVICE_VERSION',2);
  50 +define('LATEST_WEBSERVICE_VERSION',3);
51 51  
52 52  
53 53 if (function_exists('apd_set_pprof_trace')) {
... ... @@ -483,7 +483,7 @@ class KTInit {
483 483 function initConfig() {
484 484 global $default;
485 485 $oKTConfig = KTConfig::getSingleton();
486   -
  486 +
487 487 // Override the config setting - KT_DIR is resolved on page load
488 488 $oKTConfig->setdefaultns('KnowledgeTree', 'fileSystemRoot', KT_DIR);
489 489  
... ...
ktwebservice/KTUploadManager.inc.php
... ... @@ -9,7 +9,7 @@
9 9 * KnowledgeTree Community Edition
10 10 * Document Management Made Simple
11 11 * Copyright (C) 2008, 2009 KnowledgeTree Inc.
12   - *
  12 + *
13 13 *
14 14 * This program is free software; you can redistribute it and/or modify it under
15 15 * the terms of the GNU General Public License version 3 as published by the
... ... @@ -110,7 +110,7 @@ class KTUploadManager
110 110 return ($tempdir == $this->temp_dir);
111 111 */
112 112 }
113   -
  113 +
114 114 function store_base64_file($base64, $prefix= 'sa_')
115 115 {
116 116 $tempfilename = $this->get_temp_filename($prefix);
... ... @@ -134,9 +134,9 @@ class KTUploadManager
134 134  
135 135 return $tempfilename;
136 136 }
137   -
  137 +
138 138 /**
139   - *
  139 + *
140 140 * @param string $content file content NOT base64 encoded (may be string, may be binary)
141 141 * @param string $prefix [optional]
142 142 * @return $tempfilename the name of the temporary file created
... ... @@ -172,7 +172,7 @@ class KTUploadManager
172 172 * @param string $tempfile
173 173 * @param string $action
174 174 */
175   - function uploaded($filename, $tempfile, $action, $relatedid = null)
  175 + function uploaded($filename, $tempfile, $action, $unique_file_id = null)
176 176 {
177 177 $filename=basename($filename);
178 178 $now=date('Y-m-d H:i:s');
... ... @@ -190,6 +190,11 @@ class KTUploadManager
190 190 $newtempfile = str_replace('\\','/',$newtempfile);
191 191 }
192 192  
  193 + if(!empty($unique_file_id) && !$this->check_unique_id($unique_file_id)){
  194 + // If the unique_file_id is not unique then return an error
  195 + return PEAR::raiseError(_kt('Unique file id already exists.'));
  196 + }
  197 +
193 198 DBUtil::startTransaction();
194 199 $id = DBUtil::autoInsert('uploaded_files',
195 200 array(
... ... @@ -198,7 +203,7 @@ class KTUploadManager
198 203 'userid'=>$_SESSION['userID'],
199 204 'uploaddate'=>$now,
200 205 'action'=>$action,
201   - // 'related_uploadid'=>$relatedid
  206 + 'unique_file_id'=>$unique_file_id
202 207 ),
203 208 array('noid'=>true)
204 209 );
... ... @@ -232,6 +237,42 @@ class KTUploadManager
232 237 }
233 238  
234 239 /**
  240 + * Ensure the unique file id is unique for the uploaded file
  241 + *
  242 + * @param string $unique_file_id
  243 + * @return bool
  244 + */
  245 + private function check_unique_id($unique_file_id)
  246 + {
  247 + $unique = addslashes($unique_file_id);
  248 + $sql = "SELECT tempfilename FROM uploaded_files WHERE unique_file_id = '$unique'";
  249 + $result = DBUtil::getResultArray($sql);
  250 +
  251 + if(PEAR::isError($result) || empty($result)){
  252 + return true;
  253 + }
  254 +
  255 + return false;
  256 + }
  257 +
  258 + function get_tempfile_from_unique_id($unique_file_id)
  259 + {
  260 + $unique = addslashes($unique_file_id);
  261 + $sql = "SELECT tempfilename FROM uploaded_files WHERE unique_file_id = '$unique'";
  262 + $result = DBUtil::getResultArray($sql);
  263 +
  264 + if(PEAR::isError($result)){
  265 + return $result;
  266 + }
  267 +
  268 + if(empty($result)){
  269 + PEAR::raiseError(_kt('No file has been uploaded with the unique file id: ').$unique_file_id);
  270 + }
  271 +
  272 + return $result[0]['tempfilename'];
  273 + }
  274 +
  275 + /**
235 276 * This is a list of all all managed files.
236 277 *
237 278 * @param string $action
... ...
ktwebservice/upload.php
... ... @@ -9,7 +9,7 @@
9 9 * KnowledgeTree Community Edition
10 10 * Document Management Made Simple
11 11 * Copyright (C) 2008, 2009 KnowledgeTree Inc.
12   - *
  12 + *
13 13 *
14 14 * This program is free software; you can redistribute it and/or modify it under
15 15 * the terms of the GNU General Public License version 3 as published by the
... ... @@ -70,6 +70,11 @@ if (!array_key_exists('action',$_POST))
70 70 respond(2, 'Action not specified.');
71 71 }
72 72  
  73 +$unique_file_id = false;
  74 +if(array_key_exists('unique_file_id', $_POST)){
  75 + $unique_file_id = $_POST['unique_file_id'];
  76 +}
  77 +
73 78 $action = $_POST['action'];
74 79 if (!in_array($action,array('C','A')))
75 80 {
... ... @@ -77,11 +82,16 @@ if (!in_array($action,array('C','A')))
77 82 }
78 83  
79 84 //$session_id = $_POST['session_id'];
80   -if (count($_FILES) == 0)
  85 +$file_count = count($_FILES);
  86 +if ($file_count == 0)
81 87 {
82 88 respond(5, 'No files have been uploaded.');
83 89 }
84 90  
  91 +if($file_count > 1 && $unique_file_id !== false){
  92 + respond(5, 'Only one file can be uploaded with a unique file id.');
  93 +}
  94 +
85 95 if ($action == 'C')
86 96 {
87 97 if (!array_key_exists('document_id',$_POST))
... ... @@ -120,7 +130,7 @@ foreach($_FILES as $key =>$file)
120 130 $extra = $filename.'-'.$tempfile.'-'.$error;
121 131 if ($error == UPLOAD_ERR_OK)
122 132 {
123   - $result = $upload_manager->uploaded($filename, $tempfile, $action);
  133 + $result = $upload_manager->uploaded($filename, $tempfile, $action, $unique_file_id);
124 134 if (PEAR::isError($result))
125 135 {
126 136 $lastMessage=$result->getMessage();
... ...
ktwebservice/webservice.php
... ... @@ -9,7 +9,7 @@
9 9 * KnowledgeTree Community Edition
10 10 * Document Management Made Simple
11 11 * Copyright (C) 2008, 2009 KnowledgeTree Inc.
12   - *
  12 + *
13 13 *
14 14 * This program is free software; you can redistribute it and/or modify it under
15 15 * the terms of the GNU General Public License version 3 as published by the
... ... @@ -97,7 +97,7 @@ define('KTWS_ERR_DB_PROBLEM', 99);
97 97  
98 98 if (!defined('LATEST_WEBSERVICE_VERSION'))
99 99 {
100   - define('LATEST_WEBSERVICE_VERSION',2);
  100 + define('LATEST_WEBSERVICE_VERSION', 3);
101 101 }
102 102  
103 103 function bool2str($bool)
... ... @@ -435,7 +435,7 @@ class KTWebService
435 435 'item' => "{urn:$this->namespace}kt_metadata_selection_item"
436 436 )
437 437 );
438   -
  438 +
439 439 $this->__typedef["{urn:$this->namespace}kt_metadata_options"] =
440 440 array(
441 441 'ishtml' => 'string',
... ... @@ -950,6 +950,20 @@ class KTWebService
950 950 );
951 951 }
952 952  
  953 + if($this->version >= 3)
  954 + {
  955 + // add_document
  956 + $this->__dispatch_map['add_document'] =
  957 + array('in' => array('session_id'=>'string','folder_id'=>'int','title'=>'string','filename'=>'string','documentype' =>'string','tempfilename' =>'string', 'unique_file_id' => 'string' ),
  958 + 'out' => array( 'return' => "{urn:$this->namespace}kt_document_detail" ),
  959 + );
  960 +
  961 + $this->__dispatch_map['add_document_with_metadata'] =
  962 + array('in' => array('session_id'=>'string','folder_id'=>'int','title'=>'string','filename'=>'string','documentype' =>'string','tempfilename' =>'string', 'metadata'=>"{urn:$this->namespace}kt_metadata_fieldsets",'sysdata'=>"{urn:$this->namespace}kt_sysdata", 'unique_file_id' => 'string' ),
  963 + 'out' => array( 'return' => "{urn:$this->namespace}kt_document_detail" )
  964 + );
  965 + }
  966 +
953 967 // get_document_detail_by_name
954 968 $this->__dispatch_map['get_document_detail_by_name'] =
955 969 array('in' => array('session_id' => 'string', 'document_name' => 'string', 'what'=>'string' ),
... ... @@ -2260,8 +2274,20 @@ class KTWebService
2260 2274 * @param string $tempfilename
2261 2275 * @return kt_document_detail. status_code can be KTWS_ERR_INVALID_SESSION, KTWS_ERR_INVALID_FOLDER, KTWS_ERR_INVALID_DOCUMENT or KTWS_SUCCESS
2262 2276 */
2263   - function add_document($session_id, $folder_id, $title, $filename, $documenttype, $tempfilename)
  2277 + function add_document($session_id, $folder_id, $title, $filename, $documenttype, $tempfilename, $unique_file_id = null)
2264 2278 {
  2279 + if(empty($tempfilename)){
  2280 + $upload_manager = new KTUploadManager();
  2281 + $tempfilename = $upload_manager->get_tempfile_from_unique_id($unique_file_id);
  2282 +
  2283 + if (PEAR::isError($tempfilename))
  2284 + {
  2285 + $response = KTWebService::_status(KTWS_ERR_INVALID_DOCUMENT, "Invalid unique file id: {$tempfilename->getMessage()}.");
  2286 + $this->debug("add_document - cannot add document - " . $tempfilename->getMessage(), $session_id);
  2287 + return new SOAP_Value('return',"{urn:$this->namespace}kt_document_detail", $response);
  2288 + }
  2289 + }
  2290 +
2265 2291 $this->debug("add_document('$session_id',$folder_id,'$title','$filename','$documenttype','$tempfilename')");
2266 2292 $kt = &$this->get_ktapi($session_id );
2267 2293 if (is_array($kt))
... ... @@ -2347,9 +2373,9 @@ class KTWebService
2347 2373 return $update_result;
2348 2374 }
2349 2375  
2350   - function add_document_with_metadata($session_id, $folder_id, $title, $filename, $documenttype, $tempfilename, $metadata, $sysdata)
  2376 + function add_document_with_metadata($session_id, $folder_id, $title, $filename, $documenttype, $tempfilename, $metadata, $sysdata, $unique_file_id = null)
2351 2377 {
2352   - $add_result = $this->add_document($session_id, $folder_id, $title, $filename, $documenttype, $tempfilename);
  2378 + $add_result = $this->add_document($session_id, $folder_id, $title, $filename, $documenttype, $tempfilename, $unique_file_id);
2353 2379  
2354 2380 $status_code = $add_result->value['status_code'];
2355 2381 if ($status_code != 0)
... ...
sql/mysql/install/data.sql
... ... @@ -1778,7 +1778,8 @@ INSERT INTO `upgrades` VALUES
1778 1778 (232,'upgrade*3.7.0.1*99*upgrade3.7.0.1','Upgrade from version 3.6.3 to 3.7.0.1','2009-11-13 00:00:00',1,'upgrade*3.7.0.1*99*upgrade3.7.0.1'),
1779 1779 (233,'sql*3.7.0.2*0*3.7.0.2/processor_queue.sql','Database upgrade to version 3.7.0.1: Processor Queue','2009-09-01 00:00:00',1,'upgrade*3.7.0.2*99*upgrade3.7.0.2'),
1780 1780 (234,'upgrade*3.7.0.2*99*upgrade3.7.0.2','Upgrade from version 3.7.0.1 to 3.7.0.2','2009-11-19 00:00:00',1,'upgrade*3.7.0.2*99*upgrade3.7.0.2'),
1781   -(233,'sql*3.7.0.3*0*3.7.0.3/clienttools_config.sql','Database upgrade to version 3.7.0.3: Clienttools Config','2009-12-10 00:00:00',1,'upgrade*3.7.0.3*99*upgrade3.7.0.3');
  1781 +(235,'sql*3.7.0.3*0*3.7.0.3/clienttools_config.sql','Database upgrade to version 3.7.0.3: Clienttools Config','2009-12-10 00:00:00',1,'upgrade*3.7.0.3*99*upgrade3.7.0.3'),
  1782 +(236,'sql*3.7.0.3*0*3.7.0.3/uploaded_files.sql','Database upgrade to version 3.7.0.3: Uploaded Files','2009-12-10 00:00:00',1,'upgrade*3.7.0.3*99*upgrade3.7.0.3');
1782 1783 /*!40000 ALTER TABLE `upgrades` ENABLE KEYS */;
1783 1784 UNLOCK TABLES;
1784 1785  
... ...
sql/mysql/install/structure.sql
... ... @@ -1591,6 +1591,7 @@ CREATE TABLE `uploaded_files` (
1591 1591 `uploaddate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
1592 1592 `action` char(1) NOT NULL COMMENT 'A = Add, C = Checkin',
1593 1593 `document_id` int(11) default NULL,
  1594 + `unique_file_id` varchar(30),
1594 1595 PRIMARY KEY (`tempfilename`),
1595 1596 KEY `userid` (`userid`),
1596 1597 KEY `document_id` (`document_id`),
... ...
sql/mysql/upgrade/3.7.0.3/uploaded_files.sql 0 → 100644
  1 +ALTER TABLE `uploaded_files` ADD COLUMN `unique_file_id` varchar(30);
0 2 \ No newline at end of file
... ...