Commit a1e23ec9f0690b4e0552822d0caf89b241a63eb1

Authored by Conrad Vermeulen
1 parent a1a0a11b

KTS-2129

"Add get_document_type_metadata() to webservice"
Fixed. Also added links as KTS-2130.

Reviewed By: Kevin Fourie

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@6852 c91229c3-7414-0410-bfa2-8a42b809f60b
ktapi/KTAPIConstants.inc.php
1 1 <?
2 2 /**
3 3 * $Id$
4   - *
  4 + *
5 5 * The contents of this file are subject to the KnowledgeTree Public
6 6 * License Version 1.1.2 ("License"); You may not use this file except in
7 7 * compliance with the License. You may obtain a copy of the License at
8 8 * http://www.knowledgetree.com/KPL
9   - *
  9 + *
10 10 * Software distributed under the License is distributed on an "AS IS"
11 11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
12 12 * See the License for the specific language governing rights and
... ... @@ -17,9 +17,9 @@
17 17 * (ii) the KnowledgeTree copyright notice
18 18 * in the same form as they appear in the distribution. See the License for
19 19 * requirements.
20   - *
  20 + *
21 21 * The Original Code is: KnowledgeTree Open Source
22   - *
  22 + *
23 23 * The Initial Developer of the Original Code is The Jam Warehouse Software
24 24 * (Pty) Ltd, trading as KnowledgeTree.
25 25 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -31,8 +31,8 @@
31 31  
32 32 // Generic error messages used in the API. There may be some others specific to functionality
33 33 // directly in the code.
34   -// TODO: Check that they are all relevant.
35   -
  34 +// TODO: Check that they are all relevant.
  35 +
36 36 define('KTAPI_ERROR_SESSION_INVALID', 'The session could not be resolved.');
37 37 define('KTAPI_ERROR_PERMISSION_INVALID', 'The permission could not be resolved.');
38 38 define('KTAPI_ERROR_FOLDER_INVALID', 'The folder could not be resolved.');
... ... @@ -46,6 +46,7 @@ define(&#39;KTAPI_ERROR_DOCUMENT_CHECKED_OUT&#39;, &#39;The document is checked out.&#39;);
46 46 define('KTAPI_ERROR_DOCUMENT_NOT_CHECKED_OUT', 'The document is not checked out.');
47 47 define('KTAPI_ERROR_WORKFLOW_INVALID', 'The workflow could not be resolved.');
48 48 define('KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS', 'The workflow is not in progress.');
  49 +define('KTAPI_ERROR_DOCUMENT_LINK_TYPE_INVALID','The link type could not be resolved.');
49 50  
50 51 // Mapping of permissions to actions.
51 52 // TODO: Check that they are all correct.
... ... @@ -53,7 +54,7 @@ define(&#39;KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS&#39;, &#39;The workflow is not in progress
53 54 // As the permissions are currently associated with actions which are quite closely linked
54 55 // to the web interface, it is not the nicest way to do things. They should be associated at
55 56 // a lower level, such as in the api. probably, better, would be at some stage to assocate
56   -// the permissions to the action/transaction in the database so administrators can really customise
  57 +// the permissions to the action/transaction in the database so administrators can really customise
57 58 // as required.
58 59  
59 60 define('KTAPI_PERMISSION_DELETE', 'ktcore.permissions.delete');
... ...
ktapi/KTAPIDocument.inc.php
... ... @@ -6,7 +6,7 @@
6 6 * License Version 1.1.2 ("License"); You may not use this file except in
7 7 * compliance with the License. You may obtain a copy of the License at
8 8 * http://www.knowledgetree.com/KPL
9   - *
  9 + *
10 10 * Software distributed under the License is distributed on an "AS IS"
11 11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
12 12 * See the License for the specific language governing rights and
... ... @@ -17,9 +17,9 @@
17 17 * (ii) the KnowledgeTree copyright notice
18 18 * in the same form as they appear in the distribution. See the License for
19 19 * requirements.
20   - *
  20 + *
21 21 * The Original Code is: KnowledgeTree Open Source
22   - *
  22 + *
23 23 * The Initial Developer of the Original Code is The Jam Warehouse Software
24 24 * (Pty) Ltd, trading as KnowledgeTree.
25 25 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -29,14 +29,14 @@
29 29 *
30 30 */
31 31  
32   -class KTAPI_Document extends KTAPI_FolderItem
  32 +class KTAPI_Document extends KTAPI_FolderItem
33 33 {
34 34 /**
35 35 * This is a reference to the internal document object.
36 36 *
37 37 * @var Document
38 38 */
39   - var $document;
  39 + var $document;
40 40 /**
41 41 * This is the id of the document.
42 42 *
... ... @@ -49,16 +49,16 @@ class KTAPI_Document extends KTAPI_FolderItem
49 49 * @var KTAPI_Folder
50 50 */
51 51 var $ktapi_folder;
52   -
  52 +
53 53 function get_documentid()
54 54 {
55 55 return $this->documentid;
56   - }
57   -
  56 + }
  57 +
58 58 /**
59 59 * This is used to get a document based on document id.
60 60 *
61   - * @static
  61 + * @static
62 62 * @access public
63 63 * @param KTAPI $ktapi
64 64 * @param int $documentid
... ... @@ -69,42 +69,42 @@ class KTAPI_Document extends KTAPI_FolderItem
69 69 assert(!is_null($ktapi));
70 70 assert(is_a($ktapi, 'KTAPI'));
71 71 assert(is_numeric($documentid));
72   -
  72 +
73 73 $documentid += 0;
74   -
  74 +
75 75 $document = &Document::get($documentid);
76 76 if (is_null($document) || PEAR::isError($document))
77 77 {
78 78 return new KTAPI_Error(KTAPI_ERROR_DOCUMENT_INVALID,$document );
79 79 }
80   -
  80 +
81 81 $user = $ktapi->can_user_access_object_requiring_permission($document, KTAPI_PERMISSION_READ);
82   -
  82 +
83 83 if (is_null($user) || PEAR::isError($user))
84 84 {
85 85 return $user;
86   - }
87   -
  86 + }
  87 +
88 88 $folderid = $document->getParentID();
89 89  
90 90 if (!is_null($folderid))
91 91 {
92 92 $ktapi_folder = &KTAPI_Folder::get($ktapi, $folderid);
93 93 }
94   - else
  94 + else
95 95 {
96 96 $ktapi_folder = null;
97 97 }
98 98 // We don't do any checks on this folder as it could possibly be deleted, and is not required right now.
99 99  
100 100 return new KTAPI_Document($ktapi, $ktapi_folder, $document);
101   - }
102   -
  101 + }
  102 +
103 103 function is_deleted()
104 104 {
105 105 return ($this->document->getStatusID() == 3);
106 106 }
107   -
  107 +
108 108 /**
109 109 * This is the constructor for the KTAPI_Folder.
110 110 *
... ... @@ -112,18 +112,18 @@ class KTAPI_Document extends KTAPI_FolderItem
112 112 * @param KTAPI $ktapi
113 113 * @param Document $document
114 114 * @return KTAPI_Document
115   - */
  115 + */
116 116 function KTAPI_Document(&$ktapi, &$ktapi_folder, &$document)
117 117 {
118 118 assert(is_a($ktapi,'KTAPI'));
119 119 assert(is_null($ktapi_folder) || is_a($ktapi_folder,'KTAPI_Folder'));
120   -
  120 +
121 121 $this->ktapi = &$ktapi;
122 122 $this->ktapi_folder = &$ktapi_folder;
123 123 $this->document = &$document;
124 124 $this->documentid = $document->getId();
125 125 }
126   -
  126 +
127 127 /**
128 128 * This checks a document into the repository
129 129 *
... ... @@ -131,21 +131,21 @@ class KTAPI_Document extends KTAPI_FolderItem
131 131 * @param string $reason
132 132 * @param string $tempfilename
133 133 * @param bool $major_update
134   - */
  134 + */
135 135 function checkin($filename, $reason, $tempfilename, $major_update=false)
136   - {
  136 + {
137 137 if (!is_file($tempfilename))
138 138 {
139 139 return new PEAR_Error('File does not exist.');
140 140 }
141   -
  141 +
142 142 $user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
143   -
  143 +
144 144 if (PEAR::isError($user))
145 145 {
146 146 return $user;
147 147 }
148   -
  148 +
149 149 if (!$this->document->getIsCheckedOut())
150 150 {
151 151 return new PEAR_Error(KTAPI_ERROR_DOCUMENT_NOT_CHECKED_OUT);
... ... @@ -168,23 +168,63 @@ class KTAPI_Document extends KTAPI_FolderItem
168 168 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$result);
169 169 }
170 170 DBUtil::commit();
171   -
  171 +
172 172 $tempfilename=addslashes($tempfilename);
173 173 $sql = "DELETE FROM uploaded_files WHERE tempfilename='$tempfilename'";
174   - $result = DBUtil::runQuery($sql);
  174 + $result = DBUtil::runQuery($sql);
175 175 if (PEAR::isError($result))
176 176 {
177 177 return $result;
178   - }
179   -
  178 + }
  179 +
  180 + }
  181 +
  182 + /**
  183 + * Link a document to another
  184 + *
  185 + * @param KTAPI_Document $document
  186 + */
  187 + function link_document($document, $type)
  188 + {
  189 + $typeid = $this->ktapi->get_link_type_id($type);
  190 + if (PEAR::isError($typeid))
  191 + {
  192 + return $result;
  193 + }
  194 +
  195 + $link = new DocumentLink($this->get_documentid(), $document->get_documentid(), $typeid );
  196 + $created = $link->create();
  197 + if ($created === false || PEAR::isError($created))
  198 + {
  199 + return new PEAR_Error(_kt('Could not create link'));
  200 + }
  201 + }
  202 +
  203 + /**
  204 + * Unlink a document to another
  205 + *
  206 + * @param KTAPI_Document $document
  207 + */
  208 + function unlink_document($document)
  209 + {
  210 + $sql = "DELETE FROM document_link WHERE parent_document_id=$this->documentid AND child_document_id=$document->documentid";
  211 + $result = DBUtil::runQuery($sql);
  212 + if (empty($result) || PEAR::isError($created))
  213 + {
  214 + return new PEAR_Error(_kt('Could not remove link'));
  215 + }
180 216 }
181   -
182   -
  217 +
  218 +
  219 + /**
  220 + *
  221 + * @return boolean
  222 + */
183 223 function is_checked_out()
184 224 {
185   - return ($this->document->getIsCheckedOut());
  225 + return ($this->document->getIsCheckedOut());
186 226 }
187   -
  227 +
