<?php
/**
 *\licence GPL 2005-2010  The osCSS developers - osCSS Open Source E-commerce
 *\portion code Copyright (c) 2002 osCommerce
 *\package osCSS-2 <www http://www.oscss.org>
 *\version 2.0.9
 *\date  19/06/10, 21:07
 *\author oscim <mail aurelien@oscim.fr> <www http://www.oscim.fr>
 *\encode UTF-8
 *\file whos_online.php
 *\dir includes/functions/
 *\brief librairie function pour detection des users online 
*/

  function tep_update_whos_online() {
    global $customer_id;
    $DB=Database::getInstance();

    if (tep_session_is_registered('customer_id')) {
      $wo_customer_id = $customer_id;

      $customer_query = $DB->query("select customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
      $customer = $customer_query->fetchAssoc();

      $wo_full_name = $customer['customers_firstname'] . ' ' . $customer['customers_lastname'];
    } else {
      $wo_customer_id = '';
      $wo_full_name = 'Guest';
    }

    $wo_session_id = tep_session_id();
    $wo_ip_address = getenv('REMOTE_ADDR');
    $wo_last_page_url = getenv('REQUEST_URI');

    $current_time = time();
    $xx_mins_ago = ($current_time - 900);

// remove entries that have expired
    $DB->query("delete from " . TABLE_WHOS_ONLINE . " where time_last_click < '" . $xx_mins_ago . "'");

    $stored_customer_query = $DB->query("select count(*) as count from " . TABLE_WHOS_ONLINE . " where session_id = '" . $DB->db_input($wo_session_id) . "'");
    $stored_customer = $stored_customer_query->fetchAssoc();

    if ($stored_customer['count'] > 0) {
      $DB->query("update " . TABLE_WHOS_ONLINE . " set customer_id = '" . (int)$wo_customer_id . "', full_name = '" . $DB->db_input($wo_full_name) . "', ip_address = '" . $DB->db_input($wo_ip_address) . "', time_last_click = '" . $DB->db_input($current_time) . "', last_page_url = '" . $DB->db_input($wo_last_page_url) . "' where session_id = '" . $DB->db_input($wo_session_id) . "'");
    } else {
      $DB->query("insert into " . TABLE_WHOS_ONLINE . " (customer_id, full_name, session_id, ip_address, time_entry, time_last_click, last_page_url) values ('" . (int)$wo_customer_id . "', '" . $DB->db_input($wo_full_name) . "', '" . $DB->db_input($wo_session_id) . "', '" . $DB->db_input($wo_ip_address) . "', '" . $DB->db_input($current_time) . "', '" . $DB->db_input($current_time) . "', '" . $DB->db_input($wo_last_page_url) . "')");
    }
  }
?>