function _importfolder($oFolder, $sPath) { $oPermission = KTPermission::getByName('ktcore.permissions.addFolder'); $aDocPaths = $this->oStorage->listDocuments($sPath); if (PEAR::isError($aDocPaths)) { return $aDocPaths; } $oDocObjects = array(); foreach ($aDocPaths as $sDocumentPath) { $res = $this->_importdocument($oFolder, $sDocumentPath); if (PEAR::isError($res)) { return $res; } // Store document object $this->uploadedDocs[] = $res; } $aFolderPaths = $this->oStorage->listFolders($sPath); if (PEAR::isError($aFolderPaths)) { return $aFolderPaths; } $oFolderObjects = array(); foreach ($aFolderPaths as $sFolderPath) { $sFolderBasePath = basename($sFolderPath); $sFolderBasePath = $this->is_utf8($sFolderBasePath) ? $sFolderBasePath : utf8_encode($sFolderBasePath); if (Folder::folderExistsName($sFolderPath, KTUtil::getId($oFolder))) { $_SESSION['KTErrorMessage'][] = sprintf(_kt("The folder %s is already present in %s. Adding files into pre-existing folder."), $sFolderBasePath, $oFolder->getName()); $aOptions = Folder::getList("parent_id = " . KTUtil::getId($oFolder) . ' AND name = "' . DBUtil::escapeSimple($sFolderBasePath) . '"'); if (PEAR::isError($aOptions)) { return $aOptions; } if (count($aOptions) != 1) { return PEAR::raiseError(sprintf(_kt("Two folders named %s present in %s. Unable to decide which to use..."), $sFolderName, $oFolder->getName())); } else { $oThisFolder = $aOptions[0]; } } else { if (KTPermissionUtil::userHasPermissionOnItem($this->oUser, $oPermission, $oFolder)) { $oThisFolder = KTFolderUtil::add($oFolder, $sFolderBasePath, $this->oUser, true); } else { $oThisFolder = $oFolder; if (!in_array('Your documents have been added to this folder and not the folder structure within the upload file because you do not have permission to add any folders.', $_SESSION['KTErrorMessage'])) { $_SESSION['KTErrorMessage'][] = sprintf(_kt('Your documents have been added to this folder and not the folder structure within the upload file because you do not have permission to add any folders.')); } } } if (PEAR::isError($oThisFolder)) { return $oThisFolder; } $res = $this->_importfolder($oThisFolder, $sFolderPath); if (PEAR::isError($res)) { return $res; } // Store folder object $this->uploadedFolders[] = $res; } }
/** * GET method helper * * @param array parameter passing array * @param int MainFolder ID * @return bool true on success */ function _GETFolder(&$options, $iMainFolderID) { global $default; $this->ktwebdavLog("Entering _GETFolder. options are " . print_r($options, true), 'info', true); $oMainFolder =& Folder::get($iMainFolderID); $aFolderID = array(); $aChildren = Folder::getList(array('parent_id = ?', $iMainFolderID)); // $sFolderName = $oMainFolder->getName(); if (is_writeable("../var") && is_writeable("../var/log")) { $writeperms = "<font color=\"green\"><b>OK</b></font>"; } else { $writeperms = "<font color=\"red\"><b>NOT SET</b></font>"; } if ($this->ktdmsPath != '') { $ktdir = $this->ktdmsPath; } $srv_proto = split('/', $_SERVER['SERVER_PROTOCOL']); $proto = strtolower($srv_proto[0]); // check if ssl enabled if ($proto == 'http' && $default->sslEnabled) { $proto = 'https'; } $dataSafe = ''; if ($this->safeMode != 'off') { $dataSafe = "<div style=\"color: orange;\" align=\"center\">NOTE: Safe mode is currently enabled, only viewing and downloading of documents will be allowed.</div><br><br>"; } $data = "<html><head><title>KTWebDAV - The KnowledgeTree WebDAV Server</title></head>"; $data .= "<body>"; $data .= "<div align=\"center\"><IMG src=\"../resources/graphics/ktlogo-topbar_base.png\" width=\"308\" height=\"61\" border=\"0\"></div><br>"; $data .= "<div align=\"center\"><h2><strong>Welcome to KnowledgeTree WebDAV Server</strong></h2></div><br><br>"; $data .= "<div align=\"center\">To access KTWebDAV copy the following URL and paste it into your WebDAV enabled client...</div><br><br>"; $data .= $dataSafe; $data .= "<div align=\"center\"><strong>" . $proto . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . "</strong></div>"; $data .= "</body>"; $options['mimetype'] = 'text/html'; $options["data"] = $data; 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']); $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; }
/** * Updates permission lookups for this folder and any ancestors, but * only if they use the same permission object. * * To be used any time a folder permission object is changed. */ function updatePermissionLookupRecursive(&$oDocumentOrFolder) { if (is_a($oDocumentOrFolder, 'Document')) { // XXX: metadata versions may need attention here KTPermissionUtil::updatePermissionLookup($oDocumentOrFolder); return; } $iFolderID = $oDocumentOrFolder->getID(); $sFolderIDs = Folder::generateFolderIDs($iFolderID); $sFolderIDs .= '%'; $sWhere = 'permission_object_id = ? AND parent_folder_ids LIKE ?'; $aParams = array($oDocumentOrFolder->getPermissionObjectID(), $sFolderIDs); $aFolders =& Folder::getList(array($sWhere, $aParams)); foreach ($aFolders as $oFolder) { KTPermissionUtil::updatePermissionLookup($oFolder); } $aDocuments =& Document::getList(array($sWhere, $aParams)); foreach ($aDocuments as $oDocument) { KTPermissionUtil::updatePermissionLookup($oDocument); } }
} } } checkDirectory(''); print "\n"; print "Would remove these folders (and all their contents):\n"; foreach ($aFoldersToRemove as $path) { print "\t{$path}\n"; } print "\n"; print "Would remove these files:\n"; foreach ($aFilesToRemove as $path) { print "\t{$path}\n"; } print "\n"; $aFolders =& Folder::getList(); foreach ($aFolders as $oFolder) { checkRepoFolder($oFolder); } print "These folders are not on the filesystem:\n"; foreach ($aRepoFolderProblems as $path) { print "\t{$path}\n"; } $aDocuments =& Document::getList(array('status_id = ?', array(LIVE))); foreach ($aDocuments as $oDocument) { checkRepoDocument($oDocument); } print "\n"; print "These documents are not on the filesystem:\n"; foreach ($aRepoDocumentProblems as $path) { print "\t{$path}\n";
/** * Get's a folder listing, recursing to the maximum depth. * Derived from the get_listing function. * * <code> * $root = $this->ktapi->get_root_folder(); * $listing = $root->get_full_listing(); * foreach($listing as $val) { * if($val['item_type'] == 'F') { * // It's a folder * echo $val['title']; * } * } * </code> * * @author KnowledgeTree Team * @access public * @param string $what * @return array */ function get_full_listing($what = 'DFS') { $what = strtoupper($what); $read_permission =& KTPermission::getByName(KTAPI_PERMISSION_READ); $folder_permission =& KTPermission::getByName(KTAPI_PERMISSION_VIEW_FOLDER); $config = KTConfig::getSingleton(); $wsversion = $config->get('webservice/version', LATEST_WEBSERVICE_VERSION); $user = $this->ktapi->get_user(); $contents = array(); if (strpos($what, 'F') !== false) { $folder_children = Folder::getList(array('parent_id = ?', $this->folderid)); foreach ($folder_children as $folder) { if (KTPermissionUtil::userHasPermissionOnItem($user, $folder_permission, $folder) || KTPermissionUtil::userHasPermissionOnItem($user, $read_permission, $folder)) { $sub_folder =& $this->ktapi->get_folder_by_id($folder->getId()); if (!PEAR::isError($sub_folder)) { $items = $sub_folder->get_full_listing($what); } else { $items = array(); } $creator = $this->_resolve_user($folder->getCreatorID()); if ($wsversion >= 2) { $array = array('id' => (int) $folder->getId(), 'item_type' => 'F', 'custom_document_no' => 'n/a', 'oem_document_no' => 'n/a', 'title' => $folder->getName(), 'document_type' => 'n/a', 'filename' => $folder->getName(), 'filesize' => 'n/a', 'created_by' => is_null($creator) ? 'n/a' : $creator->getName(), 'created_date' => 'n/a', 'checked_out_by' => 'n/a', 'checked_out_date' => 'n/a', 'modified_by' => 'n/a', 'modified_date' => 'n/a', 'owned_by' => 'n/a', 'version' => 'n/a', 'is_immutable' => 'n/a', 'permissions' => KTAPI_Folder::get_permission_string($folder), 'workflow' => 'n/a', 'workflow_state' => 'n/a', 'mime_type' => 'folder', 'mime_icon_path' => 'folder', 'mime_display' => 'Folder', 'storage_path' => 'n/a'); if ($wsversion >= 3) { $array['linked_folder_id'] = $folder->getLinkedFolderId(); if ($folder->isSymbolicLink()) { $array['item_type'] = "S"; } } $array['items'] = $items; if ($wsversion < 3 || strpos($what, 'F') !== false && !$folder->isSymbolicLink() || $folder->isSymbolicLink() && strpos($what, 'S') !== false) { $contents[] = $array; } } else { $contents[] = array('id' => (int) $folder->getId(), 'item_type' => 'F', 'title' => $folder->getName(), 'creator' => is_null($creator) ? 'n/a' : $creator->getName(), 'checkedoutby' => 'n/a', 'modifiedby' => 'n/a', 'filename' => $folder->getName(), 'size' => 'n/a', 'major_version' => 'n/a', 'minor_version' => 'n/a', 'storage_path' => 'n/a', 'mime_type' => 'folder', 'mime_icon_path' => 'folder', 'mime_display' => 'Folder', 'items' => $items, 'workflow' => 'n/a', 'workflow_state' => 'n/a'); } } } } if (strpos($what, 'D') !== false) { $document_children = Document::getList(array('folder_id = ? AND status_id = 1', $this->folderid)); // I hate that KT doesn't cache things nicely... $mime_cache = array(); foreach ($document_children as $document) { if (KTPermissionUtil::userHasPermissionOnItem($user, $read_permission, $document)) { $created_by = $this->_resolve_user($document->getCreatorID()); $created_date = $document->getCreatedDateTime(); if (empty($created_date)) { $created_date = 'n/a'; } $checked_out_by = $this->_resolve_user($document->getCheckedOutUserID()); $checked_out_date = $document->getCheckedOutDate(); if (empty($checked_out_date)) { $checked_out_date = 'n/a'; } $modified_by = $this->_resolve_user($document->getCreatorID()); $modified_date = $document->getLastModifiedDate(); if (empty($modified_date)) { $modified_date = 'n/a'; } $owned_by = $this->_resolve_user($document->getOwnerID()); $mimetypeid = $document->getMimeTypeID(); if (!array_key_exists($mimetypeid, $mime_cache)) { $type = KTMime::getMimeTypeName($mimetypeid); $icon = KTMime::getIconPath($mimetypeid); $display = KTMime::getFriendlyNameForString($type); $mime_cache[$mimetypeid] = array('type' => $type, 'icon' => $icon, 'display' => $display); } $mimeinfo = $mime_cache[$mimetypeid]; $workflow = 'n/a'; $state = 'n/a'; $wf = KTWorkflowUtil::getWorkflowForDocument($document); if (!is_null($wf) && !PEAR::isError($wf)) { $workflow = $wf->getHumanName(); $ws = KTWorkflowUtil::getWorkflowStateForDocument($document); if (!is_null($ws) && !PEAR::isError($ws)) { $state = $ws->getHumanName(); } } if ($wsversion >= 2) { $docTypeId = $document->getDocumentTypeID(); $documentType = DocumentType::get($docTypeId); $oemDocumentNo = $document->getOemNo(); if (empty($oemDocumentNo)) { $oemDocumentNo = 'n/a'; } $array = array('id' => (int) $document->getId(), 'item_type' => 'D', 'custom_document_no' => 'n/a', 'oem_document_no' => $oemDocumentNo, 'title' => $document->getName(), 'document_type' => $documentType->getName(), 'filename' => $document->getFileName(), 'filesize' => $document->getFileSize(), 'created_by' => is_null($created_by) ? 'n/a' : $created_by->getName(), 'created_date' => $created_date, 'checked_out_by' => is_null($checked_out_by) ? 'n/a' : $checked_out_by->getName(), 'checked_out_date' => $checked_out_date, 'modified_by' => is_null($modified_by) ? 'n/a' : $modified_by->getName(), 'modified_date' => $modified_date, 'owned_by' => is_null($owned_by) ? 'n/a' : $owned_by->getName(), 'version' => $document->getMajorVersionNumber() . '.' . $document->getMinorVersionNumber(), 'content_id' => $document->getContentVersionId(), 'is_immutable' => $document->getImmutable() ? 'true' : 'false', 'permissions' => KTAPI_Document::get_permission_string($document), 'workflow' => $workflow, 'workflow_state' => $state, 'mime_type' => $mime_cache[$mimetypeid]['type'], 'mime_icon_path' => $mime_cache[$mimetypeid]['icon'], 'mime_display' => $mime_cache[$mimetypeid]['display'], 'storage_path' => $document->getStoragePath()); if ($wsversion >= 3) { $document->switchToRealCore(); $array['linked_document_id'] = $document->getLinkedDocumentId(); $document->switchToLinkedCore(); if ($document->isSymbolicLink()) { $array['item_type'] = "S"; } } $array['items'] = array(); if ($wsversion < 3 || strpos($what, 'D') !== false && !$document->isSymbolicLink() || $document->isSymbolicLink() && strpos($what, 'S') !== false) { $contents[] = $array; } } else { $contents[] = array('id' => (int) $document->getId(), 'item_type' => 'D', 'title' => $document->getName(), 'creator' => is_null($created_by) ? 'n/a' : $created_by->getName(), 'checkedoutby' => is_null($checked_out_by) ? 'n/a' : $checked_out_by->getName(), 'modifiedby' => is_null($modified_by) ? 'n/a' : $modified_by->getName(), 'filename' => $document->getFileName(), 'size' => $document->getFileSize(), 'major_version' => $document->getMajorVersionNumber(), 'minor_version' => $document->getMinorVersionNumber(), 'storage_path' => $document->getStoragePath(), 'mime_type' => $mime_cache[$mimetypeid]['type'], 'mime_icon_path' => $mime_cache[$mimetypeid]['icon'], 'mime_display' => $mime_cache[$mimetypeid]['display'], 'items' => array(), 'workflow' => $workflow, 'workflow_state' => $state); } } } } return $contents; }
/** * Checks if there are symlinks that are linking to items in the current list * Useful if you want to prompt the user with a confirmation because they're * automatically deleted when their targets are deleted or archived. * * @return boolean */ function symlinksLinkingToCurrentList() { $symlinksPresent = false; foreach ($this->oActiveEntityList->getDocumentIds() as $iDocument) { $oDocument = Document::get($iDocument); if (count($oDocument->getSymbolicLinks()) > 0) { $symlinksPresent = true; break; } } if ($symlinksPresent == false) { foreach ($this->oActiveEntityList->getFolderIds() as $iFolder) { $oStartFolder = Folder::get($iFolder); $aRemainingFolders = array($oStartFolder->getId()); while (!empty($aRemainingFolders)) { $iFolderId = array_pop($aRemainingFolders); $oFolder = Folder::get($iFolderId); if (count($oFolder->getSymbolicLinks()) > 0) { $symlinksPresent = true; break; } $aChildDocs = Document::getList(array('folder_id = ?', array($iFolderId))); foreach ($aChildDocs as $oDoc) { if (count($oDoc->getSymbolicLinks()) > 0) { $symlinksPresent = true; break; } } $aCFIds = Folder::getList(array('parent_id = ?', array($iFolderId)), array('ids' => true)); $aRemainingFolders = kt_array_merge($aRemainingFolders, $aCFIds); } } } return $symlinksPresent; }