/[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.11 - (show annotations)
Fri Feb 14 14:22:06 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.10: +5 -2 lines
+ always (re-)tries to connect

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

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