2

有没有办法修改 RHEL/Apache/PHP 的权限,以便以安全的方式使用 shell 命令调用 SVN - 即。shell_exec()、shell() 还是 system() ?

目前,我在尝试 shell 命令时被拒绝权限。

4

2 回答 2

0

你不必使用exec()调用和制造你自己的svn add ...命令,因为 PECL 有一个稳定的 PHP 扩展 - Subversion。包裹在这里

这样您就可以轻松访问代码中所有需要的 SVN 功能。

于 2012-10-12T05:25:56.170 回答
-1

我能够做到这一点的唯一方法是使用 SSH2 库并在特定帐户下本地连接。否则我永远无法正确设置它们。


这不是一个好的图书馆,我有 1 天的时间来测试和搜索文档,所以它绝对不是最好的方法,但至少它可以工作......

目前使用的图书馆:

<?php
/**
 * 
 * Runs several SSH2 commands on the devl server as root
 * 
 */
function ssh2Run(array $commands, $catchoutput = true, $server = 'localhost', $user = 'root', $logfile = NULL){

    //Open a log file for web output
    if($logfile == NULL){ $logfile = logCreate(); }

    //Connect to ssh2
    $connection = ssh2_connect($server);
    $hostkey = ssh2_fingerprint($connection);
    logWrite($logfile, 'Connected to '.$server.', hostkey = '.$hostkey);
    ssh2_auth_pubkey_file($connection, $user, '/home/myuser/.ssh/id_rsa.pub', '/home/myuser/.ssh/id_rsa');

    //Execute the various commands and read the output to the log file
    foreach($commands as $command){

        // Run a command that will probably write to stderr (unless you have a folder named /hom)
        logWrite($logfile, 'Sending command: '.$user.'@'.$server.': '.$command);
        logWrite($logfile, '----------------------------------------------------------------------------------');
        $outputStream = ssh2_exec($connection, $command, true);
        if(is_resource($outputStream)){
            stream_set_blocking($outputStream, true);
        }

        //Catch
        if($catchoutput){

            if(is_resource($errorStream)){
                $errorStream = ssh2_fetch_stream($outputStream, SSH2_STREAM_STDERR);
            }

            // Enable blocking for both streams
            if(is_resource($errorStream)){
                stream_set_blocking($errorStream, true);
            }

            // Whichever of the two below commands is listed first will receive its appropriate output.  The second command receives nothing
            logWrite($logfile, 'Output of command:');

            //Loop the stream until it is complete
            while((is_resource($outputStream) && !feof($outputStream)) || (is_resource($errorStream) && !feof($errorStream))){

                //Content read out
                if(is_resource($outputStream) && !feof($outputStream)){
                    $outputContent = trim(fgets($outputStream));
                }else{
                    $outputContent = '';
                }
                if(is_resource($errorStream) && !feof($errorStream)){
                    $errorContent = trim(fgets($errorStream));
                }else{
                    $errorContent = '';
                }

                //Add the information to the log
                if($outputContent == '' && $errorContent == ''){ continue; }
                if($outputContent !== ''){
                    logWrite($logfile, 'OUT: '.$outputContent);
                }
                if($errorContent !== ''){
                    logWrite($logfile, 'ERROR: '.$errorContent);
                }

            }

            // Close the streams       
            if(is_resource($errorStream)){ fclose($errorStream); }
            if(is_resource($outputStream)){ fclose($outputStream); }

        }

    }

    //Return the log
    return $logfile;

}

/**
 * 
 * List files in a SFTP enabled directory
 * 
 */
function sftpList($server, $user, $path){

    //Connect to ssh2
    $connection = ssh2_connect($server);
    $hostkey = ssh2_fingerprint($connection);
    ssh2_auth_pubkey_file($connection, $user, '/home/myuser/.ssh/id_rsa.pub', '/home/myuser/.ssh/id_rsa');

    //Create our SFTP resource
    if(!$sftp = ssh2_sftp($connection)){
        throw new Exception('Unable to create SFTP connection.');
    }

    /**
      * Now that we have our SFTP resource, we can open a directory resource
      * to get us a list of files. Here we will use the $sftp resource in
      * our address string as I previously mentioned since our ssh2:// 
      * protocol allows it.
      */
    $results = array();
    $dirHandle = opendir('ssh2.sftp://'.$sftp.$path);
    while (false !== ($result = readdir($dirHandle))) {
        if ($result != '.' && $result != '..') {
            $results[] = $result;
        }
    }
    closedir($dirHandle);

    //Return the log
    return $results;

}
于 2012-10-11T18:25:09.410 回答