1 |
<? |
2 |
// ----------------------------------------------------------------------------- |
3 |
// $Id: User.php,v 1.2 2002/12/03 16:47:22 joko Exp $ |
4 |
// ----------------------------------------------------------------------------- |
5 |
// $Log: User.php,v $ |
6 |
// Revision 1.2 2002/12/03 16:47:22 joko |
7 |
// - function preLogout() |
8 |
// + function preLogout($autologout = 0) |
9 |
// + function hadTimeout() |
10 |
// |
11 |
// Revision 1.1 2002/11/12 05:42:31 joko |
12 |
// + initial checkin |
13 |
// |
14 |
// Revision 1.7 2002/10/29 19:09:53 cvsjoko |
15 |
// + function getLastLogin |
16 |
// + function getUserId |
17 |
// |
18 |
// Revision 1.6 2002/10/25 11:18:10 cvsmax |
19 |
// - removed old functions ('logout', 'create',...) |
20 |
// + add function preLogout() # save user session and unset (session) state |
21 |
// |
22 |
// Revision 1.5 2002/10/22 09:47:48 cvsmax |
23 |
// + add new |
24 |
// - function create() # create & save new user-object in session |
25 |
// - function _create($attr) # create & save new user-object in backend |
26 |
// |
27 |
// - purged some very old functions |
28 |
// |
29 |
// Revision 1.4 2002/10/17 03:12:17 cvsmax |
30 |
// -(+) cleaned code from old structure |
31 |
// + function getPostVar($fieldname) |
32 |
// |
33 |
// Revision 1.3 2002/10/10 02:41:57 cvsjoko |
34 |
// + fixed typo |
35 |
// |
36 |
// Revision 1.2 2002/10/10 02:34:33 cvsjoko |
37 |
// + new level of data-caching (session and persistant) |
38 |
// + function _save() |
39 |
// + function save() |
40 |
// + handling (storing/caching) of POSTed data |
41 |
// + function getPostVars() |
42 |
// + function setPostVars() |
43 |
// + handy utils |
44 |
// + function doDataReady() cares for ready data to continue working |
45 |
// + function refresh() clears underlying data container and makes data "unready" |
46 |
// |
47 |
// Revision 1.1 2002/10/09 00:40:13 cvsjoko |
48 |
// + new |
49 |
// |
50 |
// |
51 |
// ----------------------------------------------------------------------------- |
52 |
|
53 |
class User { |
54 |
|
55 |
var $state; |
56 |
var $meta; |
57 |
|
58 |
var $pObject; |
59 |
var $data; |
60 |
|
61 |
|
62 |
function User() { |
63 |
|
64 |
// attention: |
65 |
// this code is only run when your session is _really_ fresh |
66 |
if (session_register_safe('userstate')) { |
67 |
} |
68 |
|
69 |
$this->_loadState(); |
70 |
$this->meta[logontoken] = 'k&%g2'; |
71 |
|
72 |
// print dumpVar($_SESSION); |
73 |
|
74 |
} |
75 |
|
76 |
function _loadState() { |
77 |
global $userstate; |
78 |
$this->state = $userstate; |
79 |
} |
80 |
function _saveState() { |
81 |
global $userstate; |
82 |
$userstate = $this->state; |
83 |
} |
84 |
|
85 |
|
86 |
function _loadData() { |
87 |
$this->pObject = new ProxyObject($this->state[id]); |
88 |
if ($this->state[id]) { |
89 |
$this->data = $this->pObject->getAttributes(); |
90 |
} |
91 |
$this->meta[data_ready] = 1; |
92 |
} |
93 |
|
94 |
function _saveData($newRawData) { |
95 |
|
96 |
foreach ($newRawData as $key => $val) { |
97 |
//print "$key => $val<br>"; |
98 |
$this->set($key, $val); |
99 |
} |
100 |
|
101 |
$this->_save("cache"); |
102 |
|
103 |
//$this->pObject->flushCache(); // done in "pObject->save" |
104 |
|
105 |
} |
106 |
|
107 |
function _save($type = '') { |
108 |
$this->pObject->save($this->data, $type); |
109 |
$this->meta[data_ready] = 0; |
110 |
} |
111 |
|
112 |
function save() { |
113 |
$this->_doDataReady(); |
114 |
$this->_save("commit"); |
115 |
} |
116 |
|
117 |
function create() { |
118 |
$this->_save(); |
119 |
$attr = $this->pObject->getAttributes(); |
120 |
//print DumpVar($attr); |
121 |
$this->_create($attr); |
122 |
} |
123 |
|
124 |
function _create($result) { |
125 |
//print "saving to backend: " . dumpVar($result) . "<br>"; |
126 |
$response = $this->pObject->backend->send('createUser', array('data' => $result) ); |
127 |
$objectId = $response[oid]; |
128 |
//$this->_init($objectId); |
129 |
} |
130 |
|
131 |
function _getHKR($key) { |
132 |
$attr_arr = split('\.', $key); |
133 |
$attr_hkr = '["' . join('"]["', $attr_arr) . '"]'; |
134 |
return $attr_hkr; |
135 |
} |
136 |
|
137 |
function get($attr) { |
138 |
$this->_doDataReady(); |
139 |
$hkr = $this->_getHKR($attr); |
140 |
$evalstr = 'return $this->data' . $hkr . ';'; |
141 |
$val = eval($evalstr); |
142 |
return $val; |
143 |
} |
144 |
function set($attr, $val) { |
145 |
$this->_doDataReady(); |
146 |
$hkr = $this->_getHKR($attr); |
147 |
$evalstr = '$this->data' . $hkr . ' = \'' . $val . '\';'; |
148 |
eval($evalstr); |
149 |
} |
150 |
|
151 |
function isLoggedOn() { |
152 |
if ($this->state[id] && $this->state[status] == $this->meta[logontoken]) { |
153 |
return 1; |
154 |
} |
155 |
} |
156 |
|
157 |
|
158 |
//===== function userlogin ========================= |
159 |
function login($user,$passwd) { |
160 |
connectdb(); |
161 |
//$sql = "SELECT * FROM td_users WHERE uname='$user'"; |
162 |
$sql = "SELECT user_oid, uname, pass FROM td_users WHERE uname='$user'"; |
163 |
if ($res=send_sql($sql)) { |
164 |
$row = mysql_fetch_array($res,MYSQL_ASSOC); |
165 |
if (!is_array($row)) { return 0; } |
166 |
} |
167 |
if ($row[pass] == $passwd) { |
168 |
//return $uservars_db; |
169 |
// store 1st priority user-data to state |
170 |
$this->state[id] = $row[user_oid]; |
171 |
$this->state[status] = $this->meta[logontoken]; |
172 |
$this->_saveState(); |
173 |
$this->site->log( get_class($this) . "->login ok", LOG_DEBUG ); |
174 |
return 1; |
175 |
} else { |
176 |
return 0; |
177 |
} |
178 |
} |
179 |
|
180 |
function exists($string) { |
181 |
connectdb(); |
182 |
$sql = "SELECT uname FROM td_users WHERE uname='$string'"; |
183 |
if ($res=send_sql($sql)) { |
184 |
$row = mysql_fetch_array($res,MYSQL_ASSOC); |
185 |
|
186 |
if (is_array($row)) { |
187 |
return 1; |
188 |
} |
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
|
194 |
function update(&$uservars) { |
195 |
$this->setPostVars($uservars); |
196 |
$data = $this->_transformInputData($uservars); |
197 |
$this->_saveData($data); |
198 |
} |
199 |
|
200 |
function _transformInputData(&$uservars) { |
201 |
global $def_mapping; |
202 |
$mapping_uservars2data = array_flip($def_mapping[data2uservars]); |
203 |
foreach ($uservars as $key => $val) { |
204 |
$key_target = $mapping_uservars2data[$key]; |
205 |
//print "$key => $key_target: $val<br>"; |
206 |
$target[$key_target] = $val; |
207 |
} |
208 |
return $target; |
209 |
} |
210 |
|
211 |
function logout() { |
212 |
|
213 |
global $site; |
214 |
|
215 |
// get information about user from site |
216 |
$userid = $this->state[id]; |
217 |
|
218 |
$this->site->log( get_class($this) . "->logout: userid $userid", LOG_DEBUG ); |
219 |
|
220 |
// get information about session from php |
221 |
$session_id = session_id(); |
222 |
$session_name = session_name(); |
223 |
|
224 |
// session-sqldb related |
225 |
$date = date('Y-m-d H:i:s', time()); |
226 |
$session_e = $site->session->exists($session_id); |
227 |
$sql = "UPDATE f_map_user_session SET date_logged_out='$date' WHERE session_uid='$session_e[session_uid]' AND user_uid='$userid'"; |
228 |
$res = send_sql($sql); |
229 |
|
230 |
// destroy user's session at server-side |
231 |
$session_name = session_name(); |
232 |
session_destroy(); |
233 |
|
234 |
// overwrite session-variables - to be sure ;) |
235 |
$_SESSION = array(); |
236 |
unset($_COOKIE[$session_name]); |
237 |
|
238 |
} |
239 |
|
240 |
function preLogout($autologout = 0) { |
241 |
global $site; |
242 |
$this->state[status] = ''; |
243 |
$this->state[autologout] = $autologout; |
244 |
$this->_saveState(); |
245 |
} |
246 |
|
247 |
function getAccountSum() { |
248 |
// store additional user-data to state |
249 |
if (!$this->state[accountSum]) { |
250 |
$this->state[accountSum] = $this->get("account.amount"); |
251 |
$this->_saveState(); |
252 |
} |
253 |
return $this->state[accountSum]; |
254 |
} |
255 |
|
256 |
function getAccountCurrency() { |
257 |
// store additional user-data to state |
258 |
if (!$this->state[accountCurrency]) { |
259 |
$this->state[accountCurrency] = $this->get("account.currency"); |
260 |
$this->_saveState(); |
261 |
} |
262 |
//print "state: " . $this->get("financeInfo.currency") . "<br>"; |
263 |
return $this->state[accountCurrency]; |
264 |
} |
265 |
|
266 |
function refresh() { |
267 |
if ($this->pObject) {$this->pObject->flushState();} |
268 |
$this->meta[data_ready] = 0; |
269 |
} |
270 |
|
271 |
function _doDataReady() { |
272 |
if (!$this->meta[data_ready]) { $this->_loadData(); } |
273 |
} |
274 |
|
275 |
function getLastLogin() { |
276 |
$userid = $this->getUserId(); |
277 |
connectdb(); |
278 |
$sql = "SELECT * FROM f_map_user_session WHERE user_uid='$userid' AND last='1'"; |
279 |
$res = send_sql($sql); |
280 |
while($row = mysql_fetch_array($res,MYSQL_BOTH)) { |
281 |
if($row[date_logged_out]=="0000-00-00 00:00:00") { |
282 |
$date = $row[date_logged_in]; |
283 |
} else { |
284 |
$date = $row[date_logged_out]; |
285 |
} |
286 |
$lastLogin = strftime("%d %b %Y - %H:%M",strtotime($date)); |
287 |
} |
288 |
return $lastLogin; |
289 |
} |
290 |
|
291 |
function getUserId() { |
292 |
return $this->state[id]; |
293 |
} |
294 |
|
295 |
|
296 |
function getPostVars() { |
297 |
return $this->state[postvars]; |
298 |
} |
299 |
|
300 |
function setPostVars($postvars) { |
301 |
$this->state[postvars] = $postvars; |
302 |
$this->_saveState(); |
303 |
} |
304 |
|
305 |
function getPostVar($var) { |
306 |
return $this->state[postvars][$var]; |
307 |
} |
308 |
|
309 |
function hadTimeout() { |
310 |
if ($this->state[autologout]) { return 1; } |
311 |
} |
312 |
|
313 |
} |
314 |
|
315 |
?> |