芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/public_html/mctv/lib/Factory/SessionFactory.php
. */ namespace Xibo\Factory; use Xibo\Entity\Session; use Xibo\Helper\DateFormatHelper; use Xibo\Helper\SanitizerService; use Xibo\Service\LogServiceInterface; use Xibo\Storage\StorageServiceInterface; use Xibo\Support\Exception\NotFoundException; /** * Class SessionFactory * @package Xibo\Factory */ class SessionFactory extends BaseFactory { /** * Construct a factory * @param StorageServiceInterface $store * @param LogServiceInterface $log * @param SanitizerService $sanitizerService */ public function __construct($store, $log, $sanitizerService) { $this->setCommonDependencies($store, $log, $sanitizerService); } /** * @return Session */ public function createEmpty() { return new Session($this->getStore(), $this->getLog()); } /** * @param $sessionId * @return Session * @throws NotFoundException */ public function getById($sessionId) { $session = $this->query(null, ['sessionId' => $sessionId]); if (count($session) <= 0) throw new NotFoundException(); return $session[0]; } /** * @param int $userId * @return int loggedIn */ public function getActiveSessionsForUser($userId) { $userSession = $this->query(null, ['userId' => $userId, 'type' => 'active']); return (count($userSession) > 0) ? 1 : 0; } /** * @param array $sortOrder * @param array $filterBy * @return Session[] */ public function query($sortOrder = null, $filterBy = []) { $entries = []; $params = []; $sanitizedFilter = $this->getSanitizer($filterBy); $select = ' SELECT `session`.session_id AS sessionId, session.userId, user.userName, isExpired, session.lastAccessed, remoteAddr AS remoteAddress, userAgent, user.userId AS userId, `session`.session_expiration AS expiresAt '; $body = ' FROM `session` LEFT OUTER JOIN user ON user.userID = session.userID WHERE 1 = 1 '; if ($sanitizedFilter->getString('sessionId') != null) { $body .= ' AND session.session_id = :sessionId '; $params['sessionId'] = $sanitizedFilter->getString('sessionId'); } if ($sanitizedFilter->getString('fromDt') != null) { $body .= ' AND session.LastAccessed >= :lastAccessed '; $params['lastAccessed'] = $sanitizedFilter->getDate('fromDt')->setTime(0, 0, 0)->format(DateFormatHelper::getSystemFormat()); } if ($sanitizedFilter->getString('type') != null) { if ($sanitizedFilter->getString('type') == 'active') { $body .= ' AND IsExpired = 0 '; } if ($sanitizedFilter->getString('type') == 'expired') { $body .= ' AND IsExpired = 1 '; } if ($sanitizedFilter->getString('type') == 'guest') { $body .= ' AND IFNULL(session.userID, 0) = 0 '; } } if ($sanitizedFilter->getInt('userId') != null) { $body .= ' AND user.userID = :userId '; $params['userId'] = $sanitizedFilter->getInt('userId'); } // Sorting? $order = ''; if (is_array($sortOrder)) $order .= 'ORDER BY ' . implode(',', $sortOrder); $limit = ''; // Paging if ($filterBy !== null && $sanitizedFilter->getInt('start') !== null && $sanitizedFilter->getInt('length') !== null) { $limit = ' LIMIT ' . intval($sanitizedFilter->getInt('start'), 0) . ', ' . $sanitizedFilter->getInt('length', ['default' => 10]); } $sql = $select . $body . $order . $limit; foreach ($this->getStore()->select($sql, $params) as $row) { $entries[] = $this->createEmpty()->hydrate($row, ['stringProperties' => ['sessionId']]); } // Paging if ($limit != '' && count($entries) > 0) { $results = $this->getStore()->select('SELECT COUNT(*) AS total ' . $body, $params); $this->_countLast = intval($results[0]['total']); } return $entries; } }