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