/[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.12 - (show annotations)
Mon Mar 3 21:20:47 2003 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.11: +6 -3 lines
mungled the namespaces

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

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