/**
  * 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";
     }
 }
 function do_delete()
 {
     $oForm = $this->form_main();
     $res = $oForm->validate();
     $data = $res['results'];
     if (!empty($res['errors'])) {
         return $oForm->handleError();
     }
     $sReason = $data['reason'];
     $fFolderId = $this->oDocument->getFolderId();
     $res = KTDocumentUtil::delete($this->oDocument, $sReason);
     if (PEAR::isError($res)) {
         $this->errorRedirectToMain(sprintf(_kt('Unexpected failure deleting document: %s'), $res->getMessage()));
     }
     $_SESSION['KTInfoMessage'][] = sprintf(_kt('Document "%s" Deleted.'), $this->oDocument->getName());
     controllerRedirect('browse', 'fFolderId=' . $fFolderId);
     exit(0);
 }
 /**
  * Deletes a document from the folder.
  *
  * <code>
  * $ktapi = new KTAPI();
  * $session = $ktapi->start_system_session();
  * $document = $ktapi->get_document_by_id($documentid);
  * $document->delete('Reason for deletion');
  * </code>
  *
  * @author KnowledgeTree Team
  * @access public
  * @param string $reason The reason for deleting the document
  * @return void|PEAR_Error Returns nothing on success | a PEAR_Error on failure
  */
 function delete($reason)
 {
     $user = $this->can_user_access_object_requiring_permission($this->document, KTAPI_PERMISSION_DELETE);
     if (PEAR::isError($user)) {
         return $user;
     }
     if ($this->document->getIsCheckedOut()) {
         return new PEAR_Error(KTAPI_ERROR_DOCUMENT_CHECKED_OUT);
     }
     DBUtil::startTransaction();
     $res = KTDocumentUtil::delete($this->document, $reason);
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return new KTAPI_Error(KTAPI_ERROR_INTERNAL_ERROR, $res);
     }
     DBUtil::commit();
 }
 function delete($oStartFolder, $oUser, $sReason, $aOptions = null, $bulk_action = false)
 {
     require_once KT_LIB_DIR . '/unitmanagement/Unit.inc';
     $oPerm = KTPermission::getByName('ktcore.permissions.delete');
     $bIgnorePermissions = KTUtil::arrayGet($aOptions, 'ignore_permissions');
     $aFolderIds = array();
     // of oFolder
     $aDocuments = array();
     // of oDocument
     $aFailedDocuments = array();
     // of String
     $aFailedFolders = array();
     // of String
     $aRemainingFolders = array($oStartFolder->getId());
     DBUtil::startTransaction();
     while (!empty($aRemainingFolders)) {
         $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));
         }
         $oUnit = Unit::getByFolder($oFolder);
         if (!empty($oUnit)) {
             DBUtil::rollback();
             return PEAR::raiseError(sprintf(_kt('Cannot remove unit folder: %s.'), $oFolder->getName()));
         }
         // don't just stop ... plough on.
         if (!$bIgnorePermissions && !KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oFolder)) {
             $aFailedFolders[] = $oFolder->getName();
         } else {
             $aFolderIds[] = $iFolderId;
         }
         // child documents
         $aChildDocs = Document::getList(array('folder_id = ?', array($iFolderId)));
         foreach ($aChildDocs as $oDoc) {
             if (!$bIgnorePermissions && $oDoc->getImmutable()) {
                 if (!KTBrowseUtil::inAdminMode($oUser, $oStartFolder)) {
                     $aFailedDocuments[] = $oDoc->getName();
                     continue;
                 }
             }
             if ($bIgnorePermissions || KTPermissionUtil::userHasPermissionOnItem($oUser, $oPerm, $oDoc) && $oDoc->getIsCheckedOut() == false) {
                 $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);
     }
     // FIXME we could subdivide this to provide a per-item display (viz. bulk upload, etc.)
     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 delete these items. ') . $sFD . $sFF);
     }
     // now we can go ahead.
     foreach ($aDocuments as $oDocument) {
         $res = KTDocumentUtil::delete($oDocument, $sReason);
         if (PEAR::isError($res)) {
             DBUtil::rollback();
             return PEAR::raiseError(_kt('Delete Aborted. Unexpected failure to delete document: ') . $oDocument->getName() . $res->getMessage());
         }
     }
     $oStorage =& KTStorageManagerUtil::getSingleton();
     $oStorage->removeFolderTree($oStartFolder);
     // Check for symbolic links to the folder and its sub folders
     $aSymlinks = array();
     foreach ($aFolderIds as $iFolder) {
         $oFolder = Folder::get($iFolder);
         $aLinks = $oFolder->getSymbolicLinks();
         $aSymlinks = array_merge($aSymlinks, $aLinks);
     }
     // documents all cleared.
     $sQuery = 'DELETE FROM ' . KTUtil::getTableName('folders') . ' WHERE id IN (' . DBUtil::paramArray($aFolderIds) . ')';
     $aParams = $aFolderIds;
     $res = DBUtil::runQuery(array($sQuery, $aParams));
     if (PEAR::isError($res)) {
         DBUtil::rollback();
         return PEAR::raiseError(_kt('Failure deleting folders.'));
     }
     // now that the folder has been deleted we delete all the shortcuts
     if (!empty($aSymlinks)) {
         $links = array();
         foreach ($aSymlinks as $link) {
             $links[] = $link['id'];
         }
         $linkIds = implode(',', $links);
         $query = "DELETE FROM folders WHERE id IN ({$linkIds})";
         DBUtil::runQuery($query);
     }
     /*
     foreach($aSymlinks as $aSymlink){
     	KTFolderUtil::deleteSymbolicLink($aSymlink['id']);
     }
     */
     // purge caches
     KTEntityUtil::clearAllCaches('Folder');
     // and store
     DBUtil::commit();
     return true;
 }
 function perform_action($oEntity)
 {
     $sReason = $this->res['reason'];
     if (is_a($oEntity, 'Document')) {
         $res = KTDocumentUtil::delete($oEntity, $sReason);
     } else {
         if (is_a($oEntity, 'Folder')) {
             $res = KTFolderUtil::delete($oEntity, $this->oUser, $sReason);
         }
     }
     return $res;
 }
 function perform_action($oEntity)
 {
     $sReason = $this->res['reason'];
     if (is_a($oEntity, 'Document')) {
         $res = KTDocumentUtil::delete($oEntity, $sReason, null, true);
         if (PEAR::isError($res)) {
             return $res;
         }
         return "RemoveChildDocument";
     } else {
         if (is_a($oEntity, 'Folder')) {
             $res = KTFolderUtil::delete($oEntity, $this->oUser, $sReason, null, true);
             if (PEAR::isError($res)) {
                 return $res;
             }
             return "RemoveChildFolder";
         }
     }
 }