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'); |
|
57 |
|
|
58 |
$this->PDO->beginTransaction(); |
|
59 |
$st = $this->PDO->prepare('INSERT OR REPLACE INTO data (class, name, value) VALUES (?, ?, ?)'); |
|
60 |
|
|
61 |
foreach($files as $file) { |
|
62 |
$data = unserialize(file_get_contents($file)); |
|
63 |
foreach($data as $name => $value) { |
|
64 |
$st->execute(array($this->class, $name, $value)); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
$this->PDO->commit(); |
|
69 |
|
|
70 |
foreach($files as $file) { |
|
71 |
unlink($file); |
|
72 |
} |
|
73 |
} |
|
74 |
catch(Exception $e) { |
|
75 |
@unlink(BOT_TOPDIR.'/database/'.sha1($this->user).'.sqlite'); |
|
76 |
throw $e; |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
function __get($name) { |
|
81 |
$this->init(); |
|
82 |
|
|
83 |
$st = $this->PDO->prepare('SELECT value FROM data WHERE class=? AND name=?'); |
|
84 |
$st->execute(array($this->class, $name)); |
|
85 |
$st = $st->fetch(PDO::FETCH_ASSOC); |
|
86 |
|
|
87 |
if(is_array($st)) { |
|
88 |
return unserialize($st['value']); |
|
89 |
} |
|
90 |
else |
|
91 |
{ |
|
92 |
return NULL; |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
function __set($name, $value) { |
|
97 |
$this->init(); |
|
98 |
|
|
99 |
$st = $this->PDO->prepare('INSERT OR REPLACE INTO data (class, name, value) VALUES (?, ?, ?)'); |
|
100 |
$st->execute(array($this->class, $name, serialize($value))); |
|
101 |
} |
|
102 |
|
|
103 |
function __isset($name) { |
|
104 |
$this->init(); |
|
105 |
|
|
106 |
$st = $this->PDO->prepare('SELECT COUNT(name) FROM data WHERE class=? AND name=?'); |
|
107 |
$st->execute(array($this->class, $name)); |
|
108 |
$st = $st->fetch(PDO::FETCH_NUM); |
|
109 |
|
|
110 |
return ($st[0]>0); |
|
111 |
} |
|
112 |
|
|
113 |
function __unset($name) { |
|
114 |
$this->init(); |
|
115 |
|
|
116 |
$st = $this->PDO->prepare('DELETE FROM data WHERE class=? AND name=?'); |
|
117 |
$st->execute(array($this->class, $name)); |
|
118 |
} |
|
119 |
|
|
120 |
function push($array) { |
|
121 |
$this->PDO->beginTransaction(); |
|
122 |
foreach($array as $name => $value) { |
|
123 |
$this->__set($name, $value); |
|
124 |
} |
|
125 |
$this->PDO->commit(); |
|
126 |
} |
|
127 |
|
|
128 |
function pull() { |
|
129 |
$this->init(); |
|
130 |
|
|
131 |
$st = $this->PDO->prepare('SELECT name, value FROM data WHERE class=?'); |
|
132 |
$st->execute(array($this->class)); |
|
133 |
$st = $st->fetchAll(PDO::FETCH_ASSOC); |
|
134 |
|
|
135 |
$return = array(); |
|
136 |
foreach($st as $row) { |
|
137 |
$return[$row['name']] = $row['value']; |
|
138 |
} |
|
139 |
|
|
140 |
return $return; |
|
141 |
} |
|
142 |
|
|
143 |
function truncate() { |
|
144 |
$this->init(); |
|
145 |
|
|
146 |
$st = $this->PDO->prepare('DELETE FROM data WHERE class=?'); |
|
147 |
$st->execute(array($this->class)); |
|
148 |
} |
|
149 |
} |
|
150 |
?> |