Pressroom template verwijderd, website naar root van repo

This commit is contained in:
2020-03-22 15:30:52 +01:00
parent 2cb6a77425
commit f3d1c41e91
7620 changed files with 0 additions and 186900 deletions

View File

@@ -0,0 +1,94 @@
<?php
namespace Illuminate\Session;
use SessionHandlerInterface;
use Illuminate\Contracts\Cache\Repository as CacheContract;
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);
}
/**
* {@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\Support\Composer;
use Illuminate\Filesystem\Filesystem;
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 fire()
{
$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\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSessionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->unique();
$table->unsignedInteger('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,119 @@
<?php
namespace Illuminate\Session;
use Carbon\Carbon;
use SessionHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
class CookieSessionHandler implements SessionHandlerInterface
{
/**
* 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']) && Carbon::now()->getTimestamp() <= $decoded['expires']) {
return $decoded['data'];
}
}
return '';
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$this->cookie->queue($sessionId, json_encode([
'data' => $data,
'expires' => Carbon::now()->addMinutes($this->minutes)->getTimestamp(),
]), $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,289 @@
<?php
namespace Illuminate\Session;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use SessionHandlerInterface;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Container\Container;
class DatabaseSessionHandler implements SessionHandlerInterface, ExistenceAwareInterface
{
/**
* 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);
}
}
/**
* 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 void
*/
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' => Carbon::now()->getTimestamp(),
];
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', '<=', Carbon::now()->getTimestamp() - $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 SessionHandlerInterface;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
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 Carbon\Carbon;
use SessionHandlerInterface;
use Symfony\Component\Finder\Finder;
use Illuminate\Filesystem\Filesystem;
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->exists($path = $this->path.'/'.$sessionId)) {
if (filemtime($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
return $this->files->get($path, true);
}
}
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,92 @@
<?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->user() || ! $request->session()) {
return $next($request);
}
if (! $request->session()->has('password_hash') && $this->auth->viaRemember()) {
$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->logout();
$request->session()->flush();
throw new AuthenticationException;
}
}

View File

@@ -0,0 +1,243 @@
<?php
namespace Illuminate\Session\Middleware;
use Closure;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Session\SessionManager;
use Illuminate\Contracts\Session\Session;
use Illuminate\Session\CookieSessionHandler;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
class StartSession
{
/**
* The session manager.
*
* @var \Illuminate\Session\SessionManager
*/
protected $manager;
/**
* Indicates if the session was handled for the current request.
*
* @var bool
*/
protected $sessionHandled = false;
/**
* Create a new session middleware.
*
* @param \Illuminate\Session\SessionManager $manager
* @return void
*/
public function __construct(SessionManager $manager)
{
$this->manager = $manager;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$this->sessionHandled = true;
// 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.
if ($this->sessionConfigured()) {
$request->setLaravelSession(
$session = $this->startSession($request)
);
$this->collectGarbage($session);
}
$response = $next($request);
// 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.
if ($this->sessionConfigured()) {
$this->storeCurrentUrl($request, $session);
$this->addCookieToResponse($response, $session);
}
return $response;
}
/**
* Perform any final actions for the request lifecycle.
*
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
public function terminate($request, $response)
{
if ($this->sessionHandled && $this->sessionConfigured() && ! $this->usingCookieSessions()) {
$this->manager->driver()->save();
}
}
/**
* Start the session for the given request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Session\Session
*/
protected function startSession(Request $request)
{
return tap($this->getSession($request), 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()) {
$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->usingCookieSessions()) {
$this->manager->driver()->save();
}
if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) {
$response->headers->setCookie(new Cookie(
$session->getName(), $session->getId(), $this->getCookieExpirationDate(),
$config['path'], $config['domain'], Arr::get($config, 'secure', false),
Arr::get($config, 'http_only', true)
));
}
}
/**
* Get the session lifetime in seconds.
*
* @return int
*/
protected function getSessionLifetimeInSeconds()
{
return Arr::get($this->manager->getSessionConfig(), 'lifetime') * 60;
}
/**
* Get the cookie lifetime in seconds.
*
* @return \DateTimeInterface
*/
protected function getCookieExpirationDate()
{
$config = $this->manager->getSessionConfig();
return $config['expire_on_close'] ? 0 : Carbon::now()->addMinutes($config['lifetime']);
}
/**
* Determine if a session driver has been configured.
*
* @return bool
*/
protected function sessionConfigured()
{
return ! is_null(Arr::get($this->manager->getSessionConfig(), 'driver'));
}
/**
* 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 ! in_array($config['driver'], [null, 'array']);
}
/**
* Determine if the session is using cookie sessions.
*
* @return bool
*/
protected function usingCookieSessions()
{
if ($this->sessionConfigured()) {
return $this->manager->driver()->getHandler() instanceof CookieSessionHandler;
}
return false;
}
}

