From 3c3cb2f578c92590352900fdb75525892b18336b Mon Sep 17 00:00:00 2001 From: Paul Barrett Date: Mon, 13 Jul 2009 11:27:14 +0200 Subject: [PATCH] Fix problems with CMISSpaces browsing of KT repo via AtomPub --- lib/api/ktcmis/ktcmis.inc.php | 41 ++++++++++++++++++++++++++++++++--------- webservice/atompub/cmis/KT_cmis_atom_server.services.inc.php | 249 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------- webservice/atompub/cmis/KT_cmis_atom_service_helper.inc.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- webservice/atompub/cmis/index.php | 37 ++++++++++++++++++++----------------- 4 files changed, 238 insertions(+), 153 deletions(-) diff --git a/lib/api/ktcmis/ktcmis.inc.php b/lib/api/ktcmis/ktcmis.inc.php index d931267..2d9dfc7 100644 --- a/lib/api/ktcmis/ktcmis.inc.php +++ b/lib/api/ktcmis/ktcmis.inc.php @@ -69,12 +69,22 @@ class KTCMISBase { static protected $ktapi; static protected $session; -// public function __construct($username = null, $password = null) -// { -// $this->startSession($username, $password); -// } + public function __construct(&$ktapi = null, $username = null, $password = null) + { + // TODO confirm KTAPI instance active??? shouldn't really be responsibility of this code + if (is_null($ktapi) && (!is_null($username) && !is_null($password))) { +// echo ":WGHWTWGWGHW"; + $this->startSession($username, $password); + } + else { + self::$ktapi = $ktapi; + self::$session = self::$ktapi->get_session(); + } + } - // TODO try to pick up existing session if possible, i.e. if the $session value is not empty + // TODO this probably does not belong here??? probably should require all auth external, handled by transport protocol. + // perhaps simple refusal to execute without valid session? + // NOTE left in to allow transport protocol to delegate auth to this level, but not actually used in any code at present public function startSession($username, $password) { // echo $username." :: ".$password."
"; @@ -103,6 +113,13 @@ class KTCMISBase { return self::$session; } + public function setInterface(&$ktapi = null) + { + if (!is_null($ktapi)) { + self::$ktapi = $ktapi; + } + } + public function getInterface() { return self::$ktapi; @@ -263,10 +280,12 @@ class KTNavigationService extends KTCMISBase { protected $NavigationService; - public function __construct() + public function __construct(&$ktapi = null, $username = null, $password = null) { + parent::__construct($ktapi, $username, $password); // instantiate underlying CMIS service $this->NavigationService = new CMISNavigationService(); + $this->setInterface(); } public function startSession($username, $password) @@ -276,8 +295,9 @@ class KTNavigationService extends KTCMISBase { return self::$session; } - public function setInterface() + public function setInterface(&$ktapi = null) { + parent::setInterface($ktapi); $this->NavigationService->setInterface(self::$ktapi); } @@ -455,10 +475,12 @@ class KTObjectService extends KTCMISBase { protected $ObjectService; - public function __construct() + public function __construct(&$ktapi = null, $username = null, $password = null) { + parent::__construct($ktapi, $username, $password); // instantiate underlying CMIS service $this->ObjectService = new CMISObjectService(); + $this->setInterface(); } public function startSession($username, $password) @@ -468,8 +490,9 @@ class KTObjectService extends KTCMISBase { return self::$session; } - public function setInterface() + public function setInterface(&$ktapi = null) { + parent::setInterface($ktapi); $this->ObjectService->setInterface(self::$ktapi); } diff --git a/webservice/atompub/cmis/KT_cmis_atom_server.services.inc.php b/webservice/atompub/cmis/KT_cmis_atom_server.services.inc.php index 7974173..0afce8b 100644 --- a/webservice/atompub/cmis/KT_cmis_atom_server.services.inc.php +++ b/webservice/atompub/cmis/KT_cmis_atom_server.services.inc.php @@ -18,16 +18,16 @@ class KT_cmis_atom_service_folder extends KT_cmis_atom_service { public function GET_action() { $RepositoryService = new RepositoryService(); - try { - $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } +// try { +// $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; @@ -43,24 +43,24 @@ class KT_cmis_atom_service_folder extends KT_cmis_atom_service { } else if ($this->params[0] == 'path') { - $ktapi =& $RepositoryService->getInterface(); + $ktapi =& KT_cmis_atom_service_helper::getKt(); $folderId = KT_cmis_atom_service_helper::getFolderId($this->params, $ktapi); } else { $folderId = $this->params[0]; - $ObjectService = new ObjectService(); - - try { - $ObjectService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } + $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); + +// try { +// $ObjectService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $cmisEntry = $ObjectService->getProperties($repositoryId, $folderId, false, false); $folderName = $cmisEntry['properties']['Name']['value']; @@ -69,35 +69,35 @@ class KT_cmis_atom_service_folder extends KT_cmis_atom_service { if (!empty($this->params[1]) && (($this->params[1] == 'children') || ($this->params[1] == 'descendants'))) { - $NavigationService = new NavigationService(); - - try { - $NavigationService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } + $NavigationService = new NavigationService(KT_cmis_atom_service_helper::getKt()); + +// try { +// $NavigationService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $feed = $this->getFolderChildrenFeed($NavigationService, $repositoryId, $folderId, $folderName, $this->params[1]); } else { - $ObjectService = new ObjectService(); - - try { - $ObjectService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } + $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); + +// try { +// $ObjectService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $feed = $this->getFolderFeed($ObjectService, $repositoryId, $folderId); } @@ -110,16 +110,16 @@ class KT_cmis_atom_service_folder extends KT_cmis_atom_service { { $RepositoryService = new RepositoryService(); - try { - $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } +// try { +// $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; @@ -158,18 +158,18 @@ class KT_cmis_atom_service_folder extends KT_cmis_atom_service { [0]['@children']['cmis:properties'] [0]['@children']); - $ObjectService = new ObjectService(); + $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); - try { - $ObjectService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } +// try { +// $ObjectService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } if ($type == 'folder') $newObjectId = $ObjectService->createFolder($repositoryId, ucwords($cmisObjectProperties['ObjectTypeId']), $properties, $folderId); @@ -187,18 +187,18 @@ class KT_cmis_atom_service_folder extends KT_cmis_atom_service { } else { - $NavigationService = new NavigationService(); - - try { - $NavigationService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } + $NavigationService = new NavigationService(KT_cmis_atom_service_helper::getKt()); + +// try { +// $NavigationService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $cmisEntry = $ObjectService->getProperties($repositoryId, $folderId, false, false); $feed = $this->getFolderChildrenFeed($NavigationService, $repositoryId, $folderId, $cmisEntry['properties']['Name']['value']); @@ -292,16 +292,16 @@ class KT_cmis_atom_service_types extends KT_cmis_atom_service { { $RepositoryService = new RepositoryService(); - try { - $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } +// try { +// $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } // fetch repository id $repositories = $RepositoryService->getRepositories(); @@ -329,16 +329,16 @@ class KT_cmis_atom_service_type extends KT_cmis_atom_service { { $RepositoryService = new RepositoryService(); - try { - $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } +// try { +// $RepositoryService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } // fetch repository id $repositories = $RepositoryService->getRepositories(); @@ -432,18 +432,18 @@ class KT_cmis_atom_service_checkedout extends KT_cmis_atom_service { public function GET_action() { $RepositoryService = new RepositoryService(); - $NavigationService = new NavigationService(); - - try { - $NavigationService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } + $NavigationService = new NavigationService(KT_cmis_atom_service_helper::getKt()); + +// try { +// $NavigationService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; @@ -491,19 +491,18 @@ class KT_cmis_atom_service_document extends KT_cmis_atom_service { public function GET_action() { $RepositoryService = new RepositoryService(); - - $ObjectService = new ObjectService(); - - try { - $ObjectService->startSession(self::$authData['username'], self::$authData['password']); - } - catch (Exception $e) - { - $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; - $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); - $this->responseFeed = $feed; - return null; - } + $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); + +// try { +// $ObjectService->startSession(self::$authData['username'], self::$authData['password']); +// } +// catch (Exception $e) +// { +// $this->headers[] = 'WWW-Authenticate: Basic realm="KnowledgeTree Secure Area"'; +// $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_NOT_AUTHENTICATED, $e->getMessage()); +// $this->responseFeed = $feed; +// return null; +// } $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; diff --git a/webservice/atompub/cmis/KT_cmis_atom_service_helper.inc.php b/webservice/atompub/cmis/KT_cmis_atom_service_helper.inc.php index 55cb86e..edf0699 100644 --- a/webservice/atompub/cmis/KT_cmis_atom_service_helper.inc.php +++ b/webservice/atompub/cmis/KT_cmis_atom_service_helper.inc.php @@ -2,6 +2,8 @@ class KT_cmis_atom_service_helper { + protected static $kt = null; + /** * Creates an AtomPub entry for a CMIS entry and adds it to the supplied feed * @@ -148,7 +150,7 @@ class KT_cmis_atom_service_helper { $feedElement = $feed->newElement('cmis:' . strtolower($type['typeId']) . 'Type'); foreach($type as $property => $value) { - $feed->newField($property, CMISUtil::boolToString($value), $feedElement); + $feed->newField('cmis:' . $property, CMISUtil::boolToString($value), $feedElement); } $entry->appendChild($feedElement); @@ -173,7 +175,7 @@ class KT_cmis_atom_service_helper { * @param array $path * @param object $ktapi KTAPI instance */ - // TODO make this much more efficient than this messy method + // TODO make this much more efficient than this method static public function getFolderId($path, &$ktapi) { // lose first item @@ -224,6 +226,64 @@ class KT_cmis_atom_service_helper { return null; } + /** + * Log in to KT easily + * + * @param string $username + * @param string $password + * @param string $ip + * @return object Containing the status_code of the login and session id + */ + function login($username, $password, $ip=null){ + $kt = self::getKt(); + + $session = $kt->start_session($username,$password, $ip); + if (PEAR::isError($session)){ + $response['status_code']=KT_atom_server_FAILURE; + $response['session_id']=''; + }else{ + $session= $session->get_session(); + $response['status_code'] = KT_atom_server_SUCCESS; + $response['session_id'] = $session; + } + return $response; + } + + + /** + * Log out of KT using the session id + * + * @param string $session_id + * @return object Containing the status_code of the logout attempt + */ + function logout($session_id){ + $kt = self::getKt(); + $session = $kt->get_active_session($session_id, null); + + if (PEAR::isError($session)){ + $response['status_code']=KT_atom_server_FAILURE; + }else{ + $session->logout(); + $response['status_code'] = KT_atom_server_SUCCESS; + } + return $response; + } + + /** + * Get the KT singleton instance + * + * @return object + */ + public static function getKt() + { + if(!isset(self::$kt)) + { + self::$kt = new KTAPI(); + self::$kt->get_active_session(session_id()); + } + return self::$kt; + } + } ?> diff --git a/webservice/atompub/cmis/index.php b/webservice/atompub/cmis/index.php index 9090f65..e71db98 100644 --- a/webservice/atompub/cmis/index.php +++ b/webservice/atompub/cmis/index.php @@ -40,6 +40,7 @@ */ require_once('../../../config/dmsDefaults.php'); +require_once(KT_DIR . '/ktapi/ktapi.inc.php'); define('KT_APP_BASE_URI', "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/?/'); define('KT_APP_SYSTEM_URI', "http://".$_SERVER['HTTP_HOST']); @@ -50,12 +51,6 @@ define('CMIS_APP_BASE_URI', trim(KT_APP_BASE_URI, '/')); define('CMIS_APP_SYSTEM_URI', KT_APP_SYSTEM_URI); define('CMIS_ATOM_LIB_FOLDER', trim(KT_ATOM_LIB_FOLDER, '/') . '/cmis/'); -// fetch username and password for auth; note that this apparently only works when PHP is run as an apache module -// TODO method to fetch username and password when running PHP as CGI, if possible -// HTTP Basic Auth: -//$username = $_SERVER['PHP_AUTH_USER']; -//$password = $_SERVER['PHP_AUTH_PW']; - /** * Includes */ @@ -67,15 +62,21 @@ include_once(CMIS_ATOM_LIB_FOLDER.'KT_cmis_atom_serviceDoc.inc.php'); / include_once(CMIS_ATOM_LIB_FOLDER.'KT_cmis_atom_service.inc.php'); //Containing the servicedoc class allowing easy ServiceDocument generation include_once('KT_cmis_atom_server.services.inc.php'); + +KT_cmis_atom_service_helper::login('admin', 'admin'); + //Start the AtomPubProtocol Routing Engine $APP = new KT_cmis_atom_server(); -// FIXME HACK! this should not happen every time, ONLY on a service doc request -// CMIS service document setup -$APP->initServiceDocument(); -// FIXME HACK! this should not happen every time, ONLY on a service doc request -// User defined title tag -$APP->addWorkspaceTag('dms','atom:title',$APP->repositoryInfo['repositoryName']); +$queryArray = split('/', trim($_SERVER['QUERY_STRING'], '/')); +$workspace = strtolower(trim($queryArray[0])); +if ($workspace == 'servicedocument') +{ + // CMIS service document setup + $APP->initServiceDocument(); + // User defined title tag + $APP->addWorkspaceTag('dms','atom:title',$APP->repositoryInfo['repositoryName']); +} /** * Register Services @@ -102,11 +103,13 @@ $APP->registerService('dms', 'types', 'KT_cmis_atom_service_types', 'Object Type // FIXME HACK! this should not happen every time, ONLY on a specific request, should NOT appear in service document as this is not definable at that time; // SHOULD be appearing in types listing feed // NOTE $requestParams is meaningless if not actually requesting this service, so not a good way to register the service really -$queryArray=split('/',trim($_SERVER['QUERY_STRING'],'/')); -$requestParams=array_slice($queryArray,2); -$APP->registerService('dms', 'type', 'KT_cmis_atom_service_type', 'Object Type Collection', explode('/', $requestParams), 'types-descendants'); -// FIXME HACK! see above, this one for documents -$APP->registerService('dms', 'document', 'KT_cmis_atom_service_document', 'Object Type Collection', explode('/', $requestParams), 'types-descendants'); +if ($workspace != 'servicedocument') +{ + // should check this per workspace??? + $APP->registerService('dms', 'type', 'KT_cmis_atom_service_type', 'Object Type Collection', explode('/', $requestParams), 'types-descendants'); + // FIXME HACK! see above, this one for documents + $APP->registerService('dms', 'document', 'KT_cmis_atom_service_document', 'Object Type Collection', explode('/', $requestParams), 'types-descendants'); +} //Execute the current url/header request $APP->execute(); -- libgit2 0.21.4