/[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.9 - (show annotations)
Sun Feb 9 17:46:26 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.8: +14 -18 lines
+ now utilizing Data::Driver::Proxy and Data::Deep

1 <?
2 // -----------------------------------------------------------------------------
3 // $Id: User.php,v 1.8 2002/12/28 01:16:42 jonen Exp $
4 // -----------------------------------------------------------------------------
5 // $Log: User.php,v $
6 // Revision 1.8 2002/12/28 01:16:42 jonen
7 // + added clear of $this->state[autologout] at 'hadTimeout()'
8 //
9 // Revision 1.7 2002/12/23 11:32:28 jonen
10 // + added inserting of uservars on '_create' to frontend db(needed to login user after create!)
11 // + added hard link to page 'pub/register', TODO: move this out here
12 //
13 // Revision 1.6 2002/12/18 22:46:06 jonen
14 // + added function 'getUserGuid()'
15 // (returns user's 'guid' read from database at function 'login()' (now))
16 // + function '_loadData()' loads user remote now via 'getUserGuid()'
17 //
18 // Revision 1.5 2002/12/12 21:34:32 joko
19 // + fix in 'function update': invalidate data only if it's not for caching purposes
20 //
21 // Revision 1.4 2002/12/06 04:10:28 joko
22 // + replaced 'xyzCache' through 'xyzProxy'
23 //
24 // Revision 1.3 2002/12/04 07:42:54 jonen
25 // + changes according to backend schema change
26 //
27 // Revision 1.2 2002/12/03 16:47:22 joko
28 // - function preLogout()
29 // + function preLogout($autologout = 0)
30 // + function hadTimeout()
31 //
32 // Revision 1.1 2002/11/12 05:42:31 joko
33 // + initial checkin
34 //
35 // Revision 1.7 2002/10/29 19:09:53 cvsjoko
36 // + function getLastLogin
37 // + function getUserId
38 //
39 // Revision 1.6 2002/10/25 11:18:10 cvsmax
40 // - removed old functions ('logout', 'create',...)
41 // + add function preLogout() # save user session and unset (session) state
42 //
43 // Revision 1.5 2002/10/22 09:47:48 cvsmax
44 // + add new
45 // - function create() # create & save new user-object in session
46 // - function _create($attr) # create & save new user-object in backend
47 //
48 // - purged some very old functions
49 //
50 // Revision 1.4 2002/10/17 03:12:17 cvsmax
51 // -(+) cleaned code from old structure
52 // + function getPostVar($fieldname)
53 //
54 // Revision 1.3 2002/10/10 02:41:57 cvsjoko
55 // + fixed typo
56 //
57 // Revision 1.2 2002/10/10 02:34:33 cvsjoko
58 // + new level of data-caching (session and persistant)
59 // + function _save()
60 // + function save()
61 // + handling (storing/caching) of POSTed data
62 // + function getPostVars()
63 // + function setPostVars()
64 // + handy utils
65 // + function doDataReady() cares for ready data to continue working
66 // + function refresh() clears underlying data container and makes data "unready"
67 //
68 // Revision 1.1 2002/10/09 00:40:13 cvsjoko
69 // + new
70 //
71 //
72 // -----------------------------------------------------------------------------
73
74 class User {
75
76 var $state;
77 var $meta;
78
79 var $pObject;
80 var $data;
81
82
83 function User() {
84
85 // attention:
86 // this code is only run when your session is _really_ fresh
87 if (session_register_safe('userstate')) {
88 }
89
90 $this->_loadState();
91 $this->meta[logontoken] = 'k&%g2';
92
93 // print dumpVar($_SESSION);
94
95 }
96
97 function _loadState() {
98 global $userstate;
99 $this->state = $userstate;
100 }
101 function _saveState() {
102 global $userstate;
103 $userstate = $this->state;
104 }
105
106
107 function _loadData() {
108 $this->meta[data_ready] = 1;
109 //$this->pObject = new ProxyObject($this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1) );
110 $rpcinfo = $this->site->configuration->get("rpcinfo");
111 $this->pObject = mkObject('Data::Driver::Proxy', $this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1, rpcinfo => $rpcinfo ) );
112 if ($this->getUserGuid()) {
113 $this->data = $this->pObject->getAttributes();
114 }
115 }
116
117 function _saveData($newRawData) {
118
119 foreach ($newRawData as $key => $val) {
120 //print "$key => $val<br>";
121 $this->set($key, $val);
122 }
123
124 $this->_save("cache");
125
126 //$this->pObject->flushProxy(); // done in "pObject->save"
127
128 }
129
130 function _save($type = '') {
131 //print Dumper($this->data);
132 $this->pObject->save($this->data, $type);
133 // invalidate data only if it's not for caching purposes
134 if ($type != 'cache') {
135 $this->meta[data_ready] = 0;
136 }
137 }
138
139 function save() {
140 $this->_doDataReady();
141 $this->_save("commit");
142 }
143
144 function create() {
145 $this->_save();
146 $attr = $this->pObject->getAttributes();
147 //print DumpVar($attr);
148 $this->_create($attr);
149 }
150
151 function _create($result) {
152 //print "saving to backend: " . dumpVar($result) . "<br>";
153 $response = $this->pObject->backend->send('createUser', array('data' => $result) );
154 $objectId = $response[oid];
155 if($objectId) {
156 connectdb();
157 $guid = $response[guid];
158 $username = $this->get('userData.username');
159 $password = $this->get('userData.password');
160 $sql = "INSERT into td_users VALUES ( '$objectId', '$guid', '', '$username', '$password','' )";
161 $res = send_sql($sql);
162 }
163 else {
164 // TODO: handle this with a generic rpc-debug/pending function
165 $site->session->set('register_error', 'rpc_error');
166 $site->redirect( getlink('/pub/register/') );
167 }
168 //$this->_init($objectId);
169 }
170
171 function get($attr) {
172 $this->_doDataReady();
173 $deep = new Data_Deep($this->data);
174 return $deep->get($attr);
175 }
176 function set($attr, $val) {
177 $this->_doDataReady();
178 $deep = new Data_Deep($this->data);
179 $deep->set($attr, $val);
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 $sql = "SELECT user_oid, guid, uname, pass FROM td_users WHERE uname='$user'";
194 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 $this->state[guid] = $row[guid];
203 $this->state[status] = $this->meta[logontoken];
204 $this->_saveState();
205 $this->site->log( get_class($this) . "->login ok", PEAR_LOG_NOTICE );
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 function update($vars) {
227 $this->setPostVars($vars);
228 //print Dumper($vars);
229 $data = $this->_transformInputData($vars);
230 $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 if($key_target != '') {
239 //print "$key => $key_target: $val<br>";
240 $target[$key_target] = $val;
241 }
242 }
243 return $target;
244 }
245
246 function logout() {
247
248 global $site;
249
250 // get information about user from site
251 $userid = $this->getUserId();
252
253 $this->site->log( get_class($this) . "->logout: userid $userid", PEAR_LOG_NOTICE );
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 function preLogout($autologout = 0) {
276 global $site;
277 $this->state[status] = '';
278 $this->state[autologout] = $autologout;
279 $this->_saveState();
280 }
281
282 function getAccountSum() {
283 // store additional user-data to state
284 if (!$this->state[accountSum]) {
285 $this->state[accountSum] = $this->get("account.amount");
286 $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 $this->state[accountCurrency] = $this->get("account.currency");
295 $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 function getUserGuid() {
331 return $this->state[guid];
332 }
333
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 function hadTimeout() {
348 if ($this->state[autologout]) { $this->state[autologout] = ''; return 1; }
349 }
350
351 }
352
353 ?>

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