Selasa, 18 Januari 2011

Logging into vBulletin using cURL

 Lets Begin by Creating a new PHP Document

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/
?>
Now lets crate a Function in our Document. We should call it vBulletinLogin() and Pass 2 variables. These Variables shall be $user and $pass

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{

}
?>
Now we have our function created we need it to start doing something.
First of all we will need to MD5 The $pass variable as vBulletin submits the MD5 of the password before the server begins to process it.

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{
       $md5Pass = md5($pass);
}
?>
Now we have the MD5 of the password submitted we need to create the post data we are going to submit using cURL. I usually do this by using a variable as it makes it easier to read later.

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{
       $md5Pass = md5($pass);
       $data = "do=login&url=%2Findex.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";
}
?>
Notice the syntax of the POST is Similar to a GET.

Now we have the data we should start to Work with our cURL options But first we need to activate cURL this is done by Defininf a variable using curl_init();

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{
       $md5Pass = md5($pass);
       $data = "do=login&url=%2Findex.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";

      $ch = curl_init();
}
?>
Now we have cURL Initialized lets start setting out Options. This is done using the curl_setopt() Function of PHP. I am not going into detail about each cURL option but will show you the Syntax of curl_setopt()

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{
       $md5Pass = md5($pass);
       $data = "do=login&url=%2Findex.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";

       $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, "h**p://forum.codecall.net/login.php?do=login"); // replace ** with tt
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt ($ch, CURLOPT_TIMEOUT, '10');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec ($ch);
}
?>
Now we have the Page Returned and it is stored in the Variable $store we can close cURL using curl_close()

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{
       $md5Pass = md5($pass);
       $data = "do=login&url=%2Findex.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";

       $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, "h**p://forum.codecall.net/login.php?do=login"); // replace ** with tt
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt ($ch, CURLOPT_TIMEOUT, '10');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec ($ch);

        curl_close($ch);
}
?>
Now we can check to see if we Logged in successfully. To do this we search the page for the string "thank you for logging in" using an IF statement and the strpos function

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{
       $md5Pass = md5($pass);
       $data = "do=login&url=%2Findex.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";

       $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, "h**p://forum.codecall.net/login.php?do=login"); // replace ** with tt
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt ($ch, CURLOPT_TIMEOUT, '10');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec ($ch);

        curl_close($ch);

       $pos = strpos($store, "Thank you for Logging in");
       if($pos === FALSE) {
               RETURN 0;
       }
       else
       {
               RETURN 1;
        }
}
?>
Now if the Function finds we are logged in it will return 1 or TRUE
To call it we can do something like this

Code:
<?php
/********************
* cURL Tutorial By Affix
* Login to a vB forum
*********************/

function vBulletinLogin($user, $pass)
{
       $md5Pass = md5($pass);
       $data = "do=login&url=%2Findex.php&vb_login_md5password=$md5Pass&vb_login_username=$user&cookieuser=1";

       $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, "h**p://forum.codecall.net/login.php?do=login"); // replace ** with tt
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt ($ch, CURLOPT_TIMEOUT, '10');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/codecall_$user.txt");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $store = curl_exec ($ch);

        curl_close($ch);

       $pos = strpos($store, "Thank you for Logging in");
       if($pos === FALSE)
      {
               RETURN 0;
       }

Tidak ada komentar:

Posting Komentar