Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / session / virtual / SessionVirtual.class.php
1 <?php
2 namespace wcf\data\session\virtual;
3 use wcf\data\DatabaseObject;
4 use wcf\system\WCF;
5 use wcf\util\UserUtil;
6
7 /**
8 * Virtual Sessions extend the original session system with a transparent layer.
9 * It's only purpose is to enforce session validation based on IP address and/or user agent.
10 *
11 * The legacy session system does not allow the same user being logged-in more than once
12 * and the same is true for WCF 2.1 unless we break most parts of the API.
13 * In order to solve this, we do allow multiple clients to share the exact same session
14 * among them, while the individual clients are tracked within wcf1_session_virtual.
15 *
16 * @author Alexander Ebert
17 * @copyright 2001-2014 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package com.woltlab.wcf
20 * @subpackage data.session.virtual
21 * @category Community Framework
22 */
23 class SessionVirtual extends DatabaseObject {
24 /**
25 * @see \wcf\data\DatabaseObject::$databaseTableName
26 */
27 protected static $databaseTableName = 'session_virtual';
28
29 /**
30 * @see \wcf\data\DatabaseObject::$databaseTableIndexName
31 */
32 protected static $databaseTableIndexName = 'virtualSessionID';
33
34 /**
35 * Returns the active virtual session object or null.
36 *
37 * @param string $sessionID
38 * @return \wcf\data\session\virtual\SessionVirtual
39 */
40 public static function getExistingSession($sessionID) {
41 $sql = "SELECT *
42 FROM ".static::getDatabaseTableName()."
43 WHERE sessionID = ?
44 AND ipAddress = ?
45 AND userAGent = ?";
46 $statement = WCF::getDB()->prepareStatement($sql);
47 $statement->execute(array(
48 $sessionID,
49 UserUtil::getIpAddress(),
50 UserUtil::getUserAgent()
51 ));
52
53 return $statement->fetchObject(__CLASS__);
54 }
55
56 /**
57 * Returns the number of virtual sessions associated with the given session id.
58 *
59 * @param string $sessionID
60 * @return integer
61 */
62 public static function countVirtualSessions($sessionID) {
63 $sql = "SELECT COUNT(*) AS count
64 FROM ".static::getDatabaseTableName()."
65 WHERE sessionID = ?";
66 $statement = WCF::getDB()->prepareStatement($sql);
67 $statement->execute(array($sessionID));
68 $row = $statement->fetchArray();
69
70 return $row['count'];
71 }
72 }