Server : LiteSpeed System : Linux server 3.10.0-1160.90.1.el7.x86_64 #1 SMP Thu May 4 15:21:22 UTC 2023 x86_64 User : alsaif ( 1057) PHP Version : 7.4.33 Disable Function : show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/alsaif/public_html/plugins/system/nrframework/NRFramework/ |
<?php
/**
* @author Tassos Marinos <info@tassos.gr>
* @link https://www.tassos.gr
* @copyright Copyright © 2024 Tassos All Rights Reserved
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/
namespace NRFramework;
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Crypt\Crypt;
class VisitorToken
{
/**
* Class instance
*
* @var object
*/
private static $instance;
/**
* Cookie Name
*
* @var string
*/
private $cookieName = "nrid";
/**
* Represents the maximum age of the visitor's cookie in seconds.
*
* @var Integer
*/
private $expire = 90000000;
/**
* Cookies Object
*
* @var object
*/
private $cookies;
/**
* Class constructor
*/
private function __construct()
{
$this->cookies = Factory::getApplication()->input->cookie;
$token = $this->cookies->get($this->cookieName, null);
if ($token === null)
{
$this->store($this->create());
}
}
/**
* Returns class instance
*
* @return object
*/
public static function getInstance()
{
if (is_null(self::$instance))
{
self::$instance = new self();
}
return self::$instance;
}
/**
* Get a visitor's unique token id, if a token isn't set yet one will be generated.
*
* @param boolean $forceNew If true, force a new token to be created
*
* @return string The session token
*/
public function get($forceNew = false)
{
return $this->cookies->get($this->cookieName);
}
/**
* Create a token-string
*
* @param integer $length Length of string
*
* @return string Generated token
*/
private function create($length = 8)
{
return bin2hex(Crypt::genRandomBytes($length));
}
/**
* Saves the cookie to the visitor's browser
*
* @param string $value Cookie Value
*
* @return void
*/
private function store($value)
{
$this->cookies->set($this->cookieName, $value, time() + $this->expire, '/', '', true);
}
}