Horde\ManageSieve::_doCmd PHP Method

_doCmd() protected method

Send a command and retrieves a response from the server.
protected _doCmd ( string $cmd = '', boolean $auth = false ) : string
$cmd string The command to send.
$auth boolean Whether this is an authentication command.
return string Reponse string if an OK response.
    protected function _doCmd($cmd = '', $auth = false)
    {
        $referralCount = 0;
        while ($referralCount < $this->_maxReferralCount) {
            if (strlen($cmd)) {
                $this->_sendCmd($cmd);
            }
            $response = '';
            while (true) {
                $line = $this->_recvLn();
                if (preg_match('/^(OK|NO)/i', $line, $tag)) {
                    // Check for string literal message.
                    // DBMail has some broken versions that send the trailing
                    // plus even though it's disallowed.
                    if (preg_match('/{([0-9]+)\\+?}$/', $line, $matches)) {
                        $line = substr($line, 0, -(strlen($matches[1]) + 2)) . str_replace("\r\n", ' ', $this->_recvBytes($matches[1] + 2));
                    }
                    if ('OK' == \Horde_String::upper($tag[1])) {
                        $response .= $line;
                        return rtrim($response);
                    }
                    throw new Exception(trim($response . substr($line, 2)), 3);
                }
                if (preg_match('/^BYE/i', $line)) {
                    try {
                        $this->disconnect(false);
                    } catch (Exception $e) {
                        throw new Exception('Cannot handle BYE, the error was: ' . $e->getMessage(), 4);
                    }
                    // Check for referral, then follow it.  Otherwise, carp an
                    // error.
                    if (preg_match('/^bye \\(referral "(sieve:\\/\\/)?([^"]+)/i', $line, $matches)) {
                        // Replace the old host with the referral host
                        // preserving any protocol prefix.
                        $this->_params['host'] = preg_replace('/\\w+(?!(\\w|\\:\\/\\/)).*/', $matches[2], $this->_params['host']);
                        try {
                            $this->_handleConnectAndLogin();
                        } catch (Exception $e) {
                            throw new Exception\Referral('Cannot follow referral to ' . $this->_params['host'] . ', the error was: ' . $e->getMessage());
                        }
                        break;
                    }
                    throw new Exception(trim($response . $line), 6);
                }
                if (preg_match('/^{([0-9]+)}/', $line, $matches)) {
                    // Matches literal string responses.
                    $line = $this->_recvBytes($matches[1] + 2);
                    if (!$auth) {
                        // Receive the pending OK only if we aren't
                        // authenticating since string responses during
                        // authentication don't need an OK.
                        $this->_recvLn();
                    }
                    return $line;
                }
                if ($auth) {
                    // String responses during authentication don't need an
                    // OK.
                    $response .= $line;
                    return rtrim($response);
                }
                $response .= $line . "\r\n";
                $referralCount++;
            }
        }
        throw new Exception\Referral('Max referral count (' . $referralCount . ') reached.');
    }