10.20 php saved
CakeFreak
Tags add more
 
Note
Library to extend CakeSession library in order to use the SMF forum session table (it misses the *id* column)

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

Modify this Paste