/[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.17 - (show annotations)
Mon Jun 7 16:54:53 2004 UTC (20 years, 1 month ago) by joko
Branch: MAIN
Changes since 1.16: +8 -3 lines
modified cache-key
added cache-control

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

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