Login   Register  
PHP Classes
elePHPant
Icontem

website integration example

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      PHPBB 3 Integration class  >  All threads  >  website integration example  >  (Un) Subscribe thread alerts  
Subject:website integration example
Summary:integration + some bug fixes
Messages:4
Author:Dawid Makowski
Date:2009-02-23 16:37:48
Update:2010-02-11 16:29:05
 

  1. website integration example   Reply   Report abuse  
Picture of Dawid Makowski
Dawid Makowski
2009-02-23 16:37:48
hello!

I have integrated phpBB3 with my running site (it has it's own auth system).
This is original class with some bug fixes found here and on internet + other + some more features - like avatar integration.

WARNING : this class assumes that Your website has an email as a login and a nickname which is used as a forum login. (On my forum I just redirect forum login form to my web login form). I also loads recent forum topics etc.

Examples of usage (user data update):
$phpbb_vars = array(
'user_id' => $phpbb_user->user_id,
'username' => $this->post_data['user']['nickname'],
'user_website' => $this->post_data['user']['url'],
'user_icq' => $this->post_data['user']['icq_login'],
'user_interests' => $this->post_data['user']['about_me_note'],
'user_from' => $this->post_data['user']['city'],
'user_birthday' => $this->post_data['user']['birth_date'],
);
$phpbb_result = $this->phpbb3_connector->user_update($phpbb_vars, $phpbb_user->username);
if ( isset($this->post_data['delete']) ) $_POST['delete'] = true;
$phpbb_av_result = $this->phpbb3_connector->avatar_upload();

Class code:

<?php

/**

PHPBB Forum manipulation Class

Idea By Felix Manea (felix.manea@gmail.com)

Licensed under LGPL

NOTE: You are required to leave this header intact.



Minor changes & (a lot of) bug fixes : Dawid Makowski http://neart.pl/ dawid@neart.pl

*/

class phpbb {

//various table fields

public $table_fields = array();



//constructor

// public function __construct($path, $php_extension = "php"){ // original constructor



// $params = array(); // NOT NULL!

// $params['phpbb3_path'] = '??';

// $params['php_scripts_extensions'] = 'php';



public function __construct($params){

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

define('IN_PHPBB', true);

$phpbb_root_path = $params['phpbb3_path'];

$phpEx = $params['php_scripts_extensions'];

}



//initialize phpbb

public function init($prepare_for_login = false){

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

if($prepare_for_login && !defined("IN_LOGIN")) define("IN_LOGIN", true);

require_once($phpbb_root_path.'common.'.$phpEx);



//session management

$user->session_begin();

$auth->acl($user->data);

}



//user_login

public function user_login($phpbb_vars)

{

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//prezumtia de fail

$phpbb_result = 'FAIL';



//general info

$this->init(true);



if(!isset($phpbb_vars['autologin'])) $phpbb_vars['autologin'] = false;

if(!isset($phpbb_vars['viewonline'])) $phpbb_vars['viewonline'] = 1;

if(!isset($phpbb_vars['admin'])) $phpbb_vars['admin'] = 0;



//validate and authenticate

$validation = login_db($phpbb_vars['username'], $phpbb_vars['password']);



if(

$validation['status'] == 3

&& $auth->login(

$phpbb_vars['username'],

$phpbb_vars['password'],

$phpbb_vars['autologin'],

$phpbb_vars['viewonline'],

$phpbb_vars['admin']

)

) $phpbb_result = 'SUCCESS';



return $phpbb_result;

}



//user_logout

public function user_logout(){

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//prezumtia de fail

$phpbb_result = "FAIL";



//general info

$this->init(true);



//session management

$user->session_begin();

$auth->acl($user->data);



//destroy session if needed

if($user->data['user_id'] != ANONYMOUS){

$user->session_kill();

$user->session_begin();

$phpbb_result = "SUCCESS";

}



return $phpbb_result;

}



//user_loggedin

public function user_loggedin()

{

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//fail presumtion

$phpbb_result = "FAIL";



//general info

$this->init(false);



//session management

$user->session_begin();

if(is_array($user->data) && $user->data["user_id"] != ANONYMOUS && $user->data["user_id"] > 0) $phpbb_result = "SUCCESS";

//if(is_array($user->data) && isset($user->data["user_id"]) && $user->data["user_id"] > 0) $phpbb_result = "SUCCESS";



return $phpbb_result;

}



public function get_user_data()

{

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;



//general info

$this->init(false);



//session management

$user->session_begin();



return $user->data;

}





public function get_avatar_real_filename($user_avatar)

{

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

$this->init(false);

require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);



$rv = null;

if ( isset($user_avatar) && strlen($user_avatar) ) {

$rv = get_avatar_filename($user_avatar);

}

return $rv;

}



public function avatar_upload()

{

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

$this->init(false);

require_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);

$error = null;

avatar_process_user($error);

return $error;

}



