Пример #1
0
 function do_copy()
 {
     $oForm = $this->form_copyselection();
     $res = $oForm->validate();
     $errors = $res['errors'];
     $data = $res['results'];
     $sReason = $data['reason'];
     $extra_errors = array();
     if (!is_null($data['browse'])) {
         $bNameClash = KTDocumentUtil::nameExists($data['browse'], $this->oDocument->getName());
         if ($bNameClash && isset($data['name'])) {
             $name = $data['name'];
             $bNameClash = KTDocumentUtil::nameExists($data['browse'], $name);
         } else {
             $name = $this->oDocument->getName();
         }
         if ($bNameClash) {
             $extra_errors['name'] = _kt('A document with this title already exists in your chosen folder.  Please choose a different folder, or specify a new title for the copied document.');
         }
         $bFileClash = KTDocumentUtil::fileExists($data['browse'], $this->oDocument->getFilename());
         if ($bFileClash && isset($data['filename'])) {
             $filename = $data['filename'];
             $bFileClash = KTDocumentUtil::fileExists($data['browse'], $filename);
         } else {
             $filename = $this->oDocument->getFilename();
         }
         if ($bFileClash) {
             $extra_errors['filename'] = _kt('A document with this filename already exists in your chosen folder.  Please choose a different folder, or specify a new filename for the copied document.');
         }
         if (!Permission::userHasFolderWritePermission($data['browse'])) {
             $extra_errors['browse'] = _kt('You do not have permission to create new documents in that folder.');
         }
     }
     if (!empty($errors) || !empty($extra_errors)) {
         return $oForm->handleError(null, $extra_errors);
     }
     // FIXME agree on document-duplication rules re: naming, etc.
     $this->startTransaction();
     // now try update it.
     $oNewDoc = KTDocumentUtil::copy($this->oDocument, $data['browse'], $sReason);
     if (PEAR::isError($oNewDoc)) {
         $this->errorRedirectTo('main', _kt('Failed to copy document: ') . $oNewDoc->getMessage(), sprintf('fDocumentId=%d&fFolderId=%d', $this->oDocument->getId(), $this->oFolder->getId()));
         exit(0);
     }
     $oNewDoc->setName($name);
     $oNewDoc->setFilename($filename);
     $res = $oNewDoc->update();
     if (PEAR::isError($res)) {
         return $this->errorRedirectTo('main', _kt('Failed to copy document: ') . $res->getMessage(), sprintf('fDocumentId=%d&fFolderId=%d', $this->oDocument->getId(), $this->oFolder->getId()));
     }
     $this->commitTransaction();
     $_SESSION['KTInfoMessage'][] = _kt('Document copied.');
     controllerRedirect('viewDocument', 'fDocumentId=' . $oNewDoc->getId());
     exit(0);
 }
