This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/oauth/classes/Auth/Facebook.php
2013-05-23 17:01:28 +10:00

142 lines
3.2 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Authentication using Facebook
*
* @package OAuth
* @category Classes
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Auth_Facebook extends Auth_ORM {
// Our Facebook config data
private $config;
private $data;
// Our Facebook Object
private $fb;
private $me;
// Our OAuth Object
private $oo;
// Facebook UID
private $uid;
/**
* Perform the login processing
*
* We ignore password, since it is required in the parent(), we dont need it in Oauth
*/
protected function _login($user,$password,$remember) {
$this->complete_login($user);
if ($remember) {
$aoo = ORM::factory('Account_Oauth',array('account_id'=>$user->id));
// Record our user in the DB
$aoo->account_id = $user->id;
$aoo->oauth_id = $this->oo->id;
$aoo->userid = $remember->user_id();
switch ($this->oo->name) {
case 'facebook':
$aoo->oauth_data = $remember->fb->getAccessToken();
break;
}
return $aoo->save();
}
}
public function __construct(Model_Oauth $oo) {
include Kohana::find_file('vendor', 'facebook');
$this->oo = $oo;
// Load configuration "config/facebook"
$this->config = Kohana::$config->load('facebook');
parent::__construct((array)Kohana::$config->load('auth'));
// Create new Facebook object
$this->fb = new Facebook(array(
'appId' => $oo->app_id,
'secret' => $oo->secret,
'cookie' => $this->config->cookie,
'session_type' => $this->config->session_type,
));
try {
$this->me = $this->fb->api('/' . $this->fb->getUser(), 'GET');
} catch (FacebookApiException $e) {
// Do nothing.
}
}
/**
* Returns user data, default in case of failure.
*
* @param $key
* @param null $default
* @return mixed
* @throws FacebookApiException
*/
public function get($key,$default=NULL) {
if (! $uid = $this->user_id()) {
$this->login_url();
throw new FacebookApiException('User is not logged in.');
}
if (empty($this->data))
$this->data = $this->fb->api(array(
'method' => 'fql.query',
'query' => sprintf('SELECT %s FROM user WHERE uid = %s',$this->config_fields,$uid),
));
return (! empty($this->data[0][$key])) ? $this->data[0][$key] : $default;
}
/**
* Is user currently logged into facebook?
*/
public function logged_in($role=NULL,$debug=NULL) {
return $this->fb->getUser() ? TRUE : FALSE;
}
/**
* Creates a login url, based on scope, redirect_uri and display.
*
* @return string
*/
public function login_url() {
return urldecode($this->fb->getLoginUrl(array(
'scope' => $this->config->scope,
'redirect_uri' => $this->config->redirect_uri,
'display' => $this->config->display,
)));
}
/**
* Creates a logout url based on next.
*
* @return string
*/
public function logout_url() {
return urldecode($this->fb->getLogoutUrl(array('next'=>$this->config->next)));
}
/**
* Return user id if success, otherwise FALSE.
*/
public function user_id() {
if ($this->logged_in()) {
$this->uid = $this->fb->getUser();
return $this->uid;
} else {
return FALSE;
}
}
}
?>