| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.22 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\FB; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Cookie |
| 20 |
{ |
| 21 |
/** |
| 22 |
* The box instance |
| 23 |
* |
| 24 |
* @var object |
| 25 |
*/ |
| 26 |
private $box; |
| 27 |
|
| 28 |
/** |
| 29 |
* The cookie instance |
| 30 |
* |
| 31 |
* @var object |
| 32 |
*/ |
| 33 |
private $cookie; |
| 34 |
|
| 35 |
/** |
| 36 |
* The value of the cookie |
| 37 |
* |
| 38 |
* @var int |
| 39 |
*/ |
| 40 |
private $cookie_value = 1; |
| 41 |
|
| 42 |
/** |
| 43 |
* The path of the cookie. It should be available in the whole domain. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $cookie_path = '/'; |
| 48 |
|
| 49 |
/** |
| 50 |
* Class constructor |
| 51 |
* |
| 52 |
* @param integer $box_id The box's ID |
| 53 |
*/ |
| 54 |
public function __construct($box) |
| 55 |
{ |
| 56 |
$this->box = $box; |
| 57 |
$this->cookie = new \FPFramework\Libs\Cookie(); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get the name of the cookie |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
private function getName() |
| 66 |
{ |
| 67 |
return isset($this->box->ID) ? 'firebox_' . $this->box->ID : ''; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Store cookie in the browser |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function set() |
| 76 |
{ |
| 77 |
$cookie_type = $this->box->params->get('assign_cookietype', 'days'); |
| 78 |
|
| 79 |
switch ($cookie_type) |
| 80 |
{ |
| 81 |
case 'never': |
| 82 |
return; |
| 83 |
|
| 84 |
case 'ever': |
| 85 |
$expire = strtotime('now +20 years'); // forever |
| 86 |
break; |
| 87 |
|
| 88 |
case 'custom': |
| 89 |
$assign_cookietype_param_custom_period_times = (int) $this->box->params->get('assign_cookietype_param_custom_period_times', 1); |
| 90 |
$assign_cookietype_param_custom_period = $this->box->params->get('assign_cookietype_param_custom_period', 'days'); |
| 91 |
|
| 92 |
$expire = strtotime('now +' . (int) $assign_cookietype_param_custom_period_times . ' ' . $assign_cookietype_param_custom_period); |
| 93 |
break; |
| 94 |
|
| 95 |
default: |
| 96 |
$expire = 0; // session |
| 97 |
} |
| 98 |
|
| 99 |
$this->cookie->set($this->getName(), $this->cookie_value, $expire, $this->cookie_path, '', true); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Check if the cookie of the box exist |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public function exist() |
| 108 |
{ |
| 109 |
return $this->cookie->get($this->getName()); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Removes the cookie from the browser |
| 114 |
* |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function remove() |
| 118 |
{ |
| 119 |
$this->cookie->set($this->getName(), $this->cookie_value, strtotime('-1 day'), $this->cookie_path); |
| 120 |
} |
| 121 |
} |