| 1 |
<?php |
<?php |
| 2 |
// $Id: |
// $Id: single_login.module,v 1.1.2.3 2008/10/07 22:10:21 sanduhrs Exp $ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* Single Login is a session management system for Drupal. |
| 6 |
|
* |
| 7 |
|
* It allows the site administrator to create a policy to detect, and prevent, |
| 8 |
|
* duplicate logins on the same account. This is obviously handy for a site |
| 9 |
|
* that requires paid subscriptions. Once a duplicate login is detected from |
| 10 |
|
* a different system, the first login gets logged out. The admin can set a |
| 11 |
|
* policy that determines how often and within what time period a session can |
| 12 |
|
* "ping pong" between machines. Should the policy conditions be met, the admin |
| 13 |
|
* can specify an action,typically to block the offending account. |
| 14 |
|
* |
| 15 |
|
* The module also keeps a history of duplicate logins, and if you use the |
| 16 |
|
* Google Analytics/urchin module, it will insert the session ID into the |
| 17 |
|
* urchin system. |
| 18 |
|
* |
| 19 |
|
* @file |
| 20 |
|
* Allows users to be logged on only on a single browser in one time. |
| 21 |
|
* |
| 22 |
|
* @author |
| 23 |
|
* Martijn Dekkers |
| 24 |
|
* Stefan Auditor <stefan.auditor@erdfisch.de> |
| 25 |
|
*/ |
| 26 |
|
|
| 27 |
define('SINGLE_LOGIN_CHECK_ROLES', 'single_login_check_roles'); |
define('SINGLE_LOGIN_CHECK_ROLES', 'single_login_check_roles'); |
| 28 |
define('SINGLE_LOGIN_TREAT_ONLINE', 'single_login_treat_online'); |
define('SINGLE_LOGIN_TREAT_ONLINE', 'single_login_treat_online'); |
| 40 |
define('SINGLE_LOGIN_HISTORY_UID', 'single_login_history_uid__'); |
define('SINGLE_LOGIN_HISTORY_UID', 'single_login_history_uid__'); |
| 41 |
define('SINGLE_LOGIN_HISTORY_UNAME', 'single_login_history_uname__'); |
define('SINGLE_LOGIN_HISTORY_UNAME', 'single_login_history_uname__'); |
| 42 |
|
|
| 43 |
|
/** |
| 44 |
|
* Implementation of hook_enable(). |
| 45 |
|
*/ |
| 46 |
|
function single_login_enable() { |
| 47 |
|
$exists = db_result(db_query("SELECT fid FROM {profile_fields} WHERE name='profile_current_session_id'")); |
| 48 |
|
if (!$exists) { |
| 49 |
|
// needed for google analytics |
| 50 |
|
db_query("INSERT INTO {profile_fields} (title, name, explanation, category, page, type, weight, required, register, visibility, autocomplete, options) VALUES ('Current Session ID', 'profile_current_session_id', 'User session ID', 'User Information', '', 'textfield', 0, 0, 0, 4, 0, '')"); |
| 51 |
|
} |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
/** |
| 55 |
|
* Implementation of hook_init(). |
| 56 |
|
*/ |
| 57 |
function single_login_init() { |
function single_login_init() { |
| 58 |
global $user; |
global $user; |
| 59 |
|
|
| 60 |
if (intval($user->uid)) { |
if (intval($user->uid)) { |
| 61 |
$time = time(); |
$time = time(); |
| 62 |
|
|
| 63 |
if (_single_login_is_user_single(array_keys($user->roles))) { |
if (_single_login_is_user_single(array_keys($user->roles))) { |
| 64 |
$sql = "INSERT INTO {single_login_history} SET |
$sql = "INSERT INTO {single_login_history} SET |
| 65 |
uid = %1\$d, session_id = '%2\$s', date = %3\$d, |
uid = %1\$d, session_id = '%2\$s', date = %3\$d, |
| 66 |
ip = '%4\$s', browser = '%5\$s', type = 'cookie' |
ip = '%4\$s', browser = '%5\$s', type = 'cookie' |
| 67 |
ON DUPLICATE KEY UPDATE |
ON DUPLICATE KEY UPDATE |
| 68 |
date = %3\$d"; |
date = %3\$d"; |
| 69 |
$sql = sprintf($sql, $user->uid, session_id(), $time, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']); |
$sql = sprintf($sql, $user->uid, session_id(), $time, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']); |
| 70 |
db_query($sql); |
db_query($sql); |
| 71 |
|
|
| 72 |
$sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d"; |
$sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d"; |
| 73 |
$sql = sprintf($sql, $user->uid, $time, variable_get(SINGLE_LOGIN_TREAT_ONLINE, SINGLE_LOGIN_DEF_TREAT_ONLINE)); |
$sql = sprintf($sql, $user->uid, $time, variable_get(SINGLE_LOGIN_TREAT_ONLINE, SINGLE_LOGIN_DEF_TREAT_ONLINE)); |
| 74 |
if (db_num_rows(db_query($sql)) > 1) { |
if (db_num_rows(db_query($sql)) > 1) { |
| 75 |
// if the current user is not the only logged with this account |
// if the current user is not the only logged with this account |
| 76 |
$sql = "INSERT INTO {single_login} (uid, counter) VALUES (%d, %d) ON DUPLICATE KEY UPDATE counter = counter + 1"; |
$sql = "INSERT INTO {single_login} (uid, counter) VALUES (%d, %d) ON DUPLICATE KEY UPDATE counter = counter + 1"; |
| 77 |
db_query(sprintf($sql, $user->uid, 1)); |
db_query(sprintf($sql, $user->uid, 1)); |
| 78 |
$sql = "DELETE FROM {sessions} WHERE uid = %d AND sid != '%s'"; |
$sql = "DELETE FROM {sessions} WHERE uid = %d AND sid <> '%s'"; |
| 79 |
db_query(sprintf($sql, $user->uid, session_id())); |
db_query(sprintf($sql, $user->uid, session_id())); |
| 80 |
} else { |
} |
| 81 |
// if current user is the only who logged in with current account |
else { |
| 82 |
db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid)); |
// if current user is the only who logged in with current account |
| 83 |
} |
db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid)); |
| 84 |
} |
} |
| 85 |
|
} |
| 86 |
_single_login_update_sess_field($user->uid); |
|
| 87 |
} |
_single_login_update_sess_field($user->uid); |
| 88 |
} |
} |
| 89 |
|
} |
|
/** |
|
|
* Implementation of hook_menu() |
|
|
* |
|
|
* @param bool $may_cache |
|
|
* @return array of menu items |
|
|
*/ |
|
|
function single_login_menu($may_cache) |
|
|
{ |
|
|
$items = array(); |
|
|
if ($may_cache) { |
|
|
$items[] = array( |
|
|
'path' => 'admin/settings/single_login', |
|
|
'title' => t('Single login settigns'), |
|
|
'callback' => 'drupal_get_form', |
|
|
'callback arguments' => 'single_login_settings', |
|
|
'access' => user_access('administer site configuration'), |
|
|
'type' => MENU_NORMAL_ITEM, |
|
|
); |
|
|
$items[] = array( |
|
|
'path' => 'admin/settings/single_login_history', |
|
|
'title' => t('Single login session history'), |
|
|
'callback' => 'single_login_history', |
|
|
'access' => user_access('administer site configuration'), |
|
|
'type' => MENU_NORMAL_ITEM, |
|
|
); |
|
|
$items[] = array( |
|
|
'path' => 'single_login/blocked', |
|
|
'title' => t('Account was blocked'), |
|
|
'callback' => 'single_login_static_page', |
|
|
'callback arguments' => 'blocked', |
|
|
'access' => true, |
|
|
'type' => MENU_CALLBACK, |
|
|
); |
|
|
} |
|
| 90 |
|
|
| 91 |
return $items; |
/** |
| 92 |
|
* Implementation of hook_menu(). |
| 93 |
|
*/ |
| 94 |
|
function single_login_menu($may_cache) { |
| 95 |
|
$items = array(); |
| 96 |
|
if ($may_cache) { |
| 97 |
|
$items[] = array( |
| 98 |
|
'path' => 'admin/settings/single_login', |
| 99 |
|
'title' => t('Single login settings'), |
| 100 |
|
'callback' => 'drupal_get_form', |
| 101 |
|
'callback arguments' => 'single_login_settings', |
| 102 |
|
'access' => user_access('administer site configuration'), |
| 103 |
|
'type' => MENU_NORMAL_ITEM, |
| 104 |
|
); |
| 105 |
|
$items[] = array( |
| 106 |
|
'path' => 'admin/settings/single_login_history', |
| 107 |
|
'title' => t('Single login session history'), |
| 108 |
|
'callback' => 'single_login_history', |
| 109 |
|
'access' => user_access('administer site configuration'), |
| 110 |
|
'type' => MENU_NORMAL_ITEM, |
| 111 |
|
); |
| 112 |
|
$items[] = array( |
| 113 |
|
'path' => 'single_login/blocked', |
| 114 |
|
'title' => t('Account was blocked'), |
| 115 |
|
'callback' => 'single_login_static_page', |
| 116 |
|
'callback arguments' => 'blocked', |
| 117 |
|
'access' => TRUE, |
| 118 |
|
'type' => MENU_CALLBACK, |
| 119 |
|
); |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
return $items; |
| 123 |
} |
} |
| 124 |
|
|
| 125 |
/** |
/** |
| 126 |
* Admin settings |
* Administration settings page |
|
* |
|
| 127 |
*/ |
*/ |
| 128 |
function single_login_settings() { |
function single_login_settings() { |
| 129 |
$user_roles = array(); |
$user_roles = array(); |
| 130 |
$res = db_query("SELECT * FROM {role} WHERE 1"); |
$res = db_query("SELECT * FROM {role} WHERE 1"); |
| 131 |
while ($row = db_fetch_object($res)) { |
while ($row = db_fetch_object($res)) { |
| 132 |
$user_roles[$row->rid] = $row->name; |
$user_roles[$row->rid] = $row->name; |
| 133 |
} |
} |
| 134 |
|
|
| 135 |
$form = array(); |
$form = array(); |
| 136 |
$form['sub_main'] = array( |
$form['sub_main'] = array( |
| 137 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 138 |
'#title' => t('Main settings'), |
'#title' => t('Main settings'), |
| 139 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 140 |
'#collapsed' => false, |
'#collapsed' => FALSE, |
| 141 |
); |
); |
| 142 |
$form['sub_main'][SINGLE_LOGIN_CHECK_ROLES] = array( |
$form['sub_main'][SINGLE_LOGIN_CHECK_ROLES] = array( |
| 143 |
'#title' => t('Check login for roles'), |
'#title' => t('Check login for roles'), |
| 144 |
'#type' => 'select', |
'#type' => 'select', |
| 145 |
'#multiple' => true, |
'#multiple' => TRUE, |
| 146 |
'#options' => $user_roles, |
'#options' => $user_roles, |
| 147 |
'#default_value'=> variable_get(SINGLE_LOGIN_CHECK_ROLES, array()), |
'#default_value' => variable_get(SINGLE_LOGIN_CHECK_ROLES, array()), |
| 148 |
'#size' => 10, |
'#size' => 10, |
| 149 |
); |
); |
| 150 |
$form['sub_main'][SINGLE_LOGIN_TREAT_ONLINE] = array( |
$form['sub_main'][SINGLE_LOGIN_TREAT_ONLINE] = array( |
| 151 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 152 |
'#title' => t('Treat user online for seconds'), |
'#title' => t('Treat user online for seconds'), |
| 153 |
'#default_value' => variable_get(SINGLE_LOGIN_TREAT_ONLINE, SINGLE_LOGIN_DEF_TREAT_ONLINE), |
'#default_value' => variable_get(SINGLE_LOGIN_TREAT_ONLINE, SINGLE_LOGIN_DEF_TREAT_ONLINE), |
| 154 |
); |
); |
| 155 |
$form['sub_main'][SINGLE_LOGIN_MAX_RECONNECTIONS] = array( |
$form['sub_main'][SINGLE_LOGIN_MAX_RECONNECTIONS] = array( |
| 156 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 157 |
'#title' => t('Max login ping-pong values'), |
'#title' => t('Max login ping-pong values'), |
| 158 |
'#default_value' => variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS), |
'#default_value' => variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS), |
| 159 |
); |
); |
| 160 |
$form['sub_main'][SINGLE_LOGIN_STORE_HISTORY] = array( |
$form['sub_main'][SINGLE_LOGIN_STORE_HISTORY] = array( |
| 161 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 162 |
'#title' => t('Store sessions history for days (0 - infinite)'), |
'#title' => t('Store sessions history for days (0 - infinite)'), |
| 163 |
'#default_value' => variable_get(SINGLE_LOGIN_STORE_HISTORY, SINGLE_LOGIN_DEF_STORE_HISTORY), |
'#default_value' => variable_get(SINGLE_LOGIN_STORE_HISTORY, SINGLE_LOGIN_DEF_STORE_HISTORY), |
| 164 |
); |
); |
| 165 |
$form['sub_msg'] = array( |
$form['sub_msg'] = array( |
| 166 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 167 |
'#title' => t('Messages settings'), |
'#title' => t('Messages settings'), |
| 168 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 169 |
'#collapsed' => false, |
'#collapsed' => FALSE, |
| 170 |
); |
); |
| 171 |
$form['sub_msg'][SINGLE_LOGIN_MSG_RELOGGED] = array( |
$form['sub_msg'][SINGLE_LOGIN_MSG_RELOGGED] = array( |
| 172 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 173 |
'#title' => t('Relogin message'), |
'#title' => t('Relogin message'), |
| 174 |
'#maxlength' => 500, |
'#maxlength' => 500, |
| 175 |
'#default_value' => variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED), |
'#default_value' => variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED), |
| 176 |
); |
); |
| 177 |
$form['sub_msg'][SINGLE_LOGIN_MSG_BLOCKED] = array( |
$form['sub_msg'][SINGLE_LOGIN_MSG_BLOCKED] = array( |
| 178 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 179 |
'#title' => t('Account blocked message'), |
'#title' => t('Account blocked message'), |
| 180 |
'#maxlength' => 500, |
'#maxlength' => 500, |
| 181 |
'#default_value' => variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED), |
'#default_value' => variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED), |
| 182 |
); |
); |
| 183 |
if (module_exists('googleanalytics')) { |
if (module_exists('googleanalytics')) { |
| 184 |
$form['sub_google'] = array( |
$form['sub_google'] = array( |
| 185 |
'#type' => 'item', |
'#type' => 'item', |
| 186 |
'#title' => t('Google Analytics user sessionID tracking'), |
'#title' => t('Google Analytics user sessionID tracking'), |
| 187 |
'#description' => t('Goto ' . l('Google Analytics setting', 'admin/settings/googleanalytics') . ' and select \'Current Session ID\' in \'Track\' setting'), |
'#description' => t('Goto !page and select \'Current Session ID\' in \'Track\' setting', array('!page' => l('Google Analytics setting', 'admin/settings/googleanalytics'))), |
| 188 |
); |
); |
| 189 |
} |
} |
| 190 |
|
|
| 191 |
return system_settings_form($form); |
return system_settings_form($form); |
| 192 |
} |
} |
| 193 |
|
|
| 194 |
|
/** |
| 195 |
|
* |
| 196 |
|
*/ |
| 197 |
function single_login_history() { |
function single_login_history() { |
| 198 |
$uid = variable_get(SINGLE_LOGIN_HISTORY_UID, 0); |
$uid = variable_get(SINGLE_LOGIN_HISTORY_UID, 0); |
| 199 |
$uname = variable_get(SINGLE_LOGIN_HISTORY_UNAME, ''); |
$uname = variable_get(SINGLE_LOGIN_HISTORY_UNAME, ''); |
| 200 |
|
|
| 201 |
$out = ''; |
$out = ''; |
| 202 |
$out .= drupal_get_form('single_login_history_form_uid', $uid, $uname); |
$out .= drupal_get_form('single_login_history_form_uid', $uid, $uname); |
| 203 |
$out .= drupal_get_form('single_login_history_form_list', $uid); |
$out .= drupal_get_form('single_login_history_form_list', $uid); |
| 204 |
|
|
| 205 |
return $out; |
return $out; |
| 206 |
} |
} |
| 207 |
|
|
| 208 |
|
/** |
| 209 |
|
* |
| 210 |
|
*/ |
| 211 |
function single_login_history_form_uid($uid, $uname) { |
function single_login_history_form_uid($uid, $uname) { |
| 212 |
$form = array(); |
$form = array(); |
| 213 |
|
|
| 214 |
$form['uid_fieldset'] = array( |
$form['uid_fieldset'] = array( |
| 215 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 216 |
'#title' => t('User history preferences'), |
'#title' => t('User history preferences'), |
| 217 |
'#collapsible' => true, |
'#collapsible' => TRUE, |
| 218 |
'#collapsed' => false, |
'#collapsed' => FALSE, |
| 219 |
); |
); |
| 220 |
$form['uid_fieldset']['history_for_uid'] = array( |
$form['uid_fieldset']['history_for_uid'] = array( |
| 221 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 222 |
'#title' => 'User ID', |
'#title' => 'User ID', |
| 223 |
'#default_value' => $uid, |
'#default_value' => $uid, |
| 224 |
); |
); |
| 225 |
$form['uid_fieldset']['history_for_uname'] = array( |
$form['uid_fieldset']['history_for_uname'] = array( |
| 226 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 227 |
'#title' => 'User name', |
'#title' => 'User name', |
| 228 |
'#default_value' => $uname, |
'#default_value' => $uname, |
| 229 |
'#description' => t('If name is set ID is selected automatically by name'), |
'#description' => t('If name is set ID is selected automatically by name'), |
| 230 |
); |
); |
| 231 |
$form['uid_fieldset']['submit_btn'] = array( |
$form['uid_fieldset']['submit_btn'] = array( |
| 232 |
'#type' => 'submit', |
'#type' => 'submit', |
| 233 |
'#value' => 'Show', |
'#value' => 'Show', |
| 234 |
); |
); |
| 235 |
|
|
| 236 |
return $form; |
return $form; |
| 237 |
} |
} |
| 238 |
|
|
| 239 |
|
/** |
| 240 |
|
* |
| 241 |
|
*/ |
| 242 |
function single_login_history_form_uid_submit($form_id, $form_values) { |
function single_login_history_form_uid_submit($form_id, $form_values) { |
| 243 |
if (strlen($form_values['history_for_uname']) && $form_values['history_for_uid'] == variable_get(SINGLE_LOGIN_HISTORY_UID, 0)) { |
if (strlen($form_values['history_for_uname']) && $form_values['history_for_uid'] == variable_get(SINGLE_LOGIN_HISTORY_UID, 0)) { |
| 244 |
$name = $form_values['history_for_uname']; |
$name = $form_values['history_for_uname']; |
| 245 |
$id = 0; |
$id = 0; |
| 246 |
|
|
| 247 |
$res = db_query("SELECT * FROM {users} WHERE name = '%s'", $name); |
$res = db_query("SELECT * FROM {users} WHERE name = '%s'", $name); |
| 248 |
if (db_num_rows($res)) { |
if (db_num_rows($res)) { |
| 249 |
$row = db_fetch_object($res); |
$row = db_fetch_object($res); |
| 250 |
$id = $row->uid; |
$id = $row->uid; |
| 251 |
} |
} |
| 252 |
} else { |
} |
| 253 |
$id = intval($form_values['history_for_uid']); |
else { |
| 254 |
$name = ''; |
$id = intval($form_values['history_for_uid']); |
| 255 |
|
$name = ''; |
| 256 |
$res = db_query("SELECT * FROM {users} WHERE uid = %d", $id); |
|
| 257 |
if (db_num_rows($res)) { |
$res = db_query("SELECT * FROM {users} WHERE uid = %d", $id); |
| 258 |
$row = db_fetch_object($res); |
if (db_num_rows($res)) { |
| 259 |
$name = $row->name; |
$row = db_fetch_object($res); |
| 260 |
} |
$name = $row->name; |
| 261 |
} |
} |
| 262 |
|
} |
| 263 |
|
|
| 264 |
variable_set(SINGLE_LOGIN_HISTORY_UID, $id); |
variable_set(SINGLE_LOGIN_HISTORY_UID, $id); |
| 265 |
variable_set(SINGLE_LOGIN_HISTORY_UNAME, $name); |
variable_set(SINGLE_LOGIN_HISTORY_UNAME, $name); |
| 266 |
} |
} |
| 267 |
|
|
| 268 |
|
/** |
| 269 |
|
* |
| 270 |
|
*/ |
| 271 |
function single_login_history_form_list($uid) { |
function single_login_history_form_list($uid) { |
| 272 |
$result = db_query("SELECT * FROM {single_login_history} WHERE uid = %d", $uid); |
$result = db_query("SELECT * FROM {single_login_history} WHERE uid = %d", $uid); |
| 273 |
|
|
| 274 |
$form = array(); |
$form = array(); |
| 275 |
$form['head'] = array( |
$form['head'] = array( |
| 276 |
'#type' => 'item', |
'#type' => 'item', |
| 277 |
'#title' => t('Result'), |
'#title' => t('Result'), |
| 278 |
); |
); |
| 279 |
|
|
| 280 |
if (!db_num_rows($result)) { |
if (!db_num_rows($result)) { |
| 281 |
$form['head']['#description'] = t('History for this user is empty'); |
$form['head']['#description'] = t('History for this user is empty'); |
| 282 |
} else { |
} |
| 283 |
$rows = array(); |
else { |
| 284 |
while ($row = db_fetch_object($result)) { |
$rows = array(); |
| 285 |
$rows[] = array($row->history_id, date("d.m.Y G:i", $row->date), $row->ip, $row->browser); |
while ($row = db_fetch_object($result)) { |
| 286 |
} |
$rows[] = array($row->history_id, date("d.m.Y G:i", $row->date), $row->ip, $row->browser); |
| 287 |
$form['body'] = array( |
} |
| 288 |
'#prefix' => '<div>', |
$form['body'] = array( |
| 289 |
'#value' => theme('table', array(t('ID'), t('Date'), t('IP'), t('Browser')), $rows), |
'#prefix' => '<div>', |
| 290 |
'#suffix' => '</div>', |
'#value' => theme('table', array(t('ID'), t('Date'), t('IP'), t('Browser')), $rows), |
| 291 |
); |
'#suffix' => '</div>', |
| 292 |
} |
); |
| 293 |
|
} |
| 294 |
|
|
| 295 |
return $form; |
return $form; |
| 296 |
} |
} |
| 297 |
|
|
| 298 |
/** |
/** |
| 299 |
* Implementation of hook_user() |
* Implementation of hook_user() |
|
* |
|
|
* @param string $op |
|
|
* @param array $edit |
|
|
* @param object $account |
|
|
* @param string $category |
|
| 300 |
*/ |
*/ |
| 301 |
function single_login_user($op, &$edit, &$account, $category = NULL) { |
function single_login_user($op, &$edit, &$account, $category = NULL) { |
| 302 |
global $user; |
global $user; |
| 303 |
|
|
| 304 |
switch ($op) { |
switch ($op) { |
| 305 |
case 'login': |
case 'login': |
| 306 |
if (_single_login_is_user_single(array_keys($user->roles))) { |
if (_single_login_is_user_single(array_keys($user->roles))) { |
| 307 |
$time = time(); |
$time = time(); |
| 308 |
|
|
| 309 |
$sql = "INSERT INTO {single_login_history} SET |
$sql = "INSERT INTO {single_login_history} SET |
| 310 |
uid = %1\$d, session_id = '%2\$s', date = %3\$d, |
uid = %1\$d, session_id = '%2\$s', date = %3\$d, |
| 311 |
ip = '%4\$s', browser = '5\$%s' |
ip = '%4\$s', browser = '5\$%s' |
| 312 |
ON DUPLICATE KEY UPDATE |
ON DUPLICATE KEY UPDATE |
| 313 |
date = %3\$d, type = 'login'"; |
date = %3\$d, type = 'login'"; |
| 314 |
$sql = sprintf($sql, $account->uid, session_id(), $time, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']); |
$sql = sprintf($sql, $account->uid, session_id(), $time, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']); |
| 315 |
db_query($sql); |
db_query($sql); |
| 316 |
|
|
| 317 |
$sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d"; |
$sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d"; |
| 318 |
$sql = sprintf($sql, $account->uid, $time, variable_get(SINGLE_LOGIN_TREAT_ONLINE, SINGLE_LOGIN_DEF_TREAT_ONLINE)); |
$sql = sprintf($sql, $account->uid, $time, variable_get(SINGLE_LOGIN_TREAT_ONLINE, SINGLE_LOGIN_DEF_TREAT_ONLINE)); |
| 319 |
if (db_num_rows(db_query($sql)) > 0) { |
if (db_num_rows(db_query($sql)) > 0) { |
| 320 |
// if the current user is not the only logged with this account |
// if the current user is not the only logged with this account |
| 321 |
$sql = "INSERT INTO {single_login} (uid, counter) VALUES (%d, %d) ON DUPLICATE KEY UPDATE counter = counter + 1"; |
$sql = "INSERT INTO {single_login} (uid, counter) VALUES (%d, %d) ON DUPLICATE KEY UPDATE counter = counter + 1"; |
| 322 |
db_query(sprintf($sql, $account->uid, 1)); |
db_query(sprintf($sql, $account->uid, 1)); |
| 323 |
$sql = "DELETE FROM {sessions} WHERE uid = %d AND sid != '%s'"; |
$sql = "DELETE FROM {sessions} WHERE uid = %d AND sid <> '%s'"; |
| 324 |
db_query(sprintf($sql, $account->uid, session_id())); |
db_query(sprintf($sql, $account->uid, session_id())); |
| 325 |
} else { |
} |
| 326 |
// if current user is the only who logged in with current account |
else { |
| 327 |
db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid)); |
// if current user is the only who logged in with current account |
| 328 |
} |
db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid)); |
| 329 |
|
} |
| 330 |
$res = db_query(sprintf("SELECT counter FROM {single_login} WHERE uid = %d", $account->uid)); |
|
| 331 |
$ping_pong_val = (($row = db_fetch_object($res)) === false) ? 0 : $row->counter; |
$res = db_query(sprintf("SELECT counter FROM {single_login} WHERE uid = %d", $account->uid)); |
| 332 |
if ($ping_pong_val >= variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS)) { |
$ping_pong_val = (($row = db_fetch_object($res)) === FALSE) ? 0 : $row->counter; |
| 333 |
db_query(sprintf("UPDATE {users} SET status = 0 WHERE uid = %d", $account->uid)); |
if ($ping_pong_val >= variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS)) { |
| 334 |
|
db_query(sprintf("UPDATE {users} SET status = 0 WHERE uid = %d", $account->uid)); |
| 335 |
$_REQUEST['destination'] = 'single_login/blocked'; |
|
| 336 |
|
$_REQUEST['destination'] = 'single_login/blocked'; |
| 337 |
user_logout(); |
|
| 338 |
} elseif ($ping_pong_val) { |
user_logout(); |
| 339 |
$relogins_left = variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS) - $ping_pong_val; |
} |
| 340 |
drupal_set_message(variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED) . $relogins_left); |
elseif ($ping_pong_val) { |
| 341 |
} |
$relogins_left = variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS) - $ping_pong_val; |
| 342 |
} |
drupal_set_message(variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED) . $relogins_left); |
| 343 |
|
} |
| 344 |
|
} |
| 345 |
|
|
| 346 |
_single_login_update_sess_field($account->uid); |
_single_login_update_sess_field($account->uid); |
| 347 |
|
|
| 348 |
break; |
break; |
| 349 |
} |
} |
| 350 |
} |
} |
| 351 |
|
|
| 352 |
|
/** |
| 353 |
|
* |
| 354 |
|
*/ |
| 355 |
function single_login_static_page($op) { |
function single_login_static_page($op) { |
| 356 |
switch ($op) { |
switch ($op) { |
| 357 |
case 'blocked': |
case 'blocked': |
| 358 |
return variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED); |
return variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED); |
| 359 |
default: |
default: |
| 360 |
drupal_goto(); |
drupal_goto(); |
| 361 |
} |
} |
| 362 |
} |
} |
| 363 |
|
|
| 364 |
/** |
/** |
| 365 |
* Implementation of cron job. |
* Implementation of hook_cron(). |
|
* |
|
| 366 |
*/ |
*/ |
| 367 |
function single_login_cron() { |
function single_login_cron() { |
| 368 |
$clear_older_than_days = intval(variable_get(SINGLE_LOGIN_STORE_HISTORY, SINGLE_LOGIN_DEF_STORE_HISTORY)); |
$clear_older_than_days = intval(variable_get(SINGLE_LOGIN_STORE_HISTORY, SINGLE_LOGIN_DEF_STORE_HISTORY)); |
| 369 |
if ($clear_older_than_days > 0) { |
if ($clear_older_than_days > 0) { |
| 370 |
$clear_older_than_secs = $clear_older_than_days * 24 * 60 * 60; |
$clear_older_than_secs = $clear_older_than_days * 24 * 60 * 60; |
| 371 |
$sql = "DELETE FROM {single_login_history} WHERE %d - date > %d"; |
$sql = "DELETE FROM {single_login_history} WHERE %d - date > %d"; |
| 372 |
$sql = sprintf($sql, time(), $clear_older_than_secs); |
$sql = sprintf($sql, time(), $clear_older_than_secs); |
| 373 |
db_query($sql); |
db_query($sql); |
| 374 |
} |
} |
| 375 |
} |
} |
| 376 |
|
|
| 377 |
|
/** |
| 378 |
|
* |
| 379 |
|
*/ |
| 380 |
function _single_login_is_user_single(array $user_roles) { |
function _single_login_is_user_single(array $user_roles) { |
| 381 |
$roles_single_login = variable_get(SINGLE_LOGIN_CHECK_ROLES, array()); |
$roles_single_login = variable_get(SINGLE_LOGIN_CHECK_ROLES, array()); |
| 382 |
|
|
| 383 |
foreach ($user_roles as $role_id) { |
foreach ($user_roles as $role_id) { |
| 384 |
if (in_array($role_id, $roles_single_login)) return true; |
if (in_array($role_id, $roles_single_login)) { |
| 385 |
} |
return TRUE; |
| 386 |
|
} |
| 387 |
|
} |
| 388 |
|
|
| 389 |
return false; |
return FALSE; |
| 390 |
} |
} |
| 391 |
|
|
| 392 |
|
/** |
| 393 |
|
* |
| 394 |
|
*/ |
| 395 |
function _single_login_get_session_id_fid() { |
function _single_login_get_session_id_fid() { |
| 396 |
$res = db_query('SELECT fid FROM {profile_fields} WHERE name = \'profile_current_session_id\''); |
$res = db_query('SELECT fid FROM {profile_fields} WHERE name = \'profile_current_session_id\''); |
| 397 |
$row = db_fetch_object($res); |
$row = db_fetch_object($res); |
| 398 |
return $row->fid; |
return $row->fid; |
| 399 |
} |
} |
| 400 |
|
|
| 401 |
|
/** |
| 402 |
|
* |
| 403 |
|
*/ |
| 404 |
function _single_login_update_sess_field($uid) { |
function _single_login_update_sess_field($uid) { |
| 405 |
$fid = _single_login_get_session_id_fid(); |
$fid = _single_login_get_session_id_fid(); |
| 406 |
db_query("DELETE FROM {profile_values} WHERE uid = %d AND fid = %d", $uid, $fid); |
db_query("DELETE FROM {profile_values} WHERE uid = %d AND fid = %d", $uid, $fid); |
| 407 |
$sql = "INSERT INTO |
$sql = "INSERT INTO |
| 408 |
{profile_values} |
{profile_values} |
| 409 |
SET |
SET |
| 410 |
fid = %d, |
fid = %d, |
| 411 |
uid = %d, |
uid = %d, |
| 412 |
value = '%s'"; |
value = '%s'"; |
| 413 |
db_query(sprintf($sql, $fid, $uid, session_id())); |
db_query(sprintf($sql, $fid, $uid, session_id())); |
| 414 |
} |
} |