ktwebdavserver.inc.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,547 行 · 第 1/5 页

PHP
1,547
字号
<?php

/**
 * $Id: KTWebDAVServer.inc.php 8387 2008-04-22 16:36:04Z kevin_fourie $
 *
 * KnowledgeTree Community Edition
 * Document Management Made Simple
 * Copyright (C) 2008 KnowledgeTree Inc.
 * Portions copyright The Jam Warehouse Software (Pty) Limited
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, 
 * California 94120-7775, or email info@knowledgetree.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the 
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original 
 * copyright notice.
 * Contributor( s): ______________________________________
 *
 */

require_once 'HTTP/WebDAV/Server.php'; // thirdparty PEAR
require_once 'Config.php';             // thirdparty PEAR
require_once 'Log.php';                // thirdparty PEAR

$userAgentValue = $_SERVER['HTTP_USER_AGENT'];
if (stristr($userAgentValue, "Microsoft Data Access Internet Publishing Provider DAV")) {
    // Fix for Novell Netdrive
    chdir(realpath(dirname(__FILE__)));
    require_once '../../config/dmsDefaults.php'; // This is our plug into KT.
}else{
    require_once '../config/dmsDefaults.php'; // This is our plug into KT.
}

DEFINE('STATUS_WEBDAV', 5);  // Status code to handle 0 byte PUT    FIXME: Do we still need this!

/**
 * KnowledgeTree access using WebDAV protocol
 *
 * @access public
 */
class KTWebDAVServer extends HTTP_WebDAV_Server
{
    /**
     * String to be used in "X-Dav-Powered-By" header
     *
     * @var string
     */
    var $dav_powered_by = 'KTWebDAV (1.0.0)';

    /**
     * Realm string to be used in authentication
     *
     * @var string
     */
    var $http_auth_realm = 'KTWebDAV Server';

    /**
     * Path to KT install root
     *
     * @var string
     */
    var $ktdmsPath = '';

    /**
     * Debug Info Toggle
     *
     * @var string
     */
    var $debugInfo = 'off';

    /**
     * Safe Mode Toggle
     *
     * @var string
     */
    var $safeMode = 'on';

    /**
     * Configuration Array
     *
     * @var array
     */
    var $config = array();

    /**
     * Settings Section Configuration Array
     *
     * @var array
     */
    var $settings = array();

    /**
     * Current User ID
     *
     * @var int
     */
    var $userID;

    /**
     * Current Method
     *
     * @var string
     */
    var $currentMethod;

    /**
     * Last Created Folder ID
     *
     * @var string
     */
    var $lastFolderID;

    /**
     * DAV Client
     *
     * @var String
     */
    var $dav_client;

    /**
     * Root Folder Name
     *
     * @var String
     */
    var $rootFolder = 'Root Folder';

    /**
     * Last Message
     *
     * @var String
     */
    var $lastMsg = '';

    /**
     * Constructor
     *
     * @param void
     * @return void
     */
    function KTWebDAVServer() {
        // CGI compatible auth setup
        $altinfo = KTUtil::arrayGet( $_SERVER, 'kt_auth', KTUtil::arrayGet( $_SERVER, 'REDIRECT_kt_auth'));
        if ( !empty( $altinfo) && !isset( $_SERVER['PHP_AUTH_USER'])) {
            $val = $altinfo;
            $pieces = explode( ' ', $val);   // bad.
            if ( $pieces[0] == 'Basic') {
                $chunk = $pieces[1];
                $decoded = base64_decode( $chunk);
                $credential_info = explode( ':', $decoded);
                if ( count( $credential_info) == 2) {
                    $_SERVER['PHP_AUTH_USER'] = $credential_info[0];
                    $_SERVER['PHP_AUTH_PW'] = $credential_info[1];
                    $_SERVER["AUTH_TYPE"] = 'Basic';
                }
            }
        }

        // Let the base class do it's thing
        parent::HTTP_WebDAV_Server();

        // Load KTWebDAV config
        if (!$this->initConfig()) {
            $this->ktwebdavLog('Could not load configuration.', 'error');
            exit(0);
        }

        if ($this->debugInfo == 'on') {

            $this->ktwebdavLog('=====================');
            $this->ktwebdavLog('  Debug Info is : ' . $this->debugInfo);
            $this->ktwebdavLog('    SafeMode is : ' . $this->safeMode);
            $this->ktwebdavLog(' Root Folder is : ' . $this->rootFolder);
            $this->ktwebdavLog('=====================');
        }

    }

