Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| common_session_SessionManager | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| getSession | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| startSession | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| endSession | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| isAnonymous | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 23 | use oat\oatbox\service\ServiceManager; |
| 24 | |
| 25 | /** |
| 26 | * Represents a Session on Generis. |
| 27 | * |
| 28 | * @access private |
| 29 | * @author Joel Bout, <joel@taotesting.com> |
| 30 | * @package generis |
| 31 | |
| 32 | */ |
| 33 | abstract class common_session_SessionManager |
| 34 | { |
| 35 | public const PHPSESSION_SESSION_KEY = 'common_session_Session'; |
| 36 | |
| 37 | private static $session = null; |
| 38 | |
| 39 | /** |
| 40 | * Retrurns the current session |
| 41 | * |
| 42 | * @throws common_exception_Error |
| 43 | * @return common_session_Session |
| 44 | */ |
| 45 | public static function getSession() |
| 46 | { |
| 47 | if (is_null(self::$session)) { |
| 48 | if (PHPSession::singleton()->hasAttribute(self::PHPSESSION_SESSION_KEY)) { |
| 49 | $session = PHPSession::singleton()->getAttribute(self::PHPSESSION_SESSION_KEY); |
| 50 | if (! $session instanceof common_session_Session) { |
| 51 | throw new common_exception_Error('Non session stored in php-session'); |
| 52 | } |
| 53 | self::$session = $session; |
| 54 | } else { |
| 55 | self::$session = new common_session_AnonymousSession(); |
| 56 | } |
| 57 | } |
| 58 | if (self::$session instanceof ServiceLocatorAwareInterface) { |
| 59 | self::$session->setServiceLocator(ServiceManager::getServiceManager()); |
| 60 | } |
| 61 | return self::$session; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Starts a new session and stores it in the session if stateful |
| 66 | * |
| 67 | * @param common_session_Session $session |
| 68 | * @return boolean |
| 69 | */ |
| 70 | public static function startSession(common_session_Session $session) |
| 71 | { |
| 72 | |
| 73 | self::$session = $session; |
| 74 | // do not start session in cli mode (testcase script) |
| 75 | if (PHP_SAPI != 'cli') { |
| 76 | if ($session instanceof common_session_StatefulSession) { |
| 77 | // start session if not yet started |
| 78 | if (session_id() === '') { |
| 79 | session_name(GENERIS_SESSION_NAME); |
| 80 | session_start(); |
| 81 | } else { |
| 82 | // prevent session fixation. |
| 83 | session_regenerate_id(); |
| 84 | } |
| 85 | |
| 86 | PHPSession::singleton()->setAttribute(self::PHPSESSION_SESSION_KEY, $session); |
| 87 | } |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Ends the session by replacing it with an anonymous session |
| 94 | * |
| 95 | * @return boolean |
| 96 | */ |
| 97 | public static function endSession() |
| 98 | { |
| 99 | |
| 100 | // clean session data. |
| 101 | if (session_id() != '') { |
| 102 | session_destroy(); |
| 103 | } |
| 104 | |
| 105 | return self::startSession(new common_session_AnonymousSession()); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Is the current session anonymous or associated to a user? |
| 110 | * |
| 111 | * @return boolean |
| 112 | */ |
| 113 | public static function isAnonymous() |
| 114 | { |
| 115 | return is_null(self::getSession()->getUserUri()); |
| 116 | } |
| 117 | } |