ktapi.inc.php
42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
<?php
/**
* Implements a cleaner wrapper API for KnowledgeTree.
*
* KnowledgeTree Community Edition
* Document Management Made Simple
* Copyright (C) 2008,2009 KnowledgeTree Inc.
* Portions copyright The Jam Warehouse Software (Pty) Limited
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
*
* @copyright 2008-2009, KnowledgeTree Inc.
* @license GNU General Public License version 3
* @author KnowledgeTree Team
* @package KTAPI
* @version Version 0.9
*/
$_session_id = session_id();
if (empty($_session_id)) session_start();
unset($_session_id);
require_once(realpath(dirname(__FILE__) . '/../config/dmsDefaults.php'));
require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
require_once(KT_LIB_DIR . '/foldermanagement/folderutil.inc.php');
require_once(KT_LIB_DIR . '/browse/DocumentCollection.inc.php');
require_once(KT_LIB_DIR . "/browse/columnregistry.inc.php");
define('KTAPI_DIR',KT_DIR . '/ktapi');
require_once(KTAPI_DIR .'/KTAPIConstants.inc.php');
require_once(KTAPI_DIR .'/KTAPISession.inc.php');
require_once(KTAPI_DIR .'/KTAPIFolder.inc.php');
require_once(KTAPI_DIR .'/KTAPIDocument.inc.php');
require_once(KTAPI_DIR .'/KTAPIAcl.inc.php');
require_once(KTAPI_DIR .'/KTAPICollection.inc.php');
require_once(KTAPI_DIR .'/KTAPIBulkActions.inc.php');
/**
* This class defines functions that MUST exist in the inheriting class
*
* @abstract
* @author KnowledgeTree Team
* @package KTAPI
* @version Version 0.9
*/
abstract class KTAPI_FolderItem
{
/**
* This is a reference to the core KTAPI controller
*
* @author KnowledgeTree Team
* @access protected
* @var object $ktapi The KTAPI object
*/
protected $ktapi;
/**
* This checks if a user can access an object with a certain permission.
*
* @author KnowledgeTree Team
* @access public
* @param object $object The object the user is trying to access
* @param string $permission The permissions string
* @return object $user The User object
*/
public function &can_user_access_object_requiring_permission(&$object, $permission)
{
$user = $this->ktapi->can_user_access_object_requiring_permission($object, $permission);
return $user;
}
public abstract function getObject();
public abstract function getRoleAllocation();
public abstract function getPermissionAllocation();
public abstract function isSubscribed();
public abstract function unsubscribe();
public abstract function subscribe();
}
/**
* This class extends the PEAR_Error class for errors in the KTAPI class
*
* @author KnowledgeTree Team
* @package KTAPI
* @version Version 0.9
*/
class KTAPI_Error extends PEAR_Error
{
/**
* This method determines if there is an error in the object itself or just a common error
*
* @author KnowledgeTree Team
* @access public
* @return VOID
*/
public function KTAPI_Error($msg, $obj = null)
{
if (PEAR::isError($obj))
{
parent::PEAR_Error($msg . ' - ' . $obj->getMessage());
}
else
{
parent::PEAR_Error($msg);
}
}
}
/**
* This class extends the KTAPI_Error class for errors in the KTAPI Document class
*
* @author KnowledgeTree Team
* @package KTAPI
* @version Version 0.9
*/
class KTAPI_DocumentTypeError extends KTAPI_Error
{
}
/**
* This is the main KTAPI class
*
* @author KnowledgeTree Team
* @package KTAPI
* @version Version 0.9
*/
class KTAPI
{
/**
* This is the current session.
*
* @author KnowledgeTree Team
* @access protected
* @var object $session The KTAPI_Session object
*/
protected $session = null;
/**
* This returns the current session.
*
* @author KnowledgeTree Team
* @access public
* @return object $session The KTAPI_Session object
*/
public function &get_session()
{
$session = $this->session;
return $session;
}
/**
* This returns the session user object or an error object.
*
* @author KnowledgeTree Team
* @access public
* @return object $user SUCCESS - The User object | FAILURE - an error object
*/
public function & get_user()
{
$ktapi_session = $this->get_session();
if (is_null($ktapi_session) || PEAR::isError($ktapi_session))
{
$error = new PEAR_Error(KTAPI_ERROR_SESSION_INVALID);
return $error;
}
$user = $ktapi_session->get_user();
if (is_null($user) || PEAR::isError($user))
{
$error = new PEAR_Error(KTAPI_ERROR_USER_INVALID);
return $error;
}
return $user;
}
function get_columns_for_view($view = 'ktcore.views.browse') {
$ktapi_session = $this->get_session();
if (is_null($ktapi_session) || PEAR::isError($ktapi_session))
{
$error = new PEAR_Error(KTAPI_ERROR_SESSION_INVALID);
return $error;
}
$collection = new KTAPI_Collection();
return $collection->get_columns($view);
}
/**
* This returns a permission object or an error object.
*
* @author KnowledgeTree Team
* @access public
* @param string $permission The permissions string
* @return object $permissions SUCCESS - The KTPermission object | FAILURE - an error object
*/
public function &get_permission($permission)
{
$permissions = & KTPermission::getByName($permission);
if (is_null($permissions) || PEAR::isError($permissions))
{
$error = new PEAR_Error(KTAPI_ERROR_PERMISSION_INVALID);
return $error;
}
return $permissions;
}
/**
* Returns an associative array of permission namespaces and their names
*
* @access public
* @return array
*/
public function get_permission_types() {
$types = array();
$list = KTAPI_Permission::getList();
foreach($list as $val) {
$types[$val->getNameSpace()] = $val->getName();
}
return $types;
}
/**
* Returns folder permissions
*
* @access public
* @param string
* @param int
*
*/
public function get_folder_permissions($username, $folder_id) {
if (is_null($this->session))
{
$error = new PEAR_Error('A session is not active');
return $error;
}
/* We need to create a new instance of KTAPI to get another user */
$user_ktapi = new KTAPI();
$user_ktapi->start_system_session($username);
$folder = KTAPI_Folder::get($user_ktapi, $folder_id);
$permissions = $folder->getPermissionAllocation();
$user_ktapi->session_logout();
return $permissions->permissions;
}
/**
* Add folder permission
*
* @access public
* @param string
* @param string
* @param int
*
*/
public function add_folder_permissions($username, $folder_id, $namespace) {
if (is_null($this->session))
{
$error = new PEAR_Error('A session is not active');
return $error;
}
/* First check that user trying to add permission can actually do so */
$folder = KTAPI_Folder::get($this, $folder_id);
$permissions = $folder->getPermissionAllocation();
$detail = $permissions->permissions;
if(!in_array("Manage security", $detail)) {
return new PEAR_Error("User does not have permission to manage security");
}
/* We need to create a new instance of KTAPI to get another user */
$user_ktapi = new KTAPI();
$user_ktapi->start_system_session($username);
$folder = KTAPI_Folder::get($user_ktapi, $folder_id);
if(PEAR::isError($folder))
{
$user_ktapi->session_logout();
return $folder;
}
$permission = KTAPI_Permission::getByNamespace($namespace);
if(PEAR::isError($permission)) {
$user_ktapi->session_logout();
return $permission;
}
$user = KTAPI_User::getByUsername($username);
if(PEAR::isError($user)) {
$user_ktapi->session_logout();
return $user;
}
$permissions = $folder->getPermissionAllocation();
$permissions->add($user, $permissions);
$permissions->save();
}
/**
* This checks if a user can access an object with a certain permission.
*
* @author KnowledgeTree Team
* @access public
* @param object $object The internal document object or a folder object
* @param string $permission The permissions string
* @return object $user SUCCESS - The User object | FAILURE - an error object
*/
public function can_user_access_object_requiring_permission(&$object, $permission)
{
assert(!is_null($object));
assert(is_a($object,'DocumentProxy') || is_a($object,'FolderProxy') || is_a($object,'Document') || is_a($object,'Folder'));
/*
if(is_null($object) || PEAR::isError($object)){
$error = $object;
return $object;
}
if(!is_a($object,'DocumentProxy') && !is_a($object,'FolderProxy') && !is_a($object,'Document') && !is_a($object,'Folder')){
$error = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
return $error;
}
*/
$permissions = &KTAPI::get_permission($permission);
if (is_null($permissions) || PEAR::isError($permissions))
{
$error = $permissions;
return $error;
}
$user = &KTAPI::get_user();
if (is_null($user) || PEAR::isError($user))
{
$error = $user;
return $error;
}
if (!KTPermissionUtil::userHasPermissionOnItem($user, $permission, $object))
{
$error = new PEAR_Error(KTAPI_ERROR_INSUFFICIENT_PERMISSIONS);
return $error;
}
return $user;
}
/**
* Returns the version id for the associated version number
*
* @param int $document_id
* @param string $version_number
* @return int
*/
function get_url_version_number($document_id, $version_number) {
$ktapi_session = $this->get_session();
if (is_null($ktapi_session) || PEAR::isError($ktapi_session))
{
$error = new PEAR_Error(KTAPI_ERROR_SESSION_INVALID);
return $error;
}
$document_id = sanitizeForSQL($document_id);
$version_number = sanitizeForSQL($version_number);
$pos = strpos($version_number, ".");
$major = substr($version_number, 0, $pos);
$minor = substr($version_number, ($pos+1));
$sql = "SELECT id FROM document_content_version WHERE document_id = {$document_id} AND major_version = '{$major}' AND minor_version = '{$minor}'";
$row = DBUtil::getOneResult($sql);
$row = (int)$row['id'];
if (is_null($row) || PEAR::isError($row))
{
$row = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $row);
}
return $row;
}
/**
* Search for documents matching the oem_no.
*
* Note that oem_no is associated with a document and not with version of file (document content).
* oem_no is set on a document using document::update_sysdata().
*
* @author KnowledgeTree Team
* @access public
* @param string $oem_no The oem number
* @param boolean $idsOnly Defaults to true
* @return array|object $results SUCCESS - the list of documents | FAILURE - and error object
*/
public function get_documents_by_oem_no($oem_no, $idsOnly=true)
{
$sql = array("SELECT id FROM documents WHERE oem_no=?",$oem_no);
$rows = DBUtil::getResultArray($sql);
if (is_null($rows) || PEAR::isError($rows))
{
$results = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
}
else
{
$results = array();
foreach($rows as $row)
{
$documentid = $row['id'];
$results[] = $idsOnly?$documentid:KTAPI_Document::get($this, $documentid);
}
}
return $results;
}
/**
* This returns a session object based on a session id.
*
* @author KnowledgeTree Team
* @access public
* @param string $session The sesssion id
* @param string $ip The users ip address
* @param string $app The originating application type Webservices|Webdav|Webapp
* @return object $session_object SUCCESS - The KTAPI_Session object | FAILURE - an error object
*/
public function & get_active_session($session, $ip=null, $app='ws')
{
if (!is_null($this->session))
{
$error = new PEAR_Error('A session is currently active.');
return $error;
}
$session_object = &KTAPI_UserSession::get_active_session($this, $session, $ip, $app);
if (is_null($session_object) || PEAR::isError($session_object))
{
$error = new PEAR_Error('Session is invalid');
return $error;
}
$this->session = &$session_object;
return $session_object;
}
/**
* Creates a session and returns the session object based on authentication credentials.
*
* @author KnowledgeTree Team
* @access public
* @param string $username The users username
* @param string $password The password of the user
* @param string $ip The users ip address
* @param string $app The originating application type Webservices|Webdav|Webapp
* @return object $session SUCCESS - The KTAPI_Session object | FAILURE - an error object
*/
public function & start_session($username, $password, $ip=null, $app='ws')
{
if (!is_null($this->session))
{
$error = new PEAR_Error('A session is currently active.');
return $error;
}
$session = &KTAPI_UserSession::start_session($this, $username, $password, $ip, $app);
if (is_null($session))
{
$error = new PEAR_Error('Session is null.');
return $error;
}
if (PEAR::isError($session))
{
$error = new PEAR_Error('Session is invalid. ' . $session->getMessage());
return $error;
}
$this->session = &$session;
return $session;
}
/**
* start a root session.
*
* @author KnowledgeTree Team
* @access public
* @return object $session The KTAPI_SystemSession
*/
public function & start_system_session($username = null)
{
if(is_null($username))
{
$user = User::get(1);
} else {
$user = User::getByUserName($username);
}
if(PEAR::isError($user)) {
return new PEAR_Error('Username invalid');
}
$session = & new KTAPI_SystemSession($this, $user);
$this->session = &$session;
return $session;
}
/**
* Starts an anonymous session.
*
* @author KnowledgeTree Team
* @param string $ip The users ip address
* @return object $session SUCCESS - The KTAPI_Session object | FAILURE - an error object
*/
function &start_anonymous_session($ip=null)
{
if (!is_null($this->session))
{
$error = new PEAR_Error('A session is currently active.');
return $error;
}
$session = &KTAPI_AnonymousSession::start_session($this, $ip);
if (is_null($session))
{
$error = new PEAR_Error('Session is null.');
return $error;
}
if (PEAR::isError($session))
{
$error = new PEAR_Error('Session is invalid. ' . $session->getMessage());
return $error;
}
$this->session = &$session;
return $session;
}
function session_logout()
{
$this->session->logout();
$this->session = null;
}
/**
* Gets the root folder.
* Root folder id is always equal to '1'
*
* @author KnowledgeTree Team
* @access public
* @return object $folder The KTAPI_Folder object
*/
public function &get_root_folder()
{
$folder = $this->get_folder_by_id(1);
return $folder;
}
/**
* Obtains the folder using a folder id.
*
* @author KnowledgeTree Team
* @access public
* @param integer $folderid The id of the folder
* @return object $session SUCCESS - The KTAPI_Folder object | FAILURE - an error object
*/
public function &get_folder_by_id($folderid)
{
if (is_null($this->session))
{
$error = new PEAR_Error('A session is not active');
return $error;
}
$folder = KTAPI_Folder::get($this, $folderid);
return $folder;
}
/**
* Gets the the folder object based on the folder name
*
* @author KnowledgeTree Team
* @access public
* @param string $foldername The folder name
* @return object $folder The KTAPI_Folder object
*/
public function &get_folder_by_name($foldername)
{
$folder = KTAPI_Folder::_get_folder_by_name($this, $foldername, 1);
return $folder;
}
/**
* This returns a refererence to a document based on document id.
*
* @author KnowledgeTree Team
* @access public
* @param integer $documentid The document id
* @return object $document The KTAPI_Document object
*/
public function &get_document_by_id($documentid)
{
$document = KTAPI_Document::get($this, $documentid);
return $document;
}
/**
* This returns a document type id based on the name or an error object.
*
* @author KnowledgeTree Team
* @access public
* @param string $documenttype The document type
* @return integer|object $result SUCCESS - the document type id | FAILURE - an error object
*/
public function get_documenttypeid($documenttype)
{
$sql = array("SELECT id FROM document_types_lookup WHERE name=? and disabled=0", $documenttype);
$row = DBUtil::getOneResult($sql);
if (is_null($row) || PEAR::isError($row))
{
$result = new KTAPI_DocumentTypeError(KTAPI_ERROR_DOCUMENT_TYPE_INVALID, $row);
}
else
{
$result = $row['id'];
}
return $result;
}
/**
* Returns the id for a link type or an error object.
*
* @author KnowledgeTree Team
* @access public
* @param string $linktype The link type
* @return integer|object $result SUCCESS - the link type id | FAILURE - an error object
*/
public function get_link_type_id($linktype)
{
$sql = array("SELECT id FROM document_link_types WHERE name=?",$linktype);
$row = DBUtil::getOneResult($sql);
if (is_null($row) || PEAR::isError($row))
{
$result = new PEAR_Error(KTAPI_ERROR_DOCUMENT_LINK_TYPE_INVALID);
}
else
{
$result = $row['id'];
}
return $result;
}
/**
* Returns an array of document types or an error object.
*
* @author KnowledgeTree Team
* @access public
* @return array|object $results SUCCESS - the array of document types | FAILURE - an error object
*/
public function get_documenttypes()
{
$sql = "SELECT name FROM document_types_lookup WHERE disabled=0";
$rows = DBUtil::getResultArray($sql);
if (is_null($rows) || PEAR::isError($rows))
{
$results = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
}
else
{
$results = array();
foreach($rows as $row)
{
$results[] = $row['name'];
}
}
return $results;
}
/**
* Returns an array of document link types or an error object.
*
* @author KnowledgeTree Team
* @access public
* @return array|object $results SUCCESS - the array of document link types | FAILURE - an error object
*/
public function get_document_link_types()
{
$sql = "SELECT name FROM document_link_types order by name";
$rows = DBUtil::getResultArray($sql);
if (is_null($rows) || PEAR::isError($rows))
{
$results = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
}
else
{
$results = array();
foreach($rows as $row)
{
$results[] = $row['name'];
}
}
return $results;
}
/**
* This should actually not be in ktapi, but in webservice
* This method gets metadata fieldsets based on the document type
*
* @author KnowledgeTree Team
* @access public
* @param string $document_type The type of document
* @return mixed Error object|SOAP object|Array of fieldsets
*/
public function get_document_type_metadata($document_type='Default')
{
// now get document type specifc ids
$typeid =$this->get_documenttypeid($document_type);
if (is_a($typeid, 'KTAPI_DocumentTypeError'))
{
return $typeid;
}
if (is_null($typeid) || PEAR::isError($typeid))
{
$response['message'] = $typeid->getMessage();
return new SOAP_Value('return',"{urn:$this->namespace}kt_metadata_response", $response);
}
$doctype_ids = KTFieldset::getForDocumentType($typeid, array('ids' => false));
if (is_null($doctype_ids) || PEAR::isError($doctype_ids))
{
$response['message'] = $generic_ids->getMessage();
return new SOAP_Value('return',"{urn:$this->namespace}kt_metadata_response", $response);
}
// first get generic ids
$generic_ids = KTFieldset::getGenericFieldsets(array('ids' => false));
if (is_null($generic_ids) || PEAR::isError($generic_ids))
{
$response['message'] = $generic_ids->getMessage();
return new SOAP_Value('return',"{urn:$this->namespace}kt_metadata_response", $response);
}
// lets merge the results
$fieldsets = kt_array_merge($generic_ids, $doctype_ids);
$results = array();
foreach ($fieldsets as $fieldset)
{
if ($fieldset->getIsConditional()) { /* this is not implemented...*/ continue; }
$fields = $fieldset->getFields();
$result = array(
'fieldset' => $fieldset->getName(),
'description' => $fieldset->getDescription()
);
$fieldsresult = array();
foreach ($fields as $field)
{
$value = 'n/a';
$controltype = 'string';
if ($field->getHasLookup())
{
$controltype = 'lookup';
if ($field->getHasLookupTree())
{
$controltype = 'tree';
}
}
switch ($controltype)
{
case 'lookup':
$selection = KTAPI::get_metadata_lookup($field->getId());
break;
case 'tree':
$selection = KTAPI::get_metadata_tree($field->getId());
break;
default:
$selection= array();
}
$fieldsresult[] = array(
'name' => $field->getName(),
'required' => $field->getIsMandatory(),
'value' => $value,
'description' => $field->getDescription(),
'control_type' => $controltype,
'selection' => $selection
);
}
$result['fields'] = $fieldsresult;
$results [] = $result;
}
return $results;
}
/**
* Returns an array of username/name combinations or an error object.
*
* @author KnowledgeTree Team
* @access public
* @return array|object $results SUCCESS - the array of all username/name combinations | FAILURE - an error object
*/
public function get_users()
{
$sql = "SELECT username, name FROM users WHERE disabled=0";
$rows = DBUtil::getResultArray($sql);
if (is_null($rows) || PEAR::isError($rows))
{
$results = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
}
else
{
$results = $rows;
}
return $results;
}
/**
* This returns an array for a metadata tree lookup or an error object.
*
* @author KnowledgeTree Team
* @access public
* @param integer $fieldid The field id to get metadata for
* @return array|object $results SUCCESS - the array of metedata for the field | FAILURE - an error object
*/
public function get_metadata_lookup($fieldid)
{
$sql = "SELECT name FROM metadata_lookup WHERE disabled=0 AND document_field_id=$fieldid ORDER BY name";
$rows = DBUtil::getResultArray($sql);
if (is_null($rows) || PEAR::isError($rows))
{
$results = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
}
else
{
$results = array();
foreach($rows as $row)
{
$results[] = $row['name'];
}
}
return $results;
}
/**
* This returns a metadata tree or an error object.
*
* @author KnowledgeTree Team
* @access private
* @param integer $fieldid The field id of the document to get data for
* @param integer $parentid The id of the parent of the metadata tree
* @return array|object $results SUCCESS - the array of metadata for the field | FAILURE - an error object
*/
private function _load_metadata_tree($fieldid, $parentid=0)
{
$results = KTAPI::get_metadata_lookup($fieldid);
return $results;
/*
$sql = "SELECT id, name FROM metadata_lookup_tree WHERE document_field_id=$fieldid AND metadata_lookup_tree_parent=$parentid";
$rows = DBUtil::getResultArray($sql);
if (is_null($rows) || PEAR::isError($rows))
{
return new PEAR_Error(KTAPI_ERROR_INTERNAL_ERROR);
}
$results=array();
foreach($rows as $row)
{
$result=array(
'name' => $row['name'],
'children' => load($fieldid, $row['id'])
);
$results[] = $result;
}
return $results;*/
}
/**
* This returns a metadata tree or an error object.
*
* @author KnowledgeTree Team
* @access public
* @param integer $fieldid The id of the tree field to get the metadata for
* @return array|object $results SUCCESS - the array of metadata for the field | FAILURE - an error object
*/
public function get_metadata_tree($fieldid)
{
$results = KTAPI::_load_metadata_tree($fieldid);
return $results;
}
/**
* Returns a list of active workflows or an error object.
*
* @author KnowledgeTree Team
* @access public
* @return array|object $results SUCCESS - the array of active workflows | FAILURE - an error object
*/
public function get_workflows()
{
$sql = "SELECT name FROM workflows WHERE enabled=1";
$rows=DBUtil::getResultArray($sql);
if (is_null($rows) || PEAR::isError($rows))
{
$results = new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $rows);
}
else
{
$results = array();
foreach($rows as $row)
{
$results[] = $row['name'];
}
}
return $results;
}
/**
* Get the users subscriptions
*
* @author KnowledgeTree Team
* @access public
* @param string $filter
* @return array of Subscription
*/
public function getSubscriptions($filter=null)
{
$user = $this->get_user();
$userId = $user->getID();
$subscriptions = SubscriptionManager::listSubscriptions($userId);
return $subscriptions;
}
/* *** Refactored web services functions *** */
/**
* Creates a new anonymous session.
*
* @param string $ip
*/
function anonymous_login($ip=null)
{
$session = $this->start_anonymous_session($ip);
if(PEAR::isError($session)){
return new KTAPI_Error($session);
}
$session= $session->get_session();
return $session;
}
/**
* Creates a new session for the user.
*
* @param string $username
* @param string $password
* @param string $ip
* @return string
*/
function login($username, $password, $ip=null)
{
$session = $this->start_session($username,$password, $ip);
if(PEAR::isError($session)){
return new KTAPI_Error($session);
}
$session = $session->get_session();
return $session;
}
/**
* Closes an active session.
*
* @return KTAPI_Error on failure
*/
function logout()
{
$session = &$this->get_session();
if(PEAR::isError($session)){
return new KTAPI_Error($session);
}
$session->logout();
}
/**
* Returns folder detail given a folder_id.
*
* @param int $folder_id
* @return kt_folder_detail.
*/
function get_folder_detail($folder_id)
{
$folder = &$this->get_folder_by_id($folder_id);
if (PEAR::isError($folder))
{
return new KTAPI_Error($folder);
}
$detail = $folder->get_detail();
return $detail;
}
/**
* Retrieves all shortcuts linking to a specific document
*
* @param ing $document_id
* @return kt_document_shortcuts
*
*/
function get_folder_shortcuts($folder_id)
{
$folder = $this->get_folder_by_id($folder_id);
if(PEAR::isError($folder)){
return new KTAPI_Error($folder);
}
$shortcuts = $folder->get_shortcuts();
if(PEAR::isError($shortcuts)){
return new KTAPI_Error($shortcuts);
}
return $shortcuts;
}
/**
* Returns folder detail given a folder name which could include a full path.
*
* @param string $folder_name
* @return kt_folder_detail.
*/
function get_folder_detail_by_name($folder_name)
{
$folder = &$this->get_folder_by_name($folder_name);
if(PEAR::isError($folder)){
return new KTAPI_Error($folder);
}
$detail = $folder->get_detail();
return $detail;
}
/**
* Returns the contents of a folder.
*
* @param int $folder_id
* @param int $depth
* @param string $what
* @return kt_folder_contents
*/
function get_folder_contents($folder_id, $depth=1, $what='DFS')
{
$folder = &$this->get_folder_by_id($folder_id);
if(PEAR::isError($folder)){
return new KTAPI_Error($folder);
}
$listing = $folder->get_listing($depth, $what);
$contents = array(
'folder_id' => $folder_id+0,
'folder_name'=>$folder->get_folder_name(),
'full_path'=>$folder->get_full_path(),
'items'=>$listing
);
return $contents;
}
/**
* Creates a new folder.
*
* @param int $folder_id
* @param string $folder_name
* @return kt_folder_detail.
*/
function create_folder($folder_id, $folder_name)
{
$folder = &$this->get_folder_by_id($folder_id);
if (PEAR::isError($folder))
{
return new KTAPI_Error($folder);
}
$newfolder = &$folder->add_folder($folder_name);
$detail = $newfolder->get_detail();
return $detail;
}
/**
* Creates a shortcut to an existing folder
*
* @param int $target_folder_id Folder to place the shortcut in
* @param int $source_folder_id Folder to create the shortcut to
* @return kt_folder_detail.
*/
function create_folder_shortcut($target_folder_id, $source_folder_id)
{
$folder = &$this->get_folder_by_id($target_folder_id);
if (PEAR::isError($folder))
{
return new KTAPI_Error($folder);
}
$source_folder = &$kt->get_folder_by_id($source_folder_id);
if (PEAR::isError($source_folder))
{
return new KTAPI_Error($source_folder);
}
$shortcut = &$folder->add_folder_shortcut($source_folder_id);
if (PEAR::isError($shortcut))
{
return new KTAPI_Error($shortcut);
}
$detail = $shortcut->get_detail();
return $detail;
}
/**
* Creates a shortcut to an existing document
*
* @param int $target_folder_id Folder to place the shortcut in
* @param int $source_document_id Document to create the shortcut to
* @return kt_document_detail.
*/
function create_document_shortcut($target_folder_id, $source_document_id)
{
$folder = &$this->get_folder_by_id($target_folder_id);
if (PEAR::isError($folder))
{
return new KTAPI_Error($folder);
}
$source_document = &$kt->get_document_by_id($source_document_id);
if (PEAR::isError($source_document))
{
return new KTAPI_Error($source_document);
}
$shortcut = &$folder->add_document_shortcut($source_document_id);
if (PEAR::isError($shortcut))
{
return new KTAPI_Error($shortcut);
}
$detail = $shortcut->get_detail();
return $detail;
}
/**
* Deletes a folder.
*
* @param int $folder_id
* @param string $reason
* @return kt_response.
*/
function delete_folder($folder_id, $reason)
{
$folder = &$this->get_folder_by_id($folder_id);
if (PEAR::isError($folder))
{
return new KTAPI_Error($folder);
}
$result = $folder->delete($reason);
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
}
/**
* Renames a folder.
*
* @param int $folder_id
* @param string $newname
* @return kt_response.
*/
function rename_folder($folder_id, $newname)
{
$folder = &$this->get_folder_by_id($folder_id);
if (PEAR::isError($folder))
{
return new KTAPI_Error($folder);
}
$result = $folder->rename($newname);
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
}
/**
* Makes a copy of a folder in another location.
*
* @param int $sourceid
* @param int $targetid
* @param string $reason
* @return kt_response
*/
function copy_folder($source_id, $target_id, $reason)
{
$src_folder = &$this->get_folder_by_id($source_id);
if (PEAR::isError($src_folder))
{
return new KTAPI_Error($src_folder);
}
$tgt_folder = &$this->get_folder_by_id($target_id);
if (PEAR::isError($tgt_folder))
{
return new KTAPI_Error($tgt_folder);
}
$result= $src_folder->copy($tgt_folder, $reason);
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
$sourceName = $src_folder->get_folder_name();
$targetPath = $tgt_folder->get_full_path();
$response = $this->get_folder_detail_by_name($targetPath . '/' . $sourceName);
return $response;
}
/**
* Returns a list of available transitions on a give document with a workflow.
*
* @param int $document_id
* @return array
*/
function get_document_workflow_transitions($document_id)
{
$document = &$this->get_document_by_id($document_id);
if (PEAR::isError($document))
{
return new KTAPI_Error($document);
}
$result = $document->get_workflow_transitions();
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
return $result;
}
/**
* Returns the current state that the document is in.
*
* @param int $document_id
* @return array
*/
function get_document_workflow_state($document_id)
{
$document = &$this->get_document_by_id($document_id);
if (PEAR::isError($document))
{
return new KTAPI_Error($document);
}
$result = $document->get_workflow_state();
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
return $result;
}
/**
* Returns the document transaction history.
*
* @param int $document_id
* @return array
*/
function get_document_transaction_history($document_id)
{
$document = &$this->get_document_by_id($document_id);
if (PEAR::isError($document))
{
return new KTAPI_Error($document);
}
$result = $document->get_transaction_history();
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
return $result;
}
/**
* Returns the version history.
*
* @param int $document_id
* @return kt_document_version_history_response
*/
function get_document_version_history($document_id)
{
$document = &$this->get_document_by_id($document_id);
if (PEAR::isError($document))
{
return new KTAPI_Error($document);
}
$result = $document->get_version_history();
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
return $result;
}
/**
* Returns a list of linked documents
*
* @param string $session_id
* @param int $document_id
* @return array
*
*
*/
function get_document_links($document_id)
{
$document = &$this->get_document_by_id($document_id);
if (PEAR::isError($document))
{
return new KTAPI_Error($document);
}
$links = $document->get_linked_documents();
return $links;
}
/**
* Moves a folder to another location.
*
* @param int $sourceid
* @param int $targetid
* @param string $reason
* @return kt_response.
*/
function move_folder($source_id, $target_id, $reason)
{
$src_folder = &$this->get_folder_by_id($source_id);
if (PEAR::isError($src_folder))
{
return new KTAPI_Error($src_folder);
}
$tgt_folder = &$this->get_folder_by_id($target_id);
if (PEAR::isError($tgt_folder))
{
return new KTAPI_Error($tgt_folder);
}
$result = $src_folder->move($tgt_folder, $reason);
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
$response = $this->get_folder_detail($source_id);
return $response;
}
/**
* Removes a link between documents
*
* @param int $parent_document_id
* @param int $child_document_id
* @return kt_response
*/
function unlink_documents($parent_document_id, $child_document_id)
{
$document = &$this->get_document_by_id($parent_document_id);
if (PEAR::isError($document))
{
return new KTAPI_Error($document);
}
$child_document = &$this->get_document_by_id($child_document_id);
if (PEAR::isError($child_document))
{
return new KTAPI_Error($child_document);
}
$result = $document->unlink_document($child_document);
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
return true;
}
/**
* Creates a link between documents
*
* @param int $parent_document_id
* @param int $child_document_id
* @param string $type
* @return boolean
*/
function link_documents($parent_document_id, $child_document_id, $type)
{
$document = &$this->get_document_by_id($parent_document_id);
if (PEAR::isError($document))
{
return new KTAPI_Error($document);
}
$child_document = &$this->get_document_by_id($child_document_id);
if (PEAR::isError($child_document))
{
return new KTAPI_Error($child_document);
}
$result = $document->link_document($child_document, $type);
if (PEAR::isError($result))
{
return new KTAPI_Error($result);
}
return true;
}
/**
* Retrieves the server policies for this server
*
* @return array
*/
function get_client_policies($client=null)
{
$config = KTConfig::getSingleton();
$policies = array(
array(
'name' => 'explorer_metadata_capture',
'value' => bool2str($config->get('clientToolPolicies/explorerMetadataCapture')),
'type' => 'boolean'
),
array(
'name' => 'office_metadata_capture',
'value' => bool2str($config->get('clientToolPolicies/officeMetadataCapture')),
'type' => 'boolean'
),
array(
'name' => 'capture_reasons_delete',
'value' => bool2str($config->get('clientToolPolicies/captureReasonsDelete')),
'type' => 'boolean'
),
array(
'name' => 'capture_reasons_checkin',
'value' => bool2str($config->get('clientToolPolicies/captureReasonsCheckin')),
'type' => 'boolean'
),
array(
'name' => 'capture_reasons_checkout',
'value' => bool2str($config->get('clientToolPolicies/captureReasonsCheckout')),
'type' => 'boolean'
),
array(
'name' => 'capture_reasons_cancelcheckout',
'value' => bool2str($config->get('clientToolPolicies/captureReasonsCancelCheckout')),
'type' => 'boolean'
),
array(
'name' => 'capture_reasons_copyinkt',
'value' => bool2str($config->get('clientToolPolicies/captureReasonsCopyInKT')),
'type' => 'boolean'
),
array(
'name' => 'capture_reasons_moveinkt',
'value' => bool2str($config->get('clientToolPolicies/captureReasonsMoveInKT')),
'type' => 'boolean'
),
array(
'name' => 'allow_remember_password',
'value' => bool2str($config->get('clientToolPolicies/allowRememberPassword')),
'type' => 'boolean'
),
);
return $response['policies'];
}
/**
* This is the search interface
*
* @param string $query
* @param string $options
* @return kt_search_response
*/
function search($query, $options)
{
$results = processSearchExpression($query);
if (PEAR::isError($results))
{
return new KTAPI_Error($results);
}
return $results;
}
}
/**
* This class handles the saved search functionality within the API
*
* @author KnowledgeTree Team
* @package KTAPI
* @version Version 0.9
*/
class savedSearches
{
/**
* Instance of the KTAPI object
*
* @access private
*/
private $ktapi;
/**
* Constructs the bulk actions object
*
* @author KnowledgeTree Team
* @access public
* @param KTAPI $ktapi Instance of the KTAPI object
*/
function __construct(&$ktapi)
{
// $this->ktapi = new KTAPI();
$this->ktapi = $ktapi;
}
/**
* This method creates the saved search
*
* @author KnowledgeTree Team
* @access public
* @param string $name The name of the search
* @param string $query The query string to be saved
* @return string|object $result SUCCESS - The id of the saved search | FAILURE - an error object
*/
public function create($name, $query)
{
$user = $this->ktapi->get_user();
if (is_null($user) || PEAR::isError($user))
{
$result = new PEAR_Error(KTAPI_ERROR_USER_INVALID);
return $result;
}
$userID = $user->getId();
$result = SearchHelper::saveSavedSearch($name, $query, $userID);
return $result;
}
/**
* This method gets a saved searche based on the id
*
* @author KnowledgeTree Tean
* @access public
* @param integer $searchID The id of the saved search
* @return array|object $search SUCESS - The saved search data | FAILURE - a pear error object
*/
public function getSavedSearch($searchID)
{
$search = SearchHelper::getSavedSearch($searchID);
return $search;
}
/**
* This method gets a list of saved searches
*
* @author KnowledgeTree Tean
* @access public
* @return array|object $list SUCESS - The list of saved searches | FAILURE - an error object
*/
public function getList()
{
$user = $this->ktapi->get_user();
if (is_null($user) || PEAR::isError($user))
{
$list = new PEAR_Error(KTAPI_ERROR_USER_INVALID);
return $list;
}
$userID = $user->getId();
$list = SearchHelper::getSavedSearches($userID);
if (PEAR::isError($list))
{
$list = new PEAR_Error('Invalid saved search result');
return $list;
}
return $list;
}
/**
* This method deletes the saved search
*
* @author KnowledgeTree Team
* @access public
* @param string $searchID The id of the saved search
* @return void
*/
public function delete($searchID)
{
SearchHelper::deleteSavedSearch($searchID);
}
/**
* This method runs the saved search bsed on the id of the saved search
*
* @author KnowledgeTree Team
* @access public
* @param integer $searchID The id of the saved search
* @return array|object $results SUCCESS - The results of the saved serach | FAILURE - a pear error object
*/
public function runSavedSearch($searchID)
{
$search = $this->getSavedSearch($searchID);
if(is_null($search) || PEAR::isError($search)){
$results = new PEAR_Error('Invalid saved search');
return $results;
}
$query = $search[0]['expression'];
$results = processSearchExpression($query);
return $results;
}
}
?>