/[cvs]/nfo/php/libs/org.netfrag.flib/Tracker/User.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.flib/Tracker/User.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (show annotations)
Wed Nov 17 16:14:47 2004 UTC (19 years, 9 months ago) by jonen
Branch: MAIN
Changes since 1.21: +5 -2 lines
+ minor changes related to utf8 encoding

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed