2012-02-05 30 views
0

我有一個使用Rails/MySQL作爲後端的移動應用程序(只提供JSON,我知道我們不需要全面的Rails,但這是最簡單的入門解決方案)。我的Rails應用程序使用設計進行身份驗證。我希望我的用戶能夠訪問Phpbb3論壇,而無需再次註冊。什麼是最好的方法來做到這一點? Phpbb3論壇是否從相同的MySQL中讀取帳戶?如何根據Rails應用中的帳戶自動在phpBB3論壇中登錄用戶?

回答

1

使用電子郵件地址作爲基礎。

名稱的文件中包含/ UCP稱爲ucp_my_rails_app_connect.php

<?php 
/* 
* @package  My Package 
* @author  Me 
* @license  http://opensource.org/licenses/gpl-license.php GNU Public License 
* @link  my href 
* @copyright (c) my copyright 
* 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 

* 
*/ 
/* 
* @ignore 
*/ 
if (!defined('IN_PHPBB')) 
{ 
    exit; 
} 
    /* 
    * ucp_myclass 
    * my rails app connect 
    * @package  my package 
    */ 
class ucp_my_rails_app_connect 
{ 
    var $u_action; 

    function main($id, $mode) 
    { 
    global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx; 

    /** Do some DB code here for rails or wrap it in a private function*/ 

    $server_url = generate_board_url(); 
    $key_len = 54 - strlen($server_url); 
    $key_len = max(6, $key_len); // we want at least 6 
    $key_len = ($config['max_pass_chars']) ? min($key_len, $config['max_pass_chars']) : $key_len; // we want at most $config['max_pass_chars'] 
    $user_actkey = substr(gen_rand_string(10), 0, $key_len); 
    $new_user_password = gen_rand_string(8); 
    $data = array(
     'username'   => utf8_normalize_nfc(/** rails DB username*/), 
     'steam_id'   => request_var('steam_id', ''),    
     'new_password' => $new_user_password, 
     'password_confirm' => $new_user_password,   
     'email'    => strtolower(/** rails DB email*/), 
     'email_confirm'  => strtolower(/** rails DB email*/) 
    );   
    if($my_rails_exec_func == $some_val) /* make some code so not just anyone can submit stuff to this area*/ 
    {  
     //Check and initialize some variables if needed 
     $error = validate_data($data, array(
     'username'   => array(
      array('string', false, $config['min_name_chars'], $config['max_name_chars']), 
      array('username', '')), 
     'new_password'  => array(
      array('string', false, $config['min_pass_chars'], $config['max_pass_chars']), 
      array('password')), 
     'password_confirm' => array('string', false, $config['min_pass_chars'], $config['max_pass_chars']), 
     'email'    => array(
      array('string', false, 6, 60), 
      array('email')), 
     'email_confirm'  => array('string', false, 6, 60), 
     'tz'    => array('num', false, -14, 14), 
     'lang'    => array('match', false, '#^[a-z_\-]{2,}$#i'), 
    )); 
     $error = preg_replace('#^([A-Z_]+)$#e', "(!empty(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '\\1'", $error); 

     if (!sizeof($error)) 
     { 
     // Which group by default? 
     $group_name = ($coppa) ? 'REGISTERED_COPPA' : 'REGISTERED'; 
     $sql = 'SELECT group_id 
      FROM ' . GROUPS_TABLE . " 
      WHERE group_name = '" . $db->sql_escape($group_name) . "' 
      AND group_type = " . GROUP_SPECIAL; 
     $result = $db->sql_query($sql); 
     $row = $db->sql_fetchrow($result); 
     $db->sql_freeresult($result); 

     $group_id = $row['group_id']; 
     if (($config['require_activation'] == USER_ACTIVATION_SELF || 
      $config['require_activation'] == USER_ACTIVATION_ADMIN) && $config['email_enable']) 
     { 
      $user_actkey = gen_rand_string(mt_rand(6, 10)); 
      $user_type = USER_INACTIVE; 
      $user_inactive_reason = INACTIVE_REGISTER; 
      $user_inactive_time = time(); 
     } 
     else 
     { 
      $user_type = USER_NORMAL; 
      $user_actkey = ''; 
      $user_inactive_reason = 0; 
      $user_inactive_time = 0; 
     } 
     $user_row = array(
      'username'    => $data['username'], 
      'user_password'   => phpbb_hash($data['new_password']), 
      'user_email'   => $data['email'], 
      'group_id'    => (int) $group_id, 
      'user_timezone'   => (float) $data['tz'], 
      'user_dst'    => $is_dst, 
      'user_lang'    => $data['lang'], 
      'user_type'    => $user_type, 
      'user_actkey'   => $user_actkey, 
      'user_ip'    => $user->ip, 
      'user_regdate'   => time(), 
      'user_inactive_reason' => $user_inactive_reason, 
      'user_inactive_time' => $user_inactive_time, 

     ); 
     if ($config['new_member_post_limit']) 
     { 
      $user_row['user_new'] = 1; 
     } 
     // Register user... 
     $user_id = user_add($user_row); 
     // This should not happen, because the required variables are listed above... 
     if ($user_id === false) 
     { 
      trigger_error('NO_USER', E_USER_ERROR); 
     } 

     // DB Error 
     if(!$result) 
     { 
      trigger_error('Unable to connect with phpBB database.'); 
     } 

     // Okay, captcha, your job is done. 
     if ($config['enable_confirm'] && isset($captcha)) 
     { 
      $captcha->reset(); 
     } 
     if ($coppa && $config['email_enable']) 
     { 
      $message = $user->lang['ACCOUNT_COPPA']; 
      $email_template = 'coppa_welcome_inactive_steam'; 
     } 
     else if ($config['require_activation'] == USER_ACTIVATION_SELF && $config['email_enable']) 
     { 
      $message = $user->lang['ACCOUNT_INACTIVE']; 
      $email_template = 'user_welcome_inactive_steam'; 
     } 
     else if ($config['require_activation'] == USER_ACTIVATION_ADMIN && $config['email_enable']) 
     { 
      $message = $user->lang['ACCOUNT_INACTIVE_ADMIN']; 
      $email_template = 'admin_welcome_inactive_steam'; 
     } 
     else 
     { 
      $message = $user->lang['ACCOUNT_ADDED']; 
      $email_template = 'user_welcome_steam'; 
     } 
     if ($config['email_enable']) 
     { 
      include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx); 
      $messenger = new messenger(false); 
      $messenger->template($email_template, $data['lang']); 
      $messenger->to($data['email'], $data['username']); 
      $messenger->headers('X-AntiAbuse: Board servername - ' . $config['server_name']); 
      $messenger->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']); 
      $messenger->headers('X-AntiAbuse: Username - ' . $user->data['username']); 
      $messenger->headers('X-AntiAbuse: User IP - ' . $user->ip); 
      $messenger->assign_vars(array(
      'WELCOME_MSG' => htmlspecialchars_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename'])), 
      'USERNAME'  => htmlspecialchars_decode($data['username']), 
      'U_ACTIVATE' => "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey") 
     ); 
      if ($coppa) 
      { 
      $messenger->assign_vars(array(
       'FAX_INFO'  => $config['coppa_fax'], 
       'MAIL_INFO'  => $config['coppa_mail'], 
       'EMAIL_ADDRESS' => $data['email']) 
      ); 
      } 
      $messenger->send(NOTIFY_EMAIL); 
      if ($config['require_activation'] == USER_ACTIVATION_ADMIN) 
      { 
      // Grab an array of user_id's with a_user permissions ... these users can activate a user 
      $admin_ary = $auth->acl_get_list(false, 'a_user', false); 
      $admin_ary = (!empty($admin_ary[0]['a_user'])) ? $admin_ary[0]['a_user'] : array(); 
      // Also include founders 
      $where_sql = ' WHERE user_type = ' . USER_FOUNDER; 
      if (sizeof($admin_ary)) 
      { 
       $where_sql .= ' OR ' . $db->sql_in_set('user_id', $admin_ary); 
      } 
      $sql = 'SELECT user_id, username, user_email, user_lang, user_jabber, user_notify_type 
       FROM ' . USERS_TABLE . ' ' . 
       $where_sql; 
      $result = $db->sql_query($sql); 
      while ($row = $db->sql_fetchrow($result)) 
      { 
       $messenger->template('admin_activate', $row['user_lang']); 
       $messenger->to($row['user_email'], $row['username']); 
       $messenger->im($row['user_jabber'], $row['username']); 
       $messenger->assign_vars(array(
       'USERNAME'   => htmlspecialchars_decode($data['username']), 
       'U_USER_DETAILS' => "$server_url/memberlist.$phpEx?mode=viewprofile&u=$user_id", 
       'U_ACTIVATE'  => "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey") 
      ); 
       $messenger->send($row['user_notify_type']); 
      } 
      $db->sql_freeresult($result); 
      } 
     } 
     $message = $message . '<br /><br />' . sprintf($user->lang['RETURN_INDEX'], '<a href="' . append_sid("{$phpbb_root_path}index.$phpEx") . '">', '</a>'); 
     trigger_error($message); 
     } 
    } 
    }  
} 

