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