Test Failed
Push — master ( 6ccab2...d43578 )
by Jean-Christophe
20:52
created

MultisiteSession::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Ubiquity\utils\http\session;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\utils\base\UFileSystem;
7
use Ubiquity\utils\http\UCookie;
8
9
/**
10
 * Multi-sites session.
11
 * Ubiquity\utils\http\session$MultisiteSession
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.4-beta
16
 *
17
 */
18
class MultisiteSession extends AbstractSession {
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
}
117