//user_add

public function user_add($phpbb_vars){

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//fail presumtion

$phpbb_result = "FAIL";



//if the mandatory parameters are not given fail

if(@empty($phpbb_vars['username']) || !isset($phpbb_vars['group_id']) || !isset($phpbb_vars['user_email']) )

return $phpbb_result;



//general info

$this->init(false);



//user functions

require_once($phpbb_root_path ."includes/functions_user.".$phpEx);



//default user info

$user_row = array(

"username" => $phpbb_vars["username"],

"user_password" => $phpbb_vars["password"],

"user_email" => $phpbb_vars["user_email"],

"group_id" => !isset($phpbb_vars["group_id"])?"2":$phpbb_vars["group_id"],

"user_timezone" => "1.00",

"user_dst" => 0,

"user_lang" => "pl",

"user_type" => !isset($phpbb_vars["user_type"])?"0":$phpbb_vars["user_type"],

"user_actkey" => "",

"user_dateformat" => "D M d, Y g:i a",

"user_style" => "1",

"user_regdate" => time(),

"user_colour" => "",

);



//replace default values with the ones in phpbb_vars array (not yet tested / implemented)

//foreach($user_row as $key => $value) if(isset($phpbb_vars[$key])) $user_row[$key] = $phpbb_vars[$key];



//register user

if($phpbb_user_id = user_add($user_row)) $phpbb_result = "SUCCESS";



//update the rest of the fields

$this->user_update($phpbb_vars);



return $phpbb_result;

}



//user_delete

public function user_delete($phpbb_vars){

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//fail presumtion

$phpbb_result = "FAIL";



//general info

$this->init(false);



//user functions

require_once($phpbb_root_path ."includes/functions_user.".$phpEx);



//get user_id if possible

if(!isset($phpbb_vars["user_id"]))

if(!$phpbb_vars["user_id"] = $this->get_user_id_from_name($phpbb_vars["username"]))

return $phpbb_result;



//delete user (always returns false)

user_delete("remove", $phpbb_vars["user_id"]);

$phpbb_result = "SUCCESS";



return $phpbb_result;

}



//user_update

public function user_update($phpbb_vars, $old_name)

{

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//fail presumtion

$phpbb_result = "FAIL";



//general info

$this->init(false);



//user functions

require_once($phpbb_root_path ."includes/functions_user.".$phpEx);



//get user_id if possible

// w produkcjach Hanza/Neart Email jest niezmienny



if(!isset($phpbb_vars["user_id"]))

if(!$phpbb_vars["user_id"] = $this->get_user_id_from_name($phpbb_vars["username"]))

return $phpbb_result;



$this->get_table_fields(USERS_TABLE);



$phpbb_vars['username_clean'] = utf8_clean_string($phpbb_vars['username']);

$ignore_fields = array("user_id");

//if(isset($phpbb_vars["user_password"])) $phpbb_vars["user_password"] = md5($phpbb_vars["user_password"]);

if(isset($phpbb_vars["user_newpasswd"])) $phpbb_vars["user_newpasswd"] = md5($phpbb_vars["user_newpasswd"]);

$sql = "";

//generate sql

for($i = 0;$i < count($this->table_fields[USERS_TABLE]); $i++)

if(isset($phpbb_vars[$this->table_fields[USERS_TABLE][$i]]) && !in_array($this->table_fields[USERS_TABLE][$i], $ignore_fields))

$sql .= ", ".$this->table_fields[USERS_TABLE][$i]." = '".$db->sql_escape($phpbb_vars[$this->table_fields[USERS_TABLE][$i]])."'";



if(strlen($sql) != 0){

$db->sql_query("UPDATE ".USERS_TABLE." SET ".substr($sql, 2)." WHERE user_id = '".$phpbb_vars["user_id"]."'");

$phpbb_result = "SUCCESS";

}



user_update_name($old_name, $phpbb_vars["username"]);

update_last_username();



return $phpbb_result;

}



