SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Drivers/Session/Database.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers\Drivers\Session;
  17.  
  18. use Kohana;
  19.  
  20. use karmabunny\pdb\Exceptions\RowMissingException;
  21. use Sprout\Helpers\Drivers\SessionDriver;
  22. use Sprout\Helpers\Encrypt;
  23. use Sprout\Helpers\Pdb;
  24.  
  25.  
  26. /**
  27.  * Session database driver.
  28.  */
  29. class Database implements SessionDriver
  30. {
  31. // Database settings
  32. protected $table = 'sessions';
  33.  
  34. // Encryption
  35. protected $encrypt;
  36.  
  37. // Session settings
  38. protected $session_id;
  39. protected $written = FALSE;
  40.  
  41. public function __construct()
  42. {
  43. // Load configuration
  44. $config = Kohana::config('session');
  45.  
  46. if ( ! empty($config['encryption']))
  47. {
  48. // Load encryption
  49. $this->encrypt = Encrypt::instance();
  50. }
  51.  
  52. if (is_array($config['storage'])) {
  53. if (!empty($config['storage']['table'])) {
  54. // Set the table name
  55. Pdb::validateIdentifier($config['storage']['table']);
  56. $this->table = $config['storage']['table'];
  57. }
  58. }
  59.  
  60. Kohana::log('debug', 'Session Database Driver Initialized');
  61. }
  62.  
  63. public function open($path, $name)
  64. {
  65. return TRUE;
  66. }
  67.  
  68. public function close()
  69. {
  70. return TRUE;
  71. }
  72.  
  73. public function read($id)
  74. {
  75. // Load the session
  76. try {
  77. $q = "SELECT data FROM ~{$this->table} WHERE session_id = ? LIMIT 1";
  78. $data = Pdb::q($q, [$id], 'val');
  79. } catch (RowMissingException $ex) {
  80.  
  81. // No current session
  82. $this->session_id = NULL;
  83. return '';
  84. }
  85.  
  86. // Set the current session id
  87. $this->session_id = $id;
  88.  
  89. return ($this->encrypt === NULL) ? base64_decode($data) : $this->encrypt->decode($data);
  90. }
  91.  
  92. public function write($id, $data)
  93. {
  94. $data = array
  95. (
  96. 'session_id' => $id,
  97. 'last_activity' => time(),
  98. 'data' => ($this->encrypt === NULL) ? base64_encode($data) : $this->encrypt->encode($data)
  99. );
  100.  
  101. if ($this->session_id === NULL)
  102. {
  103. // Insert a new session
  104. $count = Pdb::insert($this->table, $data);
  105. }
  106. elseif ($id === $this->session_id)
  107. {
  108. // Do not update the session_id
  109. unset($data['session_id']);
  110.  
  111. // Update the existing session
  112. $count = Pdb::update($this->table, $data, array('session_id' => $id));
  113. }
  114. else
  115. {
  116. // Update the session and id
  117. $count = Pdb::update($this->table, $data, array('session_id' => $this->session_id));
  118.  
  119. // Set the new session id
  120. $this->session_id = $id;
  121. }
  122.  
  123. return (bool) $count;
  124. }
  125.  
  126. public function destroy($id)
  127. {
  128. // Delete the requested session
  129. Pdb::delete($this->table, array('session_id' => $id));
  130.  
  131. // Session id is no longer valid
  132. $this->session_id = NULL;
  133.  
  134. return TRUE;
  135. }
  136.  
  137. public function regenerate()
  138. {
  139. // Generate a new session id
  140.  
  141. // Return new session id
  142. return session_id();
  143. }
  144.  
  145. public function gc($maxlifetime)
  146. {
  147. // Delete all expired sessions
  148. Pdb::delete($this->table, [['last_activity', '<', time() - $maxlifetime]]);
  149. }
  150.  
  151. } // End Session Database Driver
  152.