Commit 6c8c8ade095731661058a0fd4e3a7350156e4346

Authored by Megan Watson
1 parent 7e3bf9f3

Added webservice api functions for use by the REST layer. Login,logout and folder functionality.

Committed by: Megan Watson
Reviewed by: Donald Jackson



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@9770 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 352 additions and 25 deletions
ktapi/ktapi.inc.php
@@ -238,7 +238,7 @@ class KTAPI @@ -238,7 +238,7 @@ class KTAPI
238 } 238 }
239 239
240 /** 240 /**
241 - * Returns an associative array of permission namespaces and their names 241 + * Returns an associative array of permission namespaces and their names
242 * 242 *
243 * @access public 243 * @access public
244 * @return array 244 * @return array
@@ -255,9 +255,9 @@ class KTAPI @@ -255,9 +255,9 @@ class KTAPI
255 255
256 /** 256 /**
257 * Returns folder permissions 257 * Returns folder permissions
258 - * 258 + *
259 * @access public 259 * @access public
260 - * @param string 260 + * @param string
261 * @param int 261 * @param int
262 * 262 *
263 */ 263 */
@@ -272,20 +272,20 @@ class KTAPI @@ -272,20 +272,20 @@ class KTAPI
272 $user_ktapi->start_system_session($username); 272 $user_ktapi->start_system_session($username);
273 273
274 $folder = KTAPI_Folder::get($user_ktapi, $folder_id); 274 $folder = KTAPI_Folder::get($user_ktapi, $folder_id);
275 - 275 +
276 $permissions = $folder->getPermissionAllocation(); 276 $permissions = $folder->getPermissionAllocation();
277 - 277 +
278 $user_ktapi->session_logout(); 278 $user_ktapi->session_logout();
279 - 279 +
280 return $permissions->permissions; 280 return $permissions->permissions;
281 } 281 }
282 - 282 +
283 /** 283 /**
284 * Add folder permission 284 * Add folder permission
285 - * 285 + *
286 * @access public 286 * @access public
287 * @param string 287 * @param string
288 - * @param string 288 + * @param string
289 * @param int 289 * @param int
290 * 290 *
291 */ 291 */
@@ -295,7 +295,7 @@ class KTAPI @@ -295,7 +295,7 @@ class KTAPI
295 $error = new PEAR_Error('A session is not active'); 295 $error = new PEAR_Error('A session is not active');
296 return $error; 296 return $error;
297 } 297 }
298 - 298 +
299 /* First check that user trying to add permission can actually do so */ 299 /* First check that user trying to add permission can actually do so */
300 $folder = KTAPI_Folder::get($this, $folder_id); 300 $folder = KTAPI_Folder::get($this, $folder_id);
301 $permissions = $folder->getPermissionAllocation(); 301 $permissions = $folder->getPermissionAllocation();
@@ -303,7 +303,7 @@ class KTAPI @@ -303,7 +303,7 @@ class KTAPI
303 if(!in_array("Manage security", $detail)) { 303 if(!in_array("Manage security", $detail)) {
304 return new PEAR_Error("User does not have permission to manage security"); 304 return new PEAR_Error("User does not have permission to manage security");
305 } 305 }
306 - 306 +
307 /* We need to create a new instance of KTAPI to get another user */ 307 /* We need to create a new instance of KTAPI to get another user */
308 $user_ktapi = new KTAPI(); 308 $user_ktapi = new KTAPI();
309 $user_ktapi->start_system_session($username); 309 $user_ktapi->start_system_session($username);
@@ -314,29 +314,29 @@ class KTAPI @@ -314,29 +314,29 @@ class KTAPI
314 $user_ktapi->session_logout(); 314 $user_ktapi->session_logout();
315 return $folder; 315 return $folder;
316 } 316 }
317 - 317 +
318 $permission = KTAPI_Permission::getByNamespace($namespace); 318 $permission = KTAPI_Permission::getByNamespace($namespace);
319 if(PEAR::isError($permission)) { 319 if(PEAR::isError($permission)) {
320 $user_ktapi->session_logout(); 320 $user_ktapi->session_logout();
321 return $permission; 321 return $permission;
322 } 322 }
323 -  
324 - 323 +
  324 +
325 $user = KTAPI_User::getByUsername($username); 325 $user = KTAPI_User::getByUsername($username);
326 if(PEAR::isError($user)) { 326 if(PEAR::isError($user)) {
327 $user_ktapi->session_logout(); 327 $user_ktapi->session_logout();
328 return $user; 328 return $user;
329 } 329 }
330 - 330 +
331 $permissions = $folder->getPermissionAllocation(); 331 $permissions = $folder->getPermissionAllocation();
332 - 332 +
333 $permissions->add($user, $permissions); 333 $permissions->add($user, $permissions);
334 $permissions->save(); 334 $permissions->save();
335 } 335 }
336 -  
337 -  
338 -  
339 - 336 +
  337 +
  338 +
  339 +
