ktutil.inc

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

INC
1,334
字号
    	if ($sPopen) {
    	    if (OS_WINDOWS) {
                $sCmd = "start /b \"kt\" " . $sCmd;
    	    }
    		return popen($sCmd, $sPopen);
    	}

    	// for exec, check return code and output...
    	$aRet = array();
    	$aOutput = array();
    	$iRet = '';

    	if(OS_WINDOWS){
    	    $sCmd = 'call '.$sCmd;
    	}
    	exec($sCmd, $aOutput, $iRet);
    	$aRet['ret'] = $iRet;
    	$aRet['out'] = $aOutput;
    	return $aRet;
    }
    // }}}


    // {{{ winexec
    /**
     * Execute a command on a windows platform.
     */
    function winexec($aCmd, $aOptions = null) {
    	if (is_array($aCmd)) {
    		$sCmd = KTUtil::safeShellString($aCmd);
    	} else {
    		$sCmd = $aCmd;
    	}
    	$sAppend = KTUtil::arrayGet($aOptions, 'append');
    	if ($sAppend) {
    		$sCmd .= " >> " . escapeshellarg($sAppend);
    	}

    	$sCmd = str_replace( '/','\\',$sCmd);

    	// Set wait to true if the execute must wait for the script to complete before continuing
    	$wait = true;
        if(isset($aOptions['exec_wait']) && ($aOptions['exec_wait'] == 'false')){
            $wait = false;
        }

    	// Iterate through the various execute functions till one works.
  		$WshShell = new COM("WScript.Shell");
        $res = $WshShell->Run($sCmd, 0, $wait);

        if($res){
            return $res;
        }

    	$sCmd = "start /b \"kt\" " . $sCmd;
    	$fp = popen($sCmd, 'r');
    	fclose($fp);

    	if($wait){
    	    sleep(1);
    	}
    	return 1;
    }
    // }}}

    // {{{ copyDirectory
    function copyDirectory($sSrc, $sDst, $bMove = false) {
        if (file_exists($sDst)) {
            return PEAR::raiseError(_kt("Destination directory already exists."));
        }
        if (OS_UNIX) {
            if ($bMove && file_exists('/bin/mv')) {
                KTUtil::pexec(array('/bin/mv', $sSrc, $sDst));
                return;
            }
            if (!$bMove && file_exists('/bin/cp')) {
                KTUtil::pexec(array('/bin/cp', '-R', $sSrc, $sDst));
                return;
            }
        }

        if (substr($sDst, 0, strlen($sSrc)) === $sSrc) {
            return PEAR::raiseError(_kt("Destination of move is within source"));
        }

        $hSrc = @opendir($sSrc);
        if ($hSrc === false) {
            return PEAR::raiseError(sprintf(_kt("Could not open source directory: %s"), $sSrc));
        }

        if (@mkdir($sDst, 0777) === false) {
            return PEAR::raiseError(sprintf(_kt("Could not create destination directory: %s"), $sDst));
        }

        while (($sFilename = readdir($hSrc)) !== false) {
            if (in_array($sFilename, array('.', '..'))) {
                continue;
            }
            $sOldFile = sprintf("%s/%s", $sSrc, $sFilename);
            $sNewFile = sprintf("%s/%s", $sDst, $sFilename);

            if (is_dir($sOldFile)) {
                KTUtil::copyDirectory($sOldFile, $sNewFile, $bMove);
                continue;
            }

            if ($bMove) {
                KTUtil::moveFile($sOldFile, $sNewFile);
            } else {
                copy($sOldFile, $sNewFile);
            }
        }
        if ($bMove) {
            @rmdir($sSrc);
        }
    }
    // }}}

    // {{{ moveDirectory
    function moveDirectory($sSrc, $sDst) {
        return KTUtil::copyDirectory($sSrc, $sDst, true);
    }
    // }}}

    // {{{ deleteDirectory
    function deleteDirectory($sPath) {
        if (OS_UNIX) {
            if (file_exists('/bin/rm')) {
                KTUtil::pexec(array('/bin/rm', '-rf', $sPath));
                return;
            }
        }
        if (OS_WINDOWS) {
            // Potentially kills off all the files in the path, speeding
            // things up a bit
            exec("del /q /s " . escapeshellarg($sPath));
        }
        $hPath = @opendir($sPath);
        while (($sFilename = readdir($hPath)) !== false) {
            if (in_array($sFilename, array('.', '..'))) {
                continue;
            }
            $sFullFilename = sprintf("%s/%s", $sPath, $sFilename);
            if (is_dir($sFullFilename)) {
                KTUtil::deleteDirectory($sFullFilename);
                continue;
            }
            @chmod($sFullFilename, 0666);
            @unlink($sFullFilename);
        }
        closedir($hPath);
        @rmdir($sPath);
    }
    // }}}

    // {{{ moveFile
    function moveFile ($sSrc, $sDst) {
        // Only 4.3.3 and above allow us to use rename across partitions
        // on Unix-like systems.
        if (OS_UNIX) {
            // If /bin/mv exists, just use it.
            if (file_exists('/bin/mv')) {
                KTUtil::pexec(array('/bin/mv', $sSrc, $sDst));
                return;
            }

            $aSrcStat = stat($sSrc);
            if ($aSrcStat === false) {
                return PEAR::raiseError(sprintf(_kt("Couldn't stat source file: %s"), $sSrc));
            }
            $aDstStat = stat(dirname($sDst));
            if ($aDstStat === false) {
                return PEAR::raiseError(sprintf(_kt("Couldn't stat destination location: %s"), $sDst));
            }
            if ($aSrcStat["dev"] === $aDstStat["dev"]) {
                $res = @rename($sSrc, $sDst);
                if ($res === false) {
                    return PEAR::raiseError(sprintf(_kt("Couldn't move file to destination: %s"), $sDst));
                }
                return;
            }

            $res = @copy($sSrc, $sDst);
            if ($res === false) {
                return PEAR::raiseError(sprintf(_kt("Could not copy to destination: %s"), $sDst));
            }
            $res = @unlink($sSrc);
            if ($res === false) {
                return PEAR::raiseError(sprintf(_kt("Could not remove source: %s"), $sSrc));
            }
        } else {
            $res = @rename($sSrc, $sDst);
            if ($res === false) {
                return PEAR::raiseError(sprintf(_kt("Could not move to destination: %s"), $sDst));
            }
        }
    }
    // }}}

    // {{{ copyFile
    function copyFile ($sSrc, $sDst) {
        // Only 4.3.3 and above allow us to use rename across partitions
        // on Unix-like systems.
        if (OS_UNIX) {
            $aSrcStat = stat($sSrc);
            if ($aSrcStat === false) {
                return PEAR::raiseError(sprintf(_kt("Couldn't stat source file: %s"), $sSrc));
            }
            $aDstStat = stat(dirname($sDst));
            if ($aDstStat === false) {
                return PEAR::raiseError(sprintf(_kt("Couldn't stat destination location: %s"), $sDst));
            }

            $res = @copy($sSrc, $sDst);
            if ($res === false) {
                return PEAR::raiseError(sprintf(_kt("Could not copy to destination: %s"), $sDst));
            }
        } else {
            $res = @copy($sSrc, $sDst);
            if ($res === false) {
                return PEAR::raiseError(sprintf(_kt("Could not copy to destination: %s"), $sDst));
            }
        }
    }
    // }}}

    // {{{ getTableName
    /**
     * The one true way to get the correct name for a table whilst
     * respecting the administrator's choice of table naming.
     */
    static function getTableName($sTable) {
        $sDefaultsTable = $sTable . "_table";
        if (isset($GLOBALS['default']->$sDefaultsTable)) {
            return $GLOBALS['default']->$sDefaultsTable;
        }
        return $sTable;
    }
    // }}}

    // {{{ getId
    function getId($oEntity) {
        if (is_object($oEntity)) {
            if (method_exists($oEntity, 'getId')) {
                return $oEntity->getId();
            }
            return PEAR::raiseError(_kt('Non-entity object'));
        }

        if (is_numeric($oEntity)) {
            return $oEntity;
        }

        return PEAR::raiseError(_kt('Non-entity object'));
    }
    // }}}

    // {{{ getObject
    function getObject($sClassName, &$iId) {
        if (is_object($iId)) {
            return $iId;
        }

        if (is_numeric($iId)) {
            return call_user_func(array($sClassName, 'get'), $iId);
        }
        return PEAR::raiseError(_kt('Non-entity object'));
    }
    // }}}

    // {{{ meldOptions
    static function meldOptions($aStartOptions, $aAddOptions) {
        if (!is_array($aStartOptions)) {
            $aStartOptions = array();
        }
        if (!is_array($aAddOptions)) {
            $aAddOptions = array();
        }
        return array_merge($aStartOptions, $aAddOptions);
    }
    // }}}

    // {{{ getRequestScriptName
    function getRequestScriptName($server) {
        $request_uri = $server['REQUEST_URI'];
        $script_name = $server['SCRIPT_NAME'];

        /*
         * Until script_name is fully inside request_uri, strip off bits
         * of script_name.
         */

        //print "Checking if $script_name is in $request_uri\n";
        while ($script_name && strpos($request_uri, $script_name) === false) {
            //print "No it isn't.\n";
            $lastslash = strrpos($script_name, '/');
            $lastdot = strrpos($script_name, '.');
            //print "Last slash is at: $lastslash\n";
            //print "Last dot is at: $lastdot\n";
            if ($lastslash > $lastdot) {
                $script_name = substr($script_name, 0, $lastslash);
            } else {
                $script_name = substr($script_name, 0, $lastdot);
            }
            //print "Checking is $script_name is in $request_uri\n";
        }
        return $script_name;
    }
    // }}}

    // {{{ nameToLocalNamespace
    function nameToLocalNamespace ($sSection, $sName) {
        $sBase = generateLink("");
        $sName = trim($sName);
        $sName = strtolower($sName);
        $sName = str_replace(array("'", "/",'"', " "), array(), $sName);
        $sSection = trim($sSection);
        $sSection = strtolower($sSection);
        $sSection = str_replace(array("'", "/",'"', " "), array(),
                $sSection);
        return $sBase . 'local' . '/' . $sSection . '/' . $sName;
    }
    // }}}

    // {{{ findCommand
    function findCommand($sConfigVar, $sDefault = null) {
        // Check for the stack command before using the user defined command
        $result = KTUtil::checkForStackCommand($sConfigVar);
    	if (!empty($result))
    	{
    		return $result;
    	}

    	$oKTConfig =& KTConfig::getSingleton();
    	$sCommand = $oKTConfig->get($sConfigVar, $sDefault);
    	if (empty($sCommand)) {
    		return false;
    	}
    	if (file_exists($sCommand)) {
    		return $sCommand;
    	}
    	if (file_exists($sCommand . ".exe")) {
    		return $sCommand . ".exe";
    	}

    	$sExecSearchPath = $oKTConfig->get("KnowledgeTree/execSearchPath");
    	$sExecSearchPath .= PATH_SEPARATOR . KT_DIR . "/../common/";
    	$sExecSearchPath .= PATH_SEPARATOR . KT_DIR . "/../bin/xpdf/";
    	$sExecSearchPath .= PATH_SEPARATOR . KT_DIR . "/../bin/antiword/";
    	$sExecSearchPath .= PATH_SEPARATOR . KT_DIR . "/../bin/zip/";
    	$sExecSearchPath .= PATH_SEPARATOR . KT_DIR . "/../bin/unzip/";

    	$paths = split(PATH_SEPARATOR, $sExecSearchPath);
    	foreach ($paths as $path) {

    		if (file_exists($path . '/' . $sCommand)) {
    			return $path . '/' . $sCommand;
    		}
    		if (file_exists($path . '/' . $sCommand . ".exe")) {
    			return $path . '/' . $sCommand . ".exe";
    		}
    	}
    	return false;
    }
    // }}}

    function checkForStackCommand($configCommand)
    {
    	$config = KTConfig::getSingleton();
    	$stackPath = realpath(KT_DIR . '/..');

    	switch ($configCommand)
    	{
    		case 'externalBinary/xls2csv':
    			if (OS_WINDOWS)
				{
					$script = $stackPath . '/bin/catdoc/xls2csv.exe';
				}
				else
				{
					$script = $stackPath . '/common/bin/xls2csv';
				}
				break;
    		case 'externalBinary/pdftotext':;
    			if (OS_WINDOWS)
    			{
    				$script = $stackPath . '/bin/xpdf/pdftotext.exe';
    			}
    			else
    			{
    				$script = $stackPath . '/common/bin/pdftotext';
    			}
    			break;
    		case 'externalBinary/catppt':
    			if (OS_WINDOWS)
    			{
    				$script = $stackPath . '/bin/catdoc/catppt.exe';
    			}
    			else
    			{
    				$script = $stackPath . '/common/bin/catppt';
    			}
    			break;
    		case 'externalBinary/pstotext':
    			if (OS_WINDOWS)
    			{
    				$script = $stackPath . '/bin/pstotext/pstotext.exe';
    			}
    			else
    			{
    				$script = $stackPath . '/common/bin/pstotext';
    			}
    			break;
    		case 'externalBinary/catdoc':
    			if (OS_WINDOWS)
    			{
    				$script = $stackPath . '/bin/catdoc/catdoc.exe';
    			}
    			else
    			{
    				$script = $stackPath . '/common/bin/catdoc';
    			}
    			break;
    		case 'externalBinary/antiword':
    			if (OS_WINDOWS)
    			{
    				$script = $stackPath . '/bin/antiword/antiword.exe';
    			}
    			else
    			{
    				$script = $stackPath . '/common/bin/antiword';
    			}
    			break;
    		case 'externalBinary/python':
    			if (OS_WINDOWS)
				{
					$script = $stackPath . '/openoffice/program/python.bat';
				}
				else
				{
					$script = $stackPath . '/openoffice/program/python';
				}
				break;
    		case 'externalBinary/java':
    			if (OS_WINDOWS)

⌨️ 快捷键说明

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