PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/library/People/People.Class.Session.php

http://lussumo-vanilla.googlecode.com/
PHP | 214 lines | 108 code | 19 blank | 87 comment | 35 complexity | a63c1ed2f358e6709239c1564b023b43 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * Class that handles user sessions.
  4. *
  5. * Copyright 2003 Mark O'Sullivan
  6. * This file is part of Lussumo's Software Library.
  7. * Lussumo's Software Library 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.
  8. * Lussumo's Software Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. * You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  10. * The latest source code is available at www.vanilla1forums.com
  11. * Contact Mark O'Sullivan at mark [at] lussumo [dot] com
  12. *
  13. * @author Mark O'Sullivan
  14. * @copyright 2003 Mark O'Sullivan
  15. * @license http://www.gnu.org/licenses/gpl-2.0.html GPL 2
  16. * @package People
  17. */
  18. /**
  19. * Handles user sessions.
  20. * @package People
  21. */
  22. class PeopleSession {
  23. /**
  24. * Unique user identifier
  25. * @var int
  26. */
  27. var $UserID;
  28. /**
  29. * User object containing properties relevant to session
  30. * @var User
  31. */
  32. var $User;
  33. /**
  34. * Ensure that there is an active session.
  35. *
  36. * If there isn't an active session, send the user to the SignIn Url
  37. *
  38. * @param Context $Context
  39. */
  40. function Check(&$Context) {
  41. if (($this->UserID == 0 && !$Context->Configuration['PUBLIC_BROWSING']) || ($this->UserID > 0 && !$this->User->PERMISSION_SIGN_IN)) {
  42. if ($this->UserID > 0 && !$this->User->PERMISSION_SIGN_IN) $this->End($Context->Authenticator);
  43. $Url = AppendUrlParameters(
  44. $Context->Configuration['SAFE_REDIRECT'],
  45. 'ReturnUrl=' . urlencode( GetRequestUri() ) );
  46. Redirect($Url);
  47. }
  48. }
  49. /**
  50. * End the session and remove the session data.
  51. */
  52. function Destroy() {
  53. if (session_id()) {
  54. session_destroy();
  55. }
  56. $this->UserID = 0;
  57. if($this->User) {
  58. $this->User->Clear();
  59. }
  60. }
  61. /**
  62. * End a session
  63. *
  64. * @param Authenticator $Authenticator
  65. */
  66. function End($Authenticator) {
  67. $Authenticator->DeAuthenticate();
  68. }
  69. /**
  70. * Get a session variable
  71. *
  72. * @param string $Name
  73. * @param string $DataType Can be int|bool|array|string.
  74. * @return int|boolean|array|string
  75. */
  76. function GetVariable($Name, $DataType = 'bool') {
  77. if ($DataType == 'int') {
  78. return ForceInt(@$_SESSION[$Name], 0);
  79. } else if ($DataType == 'bool') {
  80. return ForceBool(@$_SESSION[$Name], 0);
  81. } else if ($DataType == 'array') {
  82. return ForceArray(@$_SESSION[$Name], array());
  83. } else {
  84. return ForceString(@$_SESSION[$Name], '');
  85. }
  86. }
  87. /**
  88. * Set a session variable
  89. *
  90. * @param string $Name
  91. * @param int|bool|array|string $Value
  92. */
  93. function SetVariable($Name, $Value) {
  94. @$_SESSION[$Name] = $Value;
  95. }
  96. /**
  97. * Return the key used for CSRF protection.
  98. * @return String
  99. */
  100. function GetCsrfValidationKey() {
  101. $Key = $this->GetVariable('SessionPostBackKey', 'string');
  102. if ($Key == '') {
  103. $Key = DefineVerificationKey();
  104. $this->SetVariable('SessionPostBackKey', $Key);
  105. }
  106. return $Key;
  107. }
  108. /**
  109. * Regenerate the session id.
  110. *
  111. * The old session id and the data associated to it should be destroyed.
  112. * Sending a session id is not enought since someone with the old id would
  113. * be able the claim the identity of the user.
  114. *
  115. * (the user should not lose his/her session data)
  116. *
  117. * @param Context $Context
  118. */
  119. function RegenerateId($Context) {
  120. if (session_id()) {
  121. if (version_compare(phpversion(), '5.0.0', '>=')) {
  122. session_regenerate_id(true);
  123. } else {
  124. $SessionCopy = $_SESSION;
  125. session_destroy();
  126. session_id(md5(uniqid(rand(), true) . rand()));
  127. if ($Context->Configuration['SESSION_NAME']) {
  128. session_name($Context->Configuration['SESSION_NAME']);
  129. }
  130. if (!empty($Context->Configuration['SESSION_SAVE_PATH'])) {
  131. session_save_path($Context->$Configuration['SESSION_SAVE_PATH']);
  132. }
  133. session_start();
  134. setcookie(session_name(), session_id(), null,
  135. $Context->Configuration['COOKIE_PATH'],
  136. $Context->Configuration['COOKIE_DOMAIN'],
  137. ($Context->Configuration['HTTP_METHOD'] === "https"));
  138. $_SESSION = $SessionCopy;
  139. }
  140. }
  141. }
  142. /**
  143. * Start a session if required username/password exist in the system
  144. *
  145. * @param Context $Context
  146. * @param Authenticator $Authenticator
  147. * @param int $UserID
  148. */
  149. function Start(&$Context, $Authenticator, $UserID = '0') {
  150. $this->StartSession($Context);
  151. // If the UserID is not explicitly defined (ie. by some vanilla-based login module),
  152. // retrieve the authenticated UserID from the Authenticator module.
  153. $this->UserID = ForceInt($UserID, 0);
  154. if ($this->UserID == 0) $this->UserID = $Authenticator->GetIdentity();
  155. // Now retrieve user information
  156. if ($this->UserID > 0) {
  157. $UserManager = $Context->ObjectFactory->NewContextObject($Context, 'UserManager');
  158. $this->User = $UserManager->GetSessionDataById($this->UserID);
  159. // If the session data retrieval failed for some reason, dump the user
  160. if (!$this->User) {
  161. $this->User = $Context->ObjectFactory->NewContextObject($Context, 'User');
  162. $this->User->Clear();
  163. $this->UserID = 0;
  164. }
  165. } else {
  166. $this->User = $Context->ObjectFactory->NewContextObject($Context, 'User');
  167. $this->User->Clear();
  168. }
  169. }
  170. /**
  171. * Start the PHP session
  172. *
  173. * @param Context $Context
  174. */
  175. function StartSession($Context) {
  176. if (!session_id()) {
  177. if (!empty($Context->Configuration['SESSION_NAME'])) {
  178. session_name($Context->Configuration['SESSION_NAME']);
  179. }
  180. if (!empty($Context->Configuration['SESSION_SAVE_PATH'])) {
  181. session_save_path($Context->$Configuration['SESSION_SAVE_PATH']);
  182. }
  183. $UseSsl = ($Context->Configuration['HTTP_METHOD'] === "https");
  184. if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
  185. session_set_cookie_params(0, $Context->Configuration['COOKIE_PATH'],
  186. $Context->Configuration['COOKIE_DOMAIN'], $UseSsl, true);
  187. } else {
  188. session_set_cookie_params(0, $Context->Configuration['COOKIE_PATH'],
  189. $Context->Configuration['COOKIE_DOMAIN'], $UseSsl);
  190. }
  191. session_start();
  192. }
  193. }
  194. }
  195. ?>