LEPTON CMS 7.2.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
lepton_session.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21{
23
24 public static $instance;
25
26 // Default constructor of the class
27 final public function __construct()
28 {
29 // return the class instance
30 //return self::getInstance();
31 }
32
49 public static function set_cookie (
50 string $name,
51 string|int $value,
52 array $options = [],
53 bool $mustExists = false,
54 bool $mergeDefault = true
55 ): bool
56 {
57 // check if name exists in $_COOKIE
58 if ((true === $mustExists ) && (false === isset($_COOKIE[ $name ])))
59 {
60 return false;
61 }
62
63 // clear options, remove empty settings
64 $temp = $options;
65 $options = [];
66 foreach( $temp as $optkey => $optvalue )
67 {
68 if ($optvalue != "")
69 {
70 $options[$optkey] = $optvalue;
71 }
72 }
73
74 // merge options array with defaults
75 if (true === $mergeDefault )
76 {
77 $defaults = self::get_cookie_defaults();
78 $options = array_merge($defaults, $options);
79 }
80
81 // Update cookie settings, see: https://www.php.net/manual/de/function.setcookie.php
82 $cookie = setcookie($name, $value, $options);
83
84 return $cookie;
85 }
86
96 public static function deleteCookieSession(string $name, string|int $value = "", array $options = []): bool
97 {
98 // check if name exists in $_COOKIE
99 if (false === isset($_COOKIE[$name]))
100 {
101 return false; // not exists means also cookie not exists
102 }
103
104 // overwrite session array
105 $_SESSION = [];
106
107 // set/overwrite expire option
108 $options["expires"] = 1; // 01.01.1970 00:00:01
109
110 // delete session cookie if set
111 $returnVal = self::set_cookie($name, $value, $options);
112
113 // overwrite session array again
114 $_SESSION = [];
115
116 // delete the session itself
117 session_destroy();
118
119 return $returnVal;
120 }
121
126 public static function get_cookie_defaults(): array
127 {
128 $iniValues = self::getConfigValuesFromIni();
129
130 // return the defaults
131 return [
137 "expires" => time() + ($iniValues['lifetime'] ?? (3 * 3600)), // three hours
138
142 "path" => "/",
143
149 "domain" => "",
150
155 "secure" => (isset($_SERVER['HTTPS'])
156 ? (strtolower( $_SERVER['HTTPS'] ) == "on") // bool
157 : false
158 ),
159
166 "httponly" => $iniValues['httponly'] ?? true,
167
177 "samesite" => $iniValues['samesite'] ?? "Lax"
178 ];
179 }
180
181
182 public static function getConfigValuesFromIni(): array
183 {
184 $vals = [
185 'samesite' => ['type' => 'string', 'default' => 'Lax', 'range' => ['none', 'lax', 'strict']],
186 'httponly' => ['type' => 'bool', 'default' => true],
187 'lifetime' => ['type' => 'integer_eval', 'default' => (3*3600)],
188 'domain' => ['type' => 'string', 'default' => '/']
189 ];
190 $returnValues = [];
191 $ini_file_name = LEPTON_PATH . "/config/lepton.ini.php";
192 if (true === file_exists($ini_file_name))
193 {
194 $config = parse_ini_string(";" . file_get_contents($ini_file_name), true);
195 if (isset($config['lepton_cookie']))
196 {
197 foreach($config['lepton_cookie'] as $key => $value)
198 {
199 $returnValues[$key] = $value;
200 }
201 }
202 }
203
204 $oREQUEST = LEPTON_request::getInstance();
205
206 foreach ($returnValues as $key => &$val)
207 {
208 if (isset($vals[$key]))
209 {
210 $val = $oREQUEST->filterValue(
211 $val,
212 $vals[$key]['type'],
213 $vals[$key]['default'],
214 $vals[$key]['range'] ?? ''
215 );
216 }
217 }
218 return $returnValues;
219 }
220}
static getConfigValuesFromIni()
static deleteCookieSession(string $name, string|int $value="", array $options=[])
static get_cookie_defaults()
static set_cookie(string $name, string|int $value, array $options=[], bool $mustExists=false, bool $mergeDefault=true)
trait LEPTON_singleton