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