09.30 php saved
Phoenix
Tags add more
SMF and smake  
Note
For more info read http://groups.google.com/group/cake-php/browse_frm/thread/e44d0a4b957f3bf0

Reedited to work with database which you entered (Session.database) instead of the default one
  1. <?php
  2. /* SVN FILE: $Id: session.php 5700 2007-09-30 07:45:34Z gwoo $ */
  3. /**
  4. * Session class for using the Session Table of the SMF forum.
  5. * This is needed because the SMF forum does not have a primary
  6. * column called *id*, and then we want CakePHP to use a primary column
  7. * called *session_id*
  8. *
  9. * Cake 1.2
  10. *
  11. * Read this Post for further instructions:
  12. * http://groups.google.es/group/cake-php/browse_thread/thread/6440d2b1f7a29534/cc62252f5dfedcf0
  13. *
  14. *
  15. * @author          Daniel Vecchiato (aka CakeFreak)
  16. * @link            http://www.4webby.com/
  17. * @package   cake
  18. * @subpackage    cake.cake.libs
  19. * @since         CakePHP(tm) v .0.10.0.1222
  20. * @version   1.0
  21. * @lastmodified    2007-10-20
  22. * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
  23. */
  24. /**
  25. * Database name for cake sessions.
  26. *
  27. */
  28. uses('set');
  29. /**
  30. * Session class for Cake.
  31. *
  32. * Cake abstracts the handling of sessions. There are several convenient methods to access session information.
  33. * This class is the implementation of those methods. They are mostly used by the Session Component.
  34. *
  35. * @package  cake
  36. * @subpackage  cake.cake.libs
  37. */
  38. require_once('session_cake.php');
  39. class SmfSession extends CakeOriginalSession {
  40.  
  41. /**
  42. * Constructor.
  43. *
  44. * @param string $base The base path for the Session
  45. * @param boolean $start Should session be started right now
  46. * @access public
  47. */
  48.     function __construct($base = null, $start = true) {
  49.  
  50.         //Set the name of the primary column used by SMF the session DB table
  51.         if (!defined('SESS_COL_ID')) {
  52.             define('SESS_COL_ID', 'session_id');
  53.         }
  54.         parent::__construct();
  55.     }
  56.  
  57. /**
  58. * Method used to read from a database session.
  59. *
  60. * @param mixed $key The key of the value to read
  61. * @return mixed The value of the key or false if it does not exist
  62. * @access private
  63. */
  64.     function __read($key) {
  65.         $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
  66.         $table = $db->fullTableName(Configure::read('Session.table'), false);
  67.         $row = $db->query("SELECT " . $db->name($table.'.data') . " FROM " . $db->name($table) . " WHERE " . $db->name($table.'.'.SESS_COL_ID) . " = " . $db->value($key), false);
  68.  
  69.         if ($row && !isset($row[0][$table]) && isset($row[0][0])) {
  70.             $table = 0;
  71.         }
  72.  
  73.         if ($row && $row[0][$table]['data']) {
  74.             return $row[0][$table]['data'];
  75.         } else {
  76.             return false;
  77.         }
  78.     }
  79. /**
  80. * Helper function called on write for database sessions.
  81. *
  82. * @param mixed $key The name of the var
  83. * @param mixed $value The value of the var
  84. * @return boolean Success
  85. * @access private
  86. */
  87.     function __write($key, $value) {
  88.         $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
  89.         $table = $db->fullTableName(Configure::read('Session.table'));
  90.  
  91.         switch(CAKE_SECURITY) {
  92.             case 'high':
  93.                 $factor = 10;
  94.             break;
  95.             case 'medium':
  96.                 $factor = 100;
  97.             break;
  98.             case 'low':
  99.                 $factor = 300;
  100.             break;
  101.             default:
  102.                 $factor = 10;
  103.             break;
  104.         }
  105.         $expires = time() + CAKE_SESSION_TIMEOUT * $factor;
  106.         $row = $db->query("SELECT COUNT(".SESS_COL_ID.") AS count FROM " . $db->name($table) . " WHERE "
  107.                                  . $db->name(SESS_COL_ID) . " = "
  108.                                  . $db->value($key), false);
  109.  
  110.         if ($row[0][0]['count'] > 0) {
  111.             $db->execute("UPDATE " . $db->name($table) . " SET " . $db->name('data') . " = "
  112.                                 . $db->value($value) . ", " . $db->name('expires') . " = "
  113.                                 . $db->value($expires) . " WHERE " . $db->name(SESS_COL_ID) . " = "
  114.                                 . $db->value($key));
  115.         } else {
  116.             $db->execute("INSERT INTO " . $db->name($table) . " (" . $db->name('data') . ","
  117.                                 . $db->name('expires') . "," . $db->name(SESS_COL_ID)
  118.                                 . ") VALUES (" . $db->value($value) . ", " . $db->value($expires) . ", "
  119.                                 . $db->value($key) . ")");
  120.         }
  121.         return true;
  122.     }
  123. /**
  124. * Method called on the destruction of a database session.
  125. *
  126. * @param int $key Key that uniquely identifies session in database
  127. * @return boolean Success
  128. * @access private
  129. */
  130.     function __destroy($key) {
  131.         $db =& ConnectionManager::getDataSource(Configure::read('Session.database'));
  132.         $table = $db->fullTableName(CAKE_SESSION_TABLE);
  133.         $db->execute("DELETE FROM " . $db->name($table) . " WHERE " . $db->name($table.'.'.SESS_COL_ID) . " = " . $db->value($key, 'integer'));
  134.         return true;
  135.     }
  136.  
  137. }
  138. ?>
  139.  
Parsed in 0.187 seconds, using GeSHi 1.0.7.14

Modify this Paste