188 228 /**
189 229 * This reverses the checkout process.
190 230 *
... ... @@ -193,7 +233,7 @@ class KTAPI_Document extends KTAPI_FolderItem
193 233 function undo_checkout($reason)
194 234 {
195 235 $user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE);
196   -
  236 +
197 237 if (PEAR::isError($user))
198 238 {
199 239 return $user;
... ... @@ -214,9 +254,9 @@ class KTAPI_Document extends KTAPI_FolderItem
214 254 DBUtil::rollback();
215 255 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res);
216 256 }
217   -
  257 +
218 258 $oDocumentTransaction = & new DocumentTransaction($this->document, $reason, 'ktcore.transactions.force_checkin');
219   -
  259 +
220 260 $res = $oDocumentTransaction->create();
221 261 if (($res === false) || PEAR::isError($res)) {
222 262 DBUtil::rollback();
... ... @@ -225,6 +265,63 @@ class KTAPI_Document extends KTAPI_FolderItem
225 265 DBUtil::commit();
226 266 }
227 267  
  268 + function get_linked_documents()
  269 + {
  270 + $sql = "
  271 + SELECT
  272 + dl.child_document_id as document_id,
  273 + dmv.name as title,
  274 + dcv.size,
  275 + w.name as workflow,
  276 + ws.name as workflow_state,
  277 + dlt.name as link_type
  278 + FROM
  279 + document_link dl
  280 + INNER JOIN document_link_types dlt ON dl.link_type_id=dlt.id
  281 + INNER JOIN documents d ON dl.child_document_id=d.id
  282 + INNER JOIN document_metadata_version dmv ON d.metadata_version_id=dmv.id
  283 + INNER JOIN document_content_version dcv ON dmv.content_version_id=dcv.id
  284 + LEFT OUTER JOIN workflow_documents wd ON d.id=wd.document_id
  285 + LEFT OUTER JOIN workflows w ON w.id=wd.workflow_id
  286 + LEFT OUTER JOIN workflow_states ws ON wd.state_id=ws.id
  287 + WHERE
  288 + dl.parent_document_id=$this->documentid
  289 +
  290 + ";
  291 + $rows = DBUtil::getResultArray($sql);
  292 + if (PEAR::isError($rows))
  293 + {
  294 + return $rows;
  295 + }
  296 + $result=array();
  297 + $read_permission = &KTPermission::getByName(KTAPI_PERMISSION_READ);
  298 + $user = $this->ktapi->get_user();
  299 +
  300 + foreach($rows as $row)
  301 + {
  302 + $document = Document::get($row['document_id']);
  303 + if (PEAR::isError($document) || is_null($document))
  304 + {
  305 + continue;
  306 + }
  307 + if(!KTPermissionUtil::userHasPermissionOnItem($user, $read_permission, $document))
  308 + {
  309 + continue;
  310 + }
  311 + $result[] = array(
  312 + 'document_id'=>(int)$row['document_id'],
  313 + 'title'=> $row['title'],
  314 + 'size'=>(int)$row['size'],
  315 + 'workflow'=>$row['workflow'],
  316 + 'workflow_state'=>$row['workflow_state'],
  317 + 'link_type'=>$row['link_type'],
  318 + );
  319 + }
  320 +
  321 + return $result;
  322 + }
  323 +
  324 +
228 325 /**
229 326 * This returns a URL to the file that can be downloaded.
230 327 *
... ... @@ -238,7 +335,7 @@ class KTAPI_Document extends KTAPI_FolderItem
238 335 {
239 336 return $user;
240 337 }
241   -
  338 +
242 339 if ($this->document->getIsCheckedOut())
243 340 {
244 341 return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT);
... ... @@ -254,7 +351,7 @@ class KTAPI_Document extends KTAPI_FolderItem
254 351  
255 352 DBUtil::commit();
256 353 }
257   -
  354 +
258 355 /**
259 356 * This deletes a document from the folder.
260 357 *
... ... @@ -284,12 +381,12 @@ class KTAPI_Document extends KTAPI_FolderItem
284 381  
285 382 DBUtil::commit();
286 383 }
287   -
  384 +
288 385 /**
289 386 * This changes the owner of the file.
290 387 *
291 388 * @param string $ktapi_newuser
292   - */
  389 + */
293 390 function change_owner($newusername, $reason='Changing of owner.')
294 391 {
295 392 $user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_CHANGE_OWNERSHIP);
... ... @@ -297,46 +394,46 @@ class KTAPI_Document extends KTAPI_FolderItem
297 394 if (PEAR::isError($user))
298 395 {
299 396 return $user;
300   - }
301   -
  397 + }
  398 +
302 399 DBUtil::startTransaction();
303   -
  400 +
304 401 $user = &User::getByUserName($newusername);
305 402 if (is_null($user) || PEAR::isError($user))
306 403 {
307 404 return new KTAPI_Error('User could not be found',$user);
308 405 }
309   -
  406 +
310 407 $newuserid = $user->getId();
311   -
  408 +
312 409 $this->document->setOwnerID($newuserid);
313   -
  410 +
314 411 $res = $this->document->update();
315   -
316   - if (PEAR::isError($res))
  412 +
  413 + if (PEAR::isError($res))
317 414 {
318 415 DBUtil::rollback();
319 416 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR ,$res );
320 417 }
321   -
  418 +
322 419 $res = KTPermissionUtil::updatePermissionLookup($this->document);
323   - if (PEAR::isError($res))
  420 + if (PEAR::isError($res))
324 421 {
325 422 DBUtil::rollback();
326 423 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
327 424 }
328   -
  425 +
329 426 $oDocumentTransaction = & new DocumentTransaction($this->document, $reason, 'ktcore.transactions.permissions_change');
330   -
  427 +
331 428 $res = $oDocumentTransaction->create();
332 429 if (($res === false) || PEAR::isError($res)) {
333 430 DBUtil::rollback();
334 431 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
335 432 }
336   -
  433 +
337 434 DBUtil::commit();
338   - }
339   -
  435 + }
  436 +
