www/src/rootio/Bundle/vmailmeBundle/Lib/RoundcubeLogin.php
/**
     * Send a POST/GET request to the Roundcube login-script
     * to simulate the login.
     *
     * If the second parameter $postData is set, the function will
     * use the POST method, otherwise a GET will be sent.
     *
     * Ensures that all cookies are sent and parses all response headers
     * for a new Roundcube session ID. If a new SID is found, rcSessionId is set.
     *
     * @param string Optional POST data in urlencoded form (param1=value1&...)
     * @return string Returns the complete request response with all headers.
     */
Send a POST/GET request to the Roundcube login-script
to simulate the login.

If the second parameter $postData is set, the function will
use the POST method, otherwise a GET will be sent.

Ensures that all cookies are sent and parses all response headers
for a new Roundcube session ID. If a new SID is found, rcSessionId is set.

@param string Optional POST data in urlencoded form (param1=value1&...)
@return string Returns the complete request response with all headers.
Send a POST / GET request to the Roundcube login - script to simulate the login . If the second parameter $postData is set the function will use the POST method otherwise a GET will be sent . Ensures that all cookies are sent and parses all response headers for a new Roundcube session ID . If a new SID is found rcSessionId is set . @param string Optional POST data in urlencoded form ( param1 = value1& ... ) @return string Returns the complete request response with all headers .
private function sendRequest($path, $postData = false) {
        $method = (!$postData) ? "GET" : "POST";
        $isSSL = $this->ssl;
        $port = intval($this->port);
        $host = $this->hostname;
        if ($isSSL === null) {
            $isSSL = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'];
        }
        if (!$port) {
            if (isset($_SERVER['SERVER_PORT']) && is_numeric($_SERVER['SERVER_PORT'])) {
                $port = $_SERVER['SERVER_PORT'];
            }
            else if ($isSSL) {
                $port = 443;
            }
            else {
                $port = 80;
            }
        }
        if (!$host) {
            if ($isSSL && isset($_SERVER['SSL_TLS_SNI']) && $_SERVER['SSL_TLS_SNI']) {
                $host = $_SERVER['SSL_TLS_SNI'];
            }
            else if (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']) {
                $host = $_SERVER['HTTP_HOST'];
            }
            else {
                $host = "127.0.0.1";
            }
        }
        if ($isSSL) {
            $host = "ssl://$host";
        }
        $COOKIES = array();
        foreach ($this->cookies as $name=>$value) {
            $COOKIES[] = "$name=$value";
        }
        if (!$this->cookies->get('roundcube_sessid') && $this->rcSessionID)
            $COOKIES[] = "roundcube_sessid={$this->rcSessionID}";
        if (!$this->cookies->get('roundcube_sessauth') && $this->rcSessionAuth)
            $COOKIES[] = "roundcube_sessauth={$this->rcSessionAuth}";
        $COOKIES = ($COOKIES) ? "Cookie: ".join("; ",$COOKIES)."\r\n" : "";
        if ($method == "POST") {
            $request =
                  "POST ".$path." HTTP/1.1\r\n"
                . "Host: ".$_SERVER['HTTP_HOST']."\r\n"
                . "User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\r\n"
                . "Content-Type: application/x-www-form-urlencoded\r\n"
                . "Content-Length: ". strlen($postData) ."\r\n"
                . $COOKIES
                . "Connection: close\r\n\r\n"
                . $postData;
        }
        else {
            $request =
                  "GET ".$path." HTTP/1.1\r\n"
                . "Host: ".$_SERVER['HTTP_HOST']."\r\n"
                . "User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\r\n"
                . $COOKIES
                . "Connection: close\r\n\r\n";
        }
        $fp = fsockopen($host, $port);
        if (!$fp) {
            $this->addDebug("UNABLE TO OPEN SOCKET", "Could not open socket for $host at port $port");
            throw new RoundcubeLoginException("Could not open socket for $host at port $port");
        }
        $this->addDebug("REQUEST", $request);
        fputs($fp, $request);
        $response = "";
        while (!feof($fp)) {
            $line = fgets($fp, 4096);
            if (preg_match('/^HTTP\/1\.\d\s+404\s+/',$line))
                throw new RoundcubeLoginException("No Roundcube installation found at '$path'");
            if (preg_match('/^Set-Cookie:\s*(.+roundcube_sessid=([^;]+);.+)$/i',$line,$match)) {
                header($line, false);
                $this->addDebug("GOT SESSION ID", "New session ID: '$match[2]'.");
                $this->rcSessionID = $match[2];
            }
            if (preg_match('/^Set-Cookie:.+roundcube_sessauth=([^;]+);/i',$line,$match)) {
                header($line, false);
                $this->addDebug("GOT SESSION AUTH", "New session auth: '$match[1]'.");
                $this->rcSessionAuthi = $match[1];
            }
             token (since Roundcube 0.5.1)
            if (preg_match('/"request_token":"([^"]+)",/mi', $response, $m))
                $this->lastToken = $m[1];
            if (preg_match('/<input.+name="_token".+value="([^"]+)"/mi', $response, $m))
                $this->lastToken = $m[1]; 
            $response .= $line;
        }
        fclose($fp);
        $this->addDebug("RESPONSE", $response);
        return $response;
    }
