Source for file ktwsapi.inc.php
Documentation is available at ktwsapi.inc.php
* This is the object model for the KnowledgeTree WebService.
* @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;
// TODO: add more validation based on information already returned. this can prevent all validation being done server side and minimise a little traffic possibly...
// TODO: caching so that requests don't have to be returned.
// TODO: possibly add a subscriber model, where updates can be propogated through to a cache/model on the client side.
require_once('ktwsapi_cfg.inc.php');
require_once('SOAP/Client.php');
define('KTWSAPI_ERR_SESSION_IN_USE','There is a session already active.');
define('KTWSAPI_ERR_SESSION_NOT_STARTED','An active session has not been started.');
* Upload a file to KT. Returns a temp filename on the server.
* @param string $filename
* @param int $document_id
function _upload_file($filename, $action, $document_id=
null)
return new PEAR_Error('CURL library not included.');
return new PEAR_Error('Could not access file to upload.');
$uploadname=
"upload_document";
$uploadname=
"upload_$document_id";
$session_id =
$this->ktapi->session;
$fp =
fopen ($filename, 'r');
curl_setopt($ch, CURLOPT_POST, 1);
'session_id'=>
$session_id,
'document_id'=>
$document_id,
$uploadname=>
'@' .
$filename
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$response =
curl_exec ($ch);
return new PEAR_Error('No response from server.');
foreach($fields as $field)
list
($fieldname, $value) =
explode('=', $field);
if ($upload_status[$uploadname]['size'] ==
filesize($filename))
return $upload_status[$uploadname]['tmp_name'];
return new PEAR_Error('Could not upload file.');
* Downlaods a file from KT.
* @param string $localpath
* @param string $filename
$localfilename =
$localpath .
'/' .
$filename;
$fp =
fopen($localfilename,'wb');
return new PEAR_Error('Could not create local file');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
$response =
curl_exec($ch);
* Returns a reference to the parent folder.
* @param kt_folder_detail $kt_folder_detail
$this->parent_id =
$kt_folder_detail->parent_id+
0;
$this->full_path =
$kt_folder_detail->full_path;
* Returns a reference to a KTWSAPI_Folder
function &get(&$ktapi, $folderid)
$kt_folder_detail =
$ktapi->soapclient->get_folder_detail($ktapi->session, $folderid);
if (SOAP_Client::isError($kt_folder_detail))
return $kt_folder_detail;
if ($kt_folder_detail->status_code !=
0)
return new PEAR_Error($kt_folder_detail->message);
* Returnss the parent folder id.
* Returns the folder name.
* Returns the current folder id.
* Returns the foldre based on foldername.
* @param string $foldername
$path =
$this->full_path .
'/' .
$foldername;
if (substr($path,0,13) ==
'/Root Folder/')
if (substr($path,0,12) ==
'Root Folder/')
$kt_folder_detail =
$this->ktapi->soapclient->get_folder_detail_by_name($this->ktapi->session, $path);
if (SOAP_Client::isError($kt_folder_detail))
return $kt_folder_detail;
if ($kt_folder_detail->status_code !=
0)
return new PEAR_Error($kt_folder_detail->message);
* Returns the full folder path.
* Returns the contents of a folder.
* @return kt_folder_items
$kt_folder_contents =
$this->ktapi->soapclient->get_folder_contents($this->ktapi->session, $this->folder_id, $depth+
0, $what);
if (SOAP_Client::isError($kt_folder_contents))
return $kt_folder_contents;
if ($kt_folder_contents->status_code !=
0)
return new PEAR_Error($kt_folder_contents->message);
return $kt_folder_contents->items;
* Returns a document based on title.
* @return KTWSAPI_Document
if (substr($path,0,13) ==
'/Root Folder/')
if (substr($path,0,12) ==
'Root Folder/')
$kt_document_detail =
$this->ktapi->soapclient->get_document_detail_by_name($this->ktapi->session, $path, 'T');
if (SOAP_Client::isError($kt_document_detail))
return $kt_document_detail;
if ($kt_document_detail->status_code !=
0)
return new PEAR_Error($kt_document_detail->message);
* Returns a document based on filename.
* @param string $filename
* @return KTWSAPI_Document
if (substr($path,0,13) ==
'/Root Folder/')
if (substr($path,0,12) ==
'Root Folder/')
$kt_document_detail =
$this->ktapi->soapclient->get_document_detail_by_name($this->ktapi->session, $path, 'F');
if (SOAP_Client::isError($kt_document_detail))
return $kt_document_detail;
if ($kt_document_detail->status_code !=
0)
return new PEAR_Error($kt_document_detail->message);
* @param string $foldername
$kt_folder_detail =
$this->ktapi->soapclient->create_folder($this->ktapi->session, $this->folder_id, $foldername);
if (SOAP_Client::isError($kt_folder_detail))
return $kt_folder_detail;
if ($kt_folder_detail->status_code !=
0)
return new PEAR_Error($kt_folder_detail->message);
* Deletes the current folder.
// TODO: check why no transaction in folder_transactions
$kt_response =
$this->ktapi->soapclient->delete_folder($this->ktapi->session, $this->folder_id, $reason);
if (SOAP_Client::isError($kt_response))
if ($kt_response->status_code !=
0)
return new PEAR_Error($kt_response->message);
* Renames the current folder.
$kt_response =
$this->ktapi->soapclient->rename_folder($this->ktapi->session, $this->folder_id, $newname);
if (SOAP_Client::isError($kt_response))
if ($kt_response->status_code !=
0)
return new PEAR_Error($kt_response->message);
* Moves a folder to another location.
* @param KTWSAPI_Folder $ktwsapi_target_folder
function move(&$ktwsapi_target_folder, $reason=
'')
assert(is_a($ktwsapi_target_folder,'KTWSAPI_Folder'));
$kt_response =
$this->ktapi->soapclient->move_folder($this->ktapi->session, $this->folder_id,$ktwsapi_target_folder->get_folderid());
if (SOAP_Client::isError($kt_response))
if ($kt_response->status_code !=
0)
return new PEAR_Error($kt_response->message);
* Copies a folder to another location
* @param KTWSAPI_Folder $ktwsapi_target_folder
function copy(&$ktwsapi_target_folder, $reason=
'')
assert(is_a($ktwsapi_target_folder,'KTWSAPI_Folder'));
$targetid=
$ktwsapi_target_folder->get_folderid();
$kt_response =
$this->ktapi->soapclient->copy_folder($this->ktapi->session, $this->folder_id,$targetid, $reason);
if (SOAP_Client::isError($kt_response))
if ($kt_response->status_code !=
0)
return new PEAR_Error($kt_response->message);
* Adds a document to the current folder.
* @param string $filename
* @param string $documenttype
* @return KTWSAPI_Document
function &add_document($filename, $title=
null, $documenttype=
null)
if (empty($documenttype))
// First step - upload file
if (PEAR::isError($tempfilename))
return new PEAR_Error($tempfilename->message);
// Second step - move file into KT
$kt_document_detail =
$this->ktapi->soapclient->add_document($this->ktapi->session, $this->folder_id, $title, $basename, $documenttype, $tempfilename );
if (SOAP_Client::isError($kt_document_detail))
return $kt_document_detail;
if ($kt_document_detail->status_code !=
0)
return new PEAR_Error($kt_document_detail->message);