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