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