SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Drivers/Session/Cache.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. use Kohana_Exception;
  20.  
  21. use Sprout\Helpers\Drivers\Cache AS CacheDriver;
  22. use Sprout\Helpers\Encrypt;
  23. use Sprout\Helpers\Session;
  24.  
  25.  
  26. /**
  27.  * Session cache driver.
  28.  *
  29.  * Cache library config goes in the session.storage config entry:
  30.  * $config['storage'] = array(
  31.  * 'driver' => 'apc',
  32.  * 'requests' => 10000
  33.  * );
  34.  * Lifetime does not need to be set as it is
  35.  * overridden by the session expiration setting.
  36.  */
  37. class Cache implements Session
  38. {
  39.  
  40. protected $cache;
  41. protected $encrypt;
  42.  
  43. public function __construct()
  44. {
  45. // Load Encrypt library
  46. if (Kohana::config('session.encryption'))
  47. {
  48. $this->encrypt = new Encrypt;
  49. }
  50.  
  51. Kohana::log('debug', 'Session Cache Driver Initialized');
  52. }
  53.  
  54. public function open($path, $name)
  55. {
  56. $config = Kohana::config('session.storage');
  57.  
  58. if (empty($config))
  59. {
  60. // Load the default group
  61. $config = Kohana::config('cache.default');
  62. }
  63. elseif (is_string($config))
  64. {
  65. $name = $config;
  66.  
  67. // Test the config group name
  68. if (($config = Kohana::config('cache.'.$config)) === NULL)
  69. throw new Kohana_Exception('cache.undefined_group', $name);
  70. }
  71.  
  72. $config['lifetime'] = (Kohana::config('session.expiration') == 0) ? 86400 : Kohana::config('session.expiration');
  73. $this->cache = new CacheDriver($config);
  74.  
  75. return is_object($this->cache);
  76. }
  77.  
  78. public function close()
  79. {
  80. return TRUE;
  81. }
  82.  
  83. public function read($id)
  84. {
  85. $id = 'session_'.$id;
  86. if ($data = $this->cache->get($id))
  87. {
  88. return Kohana::config('session.encryption') ? $this->encrypt->decode($data) : $data;
  89. }
  90.  
  91. // Return value must be string, NOT a boolean
  92. return '';
  93. }
  94.  
  95. public function write($id, $data)
  96. {
  97. $id = 'session_'.$id;
  98. $data = Kohana::config('session.encryption') ? $this->encrypt->encode($data) : $data;
  99.  
  100. return $this->cache->set($id, $data);
  101. }
  102.  
  103. public function destroy($id)
  104. {
  105. $id = 'session_'.$id;
  106. return $this->cache->delete($id);
  107. }
  108.  
  109. public function regenerate()
  110. {
  111.  
  112. // Return new session id
  113. return session_id();
  114. }
  115.  
  116. public function gc($maxlifetime)
  117. {
  118. // Just return, caches are automatically cleaned up
  119. return TRUE;
  120. }
  121.  
  122. } // End Session Cache Driver
  123.