?>

現在我們添加了類ucp.php

case 'register': 
    if ($user->data['is_registered'] || isset($_REQUEST['not_agreed'])) 
    { 
     redirect(append_sid("{$phpbb_root_path}index.$phpEx")); 
    } 

    $module->load('ucp', 'register'); 
    $module->display($user->lang['REGISTER']); 
break; 
case 'my_rails_app_connect': 
    if ($user->data['is_registered']) 
    { 
     redirect(append_sid("{$phpbb_root_path}index.$phpEx")); 
    } 

    $module->load('ucp', 'my_rails_app_connect'); 
    $module->display($user->lang['REGISTER']); 
break; 

現在我們添加一個登錄導軌應用程序

創建一個名爲railsapp.php的文件

<?php define('IN_PHPBB', true); 
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../'; 
$phpEx = substr(strrchr(__FILE__, '.'), 1); 
// Load include files. 
include($phpbb_root_path . 'common.' . $phpEx); 
include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx); 
// Set up a new user session. 
$user->session_begin(); 
$auth->acl($user->data); 
$user->setup('ucp'); 

$my_rails_user_email = some_code_to_get_user_email_from_rails_database; //maybe use a cookie or make the user allow the phpBB script access to the rails DB or make them login into the rails app 

$mysql = 'SELECT user_id 
     FROM ' . USERS_TABLE 
     . " WHERE user_email='$my_user_rails_email'"; 
