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