1 |
<? |
<? |
2 |
|
/** |
3 |
|
* This file contains the Tracker::User module. |
4 |
|
* |
5 |
|
* @author Andreas Motl <andreas.motl@ilo.de> |
6 |
|
* @package org.netfrag.flib |
7 |
|
* @name Tracker::User |
8 |
|
* |
9 |
|
*/ |
10 |
|
|
11 |
|
|
12 |
// ----------------------------------------------------------------------------- |
// ----------------------------------------------------------------------------- |
13 |
// $Id$ |
// $Id$ |
14 |
// ----------------------------------------------------------------------------- |
// ----------------------------------------------------------------------------- |
15 |
// $Log$ |
// $Log$ |
16 |
|
// Revision 1.17 2004/06/07 16:54:53 joko |
17 |
|
// modified cache-key |
18 |
|
// added cache-control |
19 |
|
// |
20 |
|
// Revision 1.16 2004/05/06 13:02:09 jonen |
21 |
|
// + added/modified functions related to User-Messages |
22 |
|
// |
23 |
|
// Revision 1.15 2004/03/11 21:04:48 jonen |
24 |
|
// +changed backend-functions to only get/save User ONLY not SOME object (security!) |
25 |
|
// |
26 |
|
// Revision 1.14 2003/07/02 11:02:09 joko |
27 |
|
// + fixed bug with create/_create |
28 |
|
// |
29 |
|
// Revision 1.13 2003/04/04 02:15:09 joko |
30 |
|
// renamed method of helper object |
31 |
|
// |
32 |
|
// Revision 1.12 2003/03/03 21:20:47 joko |
33 |
|
// mungled the namespaces |
34 |
|
// |
35 |
|
// Revision 1.11 2003/02/14 14:22:06 joko |
36 |
|
// + always (re-)tries to connect |
37 |
|
// |
38 |
|
// Revision 1.10 2003/02/13 21:58:39 joko |
39 |
|
// + caching mechanisms more configurable now |
40 |
|
// |
41 |
|
// Revision 1.9 2003/02/09 17:46:26 joko |
42 |
|
// + now utilizing DesignPattern::RemoteProxy and Data::Deep |
43 |
|
// |
44 |
|
// Revision 1.8 2002/12/28 01:16:42 jonen |
45 |
|
// + added clear of $this->state[autologout] at 'hadTimeout()' |
46 |
|
// |
47 |
|
// Revision 1.7 2002/12/23 11:32:28 jonen |
48 |
|
// + added inserting of uservars on '_create' to frontend db(needed to login user after create!) |
49 |
|
// + added hard link to page 'pub/register', TODO: move this out here |
50 |
|
// |
51 |
|
// Revision 1.6 2002/12/18 22:46:06 jonen |
52 |
|
// + added function 'getUserGuid()' |
53 |
|
// (returns user's 'guid' read from database at function 'login()' (now)) |
54 |
|
// + function '_loadData()' loads user remote now via 'getUserGuid()' |
55 |
|
// |
56 |
// Revision 1.5 2002/12/12 21:34:32 joko |
// Revision 1.5 2002/12/12 21:34:32 joko |
57 |
// + fix in 'function update': invalidate data only if it's not for caching purposes |
// + fix in 'function update': invalidate data only if it's not for caching purposes |
58 |
// |
// |
109 |
// |
// |
110 |
// ----------------------------------------------------------------------------- |
// ----------------------------------------------------------------------------- |
111 |
|
|
112 |
|
|
113 |
|
/** |
114 |
|
* This is the User class. |
115 |
|
* |
116 |
|
* @author Andreas Motl <andreas.motl@ilo.de> |
117 |
|
* @package org.netfrag.flib |
118 |
|
* @name Tracker::User |
119 |
|
* |
120 |
|
*/ |
121 |
class User { |
class User { |
122 |
|
|
123 |
var $state; |
var $state; |
152 |
|
|
153 |
|
|
154 |
function _loadData() { |
function _loadData() { |
|
$this->pObject = new ProxyObject($this->state[id]); |
|
|
if ($this->state[id]) { |
|
|
$this->data = $this->pObject->getAttributes(); |
|
|
} |
|
155 |
$this->meta[data_ready] = 1; |
$this->meta[data_ready] = 1; |
156 |
|
//$this->pObject = new ProxyObject($this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1) ); |
157 |
|
$rpcinfo = $this->site->configuration->get("rpcinfo"); |
158 |
|
//$this->pObject = mkObject('DesignPattern::RemoteProxy', $this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) ); |
159 |
|
$cache_key = $this->getUserGuid() . "_user"; |
160 |
|
$this->pObject = mkObject('DesignPattern::RemoteProxy', $cache_key, array( key => 1, command => 'getUser', query => $this->getUserGuid(), remote => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) ); |
161 |
|
if ($this->getUserGuid()) { |
162 |
|
$this->data = $this->pObject->getResult(); |
163 |
|
} |
164 |
} |
} |
165 |
|
|
166 |
function _saveData($newRawData) { |
function _saveData($newRawData) { |
167 |
|
|
168 |
|
//print "newRawData: " . Dumper($newRawData) . "<br>"; |
169 |
foreach ($newRawData as $key => $val) { |
foreach ($newRawData as $key => $val) { |
170 |
//print "$key => $val<br>"; |
//print "$key => $val<br>"; |
171 |
$this->set($key, $val); |
$this->set($key, $val); |
179 |
|
|
180 |
function _save($type = '') { |
function _save($type = '') { |
181 |
//print Dumper($this->data); |
//print Dumper($this->data); |
182 |
$this->pObject->save($this->data, $type); |
//$this->pObject->save($this->data, $type); |
183 |
|
// new of 2004-03-08: don't use generic 'saveObject'!! |
184 |
|
// for security reasons disabled at ClientBackend!!! |
185 |
|
if($type == 'commit') { |
186 |
|
$args = array( 'guid' => $this->data[guid], 'data' => $this->data ); |
187 |
|
$this->pObject->backend->send('saveUser', $args, array( utf8 => 1) ); |
188 |
|
$this->_save_local(); |
189 |
|
$this->refresh(); |
190 |
|
} |
191 |
// invalidate data only if it's not for caching purposes |
// invalidate data only if it's not for caching purposes |
192 |
if ($type != 'cache') { |
if ($type != 'cache') { |
193 |
$this->meta[data_ready] = 0; |
$this->meta[data_ready] = 0; |
194 |
} |
} |
195 |
} |
} |
196 |
|
|
197 |
|
function _save_local() { |
198 |
|
connectdb(); |
199 |
|
$username = $this->get('userData.username'); |
200 |
|
$password = $this->get('userData.password'); |
201 |
|
$sql = "UPDATE td_users SET pass='$password' WHERE uname='$username'"; |
202 |
|
$res = send_sql($sql); |
203 |
|
} |
204 |
|
|
205 |
function save() { |
function save() { |
206 |
$this->_doDataReady(); |
$this->_doDataReady(); |
209 |
|
|
210 |
function create() { |
function create() { |
211 |
$this->_save(); |
$this->_save(); |
212 |
$attr = $this->pObject->getAttributes(); |
$attr = $this->pObject->getResult(); |
213 |
//print DumpVar($attr); |
//print DumpVar($attr); |
214 |
$this->_create($attr); |
$this->_create($attr); |
215 |
} |
} |
216 |
|
|
217 |
|
function createGuest() { |
218 |
|
if($this->isLoggedOn) { return; } |
219 |
|
//$this->_save(); |
220 |
|
//$attr = $this->pObject->getResult(); |
221 |
|
//print DumpVar($attr); |
222 |
|
$data = $this->_createGuest(); |
223 |
|
return $data; |
224 |
|
} |
225 |
|
|
226 |
function _create($result) { |
function _create($result) { |
227 |
|
global $site; |
228 |
//print "saving to backend: " . dumpVar($result) . "<br>"; |
//print "saving to backend: " . dumpVar($result) . "<br>"; |
229 |
$response = $this->pObject->backend->send('createUser', array('data' => $result) ); |
$response = $this->pObject->backend->send('createUser', array('data' => $result) ); |
230 |
$objectId = $response[oid]; |
$objectId = $response[oid]; |
231 |
|
if($objectId) { |
232 |
|
connectdb(); |
233 |
|
$guid = $response[guid]; |
234 |
|
$username = $this->get('userData.username'); |
235 |
|
$password = $this->get('userData.password'); |
236 |
|
$sql = "INSERT into td_users VALUES ( '$objectId', '$guid', '', '$username', '$password','' )"; |
237 |
|
$res = send_sql($sql); |
238 |
|
} |
239 |
|
else { |
240 |
|
// TODO: handle this with a generic rpc-debug/pending function |
241 |
|
$site->session->set('register_error', 'rpc_error'); |
242 |
|
$site->redirect( getlink('/pub/register/') ); |
243 |
|
} |
244 |
//$this->_init($objectId); |
//$this->_init($objectId); |
245 |
} |
} |
246 |
|
|
247 |
function _getHKR($key) { |
function _createGuest() { |
248 |
$attr_arr = split('\.', $key); |
global $site; |
249 |
$attr_hkr = '["' . join('"]["', $attr_arr) . '"]'; |
//print "saving to backend: " . dumpVar($result) . "<br>"; |
250 |
return $attr_hkr; |
if(!$this->pObject) { |
251 |
|
$rpcinfo = $this->site->configuration->get("rpcinfo"); |
252 |
|
$pObject = mkObject('DesignPattern::RemoteProxy', 'guest', array( key => 1, command => 'createGuestUser', '', remote => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) ); |
253 |
|
$response = $pObject->getResult(); |
254 |
|
} else { |
255 |
|
$response = $this->pObject->backend->send('createGuestUser'); |
256 |
|
} |
257 |
|
//print "response: " . Dumper($response) . "<br>"; |
258 |
|
$objectId = $response[oid]; |
259 |
|
if($objectId) { |
260 |
|
return $response; |
261 |
|
} |
262 |
|
else { |
263 |
|
// TODO: handle this with a generic rpc-debug/pending function |
264 |
|
$site->session->set('login_error', 'rpc_error'); |
265 |
|
$site->redirect( getlink('/pub/login/') ); |
266 |
|
} |
267 |
|
//$this->_init($objectId); |
268 |
} |
} |
269 |
|
|
270 |
function get($attr) { |
function get($attr) { |
271 |
$this->_doDataReady(); |
$this->_doDataReady(); |
272 |
$hkr = $this->_getHKR($attr); |
$deep = new Data_Deep($this->data); |
273 |
$evalstr = 'return $this->data' . $hkr . ';'; |
return $deep->get($attr); |
|
$val = eval($evalstr); |
|
|
return $val; |
|
274 |
} |
} |
275 |
function set($attr, $val) { |
function set($attr, $val) { |
276 |
$this->_doDataReady(); |
$this->_doDataReady(); |
277 |
$hkr = $this->_getHKR($attr); |
$deep = new Data_Deep($this->data); |
278 |
$evalstr = '$this->data' . $hkr . ' = \'' . $val . '\';'; |
$deep->set($attr, $val); |
|
eval($evalstr); |
|
279 |
} |
} |
280 |
|
|
281 |
function isLoggedOn() { |
function isLoggedOn() { |
287 |
|
|
288 |
//===== function userlogin ========================= |
//===== function userlogin ========================= |
289 |
function login($user,$passwd) { |
function login($user,$passwd) { |
290 |
connectdb(); |
connectdb(); |
291 |
//$sql = "SELECT * FROM td_users WHERE uname='$user'"; |
//$sql = "SELECT * FROM td_users WHERE uname='$user'"; |
292 |
$sql = "SELECT user_oid, uname, pass FROM td_users WHERE uname='$user'"; |
$sql = "SELECT user_oid, guid, uname, pass FROM td_users WHERE uname='$user'"; |
293 |
if ($res=send_sql($sql)) { |
if ($res=send_sql($sql)) { |
294 |
$row = mysql_fetch_array($res,MYSQL_ASSOC); |
$row = mysql_fetch_array($res,MYSQL_ASSOC); |
295 |
if (!is_array($row)) { return 0; } |
if (!is_array($row)) { return 0; } |
296 |
} |
} |
297 |
if ($row[pass] == $passwd) { |
if ($row[pass] == $passwd) { |
298 |
//return $uservars_db; |
//return $uservars_db; |
299 |
// store 1st priority user-data to state |
// store 1st priority user-data to state |
300 |
$this->state[id] = $row[user_oid]; |
$this->state[id] = $row[user_oid]; |
301 |
|
$this->state[guid] = $row[guid]; |
302 |
$this->state[status] = $this->meta[logontoken]; |
$this->state[status] = $this->meta[logontoken]; |
303 |
$this->_saveState(); |
$this->_saveState(); |
304 |
$this->site->log( get_class($this) . "->login ok", LOG_DEBUG ); |
$this->site->log( get_class($this) . "->login ok", PEAR_LOG_NOTICE ); |
305 |
return 1; |
return 1; |
306 |
} else { |
} else { |
307 |
return 0; |
return 0; |
308 |
} |
} |
309 |
} |
} |
310 |
|
|
311 |
|
//===== function guestlogin ========================= |
312 |
|
function loginGuest($data) { |
313 |
|
// store 1st priority user-data to state |
314 |
|
$this->state[id] = $data[oid]; |
315 |
|
$this->state[guid] = $data[guid]; |
316 |
|
$this->state[status] = $this->meta[logontoken]; |
317 |
|
$this->_saveState(); |
318 |
|
$this->site->log( get_class($this) . "->guest-login ok", PEAR_LOG_NOTICE ); |
319 |
|
return 1; |
320 |
|
} |
321 |
|
|
322 |
function exists($string) { |
function exists($string) { |
323 |
connectdb(); |
connectdb(); |
335 |
|
|
336 |
function update($vars) { |
function update($vars) { |
337 |
$this->setPostVars($vars); |
$this->setPostVars($vars); |
338 |
//print Dumper($vars); |
//print Dumper($vars); |
339 |
$data = $this->_transformInputData($vars); |
$data = $this->_transformInputData($vars); |
340 |
|
//print Dumper($data); |
341 |
$this->_saveData($data); |
$this->_saveData($data); |
342 |
} |
} |
343 |
|
|
346 |
$mapping_uservars2data = array_flip($def_mapping[data2uservars]); |
$mapping_uservars2data = array_flip($def_mapping[data2uservars]); |
347 |
foreach ($uservars as $key => $val) { |
foreach ($uservars as $key => $val) { |
348 |
$key_target = $mapping_uservars2data[$key]; |
$key_target = $mapping_uservars2data[$key]; |
349 |
//print "$key => $key_target: $val<br>"; |
if($key_target != '') { |
350 |
$target[$key_target] = $val; |
//print "$key => $key_target: $val<br>"; |
351 |
|
$target[$key_target] = $val; |
352 |
|
} |
353 |
} |
} |
354 |
return $target; |
return $target; |
355 |
} |
} |
359 |
global $site; |
global $site; |
360 |
|
|
361 |
// get information about user from site |
// get information about user from site |
362 |
$userid = $this->state[id]; |
$userid = $this->getUserId(); |
363 |
|
|
364 |
$this->site->log( get_class($this) . "->logout: userid $userid", LOG_DEBUG ); |
$this->site->log( get_class($this) . "->logout: userid $userid", PEAR_LOG_NOTICE ); |
365 |
|
|
366 |
// get information about session from php |
// get information about session from php |
367 |
$session_id = session_id(); |
$session_id = session_id(); |
410 |
} |
} |
411 |
|
|
412 |
function refresh() { |
function refresh() { |
413 |
if ($this->pObject) {$this->pObject->flushState();} |
if ($this->pObject) {$this->pObject->flush();} |
414 |
$this->meta[data_ready] = 0; |
$this->meta[data_ready] = 0; |
415 |
} |
} |
416 |
|
|
438 |
return $this->state[id]; |
return $this->state[id]; |
439 |
} |
} |
440 |
|
|
441 |
|
function getUserGuid() { |
442 |
|
return $this->state[guid]; |
443 |
|
} |
444 |
|
|
445 |
function getPostVars() { |
function getPostVars() { |
446 |
return $this->state[postvars]; |
return $this->state[postvars]; |
456 |
} |
} |
457 |
|
|
458 |
function hadTimeout() { |
function hadTimeout() { |
459 |
if ($this->state[autologout]) { return 1; } |
if ($this->state[autologout]) { $this->state[autologout] = ''; return 1; } |
460 |
|
} |
461 |
|
|
462 |
|
function getNewMessages() { |
463 |
|
$messages = array(); |
464 |
|
$userguid = $this->getUserGuid(); |
465 |
|
connectdb(); |
466 |
|
$sql = "SELECT * FROM td_message WHERE target_guid='$userguid' AND new='1'"; |
467 |
|
if ($res=send_sql($sql)) { |
468 |
|
while($row = mysql_fetch_array($res,MYSQL_ASSOC)) { |
469 |
|
if (!is_array($row)) { return 0; } |
470 |
|
array_push($messages, $row); |
471 |
|
} |
472 |
|
} |
473 |
|
return $messages; |
474 |
|
} |
475 |
|
|
476 |
|
function markNewMessage($mid) { |
477 |
|
connectdb(); |
478 |
|
$sql = "UPDATE td_message SET new='0' WHERE mid='$mid'"; |
479 |
|
if(!$res=send_sql($sql)) { |
480 |
|
return "Message not found!"; |
481 |
|
} |
482 |
} |
} |
483 |
|
|
484 |
} |
} |