views:

112

answers:

3

Hi, I am trying to install the pligg twitter login module from: http://blog.eamonnfaherty.co.uk/2009/08/08/log-into-pligg-using-twitter-oauth/

I downloaded the zip from the above link and followed all the instructions. Then I realized one of the files are using PEAR, which is not provided by my hosting company.

The entire code from confirm.php is here:

Can some one show me how to rewrite this code so I don't have to use PEAR. Is that even possible. I am a really novice with php. Please forgive me...

<?php
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
require_once 'DB.php';

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);

$twitterObj->setToken($_GET['oauth_token']);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);

try {
    setcookie('oauth_token', $token->oauth_token);
    setcookie('oauth_token_secret', $token->oauth_token_secret);

    $twitterInfo= $twitterObj->get_accountVerify_credentials();
    $twitterUserName = $twitterInfo->screen_name;
    $twitterAvatar = $twitterInfo->profile_image_url;
} catch (Exception $e) {
    die("Sorry, there was an error connecting to twitter:".$e->getMessage());
}

$DB =& DB::connect('mysqli://USER:PASS@localhost/DB');
if (DB::isError($DB))
{
    echo 'Cannot connect to database: ' . $DB->getMessage();
}
else
{
    $query = 'select twitter_user_name, pligg_user_name, pligg_password FROM twitter_user_map where twitter_user_name = ?';
    $result = $DB->query($query, $twitterUserName);
    if (DB::isError ($result)) {
             die ("Select failed: " . $result->getMessage () . "\n");
        }
    $userDetails = $result->fetchRow(DB_FETCHMODE_OBJECT);
    if ($result->numRows() == 0) {
        createAccount($DB,$twitterUserName, 0, $twitterUserName);
    } else {
        redirectToLogin($userDetails->pligg_user_name, $userDetails->pligg_password);
    }
}

function createAccount($DB, $username, $delta, $twitterUsername) {
    $pass = genRandomString();
    $query = "insert into twitter_user_map( twitter_user_name, pligg_user_name, pligg_password) values ('$twitterUsername', '$username','$pass' )";
    $result = $DB->query($query);
    if (DB::isError ($result)) {
         die ("INSERT failed: " . $result->getMessage () . "\n");
    }
    if ($DB->affectedRows() == 0) {
        $newUsername = $twitterUsername;
        if ($delta != 0) {
            $newUsername = $username . "" . $delta;
        }
        createAccount($DB,$newUsername, $delta + 1, $twitterUsername);
    } else {
        redirectToRegister($username, $pass);
    }
}

function redirectToRegister($username, $pass) {
    print "redirectToRegister";
    $postdata = http_build_query(
        array(
            'reg_username' => $username,
            'reg_email' => "",
            'reg_password' => $pass,
            'reg_password2' => $pass,
            'recaptcha_challenge_field' => '02kOXNvO91qx4TJ6dC8evG6SkqQvGlUfjxF8bvaurguiAsftwQYut68EfNxZh6ZYMTyqcrWNT4RooYxxfjueRVFIkcN_UwRI-J6bjWZczbLk4p0Tqml6tVHQeyocVvU0SwUKUn_kmtDV4Y7kGfbn-qyiYt55-iaFojc060MJ-jAZ68z5Vlw8xrvPRhLW6JAO1F2D6oAY7vsWI_e1Nmhww1lQ6qsL10W4wWrCWLywOIZVIZnsa5p61_IQf9Yn_NV-Nir_DCWxKMUZieZkL1pril6_kMaj0B',
            'recaptcha_response_field' => '',
            'regfrom' => "full",
            'from_external' => "1"
        )
    );
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
    $context  = stream_context_create($opts);
    $result = file_get_contents('sitename/register.php', false, $context);
    print $result;
}

function redirectToLogin($username, $password) {
    $postdata = http_build_query(
        array(
            'username' => $username,
            'password' => $password,
            'persistent' => "on",
            'from_external' => "1"
        )
    );
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
    $context  = stream_context_create($opts);
    $result = file_get_contents('sitename/login.php', false, $context);
    print $result;
}

function genRandomString() {
    $length = 10;
    $characters = '123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

?>
+5  A: 

PEAR is nothing more than another PHP script and can be installed to the any hosting manually. And I guess there is no way to "show a way to rewrite", this is more likely "rewrite this code for me" question.

or even pligg promotion question

Col. Shrapnel
A: 

EAR DB Basics

Example 8-1 is a program to build an HTML table of information about James Bond movies. It demonstrates how to use the PEAR DB library (which comes with PHP) to connect to a database, issue queries, check for errors, and transform the results of queries into HTML. The library is object-oriented, with a mixture of class methods (DB::connect( ), DB::iserror( )) and object methods ($db->query( ), $q->fetchInto( )).

Yeah. I guess it is not that hard to re-write this thing. I will figure it out. Thanks guys

http://docstore.mik.ua/orelly/webprog/php/ch08_03.htm

pligg
A: 

Not answering your question as such, but why don't you do a local install of PEAR? Here's how...

http://pear.php.net/manual/en/installation.shared.php

kguest