View File

@@ -0,0 +1,216 @@
<?php
namespace Illuminate\Session;
use Illuminate\Support\Manager;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
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 "array" session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createArrayDriver()
{
return $this->buildSession(new NullSessionHandler);
}
/**
* Create an instance of the "cookie" session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createCookieDriver()
{
return $this->buildSession(new CookieSessionHandler(
$this->app['cookie'], $this->app['config']['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->app['config']['session.lifetime'];
return $this->buildSession(new FileSessionHandler(
$this->app['files'], $this->app['config']['session.files'], $lifetime
));
}
/**
* Create an instance of the database session driver.
*
* @return \Illuminate\Session\Store
*/
protected function createDatabaseDriver()
{
$table = $this->app['config']['session.table'];
$lifetime = $this->app['config']['session.lifetime'];
return $this->buildSession(new DatabaseSessionHandler(
$this->getDatabaseConnection(), $table, $lifetime, $this->app
));
}
/**
* Get the database connection for the database driver.
*
* @return \Illuminate\Database\Connection
*/
protected function getDatabaseConnection()
{
$connection = $this->app['config']['session.connection'];
return $this->app['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->app['config']['session.connection']
);
return $this->buildSession($handler);
}
/**
* 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->app['config']->get('session.store') ?: $driver;
return new CacheBasedSessionHandler(
clone $this->app['cache']->store($store),
$this->app['config']['session.lifetime']
);
}
/**
* Build the session instance.
*
* @param \SessionHandlerInterface $handler
* @return \Illuminate\Session\Store
*/
protected function buildSession($handler)
{
if ($this->app['config']['session.encrypt']) {
return $this->buildEncryptedSession($handler);
} else {
return new Store($this->app['config']['session.cookie'], $handler);
}
}
/**
* Build the encrypted session instance.
*
* @param \SessionHandlerInterface $handler
* @return \Illuminate\Session\EncryptedStore
*/
protected function buildEncryptedSession($handler)
{
return new EncryptedStore(
$this->app['config']['session.cookie'], $handler, $this->app['encrypter']
);
}
/**
* Get the session configuration.
*
* @return array
*/
public function getSessionConfig()
{
return $this->app['config']['session'];
}
/**
* Get the default session driver name.
*
* @return string
*/
public function getDefaultDriver()
{
return $this->app['config']['session.driver'];
}
/**
* Set the default session driver name.
*
* @param string $name
* @return void
*/
public function setDefaultDriver($name)
{
$this->app['config']['session.driver'] = $name;
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Illuminate\Session;
use Illuminate\Support\ServiceProvider;
use Illuminate\Session\Middleware\StartSession;
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerSessionManager();
$this->registerSessionDriver();
$this->app->singleton(StartSession::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,656 @@
<?php
namespace Illuminate\Session;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use SessionHandlerInterface;
use Illuminate\Contracts\Session\Session;
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 bool
*/
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;
}
/**
* Checks if a key exists.
*
* @param string|array $key
* @return bool
*/
public function exists($key)
{
return ! collect(is_array($key) ? $key : func_get_args())->contains(function ($key) {
return ! Arr::exists($this->attributes, $key);
});
}
/**
* 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 string $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 $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 $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($key, $value)
{
$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 $this->migrate($destroy);
}
/**
* 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": ">=5.6.4",
"illuminate/contracts": "5.4.*",
"illuminate/filesystem": "5.4.*",
"illuminate/support": "5.4.*",
"nesbot/carbon": "~1.20",
"symfony/finder": "~3.2",
"symfony/http-foundation": "~3.2"
},
"autoload": {
"psr-4": {
"Illuminate\\Session\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
}
},
"suggest": {
"illuminate/console": "Required to use the session:table command (5.4.*)."
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}