| 1 |
|
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* @file |
| 6 |
|
* Implements a REST API to an external Knowledgetree Installation |
| 7 |
|
*/ |
| 8 |
|
|
| 9 |
|
include "includes/KnowledgeTreeService_REST.php"; |
| 10 |
|
|
| 11 |
|
function kt_api_menu() { |
| 12 |
|
$items['admin/settings/kt'] = array( |
| 13 |
|
'title' => t('KnowledgeTree integration'), |
| 14 |
|
'description' => t('Configure the integration with KnowledgeTree'), |
| 15 |
|
'page callback' => 'drupal_get_form', |
| 16 |
|
'page arguments' => array('kt_api_settings'), |
| 17 |
|
'access arguments' => array('administer site configuration'), |
| 18 |
|
'type' => MENU_NORMAL_ITEM, |
| 19 |
|
); |
| 20 |
|
$items['admin/settings/kt/api'] = array( |
| 21 |
|
'title' => t('KnowledgeTree API Settings'), |
| 22 |
|
'description' => t('Configure the integration with KnowledgeTree'), |
| 23 |
|
'type' => MENU_DEFAULT_LOCAL_TASK, |
| 24 |
|
'weight' => 0, |
| 25 |
|
); |
| 26 |
|
$items['admin/settings/kt/REST_test'] = array( |
| 27 |
|
'title' => "KT REST Test", |
| 28 |
|
'page callback' => 'kt_api_rest_test_page', |
| 29 |
|
'page arguments' => array(1), |
| 30 |
|
'access arguments' => array('administer site configuration'), |
| 31 |
|
'type' => MENU_LOCAL_TASK, |
| 32 |
|
'weight' => 5, |
| 33 |
|
); |
| 34 |
|
|
| 35 |
|
return $items; |
| 36 |
|
} |
| 37 |
|
|
| 38 |
|
function kt_api_settings() { |
| 39 |
|
|
| 40 |
|
$form = array(); |
| 41 |
|
|
| 42 |
|
$form['kt_root_url'] = array( |
| 43 |
|
'#type' => 'textfield', |
| 44 |
|
'#title' => t('Knowledgetree root URL'), |
| 45 |
|
'#default_value' => variable_get('kt_root_url', 'http://localhost/kt/'), |
| 46 |
|
'#description' => t("The root URL of your Knowledgtree Installation, including a trailing slash. eg. http://localhost/kt/"), |
| 47 |
|
); |
| 48 |
|
|
| 49 |
|
$form['kt_login'] = array( |
| 50 |
|
'#type' => 'fieldset', |
| 51 |
|
'#title' => t('KnowledgeTree REST Authentication'), |
| 52 |
|
'#description' => t('Authentication details for accessing KT from Drupal. <strong>Best to create a new user in KT with restricted permissions. Whatever this user can access in KT will be accessible to all drupal users.</strong>'), |
| 53 |
|
'#collapsible' => TRUE, |
| 54 |
|
'#collapsed' => FALSE, |
| 55 |
|
); |
| 56 |
|
|
| 57 |
|
$form['kt_login']['kt_username'] = array( |
| 58 |
|
'#type' => 'textfield', |
| 59 |
|
'#title' => t('Knowledgetree Login Username'), |
| 60 |
|
'#default_value' => variable_get('kt_username', 'drupal'), |
| 61 |
|
'#size' => 20, |
| 62 |
|
'#maxlength' => 50, |
| 63 |
|
'#description' => t("The username drupal will use to access Knowledgetree."), |
| 64 |
|
); |
| 65 |
|
|
| 66 |
|
$form['kt_login']['kt_password'] = array( |
| 67 |
|
'#type' => 'password', |
| 68 |
|
'#title' => t('Knowledgetree Login Password'), |
| 69 |
|
'#size' => 20, |
| 70 |
|
'#maxlength' => 255, |
| 71 |
|
'#description' => t("The password drupal will use to access Knowledgetree."), |
| 72 |
|
); |
| 73 |
|
|
| 74 |
|
$session_timeout_options = array( |
| 75 |
|
0 => t("Do not cache session"), |
| 76 |
|
30 => t("Cache session for 30 sec"), |
| 77 |
|
60 => t("Cahce session for 1 min"), |
| 78 |
|
300 => t("Cache session for 5 min"), |
| 79 |
|
1800 => t("Cache session for 30 min"), |
| 80 |
|
); |
| 81 |
|
|
| 82 |
|
$form['kt_login']['kt_session_cache'] = array( |
| 83 |
|
'#type' => 'select', |
| 84 |
|
'#title' => t('KnowledgeTree Session Caching'), |
| 85 |
|
'#description' => t('How long should Drupal cache the KT session. The KT default timeout for inactivity is 1200 seconds (See KT General Settings page). If this cache timeout is set to a higher number than KTs session timeout, errors may occur.'), |
| 86 |
|
'#default_value' => variable_get('kt_session_cache', 300), |
| 87 |
|
'#options' => $session_timeout_options, |
| 88 |
|
'#required' => TRUE, |
| 89 |
|
); |
| 90 |
|
|
| 91 |
|
$form['kt_debug'] = array( |
| 92 |
|
'#type' => 'checkbox', |
| 93 |
|
'#title' => t('KT REST Debug'), |
| 94 |
|
'#default_value' => variable_get('kt_debug', 0), |
| 95 |
|
'#description' => t('Print verbose debug information on each REST call'), |
| 96 |
|
); |
| 97 |
|
|
| 98 |
|
return system_settings_form($form); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
//function kt_api_perms() { |
| 102 |
|
// return array('display KT REST Test Page'); |
| 103 |
|
//} |
| 104 |
|
|
| 105 |
|
function kt_api_rest_test_page() { |
| 106 |
|
|
| 107 |
|
$page = ""; |
| 108 |
|
|
| 109 |
|
$kt = new Drupal_KT_REST; |
| 110 |
|
$kt->login(); |
| 111 |
|
|
| 112 |
|
//$search_results = _kt_xmlobj2array($kt->search('Filename CONTAINS "e" ', "none")); |
| 113 |
|
//$page = "<pre>".print_r($search_results, TRUE)."</pre>"; |
| 114 |
|
|
| 115 |
|
$document = $kt->get_document_detail("1", ""); |
| 116 |
|
$document = _kt_xmlobj2array($document); |
| 117 |
|
$page = "<p>If this test is successful you should see an array with document details matching the document with ID#1 in KnowledgeTree:\n<pre>". print_r($document, TRUE) ."</pre>"; |
| 118 |
|
|
| 119 |
|
return $page; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
/** |
| 123 |
|
* Converts an XML object (such as the one returned by SimpleXML::simplexml_load_string) |
| 124 |
|
* into a simple array |
| 125 |
|
* |
| 126 |
|
*/ |
| 127 |
|
function _kt_xmlobj2array($data) { |
| 128 |
|
if (is_object($data)) { |
| 129 |
|
foreach (get_object_vars($data) as $key => $val) { |
| 130 |
|
$ret[$key] = _kt_xmlobj2array($val); |
| 131 |
|
} |
| 132 |
|
return $ret; |
| 133 |
|
} |
| 134 |
|
elseif (is_array($data)) { |
| 135 |
|
foreach ($data as $key => $val) { |
| 136 |
|
$ret[$key] = _kt_xmlobj2array($val); |
| 137 |
|
} |
| 138 |
|
return $ret; |
| 139 |
|
} |
| 140 |
|
else { |
| 141 |
|
return $data; |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
class Drupal_KT_REST extends KnowledgeTreeRESTService { |
| 148 |
|
|
| 149 |
|
function login() { |
| 150 |
|
|
| 151 |
|
// Check for a cached session first |
| 152 |
|
$cached_session=cache_get('kt_session', 'cache'); |
| 153 |
|
if (isset($cached_session->data) && ($cached_session->expire > time()) && variable_get('kt_session_cache', 0)) { |
| 154 |
|
$this->session = $cached_session->data; |
| 155 |
|
$this->log('KT REST: Using cached session :'. print_r($this->session, TRUE), 'DEBUG'); |
| 156 |
|
} |
| 157 |
|
else { // No cached session exists or cached session expired, use regular login call |
| 158 |
|
|
| 159 |
|
cache_clear_all('kt_session', 'cache'); // Clear old cached session, if any |
| 160 |
|
|
| 161 |
|
if (($kt_url = variable_get('kt_root_url', '')) == '') { |
| 162 |
|
$this->log('KT REST: No KT root URL set. Check the Knowledgetree REST API Settings.', 'ERROR'); |
| 163 |
|
} |
| 164 |
|
if (($kt_user = variable_get('kt_username', '')) == '') { |
| 165 |
|
$this->log('KT REST: No KT username set. Check the Knowledgetree REST API Settings.', 'ERROR'); |
| 166 |
|
} |
| 167 |
|
if (($kt_pass = variable_get('kt_password', '')) == '') { |
| 168 |
|
$this->log('KT REST: No KT password set. Check the Knowledgetree REST API Settings.', 'ERROR'); |
| 169 |
|
} |
| 170 |
|
parent::login($kt_url .'ktwebservice/KTWebService.php', $kt_user, $kt_pass); |
| 171 |
|
if (isset($this->session['session_id']) && $this->session['session_id'] != 0) { |
| 172 |
|
// Cache the new session id to keep persistent session with timeout |
| 173 |
|
cache_set("kt_session", $this->session, "cache", time() + variable_get('kt_session_cache', 0)); |
| 174 |
|
} |
| 175 |
|
else { // parent::login failed |
| 176 |
|
return FALSE; |
| 177 |
|
} |
| 178 |
|
} |
| 179 |
|
return TRUE; |
| 180 |
|
} |
| 181 |
|
|
| 182 |
|
function log($message, $level) { |
| 183 |
|
switch ($level) { |
| 184 |
|
case "ERROR": |
| 185 |
|
watchdog("kt_api", $message, WATCHDOG_ERROR); |
| 186 |
|
if (variable_get('kt_debug', 0)) { |
| 187 |
|
drupal_set_message($message, 'error'); |
| 188 |
|
} |
| 189 |
|
break; |
| 190 |
|
|
| 191 |
|
case "DEBUG": |
| 192 |
|
if (variable_get('kt_debug', 0)) { |
| 193 |
|
drupal_set_message($message); |
| 194 |
|
} |
| 195 |
|
break; |
| 196 |
|
} |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|