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