如何在不使用PEAR :: DB的情况下重新编写此代码?

时间:2010-03-07 07:29:51

标签: php twitter oauth pear

我正在尝试从 Pligg 安装Log into Pligg using Twitter OAuth Twitter登录模块。

我从上面的页面下载了ZIP文件,并按照所有说明进行操作。然后我意识到其中一个文件正在使用PEAR,这是我的托管公司没有提供的。

confirm.php的完整代码在这里:

如何重写此代码,以便我不必使用PEAR?这甚至可能吗?我是PHP的新手。

<?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;
}

?>

更新

来自PEAR DB Basics

  

示例8-1是用于构建关于詹姆斯邦德电影的HTML表格的程序。它演示了如何使用PEAR DB库(PHP附带)连接到数据库,发出查询,检查错误以及将查询结果转换为HTML。该库是面向对象的,混合使用类方法(DB :: connect(),DB :: iserror())和对象方法($ db-&gt; query(),$ q-&gt; fetchInto())

呀。我想重新写这个东西并不难。我会搞清楚的。

1 个答案:

答案 0 :(得分:6)

PEAR只不过是另一个PHP脚本,可以手动安装到任何托管。 我想没有办法“显示重写的方法”,这更有可能“为我重写这段代码”的问题。

甚至是pligg促销问题