340 /** 340 /**
341 * This checks if a user can access an object with a certain permission. 341 * This checks if a user can access an object with a certain permission.
342 * 342 *
@@ -384,7 +384,7 @@ class KTAPI @@ -384,7 +384,7 @@ class KTAPI
384 384
385 return $user; 385 return $user;
386 } 386 }
387 - 387 +
388 /** 388 /**
389 * Returns the version id for the associated version number 389 * Returns the version id for the associated version number
390 * 390 *
@@ -399,14 +399,14 @@ class KTAPI @@ -399,14 +399,14 @@ class KTAPI
399 $error = new PEAR_Error(KTAPI_ERROR_SESSION_INVALID); 399 $error = new PEAR_Error(KTAPI_ERROR_SESSION_INVALID);
400 return $error; 400 return $error;
401 } 401 }
402 - 402 +
403 $document_id = sanitizeForSQL($document_id); 403 $document_id = sanitizeForSQL($document_id);
404 $version_number = sanitizeForSQL($version_number); 404 $version_number = sanitizeForSQL($version_number);
405 - 405 +
406 $pos = strpos($version_number, "."); 406 $pos = strpos($version_number, ".");
407 $major = substr($version_number, 0, $pos); 407 $major = substr($version_number, 0, $pos);
408 $minor = substr($version_number, ($pos+1)); 408 $minor = substr($version_number, ($pos+1));
409 - 409 +
410 $sql = "SELECT id FROM document_content_version WHERE document_id = {$document_id} AND major_version = '{$major}' AND minor_version = '{$minor}'"; 410 $sql = "SELECT id FROM document_content_version WHERE document_id = {$document_id} AND major_version = '{$major}' AND minor_version = '{$minor}'";
411 $row = DBUtil::getOneResult($sql); 411 $row = DBUtil::getOneResult($sql);
412 $row = (int)$row['id']; 412 $row = (int)$row['id'];
@@ -531,7 +531,7 @@ class KTAPI @@ -531,7 +531,7 @@ class KTAPI
531 } else { 531 } else {
532 $user = User::getByUserName($username); 532 $user = User::getByUserName($username);
533 } 533 }
534 - 534 +
535 if(PEAR::isError($user)) { 535 if(PEAR::isError($user)) {
536 return new PEAR_Error('Username invalid'); 536 return new PEAR_Error('Username invalid');
537 } 537 }
@@ -981,6 +981,333 @@ class KTAPI @@ -981,6 +981,333 @@ class KTAPI
981 981
982 return $subscriptions; 982 return $subscriptions;
983 } 983 }
  984 +
  985 +
  986 + /* *** Refactored web services functions *** */
  987 +
  988 +
  989 + /**
  990 + * Creates a new anonymous session.
  991 + *
  992 + * @param string $ip
  993 + */
  994 + function anonymous_login($ip=null)
  995 + {
  996 + $session = $this->start_anonymous_session($ip);
  997 + if(PEAR::isError($session)){
  998 + return new KTAPI_Error($session);
  999 + }
  1000 +
  1001 + $session= $session->get_session();
  1002 + return $session;
  1003 + }
  1004 +
  1005 + /**
  1006 + * Creates a new session for the user.
  1007 + *
  1008 + * @param string $username
  1009 + * @param string $password
  1010 + * @param string $ip
  1011 + * @return string
  1012 + */
  1013 + function login($username, $password, $ip=null)
  1014 + {
  1015 + $session = $this->start_session($username,$password, $ip);
  1016 + if(PEAR::isError($session)){
  1017 + return new KTAPI_Error($session);
  1018 + }
  1019 +
  1020 + $session = $session->get_session();
  1021 + return $session;
  1022 + }
  1023 +
  1024 + /**
  1025 + * Closes an active session.
  1026 + *
  1027 + * @return KTAPI_Error on failure
  1028 + */
  1029 + function logout()
  1030 + {
  1031 + $session = &$this->get_session();
  1032 + if(PEAR::isError($session)){
  1033 + return new KTAPI_Error($session);
  1034 + }
  1035 + $session->logout();
  1036 + }
  1037 +
  1038 + /**
  1039 + * Returns folder detail given a folder_id.
  1040 + *
  1041 + * @param int $folder_id
  1042 + * @return kt_folder_detail.
  1043 + */
  1044 + function get_folder_detail($folder_id)
  1045 + {
  1046 + $folder = &$this->get_folder_by_id($folder_id);
  1047 + if (PEAR::isError($folder))
  1048 + {
  1049 + return new KTAPI_Error($folder);
  1050 + }
  1051 + $detail = $folder->get_detail();
  1052 + return $detail;
  1053 + }
  1054 +
  1055 + /**
  1056 + * Retrieves all shortcuts linking to a specific document
  1057 + *
  1058 + * @param ing $document_id
  1059 + * @return kt_document_shortcuts
  1060 + *
  1061 + */
  1062 + function get_folder_shortcuts($folder_id)
  1063 + {
  1064 + $folder = $this->get_folder_by_id($folder_id);
  1065 + if(PEAR::isError($folder)){
  1066 + return new KTAPI_Error($folder);
  1067 + }
  1068 +
  1069 + $shortcuts = $folder->get_shortcuts();
  1070 + if(PEAR::isError($shortcuts)){
  1071 + return new KTAPI_Error($shortcuts);
  1072 + }
  1073 +
  1074 + return $shortcuts;
  1075 + }
  1076 +
  1077 + /**
  1078 + * Returns folder detail given a folder name which could include a full path.
  1079 + *
  1080 + * @param string $folder_name
  1081 + * @return kt_folder_detail.
  1082 + */
  1083 + function get_folder_detail_by_name($folder_name)
  1084 + {
  1085 + $folder = &$this->get_folder_by_name($folder_name);
  1086 + if(PEAR::isError($folder)){
  1087 + return new KTAPI_Error($folder);
  1088 + }
  1089 +
  1090 + $detail = $folder->get_detail();
  1091 + return $detail;
  1092 + }
  1093 +
  1094 + /**
  1095 + * Returns the contents of a folder.
  1096 + *
  1097 + * @param int $folder_id
  1098 + * @param int $depth
  1099 + * @param string $what
  1100 + * @return kt_folder_contents
  1101 + */
  1102 + function get_folder_contents($folder_id, $depth=1, $what='DFS')
  1103 + {
  1104 + $folder = &$this->get_folder_by_id($folder_id);
  1105 + if(PEAR::isError($folder)){
  1106 + return new KTAPI_Error($folder);
  1107 + }
  1108 + $listing = $folder->get_listing($depth, $what);
  1109 +
  1110 + $contents = array(
  1111 + 'folder_id' => $folder_id+0,
  1112 + 'folder_name'=>$folder->get_folder_name(),
  1113 + 'full_path'=>$folder->get_full_path(),
  1114 + 'items'=>$listing
  1115 + );
  1116 +
  1117 + return $contents;
  1118 + }
  1119 +
  1120 + /**
  1121 + * Creates a new folder.
  1122 + *
  1123 + * @param int $folder_id
  1124 + * @param string $folder_name
  1125 + * @return kt_folder_detail.
  1126 + */
  1127 + function create_folder($folder_id, $folder_name)
  1128 + {
  1129 + $folder = &$this->get_folder_by_id($folder_id);
  1130 + if (PEAR::isError($folder))
  1131 + {
  1132 + return new KTAPI_Error($folder);
  1133 + }
  1134 + $newfolder = &$folder->add_folder($folder_name);
  1135 + $detail = $newfolder->get_detail();
  1136 + return $detail;
  1137 + }
  1138 +
  1139 + /**
  1140 + * Creates a shortcut to an existing folder
  1141 + *
  1142 + * @param int $target_folder_id Folder to place the shortcut in
  1143 + * @param int $source_folder_id Folder to create the shortcut to
  1144 + * @return kt_folder_detail.
  1145 + */
  1146 + function create_folder_shortcut($target_folder_id, $source_folder_id)
  1147 + {
  1148 + $folder = &$this->get_folder_by_id($target_folder_id);
  1149 + if (PEAR::isError($folder))
  1150 + {
  1151 + return new KTAPI_Error($folder);
  1152 + }
  1153 +
  1154 + $source_folder = &$kt->get_folder_by_id($source_folder_id);
  1155 + if (PEAR::isError($source_folder))
  1156 + {
  1157 + return new KTAPI_Error($source_folder);
  1158 + }
  1159 +
  1160 + $shortcut = &$folder->add_folder_shortcut($source_folder_id);
  1161 + if (PEAR::isError($shortcut))
  1162 + {
  1163 + return new KTAPI_Error($shortcut);
  1164 + }
  1165 +
  1166 + $detail = $shortcut->get_detail();
  1167 + return $detail;
  1168 + }
  1169 +
  1170 + /**
  1171 + * Creates a shortcut to an existing document
  1172 + *
  1173 + * @param int $target_folder_id Folder to place the shortcut in
  1174 + * @param int $source_document_id Document to create the shortcut to
  1175 + * @return kt_document_detail.
  1176 + */
  1177 + function create_document_shortcut($target_folder_id, $source_document_id)
  1178 + {
  1179 + $folder = &$this->get_folder_by_id($target_folder_id);
  1180 + if (PEAR::isError($folder))
  1181 + {
  1182 + return new KTAPI_Error($folder);
  1183 + }
  1184 +
  1185 + $source_document = &$kt->get_document_by_id($source_document_id);
  1186 + if (PEAR::isError($source_document))
  1187 + {
  1188 + return new KTAPI_Error($source_document);
  1189 + }
  1190 +
  1191 + $shortcut = &$folder->add_document_shortcut($source_document_id);
  1192 + if (PEAR::isError($shortcut))
  1193 + {
  1194 + return new KTAPI_Error($shortcut);
  1195 + }
  1196 +
  1197 + $detail = $shortcut->get_detail();
  1198 + return $detail;
  1199 + }
  1200 +
  1201 + /**
  1202 + * Deletes a folder.
  1203 + *
  1204 + * @param int $folder_id
  1205 + * @param string $reason
  1206 + * @return kt_response.
  1207 + */
  1208 + function delete_folder($folder_id, $reason)
  1209 + {
  1210 + $folder = &$this->get_folder_by_id($folder_id);
  1211 + if (PEAR::isError($folder))
  1212 + {
  1213 + return new KTAPI_Error($folder);
  1214 + }
  1215 +
  1216 + $result = $folder->delete($reason);
  1217 + if (PEAR::isError($result))
  1218 + {
  1219 + return new KTAPI_Error($result);
  1220 + }
  1221 + }
  1222 +
  1223 + /**
  1224 + * Renames a folder.
  1225 + *
  1226 + * @param int $folder_id
  1227 + * @param string $newname
  1228 + * @return kt_response.
  1229 + */
  1230 + function rename_folder($folder_id, $newname)
  1231 + {
  1232 + $folder = &$this->get_folder_by_id($folder_id);
  1233 + if (PEAR::isError($folder))
  1234 + {
  1235 + return new KTAPI_Error($folder);
  1236 + }
  1237 + $result = $folder->rename($newname);
  1238 + if (PEAR::isError($result))
  1239 + {
  1240 + return new KTAPI_Error($result);
  1241 + }
  1242 + }
  1243 +
  1244 + /**
  1245 + * Makes a copy of a folder in another location.
  1246 + *
  1247 + * @param int $sourceid
  1248 + * @param int $targetid
  1249 + * @param string $reason
  1250 + * @return kt_response
  1251 + */
  1252 + function copy_folder($source_id, $target_id, $reason)
  1253 + {
  1254 + $src_folder = &$this->get_folder_by_id($source_id);
  1255 + if (PEAR::isError($src_folder))
  1256 + {
  1257 + return new KTAPI_Error($src_folder);
  1258 + }
  1259 +
  1260 + $tgt_folder = &$this->get_folder_by_id($target_id);
  1261 + if (PEAR::isError($tgt_folder))
  1262 + {
  1263 + return new KTAPI_Error($tgt_folder);
  1264 + }
  1265 +
  1266 + $result= $src_folder->copy($tgt_folder, $reason);
  1267 + if (PEAR::isError($result))
  1268 + {
  1269 + return new KTAPI_Error($result);
  1270 + }
  1271 +
  1272 + $sourceName = $src_folder->get_folder_name();
  1273 + $targetPath = $tgt_folder->get_full_path();
  1274 +
  1275 + $response = $this->get_folder_detail_by_name($targetPath . '/' . $sourceName);
  1276 + return $response;
  1277 + }
  1278 +
  1279 + /**
  1280 + * Moves a folder to another location.
  1281 + *
  1282 + * @param int $sourceid
  1283 + * @param int $targetid
  1284 + * @param string $reason
  1285 + * @return kt_response.
  1286 + */
  1287 + function move_folder($source_id, $target_id, $reason)
  1288 + {
  1289 + $src_folder = &$this->get_folder_by_id($source_id);
  1290 + if (PEAR::isError($src_folder))
  1291 + {
  1292 + return new KTAPI_Error($src_folder);
  1293 + }
  1294 +
  1295 + $tgt_folder = &$this->get_folder_by_id($target_id);
  1296 + if (PEAR::isError($tgt_folder))
  1297 + {
  1298 + return new KTAPI_Error($tgt_folder);
  1299 + }
  1300 +
  1301 + $result = $src_folder->move($tgt_folder, $reason);
  1302 + if (PEAR::isError($result))
  1303 + {
  1304 + return new KTAPI_Error($result);
  1305 + }
  1306 +
  1307 + $response = $this->get_folder_detail($source_id);
  1308 + return $response;
  1309 + }
  1310 +
984 } 1311 }
985 1312
986 /** 1313 /**