PHPDaemon\Core\ShellCommand::execute PHP Method

execute() public method

Execute
public execute ( string $binPath = null, array $args = null, array $env = null ) : this
$binPath string Optional. Binpath
$args array Optional. Arguments
$env array Optional. Hash of environment's variables
return this
    public function execute($binPath = null, $args = null, $env = null)
    {
        if ($binPath !== null) {
            $this->binPath = $binPath;
        }
        if ($env !== null) {
            $this->env = $env;
        }
        if ($args !== null) {
            $this->args = $args;
        }
        $this->cmd = $this->binPath . static::buildArgs($this->args) . ($this->outputErrors ? ' 2>&1' : '');
        if (isset($this->setUser) || isset($this->setGroup)) {
            if (isset($this->setUser) && isset($this->setGroup) && $this->setUser !== $this->setGroup) {
                $this->cmd = 'sudo -g ' . escapeshellarg($this->setGroup) . '  -u ' . escapeshellarg($this->setUser) . ' ' . $this->cmd;
            } else {
                $this->cmd = 'su ' . escapeshellarg($this->setGroup) . ' -c ' . escapeshellarg($this->cmd);
            }
        }
        if ($this->chroot !== '/') {
            $this->cmd = 'chroot ' . escapeshellarg($this->chroot) . ' ' . $this->cmd;
        }
        if ($this->nice !== null) {
            $this->cmd = 'nice -n ' . (int) $this->nice . ' ' . $this->cmd;
        }
        $pipesDescr = [0 => ['pipe', 'r'], 1 => ['pipe', 'w']];
        if ($this->errlogfile !== null && !$this->outputErrors) {
            $pipesDescr[2] = ['file', $this->errlogfile, 'a'];
            // @TODO: refactoring
        }
        $this->pd = proc_open($this->cmd, $pipesDescr, $this->pipes, $this->cwd, $this->env);
        if ($this->pd) {
            $this->setFd($this->pipes[1]);
        }
        return $this;
    }

Usage Example

Beispiel #1
0
 /**
  * Constructor.
  * @return void
  */
 public function init()
 {
     $this->header('Content-Type: text/html');
     // default header.
     $this->proc = new \PHPDaemon\Core\ShellCommand();
     $this->proc->readPacketSize = $this->appInstance->readPacketSize;
     $this->proc->onReadData([$this, 'onReadData']);
     $this->proc->onWrite([$this, 'onWrite']);
     $this->proc->binPath = $this->appInstance->binPath;
     $this->proc->chroot = $this->appInstance->chroot;
     if (isset($this->attrs->server['BINPATH'])) {
         if (isset($this->appInstance->binAliases[$this->attrs->server['BINPATH']])) {
             $this->proc->binPath = $this->appInstance->binAliases[$this->attrs->server['BINPATH']];
         } elseif ($this->appInstance->config->allowoverridebinpath->value) {
             $this->proc->binPath = $this->attrs->server['BINPATH'];
         }
     }
     if (isset($this->attrs->server['CHROOT']) && $this->appInstance->config->allowoverridechroot->value) {
         $this->proc->chroot = $this->attrs->server['CHROOT'];
     }
     if (isset($this->attrs->server['SETUSER']) && $this->appInstance->config->allowoverrideuser->value) {
         $this->proc->setUser = $this->attrs->server['SETUSER'];
     }
     if (isset($this->attrs->server['SETGROUP']) && $this->appInstance->config->allowoverridegroup->value) {
         $this->proc->setGroup = $this->attrs->server['SETGROUP'];
     }
     if (isset($this->attrs->server['CWD']) && $this->appInstance->config->allowoverridecwd->value) {
         $this->proc->cwd = $this->attrs->server['CWD'];
     } elseif ($this->appInstance->config->cwd->value !== null) {
         $this->proc->cwd = $this->appInstance->config->cwd->value;
     } else {
         $this->proc->cwd = dirname($this->attrs->server['SCRIPT_FILENAME']);
     }
     $this->proc->setArgs([$this->attrs->server['SCRIPT_FILENAME']]);
     $this->proc->setEnv($this->attrs->server);
     $this->proc->execute();
 }