Session.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * PHPIDS
  4. *
  5. * Requirements: PHP5, SimpleXML
  6. *
  7. * Copyright (c) 2008 PHPIDS group (https://phpids.org)
  8. *
  9. * PHPIDS is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation, version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * PHPIDS is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with PHPIDS. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * PHP version 5.1.6+
  23. *
  24. * @category Security
  25. * @package PHPIDS
  26. * @author Mario Heiderich <mario.heiderich@gmail.com>
  27. * @author Christian Matthies <ch0012@gmail.com>
  28. * @author Lars Strojny <lars@strojny.net>
  29. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  30. * @link http://php-ids.org/
  31. */
  32. require_once 'IDS/Caching/Interface.php';
  33. /**
  34. * File caching wrapper
  35. *
  36. * This class inhabits functionality to get and set cache via session.
  37. *
  38. * @category Security
  39. * @package PHPIDS
  40. * @author Christian Matthies <ch0012@gmail.com>
  41. * @author Mario Heiderich <mario.heiderich@gmail.com>
  42. * @author Lars Strojny <lars@strojny.net>
  43. * @copyright 2007-2009 The PHPIDS Group
  44. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  45. * @version Release: $Id:Session.php 517 2007-09-15 15:04:13Z mario $
  46. * @link http://php-ids.org/
  47. * @since Version 0.4
  48. */
  49. class IDS_Caching_Session implements IDS_Caching_Interface
  50. {
  51. /**
  52. * Caching type
  53. *
  54. * @var string
  55. */
  56. private $type = null;
  57. /**
  58. * Cache configuration
  59. *
  60. * @var array
  61. */
  62. private $config = null;
  63. /**
  64. * Holds an instance of this class
  65. *
  66. * @var object
  67. */
  68. private static $cachingInstance = null;
  69. /**
  70. * Constructor
  71. *
  72. * @param string $type caching type
  73. * @param object $init the IDS_Init object
  74. *
  75. * @return void
  76. */
  77. public function __construct($type, $init)
  78. {
  79. $this->type = $type;
  80. $this->config = $init->config['Caching'];
  81. }
  82. /**
  83. * Returns an instance of this class
  84. *
  85. * @param string $type caching type
  86. * @param object $init the IDS_Init object
  87. *
  88. * @return object $this
  89. */
  90. public static function getInstance($type, $init)
  91. {
  92. if (!self::$cachingInstance) {
  93. self::$cachingInstance = new IDS_Caching_Session($type, $init);
  94. }
  95. return self::$cachingInstance;
  96. }
  97. /**
  98. * Writes cache data into the session
  99. *
  100. * @param array $data the caching data
  101. *
  102. * @return object $this
  103. */
  104. public function setCache(array $data)
  105. {
  106. $_SESSION['PHPIDS'][$this->type] = $data;
  107. return $this;
  108. }
  109. /**
  110. * Returns the cached data
  111. *
  112. * Note that this method returns false if either type or file cache is not set
  113. *
  114. * @return mixed cache data or false
  115. */
  116. public function getCache()
  117. {
  118. if ($this->type && $_SESSION['PHPIDS'][$this->type]) {
  119. return $_SESSION['PHPIDS'][$this->type];
  120. }
  121. return false;
  122. }
  123. }
  124. /**
  125. * Local variables:
  126. * tab-width: 4
  127. * c-basic-offset: 4
  128. * End:
  129. * vim600: sw=4 ts=4 expandtab
  130. */