vBulletin Login mit cURL

Ich brauchte kürzlich ein Methode mit der man einen vBulletin Login per cURL ausführen kann. Da sowas sicherlich für den Ein oder Anderen hilfreich ist stelle ich die Funktion hier zur Verfügung. Als Parameter werden Benutzername, Passwort und die URL des vBulletins übergeben und die Funktion übernimmt dann den Login eines Benutzer. Da Cookies gesetzt werden muss die Funktion vor der ersten Borwserausgabe aufgerufen werden.

Nützlich ist die Funktion zum Beispiel dann wenn man mit einem CMS und einem vBulletin arbeitet. Meldet sich ein User am CMS an kann man ihn mit der Funktion gleichzeigt beim vBulletin anmelden. Voraussetzung hierfür ist natürlich dass man die Benutzerdaten abgleicht.


/** function do_vbulletin_login
 *
 *  With this function you can login a user into a vbulletin board.
 *
 *  @param string vb_username The vbulletin-account username
 *  @param string vb_password The vbulletin-account password
 *  @param string vb_url    The url to the board
 */
function do_vbulletin_login($vb_username, $vb_password, $vb_url)
{

    $vb_username = trim($vb_username);
    $vb_password = trim($vb_password);
    $vb_password_md5 = md5($vb_password);

    if(strpos($vb_url, 'login.php') === false)
    {
        $vb_url = (substr($vb_url, -1) != '/') ? $vb_url . '/' : $vb_url;
        $vb_url = $vb_url . 'login.php?do=login';
    }

    $postfields = array(
        'vb_login_username' => $vb_username,
        'vb_login_password' => '',
        'cookieuser' => 1,
        's' => '',
        'do' => 'login',
        'vb_login_md5password' => $vb_password_md5,
        'vb_login_md5password_utf' => $vb_password_md5
    );

    $httpheaders = array();
    $httpheaders[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $httpheaders[] = "Accept-Language: en-us,en;q=0.5";
    $httpheaders[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";

    // do login and fetch return (with header)
    $curl = curl_init($vb_url);
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_ENCODING, '');
    curl_setopt($curl, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.16) Gecko/20080718 Ubuntu/8.04 (hardy) Firefox/2.0.0.16');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $httpheaders);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);

    $curl_return = curl_exec($curl);
    curl_close($curl);

    // get cookies from curl return:
    $curl_return = explode("\n", $curl_return);

    $cookies = array();
    foreach(array_keys($curl_return) as $i)
    {
        if(stripos($curl_return[$i], 'set-cookie') !== false)
        {
            $cookies[] = substr($curl_return[$i], 12);
        }
    }
    unset($curl_return);

    // prepare cookies to set at user:
    foreach($cookies as $key => $value)
    {
        $cookies[$key] = explode(';', $value);
        array_walk($cookies[$key] , create_function('&$temp', '$temp = trim($temp);'));
        foreach($cookies[$key] as $inner_key => $inner_value)
        {
            $temp = explode('=', $inner_value);
            if(count($temp) == 2 && ($temp[0] == 'path' || $temp[0] == 'expires' || $temp[0] == 'domain'))
            {
                $cookies[$key][$temp[0]] = $temp[1];
            }
            elseif(count($temp) == 2)
            {
                $cookies[$key]['name'] = $temp[0];
                $cookies[$key]['value'] = $temp[1];
            }

            if(count($temp) == 1 && $temp[0] == 'HttpOnly')
            {
                $cookies[$key]['HttpOnly'] = true;
            }
            else
            {
                $cookies[$key]['HttpOnly'] = false;
            }
            unset($cookies[$key][$inner_key]);
        }
    }

    // set cookies from server at user:
    $set_result = array();
    foreach($cookies as $cookie)
    {
        $set_result[] = setcookie($cookie['name'], $cookie['value'], (isset($cookie['expires'])) ? strtotime($cookie['expires']) : 0, $cookie['path'], $cookie['domain'], false, $cookie['HttpOnly']);
    }
}