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