340 437 /**
341 438 * This copies the document to another folder.
342 439 *
... ... @@ -349,7 +446,7 @@ class KTAPI_Document extends KTAPI_FolderItem
349 446 {
350 447 assert(!is_null($ktapi_target_folder));
351 448 assert(is_a($ktapi_target_folder,'KTAPI_Folder'));
352   -
  449 +
353 450 if (empty($newname))
354 451 {
355 452 $newname=null;
... ... @@ -358,7 +455,7 @@ class KTAPI_Document extends KTAPI_FolderItem
358 455 {
359 456 $newfilename=null;
360 457 }
361   -
  458 +
362 459 $user = $this->ktapi->get_user();
363 460  
364 461 if ($this->document->getIsCheckedOut())
... ... @@ -367,7 +464,7 @@ class KTAPI_Document extends KTAPI_FolderItem
367 464 }
368 465  
369 466 $target_folder = &$ktapi_target_folder->get_folder();
370   -
  467 +
371 468 $result = $this->can_user_access_object_requiring_permission( $target_folder, KTAPI_PERMISSION_WRITE);
372 469  
373 470 if (PEAR::isError($result))
... ... @@ -377,51 +474,51 @@ class KTAPI_Document extends KTAPI_FolderItem
377 474  
378 475 $name = $this->document->getName();
379 476 $clash = KTDocumentUtil::nameExists($target_folder, $name);
380   - if ($clash && !is_null($newname))
381   - {
  477 + if ($clash && !is_null($newname))
  478 + {
382 479 $name = $newname;
383 480 $clash = KTDocumentUtil::nameExists($target_folder, $name);
384 481 }
385   - if ($clash)
  482 + if ($clash)
386 483 {
387 484 return new PEAR_Error('A document with this title already exists in your chosen folder. Please choose a different folder, or specify a new title for the copied document.');
388 485 }
389   -
  486 +
390 487 $filename=$this->document->getFilename();
391 488 $clash = KTDocumentUtil::fileExists($target_folder, $filename);
392 489  
393   - if ($clash && !is_null($newname))
  490 + if ($clash && !is_null($newname))
394 491 {
395 492 $filename = $newfilename;
396 493 $clash = KTDocumentUtil::fileExists($target_folder, $filename);
397 494 }
398   - if ($clash)
  495 + if ($clash)
399 496 {
400 497 return new PEAR_Error('A document with this filename already exists in your chosen folder. Please choose a different folder, or specify a new filename for the copied document.');
401 498 }
402   -
  499 +
403 500 DBUtil::startTransaction();
404   -
  501 +
405 502 $new_document = KTDocumentUtil::copy($this->document, $target_folder, $reason);
406   - if (PEAR::isError($new_document))
  503 + if (PEAR::isError($new_document))
407 504 {
408 505 DBUtil::rollback();
409 506 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$new_document );
410 507 }
411   -
  508 +
412 509 $new_document->setName($name);
413 510 $new_document->setFilename($filename);
414   -
  511 +
415 512 $res = $new_document->update();
416   -
417   - if (PEAR::isError($res))
  513 +
  514 + if (PEAR::isError($res))
418 515 {
419 516 DBUtil::rollback();
420 517 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
421 518 }
422 519  
423 520 DBUtil::commit();
424   -
  521 +
425 522 // FIXME do we need to refactor all trigger usage into the util function?
426 523 $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
427 524 $aTriggers = $oKTTriggerRegistry->getTriggers('copyDocument', 'postValidate');
... ... @@ -437,7 +534,7 @@ class KTAPI_Document extends KTAPI_FolderItem
437 534 $ret = $oTrigger->postValidate();
438 535 }
439 536 }
440   -
  537 +
441 538 /**
442 539 * This moves the document to another folder.
443 540 *
... ... @@ -450,7 +547,7 @@ class KTAPI_Document extends KTAPI_FolderItem
450 547 {
451 548 assert(!is_null($ktapi_target_folder));
452 549 assert(is_a($ktapi_target_folder,'KTAPI_Folder'));
453   -
  550 +
454 551 if (empty($newname))
455 552 {
456 553 $newname=null;
... ... @@ -458,8 +555,8 @@ class KTAPI_Document extends KTAPI_FolderItem
458 555 if (empty($newfilename))
459 556 {
460 557 $newfilename=null;
461   - }
462   -
  558 + }
  559 +
463 560 $user = $this->can_user_access_object_requiring_permission( $this->document, KTAPI_PERMISSION_DOCUMENT_MOVE);
464 561  
465 562 if (PEAR::isError($user))
... ... @@ -473,14 +570,14 @@ class KTAPI_Document extends KTAPI_FolderItem
473 570 }
474 571  
475 572 $target_folder = $ktapi_target_folder->get_folder();
476   -
  573 +
477 574 $result= $this->can_user_access_object_requiring_permission( $target_folder, KTAPI_PERMISSION_WRITE);
478 575  
479 576 if (PEAR::isError($result))
480 577 {
481 578 return $result;
482 579 }
483   -
  580 +
484 581 if (!KTDocumentUtil::canBeMoved($this->document))
485 582 {
486 583 return new PEAR_Error('Document cannot be moved.');
... ... @@ -488,52 +585,52 @@ class KTAPI_Document extends KTAPI_FolderItem
488 585  
489 586 $name = $this->document->getName();
490 587 $clash = KTDocumentUtil::nameExists($target_folder, $name);
491   - if ($clash && !is_null($newname))
492   - {
  588 + if ($clash && !is_null($newname))
  589 + {
493 590 $name = $newname;
494 591 $clash = KTDocumentUtil::nameExists($target_folder, $name);
495 592 }
496   - if ($clash)
  593 + if ($clash)
497 594 {
498 595 return new PEAR_Error('A document with this title already exists in your chosen folder. Please choose a different folder, or specify a new title for the moved document.');
499 596 }
500   -
  597 +
501 598 $filename=$this->document->getFilename();
502 599 $clash = KTDocumentUtil::fileExists($target_folder, $filename);
503 600  
504   - if ($clash && !is_null($newname))
  601 + if ($clash && !is_null($newname))
505 602 {
506 603 $filename = $newfilename;
507 604 $clash = KTDocumentUtil::fileExists($target_folder, $filename);
508 605 }
509   - if ($clash)
  606 + if ($clash)
510 607 {
511 608 return new PEAR_Error('A document with this filename already exists in your chosen folder. Please choose a different folder, or specify a new filename for the moved document.');
512 609 }
513   -
  610 +
514 611 DBUtil::startTransaction();
515   -
  612 +
516 613 $res = KTDocumentUtil::move($this->document, $target_folder, $user, $reason);
517   - if (PEAR::isError($res))
  614 + if (PEAR::isError($res))
518 615 {
519 616 DBUtil::rollback();
520 617 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res );
521 618 }
522   -
  619 +
523 620 $this->document->setName($name);
524 621 $this->document->setFilename($filename);
525   -
  622 +
526 623 $res = $this->document->update();
527   -
528   - if (PEAR::isError($res))
  624 +
  625 + if (PEAR::isError($res))
529 626 {
530 627 DBUtil::rollback();
531 628 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
532 629 }
533 630  
534 631 DBUtil::commit();
535   - }
536   -
  632 + }
  633 +
537 634 /**
538 635 * This changes the filename of the document.
539 636 *
... ... @@ -547,17 +644,17 @@ class KTAPI_Document extends KTAPI_FolderItem
547 644 {
548 645 return $user;
549 646 }
550   -
  647 +
551 648 DBUtil::startTransaction();
552 649 $res = KTDocumentUtil::rename($this->document, $newname, $user);
553   - if (PEAR::isError($res))
  650 + if (PEAR::isError($res))
554 651 {
555 652 DBUtil::rollback();
556 653 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR,$res );
557 654 }
558 655 DBUtil::commit();
559 656 }
560   -
  657 +
561 658 /**
562 659 * This changes the document type of the document.
563 660 *
... ... @@ -571,9 +668,9 @@ class KTAPI_Document extends KTAPI_FolderItem
571 668 {
572 669 return $user;
573 670 }
574   -
575   - $doctypeid = KTAPI::get_documenttypeid($documenttype);
576   -
  671 +
  672 + $doctypeid = KTAPI::get_documenttypeid($documenttype);
  673 +
577 674 if ($this->document->getDocumentTypeId() != $doctypeid)
578 675 {
579 676 DBUtil::startTransaction();
... ... @@ -587,8 +684,8 @@ class KTAPI_Document extends KTAPI_FolderItem
587 684 }
588 685 DBUtil::commit();
589 686 }
590   - }
591   -
  687 + }
  688 +
592 689 /**
593 690 * This changes the title of the document.
594 691 *
... ... @@ -602,7 +699,7 @@ class KTAPI_Document extends KTAPI_FolderItem
602 699 {
603 700 return $user;
604 701 }
605   -
  702 +
606 703 if ($this->document->getName() != $newname)
607 704 {
608 705  
... ... @@ -618,7 +715,7 @@ class KTAPI_Document extends KTAPI_FolderItem
618 715 DBUtil::commit();
619 716 }
620 717 }
621   -
  718 +
622 719 /**
623 720 * This flags the document as 'archived'.
624 721 *
... ... @@ -633,8 +730,8 @@ class KTAPI_Document extends KTAPI_FolderItem
633 730 return $user;
634 731 }
635 732  
636   - list($permission, $user) = $perm_and_user;
637   -
  733 + list($permission, $user) = $perm_and_user;
  734 +
638 735 DBUtil::startTransaction();
639 736 $this->document->setStatusID(ARCHIVED);
640 737 $res = $this->document->update();
... ... @@ -642,15 +739,15 @@ class KTAPI_Document extends KTAPI_FolderItem
642 739 DBUtil::rollback();
643 740 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
644 741 }
645   -
  742 +
646 743 $oDocumentTransaction = & new DocumentTransaction($this->document, sprintf(_kt('Document archived: %s'), $reason), 'ktcore.transactions.update');
647 744 $oDocumentTransaction->create();
648   -
  745 +
649 746 DBUtil::commit();
650 747  
651 748 $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
652 749 $aTriggers = $oKTTriggerRegistry->getTriggers('archive', 'postValidate');
653   - foreach ($aTriggers as $aTrigger)
  750 + foreach ($aTriggers as $aTrigger)
654 751 {
655 752 $sTrigger = $aTrigger[0];
656 753 $oTrigger = new $sTrigger;
... ... @@ -659,9 +756,9 @@ class KTAPI_Document extends KTAPI_FolderItem
659 756 );
660 757 $oTrigger->setInfo($aInfo);
661 758 $ret = $oTrigger->postValidate();
662   - }
663   - }
664   -
  759 + }
  760 + }
  761 +
665 762 /**
666 763 * This starts a workflow on a document.
667 764 *
... ... @@ -675,20 +772,20 @@ class KTAPI_Document extends KTAPI_FolderItem
675 772 {
676 773 return $user;
677 774 }
678   -
  775 +
679 776 $workflowid = $this->document->getWorkflowId();
680   -
  777 +
681 778 if (!empty($workflowid))
682 779 {
683 780 return new PEAR_Error('A workflow is already defined.');
684 781 }
685   -
  782 +
686 783 $workflow = KTWorkflow::getByName($workflow);
687 784 if (is_null($workflow) || PEAR::isError($workflow))
688 785 {
689 786 return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $workflow);
690 787 }
691   -
  788 +
692 789 DBUtil::startTransaction();
693 790 $result = KTWorkflowUtil::startWorkflowOnDocument($workflow, $this->document);
694 791 if (is_null($result) || PEAR::isError($result))
... ... @@ -698,7 +795,7 @@ class KTAPI_Document extends KTAPI_FolderItem
698 795 }
699 796 DBUtil::commit();
700 797 }
701   -
  798 +
702 799 /**
703 800 * This deletes the workflow on the document.
704 801 *
... ... @@ -711,13 +808,13 @@ class KTAPI_Document extends KTAPI_FolderItem
711 808 {
712 809 return $user;
713 810 }
714   -
  811 +
715 812 $workflowid=$this->document->getWorkflowId();
716 813 if (!empty($workflowid))
717 814 {
718 815 return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
719 816 }
720   -
  817 +
721 818 DBUtil::startTransaction();
722 819 $result = KTWorkflowUtil::startWorkflowOnDocument(null, $this->document);
723 820 if (is_null($result) || PEAR::isError($result))
... ... @@ -727,7 +824,7 @@ class KTAPI_Document extends KTAPI_FolderItem
727 824 }
728 825 DBUtil::commit();
729 826 }
730   -
  827 +
731 828 /**
732 829 * This performs a transition on the workflow
733 830 *
... ... @@ -742,19 +839,19 @@ class KTAPI_Document extends KTAPI_FolderItem
742 839 {
743 840 return $user;
744 841 }
745   -
  842 +
746 843 $workflowid=$this->document->getWorkflowId();
747 844 if (empty($workflowid))
748 845 {
749 846 return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
750   - }
751   -
  847 + }
  848 +
752 849 $transition = &KTWorkflowTransition::getByName($transition);
753 850 if (is_null($transition) || PEAR::isError($transition))
754 851 {
755 852 return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $transition);
756 853 }
757   -
  854 +
758 855 DBUtil::startTransaction();
759 856 $result = KTWorkflowUtil::performTransitionOnDocument($transition, $this->document, $user, $reason);
760 857 if (is_null($result) || PEAR::isError($result))
... ... @@ -762,11 +859,11 @@ class KTAPI_Document extends KTAPI_FolderItem
762 859 DBUtil::rollback();
763 860 return new KTAPI_Error(KTAPI_ERROR_WORKFLOW_INVALID, $transition);
764 861 }
765   - DBUtil::commit();
766   - }
767   -
768   -
769   -
  862 + DBUtil::commit();
  863 + }
  864 +
  865 +
  866 +
770 867 /**
771 868 * This returns all metadata for the document.
772 869 *
... ... @@ -776,31 +873,31 @@ class KTAPI_Document extends KTAPI_FolderItem
776 873 {
777 874 $doctypeid = $this->document->getDocumentTypeID();
778 875 $fieldsets = (array) KTMetadataUtil::fieldsetsForDocument($this->document, $doctypeid);
779   -
  876 +
780 877 $results = array();
781   -
782   - foreach ($fieldsets as $fieldset)
  878 +
  879 + foreach ($fieldsets as $fieldset)
783 880 {
784 881 if ($fieldset->getIsConditional()) { /* this is not implemented...*/ continue; }
785   -
  882 +
786 883 $fields = $fieldset->getFields();
787 884 $result = array('fieldset' => $fieldset->getName(),
788 885 'description' => $fieldset->getDescription());
789   -
  886 +
790 887 $fieldsresult = array();
791   -
792   - foreach ($fields as $field)
793   - {
  888 +
  889 + foreach ($fields as $field)
  890 + {
794 891 $value = 'n/a';
795   -
  892 +
796 893 $fieldvalue = DocumentFieldLink::getByDocumentAndField($this->document, $field);
797   - if (!is_null($fieldvalue) && (!PEAR::isError($fieldvalue)))
  894 + if (!is_null($fieldvalue) && (!PEAR::isError($fieldvalue)))
798 895 {
799 896 $value = $fieldvalue->getValue();
800 897 }
801   -
  898 +
802 899 $controltype = 'string';
803   - if ($field->getHasLookup())
  900 + if ($field->getHasLookup())
804 901 {
805 902 $controltype = 'lookup';
806 903 if ($field->getHasLookupTree())
... ... @@ -808,11 +905,11 @@ class KTAPI_Document extends KTAPI_FolderItem
808 905 $controltype = 'tree';
809 906 }
810 907 }
811   -
  908 +
812 909 switch ($controltype)
813 910 {
814 911 case 'lookup':
815   - $selection = KTAPI::get_metadata_lookup($field->getId());
  912 + $selection = KTAPI::get_metadata_lookup($field->getId());
816 913 break;
817 914 case 'tree':
818 915 $selection = KTAPI::get_metadata_tree($field->getId());
... ... @@ -821,7 +918,7 @@ class KTAPI_Document extends KTAPI_FolderItem
821 918 $selection= array();
822 919 }
823 920  
824   -
  921 +
825 922 $fieldsresult[] = array(
826 923 'name' => $field->getName(),
827 924 'required' => $field->getIsMandatory(),
... ... @@ -829,27 +926,27 @@ class KTAPI_Document extends KTAPI_FolderItem
829 926 'description' => $field->getDescription(),
830 927 'control_type' => $controltype,
831 928 'selection' => $selection
832   -
  929 +
833 930 );
834   -
  931 +
835 932 }
836 933 $result['fields'] = $fieldsresult;
837   - $results [] = $result;
838   - }
839   -
  934 + $results [] = $result;
  935 + }
  936 +
840 937 return $results;
841 938 }
842   -
  939 +
843 940 /**
844 941 * This updates the metadata on the file. This includes the 'title'.
845 942 *
846 943 * @param array This is an array containing the metadata to be associated with the file.
847 944 */
848 945 function update_metadata($metadata)
849   - {
  946 + {
850 947 global $default;
851 948 $packed = array();
852   -
  949 +
853 950 foreach($metadata as $fieldset_metadata)
854 951 {
855 952 if (is_array($fieldset_metadata))
... ... @@ -875,7 +972,7 @@ class KTAPI_Document extends KTAPI_FolderItem
875 972 // exit graciously
876 973 continue;
877 974 }
878   -
  975 +
879 976 foreach($fields as $fieldinfo)
880 977 {
881 978 if (is_array($fieldinfo))
... ... @@ -901,27 +998,27 @@ class KTAPI_Document extends KTAPI_FolderItem
901 998 // exit graciously
902 999 continue;
903 1000 }
904   -
  1001 +
905 1002 $packed[] = array($field, $value);
906   - }
  1003 + }
907 1004 }
908   -
  1005 +
