/** * Creates a temporary file * Cleanup is the responsibility of the calling code * * @param string $contentStream The content to be stored (assumed to be base64) * @return string The path to the created file (for reference and cleanup.) */ public static function createTemporaryFile($contentStream) { // if contentStream is empty, cannot create file if (empty($contentStream)) { return null; } // TODO consider checking whether content is encoded (currently we expect encoded) // TODO choose between this and the alternative decode function (see CMISUtil class) // this will require some basic benchmarking $contentStream = self::decodeChunkedContentStream($contentStream); // NOTE There is a function in CMISUtil to do this, written for the unit tests but since KTUploadManager exists // and has more functionality which could come in useful at some point I decided to go with that instead // (did not know this existed when I wrote the CMISUtil function) $uploadManager = new KTUploadManager(); // assumes already decoded from base64, should use store_base64_file if not $tempfilename = $uploadManager->store_file($contentStream, 'cmis_'); return $tempfilename; }
/** * This checks a document into the repository. * * <code> * $ktapi = new KTAPI(); * $session = $ktapi->start_system_session(); * $document = $ktapi->get_document_by_id($documentid); * if($document->is_checked_out()){ * $document->checkin('filename.txt', 'Reason for checkin', '/tmp/filename'); * } * </code> * * @author KnowledgeTree Team * @access public * @param string $filename The name of the file * @param string $reason The reason for checking the document in * @param string $tempfilename The location of the temporary file * @param bool $major_update Determines if the version number should have a major increment (+1) or a minor increment (+0.1) */ function checkin($filename, $reason, $tempfilename, $major_update = false) { if (!is_file($tempfilename)) { return new PEAR_Error('File does not exist.'); } $user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_WRITE); if (PEAR::isError($user)) { return $user; } if (!$this->document->getIsCheckedOut()) { return new PEAR_Error(KTAPI_ERROR_DOCUMENT_NOT_CHECKED_OUT); } $filename = KTUtil::replaceInvalidCharacters($filename); $options = array('major_update' => $major_update); $currentfilename = $this->document->getFileName(); if ($filename != $currentfilename) { $options['newfilename'] = $filename; } DBUtil::startTransaction(); $result = KTDocumentUtil::checkin($this->document, $tempfilename, $reason, $user, $options); if (PEAR::isError($result)) { DBUtil::rollback(); return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $result); } DBUtil::commit(); KTUploadManager::temporary_file_imported($tempfilename); }
if ($action == 'C') { if (!array_key_exists('document_id', $_POST)) { respond(6, 'Document ID not specified.'); } $document_id = $_POST['document_id']; } require_once '../ktapi/ktapi.inc.php'; require_once 'KTUploadManager.inc.php'; $apptype = isset($_POST['apptype']) ? $_POST['apptype'] : 'ws'; $session_id = $_POST['session_id']; $ktapi = new KTAPI(); $session = $ktapi->get_active_session($session_id, null, $apptype); if (PEAR::isError($session)) { respond(4, $session->getMessage()); } $upload_manager = new KTUploadManager(); $upload_manager->cleanup(); $upload_manager->set_session($session); $failed = 0; $added = array(); $lastMessage = ''; foreach ($_FILES as $key => $file) { $filename = $file['name']; $tempfile = $file['tmp_name']; $error = $file['error']; if ($error == UPLOAD_ERR_OK) { $result = $upload_manager->uploaded($filename, $tempfile, $action); if (PEAR::isError($result)) { $lastMessage = $result->getMessage(); $default->log->error("Cannot upload file '{$filename}'. Temp location: '{$tempfile}'. " . $lastMessage); $failed++;
* the terms of the GNU General Public License version 3 as published by the * Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, * California 94120-7775, or email info@knowledgetree.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices * must display the words "Powered by KnowledgeTree" and retain the original * copyright notice. * Contributor( s): ______________________________________ * */ require_once '../config/dmsDefaults.php'; require_once 'KTUploadManager.inc.php'; $upload_manager = new KTUploadManager(); $upload_manager->cleanup();
public function setContentStream($repositoryId, $documentId, $overwriteFlag, $contentStream, $changeToken = null) { // if no document id was supplied, we are going to create the underlying physical document // NOTE while it might have been nice to keep this out of here, KTAPI has no method for creating a document without // a physical upload, so we cannot create the document first and then add the upload as a content stream, the // entire creation step needs to happen here. // Attempt to decode $documentId, use as is if not detected as encoded $tmpObjectId = $documentId; $tmpObjectId = CMISUtil::decodeObjectId($tmpObjectId, $tmpTypeId); if ($tmpTypeId != 'Unknown') { $documentId = $tmpObjectId; } // TODO deal with other types except documents // fetch type definition of supplied document $CMISDocument = new CMISDocumentObject($documentId, $this->ktapi); // if content stream is not allowed for this object type definition, throw a ConstraintViolationException if ($CMISDocument->getAttribute('contentStreamAllowed') == 'notAllowed') { // NOTE spec version 0.61c specifies both a ConstraintViolationException and a StreamNotSupportedException // for this case. Choosing to throw StreamNotSupportedException until the specification is clarified // as it is a more specific exception throw new StreamNotSupportedException('Content Streams are not allowed for this object type'); } $csFileName = $CMISDocument->getProperty('ContentStreamFilename'); if (!empty($csFileName) && !$overwriteFlag) { throw new ContentAlreadyExistsException('Unable to overwrite existing content stream'); } // NOTE There is a function in CMISUtil to do this but since KTUploadManager exists and has more functionality // which could come in useful at some point I decided to go with that instead (did not know it existed when // I wrote the CMISUtil function) $uploadManager = new KTUploadManager(); $tempfilename = $uploadManager->store_base64_file($contentStream, 'cmis_'); // update the document content from this temporary file as per usual // TODO Use checkin_document_with_metadata instead if metadata content submitted || update metadata separately? $response = $this->ktapi->checkin_document($documentId, $csFileName, 'CMIS setContentStream action', $tempfilename, false); if ($response['status_code'] != 0) { throw new StorageException('Unable to update the content stream. ' . $response['message']); } // else // { // $objectId = CMISUtil::encodeObjectId('Document', $response['results']['id']); // } @unlink($csFile); // update the CMIS document object with the content stream information // $CMISDocument->reload($document['result']['document_id']); return $CMISDocument->getProperty('ObjectId'); }
/** * This adds a document to the current folder. * * <code> * $kt = new KTAPI(); * $kt->start_session("admin", "admin"); * $folder = $kt->get_folder_by_name("My New folder"); * $res = $folder->add_document("Test Document", "test.txt", "Default", $tmpfname); * </code> * * @author KnowledgeTree Team * @access public * @param string $title This is the title for the file in the repository. * @param string $filename This is the filename in the system for the file. * @param string $documenttype This is the name or id of the document type. It first looks by name, then by id. * @param string $tempfilename This is a reference to the file that is accessible locally on the file system. * @return KTAPI_Document */ function add_document($title, $filename, $documenttype, $tempfilename) { if (!is_file($tempfilename)) { return new PEAR_Error('File does not exist.'); } $user = $this->can_user_access_object_requiring_permission($this->folder, KTAPI_PERMISSION_WRITE); if (PEAR::isError($user)) { return $user; } //KTS-4016: removed the replacing of special characters from the title as they should be allowed there //$title = KTUtil::replaceInvalidCharacters($title); $filename = basename($filename); $filename = KTUtil::replaceInvalidCharacters($filename); $documenttypeid = KTAPI::get_documenttypeid($documenttype); if (PEAR::isError($documenttypeid)) { $config = KTCache::getSingleton(); $defaultToDefaultDocType = $config->get('webservice/useDefaultDocumentTypeIfInvalid', true); if ($defaultToDefaultDocType) { $documenttypeid = KTAPI::get_documenttypeid('Default'); } else { return new KTAPI_DocumentTypeError('The document type could not be resolved or is disabled: ' . $documenttype); } } $options = array('contents' => new KTFSFileLike($tempfilename), 'temp_file' => $tempfilename, 'novalidate' => true, 'documenttype' => DocumentType::get($documenttypeid), 'description' => $title, 'metadata' => array(), 'cleanup_initial_file' => true); DBUtil::startTransaction(); $document =& KTDocumentUtil::add($this->folder, $filename, $user, $options); if (PEAR::isError($document)) { DBUtil::rollback(); return new PEAR_Error(KTAPI_ERROR_INTERNAL_ERROR . ' : ' . $document->getMessage()); } DBUtil::commit(); KTUploadManager::temporary_file_imported($tempfilename); return new KTAPI_Document($this->ktapi, $this, $document); }
/** * Checkin Document //TODO: Find out how upload works * params contains: * document_id * filename * reason * tempfilename * * @param array $params */ function checkin_document($params) { $session_id = $this->AuthInfo['session']; $document_id = $params['document_id']; $filename = $params['filename']; $reason = $params['reason']; $tempfilename = $params['tempfilename']; $application = $this->AuthInfo['appType']; $this->addDebug('Checkin', "checkin_document('{$session_id}',{$document_id},'{$filename}','{$reason}','{$tempfilename}', '{$application}')"); $kt =& $this->KT; // we need to add some security to ensure that people don't frig the checkin process to access restricted files. // possibly should change 'tempfilename' to be a hash or id of some sort if this is troublesome. $upload_manager = new KTUploadManager(); if (!$upload_manager->is_valid_temporary_file($tempfilename)) { $this->setResponse(array('status_code' => 12)); return; } $document =& $kt->get_document_by_id($document_id); if (PEAR::isError($document)) { $this->setResponse(array('status_code' => 13)); } // checkin $result = $document->checkin($filename, $reason, $tempfilename, false); if (PEAR::isError($result)) { $this->setResponse(array('status_code' => 14)); } // get status after checkin //$this->response= $this->get_document_detail($session_id, $document_id); $detail = $document->get_detail(); $detail['status_code'] = 0; $detail['message'] = ''; $this->setResponse($detail); }
/** * Does a document checkin. * * @param string $session_id * @param int $document_id * @param string $filename * @param string $reason * @param string $base64 * @param boolean $major_update * @return kt_document_detail. status_code can be KTWS_ERR_INVALID_SESSION, KTWS_ERR_INVALID_DOCUMENT, KTWS_ERR_INVALID_DOCUMENT or KTWS_SUCCESS */ function checkin_small_document($session_id, $document_id, $filename, $reason, $base64, $major_update) { $this->debug("checkin_small_document('{$session_id}',{$document_id},'{$filename}','{$reason}','*** base64 content ***',{$major_update})"); $kt =& $this->get_ktapi($session_id); if (is_array($kt)) { return new SOAP_Value('return', "{urn:{$this->namespace}}kt_document_detail", $kt); } $response = KTWebService::_status(KTWS_ERR_INVALID_DOCUMENT); $upload_manager = new KTUploadManager(); $tempfilename = $upload_manager->store_base64_file($base64, 'su_'); if (PEAR::isError($tempfilename)) { $reason = $tempfilename->getMessage(); $response = KTWebService::_status(KTWS_ERR_INVALID_DOCUMENT, 'Cannot write to temp file: ' . $tempfilename . ". Reason: {$reason}"); $this->debug("checkin_small_document - cannot write {$tempfilename}. Reason: {$reason}", $session_id); return new SOAP_Value('return', "{urn:{$this->namespace}}kt_document_detail", $response); } // simulate the upload $tempfilename = $upload_manager->uploaded($filename, $tempfilename, 'C'); $document =& $kt->get_document_by_id($document_id); if (PEAR::isError($document)) { $response['message'] = $document->getMessage(); $this->debug("checkin_small_document - cannot get documentid {$document_id} - " . $document->getMessage(), $session_id); return new SOAP_Value('return', "{urn:{$this->namespace}}kt_document_detail", $response); } $result = $document->checkin($filename, $reason, $tempfilename, $major_update); if (PEAR::isError($result)) { $response['message'] = $result->getMessage(); $this->debug("checkin_small_document - cannot checkin document - " . $result->getMessage(), $session_id); return new SOAP_Value('return', "{urn:{$this->namespace}}kt_document_detail", $response); } // get status after checkin return $this->get_document_detail($session_id, $document_id); }
/** * Does a document checkin. * * @author KnowledgeTree Team * @access public * @param int $document_id * @param string $filename * @param string $reason * @param string $base64 * @param boolean $major_update * @return kt_document_detail. */ public function checkin_small_document($document_id, $filename, $reason, $base64, $major_update, $sig_username = '', $sig_password = '') { $response = $this->_check_electronic_signature($document_id, $sig_username, $sig_password, $reason, $reason, 'ktcore.transactions.check_in'); if ($response['status_code'] == 1) { return $response; } $upload_manager = new KTUploadManager(); $tempfilename = $upload_manager->store_base64_file($base64, 'su_'); if (PEAR::isError($tempfilename)) { $reason = $tempfilename->getMessage(); $response['status_code'] = 1; $response['message'] = 'Cannot write to temp file: ' . $tempfilename . ". Reason: {$reason}"; return $response; } // simulate the upload $tempfilename = $upload_manager->uploaded($filename, $tempfilename, 'C'); $document =& $this->get_document_by_id($document_id); if (PEAR::isError($document)) { $response['status_code'] = 1; $response['message'] = $document->getMessage(); return $response; } $result = $document->checkin($filename, $reason, $tempfilename, $major_update); if (PEAR::isError($result)) { $response['status_code'] = 1; $response['message'] = $result->getMessage(); return $response; } // get status after checkin return $this->get_document_detail($document_id); }
function add_document_params($params) { $folder_id = $params['folder_id']; $title = $params['title']; $filename = $params['filename']; $documenttype = $params['documenttype']; $tempfilename = $params['tempfilename']; $application = $params['application']; $this->addDebug('', 'Entered add_document'); $kt =& $this->KT; $upload_manager = new KTUploadManager(); if (!$upload_manager->is_valid_temporary_file($tempfilename)) { $this->addError('Temporary File Not Valid'); $this->setResponse(array('status_code' => 1)); return false; } $this->addDebug('', 'Exited is_valid_temporary file'); $folder =& $kt->get_folder_by_id($folder_id); if (PEAR::isError($folder)) { $this->addError('Could not find Folder ' . $folder_id); $this->setResponse(array('status_code' => 1)); return false; } $this->addDebug('', 'Exited get_folder_by_id'); $document =& $folder->add_document($title, $filename, $documenttype, $tempfilename); if (PEAR::isError($document)) { $this->addError("Could add Document [title:{$title},filename:{$filename},documenttype:{$documenttype},tempfilename:{$tempfilename}]"); $this->setResponse(array('status_code' => 1)); return false; } $this->addDebug('', 'Exited folder add_document'); $detail = $document->get_detail(); $detail['status_code'] = 0; $detail['message'] = ''; $this->setResponse($detail); }