1 |
<? |
2 |
/** |
3 |
* This file contains the Tracker::User module. |
4 |
* |
5 |
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
* @package org.netfrag.flib |
7 |
* @name Tracker::User |
8 |
* |
9 |
*/ |
10 |
|
11 |
|
12 |
// ----------------------------------------------------------------------------- |
13 |
// $Id: User.php,v 1.20 2004/10/07 14:08:51 jonen Exp $ |
14 |
// ----------------------------------------------------------------------------- |
15 |
// $Log: User.php,v $ |
16 |
// Revision 1.20 2004/10/07 14:08:51 jonen |
17 |
// + bufix related to save new-registered user-data |
18 |
// |
19 |
// Revision 1.19 2004/06/22 09:18:47 jonen |
20 |
// removed debug dumps |
21 |
// |
22 |
// Revision 1.17 2004/06/07 16:54:53 joko |
23 |
// modified cache-key |
24 |
// added cache-control |
25 |
// |
26 |
// Revision 1.16 2004/05/06 13:02:09 jonen |
27 |
// + added/modified functions related to User-Messages |
28 |
// |
29 |
// Revision 1.15 2004/03/11 21:04:48 jonen |
30 |
// +changed backend-functions to only get/save User ONLY not SOME object (security!) |
31 |
// |
32 |
// Revision 1.14 2003/07/02 11:02:09 joko |
33 |
// + fixed bug with create/_create |
34 |
// |
35 |
// Revision 1.13 2003/04/04 02:15:09 joko |
36 |
// renamed method of helper object |
37 |
// |
38 |
// Revision 1.12 2003/03/03 21:20:47 joko |
39 |
// mungled the namespaces |
40 |
// |
41 |
// Revision 1.11 2003/02/14 14:22:06 joko |
42 |
// + always (re-)tries to connect |
43 |
// |
44 |
// Revision 1.10 2003/02/13 21:58:39 joko |
45 |
// + caching mechanisms more configurable now |
46 |
// |
47 |
// Revision 1.9 2003/02/09 17:46:26 joko |
48 |
// + now utilizing DesignPattern::RemoteProxy and Data::Deep |
49 |
// |
50 |
// Revision 1.8 2002/12/28 01:16:42 jonen |
51 |
// + added clear of $this->state[autologout] at 'hadTimeout()' |
52 |
// |
53 |
// Revision 1.7 2002/12/23 11:32:28 jonen |
54 |
// + added inserting of uservars on '_create' to frontend db(needed to login user after create!) |
55 |
// + added hard link to page 'pub/register', TODO: move this out here |
56 |
// |
57 |
// Revision 1.6 2002/12/18 22:46:06 jonen |
58 |
// + added function 'getUserGuid()' |
59 |
// (returns user's 'guid' read from database at function 'login()' (now)) |
60 |
// + function '_loadData()' loads user remote now via 'getUserGuid()' |
61 |
// |
62 |
// Revision 1.5 2002/12/12 21:34:32 joko |
63 |
// + fix in 'function update': invalidate data only if it's not for caching purposes |
64 |
// |
65 |
// Revision 1.4 2002/12/06 04:10:28 joko |
66 |
// + replaced 'xyzCache' through 'xyzProxy' |
67 |
// |
68 |
// Revision 1.3 2002/12/04 07:42:54 jonen |
69 |
// + changes according to backend schema change |
70 |
// |
71 |
// Revision 1.2 2002/12/03 16:47:22 joko |
72 |
// - function preLogout() |
73 |
// + function preLogout($autologout = 0) |
74 |
// + function hadTimeout() |
75 |
// |
76 |
// Revision 1.1 2002/11/12 05:42:31 joko |
77 |
// + initial checkin |
78 |
// |
79 |
// Revision 1.7 2002/10/29 19:09:53 cvsjoko |
80 |
// + function getLastLogin |
81 |
// + function getUserId |
82 |
// |
83 |
// Revision 1.6 2002/10/25 11:18:10 cvsmax |
84 |
// - removed old functions ('logout', 'create',...) |
85 |
// + add function preLogout() # save user session and unset (session) state |
86 |
// |
87 |
// Revision 1.5 2002/10/22 09:47:48 cvsmax |
88 |
// + add new |
89 |
// - function create() # create & save new user-object in session |
90 |
// - function _create($attr) # create & save new user-object in backend |
91 |
// |
92 |
// - purged some very old functions |
93 |
// |
94 |
// Revision 1.4 2002/10/17 03:12:17 cvsmax |
95 |
// -(+) cleaned code from old structure |
96 |
// + function getPostVar($fieldname) |
97 |
// |
98 |
// Revision 1.3 2002/10/10 02:41:57 cvsjoko |
99 |
// + fixed typo |
100 |
// |
101 |
// Revision 1.2 2002/10/10 02:34:33 cvsjoko |
102 |
// + new level of data-caching (session and persistant) |
103 |
// + function _save() |
104 |
// + function save() |
105 |
// + handling (storing/caching) of POSTed data |
106 |
// + function getPostVars() |
107 |
// + function setPostVars() |
108 |
// + handy utils |
109 |
// + function doDataReady() cares for ready data to continue working |
110 |
// + function refresh() clears underlying data container and makes data "unready" |
111 |
// |
112 |
// Revision 1.1 2002/10/09 00:40:13 cvsjoko |
113 |
// + new |
114 |
// |
115 |
// |
116 |
// ----------------------------------------------------------------------------- |
117 |
|
118 |
|
119 |
/** |
120 |
* This is the User class. |
121 |
* |
122 |
* @author Andreas Motl <andreas.motl@ilo.de> |
123 |
* @package org.netfrag.flib |
124 |
* @name Tracker::User |
125 |
* |
126 |
*/ |
127 |
class User { |
128 |
|
129 |
var $state; |
130 |
var $meta; |
131 |
|
132 |
var $pObject; |
133 |
var $data; |
134 |
|
135 |
|
136 |
function User() { |
137 |
|
138 |
// attention: |
139 |
// this code is only run when your session is _really_ fresh |
140 |
if (session_register_safe('userstate')) { |
141 |
} |
142 |
|
143 |
$this->_loadState(); |
144 |
$this->meta[logontoken] = 'k&%g2'; |
145 |
|
146 |
// print dumpVar($_SESSION); |
147 |
|
148 |
} |
149 |
|
150 |
function _loadState() { |
151 |
global $userstate; |
152 |
$this->state = $userstate; |
153 |
} |
154 |
function _saveState() { |
155 |
global $userstate; |
156 |
$userstate = $this->state; |
157 |
} |
158 |
|
159 |
|
160 |
function _loadData() { |
161 |
$this->meta[data_ready] = 1; |
162 |
//$this->pObject = new ProxyObject($this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1) ); |
163 |
$rpcinfo = $this->site->configuration->get("rpcinfo"); |
164 |
//$this->pObject = mkObject('DesignPattern::RemoteProxy', $this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) ); |
165 |
$cache_key = $this->getUserGuid() . "_user"; |
166 |
$this->pObject = mkObject('DesignPattern::RemoteProxy', $cache_key, array( key => 1, command => 'getUser', query => $this->getUserGuid(), remote => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) ); |
167 |
if ($this->getUserGuid()) { |
168 |
$this->data = $this->pObject->getResult(); |
169 |
} |
170 |
} |
171 |
|
172 |
function _saveData($newRawData) { |
173 |
|
174 |
//print "newRawData: " . Dumper($newRawData) . "<br>"; |
175 |
foreach ($newRawData as $key => $val) { |
176 |
//print "$key => $val<br>"; |
177 |
$this->set($key, $val); |
178 |
} |
179 |
|
180 |
$this->_save("cache"); |
181 |
//$this->_save("commit"); |
182 |
|
183 |
//$this->pObject->flushProxy(); // done in "pObject->save" |
184 |
|
185 |
} |
186 |
|
187 |
function _save($type = '') { |
188 |
//print Dumper($this->data); |
189 |
//$this->pObject->save($this->data, $type); |
190 |
// new of 2004-03-08: don't use generic 'saveObject'!! |
191 |
// for security reasons disabled at ClientBackend!!! |
192 |
if($type == 'commit') { |
193 |
$args = array( 'guid' => $this->data[guid], 'data' => $this->data ); |
194 |
$this->pObject->backend->send('saveUser', $args, array( utf8 => 1) ); |
195 |
$this->_save_local(); |
196 |
$this->refresh(); |
197 |
} |
198 |
// invalidate data only if it's not for caching purposes |
199 |
if ($type != 'cache') { |
200 |
$this->meta[data_ready] = 0; |
201 |
} |
202 |
} |
203 |
|
204 |
function _save_local() { |
205 |
connectdb(); |
206 |
$username = $this->get('userData.username'); |
207 |
$password = $this->get('userData.password'); |
208 |
$sql = "UPDATE td_users SET pass='$password' WHERE uname='$username'"; |
209 |
$res = send_sql($sql); |
210 |
} |
211 |
|
212 |
function save() { |
213 |
$this->_doDataReady(); |
214 |
$this->_save("commit"); |
215 |
} |
216 |
|
217 |
function create() { |
218 |
// How THIS can function??? we will fool ourselves... |
219 |
//$this->_save(); |
220 |
//$attr = $this->pObject->getResult(); |
221 |
$this->_create($this->data); |
222 |
} |
223 |
|
224 |
function createGuest() { |
225 |
if($this->isLoggedOn) { return; } |
226 |
//$this->_save(); |
227 |
//$attr = $this->pObject->getResult(); |
228 |
//print DumpVar($attr); |
229 |
$data = $this->_createGuest(); |
230 |
return $data; |
231 |
} |
232 |
|
233 |
function _create($result) { |
234 |
global $site; |
235 |
//print "saving to backend: " . dumpVar($result) . "<br>"; |
236 |
$response = $this->pObject->backend->send('createUser', array('data' => $result) ); |
237 |
$objectId = $response[oid]; |
238 |
if($objectId) { |
239 |
connectdb(); |
240 |
$guid = $response[guid]; |
241 |
$username = $this->get('userData.username'); |
242 |
$password = $this->get('userData.password'); |
243 |
$sql = "INSERT into td_users VALUES ( '$objectId', '$guid', '', '$username', '$password','' )"; |
244 |
$res = send_sql($sql); |
245 |
} |
246 |
else { |
247 |
// TODO: handle this with a generic rpc-debug/pending function |
248 |
$site->session->set('commit_error', array( form => getlt('page/register/notify/rpc_error') )); |
249 |
$site->redirect( getlink('/pub/register/') ); |
250 |
} |
251 |
//$this->_init($objectId); |
252 |
} |
253 |
|
254 |
function _createGuest() { |
255 |
global $site; |
256 |
//print "saving to backend: " . dumpVar($result) . "<br>"; |
257 |
if(!$this->pObject) { |
258 |
$cache_key = php::CreateGUID() . "_guest"; |
259 |
$rpcinfo = $this->site->configuration->get("rpcinfo"); |
260 |
$pObject = mkObject('DesignPattern::RemoteProxy', $cache_key, array( key => 1, command => 'createGuestUser', '', remote => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) ); |
261 |
$response = $pObject->getResult(); |
262 |
} else { |
263 |
$response = $this->pObject->backend->send('createGuestUser'); |
264 |
} |
265 |
//print "response: " . Dumper($response) . "<br>"; |
266 |
$objectId = $response[oid]; |
267 |
if($objectId) { |
268 |
return $response; |
269 |
} |
270 |
else { |
271 |
// TODO: handle this with a generic rpc-debug/pending function |
272 |
$site->session->set('login_error', 'rpc_error'); |
273 |
$site->redirect( getlink('/pub/login/') ); |
274 |
} |
275 |
//$this->_init($objectId); |
276 |
} |
277 |
|
278 |
function get($attr) { |
279 |
$this->_doDataReady(); |
280 |
$deep = new Data_Deep($this->data); |
281 |
return $deep->get($attr); |
282 |
} |
283 |
function set($attr, $val) { |
284 |
$this->_doDataReady(); |
285 |
$deep = new Data_Deep($this->data); |
286 |
$deep->set($attr, $val); |
287 |
} |
288 |
|
289 |
function isLoggedOn() { |
290 |
if ($this->state[id] && $this->state[status] == $this->meta[logontoken]) { |
291 |
return 1; |
292 |
} |
293 |
} |
294 |
|
295 |
|
296 |
//===== function userlogin ========================= |
297 |
function login($user,$passwd) { |
298 |
connectdb(); |
299 |
//$sql = "SELECT * FROM td_users WHERE uname='$user'"; |
300 |
$sql = "SELECT user_oid, guid, uname, pass FROM td_users WHERE uname='$user'"; |
301 |
if ($res=send_sql($sql)) { |
302 |
$row = mysql_fetch_array($res,MYSQL_ASSOC); |
303 |
if (!is_array($row)) { return 0; } |
304 |
} |
305 |
if ($row[pass] == $passwd) { |
306 |
//return $uservars_db; |
307 |
// store 1st priority user-data to state |
308 |
$this->state[id] = $row[user_oid]; |
309 |
$this->state[guid] = $row[guid]; |
310 |
$this->state[status] = $this->meta[logontoken]; |
311 |
$this->_saveState(); |
312 |
$this->site->log( get_class($this) . "->login ok", PEAR_LOG_NOTICE ); |
313 |
return 1; |
314 |
} else { |
315 |
return 0; |
316 |
} |
317 |
} |
318 |
|
319 |
//===== function guestlogin ========================= |
320 |
function loginGuest($data) { |
321 |
// store 1st priority user-data to state |
322 |
$this->state[id] = $data[oid]; |
323 |
$this->state[guid] = $data[guid]; |
324 |
$this->state[status] = $this->meta[logontoken]; |
325 |
$this->_saveState(); |
326 |
$this->site->log( get_class($this) . "->guest-login ok", PEAR_LOG_NOTICE ); |
327 |
return 1; |
328 |
} |
329 |
|
330 |
function exists($string) { |
331 |
connectdb(); |
332 |
$sql = "SELECT uname FROM td_users WHERE uname='$string'"; |
333 |
if ($res=send_sql($sql)) { |
334 |
$row = mysql_fetch_array($res,MYSQL_ASSOC); |
335 |
|
336 |
if (is_array($row)) { |
337 |
return 1; |
338 |
} |
339 |
} |
340 |
|
341 |
} |
342 |
|
343 |
|
344 |
function update($vars) { |
345 |
$this->setPostVars($vars); |
346 |
//print Dumper($vars); |
347 |
$data = $this->_transformInputData($vars); |
348 |
//print Dumper($data); |
349 |
$this->_saveData($data); |
350 |
} |
351 |
|
352 |
function _transformInputData(&$uservars) { |
353 |
global $def_mapping; |
354 |
$mapping_uservars2data = array_flip($def_mapping[data2uservars]); |
355 |
foreach ($uservars as $key => $val) { |
356 |
$key_target = $mapping_uservars2data[$key]; |
357 |
if($key_target != '') { |
358 |
//print "$key => $key_target: $val<br>"; |
359 |
$target[$key_target] = $val; |
360 |
} |
361 |
} |
362 |
return $target; |
363 |
} |
364 |
|
365 |
function logout() { |
366 |
|
367 |
global $site; |
368 |
|
369 |
// get information about user from site |
370 |
$userid = $this->getUserId(); |
371 |
|
372 |
$this->site->log( get_class($this) . "->logout: userid $userid", PEAR_LOG_NOTICE ); |
373 |
|
374 |
// get information about session from php |
375 |
$session_id = session_id(); |
376 |
$session_name = session_name(); |
377 |
|
378 |
// session-sqldb related |
379 |
$date = date('Y-m-d H:i:s', time()); |
380 |
$session_e = $site->session->exists($session_id); |
381 |
$sql = "UPDATE f_map_user_session SET date_logged_out='$date' WHERE session_uid='$session_e[session_uid]' AND user_uid='$userid'"; |
382 |
$res = send_sql($sql); |
383 |
|
384 |
// destroy user's session at server-side |
385 |
$session_name = session_name(); |
386 |
session_destroy(); |
387 |
|
388 |
// overwrite session-variables - to be sure ;) |
389 |
$_SESSION = array(); |
390 |
unset($_COOKIE[$session_name]); |
391 |
|
392 |
} |
393 |
|
394 |
function preLogout($autologout = 0) { |
395 |
global $site; |
396 |
$this->state[status] = ''; |
397 |
$this->state[autologout] = $autologout; |
398 |
$this->_saveState(); |
399 |
} |
400 |
|
401 |
function getAccountSum() { |
402 |
// store additional user-data to state |
403 |
if (!$this->state[accountSum]) { |
404 |
$this->state[accountSum] = $this->get("account.amount"); |
405 |
$this->_saveState(); |
406 |
} |
407 |
return $this->state[accountSum]; |
408 |
} |
409 |
|
410 |
function getAccountCurrency() { |
411 |
// store additional user-data to state |
412 |
if (!$this->state[accountCurrency]) { |
413 |
$this->state[accountCurrency] = $this->get("account.currency"); |
414 |
$this->_saveState(); |
415 |
} |
416 |
//print "state: " . $this->get("financeInfo.currency") . "<br>"; |
417 |
return $this->state[accountCurrency]; |
418 |
} |
419 |
|
420 |
function refresh() { |
421 |
if ($this->pObject) {$this->pObject->flush();} |
422 |
$this->meta[data_ready] = 0; |
423 |
} |
424 |
|
425 |
function _doDataReady() { |
426 |
if (!$this->meta[data_ready]) { $this->_loadData(); } |
427 |
} |
428 |
|
429 |
function getLastLogin() { |
430 |
$userid = $this->getUserId(); |
431 |
connectdb(); |
432 |
$sql = "SELECT * FROM f_map_user_session WHERE user_uid='$userid' AND last='1'"; |
433 |
$res = send_sql($sql); |
434 |
while($row = mysql_fetch_array($res,MYSQL_BOTH)) { |
435 |
if($row[date_logged_out]=="0000-00-00 00:00:00") { |
436 |
$date = $row[date_logged_in]; |
437 |
} else { |
438 |
$date = $row[date_logged_out]; |
439 |
} |
440 |
$lastLogin = strftime("%d %b %Y - %H:%M",strtotime($date)); |
441 |
} |
442 |
return $lastLogin; |
443 |
} |
444 |
|
445 |
function getUserId() { |
446 |
return $this->state[id]; |
447 |
} |
448 |
|
449 |
function getUserGuid() { |
450 |
return $this->state[guid]; |
451 |
} |
452 |
|
453 |
function getPostVars() { |
454 |
return $this->state[postvars]; |
455 |
} |
456 |
|
457 |
function setPostVars($postvars) { |
458 |
$this->state[postvars] = $postvars; |
459 |
$this->_saveState(); |
460 |
} |
461 |
|
462 |
function getPostVar($var) { |
463 |
return $this->state[postvars][$var]; |
464 |
} |
465 |
|
466 |
function hadTimeout() { |
467 |
if ($this->state[autologout]) { $this->state[autologout] = ''; return 1; } |
468 |
} |
469 |
|
470 |
function getNewMessages() { |
471 |
$messages = array(); |
472 |
$userguid = $this->getUserGuid(); |
473 |
connectdb(); |
474 |
$sql = "SELECT * FROM td_message WHERE target_guid='$userguid' AND new='1'"; |
475 |
if ($res=send_sql($sql)) { |
476 |
while($row = mysql_fetch_array($res,MYSQL_ASSOC)) { |
477 |
if (!is_array($row)) { return 0; } |
478 |
array_push($messages, $row); |
479 |
} |
480 |
} |
481 |
return $messages; |
482 |
} |
483 |
|
484 |
function markNewMessage($mid) { |
485 |
connectdb(); |
486 |
$sql = "UPDATE td_message SET new='0' WHERE mid='$mid'"; |
487 |
if(!$res=send_sql($sql)) { |
488 |
return "Message not found!"; |
489 |
} |
490 |
} |
491 |
|
492 |
} |
493 |
|
494 |
?> |