⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 folderutil.inc.php

📁 PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。
💻 PHP
📖 第 1 页 / 共 2 页
字号:
            }
            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();
	        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)";
        }

        foreach($aSymlinks as $aSymlink){
        	KTFolderUtil::deleteSymbolicLink($aSymlink['id']);
        }

        // purge caches
        KTEntityUtil::clearAllCaches('Folder');

        // and store
        DBUtil::commit();

        return true;
    }

    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']);
            //            var_dump($oDocument->getFolderID());
            $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, // Inherit from parent folder, even though not permission owner
                );
            KTPermissionUtil::inheritPermissionObject($oNewBaseFolder, $aOptions);
        }else{
            KTPermissionUtil::copyPermissionObject($oNewBaseFolder);
        }

        // and store
        DBUtil::commit();

        return true;
    }

/**
     * Create a symbolic link in the target folder
     *
     * @param Folder $sourceFolder Folder to create a link to
     * @param Folder $targetFolder Folder to place the link in
     * @param User $user current user
     * @return Folder the link
     */
    static function createSymbolicLink($sourceFolder, $targetFolder, $user = null) // added/
    {
    	//validate input
        if (is_numeric($sourceFolder))
        {
            $sourceFolder = Folder::get($sourceFolder);
        }
        if (!$sourceFolder instanceof Folder)
        {
            return PEAR::raiseError(_kt('Source folder not specified'));
        }
        if (is_numeric($targetFolder))
        {
            $targetFolder = Folder::get($targetFolder);
        }
        if (!$targetFolder instanceof Folder)
        {
            return PEAR::raiseError(_kt('Target folder not specified'));
        }
        if (is_null($user))
        {
            $user = $_SESSION['userID'];
        }
        if (is_numeric($user))
        {
            $user = User::get($user);
        }

        //check for permissions
        $oWritePermission =& KTPermission::getByName("ktcore.permissions.write");
		$oReadPermission =& KTPermission::getByName("ktcore.permissions.read");
		if (!KTBrowseUtil::inAdminMode($user, $targetFolder)) {
            if(!KTPermissionUtil::userHasPermissionOnItem($user, $oWritePermission, $targetFolder)){
        		return PEAR::raiseError(_kt('You\'re not authorized to create shortcuts'));
       		}
        }
        if (!KTBrowseUtil::inAdminMode($user, $sourceFolder)) {
        	if(!KTPermissionUtil::userHasPermissionOnItem($user, $oReadPermission, $sourceFolder)){
        		return PEAR::raiseError(_kt('You\'re not authorized to create a shortcut to this folder'));
       		}
        }

    	//check if the shortcut doesn't already exists in the target folder
        $aSymlinks = $sourceFolder->getSymbolicLinks();
        foreach($aSymlinks as $iSymlink){
        	$oSymlink = Folder::get($iSymlink['id']);
        	if($oSymlink->getParentID() == $targetFolder->getID()){
        		return PEAR::raiseError(_kt('There already is a shortcut to this folder in the target folder.'));
        	}
        }

        //Create the link
        $oSymlink = Folder::createFromArray(array(
            'iParentID' => $targetFolder->getId(),
            'iCreatorID' => $user->getId(),
            'sFullPath' => $targetFolder->getFullPath(),
            'sParentFolderIDs' => $targetFolder->getParentFolderIDs(),
            'iPermissionObjectID' => $targetFolder->getPermissionObjectID(),
            'iPermissionLookupID' => $targetFolder->getPermissionLookupID(),
        	'iLinkedFolderId' => $sourceFolder->getId(),
        ));
        return $oSymlink;
    }

    /**
     * Deletes a symbolic link folder
     *
     * @param Folder $folder tthe symbolic link folder to delete
     * @param User $user the current user
     * @return unknown
     */
    static function deleteSymbolicLink($folder, $user = null) // added/
    {
    	//validate input
        if (is_numeric($folder))
        {
            $folder = Folder::get($folder);
        }
        if (!$folder instanceof Folder)
        {
            return PEAR::raiseError(_kt('Folder not specified'));
        }
        if (!$folder->isSymbolicLink())
        {
            return PEAR::raiseError(_kt('Folder must be a symbolic link entity'));
        }
        if (is_null($user))
        {
            $user = $_SESSION['userID'];
        }
        if (is_numeric($user))
        {
            $user = User::get($user);
        }

        //check if the user has sufficient permissions
		$oPerm = KTPermission::getByName('ktcore.permissions.delete');
    	if (!KTBrowseUtil::inAdminMode($user, $folder)) {
            if(!KTPermissionUtil::userHasPermissionOnItem($user, $oPerm, $folder)){
        		return PEAR::raiseError(_kt('You\'re not authorized to delete shortcuts'));
       		}
        }

        // we only need to delete the folder entry for the link
        $sql = "DELETE FROM folders WHERE id=?";
        DBUtil::runQuery(array($sql, array($folder->getId())));

    }


}

?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -