1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000 |
- <?php
- namespace yii\web;
- use Yii;
- use yii\base\Component;
- use yii\base\InvalidArgumentException;
- use yii\base\InvalidConfigException;
- class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable
- {
-
- public $flashParam = '__flash';
-
- public $handler;
-
- private $_cookieParams = ['httponly' => true];
-
- private $frozenSessionData;
-
- public function init()
- {
- parent::init();
- register_shutdown_function([$this, 'close']);
- if ($this->getIsActive()) {
- Yii::warning('Session is already started', __METHOD__);
- $this->updateFlashCounters();
- }
- }
-
- public function getUseCustomStorage()
- {
- return false;
- }
-
- public function open()
- {
- if ($this->getIsActive()) {
- return;
- }
- $this->registerSessionHandler();
- $this->setCookieParamsInternal();
- YII_DEBUG ? session_start() : @session_start();
- if ($this->getIsActive()) {
- Yii::info('Session started', __METHOD__);
- $this->updateFlashCounters();
- } else {
- $error = error_get_last();
- $message = isset($error['message']) ? $error['message'] : 'Failed to start session.';
- Yii::error($message, __METHOD__);
- }
- }
-
- protected function registerSessionHandler()
- {
- if ($this->handler !== null) {
- if (!is_object($this->handler)) {
- $this->handler = Yii::createObject($this->handler);
- }
- if (!$this->handler instanceof \SessionHandlerInterface) {
- throw new InvalidConfigException('"' . get_class($this) . '::handler" must implement the SessionHandlerInterface.');
- }
- YII_DEBUG ? session_set_save_handler($this->handler, false) : @session_set_save_handler($this->handler, false);
- } elseif ($this->getUseCustomStorage()) {
- if (YII_DEBUG) {
- session_set_save_handler(
- [$this, 'openSession'],
- [$this, 'closeSession'],
- [$this, 'readSession'],
- [$this, 'writeSession'],
- [$this, 'destroySession'],
- [$this, 'gcSession']
- );
- } else {
- @session_set_save_handler(
- [$this, 'openSession'],
- [$this, 'closeSession'],
- [$this, 'readSession'],
- [$this, 'writeSession'],
- [$this, 'destroySession'],
- [$this, 'gcSession']
- );
- }
- }
- }
-
- public function close()
- {
- if ($this->getIsActive()) {
- YII_DEBUG ? session_write_close() : @session_write_close();
- }
- }
-
- public function destroy()
- {
- if ($this->getIsActive()) {
- $sessionId = session_id();
- $this->close();
- $this->setId($sessionId);
- $this->open();
- session_unset();
- session_destroy();
- $this->setId($sessionId);
- }
- }
-
- public function getIsActive()
- {
- return session_status() === PHP_SESSION_ACTIVE;
- }
- private $_hasSessionId;
-
- public function getHasSessionId()
- {
- if ($this->_hasSessionId === null) {
- $name = $this->getName();
- $request = Yii::$app->getRequest();
- if (!empty($_COOKIE[$name]) && ini_get('session.use_cookies')) {
- $this->_hasSessionId = true;
- } elseif (!ini_get('session.use_only_cookies') && ini_get('session.use_trans_sid')) {
- $this->_hasSessionId = $request->get($name) != '';
- } else {
- $this->_hasSessionId = false;
- }
- }
- return $this->_hasSessionId;
- }
-
- public function setHasSessionId($value)
- {
- $this->_hasSessionId = $value;
- }
-
- public function getId()
- {
- return session_id();
- }
-
- public function setId($value)
- {
- session_id($value);
- }
-
- public function regenerateID($deleteOldSession = false)
- {
- if ($this->getIsActive()) {
-
-
- if (YII_DEBUG && !headers_sent()) {
- session_regenerate_id($deleteOldSession);
- } else {
- @session_regenerate_id($deleteOldSession);
- }
- }
- }
-
- public function getName()
- {
- return session_name();
- }
-
- public function setName($value)
- {
- $this->freeze();
- session_name($value);
- $this->unfreeze();
- }
-
- public function getSavePath()
- {
- return session_save_path();
- }
-
- public function setSavePath($value)
- {
- $path = Yii::getAlias($value);
- if (is_dir($path)) {
- session_save_path($path);
- } else {
- throw new InvalidArgumentException("Session save path is not a valid directory: $value");
- }
- }
-
- public function getCookieParams()
- {
- return array_merge(session_get_cookie_params(), array_change_key_case($this->_cookieParams));
- }
-
- public function setCookieParams(array $value)
- {
- $this->_cookieParams = $value;
- }
-
- private function setCookieParamsInternal()
- {
- $data = $this->getCookieParams();
- if (isset($data['lifetime'], $data['path'], $data['domain'], $data['secure'], $data['httponly'])) {
- if (PHP_VERSION_ID >= 70300) {
- session_set_cookie_params($data);
- } else {
- if (!empty($data['sameSite'])) {
- throw new InvalidConfigException('sameSite cookie is not supported by PHP versions < 7.3.0 (set it to null in this environment)');
- }
- session_set_cookie_params($data['lifetime'], $data['path'], $data['domain'], $data['secure'], $data['httponly']);
- }
- } else {
- throw new InvalidArgumentException('Please make sure cookieParams contains these elements: lifetime, path, domain, secure and httponly.');
- }
- }
-
- public function getUseCookies()
- {
- if (ini_get('session.use_cookies') === '0') {
- return false;
- } elseif (ini_get('session.use_only_cookies') === '1') {
- return true;
- }
- return null;
- }
-
- public function setUseCookies($value)
- {
- $this->freeze();
- if ($value === false) {
- ini_set('session.use_cookies', '0');
- ini_set('session.use_only_cookies', '0');
- } elseif ($value === true) {
- ini_set('session.use_cookies', '1');
- ini_set('session.use_only_cookies', '1');
- } else {
- ini_set('session.use_cookies', '1');
- ini_set('session.use_only_cookies', '0');
- }
- $this->unfreeze();
- }
-
- public function getGCProbability()
- {
- return (float) (ini_get('session.gc_probability') / ini_get('session.gc_divisor') * 100);
- }
-
- public function setGCProbability($value)
- {
- $this->freeze();
- if ($value >= 0 && $value <= 100) {
-
- ini_set('session.gc_probability', floor($value * 21474836.47));
- ini_set('session.gc_divisor', 2147483647);
- } else {
- throw new InvalidArgumentException('GCProbability must be a value between 0 and 100.');
- }
- $this->unfreeze();
- }
-
- public function getUseTransparentSessionID()
- {
- return ini_get('session.use_trans_sid') == 1;
- }
-
- public function setUseTransparentSessionID($value)
- {
- $this->freeze();
- ini_set('session.use_trans_sid', $value ? '1' : '0');
- $this->unfreeze();
- }
-
- public function getTimeout()
- {
- return (int) ini_get('session.gc_maxlifetime');
- }
-
- public function setTimeout($value)
- {
- $this->freeze();
- ini_set('session.gc_maxlifetime', $value);
- $this->unfreeze();
- }
-
- public function openSession($savePath, $sessionName)
- {
- return true;
- }
-
- public function closeSession()
- {
- return true;
- }
-
- public function readSession($id)
- {
- return '';
- }
-
- public function writeSession($id, $data)
- {
- return true;
- }
-
- public function destroySession($id)
- {
- return true;
- }
-
- public function gcSession($maxLifetime)
- {
- return true;
- }
-
- public function getIterator()
- {
- $this->open();
- return new SessionIterator();
- }
-
- public function getCount()
- {
- $this->open();
- return count($_SESSION);
- }
-
- public function count()
- {
- return $this->getCount();
- }
-
- public function get($key, $defaultValue = null)
- {
- $this->open();
- return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
- }
-
- public function set($key, $value)
- {
- $this->open();
- $_SESSION[$key] = $value;
- }
-
- public function remove($key)
- {
- $this->open();
- if (isset($_SESSION[$key])) {
- $value = $_SESSION[$key];
- unset($_SESSION[$key]);
- return $value;
- }
- return null;
- }
-
- public function removeAll()
- {
- $this->open();
- foreach (array_keys($_SESSION) as $key) {
- unset($_SESSION[$key]);
- }
- }
-
- public function has($key)
- {
- $this->open();
- return isset($_SESSION[$key]);
- }
-
- protected function updateFlashCounters()
- {
- $counters = $this->get($this->flashParam, []);
- if (is_array($counters)) {
- foreach ($counters as $key => $count) {
- if ($count > 0) {
- unset($counters[$key], $_SESSION[$key]);
- } elseif ($count == 0) {
- $counters[$key]++;
- }
- }
- $_SESSION[$this->flashParam] = $counters;
- } else {
-
- unset($_SESSION[$this->flashParam]);
- }
- }
-
- public function getFlash($key, $defaultValue = null, $delete = false)
- {
- $counters = $this->get($this->flashParam, []);
- if (isset($counters[$key])) {
- $value = $this->get($key, $defaultValue);
- if ($delete) {
- $this->removeFlash($key);
- } elseif ($counters[$key] < 0) {
-
- $counters[$key] = 1;
- $_SESSION[$this->flashParam] = $counters;
- }
- return $value;
- }
- return $defaultValue;
- }
-
- public function getAllFlashes($delete = false)
- {
- $counters = $this->get($this->flashParam, []);
- $flashes = [];
- foreach (array_keys($counters) as $key) {
- if (array_key_exists($key, $_SESSION)) {
- $flashes[$key] = $_SESSION[$key];
- if ($delete) {
- unset($counters[$key], $_SESSION[$key]);
- } elseif ($counters[$key] < 0) {
-
- $counters[$key] = 1;
- }
- } else {
- unset($counters[$key]);
- }
- }
- $_SESSION[$this->flashParam] = $counters;
- return $flashes;
- }
-
- public function setFlash($key, $value = true, $removeAfterAccess = true)
- {
- $counters = $this->get($this->flashParam, []);
- $counters[$key] = $removeAfterAccess ? -1 : 0;
- $_SESSION[$key] = $value;
- $_SESSION[$this->flashParam] = $counters;
- }
-
- public function addFlash($key, $value = true, $removeAfterAccess = true)
- {
- $counters = $this->get($this->flashParam, []);
- $counters[$key] = $removeAfterAccess ? -1 : 0;
- $_SESSION[$this->flashParam] = $counters;
- if (empty($_SESSION[$key])) {
- $_SESSION[$key] = [$value];
- } elseif (is_array($_SESSION[$key])) {
- $_SESSION[$key][] = $value;
- } else {
- $_SESSION[$key] = [$_SESSION[$key], $value];
- }
- }
-
- public function removeFlash($key)
- {
- $counters = $this->get($this->flashParam, []);
- $value = isset($_SESSION[$key], $counters[$key]) ? $_SESSION[$key] : null;
- unset($counters[$key], $_SESSION[$key]);
- $_SESSION[$this->flashParam] = $counters;
- return $value;
- }
-
- public function removeAllFlashes()
- {
- $counters = $this->get($this->flashParam, []);
- foreach (array_keys($counters) as $key) {
- unset($_SESSION[$key]);
- }
- unset($_SESSION[$this->flashParam]);
- }
-
- public function hasFlash($key)
- {
- return $this->getFlash($key) !== null;
- }
-
- public function offsetExists($offset)
- {
- $this->open();
- return isset($_SESSION[$offset]);
- }
-
- public function offsetGet($offset)
- {
- $this->open();
- return isset($_SESSION[$offset]) ? $_SESSION[$offset] : null;
- }
-
- public function offsetSet($offset, $item)
- {
- $this->open();
- $_SESSION[$offset] = $item;
- }
-
- public function offsetUnset($offset)
- {
- $this->open();
- unset($_SESSION[$offset]);
- }
-
- protected function freeze()
- {
- if ($this->getIsActive()) {
- if (isset($_SESSION)) {
- $this->frozenSessionData = $_SESSION;
- }
- $this->close();
- Yii::info('Session frozen', __METHOD__);
- }
- }
-
- protected function unfreeze()
- {
- if (null !== $this->frozenSessionData) {
- YII_DEBUG ? session_start() : @session_start();
- if ($this->getIsActive()) {
- Yii::info('Session unfrozen', __METHOD__);
- } else {
- $error = error_get_last();
- $message = isset($error['message']) ? $error['message'] : 'Failed to unfreeze session.';
- Yii::error($message, __METHOD__);
- }
- $_SESSION = $this->frozenSessionData;
- $this->frozenSessionData = null;
- }
- }
-
- public function setCacheLimiter($cacheLimiter)
- {
- $this->freeze();
- session_cache_limiter($cacheLimiter);
- $this->unfreeze();
- }
-
- public function getCacheLimiter()
- {
- return session_cache_limiter();
- }
- }
|