909 1006 DBUtil::startTransaction();
910 1007 $result = KTDocumentUtil::saveMetadata($this->document, $packed);
911   -
  1008 +
912 1009 if (is_null($result))
913 1010 {
914 1011 DBUtil::rollback();
915 1012 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR);
916 1013 }
917   - if (PEAR::isError($result))
  1014 + if (PEAR::isError($result))
918 1015 {
919 1016 DBUtil::rollback();
920   - return new KTAPI_Error(sprintf(_kt("Unexpected validation failure: %s."), $result->getMessage()));
  1017 + return new KTAPI_Error(sprintf(_kt("Unexpected validation failure: %s."), $result->getMessage()));
921 1018 }
922 1019 DBUtil::commit();
923 1020 }
924   -
  1021 +
925 1022  
926 1023 /**
927 1024 * This returns a workflow transition
... ... @@ -941,10 +1038,10 @@ class KTAPI_Document extends KTAPI_FolderItem
941 1038 if (empty($workflowid))
942 1039 {
943 1040 return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
944   - }
945   -
  1041 + }
  1042 +
946 1043 $result = array();
947   -
  1044 +
948 1045 $transitions = KTWorkflowUtil::getTransitionsForDocumentUser($this->document, $user);
949 1046 if (is_null($transitions) || PEAR::isError($transitions))
950 1047 {
... ... @@ -954,10 +1051,10 @@ class KTAPI_Document extends KTAPI_FolderItem
954 1051 {
955 1052 $result[] = $transition->getName();
956 1053 }
957   -
958   - return $result;
  1054 +
  1055 + return $result;
959 1056 }
960   -
  1057 +
961 1058 /**
962 1059 * This returns the current workflow state
963 1060 *
... ... @@ -976,27 +1073,27 @@ class KTAPI_Document extends KTAPI_FolderItem
976 1073 if (empty($workflowid))
977 1074 {
978 1075 return new PEAR_Error(KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS);
979   - }
980   -
  1076 + }
  1077 +
981 1078 $result = array();
982   -
  1079 +
983 1080 $state = KTWorkflowUtil::getWorkflowStateForDocument($this->document);
984 1081 if (is_null($state) || PEAR::isError($state))
985 1082 {
986 1083 return new PEAR_Error(KTAPI_ERROR_WORKFLOW_INVALID);
987 1084 }
988   -
  1085 +
989 1086 $statename = $state->getName();
990   -
991   - return $statename;
992   -
  1087 +
  1088 + return $statename;
  1089 +
993 1090 }
994   -
  1091 +
995 1092 /**
996 1093 * This returns detailed information on the document.
997 1094 *
998 1095 * @return array
999   - */
  1096 + */
1000 1097 function get_detail()
1001 1098 {
1002 1099 $detail = array();
... ... @@ -1011,12 +1108,12 @@ class KTAPI_Document extends KTAPI_FolderItem
1011 1108  
1012 1109 $documenttype=$documenttype->getName();
1013 1110 }
1014   - else
  1111 + else
1015 1112 {
1016 1113 $documenttype = '* unknown *';
1017 1114 }
1018 1115 $detail['document_type'] = $documenttype;
1019   -
  1116 +
1020 1117 $detail['version'] = $document->getVersion();
1021 1118 $detail['filename'] = $document->getFilename();
1022 1119  
... ... @@ -1028,7 +1125,7 @@ class KTAPI_Document extends KTAPI_FolderItem
1028 1125 $user = User::get($userid);
1029 1126 $username=(is_null($user) || PEAR::isError($user))?'* unknown *':$user->getName();
1030 1127 }
1031   - else
  1128 + else
1032 1129 {
1033 1130 $username='n/a';
1034 1131 }
... ... @@ -1039,9 +1136,9 @@ class KTAPI_Document extends KTAPI_FolderItem
1039 1136 if (is_numeric($userid))
1040 1137 {
1041 1138 $user = User::get($userid);
1042   - $username=(is_null($user) || PEAR::isError($user))?'* unknown *':$user->getName();
  1139 + $username=(is_null($user) || PEAR::isError($user))?'* unknown *':$user->getName();
1043 1140 }
1044   - else
  1141 + else
1045 1142 {
1046 1143 $username='n/a';
1047 1144 }
... ... @@ -1055,7 +1152,7 @@ class KTAPI_Document extends KTAPI_FolderItem
1055 1152 $workflow = KTWorkflow::get($workflowid);
1056 1153 $workflowname=(is_null($workflow) || PEAR::isError($workflow))?'* unknown *':$workflow->getName();
1057 1154 }
1058   - else
  1155 + else
1059 1156 {
1060 1157 $workflowname='n/a';
1061 1158 }
... ... @@ -1067,57 +1164,57 @@ class KTAPI_Document extends KTAPI_FolderItem
1067 1164 $state = KTWorkflowState::get($stateid);
1068 1165 $workflowstate=(is_null($state) || PEAR::isError($state))?'* unknown *':$state->getName();
1069 1166 }
1070   - else
  1167 + else
1071 1168 {
1072 1169 $workflowstate = 'n/a';
1073 1170 }
1074 1171 $detail['workflow_state']=$workflowstate;
1075 1172  
1076 1173 $userid = $document->getCheckedOutUserID();
1077   -
  1174 +
1078 1175 if (is_numeric($userid))
1079 1176 {
1080 1177 $user = User::get($userid);
1081 1178 $username=(is_null($user) || PEAR::isError($user))?'* unknown *':$user->getName();
1082 1179 }
1083   - else
  1180 + else
1084 1181 {
1085 1182 $username = 'n/a';
1086 1183 }
1087 1184 $detail['checkout_by'] = $username;
1088   -
  1185 +
1089 1186 $detail['full_path'] = $this->ktapi_folder->get_full_path() . '/' . $this->get_title();
1090   -
  1187 +
1091 1188 return $detail;
1092 1189 }
1093   -
  1190 +
1094 1191 function get_title()
1095 1192 {
1096 1193 return $this->document->getDescription();
1097 1194 }
1098   -
  1195 +