Пример #2
0
 /**
  * COPY method helper for Documents
  *
  * @param $options array   parameter passing array
  * @param $iFolderID int     Folder ID
  * @param $iDocumentID int     Document ID
  * @return string  HTTP status code or false
  */
 function _COPYDocument($options, $iFolderID, $iDocumentID)
 {
     /* ** Ensure that the destination path exists ** */
     if ($options['dest'] == '') {
         $options["dest"] = substr($options["dest_url"], strlen($_SERVER["SCRIPT_NAME"]));
     }
     $this->ktwebdavLog("Entering _COPYDocument. options are " . print_r($options, true), 'info', true);
     /* ** Get the relevant paths. Get the basename of the destination path as the destination filename.
        Check whether the destination path refers to a folder / document. ** */
     $source_path = $options["path"];
     $dest_path = urldecode($options["dest"]);
     $sDestFileName = basename($dest_path);
     list($iDestFolder, $iDestDoc) = $this->_folderOrDocument($dest_path);
     if ($iDestFolder === false) {
         return "409 Conflict - Destination folder does not exist.";
     }
     /* ** Depth must be infinity to copy a document ** */
     if ($options["depth"] != "infinity") {
         // RFC 2518 Section 9.2, last paragraph
         $this->ktwebdavLog("400 Bad request", 'info', true);
         return "400 Bad request - Depth must be 'infinity'.";
     }
     global $default;
     /* ** Get the source folder object.
        If the destination document is null, then the destination is a folder, set the destination filename to empty, continue.
        If the destination document returns an id, then the document exists. Check overwrite.
        If overwrite is true, then check permissions and delete the document, continue.
        If the destination document is false, then continue. ** */
     $oSrcFolder = Folder::get($iFolderID);
     $new = true;
     if (is_null($iDestDoc)) {
         // the dest is a folder
         //			$this->ktwebdavLog("400 Bad request", 'info', true);
         $this->ktwebdavLog("Destination is a folder.", 'info', true);
         $sDestFileName = '';
         //return "400 Bad request - Destination is a Folder";
     } else {
         if ($iDestDoc !== false) {
             // Document exists
             $this->ktwebdavLog("Destination Document exists.", 'info', true);
             $oReplaceDoc = Document::get($iDestDoc);
             if ($options['overwrite'] != 'T') {
                 $this->ktwebdavLog("Overwrite needs to be TRUE.", 'info', true);
                 return "412 Precondition Failed - Destination Document exists. Overwrite needs to be TRUE.";
             }
             $this->ktwebdavLog("Overwrite is TRUE, deleting Destination Document.", 'info', true);
             // Check if the user has permissions to delete this document
             $oPerm =& KTPermission::getByName('ktcore.permissions.delete');
             $oUser =& User::get($this->userID);
             if (!KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oReplaceDoc)) {
                 return "403 Forbidden - User does not have sufficient permissions";
             }
             KTDocumentUtil::delete($oReplaceDoc, 'KTWebDAV copy with overwrite set.');
             $new = false;
         }
     }
     /* ** Get the destination folder object and the source document object.
        Check if user has permission to write to the document and folder.
        Copy the document. ** */
     $oDestFolder = Folder::get($iDestFolder);
     $oSrcDoc = Document::get($iDocumentID);
     include_once KT_LIB_DIR . '/foldermanagement/folderutil.inc.php';
     $this->ktwebdavLog("Got an oSrcDoc of " . $oSrcDoc->getName() . print_r($oSrcDoc, true), 'info', true);
     $this->ktwebdavLog("Got an oDestFolder of " . $oDestFolder->getName() . print_r($oDestFolder, true), 'info', true);
     // 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, $oDestFolder)) {
         return "403 Forbidden - User does not have sufficient permissions";
     }
     $reason = isset($_SERVER['HTTP_REASON']) && !empty($_SERVER['HTTP_REASON']) ? $_SERVER['HTTP_REASON'] : "KTWebDAV Copy.";
     $oDesDoc = KTDocumentUtil::copy($oSrcDoc, $oDestFolder, $reason, $sDestFileName);
     if (PEAR::isError($oDesDoc)) {
         $this->ktwebdavLog("Copy on document failed: " . $oDesDoc->getMessage(), 'info', true);
         return "500 Internal Server Error - Copy on document failed.";
     }
     if ($new) {
         $this->ktwebdavLog("201 Created", 'info', true);
         return "201 Created";
     } else {
         $this->ktwebdavLog("204 No Content", 'info', true);
         return "204 No Content";
     }
 }