private function sendRequest ( $ path , $ postData = false ) { $ method = ( ! $ postData ) ? " GET " : " POST " ; $ isSSL = $ this -> ssl ; $ port = intval ( $ this -> port ) ; $ host = $ this -> hostname ; if ( $ isSSL === null ) { $ isSSL = isset ( $ _SERVER [ 'HTTPS' ] ) && $ _SERVER [ 'HTTPS' ] ; } if ( ! $ port ) { if ( isset ( $ _SERVER [ 'SERVER_PORT' ] ) && is_numeric ( $ _SERVER [ 'SERVER_PORT' ] ) ) { $ port = $ _SERVER [ 'SERVER_PORT' ] ; } else if ( $ isSSL ) { $ port = 443 ; } else { $ port = 80 ; } } if ( ! $ host ) { if ( $ isSSL && isset ( $ _SERVER [ 'SSL_TLS_SNI' ] ) && $ _SERVER [ 'SSL_TLS_SNI' ] ) { $ host = $ _SERVER [ 'SSL_TLS_SNI' ] ; } else if ( isset ( $ _SERVER [ 'HTTP_HOST' ] ) && $ _SERVER [ 'HTTP_HOST' ] ) { $ host = $ _SERVER [ 'HTTP_HOST' ] ; } else { $ host = " 127.0.0.1 " ; } } if ( $ isSSL ) { $ host = " ssl:// $ host " ; } $ COOKIES = array ( ) ; foreach ( $ this -> cookies as $ name => $ value ) { $ COOKIES [ ] = " $ name = $ value " ; } if ( ! $ this -> cookies -> get ( 'roundcube_sessid' ) && $ this -> rcSessionID ) $ COOKIES [ ] = " roundcube_sessid= { $ this -> rcSessionID } " ; if ( ! $ this -> cookies -> get ( 'roundcube_sessauth' ) && $ this -> rcSessionAuth ) $ COOKIES [ ] = " roundcube_sessauth= { $ this -> rcSessionAuth } " ; $ COOKIES = ( $ COOKIES ) ? " Cookie:  " . join ( " ;  " , $ COOKIES ) . " \r \n " : " " ; if ( $ method == " POST " ) { $ request = " POST  " . $ path . "  HTTP/1.1 \r \n " . " Host:  " . $ _SERVER [ 'HTTP_HOST' ] . " \r \n " . " User-Agent:  " . $ _SERVER [ 'HTTP_USER_AGENT' ] . " \r \n " . " Content-Type: application/x-www-form-urlencoded \r \n " . " Content-Length:  " . strlen ( $ postData ) . " \r \n " . $ COOKIES . " Connection: close \r \n \r \n " . $ postData ; } else { $ request = " GET  " . $ path . "  HTTP/1.1 \r \n " . " Host:  " . $ _SERVER [ 'HTTP_HOST' ] . " \r \n " . " User-Agent:  " . $ _SERVER [ 'HTTP_USER_AGENT' ] . " \r \n " . $ COOKIES . " Connection: close \r \n \r \n " ; } $ fp = fsockopen ( $ host , $ port ) ; if ( ! $ fp ) { $ this -> addDebug ( " UNABLE TO OPEN SOCKET " , " Could not open socket for  $ host  at port  $ port " ) ; throw new RoundcubeLoginException ( " Could not open socket for  $ host  at port  $ port " ) ; } $ this -> addDebug ( " REQUEST " , $ request ) ; fputs ( $ fp , $ request ) ; $ response = " " ; while ( ! feof ( $ fp ) ) { $ line = fgets ( $ fp , 4096 ) ; if ( preg_match ( '/^HTTP\/1\.\d\s+404\s+/' , $ line ) ) throw new RoundcubeLoginException ( " No Roundcube installation found at ' $ path ' " ) ; if ( preg_match ( '/^Set-Cookie:\s*(.+roundcube_sessid=([^;]+);.+)$/i' , $ line , $ match ) ) { header ( $ line , false ) ; $ this -> addDebug ( " GOT SESSION ID " , " New session ID: ' $ match [ 2 ] '. " ) ; $ this -> rcSessionID = $ match [ 2 ] ; } if ( preg_match ( '/^Set-Cookie:.+roundcube_sessauth=([^;]+);/i' , $ line , $ match ) ) { header ( $ line , false ) ; $ this -> addDebug ( " GOT SESSION AUTH " , " New session auth: ' $ match [ 1 ] '. " ) ; $ this -> rcSessionAuthi = $ match [ 1 ] ; } if ( preg_match ( '/"request_token":"([^"]+)",/mi' , $ response , $ m ) ) $ this -> lastToken = $ m [ 1 ] ; if ( preg_match ( '/<input.+name="_token".+value="([^"]+)"/mi' , $ response , $ m ) ) $ this -> lastToken = $ m [ 1 ] ; $ response .= $ line ; } fclose ( $ fp ) ; $ this -> addDebug ( " RESPONSE " , $ response ) ; return $ response ; }
Send a POST/GET request to the Roundcube login-script
 to simulate the login.
Send a POST / GET request to the Roundcube login - script to simulate the login .
// Set hostname & port // Load cookies and save them in a key/value array // Add roundcube session ID if available // Create POST request with the given data // Make GET // Send request // Request // Read response and set received cookies // Not found // Got session ID! // Got sessauth // Request token (since Roundcube 0.5.1) // override previous token (if this one exists!)