PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mvc/Core/Session.php

https://bitbucket.org/klyve/imt2291-project1-spring2018
PHP | 157 lines | 89 code | 19 blank | 49 comment | 26 complexity | 52aa6b20c4428f77e671029648a819e5 MD5 | raw file
  1. <?php namespace MVC\Core;
  2. /**
  3. * @TODO: BJARTE
  4. * Description of file
  5. *
  6. *
  7. * PHP version 7
  8. *
  9. *
  10. * @category Core
  11. * @package Framework
  12. * @version 1.0
  13. * @link https://bitbucket.org/klyve/imt2291-project1-spring2018
  14. * @since File available since Release 1.0
  15. */
  16. class SessionHandlerException extends \Exception {}
  17. class SessionDisabledException extends SessionHandlerException {}
  18. class InvalidArgumentTypeException extends SessionHandlerException {}
  19. class ExpiredSessionException extends SessionHandlerException {}
  20. class SessionUseOnlyCookiesException extends SessionHandlerException {}
  21. class SessionHttpOnlyCookieException extends SessionHandlerException {}
  22. class SessionCookieSecureException extends SessionHandlerException {}
  23. class Session {
  24. protected static $SESSION_AGE = 3600;
  25. /**
  26. * @param $key @TODO describe this parameter
  27. * @param $value @TODO describe this parameter
  28. * @return $value @TODO describe whats returned
  29. */
  30. public static function write($key, $value) {
  31. if ( !is_string($key) )
  32. throw new InvalidArgumentTypeException('Session key must be string value');
  33. self::_init();
  34. $_SESSION[$key] = $value;
  35. self::_age();
  36. return $value;
  37. }
  38. /**
  39. * @param $key @TODO describe this parameter
  40. * @param $child @TODO describe this parameter
  41. * @return $_SESSION @TODO describe whats returned
  42. */
  43. public static function get($key, $child = false) {
  44. if ( !is_string($key) )
  45. throw new InvalidArgumentTypeException('Session key must be string value');
  46. self::_init();
  47. if (isset($_SESSION[$key])) {
  48. self::_age();
  49. if (false == $child) {
  50. return $_SESSION[$key];
  51. } else {
  52. if (isset($_SESSION[$key][$child])) {
  53. return $_SESSION[$key][$child];
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. /**
  60. * @return session_id @TODO describe whats returned
  61. */
  62. public static function getSessionId() {
  63. if(!isset($_ENV['SESSION_DRIVER']) || !isset($_ENV['SESSION_DRIVER']) || $_ENV['SESSION_DRIVER'] !== 'array')
  64. return session_id();
  65. return 1;
  66. }
  67. /**
  68. * @param $key @TODO describe this parameter
  69. */
  70. public static function delete($key) {
  71. if ( !is_string($key) )
  72. throw new InvalidArgumentTypeException('Session key must be string value');
  73. self::_init();
  74. unset($_SESSION[$key]);
  75. self::_age();
  76. }
  77. /**
  78. * @TODO: [dump description]
  79. * @return [type] [description]
  80. */
  81. public static function dump() {
  82. self::_init();
  83. echo nl2br(print_r($_SESSION));
  84. }
  85. /**
  86. * @return _init @TODO describe whats returned
  87. */
  88. public static function start() {
  89. return self::_init();
  90. }
  91. /**
  92. * @TODO: [_age description]
  93. * @return [type] [description]
  94. */
  95. private static function _age() {
  96. $last = isset($_SESSION['LAST_ACTIVE']) ? $_SESSION['LAST_ACTIVE'] : false ;
  97. if (false !== $last && (time() - $last > self::$SESSION_AGE)) {
  98. self::destroy();
  99. throw new ExpiredSessionException();
  100. }
  101. $_SESSION['LAST_ACTIVE'] = time();
  102. }
  103. /**
  104. * @TODO: [destroy description]
  105. * @return [type] [description]
  106. */
  107. public static function destroy() {
  108. if (self::getSessionId() !== '') {
  109. $_SESSION = array();
  110. if(!isset($_ENV['SESSION_DRIVER']) || $_ENV['SESSION_DRIVER'] !== 'array')
  111. session_destroy();
  112. }
  113. }
  114. /**
  115. * @return session_start @TODO describe whats returned
  116. * @return session_regenerate_id @TODO describe whats returned
  117. */
  118. private static function _init() {
  119. if (function_exists('session_status')) {
  120. if (session_status() == PHP_SESSION_DISABLED)
  121. throw new SessionDisabledException();
  122. }
  123. if (self::getSessionId() === '') {
  124. $secure = true;
  125. $httponly = true;
  126. if (ini_set('session.use_only_cookies', 1) === false) {
  127. throw new SessionUseOnlyCookiesException();
  128. }
  129. if (ini_set('session.cookie_httponly', 1) === false) {
  130. throw new SessionHttpOnlyCookieException();
  131. }
  132. if(!isset($_ENV['SESSION_DRIVER']) || $_ENV['SESSION_DRIVER'] !== 'array')
  133. return session_start();
  134. }
  135. if(!isset($_ENV['SESSION_DRIVER']) || $_ENV['SESSION_DRIVER'] !== 'array')
  136. return session_regenerate_id(true);
  137. }
  138. }