home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / online.php < prev    next >
Encoding:
PHP Script  |  2006-09-04  |  6.5 KB  |  175 lines

  1. <?php
  2. // $Id: online.php 694 2006-09-04 11:33:22Z skalpa $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31. /**
  32.  * @package     kernel
  33.  * 
  34.  * @author        Kazumi Ono    <onokazu@xoops.org>
  35.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  36.  */
  37.  
  38. /**
  39.  * A handler for "Who is Online?" information
  40.  * 
  41.  * @package     kernel
  42.  * 
  43.  * @author        Kazumi Ono    <onokazu@xoops.org>
  44.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  45.  */
  46. class XoopsOnlineHandler
  47. {
  48.  
  49.     /**
  50.      * Database connection
  51.      * 
  52.      * @var    object
  53.      * @access    private
  54.      */
  55.     var $db;
  56.  
  57.     /**
  58.      * Constructor
  59.      * 
  60.      * @param    object  &$db    {@link XoopsHandlerFactory} 
  61.      */
  62.     function XoopsOnlineHandler(&$db)
  63.     {
  64.         $this->db =& $db;
  65.     }
  66.  
  67.     /**
  68.      * Write online information to the database
  69.      * 
  70.      * @param    int     $uid    UID of the active user
  71.      * @param    string  $uname  Username
  72.      * @param    string  $timestamp
  73.      * @param    string  $module Current module
  74.      * @param    string  $ip     User's IP adress
  75.      * 
  76.      * @return    bool    TRUE on success
  77.      */
  78.     function write($uid, $uname, $time, $module, $ip)
  79.     {
  80.         $uid = intval($uid);
  81.         if ($uid > 0) {
  82.             $sql = "SELECT COUNT(*) FROM ".$this->db->prefix('online')." WHERE online_uid=".$uid;
  83.         } else {
  84.             $sql = "SELECT COUNT(*) FROM ".$this->db->prefix('online')." WHERE online_uid=".$uid." AND online_ip='".$ip."'";
  85.         }
  86.         list($count) = $this->db->fetchRow($this->db->queryF($sql));
  87.         if ( $count > 0 ) {
  88.             $sql = "UPDATE ".$this->db->prefix('online')." SET online_updated=".$time.", online_module = ".$module." WHERE online_uid = ".$uid;
  89.             if ($uid == 0) {
  90.                 $sql .= " AND online_ip='".$ip."'";
  91.             }
  92.         } else {
  93.             $sql = sprintf("INSERT INTO %s (online_uid, online_uname, online_updated, online_ip, online_module) VALUES (%u, %s, %u, %s, %u)", $this->db->prefix('online'), $uid, $this->db->quoteString($uname), $time, $this->db->quoteString($ip), $module);
  94.         }
  95.         if (!$this->db->queryF($sql)) {
  96.             return false;
  97.         }
  98.         return true;
  99.     }
  100.  
  101.     /**
  102.      * Delete online information for a user
  103.      * 
  104.      * @param    int $uid    UID
  105.      * 
  106.      * @return    bool    TRUE on success
  107.      */
  108.     function destroy($uid)
  109.     {
  110.         $sql = sprintf("DELETE FROM %s WHERE online_uid = %u", $this->db->prefix('online'), $uid);
  111.         if (!$result = $this->db->queryF($sql)) {
  112.             return false;
  113.         }
  114.         return true;
  115.     }
  116.  
  117.     /**
  118.      * Garbage Collection
  119.      * 
  120.      * Delete all online information that has not been updated for a certain time
  121.      * 
  122.      * @param    int $expire Expiration time in seconds
  123.      */
  124.     function gc($expire)
  125.     {
  126.         $sql = sprintf("DELETE FROM %s WHERE online_updated < %u", $this->db->prefix('online'), time() - intval($expire));
  127.         $this->db->queryF($sql);
  128.     }
  129.  
  130.     /**
  131.      * Get an array of online information
  132.      * 
  133.      * @param    object  $criteria   {@link CriteriaElement} 
  134.      * @return    array   Array of associative arrays of online information
  135.      */
  136.     function getAll($criteria = null)
  137.     {
  138.         $ret = array();
  139.         $limit = $start = 0;
  140.         $sql = 'SELECT * FROM '.$this->db->prefix('online');
  141.         if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  142.             $sql .= ' '.$criteria->renderWhere();
  143.             $limit = $criteria->getLimit();
  144.             $start = $criteria->getStart();
  145.         }
  146.         $result = $this->db->query($sql, $limit, $start);
  147.         if (!$result) {
  148.             return false;
  149.         }
  150.         while ($myrow = $this->db->fetchArray($result)) {
  151.             $ret[] = $myrow;
  152.             unset($myrow);
  153.         }
  154.         return $ret;
  155.     }
  156.  
  157.     /**
  158.      * Count the number of online users
  159.      * 
  160.      * @param    object  $criteria   {@link CriteriaElement} 
  161.      */
  162.     function getCount($criteria = null)
  163.     {
  164.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('online');
  165.         if (is_object($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  166.             $sql .= ' '.$criteria->renderWhere();
  167.         }
  168.         if (!$result = $this->db->query($sql)) {
  169.             return false;
  170.         }
  171.         list($ret) = $this->db->fetchRow($result);
  172.         return $ret;
  173.     }
  174. }
  175. ?>