/**
  * MOVE method helper for Documents
  *
  * @param  array  parameter passing array
  * @param  int    Folder ID
  * @param  int    Document ID
  * @return string  HTTP status code or false
  */
 function _MOVEDocument($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 _MOVEDocument. options are " . print_r($options, true), 'info', true);
     // Fix for Mac Goliath
     // Modified - 25/10/07 - remove ktwebdav from document path
     if ($this->dav_client == 'MG' || $this->dav_client == 'MS') {
         $this->ktwebdavLog("Remove ktwebdav from destination path: " . $options['dest'], 'info', true);
         if (!(strpos($options['dest'], 'ktwebdav/ktwebdav.php/') === FALSE)) {
             $options['dest'] = substr($options['dest'], 22);
         }
         if ($options['dest'][0] != '/') {
             $options['dest'] = '/' . $options['dest'];
         }
     }
     global $default;
     $new = 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. ** */
     $oDocument = Document::get($iDocumentID);
     $oSrcFolder = Folder::get($iFolderID);
     $oUser =& User::get($this->userID);
     $source_path = $options["path"];
     $dest_path = urldecode($options["dest"]);
     /* ** Get the source folder object.
        If the destination document is null, then the destination is a folder, 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. ** */
     list($iDestFolder, $iDestDoc) = $this->_folderOrDocument($dest_path);
     if (is_null($iDestDoc)) {
         // the dest is a folder
         $this->ktwebdavLog("Destination is a folder.", 'info', true);
     } 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 move overwrites target.');
             $new = false;
         }
     }
     /* ** Check if the source and destination directories are the same and the destination is not a folder.
        Then action is probably a rename.
        Check if user has permission to write to the document and folder.
        Rename the document. ** */
     if (dirname($source_path) == dirname($dest_path) && !is_null($iDestDoc)) {
         // This is a rename
         $this->ktwebdavLog("This is a rename.", 'info', true);
         $this->ktwebdavLog("Got an oDocument of " . print_r($oDocument, true), 'info', true);
         $this->ktwebdavLog("Got a new name of " . basename($dest_path), 'info', true);
         // Check if the user has permissions to write this document
         $oPerm =& KTPermission::getByName('ktcore.permissions.write');
         $oUser =& User::get($this->userID);
         if (!KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oDocument)) {
             return "403 Forbidden - User does not have sufficient permissions";
         }
         // Perform rename
         $res = KTDocumentUtil::rename($oDocument, basename($dest_path), $oUser);
         if (PEAR::isError($res) || is_null($res) || $res === false) {
             return "404 Not Found - " . $res->getMessage();
         } else {
             if ($new) {
                 $this->ktwebdavLog("201 Created", 'info', true);
                 return "201 Created";
             } else {
                 $this->ktwebdavLog("204 No Content", 'info', true);
                 return "204 No Content";
             }
         }
     }
     /* ** Get the destination folder object and the source document object.
        Check if user has permission to write to the document and folder.
        Move the document. ** */
     $oDestFolder = Folder::get($iDestFolder);
     $this->ktwebdavLog("Got a destination folder of " . 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 Move.";
     $res = KTDocumentUtil::move($oDocument, $oDestFolder, $oUser, $reason);
     if (PEAR::isError($res)) {
         $this->ktwebdavLog("Move on document failed: " . $res->getMessage(), 'info', true);
         return "500 Internal Server Error - Move 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";
     }
 }
Esempio n. 2
0
 function do_move()
 {
     $oForm = $this->form_move();
     $res = $oForm->validate();
     $errors = $res['errors'];
     $data = $res['results'];
     $sReason = $data['reason'];
     $extra_errors = array();
     if (!is_null($data['browse'])) {
         if ($data['browse']->getId() == $this->oDocument->getFolderID()) {
             $extra_errors['browse'] = _kt('You cannot move the document within the same folder.');
         } else {
             $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);
     }
     $this->startTransaction();
     // now try update it.
     $res = KTDocumentUtil::move($this->oDocument, $data['browse'], $this->oUser, $sReason);
     if (PEAR::isError($oNewDoc)) {
         $this->errorRedirectTo('main', _kt('Failed to move document: ') . $oNewDoc->getMessage());
         exit(0);
     }
     $this->oDocument->setName($name);
     // if needed.
     $this->oDocument->setFilename($filename);
     // if needed.
     $res = $this->oDocument->update();
     if (PEAR::isError($res)) {
         return $this->errorRedirectTo('main', _kt('Failed to move document: ') . $res->getMessage());
     }
     $this->commitTransaction();
     controllerRedirect('viewDocument', 'fDocumentId=' . $this->oDocument->getId());
     exit(0);
 }
 /**
  * Moves 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");
  * $document->move($newFolder, 'Reason for moving the document');
  * </code>
  *
  * @author KnowledgeTree Team
  * @access public
  * @param KTAPI_Folder $ktapi_target_folder The folder object where the document is being moved into
  * @param string $reason The reason for the move
  * @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 void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
  */
 function move(&$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->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_DOCUMENT_MOVE);
     if (PEAR::isError($user)) {
         return $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;
     }
     if (!KTDocumentUtil::canBeMoved($this->document)) {
         return new PEAR_Error('Document cannot be moved.');
     }
     $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) {
         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 moved 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) {
         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 moved document.');
     }
     DBUtil::startTransaction();
     $res = KTDocumentUtil::move($this->document, $target_folder, $user, $reason);
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
     }
     $this->document->setName($name);
     $this->document->setFilename($filename);
     $res = $this->document->update();
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
     }
     DBUtil::commit();
 }
 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);
     }
 }
Esempio n. 5
0
 function perform_action($oEntity)
 {
     if (is_a($oEntity, 'Document')) {
         return KTDocumentUtil::move($oEntity, $this->oTargetFolder, $this->oUser, $this->sReason);
     } else {
         if (is_a($oEntity, 'Folder')) {
             return KTFolderUtil::move($oEntity, $this->oTargetFolder, $this->oUser, $this->sReason);
         }
     }
 }
Esempio n. 6
0
 function perform_action($oEntity)
 {
     if (is_a($oEntity, 'Document')) {
         $res = KTDocumentUtil::move($oEntity, $this->oTargetFolder, $this->oUser, $this->sReason, true);
     } else {
         if (is_a($oEntity, 'Folder')) {
             $res = KTFolderUtil::move($oEntity, $this->oTargetFolder, $this->oUser, $this->sReason, true);
         }
     }
     if (PEAR::isError($res)) {
         return $res;
     }
     return 'MovedDocument';
 }