Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
<?php
namespace Illuminate\Session;
use Illuminate\Support\InteractsWithTime;
use SessionHandlerInterface;
class ArraySessionHandler implements SessionHandlerInterface
{
use InteractsWithTime;
/**
* The array of stored values.
*
* @var array
*/
protected $storage = [];
/**
* The number of minutes the session should be valid.
*
* @var int
*/
protected $minutes;
/**
* Create a new array driven handler instance.
*
* @param int $minutes
* @return void
*/
public function __construct($minutes)
{
$this->minutes = $minutes;
}
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}
/**
* {@inheritdoc}
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
if (! isset($this->storage[$sessionId])) {
return '';
}
$session = $this->storage[$sessionId];
$expiration = $this->calculateExpiration($this->minutes * 60);
if (isset($session['time']) && $session['time'] >= $expiration) {
return $session['data'];
}
return '';
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$this->storage[$sessionId] = [
'data' => $data,
'time' => $this->currentTime(),
];
return true;
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
if (isset($this->storage[$sessionId])) {
unset($this->storage[$sessionId]);
}
return true;
}
/**
* {@inheritdoc}
*/
public function gc($lifetime)
{
$expiration = $this->calculateExpiration($lifetime);
foreach ($this->storage as $sessionId => $session) {
if ($session['time'] < $expiration) {
unset($this->storage[$sessionId]);
}
}
return true;
}
/**
* Get the expiration time of the session.
*
* @param int $seconds
* @return int
*/
protected function calculateExpiration($seconds)
{
return $this->currentTime() - $seconds;
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace Illuminate\Session;
use Illuminate\Contracts\Cache\Repository as CacheContract;
use SessionHandlerInterface;
class CacheBasedSessionHandler implements SessionHandlerInterface
{
/**
* The cache repository instance.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;
/**
* The number of minutes to store the data in the cache.
*
* @var int
*/
protected $minutes;
/**
* Create a new cache driven handler instance.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param int $minutes
* @return void
*/
public function __construct(CacheContract $cache, $minutes)
{
$this->cache = $cache;
$this->minutes = $minutes;
}
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}
/**
* {@inheritdoc}
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
return $this->cache->get($sessionId, '');
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
return $this->cache->put($sessionId, $data, $this->minutes * 60);
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
return $this->cache->forget($sessionId);
}
/**
* {@inheritdoc}
*/
public function gc($lifetime)
{
return true;
}
/**
* Get the underlying cache repository.
*
* @return \Illuminate\Contracts\Cache\Repository
*/
public function getCache()
{
return $this->cache;
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace Illuminate\Session\Console;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
class SessionTableCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'session:table';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a migration for the session database table';
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* @var \Illuminate\Support\Composer
*/
protected $composer;
/**
* Create a new session table command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Support\Composer $composer
* @return void
*/
public function __construct(Filesystem $files, Composer $composer)
{
parent::__construct();
$this->files = $files;
$this->composer = $composer;
}
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$fullPath = $this->createBaseMigration();
$this->files->put($fullPath, $this->files->get(__DIR__.'/stubs/database.stub'));
$this->info('Migration created successfully!');
$this->composer->dumpAutoloads();
}
/**
* Create a base migration file for the session.
*
* @return string
*/
protected function createBaseMigration()
{
$name = 'create_sessions_table';
$path = $this->laravel->databasePath().'/migrations';
return $this->laravel['migration.creator']->create($name, $path);
}
}

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSessionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->unique();
$table->foreignId('user_id')->nullable();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sessions');
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace Illuminate\Session;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
use Illuminate\Support\InteractsWithTime;
use SessionHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
class CookieSessionHandler implements SessionHandlerInterface
{
use InteractsWithTime;
/**
* The cookie jar instance.
*
* @var \Illuminate\Contracts\Cookie\Factory
*/
protected $cookie;
/**
* The request instance.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The number of minutes the session should be valid.
*
* @var int
*/
protected $minutes;
/**
* Create a new cookie driven handler instance.
*
* @param \Illuminate\Contracts\Cookie\QueueingFactory $cookie
* @param int $minutes
* @return void
*/
public function __construct(CookieJar $cookie, $minutes)
{
$this->cookie = $cookie;
$this->minutes = $minutes;
}
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}
/**
* {@inheritdoc}
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
$value = $this->request->cookies->get($sessionId) ?: '';
if (! is_null($decoded = json_decode($value, true)) && is_array($decoded)) {
if (isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) {
return $decoded['data'];
}
}
return '';
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$this->cookie->queue($sessionId, json_encode([
'data' => $data,
'expires' => $this->availableAt($this->minutes * 60),
]), $this->minutes);
return true;
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
$this->cookie->queue($this->cookie->forget($sessionId));
return true;
}
/**
* {@inheritdoc}
*/
public function gc($lifetime)
{
return true;
}
/**
* Set the request instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return void
*/
public function setRequest(Request $request)
{
$this->request = $request;
}
}

