function &_addFile($sFileName, $aOptions = null) { if (!is_array($aOptions)) { $aOptions = array(); } $oDocument =& KTDocumentUtil::add($this->oFolder, $sFileName, $this->oUser, $aOptions); return $oDocument; }
function testBreadcrumbsForDocument() { $oFolder =& KTFolderUtil::add($this->oFolder, 'testBreadcrumbsForDocument', $this->oUser); $oDocument =& KTDocumentUtil::add($oFolder, 'testBreadcrumbsForDocument.txt', $this->oUser, array()); $aBreadcrumbs =& KTBrowseUtil::breadcrumbsForDocument($oDocument, array('final' => true)); $this->assertEqual($this->_getId($aBreadcrumbs[0]['url']), 1); $this->assertEqual($aBreadcrumbs[0]['name'], 'Folders'); $this->assertEqual($this->_getId($aBreadcrumbs[1]['url']), $this->oFolder->getId()); $this->assertEqual($aBreadcrumbs[1]['name'], $this->oFolder->getName()); $this->assertEqual($this->_getId($aBreadcrumbs[2]['url']), $oFolder->getId()); $this->assertEqual($aBreadcrumbs[2]['name'], $oFolder->getName()); $this->assertNull($aBreadcrumbs[3]['url']); $this->assertEqual($aBreadcrumbs[3]['name'], $oDocument->getName()); $aBreadcrumbs =& KTBrowseUtil::breadcrumbsForDocument($oDocument, array('final' => false)); $this->assertEqual($this->_getId($aBreadcrumbs[0]['url']), 1); $this->assertEqual($aBreadcrumbs[0]['name'], 'Folders'); $this->assertEqual($this->_getId($aBreadcrumbs[1]['url']), $this->oFolder->getId()); $this->assertEqual($aBreadcrumbs[1]['name'], $this->oFolder->getName()); $this->assertEqual($this->_getId($aBreadcrumbs[2]['url']), $oFolder->getId()); $this->assertEqual($aBreadcrumbs[2]['name'], $oFolder->getName()); $this->assertEqual($this->_getId($aBreadcrumbs[3]['url']), $oDocument->getId()); $this->assertEqual($aBreadcrumbs[3]['name'], $oDocument->getName()); }
<?php require_once "../../config/dmsDefaults.php"; require_once KT_LIB_DIR . '/documentmanagement/documentutil.inc.php'; require_once KT_LIB_DIR . '/filelike/fsfilelike.inc.php'; error_reporting(E_ALL); $oFolder =& Folder::get(1); $oUser =& User::get(1); $sLocalname = KT_DIR . "/tests/document/dataset1/critique-of-pure-reason.txt"; $sFilename = tempnam("/tmp", "kt_tests_document_add"); copy($sLocalname, $sFilename); DBUtil::startTransaction(); $oDocument =& KTDocumentUtil::add($oFolder, "testfullupload2.txt", $oUser, array('contents' => new KTFSFileLike($sFilename), 'metadata' => array())); if (PEAR::isError($oDocument)) { print "FAILURE\n"; var_dump($oDocument); exit(0); } DBUtil::commit(); print "SUCCESS\n";
/** * PUT method handler * * @param array parameter passing array * @return string HTTP status code or false */ function PUT(&$options) { global $default; if ($this->checkSafeMode()) { $this->ktwebdavLog("Entering PUT. options are " . print_r($options, true), 'info', true); $this->ktwebdavLog("dav_client is: " . $this->dav_client, 'info', true); $path = $options["path"]; // Fix for Mac // Modified - 22/10/07 // Mac adds DS_Store files when folders are added and ._filename files when files are added // we want to ignore them. if ($this->dav_client == 'MC' || $this->dav_client == 'MG') { // Remove filename from path $aPath = explode('/', $path); $fileName = $aPath[count($aPath) - 1]; if (strtolower($fileName) == '.ds_store') { $this->ktwebdavLog("Using a mac client. Ignore the .DS_Store files created with every folder.", 'info', true); // ignore return "204 No Content"; } if ($fileName[0] == '.' && $fileName[1] == '_') { $fileName = substr($fileName, 2); $this->ktwebdavLog("Using a mac client. Ignore the ._filename files created with every file.", 'info', true); // ignore return "204 No Content"; } } $res = $this->_folderOrDocument($path); list($iFolderID, $iDocumentID) = $res; if ($iDocumentID === false && $iFolderID === false) { // Couldn't find intermediary paths /* * RFC2518: 8.7.1 PUT for Non-Collection Resources * * 409 (Conflict) - A PUT that would result in the creation * of a resource without an appropriately scoped parent collection * MUST fail with a 409 (Conflict). */ return "409 Conflict - Couldn't find intermediary paths"; } $oParentFolder =& Folder::get($iFolderID); // Check if the user has permissions to write in this folder $oPerm =& KTPermission::getByName('ktcore.permissions.write'); $oUser =& User::get($this->userID); if (!KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oParentFolder)) { return "403 Forbidden - User does not have sufficient permissions"; } $this->ktwebdavLog("iDocumentID is " . $iDocumentID, 'info', true); if (is_null($iDocumentID)) { // This means there is a folder with the given path $this->ktwebdavLog("405 Method not allowed", 'info', true); return "405 Method not allowed - There is a folder with the given path"; } if ($iDocumentID == false) { $this->ktwebdavLog("iDocumentID is false", 'info', true); } if ($iDocumentID !== false) { // This means there is a document with the given path $oDocument = Document::get($iDocumentID); $this->ktwebdavLog("oDocument is " . print_r($oDocument, true), 'info', true); $this->ktwebdavLog("oDocument statusid is " . print_r($oDocument->getStatusID(), true), 'info', true); if ((int) $oDocument->getStatusID() != STATUS_WEBDAV && (int) $oDocument->getStatusID() != DELETED) { $this->ktwebdavLog("Trying to PUT to an existing document", 'info', true); if (!$this->dav_client == "MS" && !$this->dav_client == "MC") { return "409 Conflict - There is a document with the given path"; } } // FIXME: Direct filesystem access $fh = $options["stream"]; $sTempFilename = tempnam('/tmp', 'ktwebdav_dav_put'); $ofh = fopen($sTempFilename, 'w'); $contents = ''; while (!feof($fh)) { $contents .= fread($fh, 8192); } $fres = fwrite($ofh, $contents); $this->ktwebdavLog("A DELETED or CHECKEDOUT document exists. Overwriting...", 'info', true); $this->ktwebdavLog("Temp Filename is: " . $sTempFilename, 'info', true); $this->ktwebdavLog("File write result size was: " . $fres, 'info', true); fflush($fh); fclose($fh); fflush($ofh); fclose($ofh); $this->ktwebdavLog("Files have been flushed and closed.", 'info', true); $name = basename($path); $aFileArray = array("name" => $name, "size" => filesize($sTempFilename), "type" => false, "userID" => $this->_getUserID()); $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true); //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); $aOptions = array('temp_file' => $sTempFilename, 'metadata' => array(), 'novalidate' => true); $this->ktwebdavLog("DEBUG: overwriting file. Options: " . print_r($aOptions, true)); $this->ktwebdavLog("DEBUG: overwriting file. Temp name: " . $sTempFilename . ' ' . print_r($sTempFilename, true)); $this->ktwebdavLog("DEBUG: overwriting file. Name: " . $name . ' ' . print_r($name, true)); // Modified - 25/10/07 - changed add to overwrite //$oDocument =& KTDocumentUtil::add($oParentFolder, $name, $oUser, $aOptions); $oDocument =& KTDocumentUtil::overwrite($oDocument, $name, $sTempFilename, $oUser, $aOptions); if (PEAR::isError($oDocument)) { $this->ktwebdavLog("oDocument ERROR: " . $oDocument->getMessage(), 'info', true); unlink($sTempFilename); return "409 Conflict - " . $oDocument->getMessage(); } $this->ktwebdavLog("oDocument is " . print_r($oDocument, true), 'info', true); unlink($sTempFilename); return "201 Created"; } $options["new"] = true; // FIXME: Direct filesystem access $fh = $options["stream"]; $sTempFilename = tempnam('/tmp', 'ktwebdav_dav_put'); $ofh = fopen($sTempFilename, 'w'); $contents = ''; while (!feof($fh)) { $contents .= fread($fh, 8192); } $fres = fwrite($ofh, $contents); $this->ktwebdavLog("Content length was not 0, doing the whole thing.", 'info', true); $this->ktwebdavLog("Temp Filename is: " . $sTempFilename, 'info', true); $this->ktwebdavLog("File write result size was: " . $fres, 'info', true); fflush($fh); fclose($fh); fflush($ofh); fclose($ofh); $this->ktwebdavLog("Files have been flushed and closed.", 'info', true); $name = basename($path); $aFileArray = array("name" => $name, "size" => filesize($sTempFilename), "type" => false, "userID" => $this->_getUserID()); $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true); //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); $aOptions = array('temp_file' => $sTempFilename, 'metadata' => array(), 'novalidate' => true); $oDocument =& KTDocumentUtil::add($oParentFolder, $name, $oUser, $aOptions); if (PEAR::isError($oDocument)) { $this->ktwebdavLog("oDocument ERROR: " . $oDocument->getMessage(), 'info', true); unlink($sTempFilename); return "409 Conflict - " . $oDocument->getMessage(); } $this->ktwebdavLog("oDocument is " . print_r($oDocument, true), 'info', true); unlink($sTempFilename); return "201 Created"; } else { return "423 Locked - KTWebDAV is in SafeMode"; } }
function _importdocument($oFolder, $sPath) { $aInfo = $this->oStorage->getDocumentInfo($sPath); $sTmpFileName = sprintf("%s/%s", $this->oStorage->sBasePath, $sPath); if (PEAR::isError($aInfo)) { return $aInfo; } // need to check both of these. /*if (KTDocumentUtil::nameExists($oFolder, utf8_encode(basename($sPath)))) { $_SESSION['KTErrorMessage'][] = sprintf(_kt("The document %s is already present in %s. Ignoring."), utf8_encode(basename($sPath)), $oFolder->getName()); $oDocument =& Document::getByNameAndFolder(utf8_encode(basename($sPath)), KTUtil::getId($oFolder)); return $oDocument; } else if (KTDocumentUtil::fileExists($oFolder, utf8_encode(basename($sPath)))) { $_SESSION['KTErrorMessage'][] = sprintf(_kt("The document %s is already present in %s. Ignoring."), utf8_encode(basename($sPath)), $oFolder->getName()); $oDocument =& Document::getByFilenameAndFolder(utf8_encode(basename($sPath)), KTUtil::getId($oFolder)); return $oDocument; }*/ // else $aOptions = array('temp_file' => $sTmpFileName, 'metadata' => $this->aMetadata, 'documenttype' => $this->oDocumentType); $aOptions = array_merge($aOptions, $this->aOptions); $sPath = basename($sPath); $sPath = $this->is_utf8($sPath) ? $sPath : utf8_encode($sPath); $oDocument =& KTDocumentUtil::add($oFolder, $sPath, $this->oUser, $aOptions, true); return $oDocument; }
function do_finalise() { $this->persistParams(array('fFileKey')); $sess_key = $_REQUEST['fFileKey']; $oForm = $this->form_metadata($sess_key); $res = $oForm->validate(); if (!empty($res['errors'])) { return $oForm->handleError(); } $data = $res['results']; $extra_d = $_SESSION['_add_data'][$sess_key]; $doctypeid = $extra_d['document_type']; $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); $aSpecificFieldsetIds = KTFieldset::getForDocumentType($doctypeid, array('ids' => false)); $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); $MDPack = array(); foreach ($fieldsets as $oFieldset) { $fields = $oFieldset->getFields(); $values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId()); foreach ($fields as $oField) { $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); if ($oFieldset->getIsConditional()) { if ($val == _kt('No selection.')) { $val = null; } } // ALT.METADATA.LAYER.DIE.DIE.DIE if (!is_null($val)) { $MDPack[] = array($oField, $val); } } } // older code $mpo =& new JavascriptObserver($this); $oUploadChannel =& KTUploadChannel::getSingleton(); $oUploadChannel->addObserver($mpo); require_once KT_LIB_DIR . '/storage/storagemanager.inc.php'; //require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); require_once KT_LIB_DIR . '/documentmanagement/DocumentType.inc'; require_once KT_LIB_DIR . '/metadata/fieldset.inc.php'; require_once KT_LIB_DIR . '/documentmanagement/documentutil.inc.php'; $aErrorOptions = array('redirect_to' => array('main', sprintf('fFolderId=%d', $this->oFolder->getId())), 'max_str_len' => 200); $aFile = $this->oValidator->validateFile($extra_d['file'], $aErrorOptions); $sTitle = $extra_d['document_name']; $iFolderId = $this->oFolder->getId(); $aOptions = array('temp_file' => $aFile['tmp_name'], 'documenttype' => DocumentType::get($extra_d['document_type']), 'metadata' => $MDPack, 'description' => $sTitle, 'cleanup_initial_file' => true); $mpo->start(); //$this->startTransaction(); $oDocument =& KTDocumentUtil::add($this->oFolder, $aFile['name'], $this->oUser, $aOptions); if (PEAR::isError($oDocument)) { $message = $oDocument->getMessage(); $this->errorRedirectTo('main', sprintf(_kt("Unexpected failure to add document - %s"), $message), 'fFolderId=' . $this->oFolder->getId()); exit(0); } $this->addInfoMessage(_kt("Document added")); //$this->commitTransaction(); $mpo->redirectToDocument($oDocument->getId()); exit(0); }
/** * 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); }
require_once KT_LIB_DIR . '/filelike/fsfilelike.inc.php'; error_reporting(E_ALL); $oFolder =& Folder::get(1); $oUser =& User::get(1); $sLocalname = KT_DIR . "/tests/document/dataset1/critique-of-pure-reason.txt"; $sFilename = tempnam("/tmp", "kt_tests_document_add"); copy($sLocalname, $sFilename); $oDocument =& KTDocumentUtil::add($oFolder, "testquickupload.txt", $oUser, array('contents' => new KTFSFileLike($sFilename))); if (PEAR::isError($oDocument)) { print "FAILURE\n"; var_dump($oDocument); exit(0); } if (!file_exists($sFilename)) { copy($sLocalname, $sFilename); } $oDocument =& KTDocumentUtil::add($oFolder, "newtest2.txt", $oUser, array()); if (PEAR::isError($oDocument)) { print "FAILURE\n"; var_dump($oDocument); } $res = KTDocumentUtil::storeContents($oDocument, new KTFSFileLike($sFilename)); var_dump($res); /* if (file_exists($sFilename)) { unlink($sFilename); } $oDocument->setStatusID(LIVE); $oDocument->update(); */