Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
MultisiteSession.php
Go to the documentation of this file.
1<?php
2
4
8
19 private string $folder;
20 private string $id;
21 const SESSION_ID = 'multi_session_id';
22
23 private function getKey($key): string {
24 return \md5 ( $key );
25 }
26
27 private function getFilename($key): string {
28 return $this->folder . $this->id . \DS . $this->getKey ( $key ) . '.cache.ser';
29 }
30
31 protected function generateId(): string {
32 return \bin2hex ( \random_bytes ( 32 ) );
33 }
34
35 public function set(string $key, $value) {
36 $val = \serialize ( $value );
37 $tmp = "/tmp/$key." . \uniqid ( '', true ) . '.tmp';
38 \file_put_contents ( $tmp, $val, LOCK_EX );
39 \rename ( $tmp, $this->getFilename ( $key ) );
40 }
41
42 public function getAll(): array {
43 $files = UFileSystem::glob_recursive ( $this->folder . \DS . '*' );
44 $result = [ ];
45 foreach ( $files as $file ) {
46 $result [] = include $file;
47 }
48 return $result;
49 }
50
51 public function get(string $key, $default = null) {
52 $filename = $this->getFilename ( $key );
53 if (\file_exists ( $filename )) {
54 $f = \file_get_contents ( $filename );
55 $val = \unserialize ( $f );
56 }
57 return isset ( $val ) ? $val : $default;
58 }
59
60 public function start(string $name = null, $root = null) {
61 $this->name = $name;
62 if (! isset ( $root )) {
63 $this->folder = \ROOT . \DS . CacheManager::getCacheDirectory () . \DS . 'session' . \DS;
64 } else {
65 $this->folder = $root . \DS . 'session' . \DS;
66 }
67 if (isset ( $name )) {
68 $this->folder .= $name . \DS;
69 }
70 if (! UCookie::exists ( self::SESSION_ID )) {
71 $id = $this->generateId ();
72 UCookie::set ( self::SESSION_ID, $id );
73 $this->id = $id;
74 } else {
75 $this->id = UCookie::get ( self::SESSION_ID );
76 }
77 $this->verifyCsrf->start ();
78 UFileSystem::safeMkdir ( $this->folder . $this->id . \DS );
79 }
80
81 public function exists($key): bool {
82 return file_exists ( $this->getFilename ( $key ) );
83 }
84
85 public function terminate(): void {
86 $this->verifyCsrf->clear ();
87 UFileSystem::delTree ( $this->folder . $this->id . \DS );
88 }
89
90 public function isStarted(): bool {
91 return isset ( $this->id );
92 }
93
94 public function delete($key) {
95 \unlink ( $this->getFilename ( $key ) );
96 }
97
98 public function regenerateId(bool $deleteOldSession=false): bool {
99 if($deleteOldSession){
100 $this->terminate();
101 $this->start($this->name,\rtrim($this->folder,\DS . 'session' . \DS));
102 return true;
103 }
104 $newId=$this->generateId();
105 $this->verifyCsrf->clear();
106 UCookie::set ( self::SESSION_ID, $newId );
107 $oldId=$this->id;
108 $this->id = $newId;
109 $this->verifyCsrf->start ();
110 return UFileSystem::xmove($this->folder . $oldId . \DS, $this->folder . $newId . \DS);
111 }
112
113 public function visitorCount(): int {
114 return \count ( \scandir ( $this->folder . $this->id . \DS ) );
115 }
116}
Manager for caches (Router, Rest, models).
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Ubiquity\utils\http\session$AbstractSession This class is part of Ubiquity.
regenerateId(bool $deleteOldSession=false)
Re-generates the session id.