Source for file ktapi.inc.php
Documentation is available at ktapi.inc.php
* Implements a cleaner wrapper API for KnowledgeTree.
* @license http://www.knowledgetree.com/KPL KnowledgeTree Public License Version 1.1
* The contents of this file are subject to the KnowledgeTree Public
* License Version 1.1 ("License"); You may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.knowledgetree.com/KPL
* Software distributed under the License is distributed on an "AS IS"
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* The Original Code is: KnowledgeTree Open Source
* The Initial Developer of the Original Code is The Jam Warehouse Software
* (Pty) Ltd, trading as KnowledgeTree.
* Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
* (C) 2007 The Jam Warehouse Software (Pty) Ltd;
require_once('../config/dmsDefaults.php');
require_once(KT_LIB_DIR .
'/filelike/fsfilelike.inc.php');
require_once(KT_LIB_DIR .
'/foldermanagement/folderutil.inc.php');
// Generic error messages used in the API. There may be some others specific to functionality
// TODO: Check that they are all relevant.
define('KTAPI_ERROR_SESSION_INVALID', 'The session could not be resolved.');
define('KTAPI_ERROR_PERMISSION_INVALID', 'The permission could not be resolved.');
define('KTAPI_ERROR_FOLDER_INVALID', 'The folder could not be resolved.');
define('KTAPI_ERROR_DOCUMENT_INVALID', 'The document could not be resolved.');
define('KTAPI_ERROR_USER_INVALID', 'The user could not be resolved.');
define('KTAPI_ERROR_KTAPI_INVALID', 'The ktapi could not be resolved.');
define('KTAPI_ERROR_INSUFFICIENT_PERMISSIONS', 'The user does not have sufficient permissions to access the resource.');
define('KTAPI_ERROR_INTERNAL_ERROR', 'An internal error occurred. Please review the logs.');
define('KTAPI_ERROR_DOCUMENT_TYPE_INVALID', 'The document type could not be resolved.');
define('KTAPI_ERROR_DOCUMENT_CHECKED_OUT', 'The document is checked out.');
define('KTAPI_ERROR_DOCUMENT_NOT_CHECKED_OUT', 'The document is not checked out.');
define('KTAPI_ERROR_WORKFLOW_INVALID', 'The workflow could not be resolved.');
define('KTAPI_ERROR_WORKFLOW_NOT_IN_PROGRESS', 'The workflow is not in progress.');
// Mapping of permissions to actions.
// TODO: Check that they are all correct.
// Note, currently, all core actions have permissions that are defined in the plugins.
// As the permissions are currently associated with actions which are quite closely linked
// to the web interface, it is not the nicest way to do things. They should be associated at
// a lower level, such as in the api. probably, better, would be at some stage to assocate
// the permissions to the action/transaction in the database so administrators can really customise
define('KTAPI_PERMISSION_DELETE', 'ktcore.permissions.delete');
define('KTAPI_PERMISSION_READ', 'ktcore.permissions.read');
define('KTAPI_PERMISSION_WRITE', 'ktcore.permissions.write');
define('KTAPI_PERMISSION_ADD_FOLDER', 'ktcore.permissions.addFolder');
define('KTAPI_PERMISSION_RENAME_FOLDER', 'ktcore.permissions.folder_rename');
define('KTAPI_PERMISSION_CHANGE_OWNERSHIP', 'ktcore.permissions.security');
define('KTAPI_PERMISSION_DOCUMENT_MOVE', 'ktcore.permissions.write');
define('KTAPI_PERMISSION_WORKFLOW', 'ktcore.permissions.workflow');
function KTAPI_Session(&$ktapi, &$user, $session, $sessionid, $ip)
// TODO: get documenttransaction to not look at the session variable!
$_SESSION["userID"] =
$user->getId();
* This returns the session string
* This returns the sessionid in the database.
* This returns a user object for the use rassociated with the session.
* This resolves the user's ip
elseif (getenv("HTTP_X_FORWARDED_FOR"))
$forwardedip =
getenv("HTTP_X_FORWARDED_FOR");
list
($ip,$ip2,$ip3,$ip4)=
split (",", $forwardedip);
elseif (getenv("HTTP_CLIENT_IP"))
$ip =
getenv("HTTP_CLIENT_IP");
* This returns a session object based on authentication credentials.
* @param string $username
* @param string $password
return new PEAR_Error(_kt('The username is empty.'));
$user =
& User::getByUsername($username);
if (PEAR::isError($user) ||
($user ===
false))
return new PEAR_Error(_kt("The user '$username' cound not be found."));
if ($user->isAnonymous())
$config =
&KTConfig::getSingleton();
$allow_anonymous =
$config->get('session/allowAnonymousLogin', false);
return new PEAR_Error(_kt('Anonymous user not allowed'));
return new PEAR_Error(_kt('The password is empty.'));
$authenticated =
KTAuthenticationUtil::checkPassword($user, $password);
if (PEAR::isError($authenticated) ||
$authenticated ===
false)
return new PEAR_Error(_kt("The password is invalid."));
//$ip = KTAPI_Session::resolveIP();
$user_id =
$user->getId();
$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";
$row =
DBUtil::getOneResult($sql);
return new PEAR_Error('No record found for user?');
if ($row['over_limit'] ==
1)
return new PEAR_Error('Session limit exceeded. Logout of any active sessions.');
$sessionid =
DBUtil::autoInsert('active_sessions',
'lastused' =>
date('Y-m-d H:i:s'),
if (PEAR::isError($sessionid) )
$session =
&new KTAPI_Session($ktapi, $user, $session, $sessionid, $ip);
* This returns an active session.
$sql =
"SELECT id, user_id FROM active_sessions WHERE session_id='$session'";
$row =
DBUtil::getOneResult($sql);
if (is_null($row) ||
PEAR::isError($row))
$userid =
$row['user_id'];
$user =
&User::get($userid);
if (is_null($user) ||
PEAR::isError($user))
$now=
date('Y-m-d H:i:s');
$sql =
"UPDATE active_sessions SET last_used='$now' WHERE id=$sessionid";
$session =
&new KTAPI_Session($ktapi, $user, $session, $sessionid, $ip);
* This closes the current session.
$sql =
"DELETE FROM active_sessions WHERE id=$this->sessionid";
$result =
DBUtil::runQuery($sql);
if (PEAR::isError($result))
* This is a reference to the core KTAPI controller
* This is a reference to a base Folder object.
* This is the id of the folder on the database.
* This is used to get a folder based on a folder id.
function &get(&$ktapi, $folderid)
$folder =
&Folder::get($folderid);
if (is_null($folder) ||
PEAR::isError($folder))
if (is_null($user) ||
PEAR::isError($user))
* This is the constructor for the KTAPI_Folder.
* This returns a reference to the internal folder object.
* This returns detailed information on the document.
return $this->folder->getParentID();
* This returns the folderid.
* This can resolve a folder relative to the current directy by name
* @param string $foldername
$foldername=
trim($foldername);
return new PEAR_Error('A valid folder name must be specified.');
$split =
explode('/', $foldername);
foreach($split as $foldername)
$sql =
"SELECT id FROM folders WHERE name='$foldername' and parent_id=$folderid";
$row =
DBUtil::getOneResult($sql);
if (is_null($row) ||
PEAR::isError($row))
$path =
$this->folder->getFullPath() .
'/' .
$this->folder->getName();
* This gets a document by filename or name.
* @param string $documentname
* @param string $function
$documentname=
trim($documentname);
if (empty($documentname))
return new PEAR_Error('A valid document name must be specified.');
$foldername =
dirname($documentname);
$documentname =
basename($documentname);
if (!empty($foldername) &&
($foldername !=
'.'))
if (is_null($ktapi_folder) ||
PEAR::isError($ktapi_folder))
//$folder = $ktapi_folder->get_folder();
$folderid =
$ktapi_folder->folderid;
$document =
Document::$function($documentname, $folderid);
if (is_null($document) ||
PEAR::isError($document))
if (PEAR::isError($user))
* This can resolve a document relative to the current directy by name.
* @param string $documentname
* This can resolve a document relative to the current directy by filename .
* @param string $documentname