1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
<?php
/** Class ki je nadomestil session pri nastavitvah userja
* Januar 2013
*
*
*
* @author Peter Hrvatin
*
*/
class SurveyUserSessionDestructor
{
/** pokli�emo destruktor funkcijo
*
*/
public function __destruct()
{
SurveyUserSession::destruct();
}
}
class SurveyUserSession
{
private static $destructorInstance;
private static $ank_id = null; // ID ankete
private static $dataArray = array();
static function Init($anketa = null)
{
global $global_user_id;
if (null === self::$destructorInstance)
self::$destructorInstance = new SurveyUserSessionDestructor();
if ($anketa == null || (int)$anketa == 0 || !is_numeric($anketa)) {
throw new Exception('Survey ID is mandatory for SurveyUserSession!');
}
self::$ank_id = $anketa;
# preberemo vse nastavitve za to anketo - ce jih se nimamo
if (!is_countable(self::$dataArray) || count(self::$dataArray) == 0) {
$sql = sisplet_query("SELECT data FROM srv_user_session WHERE ank_id='" . self::$ank_id . "' AND usr_id='$global_user_id'");
$row = mysqli_fetch_array($sql);
self::$dataArray = unserialize($row['data']);
}
}
static function getData($what = null)
{
if (self::$ank_id == null) {
throw new Exception('Survey ID is mandatory for SurveyUserSession!');
return null;
}
if ($what == null) {
return self::$dataArray;
} else if (isset(self::$dataArray[$what])) {
return self::$dataArray[$what];
}
return null;
}
public static function saveData($value, $what = null)
{
global $global_user_id;
if (self::$ank_id == null) {
throw new Exception('Survey ID is mandatory for SurveyUserSession!');
return null;
}
// Ce smo uporabili vse podatke
if ($what == null) {
self::$dataArray = $value;
// Brez tega break nekje ne dela - TODO
$dataString = mysqli_real_escape_string($GLOBALS['connect_db'], serialize(self::$dataArray));
$sql = sisplet_query("INSERT INTO srv_user_session (ank_id, usr_id, data) VALUES ('" . self::$ank_id . "', '$global_user_id', '$dataString') ON DUPLICATE KEY UPDATE data='$dataString'");
return true;
} // Ce smo uporabljali podatke samo za posamezno podstran
else {
if (!is_string($what)) {
throw new Exception('Variable \'what\' must be string!');
return null;
} else {
self::$dataArray[$what] = $value;
return true;
}
}
}
/**
* Izbriši seje za dotičnega uporabnika
*/
public static function delete()
{
global $global_user_id;
if (self::$ank_id == null) {
throw new Exception('Survey ID is mandatory for SurveyUserSession!');
return null;
}
sisplet_query("DELETE FROM srv_user_session WHERE ank_id = '" . self::$ank_id . "' AND usr_id='" . $global_user_id . "'");
}
/** ob ukinitvi klassa shranimo vse vrednosti
*
*/
static public function destruct()
{
global $global_user_id;
global $connect_db;
if (self::$ank_id == null) {
throw new Exception('Survey ID is mandatory for SurveyUserSession!');
return null;
}
// v destructu več nimamo connect_db resoursa
if ($connect_db == null) {
// poizkusimo še 1x
global $mysql_server, $mysql_username, $mysql_password, $mysql_database_name;
if (!$connect_db = mysqli_connect($mysql_server, $mysql_username, $mysql_password, $mysql_database_name)) {
die ('Please try again later [ERR: DB])');
}
}
# pripravimo string za shranjevanje
if (is_countable(self::$dataArray) && count(self::$dataArray) > 0) {
$dataString = mysqli_real_escape_string($GLOBALS['connect_db'], serialize(self::$dataArray));
$sql = sisplet_query("INSERT INTO srv_user_session (ank_id, usr_id, data) VALUES ('" . self::$ank_id . "', '$global_user_id', '$dataString') ON DUPLICATE KEY UPDATE data='$dataString'");
if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
// Dodano ker drugace ne zapise v bazo (zaradi ExclusiveLock classa)
sisplet_query("COMMIT");
}
}
}
?>
|