getRepositories(); $repositoryId = $repositories[0]['repositoryId']; // TODO implement full path/node separation as with Alfresco - i.e. path requests come in on path/ and node requests come in on node/ // path request e.g.: Root Folder/DroppedDocuments // node request e.g.: F1/children // node request e.g.: F2 if (urldecode($this->params[0]) == 'Root Folder') { $folderId = CMISUtil::encodeObjectId('Folder', 1); $folderName = urldecode($this->params[0]); } else if ($this->params[0] == 'path') { $ktapi =& KT_cmis_atom_service_helper::getKt(); $folderId = KT_cmis_atom_service_helper::getFolderId($this->params, $ktapi); } else if (($this->params[1] == 'children') || ($this->params[1] == 'descendants')) { $folderId = $this->params[0]; $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); $response = $ObjectService->getProperties($repositoryId, $folderId, false, false); if (PEAR::isError($response)) { $feed = KT_cmis_atom_service_helper::getErrorFeed($this, KT_cmis_atom_service::STATUS_SERVER_ERROR, $response->getMessage()); $this->responseFeed = $feed; return null; } $folderName = $response['properties']['Name']['value']; } else { $folderId = $this->params[0]; } if (!empty($this->params[1]) && (($this->params[1] == 'children') || ($this->params[1] == 'descendants'))) { $NavigationService = new NavigationService(KT_cmis_atom_service_helper::getKt()); $feed = $this->getFolderChildrenFeed($NavigationService, $repositoryId, $folderId, $folderName, $this->params[1]); } else { $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); $feed = KT_cmis_atom_service_helper::getObjectFeed($this, $ObjectService, $repositoryId, $folderId); } //Expose the responseFeed $this->responseFeed = $feed; } /** * Deals with folder service POST actions. * This includes creation/moving of both folders and documents. */ public function POST_action() { $RepositoryService = new RepositoryService(); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; // set default action, objectId and typeId $action = 'create'; $objectId = null; $typeId = null; $folderId = $this->params[0]; $title = KT_cmis_atom_service_helper::getAtomValues($this->parsedXMLContent['@children'], 'title'); $summary = KT_cmis_atom_service_helper::getAtomValues($this->parsedXMLContent['@children'], 'summary'); $properties = array('name' => $title, 'summary' => $summary); // determine whether this is a folder or a document action // document action create will have a content tag or containing base64 encoding of the document // move action will have an existing id supplied as a parameter - not sure how this works yet as the CMIS clients we are // testing don't support move functionality at this time (2009/07/23) and so we are presuming the following format: // /folder//children/ // also possible that there will be an existing ObjectId property, try to cater for both until we know how it really works // check for existing object id as parameter in url if (isset($this->params[2])) { $action = 'move'; $objectId = $this->params[2]; } $cmisObjectProperties = KT_cmis_atom_service_helper::getCmisProperties($this->parsedXMLContent['@children']['cmis:object']); // check for existing object id as property of submitted object data if (!empty($cmisObjectProperties['ObjectId'])) { $action = 'move'; $objectId = $cmisObjectProperties['ObjectId']; } // TODO there may be more to do for the checking of an existing object. // e.g. verifying that it does indeed exist, and throwing an exception if it does not: // "If the objected property is present but not valid an exception will be thrown" (from CMIS specification) // NOTE this exception should be thrown in the service API code and not here. // determine type if object is being moved if (!is_null($objectId)) { CMISUtil::decodeObjectId($objectId, $typeId); } // now check for content stream $content = KT_cmis_atom_service_helper::getAtomValues($this->parsedXMLContent['@children'], 'content'); // TODO this will possibly need to change somewhat once Relationship Objects come into play. if ((($action == 'create') && (is_null($content))) || ($typeId == 'Folder')) { $type = 'folder'; } else { $type = 'document'; } $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); $success = false; $error = null; if ($action == 'create') { if ($type == 'folder') $newObjectId = $ObjectService->createFolder($repositoryId, ucwords($cmisObjectProperties['ObjectTypeId']), $properties, $folderId); else $newObjectId = $ObjectService->createDocument($repositoryId, ucwords($cmisObjectProperties['ObjectTypeId']), $properties, $folderId, $content); // check if returned Object Id is a valid CMIS Object Id CMISUtil::decodeObjectId($newObjectId, $typeId); if ($typeId != 'Unknown') $success = true; else $error = $newObjectId['message']; } else if ($action == 'move') { $response = $ObjectService->moveObject($repositoryId, $objectId, '', $folderId); if (!PEAR::isError($response)) $success = true; else $error = $response->getMessage(); // same object as before $newObjectId = $objectId; $typeId = ucwords($type); } if ($success) { $this->setStatus(($action == 'create') ? self::STATUS_CREATED : self::STATUS_UPDATED); $feed = KT_cmis_atom_service_helper::getObjectFeed($this, $ObjectService, $repositoryId, $newObjectId, 'POST'); } else { $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_SERVER_ERROR, $error); } //Expose the responseFeed $this->responseFeed = $feed; } /** * Deals with DELETE actions for folders. * This includes deleting a single folder (with no content) and deleting an entire folder tree * * @return 204 on success, 500 on error */ public function DELETE_action() { // NOTE due to the way KnowledgeTree works with folders this is always going to call deleteTree. // we COULD call deleteObject but when we delete a folder we expect to be trying to delete // the folder and all content. $RepositoryService = new RepositoryService(); $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; // attempt delete $response = $ObjectService->deleteTree($repositoryId, $this->params[0]); // error? if (PEAR::isError($response)) { $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_SERVER_ERROR, $response->getMessage()); //Expose the responseFeed $this->responseFeed = $feed; return null; } // list of failed objects? if (is_array($response)) { $this->setStatus(self::STATUS_SERVER_ERROR); $feed = new KT_cmis_atom_responseFeed_GET(CMIS_APP_BASE_URI); $feed->newField('title', 'Error: Failed to delete all objects in tree: ' . self::STATUS_SERVER_ERROR, $feed); foreach($response as $failed) { $entry = $feed->newEntry(); $objectElement = $feed->newElement('cmis:object'); $propertiesElement = $feed->newElement('cmis:properties'); $propElement = $feed->newElement('cmis:propertyId'); $propElement->appendChild($feed->newAttr('cmis:name', 'ObjectId')); $feed->newField('cmis:value', $failed, $propElement); $propertiesElement->appendChild($propElement); $objectElement->appendChild($propertiesElement); $entry->appendChild($objectElement); $entry->appendChild($feed->newElement('cmis:terminator')); } $this->responseFeed = $feed; return null; } // success $this->setStatus(self::STATUS_NO_CONTENT); } /** * Retrieves children/descendants of the specified folder * TODO this currently only works in children mode, add descendants * * @param string $repositoryId * @param string $folderId folder id for which children/descendants are requested * @param string $feedType children or descendants * @return string CMIS AtomPub feed */ private function getFolderChildrenFeed($NavigationService, $repositoryId, $folderId, $folderName, $feedType = 'children') { if ($feedType == 'children') { $entries = $NavigationService->getChildren($repositoryId, $folderId, false, false); } else if ($feedType == 'descendants') { $entries = $NavigationService->getDescendants($repositoryId, $folderId, false, false); } else { // error, we shouldn't be here, if we are then the wrong service/function was called } // $baseURI=NULL,$title=NULL,$link=NULL,$updated=NULL,$author=NULL,$id=NULL $feed = new KT_cmis_atom_responseFeed_GET(CMIS_APP_BASE_URI); $workspace = $feed->getWorkspace(); $feed->newField('title', $folderName . ' ' . ucwords($feedType), $feed); // TODO dynamic? $feedElement = $feed->newField('author'); $element = $feed->newField('name', 'System', $feedElement); $feed->appendChild($feedElement); // id $feed->newField('id', 'urn:uuid:' . $folderId . '-' . $feedType, $feed); // TODO get actual most recent update time, only use current if no other available $feed->newField('updated', KT_cmis_atom_service_helper::formatDatestamp(), $feed); $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel', 'self')); $link->appendChild($feed->newAttr('href', CMIS_APP_BASE_URI . $workspace . '/folder/' . $folderId . '/' . $feedType)); $feed->appendChild($link); $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel', 'source')); $link->appendChild($feed->newAttr('href', CMIS_APP_BASE_URI . $workspace . '/folder/' . $folderId)); $feed->appendChild($link); foreach($entries as $cmisEntry) { KT_cmis_atom_service_helper::createObjectEntry($feed, $cmisEntry, $folderName); // after each entry, add app:edited tag $feed->newField('app:edited', KT_cmis_atom_service_helper::formatDatestamp(), $feed); } $feed->newField('cmis:hasMoreItems', 'false', $feed); return $feed; } } /** * AtomPub Service: types */ class KT_cmis_atom_service_types extends KT_cmis_atom_service { public function GET_action() { $RepositoryService = new RepositoryService(); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; $types = $RepositoryService->getTypes($repositoryId); $type = ((empty($this->params[0])) ? 'all' : $this->params[0]); $feed = KT_cmis_atom_service_helper::getTypeFeed($type, $types); //Expose the responseFeed $this->responseFeed = $feed; } } /** * AtomPub Service: type */ class KT_cmis_atom_service_type extends KT_cmis_atom_service { public function GET_action() { $RepositoryService = new RepositoryService(); // fetch repository id $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; if (!isset($this->params[1])) { // For easier return in the wanted format, we call getTypes instead of getTypeDefinition. // Calling this with a single type specified returns an array containing the definition of // just the requested type. // NOTE could maybe be more efficient to call getTypeDefinition direct and then place in // an array on this side? or directly expose the individual entry response code and // call directly from here rather than via getTypeFeed. $type = ucwords($this->params[0]); $types = $RepositoryService->getTypes($repositoryId, $type); $feed = KT_cmis_atom_service_helper::getTypeFeed($type, $types); } else { // TODO dynamic dates, as needed everywhere // NOTE children of types not yet implemented and we don't support any non-basic types at this time $feed = $this->getTypeChildrenFeed($this->params[1]); } //Expose the responseFeed $this->responseFeed=$feed; } /** * Retrieves a list of child types for the supplied type * * NOTE this currently returns a hard coded empty list, since we do not currently support child types * TODO make dynamic if/when we support checking for child types (we don't actually need to support child types themselves) * * @param string $type * @return string CMIS AtomPub feed */ private function getTypeChildrenFeed() { //Create a new response feed // $baseURI=NULL,$title=NULL,$link=NULL,$updated=NULL,$author=NULL,$id=NULL $feed = new KT_cmis_atom_responseFeed_GET(CMIS_APP_BASE_URI); $feed->newField('title', 'Child Types of ' . ucwords($this->params[0]), $feed); $feed->newField('id', $this->params[0] . '-children', $feed); // TODO fetch child types - to be implemented when we support child types in the API // links $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel','first')); $link->appendChild($feed->newAttr('href', CMIS_APP_BASE_URI . 'type/' . $this->params[0] . '/' . $this->params[1] . '?pageNo=1&pageSize=0')); $link->appendChild($feed->newAttr('type', 'application/atom+xml;type=feed')); $link = $feed->newElement('link'); $link->appendChild($feed->newAttr('rel','last')); // TODO set page number correctly - to be done when we support paging the the API $link->appendChild($feed->newAttr('href', CMIS_APP_BASE_URI . 'type/' . $this->params[0] . '/' . $this->params[1] . '?pageNo=1&pageSize=0')); $link->appendChild($feed->newAttr('type', 'application/atom+xml;type=feed')); $feed->newField('updated', KT_cmis_atom_service_helper::formatDatestamp(), $feed); $feed->newField('cmis:hasMoreItems', 'false', $feed); return $feed; } } /** * AtomPub Service: checkedout */ // NOTE this is always an empty document, underlying API code still to be implemented class KT_cmis_atom_service_checkedout extends KT_cmis_atom_service { /** * Deals with GET actions for checkedout documents. */ public function GET_action() { $RepositoryService = new RepositoryService(); $NavigationService = new NavigationService(KT_cmis_atom_service_helper::getKt()); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; $checkedout = $NavigationService->getCheckedoutDocs($repositoryId); //Create a new response feed $feed = new KT_cmis_atom_responseFeed_GET(CMIS_APP_BASE_URI); $feed->newField('title', 'Checked out Documents', $feed); // TODO dynamic? $feedElement = $feed->newField('author'); $element = $feed->newField('name', 'admin', $feedElement); $feed->appendChild($feedElement); $feed->appendChild($feed->newElement('id', 'urn:uuid:checkedout')); // TODO get actual most recent update time, only use current if no other available $feed->appendChild($feed->newElement('updated', KT_cmis_atom_service_helper::formatDatestamp())); foreach($checkedout as $document) { $entry = $feed->newEntry(); $objectElement = $feed->newElement('cmis:object'); $propertiesElement = $feed->newElement('cmis:properties'); foreach($cmisEntry['properties'] as $propertyName => $property) { $propElement = $feed->newElement('cmis:' . $property['type']); $propElement->appendChild($feed->newAttr('cmis:name', $propertyName)); $feed->newField('cmis:value', CMISUtil::boolToString($property['value']), $propElement); $propertiesElement->appendChild($propElement); } $objectElement->appendChild($propertiesElement); $entry->appendChild($objectElement); } $entry = null; $feed->newField('cmis:hasMoreItems', 'false', $entry, true); //Expose the responseFeed $this->responseFeed = $feed; } public function POST_action() { $RepositoryService = new RepositoryService(); $VersioningService = new VersioningService(KT_cmis_atom_service_helper::getKt()); $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; $cmisObjectProperties = KT_cmis_atom_service_helper::getCmisProperties($this->parsedXMLContent['@children']['cmis:object']); // check for existing object id as property of submitted object data if (empty($cmisObjectProperties['ObjectId'])) { $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_SERVER_ERROR, 'No object was specified for checkout'); //Expose the responseFeed $this->responseFeed = $feed; return null; } $response = $VersioningService->checkOut($repositoryId, $cmisObjectProperties['ObjectId']); if (PEAR::isError($response)) { $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_SERVER_ERROR, 'No object was specified for checkout'); //Expose the responseFeed $this->responseFeed = $feed; return null; } $this->setStatus(self::STATUS_CREATED); $feed = KT_cmis_atom_service_helper::getObjectFeed($this, $ObjectService, $repositoryId, $cmisObjectProperties['ObjectId'], 'POST'); // $feed = KT_cmis_atom_service_helper::getObjectFeed($this, $ObjectService, $repositoryId, $newObjectId, 'POST'); //Expose the responseFeed $this->responseFeed = $feed; // $checkedout = $NavigationService->getCheckedoutDocs($repositoryId); // // //Create a new response feed // $feed = new KT_cmis_atom_responseFeed_GET(CMIS_APP_BASE_URI); // // $feed->newField('title', 'Checked out Documents', $feed); // // // TODO dynamic? // $feedElement = $feed->newField('author'); // $element = $feed->newField('name', 'admin', $feedElement); // $feed->appendChild($feedElement); // // $feed->appendChild($feed->newElement('id', 'urn:uuid:checkedout')); // // // TODO get actual most recent update time, only use current if no other available // $feed->appendChild($feed->newElement('updated', KT_cmis_atom_service_helper::formatDatestamp())); // // foreach($checkedout as $document) // { // $entry = $feed->newEntry(); // $objectElement = $feed->newElement('cmis:object'); // $propertiesElement = $feed->newElement('cmis:properties'); // // foreach($cmisEntry['properties'] as $propertyName => $property) // { // $propElement = $feed->newElement('cmis:' . $property['type']); // $propElement->appendChild($feed->newAttr('cmis:name', $propertyName)); // $feed->newField('cmis:value', CMISUtil::boolToString($property['value']), $propElement); // $propertiesElement->appendChild($propElement); // } // // $objectElement->appendChild($propertiesElement); // $entry->appendChild($objectElement); // } // // $entry = null; // $feed->newField('cmis:hasMoreItems', 'false', $entry, true); // // //Expose the responseFeed // $this->responseFeed = $feed; } } /** * AtomPub Service: document */ // TODO confirm that an error response is sent when a document has status "deleted" class KT_cmis_atom_service_document extends KT_cmis_atom_service { /** * Deals with GET actions for documents. * This includes individual document retrieval */ public function GET_action() { $RepositoryService = new RepositoryService(); $ObjectService = new ObjectService(KT_cmis_atom_service_helper::getKt()); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; // determine whether we want the document entry feed or the actual physical document content. // this depends on $this->params[1] if (!empty($this->params[1])) { $this->getContentStream($ObjectService, $repositoryId); return null; } $feed = KT_cmis_atom_service_helper::getObjectFeed($this, $ObjectService, $repositoryId, $this->params[0]); //Expose the responseFeed $this->responseFeed = $feed; } /** * Deals with DELETE actions for documents. * This includes deletion of a specific version of a document (latest version) via deleteObject * as well as deleteAllVersions * * @return 204 on success, 500 on error */ public function DELETE_action() { // NOTE due to the way KnowledgeTree works with documents this is always going to call deleteAllVersions. // we do not have support for deleting only specific versions (this may be added in the future.) $RepositoryService = new RepositoryService(); $VersioningService = new VersioningService(KT_cmis_atom_service_helper::getKt()); $repositories = $RepositoryService->getRepositories(); $repositoryId = $repositories[0]['repositoryId']; // attempt delete $response = $VersioningService->deleteAllVersions($repositoryId, $this->params[0]); // error? if (PEAR::isError($response)) { $feed = KT_cmis_atom_service_helper::getErrorFeed($this, self::STATUS_SERVER_ERROR, $response->getMessage()); //Expose the responseFeed $this->responseFeed = $feed; return null; } // success $this->setStatus(self::STATUS_NO_CONTENT); } private function getContentStream(&$ObjectService, $repositoryId) { $response = $ObjectService->getProperties($repositoryId, $this->params[0], false, false); if (PEAR::isError($response)) { $feed = KT_cmis_atom_service_helper::getErrorFeed($this, KT_cmis_atom_service::STATUS_SERVER_ERROR, $response->getMessage()); $this->responseFeed = $feed; return null; } // TODO also check If-Modified-Since? // $this->headers['If-Modified-Since'] => 2009-07-24 17:16:54 $this->contentDownload = true; $eTag = md5($response['properties']['LastModificationDate']['value'] . $response['properties']['ContentStreamLength']['value']); if ($this->headers['If-None-Match'] == $eTag) { $this->setStatus(self::STATUS_NOT_MODIFIED); $this->contentDownload = false; return null; } $contentStream = $ObjectService->getContentStream($repositoryId, $this->params[0]); // headers specific to output $this->setEtag($eTag); $this->setHeader('Last-Modified', $response['properties']['LastModificationDate']['value']); if (!empty($response['properties']['ContentStreamMimeType']['value'])) { $this->setHeader('Content-type', $response['properties']['ContentStreamMimeType']['value'] . ';charset=utf-8'); } else { $this->setHeader('Content-type', 'text/plain;charset=utf-8'); } $this->setHeader('Content-Disposition', 'attachment;filename="' . $response['properties']['ContentStreamFilename']['value'] . '"'); $this->setHeader('Content-Length', $response['properties']['ContentStreamLength']['value']); $this->output = $contentStream; } } ?>