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