/**
  * @see SessionFactory::create()
  */
 public function create()
 {
     // get spider information
     $spider = $this->isSpider(UserUtil::getUserAgent());
     if ($spider) {
         if (($session = $this->getExistingSpiderSession($spider['spiderID'])) !== null) {
             if (!$session->isCorrupt()) {
                 return $session;
             }
         }
     }
     // create new session hash
     $sessionID = StringUtil::getRandomID();
     // check cookies for userID & password
     require_once WCF_DIR . 'lib/system/auth/UserAuth.class.php';
     $user = UserAuth::getInstance()->loginAutomatically(true, $this->userClassName);
     if ($user === null) {
         // no valid user found
         // create guest user
         $user = new $this->guestClassName();
     }
     // update user session
     $user->update();
     if ($user->userID != 0) {
         // user is no guest
         // delete all other sessions of this user
         Session::deleteSessions($user->userID, true, false);
     }
     $requestMethod = !empty($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
     // insert session into database
     $sql = "INSERT INTO \twcf" . WCF_N . "_session\n\t\t\t\t\t(sessionID, packageID, userID, ipAddress, userAgent,\n\t\t\t\t\tlastActivityTime, requestURI, requestMethod,\n\t\t\t\t\tusername" . ($spider ? ", spiderID" : "") . ")\n\t\t\tVALUES\t\t('" . $sessionID . "',\n\t\t\t\t\t" . PACKAGE_ID . ",\n\t\t\t\t\t" . $user->userID . ",\n\t\t\t\t\t'" . escapeString(UserUtil::getIpAddress()) . "',\n\t\t\t\t\t'" . escapeString(UserUtil::getUserAgent()) . "',\n\t\t\t\t\t" . TIME_NOW . ",\n\t\t\t\t\t'" . escapeString(UserUtil::getRequestURI()) . "',\n\t\t\t\t\t'" . escapeString($requestMethod) . "',\n\t\t\t\t\t'" . ($spider ? escapeString($spider['spiderName']) : escapeString($user->username)) . "'\n\t\t\t\t\t" . ($spider ? ", " . $spider['spiderID'] : "") . ")";
     WCF::getDB()->sendQuery($sql);
     // save user data
     $serializedUserData = '';
     if (ENABLE_SESSION_DATA_CACHE && get_class(WCF::getCache()->getCacheSource()) == 'MemcacheCacheSource') {
         require_once WCF_DIR . 'lib/system/cache/source/MemcacheAdapter.class.php';
         MemcacheAdapter::getInstance()->getMemcache()->set('session_userdata_-' . $sessionID, $user);
     } else {
         $serializedUserData = serialize($user);
         try {
             $sql = "INSERT INTO \twcf" . WCF_N . "_session_data\n\t\t\t\t\t\t\t(sessionID, userData)\n\t\t\t\t\tVALUES \t\t('" . $sessionID . "',\n\t\t\t\t\t\t\t'" . escapeString($serializedUserData) . "')";
             WCF::getDB()->sendQuery($sql);
         } catch (DatabaseException $e) {
             // horizon update workaround
             $sql = "UPDATE \twcf" . WCF_N . "_session\n\t\t\t\t\tSET\tuserData = '" . escapeString($serializedUserData) . "'\n\t\t\t\t\tWHERE\tsessionID = '" . $sessionID . "'";
             WCF::getDB()->sendQuery($sql);
         }
     }
     // return new session object
     return new $this->sessionClassName(null, array('sessionID' => $sessionID, 'packageID' => PACKAGE_ID, 'userID' => $user->userID, 'ipAddress' => UserUtil::getIpAddress(), 'userAgent' => UserUtil::getUserAgent(), 'lastActivityTime' => TIME_NOW, 'requestURI' => UserUtil::getRequestURI(), 'requestMethod' => $requestMethod, 'userData' => $serializedUserData, 'sessionVariables' => '', 'username' => $spider ? $spider['spiderName'] : $user->username, 'spiderID' => $spider ? $spider['spiderID'] : 0, 'isNew' => true));
 }
Пример #2
0
 /**
  * @see Page::readData()
  */
 public function readData()
 {
     parent::readData();
     // init cache data
     $this->cacheData = array('source' => get_class(WCF::getCache()->getCacheSource()), 'version' => '', 'size' => 0, 'files' => 0);
     // filesystem cache
     if ($this->cacheData['source'] == 'DiskCacheSource') {
         // set version
         $this->cacheData['version'] = WCF_VERSION;
         // get package dirs
         $sql = "SELECT\t\tpackage.packageDir\n\t\t\t\tFROM\t\twcf" . WCF_N . "_package_dependency package_dependency\n\t\t\t\tLEFT JOIN\twcf" . WCF_N . "_package package\n\t\t\t\tON\t\t(package.packageID = package_dependency.dependency)\n\t\t\t\tWHERE\t\tpackage_dependency.packageID = " . PACKAGE_ID . "\n\t\t\t\t\t\tAND standalone = 1";
         $result = WCF::getDB()->sendQuery($sql);
         while ($row = WCF::getDB()->fetchArray($result)) {
             $packageDir = FileUtil::getRealPath(WCF_DIR . $row['packageDir']);
             $cacheDir = $packageDir . 'cache';
             if (file_exists($cacheDir)) {
                 $this->caches[$cacheDir] = array();
                 // get files in cache directory
                 $files = glob($cacheDir . '/*.php');
                 // get additional file information
                 if (is_array($files)) {
                     foreach ($files as $file) {
                         $filesize = filesize($file);
                         $this->caches[$cacheDir][] = array('filename' => basename($file), 'filesize' => $filesize, 'mtime' => filemtime($file), 'perm' => substr(sprintf('%o', fileperms($file)), -3), 'writable' => is_writable($file));
                         $this->cacheData['files']++;
                         $this->cacheData['size'] += $filesize;
                     }
                 }
             }
         }
     } else {
         if ($this->cacheData['source'] == 'MemcacheCacheSource') {
             // get version
             require_once WCF_DIR . 'lib/system/cache/source/MemcacheAdapter.class.php';
             $this->cacheData['version'] = MemcacheAdapter::getInstance()->getMemcache()->getVersion();
             // get stats
             $stats = MemcacheAdapter::getInstance()->getMemcache()->getStats();
             $this->cacheData['files'] = $stats['curr_items'];
             $this->cacheData['size'] = $stats['bytes'];
         }
     }
 }
Пример #3
0
 /**
  * Creates a new MemcacheCacheSource object.
  */
 public function __construct()
 {
     $this->adapter = MemcacheAdapter::getInstance();
 }
Пример #4
0
 /**
  * Deletes the expired sessions.
  * 
  * @param	integer		$timestamp
  * @param	boolean		$userSession
  * @param	boolean		$adminSession
  */
 public static function deleteExpiredSessions($timestamp, $userSession = true, $adminSession = true)
 {
     // user sessions
     if ($userSession) {
         if (ENABLE_SESSION_DATA_CACHE && get_class(WCF::getCache()->getCacheSource()) == 'MemcacheCacheSource') {
             require_once WCF_DIR . 'lib/system/cache/source/MemcacheAdapter.class.php';
             $sql = "SELECT\tsessionID\n\t\t\t\t\tFROM\twcf" . WCF_N . "_session\n\t\t\t\t\tWHERE\tlastActivityTime < " . $timestamp;
             $result = WCF::getDB()->sendQuery($sql);
             while ($row = WCF::getDB()->fetchArray($result)) {
                 MemcacheAdapter::getInstance()->getMemcache()->delete('session_userdata_' . $row['sessionID']);
                 MemcacheAdapter::getInstance()->getMemcache()->delete('session_variables_' . $row['sessionID']);
             }
             $sql = "DELETE FROM\twcf" . WCF_N . "_session\n\t\t\t\t\tWHERE\t\tlastActivityTime < " . $timestamp;
             WCF::getDB()->sendQuery($sql);
         } else {
             try {
                 $sql = "DELETE\t\tsession,\n\t\t\t\t\t\t\t\tsession_data\n\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_session session \n\t\t\t\t\t\tLEFT JOIN\twcf" . WCF_N . "_session_data session_data USING (sessionID)\n\t\t\t\t\t\tWHERE\t\tsession.lastActivityTime  < " . $timestamp;
                 WCF::getDB()->sendQuery($sql);
             } catch (DatabaseException $e) {
                 // horizon update workaround
                 $sql = "DELETE FROM\twcf" . WCF_N . "_session\n\t\t\t\t\t\tWHERE\t\tlastActivityTime < " . $timestamp;
                 WCF::getDB()->sendQuery($sql);
             }
         }
     }
     // admin sessions
     if ($adminSession) {
         if (ENABLE_SESSION_DATA_CACHE && get_class(WCF::getCache()->getCacheSource()) == 'MemcacheCacheSource') {
             require_once WCF_DIR . 'lib/system/cache/source/MemcacheAdapter.class.php';
             $sql = "SELECT\tsessionID\n\t\t\t\t\tFROM\twcf" . WCF_N . "_acp_session\n\t\t\t\t\tWHERE\tlastActivityTime < " . $timestamp;
             $result = WCF::getDB()->sendQuery($sql);
             while ($row = WCF::getDB()->fetchArray($result)) {
                 MemcacheAdapter::getInstance()->getMemcache()->delete('acp_session_userdata_' . $row['sessionID']);
                 MemcacheAdapter::getInstance()->getMemcache()->delete('acp_session_variables_' . $row['sessionID']);
             }
             $sql = "DELETE FROM\twcf" . WCF_N . "_acp_session\n\t\t\t\t\tWHERE\t\tlastActivityTime < " . $timestamp;
             WCF::getDB()->sendQuery($sql);
         } else {
             try {
                 $sql = "DELETE\t\tsession,\n\t\t\t\t\t\t\t\tsession_data\n\t\t\t\t\t\tFROM\t\twcf" . WCF_N . "_acp_session session \n\t\t\t\t\t\tLEFT JOIN\twcf" . WCF_N . "_acp_session_data session_data USING (sessionID)\n\t\t\t\t\t\tWHERE\t\tsession.lastActivityTime  < " . $timestamp;
                 WCF::getDB()->sendQuery($sql);
             } catch (DatabaseException $e) {
                 // horizon update workaround
                 $sql = "DELETE FROM\twcf" . WCF_N . "_acp_session\n\t\t\t\t\t\tWHERE\t\tlastActivityTime < " . $timestamp;
                 WCF::getDB()->sendQuery($sql);
             }
         }
     }
     self::deleteOrphanedSessionData($userSession, $adminSession);
 }
 /**
  * Creates a new session.
  * 
  * Generates a new session hash, inserts the new session into database
  * and returns the object of the created session. 
  * 
  * @return 	 Session 	$session
  */
 public function create()
 {
     // create new session hash
     $sessionID = StringUtil::getRandomID();
     // get user automatically
     if (!defined('NO_IMPORTS')) {
         require_once WCF_DIR . 'lib/system/auth/UserAuth.class.php';
     }
     $user = UserAuth::getInstance()->loginAutomatically();
     // create user
     if ($user === null) {
         // no valid user found
         // create guest user
         $user = new $this->userClassName();
     }
     // update user session
     $user->update();
     // insert session into database
     $requestMethod = !empty($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
     $sql = "INSERT INTO \twcf" . WCF_N . "_acp_session\n\t\t\t\t\t(sessionID, packageID, userID, ipAddress, userAgent, lastActivityTime, requestURI, requestMethod)\n\t\t\tVALUES \t\t('" . $sessionID . "',\n\t\t\t\t\t" . PACKAGE_ID . ",\n\t\t\t\t\t" . $user->userID . ",\n\t\t\t\t\t'" . escapeString(UserUtil::getIpAddress()) . "',\n\t\t\t\t\t'" . escapeString(UserUtil::getUserAgent()) . "',\n\t\t\t\t\t" . TIME_NOW . ",\n\t\t\t\t\t'" . escapeString(UserUtil::getRequestURI()) . "',\n\t\t\t\t\t'" . escapeString($requestMethod) . "')";
     WCF::getDB()->sendQuery($sql);
     // save user data
     $serializedUserData = '';
     if (ENABLE_SESSION_DATA_CACHE && get_class(WCF::getCache()->getCacheSource()) == 'MemcacheCacheSource') {
         require_once WCF_DIR . 'lib/system/cache/source/MemcacheAdapter.class.php';
         MemcacheAdapter::getInstance()->getMemcache()->set('acp_session_userdata_' . $sessionID, $user);
     } else {
         $serializedUserData = serialize($user);
         try {
             $sql = "INSERT INTO \twcf" . WCF_N . "_acp_session_data\n\t\t\t\t\t\t\t(sessionID, userData)\n\t\t\t\t\tVALUES \t\t('" . $sessionID . "',\n\t\t\t\t\t\t\t'" . escapeString($serializedUserData) . "')";
             WCF::getDB()->sendQuery($sql);
         } catch (DatabaseException $e) {
             // horizon update workaround
             $sql = "UPDATE \twcf" . WCF_N . "_acp_session\n\t\t\t\t\tSET\tuserData = '" . escapeString($serializedUserData) . "'\n\t\t\t\t\tWHERE\tsessionID = '" . $sessionID . "'";
             WCF::getDB()->sendQuery($sql);
         }
     }
     // return new session object
     return new $this->sessionClassName(null, array('sessionID' => $sessionID, 'packageID' => PACKAGE_ID, 'ipAddress' => UserUtil::getIpAddress(), 'userAgent' => UserUtil::getUserAgent(), 'lastActivityTime' => TIME_NOW, 'requestURI' => UserUtil::getRequestURI(), 'requestMethod' => $requestMethod, 'userData' => $serializedUserData, 'sessionVariables' => '', 'userID' => $user->userID, 'isNew' => true));
 }