1099 1196 /**
1100 1197 * This does a download of a version of the document.
1101 1198 *
1102 1199 * @param string $version
1103 1200 */
1104 1201 function download($version=null)
1105   - {
  1202 + {
1106 1203 $storage =& KTStorageManagerUtil::getSingleton();
1107 1204 $options = array();
1108   -
1109   -
  1205 +
  1206 +
1110 1207 $oDocumentTransaction = & new DocumentTransaction($this->document, 'Document downloaded', 'ktcore.transactions.download', $aOptions);
1111 1208 $oDocumentTransaction->create();
1112 1209 }
1113   -
  1210 +
1114 1211 /**
1115 1212 * This returns the transaction history for the document.
1116 1213 *
1117 1214 * @return array
1118 1215 */
1119 1216 function get_transaction_history()
1120   - {
  1217 + {
1121 1218 $sQuery = 'SELECT DTT.name AS transaction_name, U.name AS username, DT.version AS version, DT.comment AS comment, DT.datetime AS datetime ' .
1122 1219 'FROM ' . KTUtil::getTableName('document_transactions') . ' AS DT INNER JOIN ' . KTUtil::getTableName('users') . ' AS U ON DT.user_id = U.id ' .
1123 1220 'INNER JOIN ' . KTUtil::getTableName('transaction_types') . ' AS DTT ON DTT.namespace = DT.transaction_namespace ' .
... ... @@ -1125,14 +1222,14 @@ class KTAPI_Document extends KTAPI_FolderItem
1125 1222 $aParams = array($this->documentid);
1126 1223  
1127 1224 $transactions = DBUtil::getResultArray(array($sQuery, $aParams));
1128   - if (is_null($transactions) || PEAR::isError($transactions))
  1225 + if (is_null($transactions) || PEAR::isError($transactions))
1129 1226 {
1130 1227 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $transactions );
1131 1228 }
1132 1229  
1133 1230 return $transactions;
1134 1231 }
1135   -
  1232 +
1136 1233 /**
1137 1234 * This returns the version history on the document.
1138 1235 *
... ... @@ -1141,21 +1238,21 @@ class KTAPI_Document extends KTAPI_FolderItem
1141 1238 function get_version_history()
1142 1239 {
1143 1240 $metadata_versions = KTDocumentMetadataVersion::getByDocument($this->document);
1144   -
  1241 +
1145 1242 $versions = array();
1146   - foreach ($metadata_versions as $version)
  1243 + foreach ($metadata_versions as $version)
1147 1244 {
1148 1245 $document = &Document::get($this->documentid, $version->getId());
1149   -
  1246 +
1150 1247 $version = array();
1151   -
1152   - $userid = $document->getModifiedUserId();
1153   - $user = User::get($userid);
1154   -
  1248 +
  1249 + $userid = $document->getModifiedUserId();
  1250 + $user = User::get($userid);
  1251 +
1155 1252 $version['user'] = $user->getName();
1156 1253 $version['metadata_version'] = $document->getMetadataVersion();
1157 1254 $version['content_version'] = $document->getVersion();
1158   -
  1255 +
1159 1256 $versions[] = $version;
1160 1257 }
1161 1258 return $versions;
... ... @@ -1173,22 +1270,22 @@ class KTAPI_Document extends KTAPI_FolderItem
1173 1270 return new PEAR_Error('You should not purge this');
1174 1271 }
1175 1272 DBUtil::startTransaction();
1176   -
  1273 +
1177 1274 $transaction = & new DocumentTransaction($this->document, "Document expunged", 'ktcore.transactions.expunge');
1178   -
  1275 +
1179 1276 $transaction->create();
1180   -
  1277 +
1181 1278 $this->document->delete();
1182   -
1183   - $this->document->cleanupDocumentData($this->documentid);
1184   -
  1279 +
  1280 + $this->document->cleanupDocumentData($this->documentid);
  1281 +
1185 1282 $storage =& KTStorageManagerUtil::getSingleton();
1186   -
  1283 +
1187 1284 $result= $storage->expunge($this->document);
1188 1285  
1189 1286 DBUtil::commit();
1190 1287 }
1191   -
  1288 +
1192 1289 /**
1193 1290 * This expunges a document from the system.
1194 1291 *
... ... @@ -1197,28 +1294,28 @@ class KTAPI_Document extends KTAPI_FolderItem
1197 1294 function restore()
1198 1295 {
1199 1296 DBUtil::startTransaction();
1200   -
  1297 +
1201 1298 $storage =& KTStorageManagerUtil::getSingleton();
1202   -
  1299 +
1203 1300 $folder = Folder::get($this->document->getRestoreFolderId());
1204   - if (PEAR::isError($folder))
  1301 + if (PEAR::isError($folder))
1205 1302 {
1206 1303 $this->document->setFolderId(1);
1207 1304 $folder = Folder::get(1);
1208 1305 }
1209   - else
  1306 + else
1210 1307 {
1211 1308 $this->document->setFolderId($this->document->getRestoreFolderId());
1212 1309 }
1213 1310  
1214 1311 $storage->restore($this->document);
1215   -
  1312 +
1216 1313 $this->document->setStatusId(LIVE);
1217 1314 $this->document->setPermissionObjectId($folder->getPermissionObjectId());
1218 1315 $res = $this->document->update();
1219 1316  
1220 1317 $res = KTPermissionUtil::updatePermissionLookup($this->document);
1221   -
  1318 +
1222 1319 $user = $this->ktapi->get_user();
1223 1320  
1224 1321 $oTransaction = new DocumentTransaction($this->document, 'Restored from deleted state by ' . $user->getName(), 'ktcore.transactions.update');
... ...
ktapi/KTAPIFolder.inc.php
... ... @@ -6,7 +6,7 @@
6 6 * License Version 1.1.2 ("License"); You may not use this file except in
7 7 * compliance with the License. You may obtain a copy of the License at
8 8 * http://www.knowledgetree.com/KPL
9   - *
  9 + *
10 10 * Software distributed under the License is distributed on an "AS IS"
11 11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
12 12 * See the License for the specific language governing rights and
... ... @@ -17,9 +17,9 @@
17 17 * (ii) the KnowledgeTree copyright notice
18 18 * in the same form as they appear in the distribution. See the License for
19 19 * requirements.
20   - *
  20 + *
21 21 * The Original Code is: KnowledgeTree Open Source
22   - *
  22 + *
23 23 * The Initial Developer of the Original Code is The Jam Warehouse Software
24 24 * (Pty) Ltd, trading as KnowledgeTree.
25 25 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -30,7 +30,7 @@
30 30 */
31 31  
32 32 class KTAPI_Folder extends KTAPI_FolderItem
33   -{
  33 +{
34 34 /**
35 35 * This is a reference to a base Folder object.
36 36 *
... ... @@ -38,7 +38,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
38 38 * @var Folder
39 39 */
40 40 var $folder;
41   -
  41 +
42 42 /**
43 43 * This is the id of the folder on the database.
44 44 *
... ... @@ -60,25 +60,25 @@ class KTAPI_Folder extends KTAPI_FolderItem
60 60 assert(!is_null($ktapi));
61 61 assert(is_a($ktapi, 'KTAPI'));
62 62 assert(is_numeric($folderid));
63   -
  63 +
64 64 $folderid += 0;
65   -
  65 +
66 66 $folder = &Folder::get($folderid);
67 67 if (is_null($folder) || PEAR::isError($folder))
68 68 {
69 69 return new KTAPI_Error(KTAPI_ERROR_FOLDER_INVALID,$folder);
70 70 }
71   -
  71 +
72 72 $user = $ktapi->can_user_access_object_requiring_permission($folder, KTAPI_PERMISSION_READ);
73   -
  73 +
74 74 if (is_null($user) || PEAR::isError($user))
75 75 {
76 76 return $user;
77 77 }
78 78  
79   - return new KTAPI_Folder($ktapi, $folder);
80   - }
81   -
  79 + return new KTAPI_Folder($ktapi, $folder);
  80 + }
  81 +
82 82 /**
83 83 * This is the constructor for the KTAPI_Folder.
84 84 *
... ... @@ -90,27 +90,27 @@ class KTAPI_Folder extends KTAPI_FolderItem
90 90 function KTAPI_Folder(&$ktapi, &$folder)
91 91 {
92 92 $this->ktapi = &$ktapi;
93   - $this->folder = &$folder;
  93 + $this->folder = &$folder;
94 94 $this->folderid = $folder->getId();
95 95 }
96   -
  96 +
97 97 /**
98 98 * This returns a reference to the internal folder object.
99 99 *
100 100 * @access protected
101 101 * @return Folder
102   - */
  102 + */
103 103 function &get_folder()
104 104 {
105 105 return $this->folder;
106 106 }
107   -
108   -
  107 +
  108 +
109 109 /**
110 110 * This returns detailed information on the document.
111 111 *
112 112 * @return array
113   - */
  113 + */
114 114 function get_detail()
115 115 {
116 116 $detail = array(
... ... @@ -119,21 +119,21 @@ class KTAPI_Folder extends KTAPI_FolderItem
119 119 'parent_id'=>(int) $this->get_parent_folder_id(),
120 120 'full_path'=>$this->get_full_path(),
121 121 );
122   -
  122 +
123 123 return $detail;
124 124 }
125   -
  125 +
126 126 function get_parent_folder_id()
127 127 {
128 128 return (int) $this->folder->getParentID();
129 129 }
130   -
  130 +
131 131 function get_folder_name()
132 132 {
133 133 return $this->folder->getFolderName($this->folderid);
134 134 }
135   -
136   -
  135 +
  136 +
137 137 /**
138 138 * This returns the folderid.
139 139 *
... ... @@ -143,14 +143,14 @@ class KTAPI_Folder extends KTAPI_FolderItem
143 143 {
144 144 return (int) $this->folderid;
145 145 }
146   -
  146 +
147 147 /**
148 148 * This can resolve a folder relative to the current directy by name
149 149 *
150 150 * @access public
151 151 * @param string $foldername
152 152 * @return KTAPI_Folder
153   - */
  153 + */
154 154 function &get_folder_by_name($foldername)
155 155 {
156 156 $foldername=trim($foldername);
... ... @@ -158,9 +158,9 @@ class KTAPI_Folder extends KTAPI_FolderItem
158 158 {
159 159 return new PEAR_Error('A valid folder name must be specified.');
160 160 }
161   -
  161 +
162 162 $split = explode('/', $foldername);
163   -
  163 +
164 164 $folderid=$this->folderid;
165 165 foreach($split as $foldername)
166 166 {
... ... @@ -174,19 +174,19 @@ class KTAPI_Folder extends KTAPI_FolderItem
174 174 {
175 175 return new KTAPI_Error(KTAPI_ERROR_FOLDER_INVALID,$row);
176 176 }
177   - $folderid = $row['id'];
  177 + $folderid = $row['id'];
178 178 }
179   -
180   - return KTAPI_Folder::get($this->ktapi, $folderid);
  179 +
  180 + return KTAPI_Folder::get($this->ktapi, $folderid);
181 181 }
182   -
  182 +
183 183 function get_full_path()
184 184 {
185 185 $path = $this->folder->getFullPath() . '/' . $this->folder->getName();
186   -
  186 +
187 187 return $path;
188 188 }
189   -
  189 +
190 190 /**
191 191 * This gets a document by filename or name.
192 192 *
... ... @@ -194,7 +194,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
194 194 * @param string $documentname
195 195 * @param string $function
196 196 * @return KTAPI_Document
197   - */
  197 + */
198 198 function &_get_document_by_name($documentname, $function='getByNameAndFolder')
199 199 {
200 200 $documentname=trim($documentname);
... ... @@ -202,68 +202,68 @@ class KTAPI_Folder extends KTAPI_FolderItem
202 202 {
203 203 return new PEAR_Error('A valid document name must be specified.');
204 204 }
205   -
  205 +
206 206 $foldername = dirname($documentname);
207 207 $documentname = basename($documentname);
208   -
  208 +
209 209 $ktapi_folder = $this;
210   -
  210 +
211 211 if (!empty($foldername) && ($foldername != '.'))
212 212 {
213 213 $ktapi_folder = $this->get_folder_by_name($foldername);
214 214 }
215   -
  215 +
216 216 if (is_null($ktapi_folder) || PEAR::isError($ktapi_folder))
217 217 {
218 218 return new KTAPI_Error(KTAPI_ERROR_FOLDER_INVALID, $ktapi_folder);
219 219 }
220   -
  220 +
221 221 //$folder = $ktapi_folder->get_folder();
222 222 $folderid = $ktapi_folder->folderid;
223   -
224   - $document = Document::$function($documentname, $folderid);
  223 +
  224 + $document = Document::$function($documentname, $folderid);
225 225 if (is_null($document) || PEAR::isError($document))
226 226 {
227 227 return new KTAPI_Error(KTAPI_ERROR_DOCUMENT_INVALID, $document);
228 228 }
229   -
230   - $user = $this->can_user_access_object_requiring_permission($document, KTAPI_PERMISSION_READ);
  229 +
  230 + $user = $this->can_user_access_object_requiring_permission($document, KTAPI_PERMISSION_READ);
231 231 if (PEAR::isError($user))
232 232 {
233 233 return $user;
234 234 }
235   -
  235 +
236 236 return new KTAPI_Document($this->ktapi, $ktapi_folder, $document);
237 237 }
238   -
  238 +
239 239 /**
240 240 * This can resolve a document relative to the current directy by name.
241 241 *
242 242 * @access public
243 243 * @param string $documentname
244 244 * @return KTAPI_Document
245   - */
  245 + */
246 246 function &get_document_by_name($documentname)
247 247 {
248 248 return $this->_get_document_by_name($documentname,'getByNameAndFolder');
249 249 }
250   -
  250 +
251 251 /**
252 252 * This can resolve a document relative to the current directy by filename .
253 253 *
254 254 * @access public
255 255 * @param string $documentname
256 256 * @return KTAPI_Document
257   - */
  257 + */
258 258 function &get_document_by_filename($documentname)
259 259 {
260 260 return $this->_get_document_by_name($documentname,'getByFilenameAndFolder');
261   - }
262   -
  261 + }
  262 +
263 263 function _resolve_user($userid)
264 264 {
265 265 $user=null;
266   -
  266 +
267 267 if (!is_null($userid))
268 268 {
269 269 $user=User::get($userid);
... ... @@ -274,24 +274,24 @@ class KTAPI_Folder extends KTAPI_FolderItem
274 274 }
275 275 return $user;
276 276 }
277   -
278   -
  277 +
  278 +
279 279 function get_listing($depth=1, $what='DF')
280   - {
281   - if ($depth < 1)
  280 + {
  281 + if ($depth < 1)
282 282 {
283 283 return array();
284 284 }
285   -
  285 +
286 286 $what = strtoupper($what);
287   - $read_permission = &KTPermission::getByName(KTAPI_PERMISSION_READ);
  287 + $read_permission = &KTPermission::getByName(KTAPI_PERMISSION_READ);
288 288 $folder_permission = &KTPermission::getByName(KTAPI_PERMISSION_VIEW_FOLDER);
289   -
290   -
  289 +
  290 +
291 291 $user = $this->ktapi->get_user();
292 292  
293 293 $contents = array();
294   -
  294 +
295 295 if (strpos($what,'F') !== false)
296 296 {
297 297 $folder_children = Folder::getList(array('parent_id = ?', $this->folderid));
... ... @@ -369,7 +369,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
369 369 $mimeinfo=$mime_cache[$mimetypeid];
370 370  
371 371 $workflow = KTWorkflowUtil::getWorkflowForDocument($document);
372   -
  372 +
373 373 if (!is_null($workflow) && !PEAR::isError($workflow))
374 374 {
375 375 $workflow=$workflow->getHumanName();
... ... @@ -384,13 +384,13 @@ class KTAPI_Folder extends KTAPI_FolderItem
384 384 $state='n/a';
385 385 }
386 386 }
387   - else
  387 + else
388 388 {
389 389 $workflow='n/a';
390 390 $state='n/a';
391 391 }
392   -
393   -
  392 +
  393 +
394 394 $contents[] = array(
395 395 'id' => (int) $document->getId(),
396 396 'item_type'=>'D',
... ... @@ -414,10 +414,10 @@ class KTAPI_Folder extends KTAPI_FolderItem
414 414 }
415 415  
416 416 }
417   -
418   - return $contents;
  417 +
  418 + return $contents;
419 419 }
420   -
  420 +
421 421 /**
422 422 * This adds a document to the current folder.
423 423 *
... ... @@ -427,20 +427,20 @@ class KTAPI_Folder extends KTAPI_FolderItem
427 427 * @param string $documenttype This is the name or id of the document type. It first looks by name, then by id.
428 428 * @param string $tempfilename This is a reference to the file that is accessible locally on the file system.
429 429 * @return KTAPI_Document
430   - */
  430 + */
431 431 function &add_document($title, $filename, $documenttype, $tempfilename)
432 432 {
433 433 if (!is_file($tempfilename))
434 434 {
435 435 return new PEAR_Error('File does not exist.');
436 436 }
437   -
438   - $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_WRITE);
  437 +
  438 + $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_WRITE);
439 439 if (PEAR::isError($user))
440 440 {
441 441 return $user;
442 442 }
443   -
  443 +
444 444 $filename = basename($filename);
445 445 $documenttypeid = KTAPI::get_documenttypeid($documenttype);
446 446  
... ... @@ -449,7 +449,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
449 449 'novalidate' => true,
450 450 'documenttype' => DocumentType::get($documenttypeid),
451 451 'description' => $title,
452   - 'metadata'=>array(),
  452 + 'metadata'=>array(),
453 453 'cleanup_initial_file' => true
454 454 );
455 455  
... ... @@ -462,10 +462,10 @@ class KTAPI_Folder extends KTAPI_FolderItem
462 462 return new PEAR_Error(KTAPI_ERROR_INTERNAL_ERROR . ' : ' . $document->getMessage());
463 463 }
464 464 DBUtil::commit();
465   -
  465 +
