I’m developing Facebook apps.
My goal is to copy the Facebook’s The Run Around (The Run Around) connect mechanism into my application.
Please be warned, that this is my first PHP application. So, please bear with me if there’re a lot of unusual PHP method/convention/way in my code.
After googling,compare some framework, I decide to pick Yii as my first PHP framework. From my point of view, Yii is very easy to setup as web-apps. It’s backed with very good documentation. But, because it’s pretty new, I find there’re only few implementations, or maybe a ‘hack’ to the Yii framework. Specially in Facebook integration, that currently I’m working on.
So, I would like to get your opinion on my method on integrating Facebook Connect into Yii Framework.
So this is my workflow:
-
Download the Facebook client to protected/components
-
Create a HFacebook class to wrap the facebook
<?php
Yii::import('application.components.facebook.Facebook');
class HFacebook {
static public $callback_url='xd_receiver.html';
static public $api_key='thekey';
static public $secret='thesecret';
static function isFBML(){
return (isset($_POST['fb_sig_in_canvas']) || $_POST['fb_sig_in_canvas']==1);
}
static function facebook_client() {
static $_facebook = null;
if ($_facebook === null) {
$_facebook = new Facebook(self::$api_key, self::$secret);
if (!$_facebook) {
Yii::log('Could not create facebook client.','error','HFacebook');
}
}
return $_facebook;
}
static function init(){
$GLOBALS['facebook_config']['debug'] = NULL;
//return new Facebook(self::$api_key, self::$secret);
return self::facebook_client();
}
static function beforeActionController($controller){
Yii::trace('beforecontrol','HFacebook');
if (isset($controller->facebook))
Yii::trace('Controllers facebook object is set','HFacebook');
//$fb_user=$controller->facebook->require_login();
$fb_uid=$controller->facebook->get_loggedin_user();
Yii::trace('fb_uid:'.$fb_uid,'HFacebook');
if($fb_uid){
$user=Users::model()->find('fb_uid=?',array($fb_uid));
if(!Yii::app()->user->isGuest && $user!=null){
Yii::trace('Logout, and use FB User that exist in DB','HFacebook');
Yii::app()->user->logout();
$identity=new UserIdentity($user->username,$user->password);
Yii::app()->user->login($identity);
Yii::app()->user->setId($user->idusr);
}else if($user==null){
Yii::trace('Add new FB user into DB','HFacebook');
$user = new Users;
$user->username='FacebookUser_'.$fb_uid;
$user->name='FacebookUser_'.$fb_uid;
$user->password=md5('FacebookUser_'.$fb_uid);
$user->email='';
$user->fb_uid=$fb_uid;
$user->email_hash='';
if($user->save()){
$identity=new UserIdentity($user->username,$user->password);
Yii::app()->user->login($identity);
Yii::app()->user->setId($user->idusr);
}
}else if($user!=null && Yii::app()->user->isGuest){
Yii::trace('Autologon with existing FB user','HFacebook');
$identity=new UserIdentity($user->username,$user->password);
Yii::app()->user->login($identity);
Yii::app()->user->setId($user->idusr);
}
}
}
}
?>
- Create a base protected/components/HController.php class, all controller classes will be extend-ed from this base class.
<?php
class HController extends CController{
public $facebook;
public function __construct($id,$module=null)
{
parent::__construct($id,$module);
$this->facebook = HFacebook::init();
}
public function redirect($url,$terminate=true,$statusCode=302){
if (HFacebook::isFBML()){
echo '<fb:redirect url="'.$url.'"/>';
}
parent::redirect($url,$terminate,$statusCode);
}
function beforeAction($action) {
$conact=$this->id.'/'.$this->getAction()->id;
if($conact!='site/logout' && $conact!='site/index'){
Yii::trace('Execute Facebook component','HController');
HFacebook::beforeActionController($this);
}
return parent::beforeAction($action);
}
}
?>
- Modiy the protected/controller/SiteController.php
class SiteController extends HController
- Modify the protected/views/layout/main.php
<?php if (!isset($_POST['fb_sig_in_canvas'])){?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />
<title><?php echo $this->pageTitle; ?></title>
</head>
<body>
<?php }/* -------------------------------------*/ ?>
<div id="page">
....nomodificationonthispart....
</div><!-- page -->
<?php if (!isset($_POST['fb_sig_in_canvas'])){/* -------------------------------------*/?>
<?php $conact=$this->id.'/'.$this->getAction()->id;
if($conact!='site/logout' && $conact!='site/index' && $conact!='site/login'){ ?>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/id_ID" type="text/javascript"></script>
<script type="text/javascript">
FB.init("<?php echo HFacebook::$api_key; ?>", "<?php echo HFacebook::$callback_url; ?>");
</script>
<?php } ?>
</body>
</html>
<?php } ?>
- Modify the protected/views/site/login.php
...theendofthefile...
<fb:login-button autologoutlink="true" size="medium" background="light" length="long" ></fb:login-button>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/id_ID" type="text/javascript"></script>
<script type="text/javascript">
FB.init("<?php echo HFacebook::$api_key; ?>", "<?php echo HFacebook::$callback_url; ?>",{"reloadIfSessionStateChanged":true});
</script>
Please let me know what you all think about this method (or ‘hack’?).