    /**
     * Load KTWebDAV configuration from conf file
     *
     * @param void
     * @return bool	true on success
     */
    function initConfig() {

        global $default;
        $oConfig =& KTConfig::getSingleton();

        // Assign Content
        $this->debugInfo = $oConfig->get('KTWebDAVSettings/debug', 'off');
        $this->safeMode = $oConfig->get('KTWebDAVSettings/safemode', 'on');
        $this->rootFolder = $oConfig->get('KTWebDAVSettings/rootfolder', 'Root Folder');
        $this->kt_version = $default->systemVersion;

        return true;
    }

    /**
     * Log to the KTWebDAV logfile
     *
     * @todo Add other log levels for warning, profile, etc
     * @param string    log message
     * @param bool    debug only?
     * @return bool	true on success
     */
    function ktwebdavLog($entry, $type = 'info', $debug_only = false) {

        if ($debug_only && $this->debugInfo != 'on') return false;

        $ident = 'KTWEBDAV';
        $conf = array('mode' => 0644, 'timeFormat' => '%X %x');
        $logger = &Log::singleton('file', '../var/log/ktwebdav-' . date('Y-m-d') . '.txt', $ident, $conf);
        if ($type == 'error') $logger->log($entry, PEAR_LOG_ERR);
        else $logger->log($entry, PEAR_LOG_INFO);
        return true;
    }

    /**
     * Get the current UserID
     *
     * @access private
     * @param  void
     * @return int userID
     */
    function _getUserID() {
        return $this->userID;
    }

    /**
     * Set the current UserID
     *
     * @access private
     * @param  void
     * @return int UserID
     */
    function _setUserID($iUserID) {
        return $this->userID = $iUserID;
    }

    /**
     * Serve a webdav request
     *
     * @access public
     * @param  void
     * @return void
     */
    function ServeRequest()	{

        global $default;

        if ($this->debugInfo == 'on') {

            $this->ktwebdavLog('_SERVER is ' . print_r($_SERVER, true), 'info', true);
        }

        // Get the client info
        $this->checkSafeMode();

        // identify ourselves
        $this->ktwebdavLog('WebDAV Server : ' . $this->dav_powered_by . ' [KT:'.$default->systemVersion."]", 'info', true);
        header('X-Dav-Powered-By: '.$this->dav_powered_by . ' [KT:'.$default->systemVersion.']');

        // check authentication
        if (!$this->_check_auth()) {
            $this->ktwebdavLog('401 Unauthorized - Authorisation failed.' .$this->lastMsg, 'info', true);
            $this->ktwebdavLog('----------------------------------------', 'info', true);
            $this->http_status('401 Unauthorized - Authorisation failed. ' .$this->lastMsg);

            // RFC2518 says we must use Digest instead of Basic
            // but Microsoft Clients do not support Digest
            // and we don't support NTLM and Kerberos
            // so we are stuck with Basic here
            header('WWW-Authenticate: Basic realm="'.($this->http_auth_realm).'"');

            return;
        }

        // check
        if(! $this->_check_if_header_conditions()) {
            $this->http_status('412 Precondition failed');
            return;
        }

        // set path
        $request_uri = $this->_urldecode(!empty($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/');
        $this->path = str_replace($_SERVER['SCRIPT_NAME'], '', $request_uri);
        if(ini_get('magic_quotes_gpc')) {
            $this->path = stripslashes($this->path);
        }

        $this->ktwebdavLog('PATH_INFO is ' . $_SERVER['PATH_INFO'], 'info', true);

⌨️ 快捷键说明

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