/[cvs]/nfo/php/libs/org.netfrag.flib/Tracker/User.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.flib/Tracker/User.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Tue Dec 3 16:47:22 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
Changes since 1.1: +10 -4 lines
- function preLogout()
+ function preLogout($autologout = 0)
+ function hadTimeout()

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed