/[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.10, Mon Oct 29 05:44:51 2007 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    define('FACEBOOK_AUTH_SIMPLE', 1);
4    define('FACEBOOK_AUTH_FULL_DRUPAL', 2);
5    
6  if (substr(phpversion(), 0, 1) == '4') {  if (substr(phpversion(), 0, 1) == '4') {
7     if (file_exists(dirname(__FILE__) . '/facebook-platform/php4client/facebook.php')) {     if (file_exists(dirname(__FILE__) . '/facebook-platform/php4client/facebook.php')) {
8        require_once('facebook-platform/php4client/facebook.php');        require_once('facebook-platform/php4client/facebook.php');
# Line 43  function facebook_auth_menu($may_cache) Line 46  function facebook_auth_menu($may_cache)
46        );        );
47        $items[] = array(        $items[] = array(
48           'path' => 'facebook',           'path' => 'facebook',
49           'title' => t('my Facebook account'),           'title' => t('Facebook authentication'),
50           'callback' => 'facebook_auth',           'callback' => 'facebook_auth',
51           // 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
52           // Probably just disable the module           // Probably just disable the module
# Line 112  function facebook_auth() { Line 115  function facebook_auth() {
115    
116     // If we don't have a user, register them     // If we don't have a user, register them
117     if (!$account->uid) {     if (!$account->uid) {
118        $user_default = array(        return drupal_get_form('facebook_auth_email', $username, $u);
          'name' => $username,  
          'pass' => user_password(),  
          'init' => db_escape_string($username),  
          'status' => 1,  
          'authname_facebook_auth' => $u .'@facebook',  
       );  
       $user_default['roles'][DRUPAL_AUTHENTICATED_RID] = 'authenticated user';  
   
       $user = user_save('', $user_default);  
       watchdog('user', 'new user: '. $user->name .' (FB)', l(t('edit user'), 'admin/user/edit/'. $user->uid));  
       if (($user->uid) && ($user->uid > 0)) {  
          drupal_goto('user/'. $user->uid .'/edit');  
       }  
119     }     }
120     else {     else {
121        $user = $account;        if (!variable_get('user_email_verification', TRUE) || $account->login) {
122             global $user;
123             $user = $account;
124             user_login_submit('user_login', array());
125          }
126          else {
127             drupal_set_message(t('You must validate your email address for this account before logging in via Facebook.'));
128             return '';
129          }
130     }     }
131    
132     // final check     // final check
# Line 162  function facebook_auth_admin() { Line 160  function facebook_auth_admin() {
160        '#default_value' => variable_get('facebook_auth_enable_loginblock', FALSE),        '#default_value' => variable_get('facebook_auth_enable_loginblock', FALSE),
161     );     );
162    
163       $form['facebook_auth_registration_flow'] = array(
164          '#type' => 'radios',
165          '#options' => array(
166             FACEBOOK_AUTH_SIMPLE => 'Use simple authentication. Trust user on email, and auto-login.',
167             FACEBOOK_AUTH_FULL_DRUPAL => 'Use drupal full user registration (welcome email, email validation, any extra hooks, etc)',
168          ),
169          '#title' => t('Which drupal registration model to use'),
170          '#default_value' => variable_get('facebook_auth_registration_flow', FACEBOOK_AUTH_SIMPLE),
171       );
172    
173     return system_settings_form($form);     return system_settings_form($form);
174  }  }
175    
# Line 174  function facebook_auth_form_alter($form_ Line 182  function facebook_auth_form_alter($form_
182     if (!$secret || !$api_key) { return; }     if (!$secret || !$api_key) { return; }
183     if (!variable_get('facebook_auth_enable_loginblock', FALSE)) { return; }     if (!variable_get('facebook_auth_enable_loginblock', FALSE)) { return; }
184     if ($form_id == 'user_login_block' || $form_id == 'user_login') {     if ($form_id == 'user_login_block' || $form_id == 'user_login') {
185          $extra = NULL;
186          if ($_GET['q'] != 'facebook') {
187             $extra = 'destination='.$_GET['q'];
188          }
189        $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', 'http://static.ak.facebook.com/images/devsite/facebook_login.gif', t('Login to facebook'), t('Login to facebook'), NULL, FALSE);
190        $form['facebook_link'] = array(        $form['facebook_link'] = array(
191           '#type' => 'markup',           '#type' => 'markup',
192           '#value' => l($img, 'facebook', array(), 'destination='.$_GET['q'], NULL, FALSE, TRUE),           '#value' => l($img, 'facebook', array(), $extra, NULL, FALSE, TRUE),
193        );        );
194     }     }
195     return $form;     return $form;
# Line 255  function facebook_auth_user_delete($acco Line 267  function facebook_auth_user_delete($acco
267    }    }
268    drupal_goto('user/'. $account->uid .'/facebook_auth');    drupal_goto('user/'. $account->uid .'/facebook_auth');
269  }  }
270    
271    function facebook_auth_email($username, $u) {
272       $form = array();
273       $form['description'] = array(
274          '#value' => t("You need to fill out your email address to complete registration."),
275       );
276       if (db_num_rows(db_query("SELECT uid FROM {users} WHERE LOWER(name) = LOWER('%s')", $username)) > 0) {
277          $form['name'] = array('#type' => 'textfield',
278             '#title' => t('Username'),
279             '#default_value' => $username,
280             '#maxlength' => USERNAME_MAX_LENGTH,
281             '#description' => t('Your preferred username; punctuation is not allowed except for periods, hyphens, and underscores.'),
282             '#required' => TRUE,
283          );
284       }
285       else {
286          $form['name'] = array('#value'=>$username, '#type'=>'value');
287       }
288       $form['u'] = array('#value'=>$u, '#type'=>'value');
289       $form['destination'] = array('#value'=>$_GET['destination'], '#type'=>'value');
290       $form['mail'] = array('#type' => 'textfield',
291          '#title' => t('E-mail address'),
292          '#maxlength' => EMAIL_MAX_LENGTH,
293          '#description' => t('A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.'),
294          '#required' => TRUE,
295       );
296       $form['submit'] = array('#type'=>'submit','#value'=>t('Register'));
297       return $form;
298    }
299    function facebook_auth_email_validate($form_id,$form_values) {
300       /* If we are using full drupal flow, it'll handle this on its own */
301       if (variable_get('facebook_auth_registration_flow', FACEBOOK_AUTH_SIMPLE) == FACEBOOK_AUTH_SIMPLE) {
302          // Validate the e-mail address:
303          if ($error = user_validate_mail($form_values['mail'])) {
304             form_set_error('mail', $error);
305          }
306          else if (db_num_rows(db_query("SELECT uid FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_values['mail'])) > 0) {
307             form_set_error('mail', t('The e-mail address %email is already registered. <a href="@password">Have you forgotten your password?</a>', array('%email' => $form_values['mail'], '@password' => url('user/password'))));
308          }
309          else if (drupal_is_denied('mail', $form_values['mail'])) {
310             form_set_error('mail', t('The e-mail address %email has been denied access.', array('%email' => $form_values['mail'])));
311          }
312          if (db_num_rows(db_query("SELECT uid FROM {users} WHERE LOWER(name) = LOWER('%s')", $form_values['name'])) > 0) {
313             form_set_error('name', t('The name %name is already taken. Enter another one.', array('%name' => $form_values['name'])));
314          }
315       }
316    }
317    function facebook_auth_email_submit($form_id,$form_values) {
318       if (variable_get('facebook_auth_registration_flow', FACEBOOK_AUTH_SIMPLE) != FACEBOOK_AUTH_SIMPLE) {
319          $form = drupal_retrieve_form('user_register');
320          $edit['name'] = $form_values['name'];
321          $edit['mail'] = $form_values['mail'];
322          $edit['pass']  = user_password();
323          $edit['status'] = variable_get('user_register', 1) == 1;
324          $form['authname_facebook_auth'] = array('#type'=>'value', '#value'=>$form_values['u'] .'@facebook');
325          $form['#post'] = $edit;
326          drupal_prepare_form('user_register', $form);
327          drupal_validate_form('user_register', $form);
328          if (form_get_errors()) {
329             // We were unable to register a valid new user, redirect to standard
330             // user/register and prefill with the values we received.
331             drupal_set_message(t('Facebook registration failed for the reasons listed. You may register now, or if you already have an account you can <a href="@login">log in</a> now and add your Facebook under "My Account"', array('@login' => url('user/login'))), 'error');
332             $_SESSION['openid'] = $edit;
333             // We'll want to redirect back to the same place.
334             $destination = drupal_get_destination();
335             unset($_REQUEST['destination']);
336             drupal_goto('user/register', $destination);
337          }
338          drupal_submit_form('user_register', $form);
339          if (!variable_get('user_email_verification', TRUE)) {
340             global $user;
341             $account = user_external_load($form_values['u'] .'@facebook');
342             $user = $account;
343             user_login_submit('user_login', array());
344          }
345       }
346       else {
347          global $user;
348          $user_default = array(
349             'name' => $form_values['name'],
350             'pass' => user_password(),
351             'mail' => $form_values['mail'],
352             'init' => db_escape_string($form_values['name']),
353             'status' => 1,
354             'authname_facebook_auth' => $form_values['u'] .'@facebook',
355          );
356          $user_default['roles'][DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
357    
358          $user = user_save('', $user_default);
359          watchdog('user', 'new user: '. $user->name .' (FB)', l(t('edit user'), 'admin/user/edit/'. $user->uid));
360          user_login_submit('user_login', array());
361       }
362       drupal_goto('<front>');
363       return;
364    }
365    
366    function facebook_auth_user($op, &$edit, &$account, $category = NULL) {
367       if ($op == 'logout') {
368          $api_key = variable_get('facebook_apikey', '');
369          $secret = variable_get('facebook_secretkey', '');
370          // initiate class
371          $facebook = new Facebook($api_key, $secret);
372          $facebook->set_user(NULL, '', null);
373       }
374       return;
375    }
376    

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

  ViewVC Help
Powered by ViewVC 1.1.2