Пример #3
0
 /**
  * Copies the document from one folder to another.
  *
  * <code>
  * $ktapi = new KTAPI();
  * $session = $ktapi->start_system_session();
  * $document = $ktapi->get_document_by_id($documentid);
  * $newFolder = $this->root->add_folder("New folder");
  * $copyOfDoc = $document->copy($newFolder, 'Reason for copying document');
  * </code>
  *
  * @author KnowledgeTree Team
  * @access public
  * @param KTAPI_Folder $ktapi_target_folder The new parent folder where the document is being copied into
  * @param string $reason The reason for the copy
  * @param string $newname Optional. The title of the document to be used in the case of a name clash
  * @param string $newfilename Optional. The filename of the document to be used in the case of a name clash
  * @return KTAPI_Document|PEAR_Error Returns the new KTAPI Document object | a PEAR_Error on failure
  */
 function copy(&$ktapi_target_folder, $reason, $newname = null, $newfilename = null)
 {
     assert(!is_null($ktapi_target_folder));
     assert($ktapi_target_folder instanceof KTAPI_FOLDER);
     //is_a($ktapi_target_folder,'KTAPI_Folder'));
     if (empty($newname)) {
         $newname = null;
     }
     if (empty($newfilename)) {
         $newfilename = null;
     }
     $user = $this->ktapi->get_user();
     if ($this->document->getIsCheckedOut()) {
         return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT);
     }
     $target_folder =& $ktapi_target_folder->get_folder();
     $result = $this->can_user_access_object_requiring_permission($target_folder, KTAPI_PERMISSION_WRITE);
     if (PEAR::isError($result)) {
         return $result;
     }
     $name = $this->document->getName();
     $clash = KTDocumentUtil::nameExists($target_folder, $name);
     if ($clash && !is_null($newname)) {
         $name = $newname;
         $clash = KTDocumentUtil::nameExists($target_folder, $name);
     }
     if ($clash) {
         if (is_null($newname)) {
             $name = KTDocumentUtil::getUniqueDocumentName($target_folder, $name);
         } else {
             return new PEAR_Error('A document with this title already exists in your chosen folder.  Please choose a different folder, or specify a new title for the copied document.');
         }
     }
     $filename = $this->document->getFilename();
     $clash = KTDocumentUtil::fileExists($target_folder, $filename);
     if ($clash && !is_null($newname)) {
         $filename = $newfilename;
         $clash = KTDocumentUtil::fileExists($target_folder, $filename);
     }
     if ($clash) {
         if (is_null($newfilename)) {
             $filename = KTDocumentUtil::getUniqueFilename($target_folder, $newfilename);
         } else {
             return new PEAR_Error('A document with this filename already exists in your chosen folder.  Please choose a different folder, or specify a new filename for the copied document.');
         }
     }
     DBUtil::startTransaction();
     $new_document = KTDocumentUtil::copy($this->document, $target_folder, $reason);
     if (PEAR::isError($new_document)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $new_document);
     }
     $new_document->setName($name);
     $new_document->setFilename($filename);
     $res = $new_document->update();
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
     }
     DBUtil::commit();
     /*
     // FIXME do we need to refactor all trigger usage into the util function?
     $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
     $aTriggers = $oKTTriggerRegistry->getTriggers('copyDocument', 'postValidate');
     foreach ($aTriggers as $aTrigger) {
         $sTrigger = $aTrigger[0];
         $oTrigger = new $sTrigger;
         $aInfo = array(
             'document' => $new_document,
             'old_folder' => $this->ktapi_folder->get_folder(),
             'new_folder' => $target_folder,
         );
         $oTrigger->setInfo($aInfo);
         $ret = $oTrigger->postValidate();
     }
     */
     return KTAPI_Document::get($this->ktapi, $new_document->getId());
 }
 function performTransition($oDocument, $oUser)
 {
     $iFolderId = KTUtil::arrayGet($this->aConfig, 'folder_id');
     $oToFolder = Folder::get($iFolderId);
     if (PEAR::isError($oFolder)) {
         if ($this->isCopy) {
             return PEAR::raiseError(_kt('The folder to which this document should be copied does not exist.  Cancelling the transition - please contact a system administrator.'));
         } else {
             return PEAR::raiseError(_kt('The folder to which this document should be moved does not exist.  Cancelling the transition - please contact a system administrator.'));
         }
     }
     if ($this->isCopy) {
         return KTDocumentUtil::copy($oDocument, $oToFolder);
     } else {
         return KTDocumentUtil::move($oDocument, $oToFolder, $oUser);
     }
 }