View File

@@ -0,0 +1,294 @@
<?php
namespace Illuminate\Session;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\QueryException;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\InteractsWithTime;
use SessionHandlerInterface;
class DatabaseSessionHandler implements ExistenceAwareInterface, SessionHandlerInterface
{
use InteractsWithTime;
/**
* The database connection instance.
*
* @var \Illuminate\Database\ConnectionInterface
*/
protected $connection;
/**
* The name of the session table.
*
* @var string
*/
protected $table;
/**
* The number of minutes the session should be valid.
*
* @var int
*/
protected $minutes;
/**
* The container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The existence state of the session.
*
* @var bool
*/
protected $exists;
/**
* Create a new database session handler instance.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
* @param int $minutes
* @param \Illuminate\Contracts\Container\Container|null $container
* @return void
*/
public function __construct(ConnectionInterface $connection, $table, $minutes, Container $container = null)
{
$this->table = $table;
$this->minutes = $minutes;
$this->container = $container;
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}
/**
* {@inheritdoc}
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
$session = (object) $this->getQuery()->find($sessionId);
if ($this->expired($session)) {
$this->exists = true;
return '';
}
if (isset($session->payload)) {
$this->exists = true;
return base64_decode($session->payload);
}
return '';
}
/**
* Determine if the session is expired.
*
* @param \stdClass $session
* @return bool
*/
protected function expired($session)
{
return isset($session->last_activity) &&
$session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp();
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$payload = $this->getDefaultPayload($data);
if (! $this->exists) {
$this->read($sessionId);
}
if ($this->exists) {
$this->performUpdate($sessionId, $payload);
} else {
$this->performInsert($sessionId, $payload);
}
return $this->exists = true;
}
/**
* Perform an insert operation on the session ID.
*
* @param string $sessionId
* @param string $payload
* @return bool|null
*/
protected function performInsert($sessionId, $payload)
{
try {
return $this->getQuery()->insert(Arr::set($payload, 'id', $sessionId));
} catch (QueryException $e) {
$this->performUpdate($sessionId, $payload);
}
}
/**
* Perform an update operation on the session ID.
*
* @param string $sessionId
* @param string $payload
* @return int
*/
protected function performUpdate($sessionId, $payload)
{
return $this->getQuery()->where('id', $sessionId)->update($payload);
}
/**
* Get the default payload for the session.
*
* @param string $data
* @return array
*/
protected function getDefaultPayload($data)
{
$payload = [
'payload' => base64_encode($data),
'last_activity' => $this->currentTime(),
];
if (! $this->container) {
return $payload;
}
return tap($payload, function (&$payload) {
$this->addUserInformation($payload)
->addRequestInformation($payload);
});
}
/**
* Add the user information to the session payload.
*
* @param array $payload
* @return $this
*/
protected function addUserInformation(&$payload)
{
if ($this->container->bound(Guard::class)) {
$payload['user_id'] = $this->userId();
}
return $this;
}
/**
* Get the currently authenticated user's ID.
*
* @return mixed
*/
protected function userId()
{
return $this->container->make(Guard::class)->id();
}
/**
* Add the request information to the session payload.
*
* @param array $payload
* @return $this
*/
protected function addRequestInformation(&$payload)
{
if ($this->container->bound('request')) {
$payload = array_merge($payload, [
'ip_address' => $this->ipAddress(),
'user_agent' => $this->userAgent(),
]);
}
return $this;
}
/**
* Get the IP address for the current request.
*
* @return string
*/
protected function ipAddress()
{
return $this->container->make('request')->ip();
}
/**
* Get the user agent for the current request.
*
* @return string
*/
protected function userAgent()
{
return substr((string) $this->container->make('request')->header('User-Agent'), 0, 500);
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
$this->getQuery()->where('id', $sessionId)->delete();
return true;
}
/**
* {@inheritdoc}
*/
public function gc($lifetime)
{
$this->getQuery()->where('last_activity', '<=', $this->currentTime() - $lifetime)->delete();
}
/**
* Get a fresh query builder instance for the table.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function getQuery()
{
return $this->connection->table($this->table);
}
/**
* Set the existence state for the session.
*
* @param bool $value
* @return $this
*/
public function setExists($value)
{
$this->exists = $value;
return $this;
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Illuminate\Session;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
use SessionHandlerInterface;
class EncryptedStore extends Store
{
/**
* The encrypter instance.
*
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
protected $encrypter;
/**
* Create a new session instance.
*
* @param string $name
* @param \SessionHandlerInterface $handler
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param string|null $id
* @return void
*/
public function __construct($name, SessionHandlerInterface $handler, EncrypterContract $encrypter, $id = null)
{
$this->encrypter = $encrypter;
parent::__construct($name, $handler, $id);
}
/**
* Prepare the raw string data from the session for unserialization.
*
* @param string $data
* @return string
*/
protected function prepareForUnserialize($data)
{
try {
return $this->encrypter->decrypt($data);
} catch (DecryptException $e) {
return serialize([]);
}
}
/**
* Prepare the serialized session data for storage.
*
* @param string $data
* @return string
*/
protected function prepareForStorage($data)
{
return $this->encrypter->encrypt($data);
}
/**
* Get the encrypter instance.
*
* @return \Illuminate\Contracts\Encryption\Encrypter
*/
public function getEncrypter()
{
return $this->encrypter;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Illuminate\Session;
interface ExistenceAwareInterface
{
/**
* Set the existence state for the session.
*
* @param bool $value
* @return \SessionHandlerInterface
*/
public function setExists($value);
}

View File

@@ -0,0 +1,113 @@
<?php
namespace Illuminate\Session;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Carbon;
use SessionHandlerInterface;
use Symfony\Component\Finder\Finder;
class FileSessionHandler implements SessionHandlerInterface
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The path where sessions should be stored.
*
* @var string
*/
protected $path;
/**
* The number of minutes the session should be valid.
*
* @var int
*/
protected $minutes;
/**
* Create a new file driven handler instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param string $path
* @param int $minutes
* @return void
*/
public function __construct(Filesystem $files, $path, $minutes)
{
$this->path = $path;
$this->files = $files;
$this->minutes = $minutes;
}
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}
/**
* {@inheritdoc}
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
if ($this->files->isFile($path = $this->path.'/'.$sessionId)) {
if ($this->files->lastModified($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
return $this->files->sharedGet($path);
}
}
return '';
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$this->files->put($this->path.'/'.$sessionId, $data, true);
return true;
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
$this->files->delete($this->path.'/'.$sessionId);
return true;
}
/**
* {@inheritdoc}
*/
public function gc($lifetime)
{
$files = Finder::create()
->in($this->path)
->files()
->ignoreDotFiles(true)
->date('<= now - '.$lifetime.' seconds');
foreach ($files as $file) {
$this->files->delete($file->getRealPath());
}
}
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,96 @@
<?php
namespace Illuminate\Session\Middleware;
use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
class AuthenticateSession
{
/**
* The authentication factory implementation.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(AuthFactory $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (! $request->hasSession() || ! $request->user()) {
return $next($request);
}
if ($this->auth->viaRemember()) {
$passwordHash = explode('|', $request->cookies->get($this->auth->getRecallerName()))[2] ?? null;
if (! $passwordHash || $passwordHash != $request->user()->getAuthPassword()) {
$this->logout($request);
}
}
if (! $request->session()->has('password_hash')) {
$this->storePasswordHashInSession($request);
}
if ($request->session()->get('password_hash') !== $request->user()->getAuthPassword()) {
$this->logout($request);
}
return tap($next($request), function () use ($request) {
$this->storePasswordHashInSession($request);
});
}
/**
* Store the user's current password hash in the session.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function storePasswordHashInSession($request)
{
if (! $request->user()) {
return;
}
$request->session()->put([
'password_hash' => $request->user()->getAuthPassword(),
]);
}
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return void
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function logout($request)
{
$this->auth->logoutCurrentDevice();
$request->session()->flush();
throw new AuthenticationException;
}
}

View File

@@ -0,0 +1,291 @@
<?php
namespace Illuminate\Session\Middleware;
use Closure;
use Illuminate\Contracts\Session\Session;
use Illuminate\Http\Request;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
class StartSession
{
/**
* The session manager.
*
* @var \Illuminate\Session\SessionManager
*/
protected $manager;
/**
* The callback that can resolve an instance of the cache factory.
*
* @var callable|null
*/
protected $cacheFactoryResolver;
/**
* Create a new session middleware.
*
* @param \Illuminate\Session\SessionManager $manager
* @param callable|null $cacheFactoryResolver
* @return void
*/
public function __construct(SessionManager $manager, callable $cacheFactoryResolver = null)
{
$this->manager = $manager;
$this->cacheFactoryResolver = $cacheFactoryResolver;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (! $this->sessionConfigured()) {
return $next($request);
}
$session = $this->getSession($request);
if ($this->manager->shouldBlock() ||
($request->route() && $request->route()->locksFor())) {
return $this->handleRequestWhileBlocking($request, $session, $next);
} else {
return $this->handleStatefulRequest($request, $session, $next);
}
}
/**
* Handle the given request within session state.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Session\Session $session
* @param \Closure $next
* @return mixed
*/
protected function handleRequestWhileBlocking(Request $request, $session, Closure $next)
{
$lockFor = $request->route() && $request->route()->locksFor()
? $request->route()->locksFor()
: 10;
$lock = $this->cache($this->manager->blockDriver())
->lock('session:'.$session->getId(), $lockFor)
->betweenBlockedAttemptsSleepFor(50);
try {
$lock->block(
! is_null($request->route()->waitsFor())
? $request->route()->waitsFor()
: 10
);
return $this->handleStatefulRequest($request, $session, $next);
} finally {
optional($lock)->release();
}
}
/**
* Handle the given request within session state.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Session\Session $session
* @param \Closure $next
* @return mixed
*/
protected function handleStatefulRequest(Request $request, $session, Closure $next)
{
// If a session driver has been configured, we will need to start the session here
// so that the data is ready for an application. Note that the Laravel sessions
// do not make use of PHP "native" sessions in any way since they are crappy.
$request->setLaravelSession(
$this->startSession($request, $session)
);
$this->collectGarbage($session);
$response = $next($request);
$this->storeCurrentUrl($request, $session);
$this->addCookieToResponse($response, $session);
// Again, if the session has been configured we will need to close out the session
// so that the attributes may be persisted to some storage medium. We will also
// add the session identifier cookie to the application response headers now.
$this->saveSession($request);
return $response;
}
/**
* Start the session for the given request.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Session\Session $session
* @return \Illuminate\Contracts\Session\Session
*/
protected function startSession(Request $request, $session)
{
return tap($session, function ($session) use ($request) {
$session->setRequestOnHandler($request);
$session->start();
});
}
/**
* Get the session implementation from the manager.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Session\Session
*/
public function getSession(Request $request)
{
return tap($this->manager->driver(), function ($session) use ($request) {
$session->setId($request->cookies->get($session->getName()));
});
}
/**
* Remove the garbage from the session if necessary.
*
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
protected function collectGarbage(Session $session)
{
$config = $this->manager->getSessionConfig();
// Here we will see if this request hits the garbage collection lottery by hitting
// the odds needed to perform garbage collection on any given request. If we do
// hit it, we'll call this handler to let it delete all the expired sessions.
if ($this->configHitsLottery($config)) {
$session->getHandler()->gc($this->getSessionLifetimeInSeconds());
}
}
/**
* Determine if the configuration odds hit the lottery.
*
* @param array $config
* @return bool
*/
protected function configHitsLottery(array $config)
{
return random_int(1, $config['lottery'][1]) <= $config['lottery'][0];
}
/**
* Store the current URL for the request if necessary.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
protected function storeCurrentUrl(Request $request, $session)
{
if ($request->method() === 'GET' &&
$request->route() &&
! $request->ajax() &&
! $request->prefetch()) {
$session->setPreviousUrl($request->fullUrl());
}
}
/**
* Add the session cookie to the application response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @param \Illuminate\Contracts\Session\Session $session
* @return void
*/
protected function addCookieToResponse(Response $response, Session $session)
{
if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) {
$response->headers->setCookie(new Cookie(
$session->getName(), $session->getId(), $this->getCookieExpirationDate(),
$config['path'], $config['domain'], $config['secure'] ?? false,
$config['http_only'] ?? true, false, $config['same_site'] ?? null
));
}
}
/**
* Save the session data to storage.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function saveSession($request)
{
$this->manager->driver()->save();
}
/**
* Get the session lifetime in seconds.
*
* @return int
*/
protected function getSessionLifetimeInSeconds()
{
return ($this->manager->getSessionConfig()['lifetime'] ?? null) * 60;
}
/**
* Get the cookie lifetime in seconds.
*
* @return \DateTimeInterface|int
*/
protected function getCookieExpirationDate()
{
$config = $this->manager->getSessionConfig();
return $config['expire_on_close'] ? 0 : Date::instance(
Carbon::now()->addRealMinutes($config['lifetime'])
);
}
/**
* Determine if a session driver has been configured.
*
* @return bool
*/
protected function sessionConfigured()
{
return ! is_null($this->manager->getSessionConfig()['driver'] ?? null);
}
/**
* Determine if the configured session driver is persistent.
*
* @param array|null $config
* @return bool
*/
protected function sessionIsPersistent(array $config = null)
{
$config = $config ?: $this->manager->getSessionConfig();
return ! is_null($config['driver'] ?? null);
}
/**
* Resolve the given cache driver.
*
* @param string $driver
* @return \Illuminate\Cache\Store
*/
protected function cache($driver)
{
return call_user_func($this->cacheFactoryResolver)->driver($driver);
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Illuminate\Session;
use SessionHandlerInterface;
class NullSessionHandler implements SessionHandlerInterface
{
/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}
/**
* {@inheritdoc}
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
return '';
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
return true;
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
return true;
}
/**
* {@inheritdoc}
*/
public function gc($lifetime)
{
return true;
}
}

View File

@@ -0,0 +1,255 @@
<?php
namespace Illuminate\Session;
use Illuminate\Support\Manager;
class SessionManager extends Manager
{
/**
* Call a custom driver creator.
*
* @param string $driver
* @return mixed
*/
protected function callCustomCreator($driver)
{
return $this->buildSession(parent::callCustomCreator($driver));
}
/**
* Create an instance of the "null" session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createNullDriver()
{
return $this->buildSession(new NullSessionHandler);
}
/**
* Create an instance of the "array" session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createArrayDriver()
{
return $this->buildSession(new ArraySessionHandler(
$this->config->get('session.lifetime')
));
}
/**
* Create an instance of the "cookie" session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createCookieDriver()
{
return $this->buildSession(new CookieSessionHandler(
$this->container->make('cookie'), $this->config->get('session.lifetime')
));
}
/**
* Create an instance of the file session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createFileDriver()
{
return $this->createNativeDriver();
}
/**
* Create an instance of the file session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createNativeDriver()
{
$lifetime = $this->config->get('session.lifetime');
return $this->buildSession(new FileSessionHandler(
$this->container->make('files'), $this->config->get('session.files'), $lifetime
));
}
/**
* Create an instance of the database session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createDatabaseDriver()
{
$table = $this->config->get('session.table');
$lifetime = $this->config->get('session.lifetime');
return $this->buildSession(new DatabaseSessionHandler(
$this->getDatabaseConnection(), $table, $lifetime, $this->container
));
}
/**
* Get the database connection for the database driver.
*
* @return \Illuminate\Database\Connection
*/
protected function getDatabaseConnection()
{
$connection = $this->config->get('session.connection');
return $this->container->make('db')->connection($connection);
}
/**
* Create an instance of the APC session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createApcDriver()
{
return $this->createCacheBased('apc');
}
/**
* Create an instance of the Memcached session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createMemcachedDriver()
{
return $this->createCacheBased('memcached');
}
/**
* Create an instance of the Redis session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createRedisDriver()
{
$handler = $this->createCacheHandler('redis');
$handler->getCache()->getStore()->setConnection(
$this->config->get('session.connection')
);
return $this->buildSession($handler);
}
/**
* Create an instance of the DynamoDB session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createDynamodbDriver()
{
return $this->createCacheBased('dynamodb');
}
/**
* Create an instance of a cache driven driver.
*
* @param string $driver
* @return \Illuminate\Session\Store
*/
protected function createCacheBased($driver)
{
return $this->buildSession($this->createCacheHandler($driver));
}
/**
* Create the cache based session handler instance.
*
* @param string $driver
* @return \Illuminate\Session\CacheBasedSessionHandler
*/
protected function createCacheHandler($driver)
{
$store = $this->config->get('session.store') ?: $driver;
return new CacheBasedSessionHandler(
clone $this->container->make('cache')->store($store),
$this->config->get('session.lifetime')
);
}
/**
* Build the session instance.
*
* @param \SessionHandlerInterface $handler
* @return \Illuminate\Session\Store
*/
protected function buildSession($handler)
{
return $this->config->get('session.encrypt')
? $this->buildEncryptedSession($handler)
: new Store($this->config->get('session.cookie'), $handler);
}
/**
* Build the encrypted session instance.
*
* @param \SessionHandlerInterface $handler
* @return \Illuminate\Session\EncryptedStore
*/
protected function buildEncryptedSession($handler)
{
return new EncryptedStore(
$this->config->get('session.cookie'), $handler, $this->container['encrypter']
);
}
/**
* Determine if requests for the same session should wait for each to finish before executing.
*
* @return bool
*/
public function shouldBlock()
{
return $this->config->get('session.block', false);
}
/**
* Get the name of the cache store / driver that should be used to acquire session locks.
*
* @return string|null
*/
public function blockDriver()
{
return $this->config->get('session.block_store');
}
/**
* Get the session configuration.
*
* @return array
*/
public function getSessionConfig()
{
return $this->config->get('session');
}
/**
* Get the default session driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->config->get('session.driver');
}
/**
* Set the default session driver name.
*
* @param string $name
* @return void
*/
public function setDefaultDriver($name)
{
$this->config->set('session.driver', $name);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Illuminate\Session;
use Illuminate\Contracts\Cache\Factory as CacheFactory;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\ServiceProvider;
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerSessionManager();
$this->registerSessionDriver();
$this->app->singleton(StartSession::class, function () {
return new StartSession($this->app->make(SessionManager::class), function () {
return $this->app->make(CacheFactory::class);
});
});
}
/**
* Register the session manager instance.
*
* @return void
*/
protected function registerSessionManager()
{
$this->app->singleton('session', function ($app) {
return new SessionManager($app);
});
}
/**
* Register the session driver instance.
*
* @return void
*/
protected function registerSessionDriver()
{
$this->app->singleton('session.store', function ($app) {
// First, we will create the session manager which is responsible for the
// creation of the various session drivers when they are needed by the
// application instance, and will resolve them on a lazy load basis.
return $app->make('session')->driver();
});
}
}

View File

@@ -0,0 +1,672 @@
<?php
namespace Illuminate\Session;
use Closure;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use SessionHandlerInterface;
use stdClass;
class Store implements Session
{
/**
* The session ID.
*
* @var string
*/
protected $id;
/**
* The session name.
*
* @var string
*/
protected $name;
/**
* The session attributes.
*
* @var array
*/
protected $attributes = [];
/**
* The session handler implementation.
*
* @var \SessionHandlerInterface
*/
protected $handler;
/**
* Session store started status.
*
* @var bool
*/
protected $started = false;
/**
* Create a new session instance.
*
* @param string $name
* @param \SessionHandlerInterface $handler
* @param string|null $id
* @return void
*/
public function __construct($name, SessionHandlerInterface $handler, $id = null)
{
$this->setId($id);
$this->name = $name;
$this->handler = $handler;
}
/**
* Start the session, reading the data from a handler.
*
* @return bool
*/
public function start()
{
$this->loadSession();
if (! $this->has('_token')) {
$this->regenerateToken();
}
return $this->started = true;
}
/**
* Load the session data from the handler.
*
* @return void
*/
protected function loadSession()
{
$this->attributes = array_merge($this->attributes, $this->readFromHandler());
}
/**
* Read the session data from the handler.
*
* @return array
*/
protected function readFromHandler()
{
if ($data = $this->handler->read($this->getId())) {
$data = @unserialize($this->prepareForUnserialize($data));
if ($data !== false && ! is_null($data) && is_array($data)) {
return $data;
}
}
return [];
}
/**
* Prepare the raw string data from the session for unserialization.
*
* @param string $data
* @return string
*/
protected function prepareForUnserialize($data)
{
return $data;
}
/**
* Save the session data to storage.
*
* @return void
*/
public function save()
{
$this->ageFlashData();
$this->handler->write($this->getId(), $this->prepareForStorage(
serialize($this->attributes)
));
$this->started = false;
}
/**
* Prepare the serialized session data for storage.
*
* @param string $data
* @return string
*/
protected function prepareForStorage($data)
{
return $data;
}
/**
* Age the flash data for the session.
*
* @return void
*/
public function ageFlashData()
{
$this->forget($this->get('_flash.old', []));
$this->put('_flash.old', $this->get('_flash.new', []));
$this->put('_flash.new', []);
}
/**
* Get all of the session data.
*
* @return array
*/
public function all()
{
return $this->attributes;
}
/**
* Get a subset of the session data.
*
* @param array $keys
* @return array
*/
public function only(array $keys)
{
return Arr::only($this->attributes, $keys);
}
/**
* Checks if a key exists.
*
* @param string|array $key
* @return bool
*/
public function exists($key)
{
$placeholder = new stdClass;
return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) use ($placeholder) {
return $this->get($key, $placeholder) === $placeholder;
});
}
/**
* Checks if a key is present and not null.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) {
return is_null($this->get($key));
});
}
/**
* Get an item from the session.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null)
{
return Arr::get($this->attributes, $key, $default);
}
/**
* Get the value of a given key and then forget it.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function pull($key, $default = null)
{
return Arr::pull($this->attributes, $key, $default);
}
/**
* Determine if the session contains old input.
*
* @param string|null $key
* @return bool
*/
public function hasOldInput($key = null)
{
$old = $this->getOldInput($key);
return is_null($key) ? count($old) > 0 : ! is_null($old);
}
/**
* Get the requested item from the flashed input array.
*
* @param string|null $key
* @param mixed $default
* @return mixed
*/
public function getOldInput($key = null, $default = null)
{
return Arr::get($this->get('_old_input', []), $key, $default);
}
/**
* Replace the given session attributes entirely.
*
* @param array $attributes
* @return void
*/
public function replace(array $attributes)
{
$this->put($attributes);
}
/**
* Put a key / value pair or array of key / value pairs in the session.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function put($key, $value = null)
{
if (! is_array($key)) {
$key = [$key => $value];
}
foreach ($key as $arrayKey => $arrayValue) {
Arr::set($this->attributes, $arrayKey, $arrayValue);
}
}
/**
* Get an item from the session, or store the default value.
*
* @param string $key
* @param \Closure $callback
* @return mixed
*/
public function remember($key, Closure $callback)
{
if (! is_null($value = $this->get($key))) {
return $value;
}
return tap($callback(), function ($value) use ($key) {
$this->put($key, $value);
});
}
/**
* Push a value onto a session array.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function push($key, $value)
{
$array = $this->get($key, []);
$array[] = $value;
$this->put($key, $array);
}
/**
* Increment the value of an item in the session.
*
* @param string $key
* @param int $amount
* @return mixed
*/
public function increment($key, $amount = 1)
{
$this->put($key, $value = $this->get($key, 0) + $amount);
return $value;
}
/**
* Decrement the value of an item in the session.
*
* @param string $key
* @param int $amount
* @return int
*/
public function decrement($key, $amount = 1)
{
return $this->increment($key, $amount * -1);
}
/**
* Flash a key / value pair to the session.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function flash(string $key, $value = true)
{
$this->put($key, $value);
$this->push('_flash.new', $key);
$this->removeFromOldFlashData([$key]);
}
/**
* Flash a key / value pair to the session for immediate use.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function now($key, $value)
{
$this->put($key, $value);
$this->push('_flash.old', $key);
}
/**
* Reflash all of the session flash data.
*
* @return void
*/
public function reflash()
{
$this->mergeNewFlashes($this->get('_flash.old', []));
$this->put('_flash.old', []);
}
/**
* Reflash a subset of the current flash data.
*
* @param array|mixed $keys
* @return void
*/
public function keep($keys = null)
{
$this->mergeNewFlashes($keys = is_array($keys) ? $keys : func_get_args());
$this->removeFromOldFlashData($keys);
}
/**
* Merge new flash keys into the new flash array.
*
* @param array $keys
* @return void
*/
protected function mergeNewFlashes(array $keys)
{
$values = array_unique(array_merge($this->get('_flash.new', []), $keys));
$this->put('_flash.new', $values);
}
/**
* Remove the given keys from the old flash data.
*
* @param array $keys
* @return void
*/
protected function removeFromOldFlashData(array $keys)
{
$this->put('_flash.old', array_diff($this->get('_flash.old', []), $keys));
}
/**
* Flash an input array to the session.
*
* @param array $value
* @return void
*/
public function flashInput(array $value)
{
$this->flash('_old_input', $value);
}
/**
* Remove an item from the session, returning its value.
*
* @param string $key
* @return mixed
*/
public function remove($key)
{
return Arr::pull($this->attributes, $key);
}
/**
* Remove one or many items from the session.
*
* @param string|array $keys
* @return void
*/
public function forget($keys)
{
Arr::forget($this->attributes, $keys);
}
/**
* Remove all of the items from the session.
*
* @return void
*/
public function flush()
{
$this->attributes = [];
}
/**
* Flush the session data and regenerate the ID.
*
* @return bool
*/
public function invalidate()
{
$this->flush();
return $this->migrate(true);
}
/**
* Generate a new session identifier.
*
* @param bool $destroy
* @return bool
*/
public function regenerate($destroy = false)
{
return tap($this->migrate($destroy), function () {
$this->regenerateToken();
});
}
/**
* Generate a new session ID for the session.
*
* @param bool $destroy
* @return bool
*/
public function migrate($destroy = false)
{
if ($destroy) {
$this->handler->destroy($this->getId());
}
$this->setExists(false);
$this->setId($this->generateSessionId());
return true;
}
/**
* Determine if the session has been started.
*
* @return bool
*/
public function isStarted()
{
return $this->started;
}
/**
* Get the name of the session.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set the name of the session.
*
* @param string $name
* @return void
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get the current session ID.
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Set the session ID.
*
* @param string $id
* @return void
*/
public function setId($id)
{
$this->id = $this->isValidId($id) ? $id : $this->generateSessionId();
}
/**
* Determine if this is a valid session ID.
*
* @param string $id
* @return bool
*/
public function isValidId($id)
{
return is_string($id) && ctype_alnum($id) && strlen($id) === 40;
}
/**
* Get a new, random session ID.
*
* @return string
*/
protected function generateSessionId()
{
return Str::random(40);
}
/**
* Set the existence of the session on the handler if applicable.
*
* @param bool $value
* @return void
*/
public function setExists($value)
{
if ($this->handler instanceof ExistenceAwareInterface) {
$this->handler->setExists($value);
}
}
/**
* Get the CSRF token value.
*
* @return string
*/
public function token()
{
return $this->get('_token');
}
/**
* Regenerate the CSRF token value.
*
* @return void
*/
public function regenerateToken()
{
$this->put('_token', Str::random(40));
}
/**
* Get the previous URL from the session.
*
* @return string|null
*/
public function previousUrl()
{
return $this->get('_previous.url');
}
/**
* Set the "previous" URL in the session.
*
* @param string $url
* @return void
*/
public function setPreviousUrl($url)
{
$this->put('_previous.url', $url);
}
/**
* Get the underlying session handler implementation.
*
* @return \SessionHandlerInterface
*/
public function getHandler()
{
return $this->handler;
}
/**
* Determine if the session handler needs a request.
*
* @return bool
*/
public function handlerNeedsRequest()
{
return $this->handler instanceof CookieSessionHandler;
}
/**
* Set the request on the handler instance.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
public function setRequestOnHandler($request)
{
if ($this->handlerNeedsRequest()) {
$this->handler->setRequest($request);
}
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Illuminate\Session;
use Exception;
class TokenMismatchException extends Exception
{
//
}

View File

@@ -0,0 +1,42 @@
{
"name": "illuminate/session",
"description": "The Illuminate Session package.",
"license": "MIT",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"require": {
"php": "^7.2.5|^8.0",
"ext-json": "*",
"illuminate/contracts": "^7.0",
"illuminate/filesystem": "^7.0",
"illuminate/support": "^7.0",
"symfony/finder": "^5.0",
"symfony/http-foundation": "^5.0"
},
"autoload": {
"psr-4": {
"Illuminate\\Session\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "7.x-dev"
}
},
"suggest": {
"illuminate/console": "Required to use the session:table command (^7.0)."
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}