Index: session_expire.module
===================================================================
--- session_expire.module	(revision 21)
+++ session_expire.module	(working copy)
@@ -1,6 +1,4 @@
 <?php
-// $Id: session_expire.module,v 1.2.2.2 2009/02/05 03:03:10 kbahey Exp $
-
 /**
  * @file
  * Expires rows from the session table older than a certain time.
@@ -8,10 +6,12 @@
  * @copyright Copyright 2007 Khalid Baheyeldin http://2bits.com
  */
 
+define('SESSION_EXPIRE_LAST',     'session_expire_last');
+
 define('SESSION_EXPIRE_INTERVAL', 'session_expire_interval');
 define('SESSION_EXPIRE_AGE',      'session_expire_age');
-define('SESSION_EXPIRE_MODE',     'session_expire_mode');
-define('SESSION_EXPIRE_LAST',     'session_expire_last');
+define('SESSION_EXPIRE_INTERVAL_ANONYMOUS', 'session_expire_interval_anonymous');
+define('SESSION_EXPIRE_AGE_ANONYMOUS',      'session_expire_age_anonymous');
 
 /**
  * Implementation of hook_menu().
@@ -38,36 +38,54 @@
 
   $interval = drupal_map_assoc(array(0, 7200, 10800, 21600, 43200, 86400, 172800, 259200, 604800), 'format_interval');
   $interval['0'] = t('Everytime');
-  $form[SESSION_EXPIRE_INTERVAL] = array(
+  
+  $period = drupal_map_assoc(array(1800, 3600, 7200, 10800, 21600, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
+  $period['1000000000'] = t('Never');
+  
+  $form['anonymous'] = array(
+    '#title' => t('Settings for anonymous sessions'),    
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+  );  
+
+  $form['anonymous'][SESSION_EXPIRE_INTERVAL_ANONYMOUS] = array(
     '#type'          => 'select',
     '#title'         => t('Interval'),
-    '#default_value' => variable_get(SESSION_EXPIRE_INTERVAL, 86400),
+    '#default_value' => variable_get(SESSION_EXPIRE_INTERVAL_ANONYMOUS, 86400),
     '#options'       => $interval,
     '#description'   => t('Run the cleanup at the specified interval. This tells Drupal how often to run the cleanup. On a busy site, you want that to be more frequent (e.g. every day at a minimum). You don\'t want it to be too frequent though (e.g. every hour), as it can tie up the sessions table for a long time. Cron must be configured to run more frequently than the value you chose here.')
   );
 
-  $period = drupal_map_assoc(array(1800, 3600, 7200, 10800, 21600, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
-  $period['1000000000'] = t('Never');
+  $form['anonymous'][SESSION_EXPIRE_AGE_ANONYMOUS] = array(
+    '#type'          => 'select',
+    '#title'         => t('Age'),
+    '#default_value' => variable_get(SESSION_EXPIRE_AGE_ANONYMOUS, 604800),
+    '#options'       => $period,
+    '#description'   => t(' Expire sessions that are older than the specified age. Older entries will be discarded.')
+  );
+  
+  $form['authenticated'] = array(
+    '#title' => t('Settings for authenticated sessions'),    
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE,
+  );  
 
-  $form[SESSION_EXPIRE_AGE] = array(
+  $form['authenticated'][SESSION_EXPIRE_INTERVAL] = array(
     '#type'          => 'select',
+    '#title'         => t('Interval'),
+    '#default_value' => variable_get(SESSION_EXPIRE_INTERVAL, 86400),
+    '#options'       => $interval,
+    '#description'   => t('Run the cleanup at the specified interval. This tells Drupal how often to run the cleanup. On a busy site, you want that to be more frequent (e.g. every day at a minimum). You don\'t want it to be too frequent though (e.g. every hour), as it can tie up the sessions table for a long time. Cron must be configured to run more frequently than the value you chose here.')
+  );
+
+  $form['authenticated'][SESSION_EXPIRE_AGE] = array(
+    '#type'          => 'select',
     '#title'         => t('Age'),
     '#default_value' => variable_get(SESSION_EXPIRE_AGE, 604800),
     '#options'       => $period,
     '#description'   => t(' Expire sessions that are older than the specified age. Older entries will be discarded.')
   );
 
-  $form[SESSION_EXPIRE_MODE] = array(
-    '#type'          => 'radios',
-    '#title'         => t('Session types'),
-    '#default_value' => variable_get(SESSION_EXPIRE_MODE, 0),
-    '#options'       => array(
-      t('Anonymous'),
-      t('Both anonymous and authenticated users'),
-      ),
-    '#description'   => t('Types of sessions to discard. This option indicates whether only anonymous users, or both anonymous and authenticated users are expired. Note that if you choose authenticated users, they will be logged off and have to login again after the "age" specified above.'),
-  );
-
   return system_settings_form($form);
 }
 
@@ -82,24 +100,38 @@
     variable_set(SESSION_EXPIRE_LAST, time());
   }
 
-  // Check if we should run, this should only be once a day
-  if (time() > $last_run_time + variable_get(SESSION_EXPIRE_INTERVAL, 86400)) {
+  // Check if we should run, check against smallest expire interval setting
+  if (time() > $last_run_time + min(variable_get(SESSION_EXPIRE_INTERVAL, 86400), variable_get(SESSION_EXPIRE_INTERVAL_ANONYMOUS, 86400))) {
+    
     $timestamp = time() - variable_get(SESSION_EXPIRE_AGE, 604800);
-
-    // Check if we should delete anonymous only or both anonymous and authenticated users
-    $extra_cond = '';
-    $mode = variable_get(SESSION_EXPIRE_MODE, 0);
-    if (!$mode) {
-      $extra_cond = 'AND uid = 0';
+    $timestamp_anonymous = time() - variable_get(SESSION_EXPIRE_AGE_ANONYMOUS, 604800);
+    
+    $where = '';    
+    // Check if we should delete authenticated sessions
+    if (variable_get(SESSION_EXPIRE_AGE, 0) < 1000000000 && time() > $last_run_time + variable_get(SESSION_EXPIRE_INTERVAL, 86400)) {
+      $where = strtr('(timestamp < %d AND uid != 0)', array('%d' => $timestamp));
     }
-
-    // Perform the deletion
-    db_query("DELETE FROM {sessions} WHERE timestamp < %d $extra_cond", $timestamp);
-
-    // Write to the watchdog
-    watchdog('cron', 'Number of sessions deleted: '. db_affected_rows());
-
-    // Set the last time we deleted
-    variable_set(SESSION_EXPIRE_LAST, time());
+    // Check if we should delete anonymous sessions
+    if (variable_get(SESSION_EXPIRE_AGE_ANONYMOUS, 0) < 1000000000 && time() > $last_run_time + variable_get(SESSION_EXPIRE_INTERVAL_ANONYMOUS, 86400)) {
+      if (!empty($where)) {
+        $where .= ' OR ';
+      }
+      $where .= strtr('(timestamp < %d AND uid = 0)', array('%d' => $timestamp_anonymous));
+    }
+    
+    // if we have a where statement to work with
+    if (!empty($where)) {    
+      // Perform the deletion
+      db_query("DELETE FROM {sessions} WHERE $where");
+      // Write to the watchdog
+      watchdog('session expire', 'Number of sessions deleted: '. db_affected_rows());
+      // Set the last time we deleted
+      variable_set(SESSION_EXPIRE_LAST, time());
+    }
+    else {
+      // Write to the watchdog
+      watchdog('session expire', 'No sessions deleted, no matches found.');
+    }
+    
   }
 }
