<?php

// $Id: session_api.module,v 1.1.2.6 2009/09/02 01:28:59 jhedstrom Exp $

/**
 * @file
 * The Session API module provides a quick interface for storing
 * information in the session.
 */

/**
 * Determine if cookies are enabled.
 */
function session_api_available() {
  return !empty($_COOKIE);
}

/**
 * Return a Session API ID corresponding to the current session. The Session
 * API ID is the mapping of the private php session_id [1] to the ID used by
 * modules leveraging the Session API.
 *
 * 1. http://php.net/manual/en/function.session-id.php
 */
function session_api_get_sid() {
  if (!session_api_available()) {
    return FALSE;
  }

  if (!is_numeric($_SESSION['session_api_id'])) {
    $sid = FALSE;

    // First, check if the session ID has changed by pulling down the
    // Session API cookie.
    if (isset($_COOKIE['session_api_session']) && $_COOKIE['session_api_session'] != session_id()) {
      // Check if this sid exists.
      $sid = db_result(db_query("SELECT sid FROM {session_api} WHERE session_id = '%s'", array(':session_id' => $_COOKIE['session_api_session'])));
    }

    if ($sid) {
      // Update with new session ID.
      $rec = new stdClass;
      $rec->sid = $sid;
      $rec->session_id = session_id();
      drupal_write_record('session_api', $rec, 'sid');
      setcookie('session_api_session', session_id(), time() + variable_get('session_api_cookie_expire_time', 2592000));
    }
    else {
      // Secondly, check if id exists in db.
      $sid = db_result(db_query("SELECT sid FROM {session_api} WHERE session_id = '%s'", array(':session_id' => session_id())));
    }
    
    if (!$sid) {
      // No sid exists, create new one.
      $rec = new stdClass();
      $rec->session_id = session_id();
      drupal_write_record('session_api', $rec);
      $sid = $rec->sid;

      // Set cookie.
      setcookie('session_api_session', session_id(), time() + variable_get('session_api_cookie_expire_time', 2592000));
    }
      
    $_SESSION['session_api_id'] = $sid;
  }
  return $_SESSION['session_api_id'];
}

/**
 * Implementation of hook_menu().
 */
function session_api_menu() {
  $items['admin/settings/session-api'] = array(
    'title' => t('Session API Configuration'),
    'description' => t('Configure Session API behavior'),
    'access arguments' => array('administer site configuration'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('session_api_settings_form'),
    'file' => 'session_api.admin.inc',
  );

  return $items;
}

/**
 * Implementation of hook_cron().
 */
function session_api_cron() {
  // Fetch list of outdated sids.
  $result = db_query("SELECT sap.sid FROM {session_api} sap LEFT JOIN {sessions} s ON (sap.session_id = s.sid) WHERE s.sid IS NULL");
  $outdated_sids = array();
  while ($session = db_fetch_object($result)) {
    $outdated_sids[] = $session->sid;
  }
  if (!empty($outdated_sids)) {
    module_invoke_all('session_api_cleanup', $outdated_sids);
    db_query('DELETE FROM {session_api} WHERE sid IN (' . implode(',', $outdated_sids) . ')');
  }
}

/**
 * Implementation of hook_simpletest().
 */
function session_api_simpletest() {
  $module_name = 'session_api';
  $dir = drupal_get_path('module', $module_name) .'/tests';
  $tests = file_scan_directory($dir, '\.test$');
  return array_keys($tests);
}