466 466 $tempfilename=addslashes($tempfilename);
467 467 $sql = "DELETE FROM uploaded_files WHERE tempfilename='$tempfilename'";
468   - $result = DBUtil::runQuery($sql);
  468 + $result = DBUtil::runQuery($sql);
469 469 if (PEAR::isError($result))
470 470 {
471 471 return $result;
... ... @@ -473,26 +473,26 @@ class KTAPI_Folder extends KTAPI_FolderItem
473 473  
474 474 return new KTAPI_Document($this->ktapi, $this, $document);
475 475 }
476   -
  476 +
477 477 /**
478 478 * This adds a subfolder folder to the current folder.
479 479 *
480 480 * @access public
481 481 * @param string $foldername
482 482 * @return KTAPI_Folder
483   - */
  483 + */
484 484 function &add_folder($foldername)
485 485 {
486 486 $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_ADD_FOLDER);
487   -
  487 +
488 488 if (PEAR::isError($user))
489 489 {
490 490 return $user;
491   - }
492   -
  491 + }
  492 +
493 493 DBUtil::startTransaction();
494 494 $result = KTFolderUtil::add($this->folder, $foldername, $user);
495   -
  495 +
496 496 if (PEAR::isError($result))
497 497 {
498 498 DBUtil::rollback();
... ... @@ -500,10 +500,10 @@ class KTAPI_Folder extends KTAPI_FolderItem
500 500 }
501 501 DBUtil::commit();
502 502 $folderid = $result->getId();
503   -
  503 +
504 504 return $this->ktapi->get_folder_by_id($folderid);
505 505 }
506   -
  506 +
507 507 /**
508 508 * This deletes the current folder.
509 509 *
... ... @@ -515,13 +515,13 @@ class KTAPI_Folder extends KTAPI_FolderItem
515 515 if (PEAR::isError($user))
516 516 {
517 517 return $user;
518   - }
519   -
  518 + }
  519 +
520 520 if ($this->folderid == 1)
521 521 {
522 522 return new PEAR_Error('Cannot delete root folder!');
523   - }
524   -
  523 + }
  524 +
525 525 DBUtil::startTransaction();
526 526 $result = KTFolderUtil::delete($this->folder, $user, $reason);
527 527  
... ... @@ -539,13 +539,13 @@ class KTAPI_Folder extends KTAPI_FolderItem
539 539 * @param string $newname
540 540 */
541 541 function rename($newname)
542   - {
  542 + {
543 543 $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_RENAME_FOLDER);
544 544 if (PEAR::isError($user))
545 545 {
546 546 return $user;
547   - }
548   -
  547 + }
  548 +
549 549 DBUtil::startTransaction();
550 550 $result = KTFolderUtil::rename($this->folder, $newname, $user);
551 551  
... ... @@ -556,7 +556,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
556 556 }
557 557 DBUtil::commit();
558 558 }
559   -
  559 +
560 560 /**
561 561 * This moves the folder to another location.
562 562 *
... ... @@ -567,12 +567,12 @@ class KTAPI_Folder extends KTAPI_FolderItem
567 567 {
568 568 assert(!is_null($ktapi_target_folder));
569 569 assert(is_a($ktapi_target_folder,'KTAPI_Folder'));
570   -
  570 +
571 571 $user = $this->ktapi->get_user();
572   -
  572 +
573 573 $target_folder = $ktapi_target_folder->get_folder();
574 574  
575   - $result = $this->can_user_access_object_requiring_permission($target_folder, KTAPI_PERMISSION_WRITE);
  575 + $result = $this->can_user_access_object_requiring_permission($target_folder, KTAPI_PERMISSION_WRITE);
576 576 if (PEAR::isError($result))
577 577 {
578 578 return $result;
... ... @@ -588,7 +588,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
588 588 }
589 589 DBUtil::commit();
590 590 }
591   -
  591 +
592 592 /**
593 593 * This copies a folder to another location.
594 594 *
... ... @@ -599,18 +599,18 @@ class KTAPI_Folder extends KTAPI_FolderItem
599 599 {
600 600 assert(!is_null($ktapi_target_folder));
601 601 assert(is_a($ktapi_target_folder,'KTAPI_Folder'));
602   -
  602 +
603 603 $user = $this->ktapi->get_user();
604   -
  604 +
605 605 $target_folder = $ktapi_target_folder->get_folder();
606 606  
607 607 $result =$this->can_user_access_object_requiring_permission($target_folder, KTAPI_PERMISSION_WRITE);
608   -
  608 +
609 609 if (PEAR::isError($result))
610 610 {
611 611 return $result;
612   - }
613   -
  612 + }
  613 +
614 614 DBUtil::startTransaction();
615 615 $result = KTFolderUtil::copy($this->folder, $target_folder, $user, $reason);
616 616  
... ... @@ -621,7 +621,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
621 621 }
622 622 DBUtil::commit();
623 623 }
624   -
  624 +
625 625 /**
626 626 * This returns all permissions linked to the folder.
627 627 *
... ... @@ -630,7 +630,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
630 630 */
631 631 function get_permissions()
632 632 {
633   - return new PEAR_Error('TODO');
  633 + return new PEAR_Error('TODO');
634 634 }
635 635  
636 636 /**
... ... @@ -641,7 +641,7 @@ class KTAPI_Folder extends KTAPI_FolderItem
641 641 */
642 642 function get_transaction_history()
643 643 {
644   - return new PEAR_Error('TODO');
  644 + return new PEAR_Error('TODO');
645 645 }
646 646 }
647 647  
... ...
ktapi/KTAPISession.inc.php
1 1 <?
2 2 /**
3 3 * $Id$
4   - *
  4 + *
5 5 * The contents of this file are subject to the KnowledgeTree Public
6 6 * License Version 1.1.2 ("License"); You may not use this file except in
7 7 * compliance with the License. You may obtain a copy of the License at
8 8 * http://www.knowledgetree.com/KPL
9   - *
  9 + *
10 10 * Software distributed under the License is distributed on an "AS IS"
11 11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
12 12 * See the License for the specific language governing rights and
... ... @@ -17,9 +17,9 @@
17 17 * (ii) the KnowledgeTree copyright notice
18 18 * in the same form as they appear in the distribution. See the License for
19 19 * requirements.
20   - *
  20 + *
21 21 * The Original Code is: KnowledgeTree Open Source
22   - *
  22 + *
23 23 * The Initial Developer of the Original Code is The Jam Warehouse Software
24 24 * (Pty) Ltd, trading as KnowledgeTree.
25 25 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -29,26 +29,26 @@
29 29 *
30 30 */
31 31  
32   -class KTAPI_Session
  32 +class KTAPI_Session
33 33 {
34 34 var $ktapi;
35 35 var $user = null;
36 36 var $session = '';
37 37 var $sessionid = -1;
38 38 var $active;
39   -
  39 +
40 40 function KTAPI_Session(&$ktapi, &$user)
41 41 {
42 42 assert(!is_null($ktapi));
43 43 assert(is_a($ktapi,'KTAPI'));
44 44 assert(!is_null($user));
45   - assert(is_a($user,'User'));
46   -
  45 + assert(is_a($user,'User'));
  46 +
47 47 $this->ktapi=&$ktapi;
48 48 $this->user=&$user;
49 49 $this->active = false;
50 50 }
51   -
  51 +
52 52 /**
53 53 * return the session string
54 54 *
... ... @@ -58,17 +58,17 @@ class KTAPI_Session
58 58 {
59 59 die('get_session() should be overloaded!');
60 60 }
61   -
  61 +
62 62 /**
63   - * Return the session id
64   - *
  63 + * Return the session id
  64 + *
65 65 * @return int
66 66 */
67 67 function get_sessionid()
68 68 {
69 69 die('get_sessionid() should be overloaded!');
70 70 }
71   -
  71 +
72 72 /**
73 73 * Return the user
74 74 *
... ... @@ -78,29 +78,28 @@ class KTAPI_Session
78 78 {
79 79 return $this->user;
80 80 }
81   -
  81 +
82 82 function logout()
83 83 {
84 84 $this->active=false;
85 85 // don't need to do anything really
86 86 }
87   -
  87 +
88 88 function is_active()
89 89 {
90 90 return $this->active;
91 91 }
92   -
93   -}
94 92  
  93 +}
95 94  
96   -class KTAPI_UserSession extends KTAPI_Session
  95 +class KTAPI_UserSession extends KTAPI_Session
97 96 {
98 97 var $ip = null;
99   -
  98 +
100 99 function KTAPI_UserSession(&$ktapi, &$user, $session, $sessionid, $ip)
101 100 {
102 101 parent::KTAPI_Session($ktapi, $user);
103   -
  102 +
104 103 $this->ktapi = &$ktapi;
105 104 $this->user = &$user;
106 105 $this->session = $session;
... ... @@ -109,10 +108,10 @@ class KTAPI_UserSession extends KTAPI_Session
109 108  
110 109 // TODO: get documenttransaction to not look at the session variable!
111 110 $_SESSION["userID"] = $user->getId();
112   - $_SESSION["sessionID"] = $this->sessionid;
113   - $this->active = true;
  111 + $_SESSION["sessionID"] = $this->sessionid;
  112 + $this->active = true;
114 113 }
115   -
  114 +
116 115 /**
117 116 * This returns the session string
118 117 *
... ... @@ -122,7 +121,7 @@ class KTAPI_UserSession extends KTAPI_Session
122 121 {
123 122 return $this->session;
124 123 }
125   -
  124 +
126 125 /**
127 126 * This returns the sessionid in the database.
128 127 *
... ... @@ -132,7 +131,7 @@ class KTAPI_UserSession extends KTAPI_Session
132 131 {
133 132 return $this->sessionid;
134 133 }
135   -
  134 +
136 135 /**
137 136 * This resolves the user's ip
138 137 *
... ... @@ -141,38 +140,38 @@ class KTAPI_UserSession extends KTAPI_Session
141 140 */
142 141 function resolveIP()
143 142 {
144   - if (getenv("REMOTE_ADDR"))
  143 + if (getenv("REMOTE_ADDR"))
145 144 {
146 145 $ip = getenv("REMOTE_ADDR");
147   - }
148   - elseif (getenv("HTTP_X_FORWARDED_FOR"))
  146 + }
  147 + elseif (getenv("HTTP_X_FORWARDED_FOR"))
149 148 {
150 149 $forwardedip = getenv("HTTP_X_FORWARDED_FOR");
151 150 list($ip,$ip2,$ip3,$ip4)= split (",", $forwardedip);
152   - }
153   - elseif (getenv("HTTP_CLIENT_IP"))
  151 + }
  152 + elseif (getenv("HTTP_CLIENT_IP"))
