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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Tue Nov 12 05:42:31 2002 UTC revision 1.17 by joko, Mon Jun 7 16:54:53 2004 UTC
# Line 1  Line 1 
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
57    //    + fix in 'function update': invalidate data only if it's not for caching purposes
58    //
59    //    Revision 1.4  2002/12/06 04:10:28  joko
60    //    + replaced 'xyzCache' through 'xyzProxy'
61    //
62    //    Revision 1.3  2002/12/04 07:42:54  jonen
63    //    + changes according to backend schema change
64    //
65    //    Revision 1.2  2002/12/03 16:47:22  joko
66    //    - function preLogout()
67    //    + function preLogout($autologout = 0)
68    //    + function hadTimeout()
69    //
70  //    Revision 1.1  2002/11/12 05:42:31  joko  //    Revision 1.1  2002/11/12 05:42:31  joko
71  //    + initial checkin  //    + initial checkin
72  //  //
# Line 45  Line 109 
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;
# Line 79  class User { Line 152  class User {
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);
# Line 95  class User { Line 173  class User {
173    
174      $this->_save("cache");      $this->_save("cache");
175            
176      //$this->pObject->flushCache();     // done in "pObject->save"      //$this->pObject->flushProxy();     // done in "pObject->save"
177            
178    }    }
179        
180    function _save($type = '') {    function _save($type = '') {
181      $this->pObject->save($this->data, $type);  //print Dumper($this->data);
182      $this->meta[data_ready] = 0;      //$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
192        if ($type != 'cache') {
193          $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() {
# Line 111  class User { Line 209  class User {
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() {
# Line 152  class User { Line 287  class User {
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();
# Line 186  class User { Line 333  class User {
333    }    }
334        
335    
336    function update(&$uservars) {    function update($vars) {
337      $this->setPostVars($uservars);      $this->setPostVars($vars);
338      $data = $this->_transformInputData($uservars);      //print Dumper($vars);
339        $data = $this->_transformInputData($vars);
340        //print Dumper($data);
341      $this->_saveData($data);      $this->_saveData($data);
342    }    }
343    
# Line 197  class User { Line 346  class User {
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    }    }
# Line 208  class User { Line 359  class User {
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();
# Line 232  class User { Line 383  class User {
383    
384    }    }
385    
386    function preLogout() {    function preLogout($autologout = 0) {
387      global $site;      global $site;
388      $this->state[status] = '';      $this->state[status] = '';
389        $this->state[autologout] = $autologout;
390      $this->_saveState();      $this->_saveState();
391    }    }
392    
393    function getAccountSum() {    function getAccountSum() {
394      // store additional user-data to state      // store additional user-data to state
395        if (!$this->state[accountSum]) {        if (!$this->state[accountSum]) {
396          $this->state[accountSum] = $this->get("financeInfo.accountSum");          $this->state[accountSum] = $this->get("account.amount");
397          $this->_saveState();          $this->_saveState();
398        }        }
399        return $this->state[accountSum];        return $this->state[accountSum];
# Line 250  class User { Line 402  class User {
402    function getAccountCurrency() {    function getAccountCurrency() {
403      // store additional user-data to state      // store additional user-data to state
404        if (!$this->state[accountCurrency]) {        if (!$this->state[accountCurrency]) {
405          $this->state[accountCurrency] = $this->get("financeInfo.currency");          $this->state[accountCurrency] = $this->get("account.currency");
406          $this->_saveState();          $this->_saveState();
407        }        }
408        //print "state: " . $this->get("financeInfo.currency") . "<br>";        //print "state: " . $this->get("financeInfo.currency") . "<br>";
# Line 258  class User { Line 410  class User {
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        
# Line 286  class User { Line 438  class User {
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];
# Line 300  class User { Line 455  class User {
455      return $this->state[postvars][$var];      return $this->state[postvars][$var];
456    }    }
457        
458      function hadTimeout() {
459        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  }  }
485    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.17

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