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

Diff of /contributions/modules/single_login/single_login.module

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

revision 1.1, Tue Nov 6 15:31:58 2007 UTC revision 1.1.2.1, Tue Oct 7 21:14:30 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id:  // $Id$
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');
# Line 17  define('SINGLE_LOGIN_DEF_BLOCKED', 'You Line 40  define('SINGLE_LOGIN_DEF_BLOCKED', 'You
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_init().
45     */
46  function single_login_init() {  function single_login_init() {
47          global $user;    global $user;
48    
49          if (intval($user->uid)) {    if (intval($user->uid)) {
50                  $time = time();      $time = time();
51    
52                  if (_single_login_is_user_single(array_keys($user->roles))) {      if (_single_login_is_user_single(array_keys($user->roles))) {
53                          $sql = "INSERT INTO {single_login_history} SET        $sql = "INSERT INTO {single_login_history} SET
54                                                  uid = %1\$d, session_id = '%2\$s', date = %3\$d,              uid = %1\$d, session_id = '%2\$s', date = %3\$d,
55                                                  ip = '%4\$s', browser = '%5\$s', type = 'cookie'              ip = '%4\$s', browser = '%5\$s', type = 'cookie'
56                                          ON DUPLICATE KEY UPDATE            ON DUPLICATE KEY UPDATE
57                                                  date = %3\$d";              date = %3\$d";
58                          $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']);
59                          db_query($sql);        db_query($sql);
60    
61                          $sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d";        $sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d";
62                          $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));
63                          if (db_num_rows(db_query($sql)) > 1) {        if (db_num_rows(db_query($sql)) > 1) {
64                                  // if the current user is not the only logged with this account          // if the current user is not the only logged with this account
65                                  $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";
66                                  db_query(sprintf($sql, $user->uid, 1));          db_query(sprintf($sql, $user->uid, 1));
67                                  $sql = "DELETE FROM {sessions} WHERE uid = %d AND sid != '%s'";          $sql = "DELETE FROM {sessions} WHERE uid = %d AND sid <> '%s'";
68                                  db_query(sprintf($sql, $user->uid, session_id()));          db_query(sprintf($sql, $user->uid, session_id()));
69                          } else {        }
70                                  // if current user is the only who logged in with current account        else {
71                                  db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid));          // if current user is the only who logged in with current account
72                          }          db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid));
73                  }        }
74        }
75                  _single_login_update_sess_field($user->uid);  
76          }      _single_login_update_sess_field($user->uid);
77  }    }
78    }
 /**  
  * 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,  
                 );  
         }  
79    
80          return $items;  /**
81     * Implementation of hook_menu().
82     */
83    function single_login_menu($may_cache) {
84      $items = array();
85      if ($may_cache) {
86        $items[] = array(
87          'path' => 'admin/settings/single_login',
88          'title' => t('Single login settigns'),
89          'callback' => 'drupal_get_form',
90          'callback arguments' => 'single_login_settings',
91          'access' => user_access('administer site configuration'),
92          'type' => MENU_NORMAL_ITEM,
93        );
94        $items[] = array(
95          'path' => 'admin/settings/single_login_history',
96          'title' => t('Single login session history'),
97          'callback' => 'single_login_history',
98          'access' => user_access('administer site configuration'),
99          'type' => MENU_NORMAL_ITEM,
100        );
101        $items[] = array(
102          'path' => 'single_login/blocked',
103          'title' => t('Account was blocked'),
104          'callback' => 'single_login_static_page',
105          'callback arguments' => 'blocked',
106          'access' => TRUE,
107          'type' => MENU_CALLBACK,
108        );
109      }
110    
111      return $items;
112  }  }
113    
114  /**  /**
115   * Admin settings   * Administration settings page
  *  
116   */   */
117  function single_login_settings() {  function single_login_settings() {
118          $user_roles = array();    $user_roles = array();
119          $res = db_query("SELECT * FROM {role} WHERE 1");    $res = db_query("SELECT * FROM {role} WHERE 1");
120          while ($row = db_fetch_object($res)) {    while ($row = db_fetch_object($res)) {
121                  $user_roles[$row->rid] = $row->name;      $user_roles[$row->rid] = $row->name;
122          }    }
123    
124          $form = array();    $form = array();
125          $form['sub_main'] = array(    $form['sub_main'] = array(
126                  '#type'          => 'fieldset',      '#type' => 'fieldset',
127                  '#title'         => t('Main settings'),      '#title' => t('Main settings'),
128                  '#collapsible'   => true,      '#collapsible' => TRUE,
129                  '#collapsed'     => false,      '#collapsed' => FALSE,
130          );    );
131          $form['sub_main'][SINGLE_LOGIN_CHECK_ROLES] = array(    $form['sub_main'][SINGLE_LOGIN_CHECK_ROLES] = array(
132                  '#title'         => t('Check login for roles'),      '#title' => t('Check login for roles'),
133                  '#type'                 => 'select',      '#type' => 'select',
134                  '#multiple'             => true,      '#multiple' => TRUE,
135                  '#options'              => $user_roles,      '#options' => $user_roles,
136                  '#default_value'=> variable_get(SINGLE_LOGIN_CHECK_ROLES, array()),      '#default_value' => variable_get(SINGLE_LOGIN_CHECK_ROLES, array()),
137                  '#size'                 => 10,      '#size' => 10,
138          );    );
139          $form['sub_main'][SINGLE_LOGIN_TREAT_ONLINE] = array(    $form['sub_main'][SINGLE_LOGIN_TREAT_ONLINE] = array(
140                  '#type'          => 'textfield',      '#type' => 'textfield',
141                  '#title'         => t('Treat user online for seconds'),      '#title' => t('Treat user online for seconds'),
142                  '#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),
143          );    );
144          $form['sub_main'][SINGLE_LOGIN_MAX_RECONNECTIONS] = array(    $form['sub_main'][SINGLE_LOGIN_MAX_RECONNECTIONS] = array(
145                  '#type'          => 'textfield',      '#type' => 'textfield',
146                  '#title'         => t('Max login ping-pong values'),      '#title' => t('Max login ping-pong values'),
147                  '#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),
148          );    );
149          $form['sub_main'][SINGLE_LOGIN_STORE_HISTORY] = array(    $form['sub_main'][SINGLE_LOGIN_STORE_HISTORY] = array(
150                  '#type'          => 'textfield',      '#type' => 'textfield',
151                  '#title'         => t('Store sessions history for days (0 - infinite)'),      '#title' => t('Store sessions history for days (0 - infinite)'),
152                  '#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),
153          );    );
154          $form['sub_msg'] = array(    $form['sub_msg'] = array(
155                  '#type'          => 'fieldset',      '#type' => 'fieldset',
156                  '#title'         => t('Messages settings'),      '#title' => t('Messages settings'),
157                  '#collapsible'   => true,      '#collapsible' => TRUE,
158                  '#collapsed'     => false,      '#collapsed' => FALSE,
159          );    );
160          $form['sub_msg'][SINGLE_LOGIN_MSG_RELOGGED] = array(    $form['sub_msg'][SINGLE_LOGIN_MSG_RELOGGED] = array(
161                  '#type'          => 'textfield',      '#type' => 'textfield',
162                  '#title'         => t('Relogin message'),      '#title' => t('Relogin message'),
163                  '#maxlength'     => 500,      '#maxlength' => 500,
164                  '#default_value' => variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED),      '#default_value' => variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED),
165          );    );
166          $form['sub_msg'][SINGLE_LOGIN_MSG_BLOCKED] = array(    $form['sub_msg'][SINGLE_LOGIN_MSG_BLOCKED] = array(
167                  '#type'          => 'textfield',      '#type' => 'textfield',
168                  '#title'         => t('Account blocked message'),      '#title' => t('Account blocked message'),
169                  '#maxlength'     => 500,      '#maxlength' => 500,
170                  '#default_value' => variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED),      '#default_value' => variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED),
171          );    );
172          if (module_exists('googleanalytics')) {    if (module_exists('googleanalytics')) {
173                  $form['sub_google'] = array(      $form['sub_google'] = array(
174                          '#type' => 'item',        '#type' => 'item',
175                          '#title' => t('Google Analytics user sessionID tracking'),        '#title' => t('Google Analytics user sessionID tracking'),
176                          '#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'))),
177                  );      );
178          }    }
179    
180          return system_settings_form($form);    return system_settings_form($form);
181  }  }
182    
183    /**
184     *
185     */
186  function single_login_history() {  function single_login_history() {
187          $uid = variable_get(SINGLE_LOGIN_HISTORY_UID, 0);    $uid = variable_get(SINGLE_LOGIN_HISTORY_UID, 0);
188          $uname = variable_get(SINGLE_LOGIN_HISTORY_UNAME, '');    $uname = variable_get(SINGLE_LOGIN_HISTORY_UNAME, '');
189    
190          $out = '';    $out = '';
191          $out .= drupal_get_form('single_login_history_form_uid', $uid, $uname);    $out .= drupal_get_form('single_login_history_form_uid', $uid, $uname);
192          $out .= drupal_get_form('single_login_history_form_list', $uid);    $out .= drupal_get_form('single_login_history_form_list', $uid);
193    
194          return $out;    return $out;
195  }  }
196    
197    /**
198     *
199     */
200  function single_login_history_form_uid($uid, $uname) {  function single_login_history_form_uid($uid, $uname) {
201          $form = array();    $form = array();
202    
203          $form['uid_fieldset'] = array(    $form['uid_fieldset'] = array(
204                  '#type'          => 'fieldset',      '#type' => 'fieldset',
205                  '#title'         => t('User history preferences'),      '#title' => t('User history preferences'),
206                  '#collapsible'   => true,      '#collapsible' => TRUE,
207                  '#collapsed'     => false,      '#collapsed' => FALSE,
208          );    );
209          $form['uid_fieldset']['history_for_uid'] = array(    $form['uid_fieldset']['history_for_uid'] = array(
210                  '#type' => 'textfield',      '#type' => 'textfield',
211                  '#title' => 'User ID',      '#title' => 'User ID',
212                  '#default_value' => $uid,      '#default_value' => $uid,
213          );    );
214          $form['uid_fieldset']['history_for_uname'] = array(    $form['uid_fieldset']['history_for_uname'] = array(
215                  '#type' => 'textfield',      '#type' => 'textfield',
216                  '#title' => 'User name',      '#title' => 'User name',
217                  '#default_value' => $uname,      '#default_value' => $uname,
218                  '#description' => t('If name is set ID is selected automatically by name'),      '#description' => t('If name is set ID is selected automatically by name'),
219          );    );
220          $form['uid_fieldset']['submit_btn'] = array(    $form['uid_fieldset']['submit_btn'] = array(
221                  '#type' => 'submit',      '#type' => 'submit',
222                  '#value' => 'Show',      '#value' => 'Show',
223          );    );
224    
225          return $form;    return $form;
226  }  }
227    
228    /**
229     *
230     */
231  function single_login_history_form_uid_submit($form_id, $form_values) {  function single_login_history_form_uid_submit($form_id, $form_values) {
232          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)) {
233                  $name = $form_values['history_for_uname'];      $name = $form_values['history_for_uname'];
234                  $id = 0;      $id = 0;
235    
236                  $res = db_query("SELECT * FROM {users} WHERE name = '%s'", $name);      $res = db_query("SELECT * FROM {users} WHERE name = '%s'", $name);
237                  if (db_num_rows($res)) {      if (db_num_rows($res)) {
238                          $row = db_fetch_object($res);        $row = db_fetch_object($res);
239                          $id = $row->uid;        $id = $row->uid;
240                  }      }
241          } else {    }
242                  $id = intval($form_values['history_for_uid']);    else {
243                  $name = '';      $id = intval($form_values['history_for_uid']);
244        $name = '';
245                  $res = db_query("SELECT * FROM {users} WHERE uid = %d", $id);  
246                  if (db_num_rows($res)) {      $res = db_query("SELECT * FROM {users} WHERE uid = %d", $id);
247                          $row = db_fetch_object($res);      if (db_num_rows($res)) {
248                          $name = $row->name;        $row = db_fetch_object($res);
249                  }        $name = $row->name;
250          }      }
251      }
252    
253          variable_set(SINGLE_LOGIN_HISTORY_UID, $id);    variable_set(SINGLE_LOGIN_HISTORY_UID, $id);
254          variable_set(SINGLE_LOGIN_HISTORY_UNAME, $name);    variable_set(SINGLE_LOGIN_HISTORY_UNAME, $name);
255  }  }
256    
257    /**
258     *
259     */
260  function single_login_history_form_list($uid) {  function single_login_history_form_list($uid) {
261          $result = db_query("SELECT * FROM {single_login_history} WHERE uid = %d", $uid);    $result = db_query("SELECT * FROM {single_login_history} WHERE uid = %d", $uid);
262    
263          $form = array();    $form = array();
264          $form['head'] = array(    $form['head'] = array(
265                  '#type' => 'item',      '#type' => 'item',
266                  '#title' => t('Result'),      '#title' => t('Result'),
267          );    );
268    
269          if (!db_num_rows($result)) {    if (!db_num_rows($result)) {
270                  $form['head']['#description'] = t('History for this user is empty');      $form['head']['#description'] = t('History for this user is empty');
271          } else {    }
272                  $rows = array();    else {
273                  while ($row = db_fetch_object($result)) {      $rows = array();
274                          $rows[] = array($row->history_id, date("d.m.Y G:i", $row->date), $row->ip, $row->browser);      while ($row = db_fetch_object($result)) {
275                  }        $rows[] = array($row->history_id, date("d.m.Y G:i", $row->date), $row->ip, $row->browser);
276                  $form['body'] = array(      }
277                          '#prefix' => '<div>',      $form['body'] = array(
278                          '#value' => theme('table', array(t('ID'), t('Date'), t('IP'), t('Browser')), $rows),        '#prefix' => '<div>',
279                          '#suffix' => '</div>',        '#value' => theme('table', array(t('ID'), t('Date'), t('IP'), t('Browser')), $rows),
280                  );        '#suffix' => '</div>',
281          }      );
282      }
283    
284          return $form;    return $form;
285  }  }
286    
287  /**  /**
288   * Implementation of hook_user()   * Implementation of hook_user()
  *  
  * @param string $op  
  * @param array $edit  
  * @param object $account  
  * @param string $category  
289   */   */
290  function single_login_user($op, &$edit, &$account, $category = NULL) {  function single_login_user($op, &$edit, &$account, $category = NULL) {
291          global $user;    global $user;
292    
293          switch ($op) {    switch ($op) {
294                  case 'login':      case 'login':
295                          if (_single_login_is_user_single(array_keys($user->roles))) {        if (_single_login_is_user_single(array_keys($user->roles))) {
296                                  $time = time();          $time = time();
297    
298                                  $sql = "INSERT INTO {single_login_history} SET          $sql = "INSERT INTO {single_login_history} SET
299                                                          uid = %1\$d, session_id = '%2\$s', date = %3\$d,                uid = %1\$d, session_id = '%2\$s', date = %3\$d,
300                                                          ip = '%4\$s', browser = '5\$%s'                ip = '%4\$s', browser = '5\$%s'
301                                                  ON DUPLICATE KEY UPDATE              ON DUPLICATE KEY UPDATE
302                                                          date = %3\$d, type = 'login'";                date = %3\$d, type = 'login'";
303                                  $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']);
304                                  db_query($sql);          db_query($sql);
305    
306                                  $sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d";          $sql = "SELECT * FROM {sessions} WHERE uid = %d AND %d - timestamp < %d";
307                                  $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));
308                                  if (db_num_rows(db_query($sql)) > 0) {          if (db_num_rows(db_query($sql)) > 0) {
309                                          // if the current user is not the only logged with this account            // if the current user is not the only logged with this account
310                                          $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";
311                                          db_query(sprintf($sql, $account->uid, 1));            db_query(sprintf($sql, $account->uid, 1));
312                                          $sql = "DELETE FROM {sessions} WHERE uid = %d AND sid != '%s'";            $sql = "DELETE FROM {sessions} WHERE uid = %d AND sid <> '%s'";
313                                          db_query(sprintf($sql, $account->uid, session_id()));            db_query(sprintf($sql, $account->uid, session_id()));
314                                  } else {          }
315                                          // if current user is the only who logged in with current account          else {
316                                          db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid));            // if current user is the only who logged in with current account
317                                  }            db_query(sprintf("DELETE FROM {single_login} WHERE uid = %d", $account->uid));
318            }
319                                  $res = db_query(sprintf("SELECT counter FROM {single_login} WHERE uid = %d", $account->uid));  
320                                  $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));
321                                  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;
322                                          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)) {
323              db_query(sprintf("UPDATE {users} SET status = 0 WHERE uid = %d", $account->uid));
324                                          $_REQUEST['destination'] = 'single_login/blocked';  
325              $_REQUEST['destination'] = 'single_login/blocked';
326                                          user_logout();  
327                                  } elseif ($ping_pong_val) {            user_logout();
328                                          $relogins_left = variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS) - $ping_pong_val;          }
329                                          drupal_set_message(variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED) . $relogins_left);          elseif ($ping_pong_val) {
330                                  }            $relogins_left = variable_get(SINGLE_LOGIN_MAX_RECONNECTIONS, SINGLE_LOGIN_DEF_MAX_RECONNECTIONS) - $ping_pong_val;
331                          }            drupal_set_message(variable_get(SINGLE_LOGIN_MSG_RELOGGED, SINGLE_LOGIN_DEF_RELOGGED) . $relogins_left);
332            }
333          }
334    
335                          _single_login_update_sess_field($account->uid);        _single_login_update_sess_field($account->uid);
336    
337                          break;        break;
338          }    }
339  }  }
340    
341    /**
342     *
343     */
344  function single_login_static_page($op) {  function single_login_static_page($op) {
345          switch ($op) {    switch ($op) {
346                  case 'blocked':      case 'blocked':
347                          return variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED);        return variable_get(SINGLE_LOGIN_MSG_BLOCKED, SINGLE_LOGIN_DEF_BLOCKED);
348                  default:      default:
349                          drupal_goto();        drupal_goto();
350          }    }
351  }  }
352    
353  /**  /**
354   * Implementation of cron job.   * Implementation of hook_cron().
  *  
355   */   */
356  function single_login_cron() {  function single_login_cron() {
357          $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));
358          if ($clear_older_than_days > 0) {    if ($clear_older_than_days > 0) {
359                  $clear_older_than_secs = $clear_older_than_days * 24 * 60 * 60;      $clear_older_than_secs = $clear_older_than_days * 24 * 60 * 60;
360                  $sql = "DELETE FROM {single_login_history} WHERE %d - date > %d";      $sql = "DELETE FROM {single_login_history} WHERE %d - date > %d";
361                  $sql = sprintf($sql, time(), $clear_older_than_secs);      $sql = sprintf($sql, time(), $clear_older_than_secs);
362                  db_query($sql);      db_query($sql);
363          }    }
364  }  }
365    
366    /**
367     *
368     */
369  function _single_login_is_user_single(array $user_roles) {  function _single_login_is_user_single(array $user_roles) {
370          $roles_single_login = variable_get(SINGLE_LOGIN_CHECK_ROLES, array());    $roles_single_login = variable_get(SINGLE_LOGIN_CHECK_ROLES, array());
371    
372          foreach ($user_roles as $role_id) {    foreach ($user_roles as $role_id) {
373                  if (in_array($role_id, $roles_single_login)) return true;      if (in_array($role_id, $roles_single_login)) {
374          }        return TRUE;
375        }
376      }
377    
378          return false;    return FALSE;
379  }  }
380    
381    /**
382     *
383     */
384  function _single_login_get_session_id_fid() {  function _single_login_get_session_id_fid() {
385          $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\'');
386          $row = db_fetch_object($res);    $row = db_fetch_object($res);
387          return $row->fid;    return $row->fid;
388  }  }
389    
390    /**
391     *
392     */
393  function _single_login_update_sess_field($uid) {  function _single_login_update_sess_field($uid) {
394          $fid = _single_login_get_session_id_fid();    $fid = _single_login_get_session_id_fid();
395          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);
396          $sql = "INSERT INTO    $sql = "INSERT INTO
397                                  {profile_values}          {profile_values}
398                          SET        SET
399                                  fid = %d,          fid = %d,
400                                  uid = %d,          uid = %d,
401                                  value = '%s'";          value = '%s'";
402          db_query(sprintf($sql, $fid, $uid, session_id()));    db_query(sprintf($sql, $fid, $uid, session_id()));
403  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2