154 153 {
155 154 $ip = getenv("HTTP_CLIENT_IP");
156 155 }
157   -
  156 +
158 157 if ($ip == '')
159 158 {
160 159 $ip = '127.0.0.1';
161 160 }
162   -
  161 +
163 162 return $ip;
164 163 }
165   -
  164 +
166 165 /**
167 166 *
168 167 * @access protected
169   - * @static
  168 + * @static
170 169 * @param User $user
171 170 */
172 171 function _check_session(&$user)
173   - {
  172 + {
174 173 $user_id = $user->getId();
175   -
  174 +
176 175 $sql = "SELECT count(*) >= u.max_sessions as over_limit FROM active_sessions ass INNER JOIN users u ON ass.user_id=u.id WHERE ass.user_id = $user_id";
177 176 $row = DBUtil::getOneResult($sql);
178 177 if (PEAR::isError($row))
... ... @@ -182,14 +181,14 @@ class KTAPI_UserSession extends KTAPI_Session
182 181 if (is_null($row))
183 182 {
184 183 return new PEAR_Error('No record found for user?');
185   - }
  184 + }
186 185 if ($row['over_limit'] == 1)
187 186 {
188 187 return new PEAR_Error('Session limit exceeded. Logout of any active sessions.');
189 188 }
190   -
  189 +
191 190 $session = session_id();
192   -
  191 +
193 192 $sessionid = DBUtil::autoInsert('active_sessions',
194 193 array(
195 194 'user_id' => $user_id,
... ... @@ -201,32 +200,32 @@ class KTAPI_UserSession extends KTAPI_Session
201 200 {
202 201 return $sessionid;
203 202 }
204   -
  203 +
205 204 return array($session,$sessionid);
206 205 }
207   -
208   -
  206 +
  207 +
209 208 /**
210 209 * This returns a session object based on authentication credentials.
211 210 *
212 211 * @access public
213   - * @static
  212 + * @static
214 213 * @param string $username
215 214 * @param string $password
216 215 * @return KTAPI_Session
217 216 */
218 217 function &start_session(&$ktapi, $username, $password, $ip=null)
219   - {
  218 + {
220 219 $this->active=false;
221   - if ( empty($username) )
  220 + if ( empty($username) )
222 221 {
223 222 return new PEAR_Error(_kt('The username is empty.'));
224 223 }
225 224  
226 225 $user =& User::getByUsername($username);
227   - if (PEAR::isError($user) || ($user === false))
  226 + if (PEAR::isError($user) || ($user === false))
228 227 {
229   - return new KTAPI_Error(_kt("The user '$username' cound not be found."),$user);
  228 + return new KTAPI_Error(_kt("The user '$username' cound not be found."),$user);
230 229 }
231 230  
232 231 if ( empty($password) )
... ... @@ -240,24 +239,24 @@ class KTAPI_UserSession extends KTAPI_Session
240 239 {
241 240 return new KTAPI_Error(_kt("The password is invalid."),$authenticated);
242 241 }
243   -
  242 +
244 243 if (is_null($ip))
245 244 {
246 245 $ip = '127.0.0.1';
247 246 //$ip = KTAPI_Session::resolveIP();
248 247 }
249   -
  248 +
250 249 list($session,$sessionid) = KTAPI_UserSession::_check_session($user);
251 250 if (PEAR::isError($sessionid))
252 251 {
253 252 return $sessionid;
254 253 }
255   -
256   - $session = &new KTAPI_UserSession($ktapi, $user, $session, $sessionid, $ip);
257   -
  254 +
  255 + $session = &new KTAPI_UserSession($ktapi, $user, $session, $sessionid, $ip);
  256 +
258 257 return $session;
259   - }
260   -
  258 + }
  259 +
261 260 /**
262 261 * This returns an active session.
263 262 *
... ... @@ -267,42 +266,42 @@ class KTAPI_UserSession extends KTAPI_Session
267 266 * @return KTAPI_Session
268 267 */
269 268 function &get_active_session(&$ktapi, $session, $ip)
270   - {
  269 + {
271 270 $sql = "SELECT id, user_id FROM active_sessions WHERE session_id='$session'";
272 271 if (!empty($ip))
273 272 {
274 273 $sql .= " AND ip='$ip'";
275   - }
276   -
  274 + }
  275 +
277 276 $row = DBUtil::getOneResult($sql);
278 277 if (is_null($row) || PEAR::isError($row))
279 278 {
280 279 return new KTAPI_Error(KTAPI_ERROR_SESSION_INVALID, $row);
281 280 }
282   -
  281 +
283 282 $sessionid = $row['id'];
284 283 $userid = $row['user_id'];
285   -
  284 +
286 285 $user = &User::get($userid);
287 286 if (is_null($user) || PEAR::isError($user))
288 287 {
289 288 return new KTAPI_Error(KTAPI_ERROR_USER_INVALID, $user);
290 289 }
291   -
292 290  
293   -
  291 +
  292 +
294 293 $now=date('Y-m-d H:i:s');
295 294 $sql = "UPDATE active_sessions SET last_used='$now' WHERE id=$sessionid";
296 295 DBUtil::runQuery($sql);
297   -
298   -
  296 +
  297 +
299 298 if ($user->isAnonymous())
300   - $session = &new KTAPI_AnonymousSession($ktapi, $user, $session, $sessionid, $ip);
  299 + $session = &new KTAPI_AnonymousSession($ktapi, $user, $session, $sessionid, $ip);
301 300 else
302   - $session = &new KTAPI_UserSession($ktapi, $user, $session, $sessionid, $ip);
  301 + $session = &new KTAPI_UserSession($ktapi, $user, $session, $sessionid, $ip);
303 302 return $session;
304 303 }
305   -
  304 +
306 305 /**
307 306 * This closes the current session.
308 307 *
... ... @@ -315,19 +314,19 @@ class KTAPI_UserSession extends KTAPI_Session
315 314 {
316 315 return $result;
317 316 }
318   -
  317 +
319 318 $this->user = null;
320 319 $this->session = '';
321 320 $this->sessionid = -1;
322 321 $this->active=false;
323 322 }
324   -
  323 +
325 324 }
326 325  
327   -class KTAPI_AnonymousSession extends KTAPI_UserSession
  326 +class KTAPI_AnonymousSession extends KTAPI_UserSession
328 327 {
329 328 function &start_session(&$ktapi, $ip=null)
330   - {
  329 + {
331 330 $user =& User::get(-2);
332 331 if (is_null($user) || PEAR::isError($user) || ($user === false) || !$user->isAnonymous())
333 332 {
... ... @@ -355,16 +354,14 @@ class KTAPI_AnonymousSession extends KTAPI_UserSession
355 354 {
356 355 return $sessionid;
357 356 }
358   -
359   - $session = &new KTAPI_AnonymousSession($ktapi, $user, $session, $sessionid, $ip);
360   -
  357 +
  358 + $session = &new KTAPI_AnonymousSession($ktapi, $user, $session, $sessionid, $ip);
  359 +
361 360 return $session;
362 361 }
363 362 }
364 363  
365   -
366   -
367   -class KTAPI_SystemSession extends KTAPI_Session
  364 +class KTAPI_SystemSession extends KTAPI_Session
368 365 {
369 366 function KTAPI_SystemSession(&$ktapi, &$user)
370 367 {
... ...
ktapi/ktapi.inc.php
1 1 <?
2 2 /**
3 3 * $Id$
4   - *
  4 + *
5 5 * Implements a cleaner wrapper API for KnowledgeTree.
6 6 *
7 7 * The contents of this file are subject to the KnowledgeTree Public
8 8 * License Version 1.1.2 ("License"); You may not use this file except in
9 9 * compliance with the License. You may obtain a copy of the License at
10 10 * http://www.knowledgetree.com/KPL
11   - *
  11 + *
12 12 * Software distributed under the License is distributed on an "AS IS"
13 13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
14 14 * See the License for the specific language governing rights and
... ... @@ -19,9 +19,9 @@
19 19 * (ii) the KnowledgeTree copyright notice
20 20 * in the same form as they appear in the distribution. See the License for
21 21 * requirements.
22   - *
  22 + *
23 23 * The Original Code is: KnowledgeTree Open Source
24   - *
  24 + *
25 25 * The Initial Developer of the Original Code is The Jam Warehouse Software
26 26 * (Pty) Ltd, trading as KnowledgeTree.
27 27 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -42,7 +42,7 @@ require_once(KTAPI_DIR .&#39;/KTAPIConstants.inc.php&#39;);
42 42 require_once(KTAPI_DIR .'/KTAPISession.inc.php');
43 43 require_once(KTAPI_DIR .'/KTAPIFolder.inc.php');
44 44 require_once(KTAPI_DIR .'/KTAPIDocument.inc.php');
45   -
  45 +
46 46 class KTAPI_FolderItem
47 47 {
48 48 /**
... ... @@ -51,10 +51,10 @@ class KTAPI_FolderItem
51 51 * @access protected
52 52 * @var KTAPI
53 53 */
54   - var $ktapi;
55   -
  54 + var $ktapi;
  55 +
56 56 function &can_user_access_object_requiring_permission(&$object, $permission)
57   - {
  57 + {
58 58 return $this->ktapi->can_user_access_object_requiring_permission($object, $permission);
59 59 }
60 60 }
... ... @@ -67,7 +67,7 @@ class KTAPI_Error extends PEAR_Error
67 67 {
68 68 parent::PEAR_Error($msg . ' - ' . $obj->getMessage());
69 69 }
70   - else
  70 + else
71 71 {
72 72 parent::PEAR_Error($msg);
73 73 }
... ... @@ -83,18 +83,18 @@ class KTAPI
83 83 * @var KTAPI_Session
84 84 */
85 85 var $session = null;
86   -
  86 +
87 87 /**
88 88 * This returns the current session.
89 89 *
90 90 * @access public
91 91 * @return KTAPI_Session
92   - */
  92 + */
93 93 function &get_session()
94 94 {
95 95 return $this->session;
96 96 }
97   -
  97 +
98 98 /**
99 99 * This returns the session user.
100 100 *
... ... @@ -102,22 +102,22 @@ class KTAPI
102 102 * @return User
103 103 */
104 104 function & get_user()
105   - {
  105 + {
106 106 $ktapi_session = $this->get_session();
107 107 if (is_null($ktapi_session) || PEAR::isError($ktapi_session))
108 108 {
109 109 return new PEAR_Error(KTAPI_ERROR_SESSION_INVALID);
110 110 }
111   -
  111 +
112 112 $user = $ktapi_session->get_user();
113 113 if (is_null($user) || PEAR::isError($user))
114 114 {
115 115 return new PEAR_Error(KTAPI_ERROR_USER_INVALID);
116   - }
117   -
  116 + }
  117 +
118 118 return $user;
119 119 }
120   -
  120 +
121 121 /**
122 122 * This returns a permission.
123 123 *
... ... @@ -127,16 +127,16 @@ class KTAPI
127 127 * @return KTPermission
128 128 */
129 129 function &get_permission($permission)
130   - {
  130 + {
131 131 $permission = & KTPermission::getByName($permission);
132 132 if (is_null($permission) || PEAR::isError($permission))
133 133 {
134 134 return new PEAR_Error(KTAPI_ERROR_PERMISSION_INVALID);
135 135 }
136   -
137   - return $permission;
  136 +
  137 + return $permission;
138 138 }
139   -
  139 +
140 140 /**
141 141 * This checks if a user can access an object with a certain permission.
142 142 *
... ... @@ -148,34 +148,34 @@ class KTAPI
148 148 function can_user_access_object_requiring_permission(&$object, $permission)
149 149 {
150 150 assert(!is_null($object));
151   - assert(is_a($object,'DocumentProxy') || is_a($object,'FolderProxy') || is_a($object,'Document') || is_a($object,'Folder'));
152   -
  151 + assert(is_a($object,'DocumentProxy') || is_a($object,'FolderProxy') || is_a($object,'Document') || is_a($object,'Folder'));
  152 +
153 153 $permission = &KTAPI::get_permission($permission);
154 154 if (is_null($permission) || PEAR::isError($permission))
155 155 {
156 156 return $permission;
157 157 }
158   -
  158 +
159 159 $user = &KTAPI::get_user();
160 160 if (is_null($user) || PEAR::isError($user))
161 161 {
162 162 return $user;
163   - }
  163 + }
164 164  
165   - if (!KTPermissionUtil::userHasPermissionOnItem($user, $permission, $object))
  165 + if (!KTPermissionUtil::userHasPermissionOnItem($user, $permission, $object))
166 166 {
167 167 return new PEAR_Error(KTAPI_ERROR_INSUFFICIENT_PERMISSIONS);
168 168 }
169   -
170   - return $user;
  169 +
  170 + return $user;
171 171 }
172   -
  172 +
173 173 /**
174 174 * This returns a session object based on a session string.
175 175 *
176 176 * @access public
177 177 * @param string $session
178   - * @return KTAPI_Session
  178 + * @return KTAPI_Session
179 179 */
180 180 function & get_active_session($session, $ip=null)
181 181 {
... ... @@ -183,33 +183,33 @@ class KTAPI
183 183 {
184 184 return new PEAR_Error('A session is currently active.');
185 185 }
186   -
  186 +
187 187 $session = &KTAPI_UserSession::get_active_session($this, $session, $ip);
188   -
  188 +
189 189 if (is_null($session) || PEAR::isError($session))
190 190 {
191 191 return new PEAR_Error('Session is invalid');
192 192 }
193   -
  193 +
194 194 $this->session = &$session;
195 195 return $session;
196 196 }
197   -
  197 +
198 198 /**
199 199 * This returns a session object based on authentication credentials.
200 200 *
201 201 * @access public
202 202 * @param string $username
203 203 * @param string $password
204   - * @return KTAPI_Session
  204 + * @return KTAPI_Session
205 205 */
206 206 function & start_session($username, $password, $ip=null)
207   - {
  207 + {
208 208 if (!is_null($this->session))
209 209 {
210 210 return new PEAR_Error('A session is currently active.');
211 211 }
212   -
  212 +
213 213 $session = &KTAPI_UserSession::start_session($this, $username, $password, $ip);
214 214 if (is_null($session))
215 215 {
... ... @@ -220,23 +220,23 @@ class KTAPI
220 220 return new PEAR_Error('Session is invalid. ' . $session->getMessage());
221 221 }
222 222 $this->session = &$session;
223   -
  223 +
224 224 return $session;
225 225 }
226   -
227   -
  226 +
  227 +
228 228 function & start_system_session()
229 229 {
230 230 $user = User::get(1);
231   -
  231 +
232 232 $session = & new KTAPI_SystemSession($this, $user);
233 233 $this->session = &$session;
234   -
  234 +
235 235 return $session;
236 236 }
237   -
238   -
239   -
  237 +
  238 +
  239 +
240 240 /**
241 241 * Starts an anonymous session.
242 242 *
... ... @@ -249,7 +249,7 @@ class KTAPI
249 249 {
250 250 return new PEAR_Error('A session is currently active.');
251 251 }
252   -
  252 +
253 253 $session = &KTAPI_AnonymousSession::start_session($this, $ip);
254 254 if (is_null($session))
255 255 {
... ... @@ -260,11 +260,11 @@ class KTAPI
260 260 return new PEAR_Error('Session is invalid. ' . $session->getMessage());
261 261 }
262 262 $this->session = &$session;
263   -
  263 +
264 264 return $session;
265 265 }
266   -
267   -
  266 +
  267 +
268 268 /**
269 269 * Obtains the root folder.
270 270 *
... ... @@ -275,7 +275,7 @@ class KTAPI
275 275 {
276 276 return $this->get_folder_by_id(1);
277 277 }
278   -
  278 +
279 279 /**
280 280 * Obtains the folder using a folder id.
281 281 *
... ... @@ -287,12 +287,12 @@ class KTAPI
287 287 {
288 288 if (is_null($this->session))
289 289 {
290   - return new PEAR_Error('A session is not active');
  290 + return new PEAR_Error('A session is not active');
291 291 }
292   -
  292 +
293 293 return KTAPI_Folder::get($this, $folderid);
294 294 }
295   -
  295 +
296 296 /**
297 297 * This returns a refererence to a document based on document id.
298 298 *
... ... @@ -301,10 +301,10 @@ class KTAPI
301 301 * @return KTAPI_Document
302 302 */
303 303 function &get_document_by_id($documentid)
304   - {
  304 + {
305 305 return KTAPI_Document::get($this, $documentid);
306 306 }
307   -
  307 +
308 308 /**
309 309 * This returns a document type id based on the name.
310 310 *
... ... @@ -324,7 +324,19 @@ class KTAPI
324 324 list($documenttypeid) = $row['id'];
325 325 return $documenttypeid;
326 326 }
327   -
  327 +
  328 + function get_link_type_id($linktype)
  329 + {
  330 + $sql = "SELECT id FROM document_link_types WHERE name='$linktype'";
  331 + $row = DBUtil::getOneResult($sql);
  332 + if (is_null($row) || PEAR::isError($row))
  333 + {
  334 + return new PEAR_Error(KTAPI_ERROR_DOCUMENT_LINK_TYPE_INVALID);
  335 + }
  336 + list($typeid) = $row['id'];
  337 + return $typeid;
  338 + }
  339 +
328 340 /**
329 341 * Returns an array of document types.
330 342 *
... ... @@ -340,16 +352,122 @@ class KTAPI
340 352 {
341 353 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
342 354 }
343   -
  355 +
344 356 $result = array();
345 357 foreach($rows as $row)
346 358 {
347 359 $result[] = $row['name'];
348 360 }
349 361  
350   - return $result;
  362 + return $result;
  363 + }
  364 +
  365 + function get_document_link_types()
  366 + {
  367 + $sql = "SELECT name FROM document_link_types order by name";
  368 + $rows = DBUtil::getResultArray($sql);
  369 + if (is_null($rows) || PEAR::isError($rows))
  370 + {
  371 + return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
  372 + }
  373 +
  374 + $result = array();
  375 + foreach($rows as $row)
  376 + {
  377 + $result[] = $row['name'];
  378 + }
  379 +
  380 + return $result;
  381 + }
  382 +
  383 + function get_document_type_metadata($document_type='Default')
  384 + {
  385 + // now get document type specifc ids
  386 + $typeid =$this->get_documenttypeid($document_type);
  387 + if (PEAR::isError($typeid))
  388 + {
  389 + $response['message'] = $typeid->getMessage();
  390 + return new SOAP_Value('return',"{urn:$this->namespace}kt_metadata_response", $response);
  391 + }
  392 +
  393 + $doctype_ids = KTFieldset::getForDocumentType($typeid, array('ids' => false));
  394 + if (PEAR::isError($doctype_ids))
  395 + {
  396 + $response['message'] = $generic_ids->getMessage();
  397 + return new SOAP_Value('return',"{urn:$this->namespace}kt_metadata_response", $response);
  398 + }
  399 +
  400 + // first get generic ids
  401 + $generic_ids = KTFieldset::getGenericFieldsets(array('ids' => false));
  402 + if (PEAR::isError($generic_ids))
  403 + {
  404 + $response['message'] = $generic_ids->getMessage();
  405 + return new SOAP_Value('return',"{urn:$this->namespace}kt_metadata_response", $response);
  406 + }
  407 +
  408 + // lets merge the results
  409 + $fieldsets = kt_array_merge($generic_ids, $doctype_ids);
  410 +
  411 + $results = array();
  412 +
  413 + foreach ($fieldsets as $fieldset)
  414 + {
  415 + if ($fieldset->getIsConditional()) { /* this is not implemented...*/ continue; }
  416 +
  417 + $fields = $fieldset->getFields();
  418 + $result = array(
  419 + 'fieldset' => $fieldset->getName(),
  420 + 'description' => $fieldset->getDescription()
  421 + );
  422 +
  423 + $fieldsresult = array();
  424 +
  425 + foreach ($fields as $field)
  426 + {
  427 + $value = 'n/a';
  428 +
  429 +
  430 + $controltype = 'string';
  431 + if ($field->getHasLookup())
  432 + {
  433 + $controltype = 'lookup';
  434 + if ($field->getHasLookupTree())
  435 + {
  436 + $controltype = 'tree';
  437 + }
  438 + }
  439 +
  440 + switch ($controltype)
  441 + {
  442 + case 'lookup':
  443 + $selection = KTAPI::get_metadata_lookup($field->getId());
  444 + break;
  445 + case 'tree':
  446 + $selection = KTAPI::get_metadata_tree($field->getId());
  447 + break;
  448 + default:
  449 + $selection= array();
  450 + }
  451 +
  452 +
  453 + $fieldsresult[] = array(
  454 + 'name' => $field->getName(),
  455 + 'required' => $field->getIsMandatory(),
  456 + 'value' => $value,
  457 + 'description' => $field->getDescription(),
  458 + 'control_type' => $controltype,
  459 + 'selection' => $selection
  460 + );
  461 + }
  462 + $result['fields'] = $fieldsresult;
  463 + $results [] = $result;
  464 + }
  465 +
  466 + return $results;
351 467 }
352   -
  468 +
  469 +
  470 +
353 471 /**
354 472 * Returns an array of username/name combinations.
355 473 *
... ... @@ -366,8 +484,8 @@ class KTAPI
366 484 return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
367 485 }
368 486 return $rows;
369   - }
370   -
  487 + }
  488 +
371 489 /**
372 490 * This returns an array for a lookup.
373 491 *
... ... @@ -388,10 +506,10 @@ class KTAPI
388 506 foreach($rows as $row)
389 507 {
390 508 $results[] = $row['name'];
391   - }
  509 + }
392 510 return $results;
393 511 }
394   -
  512 +
395 513 /**
396 514 * This returns a metadata tree.
397 515 *
... ... @@ -421,7 +539,7 @@ class KTAPI
421 539 }
422 540 return $results;*/
423 541 }
424   -
  542 +
425 543 /**
426 544 * This returns a metadata tree.
427 545 *
... ... @@ -432,13 +550,13 @@ class KTAPI
432 550 */
433 551 function get_metadata_tree($fieldid)
434 552 {
435   - return KTAPI::_load_metadata_tree($fieldid);
436   - }
437   -
  553 + return KTAPI::_load_metadata_tree($fieldid);
  554 + }
  555 +
438 556 /**
439 557 * Returns a list of workflows that are active.
440 558 *
441   - * @static
  559 + * @static
442 560 * @access public
443 561 * @return array
444 562 */
... ... @@ -457,7 +575,7 @@ class KTAPI
457 575 }
458 576 return $results;
459 577 }
460   -
  578 +
461 579 }
462 580  
463 581 ?>
464 582 \ No newline at end of file
... ...