Пример #5
0
 function copy($oSrcFolder, $oDestFolder, $oUser, $sReason, $sDestFolderName = NULL, $copyAll = true)
 {
     $sDestFolderName = empty($sDestFolderName) ? $oSrcFolder->getName() : $sDestFolderName;
     if (KTFolderUtil::exists($oDestFolder, $sDestFolderName)) {
         return PEAR::raiseError(_kt("Folder with the same name already exists in the new parent folder"));
     }
     //
     // FIXME the failure cleanup code here needs some serious work.
     //
     $oPerm = KTPermission::getByName('ktcore.permissions.read');
     $oBaseFolderPerm = KTPermission::getByName('ktcore.permissions.addFolder');
     if (!KTPermissionUtil::userHasPermissionOnItem($oUser, $oBaseFolderPerm, $oDestFolder)) {
         return PEAR::raiseError(_kt('You are not allowed to create folders in the destination.'));
     }
     // Check if the source folder inherits its permissions
     // Get source PO id and its parent PO id
     $iSrcPoId = $oSrcFolder->getPermissionObjectID();
     $oSrcParent = Folder::get($oSrcFolder->getParentID());
     $iSrcParentPoId = $oSrcParent->getPermissionObjectID();
     // If the folder defines its own permissions then we copy the permission object
     // If the source folder inherits permissions we must change it to inherit from the new parent folder
     $bInheritPermissions = false;
     if ($iSrcPoId == $iSrcParentPoId) {
         $bInheritPermissions = true;
     }
     $aFolderIds = array();
     // of oFolder
     $aDocuments = array();
     // of oDocument
     $aFailedDocuments = array();
     // of String
     $aFailedFolders = array();
     // of String
     $aRemainingFolders = array($oSrcFolder->getId());
     DBUtil::startTransaction();
     while (!empty($aRemainingFolders) && $copyAll) {
         $iFolderId = array_pop($aRemainingFolders);
         $oFolder = Folder::get($iFolderId);
         if (PEAR::isError($oFolder) || $oFolder == false) {
             DBUtil::rollback();
             return PEAR::raiseError(sprintf(_kt('Failure resolving child folder with id = %d.'), $iFolderId));
         }
         // don't just stop ... plough on.
         if (KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oFolder)) {
             $aFolderIds[] = $iFolderId;
         } else {
             $aFailedFolders[] = $oFolder->getName();
         }
         // child documents
         $aChildDocs = Document::getList(array('folder_id = ?', array($iFolderId)));
         foreach ($aChildDocs as $oDoc) {
             if (KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oDoc)) {
                 $aDocuments[] = $oDoc;
             } else {
                 $aFailedDocuments[] = $oDoc->getName();
             }
         }
         // child folders.
         $aCFIds = Folder::getList(array('parent_id = ?', array($iFolderId)), array('ids' => true));
         $aRemainingFolders = kt_array_merge($aRemainingFolders, $aCFIds);
     }
     if (!empty($aFailedDocuments) || !empty($aFailedFolders)) {
         $sFD = '';
         $sFF = '';
         if (!empty($aFailedDocuments)) {
             $sFD = _kt('Documents: ') . implode(', ', $aFailedDocuments) . '. ';
         }
         if (!empty($aFailedFolders)) {
             $sFF = _kt('Folders: ') . implode(', ', $aFailedFolders) . '.';
         }
         return PEAR::raiseError(_kt('You do not have permission to copy these items. ') . $sFD . $sFF);
     }
     // first we walk the tree, creating in the new location as we go.
     // essentially this is an "ok" pass.
     $oStorage =& KTStorageManagerUtil::getSingleton();
     $aFolderMap = array();
     $sTable = 'folders';
     $sGetQuery = 'SELECT * FROM ' . $sTable . ' WHERE id = ? ';
     $aParams = array($oSrcFolder->getId());
     $aRow = DBUtil::getOneResult(array($sGetQuery, $aParams));
     unset($aRow['id']);
     $aRow['name'] = $sDestFolderName;
     $aRow['description'] = $sDestFolderName;
     $aRow['parent_id'] = $oDestFolder->getId();
     $aRow['parent_folder_ids'] = sprintf('%s,%s', $oDestFolder->getParentFolderIDs(), $oDestFolder->getId());
     $aRow['full_path'] = $oDestFolder->getFullPath() . '/' . $aRow['name'];
     $id = DBUtil::autoInsert($sTable, $aRow);
     if (PEAR::isError($id)) {
         DBUtil::rollback();
         return $id;
     }
     $sSrcFolderId = $oSrcFolder->getId();
     $aFolderMap[$sSrcFolderId]['parent_id'] = $id;
     $aFolderMap[$sSrcFolderId]['parent_folder_ids'] = $aRow['parent_folder_ids'];
     $aFolderMap[$sSrcFolderId]['full_path'] = $aRow['full_path'];
     $aFolderMap[$sSrcFolderId]['name'] = $aRow['name'];
     $oNewBaseFolder = Folder::get($id);
     $res = $oStorage->createFolder($oNewBaseFolder);
     if (PEAR::isError($res)) {
         // it doesn't exist, so rollback and raise..
         DBUtil::rollback();
         return $res;
     }
     $aRemainingFolders = Folder::getList(array('parent_id = ?', array($oSrcFolder->getId())), array('ids' => true));
     while (!empty($aRemainingFolders) && $copyAll) {
         $iFolderId = array_pop($aRemainingFolders);
         $aParams = array($iFolderId);
         $aRow = DBUtil::getOneResult(array($sGetQuery, $aParams));
         unset($aRow['id']);
         // since we are nested, we will have solved the parent first.
         $sPrevParentId = $aRow['parent_id'];
         $aRow['parent_id'] = $aFolderMap[$aRow['parent_id']]['parent_id'];
         $aRow['parent_folder_ids'] = sprintf('%s,%s', $aFolderMap[$sPrevParentId]['parent_folder_ids'], $aRow['parent_id']);
         $aRow['full_path'] = sprintf('%s/%s', $aFolderMap[$sPrevParentId]['full_path'], $aRow['name']);
         $id = DBUtil::autoInsert($sTable, $aRow);
         if (PEAR::isError($id)) {
             $oStorage->removeFolder($oNewBaseFolder);
             DBUtil::rollback();
             return $id;
         }
         $aFolderMap[$iFolderId]['parent_id'] = $id;
         $aFolderMap[$iFolderId]['parent_folder_ids'] = $aRow['parent_folder_ids'];
         $aFolderMap[$iFolderId]['full_path'] = $aRow['full_path'];
         $aFolderMap[$iFolderId]['name'] = $aRow['name'];
         $oNewFolder = Folder::get($id);
         $res = $oStorage->createFolder($oNewFolder);
         if (PEAR::isError($res)) {
             // first delete, then rollback, then fail out.
             $oStorage->removeFolder($oNewBaseFolder);
             DBUtil::rollback();
             return $res;
         }
         $aCFIds = Folder::getList(array('parent_id = ?', array($iFolderId)), array('ids' => true));
         $aRemainingFolders = kt_array_merge($aRemainingFolders, $aCFIds);
     }
     // now we can go ahead.
     foreach ($aDocuments as $oDocument) {
         $oChildDestinationFolder = Folder::get($aFolderMap[$oDocument->getFolderID()]['parent_id']);
         $res = KTDocumentUtil::copy($oDocument, $oChildDestinationFolder);
         if (PEAR::isError($res) || $res === false) {
             $oStorage->removeFolder($oNewBaseFolder);
             DBUtil::rollback();
             return PEAR::raiseError(_kt('Delete Aborted. Unexpected failure to copydocument: ') . $oDocument->getName() . $res->getMessage());
         }
     }
     $sComment = sprintf(_kt("Folder copied from %s to %s"), $oSrcFolder->getFullPath(), $oDestFolder->getFullPath());
     if ($sReason !== null) {
         $sComment .= sprintf(_kt(" (reason: %s)"), $sReason);
     }
     $oTransaction = KTFolderTransaction::createFromArray(array('folderid' => $oFolder->getId(), 'comment' => $sComment, 'transactionNS' => 'ktcore.transactions.copy', 'userid' => $oUser->getId(), 'ip' => Session::getClientIP()));
     // If the folder inherits its permissions then we set it to inherit from the new parent folder and update permissions
     // If it defines its own then copy the permission object over
     if ($bInheritPermissions) {
         $aOptions = array('evenifnotowner' => true);
         KTPermissionUtil::inheritPermissionObject($oNewBaseFolder, $aOptions);
     } else {
         KTPermissionUtil::copyPermissionObject($oNewBaseFolder);
     }
     // and store
     DBUtil::commit();
     return true;
 }
Пример #6
0
 function perform_action($oEntity)
 {
     if (is_a($oEntity, 'Document')) {
         return KTDocumentUtil::copy($oEntity, $this->oTargetFolder, $this->sReason);
     } else {
         if (is_a($oEntity, 'Folder')) {
             return KTFolderUtil::copy($oEntity, $this->oTargetFolder, $this->oUser, $this->sReason);
         }
     }
 }
Пример #7
0
 function perform_action($oEntity)
 {
     if (is_a($oEntity, 'Document')) {
         $res = KTDocumentUtil::copy($oEntity, $this->oTargetFolder, $this->sReason, null, true);
         if (PEAR::isError($res)) {
             return $res;
         }
     } else {
         if (is_a($oEntity, 'Folder')) {
             $res = KTFolderUtil::copy($oEntity, $this->oTargetFolder, $this->oUser, $this->sReason, null, true);
             if (PEAR::isError($res)) {
                 return $res;
             }
         }
     }
     return 'CopiedDocument';
 }