/[drupal]/contributions/modules/facebook_auth/facebook_auth.module
ViewVC logotype

Diff of /contributions/modules/facebook_auth/facebook_auth.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.9, Thu Sep 6 07:25:03 2007 UTC revision 1.9.4.1, Mon Feb 18 06:40:45 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: facebook_auth.module,v 1.8 2007/09/06 07:07:13 halkeye Exp $  // $Id: facebook_auth.module,v 1.9 2007/09/06 07:25:03 halkeye Exp $
3  if (substr(phpversion(), 0, 1) == '4') {  if (substr(phpversion(), 0, 1) == '4') {
4     if (file_exists(dirname(__FILE__) . '/facebook-platform/php4client/facebook.php')) {     if (file_exists(dirname(__FILE__) . '/facebook-platform/php4client/facebook.php')) {
5        require_once('facebook-platform/php4client/facebook.php');       require_once('facebook-platform/php4client/facebook.php');
6     }     }
7     else {     else {
8        drupal_set_message('Platform needs to be installed. See readme file');       drupal_set_message('Platform needs to be installed. See readme file');
9     }     }
10  }  }
11  else {  else {
12     if (file_exists(dirname(__FILE__) . '/facebook-platform/client/facebook.php')) {     if (file_exists(dirname(__FILE__) . '/facebook-platform/client/facebook.php')) {
13        require_once('facebook-platform/client/facebook.php');       require_once('facebook-platform/client/facebook.php');
14     }     }
15     else {     else {
16        drupal_set_message('Platform needs to be installed. See readme file');       drupal_set_message('Platform needs to be installed. See readme file');
17     }     }
18  }  }
19    
20  function facebook_auth_help($section) {  function facebook_auth_help($section) {
21     switch($section) {     switch($section) {
22        case 'admin/settings':       case 'admin/settings':
23           return t('Configure your API and Secret Key provided by facebook.');         return t('Configure your API and Secret Key provided by facebook.');
24        case 'admin/settings/facebook_auth':       case 'admin/settings/facebook_auth':
25           return t('Sign up for a new application at !url. Fill in provided keys here.',         return t('Sign up for a new application at !url. Fill in provided keys here.',
26              array('!url'=>l(t('facebook developer page'), 'http://www.facebook.com/developers/'))           array('!url'=>l(t('facebook developer page'), 'http://www.facebook.com/developers/'))
27           );         );
28     }     }
29  }  }
30    
31  function facebook_auth_menu($may_cache) {  function facebook_auth_menu() {
    global $user;  
   
32     $items = array();     $items = array();
33     if ($may_cache) {     $items['admin/settings/facebook_auth'] = array(
34        $items[] = array(           'title' => 'Configure Facebook settings',
35           'path' => 'admin/settings/facebook_auth',           'page callback' => 'drupal_get_form',
36           'title' => t('Configure Facebook settings'),           'page arguments' => array('facebook_auth_admin'),
37           'callback' => 'drupal_get_form',           'access callback' => 'user_access',
38           'callback arguments' => 'facebook_auth_admin',           'access arguments' => array('administer facebook'),
          'access' => user_access('administer facebook'),  
39           'type' => MENU_NORMAL_ITEM,           'type' => MENU_NORMAL_ITEM,
40        );           );
41        $items[] = array(  
42           'path' => 'facebook',     $items['facebook'] = array(
43           'title' => t('my Facebook account'),           'title' => 'my Facebook account',
44           'callback' => 'facebook_auth',           'page callback' => 'facebook_auth',
45           // There's no real reason to have this disallowed unless you don't want facebook auth, in that case, you should           // There's no real reason to have this disallowed unless you don't want facebook auth, in that case, you should
46           // Probably just disable the module           // Probably just disable the module
47           'access' => TRUE,           'access callback' => TRUE,
48           'type' => MENU_CALLBACK           'type' => MENU_CALLBACK
49        );     );
50       $items['user/%user/facebook_auth'] = array(
51     }           'title' => 'Facebook Identities',
52     else {           'page callback' => 'facebook_auth_user_identities',
53        if (arg(0) == 'user' && is_numeric(arg(1))) {           'page arguments' => array(1),
54           $account = user_load(array('uid' => arg(1)));           'access callback' => 'facebook_auth_access_user',
55             'access arguments' => array(1),
56           if ($account->uid) {           'type' => MENU_LOCAL_TASK,
57              global $user;           //'file' => 'tracker.pages.inc',
58       );
59       $items['user/%user/facebook_auth/delete'] = array(
60             'title' => t('Delete Facebook'),
61             'page callback' => 'facebook_auth_user_delete',
62             'page arguments' => array(1),
63             'access callback' => 'facebook_auth_access_user',
64             'access arguments' => array(1),
65             'type' => MENU_CALLBACK
66             //'file' => 'tracker.pages.inc',
67       );
68    
             $access = (user_access('administer users') || $user->uid == arg(1));  
             $items[] = array(  
                'path' => 'user/'. arg(1) .'/facebook_auth',  
                'title' => t('Facebook Identities'),  
                'callback' => 'facebook_auth_user_identities',  
                'callback arguments' => array($account),  
                'access' => $access,  
                'type' => MENU_LOCAL_TASK  
             );  
             $items[] = array(  
                'path' => 'user/'. arg(1) .'/facebook_auth/delete',  
                'title' => t('Delete Facebook'),  
                'callback' => 'facebook_auth_user_delete',  
                'callback arguments' => array($account),  
                'access' => $access,  
                'type' => MENU_CALLBACK  
             );  
          }  
       }  
    }  
69     return $items;     return $items;
70  }  }
71    
72    function facebook_auth_access_user($account) {
73       $access = (user_access('administer users') || $GLOBALS['user']->uid == $account->uid);
74       return (bool) $access;
75    }
76    
77  function facebook_auth_perm() {  function facebook_auth_perm() {
78     return array('administer facebook');     return array('administer facebook');
79  }  }
# Line 91  function facebook_auth() { Line 83  function facebook_auth() {
83     $api_key = variable_get('facebook_apikey', '');     $api_key = variable_get('facebook_apikey', '');
84     $secret = variable_get('facebook_secretkey', '');     $secret = variable_get('facebook_secretkey', '');
85     // initiate class     // initiate class
86     $facebook = new Facebook($api_key, $secret);     $facebook = @new Facebook($api_key, $secret);
87     $u = $facebook->require_login();     $u = @$facebook->require_login();
   
