commit | author | age
|
8bd4d9
|
1 |
<?php |
JK |
2 |
/** |
|
3 |
* Klasa przechowująca dane użytkownika. Całość przypomina mechanizm sesji w PHP. |
|
4 |
*/ |
|
5 |
class BotSession { |
|
6 |
private $PDO; |
|
7 |
|
|
8 |
/** |
|
9 |
* Nazwa modułu, którego zmienne klasa przetwarza |
|
10 |
* @var string max. 40 znaków |
|
11 |
*/ |
|
12 |
var $class; |
|
13 |
|
|
14 |
private $user; |
|
15 |
|
|
16 |
|
|
17 |
/** |
|
18 |
* Inicjuje klasę w zależności od użytkownika |
|
19 |
*/ |
|
20 |
function __construct($user) { |
|
21 |
$this->user = sha1($user); |
|
22 |
$this->user_struct = parse_url($user); |
|
23 |
|
|
24 |
$this->class = ''; |
|
25 |
} |
|
26 |
|
|
27 |
private function init() { |
|
28 |
if($this->PDO) { |
|
29 |
return NULL; |
|
30 |
} |
|
31 |
|
|
32 |
if(is_file(BOT_TOPDIR.'/database/'.sha1($this->user).'.sqlite')) { |
|
33 |
$this->PDO = new PDO('sqlite:'.BOT_TOPDIR.'/database/'.sha1($this->user).'.sqlite'); |
|
34 |
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
35 |
$this->PDO->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING); |
|
36 |
return; |
|
37 |
} |
|
38 |
|
|
39 |
try { |
|
40 |
$this->PDO = new PDO('sqlite:'.BOT_TOPDIR.'/database/'.sha1($this->user).'.sqlite'); |
|
41 |
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
42 |
$this->PDO->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING); |
|
43 |
|
|
44 |
$this->PDO->query( |
|
45 |
'CREATE TABLE data ( |
|
46 |
class VARCHAR(50), |
|
47 |
name VARCHAR(40) NOT NULL, |
|
48 |
value TEXT NOT NULL, |
|
49 |
PRIMARY KEY ( |
|
50 |
class ASC, |
|
51 |
name ASC |
|
52 |
) |
|
53 |
)' |
|
54 |
); |
|
55 |
|
|
56 |
$files = glob(BOT_TOPDIR.'/db/*/'.$this->user_struct['user'].'.ggdb'); |
c661ee
|
57 |
if(!$files) { |
JK |
58 |
return; |
|
59 |
} |
8bd4d9
|
60 |
|
JK |
61 |
$this->PDO->beginTransaction(); |
|
62 |
$st = $this->PDO->prepare('INSERT OR REPLACE INTO data (class, name, value) VALUES (?, ?, ?)'); |
|
63 |
|
|
64 |
foreach($files as $file) { |
|
65 |
$data = unserialize(file_get_contents($file)); |
|
66 |
foreach($data as $name => $value) { |
|
67 |
$st->execute(array($this->class, $name, $value)); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 |
$this->PDO->commit(); |
|
72 |
|
|
73 |
foreach($files as $file) { |
|
74 |
unlink($file); |
|
75 |
} |
|
76 |
} |
|
77 |
catch(Exception $e) { |
d22cb4
|
78 |
if(file_exists(BOT_TOPDIR.'/database/'.sha1($this->user).'.sqlite')) { |
JK |
79 |
@unlink(BOT_TOPDIR.'/database/'.sha1($this->user).'.sqlite'); |
|
80 |
} |
8bd4d9
|
81 |
throw $e; |
JK |
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
function __get($name) { |
|
86 |
$this->init(); |
|
87 |
|
|
88 |
$st = $this->PDO->prepare('SELECT value FROM data WHERE class=? AND name=?'); |
|
89 |
$st->execute(array($this->class, $name)); |
|
90 |
$st = $st->fetch(PDO::FETCH_ASSOC); |
|
91 |
|
|
92 |
if(is_array($st)) { |
|
93 |
return unserialize($st['value']); |
|
94 |
} |
|
95 |
else |
|
96 |
{ |
|
97 |
return NULL; |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
function __set($name, $value) { |
|
102 |
$this->init(); |
|
103 |
|
|
104 |
$st = $this->PDO->prepare('INSERT OR REPLACE INTO data (class, name, value) VALUES (?, ?, ?)'); |
|
105 |
$st->execute(array($this->class, $name, serialize($value))); |
|
106 |
} |
|
107 |
|
|
108 |
function __isset($name) { |
|
109 |
$this->init(); |
|
110 |
|
|
111 |
$st = $this->PDO->prepare('SELECT COUNT(name) FROM data WHERE class=? AND name=?'); |
|
112 |
$st->execute(array($this->class, $name)); |
|
113 |
$st = $st->fetch(PDO::FETCH_NUM); |
|
114 |
|
|
115 |
return ($st[0]>0); |
|
116 |
} |
|
117 |
|
|
118 |
function __unset($name) { |
|
119 |
$this->init(); |
|
120 |
|
|
121 |
$st = $this->PDO->prepare('DELETE FROM data WHERE class=? AND name=?'); |
|
122 |
$st->execute(array($this->class, $name)); |
|
123 |
} |
|
124 |
|
|
125 |
function push($array) { |
|
126 |
$this->PDO->beginTransaction(); |
|
127 |
foreach($array as $name => $value) { |
|
128 |
$this->__set($name, $value); |
|
129 |
} |
|
130 |
$this->PDO->commit(); |
|
131 |
} |
|
132 |
|
|
133 |
function pull() { |
|
134 |
$this->init(); |
|
135 |
|
|
136 |
$st = $this->PDO->prepare('SELECT name, value FROM data WHERE class=?'); |
|
137 |
$st->execute(array($this->class)); |
|
138 |
$st = $st->fetchAll(PDO::FETCH_ASSOC); |
|
139 |
|
|
140 |
$return = array(); |
|
141 |
foreach($st as $row) { |
|
142 |
$return[$row['name']] = $row['value']; |
|
143 |
} |
|
144 |
|
|
145 |
return $return; |
|
146 |
} |
|
147 |
|
|
148 |
function truncate() { |
|
149 |
$this->init(); |
|
150 |
|
|
151 |
$st = $this->PDO->prepare('DELETE FROM data WHERE class=?'); |
|
152 |
$st->execute(array($this->class)); |
|
153 |
} |
|
154 |
} |
79d1cc
|
155 |
?> |