ktwebdavserver.inc.php

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

PHP
1,547
字号
            $sQuery .= "WHERE D.folder_id = ? AND DC.filename = ? AND D.status_id=1";

            $aParams = array($iFolderID, $sFileName);
            $iDocumentID = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');

            if (PEAR::isError($iDocumentID)) {
                $this->ktwebdavLog("iDocumentID error in _folderOrDocument", 'info', true);
                return false;
            }

            /* ** If the path refers to a folder or a non-existing document,
                Get the folder id using the basename and parent folder id as parameters.
                If an id is obtained then the path refers to an existing folder.
                If no id is returned and the basename is empty then path refers to the root folder.
                If no id is returned and the basename is not empty, then the path refers to either a non-existing folder or document. ** */
            if ($iDocumentID === null) {
                $this->ktwebdavLog("iDocumentID is null", 'info', true);
                // FIXME: Direct database access
                $sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?";
                $aParams = array($iFolderID, $sFileName);
                $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');

                if (PEAR::isError($id)) {
                    $this->ktwebdavLog("A DB(2) error occurred in _folderOrDocument", 'info', true);
                    return false;
                }
                if (is_null($id)) {
                    if ($sFileName == '') {
                        return array($iFolderID, null);
                    }
                    $this->ktwebdavLog("id is null in _folderOrDocument", 'info', true);
                    return array($iFolderID, false);
                }
                if (substr($path, -1) !== "/") {
                    $this->ktwebdavLog("Setting Location Header to " . "Location: " . $_SERVER["PHP_SELF"] . "/", 'info', true);
                    header("Location: " . $_SERVER["PHP_SELF"] . "/");
                }
                $this->ktwebdavLog("DEBUG: return id ".$id, 'info', true);
                return array($id, null);
            }

            return array($iFolderID, (int)$iDocumentID);
        }

        /**
         *  PUT method handler
         *
         * @param  array  parameter passing array
         * @return string  HTTP status code or false
         */
        function PUT(&$options)
        {
            global $default;

            if ($this->checkSafeMode()) {

                $this->ktwebdavLog("Entering PUT. options are " .  print_r($options, true), 'info', true);
                $this->ktwebdavLog("dav_client is: " .  $this->dav_client, 'info', true);

                $path = $options["path"];

                // Fix for Mac
                // Modified - 22/10/07
                // Mac adds DS_Store files when folders are added and ._filename files when files are added
                // we want to ignore them.
                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. Ignore the .DS_Store files created with every folder.", 'info', true);
                        // ignore
                        return "204 No Content";
                    }

                    if($fileName[0] == '.' && $fileName[1] == '_'){
                        $fileName = substr($fileName, 2);
                        $this->ktwebdavLog("Using a mac client. Ignore the ._filename files created with every file.", 'info', true);
                        // ignore
                        return "204 No Content";
                    }
                }

                $res = $this->_folderOrDocument($path);
                list($iFolderID, $iDocumentID) = $res;

                if ($iDocumentID === false && $iFolderID === false) {
                    // Couldn't find intermediary paths
                    /*
                     * RFC2518: 8.7.1 PUT for Non-Collection Resources
                     *
                     * 409 (Conflict) - A PUT that would result in the creation
                     * of a resource without an appropriately scoped parent collection
                     * MUST fail with a 409 (Conflict).
                     */
                    return "409 Conflict - Couldn't find intermediary paths";
                }

                $oParentFolder =& Folder::get($iFolderID);
                // 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, $oParentFolder)) {
                    return "403 Forbidden - User does not have sufficient permissions";
                }

                $this->ktwebdavLog("iDocumentID is " . $iDocumentID, 'info', true);

                if (is_null($iDocumentID)) {
                    // This means there is a folder with the given path
                    $this->ktwebdavLog("405 Method not allowed", 'info', true);
                    return "405 Method not allowed - There is a folder with the given path";
                }

                if ($iDocumentID == false) {
                    $this->ktwebdavLog("iDocumentID is false", 'info', true);
                }

                if ($iDocumentID !== false) {
                    // This means there is a document with the given path
                    $oDocument = Document::get($iDocumentID);

                    $this->ktwebdavLog("oDocument is " .  print_r($oDocument, true), 'info', true);
                    $this->ktwebdavLog("oDocument statusid is " .  print_r($oDocument->getStatusID(), true), 'info', true);

                    if ( ( (int)$oDocument->getStatusID() != STATUS_WEBDAV ) && ( (int)$oDocument->getStatusID() != DELETED )) {
                        $this->ktwebdavLog("Trying to PUT to an existing document", 'info', true);
                        if (!$this->dav_client == "MS" && !$this->dav_client == "MC") return "409 Conflict - There is a document with the given path";
                    }

                    // FIXME: Direct filesystem access
                    $fh = $options["stream"];
                    $sTempFilename = tempnam('/tmp', 'ktwebdav_dav_put');
                    $ofh = fopen($sTempFilename, 'w');

                    $contents = '';
                    while (!feof($fh)) {
                        $contents .= fread($fh, 8192);
                    }
                    $fres = fwrite($ofh, $contents);
                    $this->ktwebdavLog("A DELETED or CHECKEDOUT document exists. Overwriting...", 'info', true);
                    $this->ktwebdavLog("Temp Filename is: " . $sTempFilename, 'info', true );
                    $this->ktwebdavLog("File write result size was: " . $fres, 'info', true );

                    fflush($fh);
                    fclose($fh);
                    fflush($ofh);
                    fclose($ofh);
                    $this->ktwebdavLog("Files have been flushed and closed.", 'info', true );

                    $name = basename($path);
                    $aFileArray = array(
                            "name" => $name,
                            "size" => filesize($sTempFilename),
                            "type" => false,
                            "userID" => $this->_getUserID(),
                            );
                    $this->ktwebdavLog("aFileArray is " .  print_r($aFileArray, true), 'info', true);

                    //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
                    $aOptions = array(
                            //'contents' => new KTFSFileLike($sTempFilename),
                            'temp_file' => $sTempFilename,
                            'metadata' => array(),
                            'novalidate' => true,
                            );
                    $this->ktwebdavLog("DEBUG: overwriting file. Options: ".print_r($aOptions, true));
                    $this->ktwebdavLog("DEBUG: overwriting file. Temp name: ".$sTempFilename.' '.print_r($sTempFilename, true));
                    $this->ktwebdavLog("DEBUG: overwriting file. Name: ".$name.' '.print_r($name, true));

                    // Modified - 25/10/07 - changed add to overwrite
                    //$oDocument =& KTDocumentUtil::add($oParentFolder, $name, $oUser, $aOptions);
                    $oDocument =& KTDocumentUtil::overwrite($oDocument, $name, $sTempFilename, $oUser, $aOptions);

                    if(PEAR::isError($oDocument)) {
                        $this->ktwebdavLog("oDocument ERROR: " .  $oDocument->getMessage(), 'info', true);
		                unlink($sTempFilename);
                        return "409 Conflict - " . $oDocument->getMessage();
                    }

                    $this->ktwebdavLog("oDocument is " .  print_r($oDocument, true), 'info', true);

                    unlink($sTempFilename);
                    return "201 Created";
                }

                $options["new"] = true;
                // FIXME: Direct filesystem access
                $fh = $options["stream"];
                $sTempFilename = tempnam('/tmp', 'ktwebdav_dav_put');
                $ofh = fopen($sTempFilename, 'w');

                $contents = '';
                while (!feof($fh)) {
                    $contents .= fread($fh, 8192);
                }
                $fres = fwrite( $ofh, $contents);
                $this->ktwebdavLog("Content length was not 0, doing the whole thing.", 'info', true );
                $this->ktwebdavLog("Temp Filename is: " . $sTempFilename, 'info', true );
                $this->ktwebdavLog("File write result size was: " . $fres, 'info', true );

                fflush($fh);
                fclose($fh);
                fflush($ofh);
                fclose($ofh);
                $this->ktwebdavLog("Files have been flushed and closed.", 'info', true );

                $name = basename($path);
                $aFileArray = array(
                        "name" => $name,
                        "size" => filesize($sTempFilename),
                        "type" => false,
                        "userID" => $this->_getUserID(),
                        );
                $this->ktwebdavLog("aFileArray is " .  print_r($aFileArray, true), 'info', true);

                //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
                $aOptions = array(
                        //'contents' => new KTFSFileLike($sTempFilename),
                        'temp_file' => $sTempFilename,
                        'metadata' => array(),
                        'novalidate' => true,
                        );
                $oDocument =& KTDocumentUtil::add($oParentFolder, $name, $oUser, $aOptions);

                if(PEAR::isError($oDocument)) {
                    $this->ktwebdavLog("oDocument ERROR: " .  $oDocument->getMessage(), 'info', true);
                    unlink($sTempFilename);
                    return "409 Conflict - " . $oDocument->getMessage();
                }

                $this->ktwebdavLog("oDocument is " .  print_r($oDocument, true), 'info', true);

                unlink($sTempFilename);
                return "201 Created";

            }  else return "423 Locked - KTWebDAV is in SafeMode";

        }

        /**
         * MKCOL method handler
         *
         * @param  array  parameter passing array
         * @return string  HTTP status code or false
         */
        function MKCOL($options)
        {
            $this->ktwebdavLog("Entering MKCOL. options are " .  print_r($options, true), 'info', true);

            if ($this->checkSafeMode()) {

                global $default;

                if (!empty($_SERVER["CONTENT_LENGTH"])) {
                    /*
                     * RFC2518: 8.3.2 MKCOL status codes
                     *
                     * 415 (Unsupported Media Type)- The server does not support
                     * the request type of the body.
                     */
                    return "415 Unsupported media type";
                }

                // Take Windows's escapes out
                $path = str_replace('\\', '' , $options['path']);


                $res = $this->_folderOrDocument($path);
                list($iFolderID, $iDocumentID) = $res;

                if ($iDocumentID === false && $iFolderID === false) {
                    // Couldn't find intermediary paths
                    /*
                     * RFC2518: 8.3.2 MKCOL status codes
                     *
                     * 409 (Conflict) - A collection cannot be made at the
                     * Request-URI until one or more intermediate collections
                     * have been created.
                     */
                    $this->ktwebdavLog("409 Conflict in MKCOL", 'info', true);
                    return "409 Conflict - Couldn't find intermediary paths";
                }


                if (is_null($iDocumentID)) {
                    // This means there is a folder with the given path
                    /*
                     * RFC2518: 8.3.2 MKCOL status codes
                     *
                     * 405 (Method Not Allowed) - MKCOL can only be executed on
                     * a deleted/non-existent resource.
                     */
                    $this->ktwebdavLog("405 Method not allowed - There is a folder with the given path", 'info', true);
                    return "405 Method not allowed - There is a folder with the given path";
                }
                if ($iDocumentID !== false) {
                    // This means there is a document with the given path
                    /*
                     * RFC2518: 8.3.2 MKCOL status codes
                     *
                     * 405 (Method Not Allowed) - MKCOL can only be executed on
                     * a deleted/non-existent resource.
                     */
                    $this->ktwebdavLog("405 Method not allowed - There is a document with the given path", 'info', true);
                    return "405 Method not allowed - There is a document with t

⌨️ 快捷键说明

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