//user_change_password

public function user_change_password($phpbb_vars){

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//fail presumtion

$phpbb_result = "FAIL";



//general info

$this->init(false);



//user functions

require_once($phpbb_root_path ."includes/functions_user.".$phpEx);



//get user_id if possible

if(!isset($phpbb_vars["user_id"]))

if(!$phpbb_vars["user_id"] = $this->get_user_id_from_name($phpbb_vars["username"]))

return $phpbb_result;



$db->sql_query("UPDATE ".USERS_TABLE." SET user_password = '".md5($phpbb_vars["password"])."' WHERE user_id = '".$phpbb_vars["user_id"]."'");

$phpbb_result = "SUCCESS";



return $phpbb_result;

}



private function get_table_fields($table){

//if already got table fields once

if(isset($this->table_fields[$table])) return true;



global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;



//general info

$this->init(false);



//get table fields

$this->table_fields[$table] = array();

/* CODE for MYSQL:

* $sql = "SHOW FIELDS FROM ".$table;

* if(!$result = $db->sql_query($sql)) return false;

while($row = $db->sql_fetchrow($result)) $this->table_fields[$table][] = $row["Field"];

*/



$sql = 'SELECT column_name ' .

'FROM information_schema.columns ' .

'WHERE table_name =\'' . $table . '\'';

if(!$result = $db->sql_query($sql)) return false;

while($row = $db->sql_fetchrow($result)) $this->table_fields[$table][] = $row["column_name"];

$db->sql_freeresult($result);



return true;

}



//get user id if we know username

public function get_user_id_from_name($username){

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;



//user functions

require_once($phpbb_root_path ."includes/functions_user.".$phpEx);



$user_id = false;

if(!isset($username)) return false;

user_get_id_name($user_id, $username);

if(!isset($user_id[0])) return false;

return $user_id[0];

}



public function get_recent_topics($topics_count = 10)

{

$rv = array();

global $phpbb_root_path, $phpEx, $db, $config, $user, $auth, $cache, $template;

//general info

$this->init(false);

$sql = 'SELECT * FROM phpbb_topics ORDER BY topic_time DESC LIMIT ' . $topics_count;

if(!$result = $db->sql_query($sql)) return false;

while($row = $db->sql_fetchrow($result)) $rv[] = $row;

$db->sql_freeresult($result);



return $rv;

}



}


  2. Re: website integration example   Reply   Report abuse  
Picture of Felix Manea
Felix Manea
2009-06-16 06:37:00 - In reply to message 1 from Dawid Makowski
Thank you for this class update. If you want, you can send it to me with the update specifications (if possible) and I can make a beta upgrade to the real class so users can test it and enjoy the new features you have added. Of course you will be credited for all the additions :).

  3. Re: website integration example   Reply   Report abuse  
Picture of Bhupesh Upadhyay
Bhupesh Upadhyay
2009-11-04 06:15:52 - In reply to message 2 from Felix Manea
sir,
i want to know where i put these class. i have my own website with some user authentication and phpbb3 have its own user authentication . so i want when a user login through my website it automatically logged in the forum .Is that possible ? if it is please reply .
thanks in advance

  4. Re: website integration example   Reply   Report abuse  
Picture of Tim Mikula
Tim Mikula
2010-02-11 16:29:05 - In reply to message 1 from Dawid Makowski
This class is just what I need, but reading all the forum posts, I am not sure where the latest and most bug free code can be found. Is it the post by Dawid Makowski on 2/23/09? Of the author's code post on 4/27/09 as part of the package? Or is there a better version. I don't want to fix all the bugs from the community again.

Thanks in advance,
Tim
tmikula@lakesidecoders.com