ktwebdavserver.inc.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,547 行 · 第 1/5 页
PHP
1,547 行
} else {
// properties from namespaces != "DAV:" or without any namespace
$this->ktwebdavLog('Getting != "DAV:" or without any namespace Properties...', 'info', true);
if ($prop["ns"]) {
echo " <" . $ns_hash[$prop["ns"]] . ":$prop[name]>"
. $this->_prop_encode(htmlspecialchars($prop['val']))
. "</" . $ns_hash[$prop["ns"]] . ":$prop[name]>\n";
} else {
echo " <$prop[name] xmlns=\"\">"
. $this->_prop_encode(htmlspecialchars($prop['val']))
. "</$prop[name]>\n";
}
}
}
echo " </D:prop>\n";
echo " <D:status>HTTP/1.1 200 OK</D:status>\n";
echo " </D:propstat>\n";
}
// now report all properties requested but not found
$this->ktwebdavLog('Getting all properties requested but not found...', 'info', true);
if (isset($file["noprops"])) {
echo " <D:propstat>\n";
echo " <D:prop>\n";
foreach($file["noprops"] as $key => $prop) {
if ($prop["ns"] == "DAV:") {
echo " <D:$prop[name]/>\n";
} else if ($prop["ns"] == '') {
echo " <$prop[name] xmlns=\"\"/>\n";
} else {
echo " <" . $ns_hash[$prop["ns"]] . ":$prop[name]/>\n";
}
}
echo " </D:prop>\n";
echo " <D:status>HTTP/1.1 404 Not Found</D:status>\n";
echo " </D:propstat>\n";
}
echo " </D:response>\n";
}
echo "</D:multistatus>\n";
}
/**
* PROPFIND helper for Folder Info
*
* @param int Folder ID
* @param string path
* @return array Folder info array
*/
function _fileinfoForFolderID($iFolderID, $path) {
global $default;
$this->ktwebdavLog("Entering _fileinfoForFolderID. FolderID is " . $iFolderID, 'info', true);
if($iFolderID == '') return false;
$oFolder =& Folder::get($iFolderID);
if (is_null($oFolder) || ($oFolder === false)) {
$this->ktwebdavLog("oFolderID error. ", 'info', true);
return false;
}
return $this->_fileinfoForFolder($oFolder, $path);
}
/**
* GET method handler
*
* @param array parameter passing array
* @return bool true on success
*/
function GET(&$options)
{
// required for KT
global $default;
$this->ktwebdavLog("Entering GET. options are " . print_r($options, true), 'info', true);
// Get the client info
$this->checkSafeMode();
// get path to requested resource
$path = $options["path"];
// Fix for Mac Clients
// Mac adds DS_Store files when folders are added and ._filename files when files are added
// The PUT function doesn't add these files to the dms but PROPFIND still looks for the .DS_Store file,
// and returns an error if not found. We emulate its existence by returning a positive result.
if($this->dav_client == 'MC' || $this->dav_client == 'MG'){
// Remove filename from path
$aPath = explode('/', $path);
$fileName = $aPath[count($aPath)-1];
if(strtolower($fileName) == '.ds_store'){
$this->ktwebdavLog("Using a Mac client. Filename is .DS_Store so we emulate a positive result.", 'info', true);
// ignore
return true;
}
if($fileName[0] == '.' && $fileName[1] == '_'){
$this->ktwebdavLog("Using a Mac client. Filename is ._Filename so we emulate a positive result.", 'info', true);
// ignore
return true;
}
}
list($iFolderID, $iDocumentID) = $this->_folderOrDocument($path);
if ($iDocumentID === false) {
$this->ktwebdavLog("Document not found.", 'info', true);
return "404 Not found - Document not found.";
}
if (is_null($iDocumentID)) {
return $this->_GETFolder($options, $iFolderID);
}
return $this->_GETDocument($options, $iDocumentID);
}
/**
* 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;
}
/**
* GET method helper
*
* @param array parameter passing array
* @param int Document ID
* @return bool true on success
*/
function _GETDocument(&$options, $iDocumentID) {
global $default;
$oDocument =& Document::get($iDocumentID);
// get a temp file, and read. NOTE: NEVER WRITE TO THIS
$oStorage =& KTStorageManagerUtil::getSingleton();
$fspath = $oStorage->temporaryFile($oDocument);
$this->ktwebdavLog("Filesystem Path is " . $fspath, 'info', true );
// detect resource type
$mimetype = KTMime::getMimeTypeName($oDocument->getMimeTypeID());
$options['mimetype'] = KTMime::getFriendlyNameForString($mimetype);
// detect modification time
// see rfc2518, section 13.7
// some clients seem to treat this as a reverse rule
// requiering a Last-Modified header if the getlastmodified header was set
$options['mtime'] = $oDocument->getVersionCreated();
// detect resource size
$options['size'] = $oDocument->getFileSize();
// no need to check result here, it is handled by the base class
$options['stream'] = fopen($fspath, "r");
$this->ktwebdavLog("Method is " . $this->currentMethod, 'info', true );
if ($this->currentMethod == "get") {
// create the document transaction record
include_once(KT_LIB_DIR . '/documentmanagement/DocumentTransaction.inc');
$oDocumentTransaction = & new DocumentTransaction($oDocument, "Document viewed via KTWebDAV", 'ktcore.transactions.view');
$oDocumentTransaction->iUserID = $this->userID;
$oDocumentTransaction->create();
}
return true;
}
/**
* GET method helper
* Method takes a directory path and checks whether it refers to a document or folder. The relevant folder and/or document id is returned.
*
* @param $path string The directory path
* @return array or bool Either returns an array of folder/document id's or false if an error occurred
*/
function _folderOrDocument($path) {
global $default;
$this->ktwebdavLog("Entering _folderOrDocument. path is " . $path, 'info', true);
/* ** Get the directory path and the folder/document being acted on ** */
$sFileName = basename($path);
// for windows replace backslash with forwardslash
$sFolderPath = str_replace("\\", '/', dirname($path) );
/* ** Get the starting point for recursing through the directory structure
FolderId = 0 if we're in the root folder
FolderId = 1 the starting point for locating any other folder ** */
if ($sFolderPath == "/" || $sFolderPath == "/ktwebdav") {
$this->ktwebdavLog("This is the root folder.", 'info', true);
$sFolderPath = $this->rootFolder;
$iFolderID = 0;
} else $iFolderID = 1;
if ($sFileName == "ktwebdav.php") {
$this->ktwebdavLog("This is the root folder file.", 'info', true);
$sFileName = '';
}
$this->ktwebdavLog("sFileName is " . $sFileName, 'info', true);
$this->ktwebdavLog("sFolderName is " . $sFolderPath, 'info', true);
$this->ktwebdavLog("iFolderID is " . $iFolderID, 'info', true);
/* ** Break up the directory path into its component directory's,
recurse through the directory's to find the correct id of the current directory.
Avoids situations where several directory's have the same name. ** */
$aFolderNames = split('/', $sFolderPath);
$this->ktwebdavLog("aFolderNames are: " . print_r($aFolderNames, true), 'info', true);
$aRemaining = $aFolderNames;
while (count($aRemaining)) {
$sFolderName = $aRemaining[0];
$aRemaining = array_slice($aRemaining, 1);
if (empty($sFolderName)) {
continue;
}
// FIXME: Direct database access
if($iFolderID == 0){
$sQuery = "SELECT id FROM folders WHERE parent_id is null AND name = ?";
$aParams = array($sFolderName);
}else{
$sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?";
$aParams = array($iFolderID, $sFolderName);
}
$id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
if (PEAR::isError($id)) {
$this->ktwebdavLog("A DB error occurred in _folderOrDocument", 'info', true);
return false;
}
if (is_null($id)) {
// Some intermediary folder path doesn't exist
$this->ktwebdavLog("Some intermediary folder does not exist in _folderOrDocument", 'error', true);
return array(false, false);
}
$iFolderID = (int)$id;
$this->ktwebdavLog("iFolderID set to " . $iFolderID, 'info', true);
}
/* ** Get the document id using the basename and parent folder id as parameters.
If an id is obtained then the path refers to a document.
If no id is returned then the path refers to a folder or a non-existing document. ** */
// FIXME: Direct database access
// $sQuery = "SELECT id FROM documents WHERE folder_id = ? AND filename = ? AND status_id = 1";
$sQuery = "SELECT D.id ";
$sQuery .= "FROM documents AS D ";
$sQuery .= "LEFT JOIN document_metadata_version AS DM ";
$sQuery .= "ON D.metadata_version_id = DM.id ";
$sQuery .= "LEFT JOIN document_content_version AS DC ";
$sQuery .= "ON DM.content_version_id = DC.id ";
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?