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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Mon Dec 23 11:32:28 2002 UTC (21 years, 8 months ago) by jonen
Branch: MAIN
Changes since 1.6: +23 -3 lines
+ added inserting of uservars on '_create' to frontend db(needed to login user after create!)
  + added hard link to page 'pub/register', TODO: move this out here

1 joko 1.1 <?
2     // -----------------------------------------------------------------------------
3 jonen 1.7 // $Id: User.php,v 1.6 2002/12/18 22:46:06 jonen Exp $
4 joko 1.1 // -----------------------------------------------------------------------------
5 joko 1.2 // $Log: User.php,v $
6 jonen 1.7 // Revision 1.6 2002/12/18 22:46:06 jonen
7     // + added function 'getUserGuid()'
8     // (returns user's 'guid' read from database at function 'login()' (now))
9     // + function '_loadData()' loads user remote now via 'getUserGuid()'
10     //
11 jonen 1.6 // Revision 1.5 2002/12/12 21:34:32 joko
12     // + fix in 'function update': invalidate data only if it's not for caching purposes
13     //
14 joko 1.5 // Revision 1.4 2002/12/06 04:10:28 joko
15     // + replaced 'xyzCache' through 'xyzProxy'
16     //
17 joko 1.4 // Revision 1.3 2002/12/04 07:42:54 jonen
18     // + changes according to backend schema change
19     //
20 jonen 1.3 // Revision 1.2 2002/12/03 16:47:22 joko
21     // - function preLogout()
22     // + function preLogout($autologout = 0)
23     // + function hadTimeout()
24     //
25 joko 1.2 // Revision 1.1 2002/11/12 05:42:31 joko
26     // + initial checkin
27     //
28 joko 1.1 // Revision 1.7 2002/10/29 19:09:53 cvsjoko
29     // + function getLastLogin
30     // + function getUserId
31     //
32     // Revision 1.6 2002/10/25 11:18:10 cvsmax
33     // - removed old functions ('logout', 'create',...)
34     // + add function preLogout() # save user session and unset (session) state
35     //
36     // Revision 1.5 2002/10/22 09:47:48 cvsmax
37     // + add new
38     // - function create() # create & save new user-object in session
39     // - function _create($attr) # create & save new user-object in backend
40     //
41     // - purged some very old functions
42     //
43     // Revision 1.4 2002/10/17 03:12:17 cvsmax
44     // -(+) cleaned code from old structure
45     // + function getPostVar($fieldname)
46     //
47     // Revision 1.3 2002/10/10 02:41:57 cvsjoko
48     // + fixed typo
49     //
50     // Revision 1.2 2002/10/10 02:34:33 cvsjoko
51     // + new level of data-caching (session and persistant)
52     // + function _save()
53     // + function save()
54     // + handling (storing/caching) of POSTed data
55     // + function getPostVars()
56     // + function setPostVars()
57     // + handy utils
58     // + function doDataReady() cares for ready data to continue working
59     // + function refresh() clears underlying data container and makes data "unready"
60     //
61     // Revision 1.1 2002/10/09 00:40:13 cvsjoko
62     // + new
63     //
64     //
65     // -----------------------------------------------------------------------------
66    
67     class User {
68    
69     var $state;
70     var $meta;
71    
72     var $pObject;
73     var $data;
74    
75    
76     function User() {
77    
78     // attention:
79     // this code is only run when your session is _really_ fresh
80     if (session_register_safe('userstate')) {
81     }
82    
83     $this->_loadState();
84     $this->meta[logontoken] = 'k&%g2';
85    
86     // print dumpVar($_SESSION);
87    
88     }
89    
90     function _loadState() {
91     global $userstate;
92     $this->state = $userstate;
93     }
94     function _saveState() {
95     global $userstate;
96     $userstate = $this->state;
97     }
98    
99    
100     function _loadData() {
101 jonen 1.6 $this->pObject = new ProxyObject($this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1) );
102     if ($this->getUserGuid()) {
103 joko 1.1 $this->data = $this->pObject->getAttributes();
104     }
105     $this->meta[data_ready] = 1;
106     }
107    
108     function _saveData($newRawData) {
109    
110     foreach ($newRawData as $key => $val) {
111     //print "$key => $val<br>";
112     $this->set($key, $val);
113     }
114    
115     $this->_save("cache");
116    
117 joko 1.4 //$this->pObject->flushProxy(); // done in "pObject->save"
118 joko 1.1
119     }
120    
121     function _save($type = '') {
122 joko 1.5 //print Dumper($this->data);
123 joko 1.1 $this->pObject->save($this->data, $type);
124 joko 1.5 // invalidate data only if it's not for caching purposes
125     if ($type != 'cache') {
126     $this->meta[data_ready] = 0;
127     }
128 joko 1.1 }
129    
130     function save() {
131     $this->_doDataReady();
132     $this->_save("commit");
133     }
134    
135     function create() {
136     $this->_save();
137     $attr = $this->pObject->getAttributes();
138     //print DumpVar($attr);
139     $this->_create($attr);
140     }
141    
142     function _create($result) {
143     //print "saving to backend: " . dumpVar($result) . "<br>";
144     $response = $this->pObject->backend->send('createUser', array('data' => $result) );
145     $objectId = $response[oid];
146 jonen 1.7 if($objectId) {
147     connectdb();
148     $guid = $response[guid];
149     $username = $this->get('userData.username');
150     $password = $this->get('userData.password');
151     $sql = "INSERT into td_users VALUES ( '$objectId', '$guid', '', '$username', '$password','' )";
152     $res = send_sql($sql);
153     }
154     else {
155     // TODO: handle this with a generic rpc-debug/pending function
156     $site->session->set('register_error', 'rpc_error');
157     $site->redirect( getlink('/pub/register/') );
158     }
159 joko 1.1 //$this->_init($objectId);
160     }
161    
162     function _getHKR($key) {
163     $attr_arr = split('\.', $key);
164     $attr_hkr = '["' . join('"]["', $attr_arr) . '"]';
165     return $attr_hkr;
166     }
167    
168     function get($attr) {
169     $this->_doDataReady();
170     $hkr = $this->_getHKR($attr);
171     $evalstr = 'return $this->data' . $hkr . ';';
172     $val = eval($evalstr);
173     return $val;
174     }
175     function set($attr, $val) {
176     $this->_doDataReady();
177     $hkr = $this->_getHKR($attr);
178     $evalstr = '$this->data' . $hkr . ' = \'' . $val . '\';';
179     eval($evalstr);
180     }
181    
182     function isLoggedOn() {
183     if ($this->state[id] && $this->state[status] == $this->meta[logontoken]) {
184     return 1;
185     }
186     }
187    
188    
189     //===== function userlogin =========================
190     function login($user,$passwd) {
191     connectdb();
192     //$sql = "SELECT * FROM td_users WHERE uname='$user'";
193 jonen 1.6 $sql = "SELECT user_oid, guid, uname, pass FROM td_users WHERE uname='$user'";
194 joko 1.1 if ($res=send_sql($sql)) {
195     $row = mysql_fetch_array($res,MYSQL_ASSOC);
196     if (!is_array($row)) { return 0; }
197     }
198     if ($row[pass] == $passwd) {
199     //return $uservars_db;
200     // store 1st priority user-data to state
201     $this->state[id] = $row[user_oid];
202 jonen 1.6 $this->state[guid] = $row[guid];
203 joko 1.1 $this->state[status] = $this->meta[logontoken];
204     $this->_saveState();
205     $this->site->log( get_class($this) . "->login ok", LOG_DEBUG );
206     return 1;
207     } else {
208     return 0;
209     }
210     }
211    
212     function exists($string) {
213     connectdb();
214     $sql = "SELECT uname FROM td_users WHERE uname='$string'";
215     if ($res=send_sql($sql)) {
216     $row = mysql_fetch_array($res,MYSQL_ASSOC);
217    
218     if (is_array($row)) {
219     return 1;
220     }
221     }
222    
223     }
224    
225    
226 joko 1.5 function update($vars) {
227     $this->setPostVars($vars);
228     //print Dumper($vars);
229     $data = $this->_transformInputData($vars);
230 joko 1.1 $this->_saveData($data);
231     }
232    
233     function _transformInputData(&$uservars) {
234     global $def_mapping;
235     $mapping_uservars2data = array_flip($def_mapping[data2uservars]);
236     foreach ($uservars as $key => $val) {
237     $key_target = $mapping_uservars2data[$key];
238 jonen 1.7 if($key_target != '') {
239     //print "$key => $key_target: $val<br>";
240     $target[$key_target] = $val;
241     }
242 joko 1.1 }
243     return $target;
244     }
245    
246     function logout() {
247    
248     global $site;
249    
250     // get information about user from site
251 jonen 1.6 $userid = $this->getUserId();
252 joko 1.1
253     $this->site->log( get_class($this) . "->logout: userid $userid", LOG_DEBUG );
254    
255     // get information about session from php
256     $session_id = session_id();
257     $session_name = session_name();
258    
259     // session-sqldb related
260     $date = date('Y-m-d H:i:s', time());
261     $session_e = $site->session->exists($session_id);
262     $sql = "UPDATE f_map_user_session SET date_logged_out='$date' WHERE session_uid='$session_e[session_uid]' AND user_uid='$userid'";
263     $res = send_sql($sql);
264    
265     // destroy user's session at server-side
266     $session_name = session_name();
267     session_destroy();
268    
269     // overwrite session-variables - to be sure ;)
270     $_SESSION = array();
271     unset($_COOKIE[$session_name]);
272    
273     }
274    
275 joko 1.2 function preLogout($autologout = 0) {
276 joko 1.1 global $site;
277     $this->state[status] = '';
278 joko 1.2 $this->state[autologout] = $autologout;
279 joko 1.1 $this->_saveState();
280     }
281    
282     function getAccountSum() {
283     // store additional user-data to state
284     if (!$this->state[accountSum]) {
285 jonen 1.3 $this->state[accountSum] = $this->get("account.amount");
286 joko 1.1 $this->_saveState();
287     }
288     return $this->state[accountSum];
289     }
290    
291     function getAccountCurrency() {
292     // store additional user-data to state
293     if (!$this->state[accountCurrency]) {
294 jonen 1.3 $this->state[accountCurrency] = $this->get("account.currency");
295 joko 1.1 $this->_saveState();
296     }
297     //print "state: " . $this->get("financeInfo.currency") . "<br>";
298     return $this->state[accountCurrency];
299     }
300    
301     function refresh() {
302     if ($this->pObject) {$this->pObject->flushState();}
303     $this->meta[data_ready] = 0;
304     }
305    
306     function _doDataReady() {
307     if (!$this->meta[data_ready]) { $this->_loadData(); }
308     }
309    
310     function getLastLogin() {
311     $userid = $this->getUserId();
312     connectdb();
313     $sql = "SELECT * FROM f_map_user_session WHERE user_uid='$userid' AND last='1'";
314     $res = send_sql($sql);
315     while($row = mysql_fetch_array($res,MYSQL_BOTH)) {
316     if($row[date_logged_out]=="0000-00-00 00:00:00") {
317     $date = $row[date_logged_in];
318     } else {
319     $date = $row[date_logged_out];
320     }
321     $lastLogin = strftime("%d %b %Y - %H:%M",strtotime($date));
322     }
323     return $lastLogin;
324     }
325    
326     function getUserId() {
327     return $this->state[id];
328     }
329    
330 jonen 1.6 function getUserGuid() {
331     return $this->state[guid];
332     }
333 joko 1.1
334     function getPostVars() {
335     return $this->state[postvars];
336     }
337    
338     function setPostVars($postvars) {
339     $this->state[postvars] = $postvars;
340     $this->_saveState();
341     }
342    
343     function getPostVar($var) {
344     return $this->state[postvars][$var];
345     }
346    
347 joko 1.2 function hadTimeout() {
348     if ($this->state[autologout]) { return 1; }
349     }
350 joko 1.1
351     }
352    
353     ?>

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