// Execute the query. 
$result = $db->sql_query($sql); 
// Retrieve the row data. 
$row = $db->sql_fetchrow($result); 
// Free up the result handle from the query. 
$db->sql_freeresult($result); 
// Check to see if we found a user_id with the associated Facebook Id. 
if ($row) // User is registered already, let's log him in! 
{ 
    // Check for user ban. 
    if($user->check_ban($row['user_id'])) 
    { 
     trigger_error($user->lang['BAN_TRIGGERED_BY_USER']); 
    } 

    // Log user in. 
    $result = $user->session_create($row['user_id'], 0, 0, 1); 

    // Alert user if we failed to log them in. 
    if(!$result) 
    { 
     trigger_error($user->lang['LOGIN_FAILURE']); 
    } 

    $redirect = $phpbb_root_path . 'index.' . $phpEx; 
    $message = ($l_success) ? $l_success : $user->lang['LOGIN_REDIRECT']; 
    $l_redirect = ($admin) ? $user->lang['PROCEED_TO_ACP'] : (($redirect === "{$phpbb_root_path}index.$phpEx" || $redirect === "index.$phpEx") ? $user->lang['RETURN_INDEX'] : $user->lang['RETURN_PAGE']); 
    // append/replace SID (may change during the session for AOL users) 
    $redirect = reapply_sid($redirect); 
    // Special case... the user is effectively banned, but we allow founders to login 
    if (defined('IN_CHECK_BAN') && $result['user_row']['user_type'] != USER_FOUNDER) 
    { 
    return; 
    } 
    $redirect = meta_refresh(3, $redirect); 
    trigger_error($message . '<br /><br />' . sprintf($l_redirect, '<a href="' . $redirect . '">', '</a>')); 
} 
?> 

在款式/ your_template_name /模板index_body.html /加

<a href="http://www.myboardurl.com/railsapp.php">Connect With Rails</a> 

如果您需要幫助只是MY phpBB mod support forums to discuss further

+0

也下降到從phpBB的通話HTTP內訪問文件:// www.myhref.com/ucp.php?mode=my_rails_app_connect – 2012-02-05 10:51:06

相關問題