Index: ip_login.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ip_login/ip_login.module,v retrieving revision 1.1.4.5 diff -u -r1.1.4.5 ip_login.module --- ip_login.module 15 Nov 2008 04:37:16 -0000 1.1.4.5 +++ ip_login.module 26 Aug 2009 18:09:54 -0000 @@ -1,44 +1,46 @@ uid && variable_get('ip_login_enabled',1) && ! $_SESSION['ip_login_complete']){ - ip_login_login($_SERVER['REMOTE_ADDR']); + // call ip login on init for any page, if not already processed + if (!$user->uid && variable_get('ip_login_enabled', 1) && !$_SESSION['ip_login_complete']) { + ip_login_login(ip_address()); } } /** - * Implementation of hook_menu + * Implementation of hook_menu(). */ -function ip_login_menu(){ +function ip_login_menu() { $items['admin/settings/iplogin'] = array( - 'title' => t('IP Login Settings'), - 'description' => t('configure IP login settings'), - 'access callback' => 'user_access', - 'access arguments' => array('administer site configuration'), - 'page callback' => 'drupal_get_form', - 'page arguments' => array('ip_login_admin_settings'), - 'type' => MENU_NORMAL_ITEM, - ); + 'title' => 'IP Login Settings', + 'description' => 'Configure IP login settings.', + 'access callback' => 'user_access', + 'access arguments' => array('administer site configuration'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('ip_login_admin_settings'), + 'type' => MENU_NORMAL_ITEM, + ); return $items; } -function ip_login_admin_settings(){ - +function ip_login_admin_settings() { + $result = db_query("SELECT DISTINCT name FROM {profile_fields}"); - while($field = db_fetch_object($result)){ + while ($field = db_fetch_object($result)) { $fields[$field->name] = $field->name; } - if( ! $fields || ! variable_get('ip_login_profile_ip_field','')){ - drupal_set_message(t('You can use the !profile_module to add a text field to the user profile to store the user ip address.', array('!profile_module'=> l('profile module admin', 'admin/user/profile/add/textfield')))); + if (!$fields || !variable_get('ip_login_profile_ip_field', '')) { + drupal_set_message(t('You can use the !profile_module to add a text field to the user profile to store the user ip address.', array('!profile_module' => l('profile module admin', 'admin/user/profile/add/textfield')))); } $form['info'] = array( '#type' => 'markup', @@ -48,51 +50,52 @@ '#type' => 'checkbox', '#title' => t('IP Login enabled'), '#description' => t('Check to enable IP Login, uncheck to disable. If unchecked no user will be able to login via IP address.'), - '#default_value' => variable_get('ip_login_enabled',1) + '#default_value' => variable_get('ip_login_enabled', 1) ); $form['ip_login_profile_ip_field'] = array( '#type' => 'select', '#title' => t('Profile module IP address field'), '#description' => t('Select the profile field containing the user IP address.'), '#options' => $fields, - '#default_value' => variable_get('ip_login_profile_ip_field','profile_ip') + '#default_value' => variable_get('ip_login_profile_ip_field', 'profile_ip') ); return system_settings_form($form); } /** - * Log user in by ip address and profile_ip profile field + * Log user in by ip address and profile_ip profile field. + * * @param string $ip ip address * @return void */ -function ip_login_login($ip){ +function ip_login_login($ip) { //get wildcard IP for fuzzy range matches e.g 203.123.456.* $ip_range = ip_login_get_range($ip); - + //get profile field for ip address $profile_field = variable_get('ip_login_profile_ip_field', 'profile_ip'); - + //DB lookup uid with passed ip address or range $result = db_fetch_object( db_query("SELECT pv.uid, pv.value FROM {profile_values} pv - INNER JOIN {profile_fields} pf ON pv.fid = pf.fid - WHERE pf.name = '%s' AND (pv.value = '%s' OR pv.value = '%s')", $profile_field, $ip, $ip_range) + INNER JOIN {profile_fields} pf ON pv.fid = pf.fid + WHERE pf.name = '%s' AND (pv.value = '%s' OR pv.value = '%s')", $profile_field, $ip, $ip_range) ); //if a uid is returned with the passed ip address and user_load is available - if($result && function_exists("user_load")){ + if ($result && function_exists('user_load')) { //if can load user from uid where user status is active if ($account = user_load(array('uid' => $result->uid, 'status' => 1))) { //login by assigning account to global $user object global $user; $user = $account; //this part logs the user in $message = t('Welcome %name. You are now logged into %sitename.', - array( - '%name' => $user->name, - '%sitename' => variable_get('site_name', 'this website') - ) - ); + array( + '%name' => $user->name, + '%sitename' => variable_get('site_name', 'this website') + ) + ); //set welcome message drupal_set_message($message); $message = t('Session opened for %name.', array('%name' => $user->name)); @@ -103,25 +106,26 @@ //IMPORTANT: drupal call to regenerate session data for anon user to authenticated user sess_regenerate(); } - //set processed session flag - $_SESSION['ip_login_complete'] = 1; - }else{ - return false; //try again later + //set processed session flag + $_SESSION['ip_login_complete'] = 1; + } + else { + return FALSE; //try again later } } -/* - * Return ip range from ip address +/** + * Return ip range from ip address. + * * @param string $ip ip address * @return string $ip ip address with wildcard * - * -*/ -function ip_login_get_range($ip){ - if($ip){ + */ +function ip_login_get_range($ip) { + if ($ip) { $ip = explode('.', $ip); //explode ip to array array_pop($ip); //remove last element - $ip = implode('.', $ip).'.*'; //convert to string and add .* + $ip = implode('.', $ip) .'.*'; //convert to string and add .* } return $ip; }