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