88     # http://developers.facebook.com/documentation.php?v=1.0&method=users.getInfo     # http://developers.facebook.com/documentation.php?v=1.0&method=users.getInfo
89     if ($facebook->api_client->error_code) {     if (@$facebook->api_client->error_code) {
90        return t('Unable to load profile from facebook');       return t('Unable to load profile from facebook');
91     }     }
92     $info = $facebook->api_client->users_getInfo($u, array('first_name', 'last_name'));     $info = @$facebook->api_client->users_getInfo($u, array('first_name', 'last_name'));
93     $username = $info[0]['first_name'] .' '. $info[0]['last_name'];     $username = $info[0]['first_name'] .' '. $info[0]['last_name'];
94    
95     // user should be authenticated by Facebook now     // user should be authenticated by Facebook now
96     $account = user_external_load($u .'@facebook');     $account = user_external_load($u .'@facebook');
97     if ($user->uid && $account) {     if ($user->uid && $account) {
98        return t('Account has already been associated with an account.');       return t('Account has already been associated with an account.');
99     }     }
100     else if ($user->uid && !$account) {     else if ($user->uid && !$account) {
101        return drupal_get_form('facebook_auth_user_assoc');       return drupal_get_form('facebook_auth_user_assoc');
102     }     }
103    
104     // If we don't have a user, register them     // If we don't have a user, register them
105     if (!$account->uid) {     if (!$account->uid) {
106        $user_default = array(       $user_default = array(
107           'name' => $username,         'name' => $username,
108           'pass' => user_password(),         'pass' => user_password(),
109           'init' => db_escape_string($username),         'init' => db_escape_string($username),
110           'status' => 1,         'status' => 1,
111           'authname_facebook_auth' => $u .'@facebook',         'authname_facebook_auth' => $u .'@facebook',
112        );       );
113        $user_default['roles'][DRUPAL_AUTHENTICATED_RID] = 'authenticated user';       $user_default['roles'][DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
114    
115        $user = user_save('', $user_default);       $user = user_save('', $user_default);
116        watchdog('user', 'new user: '. $user->name .' (FB)', l(t('edit user'), 'admin/user/edit/'. $user->uid));       watchdog('user', 'new user: '. $user->name .' (FB)', l(t('edit user'), 'admin/user/edit/'. $user->uid));
117        if (($user->uid) && ($user->uid > 0)) {       if (($user->uid) && ($user->uid > 0)) {
118           drupal_goto('user/'. $user->uid .'/edit');         drupal_goto('user/'. $user->uid .'/edit');
119        }       }
120     }     }
121     else {     else {
122        $user = $account;       $user = $account;
123     }     }
124    
125     // final check     // final check
126     if ($user->uid && $user->uid > 0) {     if ($user->uid && $user->uid > 0) {
127        drupal_set_message('Logged in with Facebook as '. $user->name .'.');       drupal_set_message('Logged in with Facebook as '. $user->name .'.');
128        return drupal_goto('<front>');       return drupal_goto('<front>');
129     }     }
130     # Its not going to get here if all goes well     # Its not going to get here if all goes well
131     # TODO: add some error handling here if it fails everything else     # TODO: add some error handling here if it fails everything else
# Line 143  function facebook_auth() { Line 134  function facebook_auth() {
134  }  }
135    
136    
137  function facebook_auth_admin() {  function facebook_auth_admin($form_state) {
138       $form = array();
139    
140     $form['facebook_apikey'] = array(     $form['facebook_apikey'] = array(
141        '#type' => 'textfield',       '#type' => 'textfield',
142        '#title' => t('API key'),       '#title' => t('API key'),
143        '#default_value' => variable_get('facebook_apikey', ''),       '#default_value' => variable_get('facebook_apikey', ''),
144     );     );
145     $form['facebook_secretkey'] = array(     $form['facebook_secretkey'] = array(
146        '#type' => 'textfield',       '#type' => 'textfield',
147        '#title' => t('Secret key'),       '#title' => t('Secret key'),
148        '#default_value' => variable_get('facebook_secretkey', ''),       '#default_value' => variable_get('facebook_secretkey', ''),
149     );     );
150     $form['facebook_auth_enable_loginblock'] = array(     $form['facebook_auth_enable_loginblock'] = array(
151        '#type' => 'checkbox',       '#type' => 'checkbox',
152        '#title' => t('Show button in login block'),       '#title' => t('Show button in login block'),
153        '#description' => t('Enable if you want to include a button to login with facebook under the normal login forum.'),       '#description' => t('Enable if you want to include a button to login with facebook under the normal login forum.'),
154        '#default_value' => variable_get('facebook_auth_enable_loginblock', FALSE),       '#default_value' => variable_get('facebook_auth_enable_loginblock', FALSE),
155     );     );
156    
157     return system_settings_form($form);     return system_settings_form($form);
# Line 168  function facebook_auth_admin() { Line 160  function facebook_auth_admin() {
160  /**  /**
161   * Implementation of hook_form_alter : adds Facebook login to the login forms.   * Implementation of hook_form_alter : adds Facebook login to the login forms.
162   */   */
163  function facebook_auth_form_alter($form_id, &$form) {  function facebook_auth_form_alter(&$form, $form_state, $form_id) {
164     $api_key = variable_get('facebook_apikey', '');     $api_key = variable_get('facebook_apikey', '');
165     $secret = variable_get('facebook_secretkey', '');     $secret = variable_get('facebook_secretkey', '');
166     if (!$secret || !$api_key) { return; }     if (!$secret || !$api_key) { return; }
167     if (!variable_get('facebook_auth_enable_loginblock', FALSE)) { return; }     if (!variable_get('facebook_auth_enable_loginblock', FALSE)) { return; }
168     if ($form_id == 'user_login_block' || $form_id == 'user_login') {     if ($form_id == 'user_login_block' || $form_id == 'user_login') {
169        $img = theme('image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', t('Login to facebook'), t('Login to facebook'), NULL, FALSE);       $img = theme('image',
170        $form['facebook_link'] = array(               variable_get('facebook_auth_image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif'),
171           '#type' => 'markup',               t('Login to facebook'), t('Login to facebook'), NULL, FALSE
172           '#value' => l($img, 'facebook', array(), 'destination='.$_GET['q'], NULL, FALSE, TRUE),           );
173        );       $form['facebook_link'] = array(
174           '#type' => 'markup',
175           '#value' => l($img, 'facebook', array('query' => 'destination='.$_GET['q'], 'html' => TRUE)),
176         );
177     }     }
178     return $form;     return $form;
179  }  }
# Line 186  function facebook_auth_form_alter($form_ Line 181  function facebook_auth_form_alter($form_
181  function facebook_auth_block($op = 'list', $delta = 0, $edit = array()) {  function facebook_auth_block($op = 'list', $delta = 0, $edit = array()) {
182     global $user;     global $user;
183     if ($op == 'list') {     if ($op == 'list') {
184        $blocks[0] = array(       $blocks[0] = array(
185           'info' => t('Facebook Login Button'),         'info' => t('Facebook Login Button'),
186           'weight' => 0,         'weight' => 0,
187           'enabled' => 0,         'enabled' => 0,
188           'region' => 'left'         'region' => 'left'
189        );       );
190        return $blocks;       return $blocks;
191     }     }
192     else if ($op == 'view') {     else if ($op == 'view') {
193        switch($delta) {       switch($delta) {
194        case 0:       case 0:
195           if ($user && $user->uid) { return; }         if ($user && $user->uid) { return; }
196           $block = array(         $image = theme('image',
197              'subject' => t('Login with facebook'),                  variable_get('facebook_auth_image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif'),
198              'content' => l(theme('image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', t('Login to facebook'), t('Login to facebook'), NULL, FALSE), 'facebook', array(), 'destination='.$_GET['q'], NULL, FALSE, TRUE),                  t('Login to facebook'), t('Login to facebook'), NULL, FALSE
199           );              );
200           break;         $block = array(
201        }           'subject' => t('Login with facebook'),
202        return $block;           'content' => l($image, 'facebook', array('query' => 'destination='.$_GET['q'], 'html' => TRUE)),
203           );
204           break;
205         }
206         return $block;
207     }     }
208  }  }
209    
# Line 213  function facebook_auth_user_assoc() { Line 212  function facebook_auth_user_assoc() {
212     $api_key = variable_get('facebook_apikey', '');     $api_key = variable_get('facebook_apikey', '');
213     $secret = variable_get('facebook_secretkey', '');     $secret = variable_get('facebook_secretkey', '');
214     // initiate class     // initiate class
215     $facebook = new Facebook($api_key, $secret);     $facebook = @new Facebook($api_key, $secret);
216     $u = $facebook->require_login();     $u = @$facebook->require_login();
217       $info = @$facebook->api_client->users_getInfo($u, array('first_name', 'last_name'));
218       $username = $info[0]['first_name'] .' '. $info[0]['last_name'];
219    
220     $form = array();     $form = array();
221     $form['fb_uid'] = array('#type' => 'value', '#value' => $u);     $form['fb_uid'] = array('#type' => 'value', '#value' => $u);
222     return confirm_form(     return confirm_form(
223        $form,       $form,
224        t('Are you sure you want to link %fbname to %username?',array('%fbname' => $username, '%username' => $user->name)),       t('Are you sure you want to link %fbname to %username?',array('%fbname' => $username, '%username' => $user->name)),
225        '<front>', // Not sure what to provide here       '<front>', // Not sure what to provide here
226        NULL,       NULL,
227        t('Link'),       t('Link'),
228        t('Cancel')       t('Cancel')
229     );     );
230  }  }
231    
232  function facebook_auth_user_assoc_submit($form_id, $form_values) {  function facebook_auth_user_assoc_submit($form, &$form_state) {
233     global $user;     global $user;
234     user_save($user, array('auth_facebook_auth' => $form_values['fb_uid'] . '@facebook' ), '');     user_save($user, array('auth_facebook_auth' => $form_state['values']['fb_uid'] . '@facebook' ), '');
235     return drupal_goto('user/'. $user->uid .'/edit');     return drupal_goto('user/'. $user->uid .'/edit');
236  }  }
237    
# Line 239  function facebook_auth_user_identities($ Line 241  function facebook_auth_user_identities($
241    
242     $result = db_query("SELECT * FROM {authmap} WHERE module='facebook_auth' AND uid=%d", $account->uid);     $result = db_query("SELECT * FROM {authmap} WHERE module='facebook_auth' AND uid=%d", $account->uid);
243     while ($identity = db_fetch_object($result)) {     while ($identity = db_fetch_object($result)) {
244        list($userId) = explode('@',$identity->authname);       list($userId) = explode('@',$identity->authname);
245        $rows[] = array(l($userId,'http://www.facebook.com/profile.php?id='.$userId), l(t('Delete'), 'user/'. $account->uid .'/facebook_auth/delete/'. $identity->aid));       $rows[] = array(
246                 l($userId,'http://www.facebook.com/profile.php?id='.$userId),
247                 l(t('Delete'), 'user/'. $account->uid .'/facebook_auth/delete/'. $identity->aid)
248             );
249     }     }
250    
251     $output = theme('table', $header, $rows);     $output = theme('table', $header, $rows);
252     $output .= l(theme('image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', t('Login to facebook'), t('Login to facebook'), NULL, FALSE), 'facebook', array(), 'destination='.$_GET['q'], NULL, FALSE, TRUE);     $output .= l(
253             theme('image',
254                variable_get('facebook_auth_image', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif'),
255                t('Login to facebook'), t('Login to facebook'), NULL, FALSE
256             ),
257             'facebook',
258             array(
259                'query' => 'destination='.$_GET['q'],
260                'html' => TRUE,
261             )
262          );
263     return $output;     return $output;
264  }  }
265    
266  function facebook_auth_user_delete($account, $aid = 0) {  function facebook_auth_user_delete($account, $aid = 0) {
267    db_query("DELETE FROM {authmap} WHERE uid=%d AND aid=%d AND module='facebook_auth'", $account->uid, $aid);    db_query("DELETE FROM {authmap} WHERE uid=%d AND aid=%d AND module='facebook_auth'", $account->uid, $aid);
268    if (db_affected_rows()) {    if (db_affected_rows()) {
269      drupal_set_message(t('Facebook association deleted.'));     drupal_set_message(t('Facebook association deleted.'));
270    }    }
271    drupal_goto('user/'. $account->uid .'/facebook_auth');    drupal_goto('user/'. $account->uid .'/facebook_auth');
272  }  }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.9.4.1

  ViewVC Help
Powered by ViewVC 1.1.2