pot/author/project/system/models/session.php

234 lines
7.2 KiB
PHP
Raw Normal View History

2024-01-11 04:35:40 +07:00
<?php
declare(strict_types=1);
2024-01-11 05:38:30 +07:00
namespace ${REPO_OWNER}\${REPO_NAME}\models;
2024-01-11 04:35:40 +07:00
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\models\traits\status,
${REPO_OWNER}\${REPO_NAME}\models\traits\buffer,
${REPO_OWNER}\${REPO_NAME}\models\traits\document as document_trait,
${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface,
${REPO_OWNER}\${REPO_NAME}\models\interfaces\collection as collection_interface,
${REPO_OWNER}\${REPO_NAME}\models\enumerations\session as verification,
${REPO_OWNER}\${REPO_NAME}\models\enumerations\language;
2024-01-11 04:35:40 +07:00
// Framework for ArangoDB
use mirzaev\arangodb\collection,
mirzaev\arangodb\document;
// Library для ArangoDB
use ArangoDBClient\Document as _document;
// Built-in libraries
use exception;
/**
* Session model
2024-01-11 04:35:40 +07:00
*
* @package ${REPO_OWNER}\${REPO_NAME}\models
*
* @param string COLLECTION Name of the collection in ArangoDB
* @param verification VERIFICATION Type of session verification
*
2024-12-15 22:16:01 +07:00
* @method void __construct(?string $$hash, ?int $$expires, array &$$errors) Constructor
* @method document|null hash(string $$hash, array &$$errors) Search by hash
* @method document|null address(string $$address, array &$$errors) Search by IP-address
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author ${REPO_OWNER} <mail@domain.zone>
2024-01-11 04:35:40 +07:00
*/
final class session extends core implements document_interface, collection_interface
2024-01-11 04:35:40 +07:00
{
use status, document_trait, buffer, cart {
buffer::write as write;
cart::initialize as cart;
}
2024-01-11 04:35:40 +07:00
/**
* Collection name
*
* @var string COLLECTION Name of the collection in ArangoDB
2024-01-11 04:35:40 +07:00
*/
final public const string COLLECTION = 'session';
2024-01-11 04:35:40 +07:00
/**
* Session verification type
*
* @var verification VERIFICATION Type of session verification
2024-01-11 04:35:40 +07:00
*/
final public const verification VERIFICATION = verification::hash_else_address;
2024-01-11 04:35:40 +07:00
/**
* Constructor
2024-01-11 04:35:40 +07:00
*
2024-12-15 22:16:01 +07:00
* Initialize session and write into the $$this->document property
2024-01-11 04:35:40 +07:00
*
2024-12-15 22:16:01 +07:00
* @param ?string $$hash Hash of the session in ArangoDB
* @param ?int $$expires Date of expiring of the session (used for creating a new session)
* @param array &$$errors Registry of errors
2024-01-11 04:35:40 +07:00
*
* @return void
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
public function __construct(?string $$hash = null, ?int $$expires = null, array &$$errors = [])
2024-01-11 04:35:40 +07:00
{
try {
2024-12-15 22:16:01 +07:00
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $$errors)) {
2024-01-11 04:35:40 +07:00
// Initialized the collection
2024-12-15 22:16:01 +07:00
if (isset($$hash) && $$document = $$this->hash($$hash, errors: $$errors)) {
// Found the instance of the ArangoDB document of session and received a session hash
// Writing document instance of the session from ArangoDB to the property of the implementing object
2024-12-15 22:16:01 +07:00
$$this->__document($$document);
} else if (static::VERIFICATION === verification::hash_else_address && $$document = $$this->address($$_SERVER['REMOTE_ADDR'], errors: $$errors)) {
// Found the instance of the ArangoDB document of session and received a session hash
// Writing document instance of the session from ArangoDB to the property of the implementing object
2024-12-15 22:16:01 +07:00
$$this->__document($$document);
2024-01-11 04:35:40 +07:00
} else {
// Not found the instance of the ArangoDB document of session
2024-01-11 04:35:40 +07:00
// Initializing a new session and write they into ArangoDB
2024-12-15 22:16:01 +07:00
$$_id = document::write(
static::COLLECTION,
[
'active' => true,
2024-12-15 22:16:01 +07:00
'expires' => $$expires ?? time() + 604800,
'address' => $$_SERVER['REMOTE_ADDR'],
'x-forwarded-for' => $$_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
'referer' => $$_SERVER['HTTP_REFERER'] ?? null,
'useragent' => $$_SERVER['HTTP_USER_AGENT'] ?? null
]
);
2024-12-15 22:16:01 +07:00
if ($$session = collection::execute(
<<<'AQL'
FOR d IN @@collection
FILTER d._id == @_id && d.expires > @time && d.active == true
2024-01-11 04:35:40 +07:00
RETURN d
AQL,
[
'@collection' => static::COLLECTION,
2024-12-15 22:16:01 +07:00
'_id' => $$_id,
'time' => time()
],
2024-12-15 22:16:01 +07:00
errors: $$errors
)) {
// Found the instance of just created new session
// Generating a hash and write into the instance of the ArangoDB document of session property
2024-12-15 22:16:01 +07:00
$$session->hash = sodium_bin2hex(sodium_crypto_generichash($$_id));
2024-01-11 04:35:40 +07:00
2024-12-15 22:16:01 +07:00
if (document::update($$session, errors: $$errors)) {
// Writed to ArangoDB
2024-01-11 04:35:40 +07:00
// Writing instance of the session document from ArangoDB to the property of the implementing object
2024-12-15 22:16:01 +07:00
$$this->__document($$session);
} else throw new exception('Failed to write the session data');
} else throw new exception('Failed to create or find just created session');
2024-01-11 04:35:40 +07:00
}
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
2024-12-15 22:16:01 +07:00
} catch (exception $$e) {
// Writing to the registry of errors
2024-12-15 22:16:01 +07:00
$$errors[] = [
'text' => $$e->getMessage(),
'file' => $$e->getFile(),
'line' => $$e->getLine(),
'stack' => $$e->getTrace()
2024-01-11 04:35:40 +07:00
];
}
}
/**
* Search by hash
2024-01-11 04:35:40 +07:00
*
* Search for the session in ArangoDB by hash
2024-01-11 04:35:40 +07:00
*
2024-12-15 22:16:01 +07:00
* @param string $$hash Hash of the session in ArangoDB
* @param array &$$errors Registry of errors
2024-01-11 04:35:40 +07:00
*
* @return _document|null instance of document of the session in ArangoDB
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
public static function hash(string $$hash, array &$$errors = []): ?_document
2024-01-11 04:35:40 +07:00
{
try {
2024-12-15 22:16:01 +07:00
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $$errors)) {
// Collection initialized
2024-01-11 04:35:40 +07:00
// Search the session data in ArangoDB
return collection::execute(
<<<'AQL'
FOR d IN @@collection
FILTER d.hash == @hash && d.expires > @time && d.active == true
2024-01-11 04:35:40 +07:00
RETURN d
AQL,
[
'@collection' => static::COLLECTION,
2024-12-15 22:16:01 +07:00
'hash' => $$hash,
'time' => time()
],
2024-12-15 22:16:01 +07:00
errors: $$errors
);
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
2024-12-15 22:16:01 +07:00
} catch (exception $$e) {
// Writing to the registry of errors
2024-12-15 22:16:01 +07:00
$$errors[] = [
'text' => $$e->getMessage(),
'file' => $$e->getFile(),
'line' => $$e->getLine(),
'stack' => $$e->getTrace()
2024-01-11 04:35:40 +07:00
];
}
// Exit (fail)
return null;
2024-01-11 04:35:40 +07:00
}
/**
* Search by IP-address
2024-01-11 04:35:40 +07:00
*
* Search for the session in ArangoDB by IP-address
*
2024-12-15 22:16:01 +07:00
* @param string $$address IP-address writed to the session in ArangoDB
* @param array &$$errors Registry of errors
2024-01-11 04:35:40 +07:00
*
* @return _document|null instance of document of the session in ArangoDB
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
public static function address(string $$address, array &$$errors = []): ?_document
2024-01-11 04:35:40 +07:00
{
try {
2024-12-15 22:16:01 +07:00
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $$errors)) {
// Collection initialized
2024-01-11 04:35:40 +07:00
// Search the session data in ArangoDB
return collection::execute(
<<<'AQL'
FOR d IN @@collection
FILTER d.address == @address && d.expires > @time && d.active == true
SORT d.updated DESC
LIMIT 1
RETURN d
AQL,
[
'@collection' => static::COLLECTION,
2024-12-15 22:16:01 +07:00
'address' => $$address,
'time' => time()
],
2024-12-15 22:16:01 +07:00
errors: $$errors
2024-01-11 04:35:40 +07:00
);
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
2024-12-15 22:16:01 +07:00
} catch (exception $$e) {
// Writing to the registry of errors
2024-12-15 22:16:01 +07:00
$$errors[] = [
'text' => $$e->getMessage(),
'file' => $$e->getFile(),
'line' => $$e->getLine(),
'stack' => $$e->getTrace()
2024-01-11 04:35:40 +07:00
];
}
// Exit (fail)
return null;
}}