/[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.20 - (show annotations)
Thu Oct 7 14:08:51 2004 UTC (19 years, 9 months ago) by jonen
Branch: MAIN
Changes since 1.19: +9 -5 lines
+ bufix related to save new-registered user-data

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

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