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 / session.php < prev    next >
Encoding:
PHP Script  |  2008-01-13  |  9.8 KB  |  278 lines

  1. <?php
  2. // $Id: session.php 1251 2008-01-12 16:01:19Z phppp $
  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.  * @author        Taiwen Jiang <phppp@users.sourceforge.net>
  36.  * @copyright    copyright (c) The XOOPS project XOOPS.org
  37.  */
  38.  
  39.  
  40. /**
  41.  * Handler for a session
  42.  * @package     kernel
  43.  * 
  44.  * @author        Kazumi Ono    <onokazu@xoops.org>
  45.  * @author        Taiwen Jiang <phppp@users.sourceforge.net>
  46.  * @copyright    copyright (c) The XOOPS project XOOPS.org
  47.  */
  48. class XoopsSessionHandler
  49. {
  50.  
  51.     /**
  52.      * Database connection
  53.      * 
  54.      * @var    object
  55.      * @access    private
  56.      */
  57.     var $db;
  58.     
  59.     /**
  60.      * Security checking level
  61.      *
  62.      * Possible value: 
  63.      *    0 - no check;
  64.      *    1 - check browser characteristics (HTTP_USER_AGENT/HTTP_ACCEPT_LANGUAGE), to be implemented in the future now;
  65.      *    2 - check browser and IP A.B;
  66.      *    3 - check browser and IP A.B.C, recommended;
  67.      *    4 - check browser and IP A.B.C.D;
  68.      * 
  69.      * @var    int
  70.      * @access    public
  71.      */
  72.     var $securityLevel = 3;
  73.     
  74.     /**
  75.      * Enable regenerate_id
  76.      *
  77.      * @var    bool
  78.      * @access    public
  79.      */
  80.     var $enableRegenerateId = false;
  81.     
  82.     /**
  83.      * Constructor
  84.      * 
  85.      * @param object $db reference to the {@link XoopsDatabase} object
  86.      * 
  87.      */
  88.     function XoopsSessionHandler(&$db)
  89.     {
  90.         $this->db =& $db;
  91.     }
  92.  
  93.     /**
  94.      * Open a session
  95.      * 
  96.      * @param    string  $save_path
  97.      * @param    string  $session_name
  98.      * 
  99.      * @return    bool
  100.      */
  101.     function open($save_path, $session_name)
  102.     {
  103.         return true;
  104.     }
  105.  
  106.     /**
  107.      * Close a session
  108.      * 
  109.      * @return    bool
  110.      */
  111.     function close()
  112.     {
  113.         $this->gc_force();
  114.         return true;
  115.     }
  116.  
  117.     /**
  118.      * Read a session from the database
  119.      * 
  120.      * @param    string  &sess_id    ID of the session
  121.      * 
  122.      * @return    array   Session data
  123.      */
  124.     function read($sess_id)
  125.     {
  126.         $sql = sprintf('SELECT sess_data, sess_ip FROM %s WHERE sess_id = %s', $this->db->prefix('session'), $this->db->quoteString($sess_id));
  127.         if (false != $result = $this->db->query($sql)) {
  128.             if (list($sess_data, $sess_ip) = $this->db->fetchRow($result)) {
  129.                 if ($this->securityLevel > 1) {
  130.                     $pos = strpos($sess_ip, ".", $this->securityLevel - 1);
  131.                     if (strncmp($sess_ip, $_SERVER['REMOTE_ADDR'], $pos)) {
  132.                         $sess_data = '';
  133.                     }
  134.                 }
  135.                 return $sess_data;
  136.             }
  137.         }
  138.         return '';
  139.     }
  140.  
  141.     /**
  142.      * Write a session to the database
  143.      * 
  144.      * @param   string  $sess_id
  145.      * @param   string  $sess_data
  146.      * 
  147.      * @return  bool    
  148.      **/
  149.     function write($sess_id, $sess_data)
  150.     {
  151.         $sess_id = $this->db->quoteString($sess_id);
  152.         $sql = sprintf('UPDATE %s SET sess_updated = %u, sess_data = %s WHERE sess_id = %s', $this->db->prefix('session'), time(), $this->db->quoteString($sess_data), $sess_id);
  153.         $this->db->queryF($sql);
  154.         if (!$this->db->getAffectedRows()) {
  155.             $sql = sprintf('INSERT INTO %s (sess_id, sess_updated, sess_ip, sess_data) VALUES (%s, %u, %s, %s)', $this->db->prefix('session'), $sess_id, time(), $this->db->quoteString($_SERVER['REMOTE_ADDR']), $this->db->quoteString($sess_data));
  156.             return $this->db->queryF($sql);
  157.         }
  158.         return true;
  159.     }
  160.  
  161.     /**
  162.      * Destroy a session
  163.      * 
  164.      * @param   string  $sess_id
  165.      * 
  166.      * @return  bool
  167.      **/
  168.     function destroy($sess_id)
  169.     {
  170.         $sql = sprintf('DELETE FROM %s WHERE sess_id = %s', $this->db->prefix('session'), $this->db->quoteString($sess_id));
  171.         if ( !$result = $this->db->queryF($sql) ) {
  172.             return false;
  173.         }
  174.         return true;
  175.     }
  176.  
  177.     /**
  178.      * Garbage Collector
  179.      * 
  180.      * @param   int $expire Time in seconds until a session expires
  181.      * @return  bool
  182.      **/
  183.     function gc($expire)
  184.     {
  185.         if (empty($expire)) {
  186.             return true;
  187.         }
  188.         
  189.         $mintime = time() - intval($expire);
  190.         $sql = sprintf('DELETE FROM %s WHERE sess_updated < %u', $this->db->prefix('session'), $mintime);
  191.         return $this->db->queryF($sql);
  192.     }
  193.     
  194.     /**
  195.      * Force gc for situations where gc is registered but not executed
  196.      **/
  197.     function gc_force()
  198.     {
  199.         if (rand(1, 100) < 11) {
  200.             $expiration = empty($GLOBALS["xoopsConfig"]["session_expire"]) ? @ini_get('session.gc_maxlifetime') : $GLOBALS["xoopsConfig"]["session_expire"] * 60;
  201.             $this->gc($expiration);
  202.         }
  203.     }
  204.     
  205.     /**
  206.      * Update the current session id with a newly generated one
  207.      *
  208.      * To be refactored 
  209.      * 
  210.      * @param   bool $delete_old_session
  211.      * @return  bool
  212.      **/
  213.     function regenerate_id($delete_old_session = false)
  214.     {
  215.         if (!$this->enableRegenerateId) {
  216.             return true;
  217.         }
  218.         
  219.         $phpversion = phpversion();
  220.         
  221.         // parameter "delete_old_session" only available as of PHP 5.1.0
  222.         if (version_compare($phpversion, "5.1.0", ">=")) {
  223.              $success = session_regenerate_id($delete_old_session);
  224.         } else {
  225.             $old_session_id = session_id();
  226.             // session_regenerate_id function available as of PHP 4.3.2
  227.             if (function_exists("session_regenerate_id")) {
  228.                 $success = session_regenerate_id();
  229.                 if ($success && $delete_old_session) {
  230.                     // Extra step to destroy old session
  231.                     $this->destroy($old_session_id);
  232.                 }
  233.             // For PHP prior to 4.3.2
  234.             } else {
  235.                 // session_regenerate_id is not defined, create new session ID
  236.                 $session_id = md5( uniqid(rand(), true) . @$_SERVER['HTTP_USER_AGENT'] );
  237.                 // Set the new session ID
  238.                 session_id($session_id);
  239.                 // Destory old session on request
  240.                 if ($delete_old_session) {
  241.                     $this->destroy($old_session_id);
  242.                 // switch old session to new one
  243.                 } else {
  244.                     $sql = sprintf('UPDATE %s SET sess_id = %s WHERE sess_id = %s', $this->db->prefix('session'), $this->db->quoteString($session_id), $this->db->quoteString($old_session_id));
  245.                     $this->db->queryF($sql);
  246.                 }
  247.                 $success = true;
  248.             }
  249.         }
  250.         
  251.         // Force updating cookie for session cookie is not issued correctly in some IE versions or not automatically issued prior to PHP 4.3.3 for all browsers 
  252.         if ($success) {
  253.             $this->update_cookie();
  254.         }
  255.         
  256.         return $success;
  257.     }
  258.     
  259.     /**
  260.      * Update cookie status for current session
  261.      *
  262.      * To be refactored 
  263.      * FIXME: how about $xoopsConfig['use_ssl'] is enabled?
  264.      * 
  265.      * @param   string  $sess_id    session ID
  266.      * @param   int     $expire     Time in seconds until a session expires
  267.      * @return  bool
  268.      **/
  269.     function update_cookie($sess_id = null, $expire = null)
  270.     {
  271.         global $xoopsConfig;
  272.         $session_name = ($xoopsConfig['use_mysession'] && $xoopsConfig['session_name'] != '') ? $xoopsConfig['session_name'] : session_name();
  273.         $session_expire = !is_null($expire) ? intval($expire) : ( ($xoopsConfig['use_mysession'] && $xoopsConfig['session_name'] != '') ? $xoopsConfig['session_expire'] * 60 : ini_get("session.cookie_lifetime") );
  274.         $session_id = empty($sess_id) ? session_id() : $sess_id;
  275.         setcookie($session_name, $session_id, $session_expire ? time() + $session_expire : 0, '/',  '', 0);
  276.     }
  277. }
  278. ?>