/[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.2 by joko, Tue Dec 3 16:47:22 2002 UTC revision 1.22 by jonen, Wed Nov 17 16:14:47 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.22  2004/11/17 16:14:47  jonen
17    //    + minor changes related to utf8 encoding
18    //
19    //    Revision 1.21  2004/11/03 14:13:21  jonen
20    //     small changes at create function
21    //
22    //    Revision 1.20  2004/10/07 14:08:51  jonen
23    //    + bufix related to save new-registered user-data
24    //
25    //    Revision 1.19  2004/06/22 09:18:47  jonen
26    //    removed debug dumps
27    //
28    //    Revision 1.17  2004/06/07 16:54:53  joko
29    //    modified cache-key
30    //    added cache-control
31    //
32    //    Revision 1.16  2004/05/06 13:02:09  jonen
33    //    + added/modified functions related to User-Messages
34    //
35    //    Revision 1.15  2004/03/11 21:04:48  jonen
36    //    +changed backend-functions to only get/save User ONLY not SOME object (security!)
37    //
38    //    Revision 1.14  2003/07/02 11:02:09  joko
39    //    + fixed bug with create/_create
40    //
41    //    Revision 1.13  2003/04/04 02:15:09  joko
42    //    renamed method of helper object
43    //
44    //    Revision 1.12  2003/03/03 21:20:47  joko
45    //    mungled the namespaces
46    //
47    //    Revision 1.11  2003/02/14 14:22:06  joko
48    //    + always (re-)tries to connect
49    //
50    //    Revision 1.10  2003/02/13 21:58:39  joko
51    //    + caching mechanisms more configurable now
52    //
53    //    Revision 1.9  2003/02/09 17:46:26  joko
54    //    + now utilizing DesignPattern::RemoteProxy and Data::Deep
55    //
56    //    Revision 1.8  2002/12/28 01:16:42  jonen
57    //    + added clear of $this->state[autologout] at 'hadTimeout()'
58    //
59    //    Revision 1.7  2002/12/23 11:32:28  jonen
60    //    + added inserting of uservars on '_create' to frontend db(needed to login user after create!)
61    //      + added hard link to page 'pub/register', TODO: move this out here
62    //
63    //    Revision 1.6  2002/12/18 22:46:06  jonen
64    //    + added function 'getUserGuid()'
65    //       (returns user's 'guid' read from database at function 'login()'  (now))
66    //    + function '_loadData()' loads user remote now via 'getUserGuid()'
67    //
68    //    Revision 1.5  2002/12/12 21:34:32  joko
69    //    + fix in 'function update': invalidate data only if it's not for caching purposes
70    //
71    //    Revision 1.4  2002/12/06 04:10:28  joko
72    //    + replaced 'xyzCache' through 'xyzProxy'
73    //
74    //    Revision 1.3  2002/12/04 07:42:54  jonen
75    //    + changes according to backend schema change
76    //
77  //    Revision 1.2  2002/12/03 16:47:22  joko  //    Revision 1.2  2002/12/03 16:47:22  joko
78  //    - function preLogout()  //    - function preLogout()
79  //    + function preLogout($autologout = 0)  //    + function preLogout($autologout = 0)
# Line 50  Line 121 
121  //  //
122  //    -----------------------------------------------------------------------------  //    -----------------------------------------------------------------------------
123    
124    
125    /**
126     * This is the User class.
127     *
128     * @author Andreas Motl <andreas.motl@ilo.de>
129     * @package org.netfrag.flib
130     * @name Tracker::User
131     *
132     */
133  class User {  class User {
134        
135    var $state;    var $state;
# Line 84  class User { Line 164  class User {
164    
165    
166    function _loadData() {    function _loadData() {
     $this->pObject = new ProxyObject($this->state[id]);  
     if ($this->state[id]) {  
       $this->data = $this->pObject->getAttributes();  
     }  
167      $this->meta[data_ready] = 1;      $this->meta[data_ready] = 1;
168        //$this->pObject = new ProxyObject($this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1) );
169        $rpcinfo = $this->site->configuration->get("rpcinfo");
170        //$this->pObject = mkObject('DesignPattern::RemoteProxy', $this->getUserGuid(), array( remote => 1, classname => "NetPerson", guid => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) );
171        $cache_key = $this->getUserGuid() . "_user";
172        $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 ) );
173         if ($this->getUserGuid()) {
174          $this->data = $this->pObject->getResult();
175        }
176    }    }
177    
178    function _saveData($newRawData) {    function _saveData($newRawData) {
179    
180        //print "newRawData: " . Dumper($newRawData) . "<br>";
181      foreach ($newRawData as $key => $val) {      foreach ($newRawData as $key => $val) {
182        //print "$key => $val<br>";        //print "$key => $val<br>";
183        $this->set($key, $val);        $this->set($key, $val);
184      }      }
185    
186      $this->_save("cache");      $this->_save("cache");
187        //$this->_save("commit");
188            
189      //$this->pObject->flushCache();     // done in "pObject->save"      //$this->pObject->flushProxy();     // done in "pObject->save"
190            
191    }    }
192        
193    function _save($type = '') {    function _save($type = '') {
194      $this->pObject->save($this->data, $type);  //print Dumper($this->data);
195      $this->meta[data_ready] = 0;      //$this->pObject->save($this->data, $type);
196        // new of 2004-03-08: don't use generic 'saveObject'!!
197        //   for security reasons disabled at ClientBackend!!!
198        if($type == 'commit') {
199          $args = array( 'guid' => $this->data[guid], 'data' => $this->data );
200          $this->pObject->backend->send('saveUser', $args, array( utf8 => 1) );
201          $this->_save_local();
202          $this->refresh();
203        }
204        // invalidate data only if it's not for caching purposes
205        if ($type != 'cache') {
206          $this->meta[data_ready] = 0;
207        }
208      }
209    
210      function _save_local() {
211            connectdb();
212            $username = $this->get('userData.username');
213            $password = $this->get('userData.password');
214            $sql = "UPDATE td_users SET pass='$password' WHERE uname='$username'";
215                $res = send_sql($sql);
216    }    }
217        
218    function save() {    function save() {
# Line 115  class User { Line 221  class User {
221    }    }
222        
223    function create() {    function create() {
224     $this->_save();      // How THIS can function??? we will fool ourselves...
225     $attr = $this->pObject->getAttributes();     //$this->_save();
226       //$attr = $this->pObject->getResult();
227       $this->_create($this->data);
228      }
229    
230      function createGuest() {
231        if($this->isLoggedOn) { return; }
232       //$this->_save();
233       //$attr = $this->pObject->getResult();
234     //print DumpVar($attr);     //print DumpVar($attr);
235     $this->_create($attr);     $data = $this->_createGuest();
236       return $data;
237    }    }
238    
239    function _create($result) {    function _create($result) {
240        global $site;
241      //print "saving to backend: " . dumpVar($result) . "<br>";      //print "saving to backend: " . dumpVar($result) . "<br>";
242      $response = $this->pObject->backend->send('createUser', array('data' => $result) );      $response = $this->pObject->backend->send('createUser', array('data' => $result), array( utf8 => 1) );
243      $objectId = $response[oid];      $objectId = $response[oid];
244        if($objectId) {
245            connectdb();
246            $guid = $response[guid];
247            $username = $this->get('userData.username');
248            $password = $this->get('userData.password');
249            $sql = "INSERT into td_users VALUES ( '$objectId', '$guid', '', '$username', '$password','' )";
250                $res = send_sql($sql);
251              }
252              else {
253                // TODO: handle this with a generic rpc-debug/pending function
254          $site->session->set('commit_error', array( form => getlt('page/register/notify/rpc_error') ));
255          $site->redirect( getlink('/pub/register/') );
256              }
257      //$this->_init($objectId);      //$this->_init($objectId);
258    }    }
259    
260    function _getHKR($key) {    function _createGuest() {
261      $attr_arr = split('\.', $key);      global $site;
262      $attr_hkr = '["' . join('"]["', $attr_arr) . '"]';      //print "saving to backend: " . dumpVar($result) . "<br>";
263      return $attr_hkr;      if(!$this->pObject) {
264          $cache_key = php::CreateGUID() . "_guest";
265          $rpcinfo = $this->site->configuration->get("rpcinfo");
266          $pObject = mkObject('DesignPattern::RemoteProxy', $cache_key, array( key => 1, command => 'createGuestUser', '', remote => 1, rpcinfo => $rpcinfo, cache => array( db => 1, session => 1 ), connect => 1 ) );
267          $response = $pObject->getResult();
268        } else {
269          $response = $this->pObject->backend->send('createGuestUser');
270        }
271        //print "response: " . Dumper($response) . "<br>";
272        $objectId = $response[oid];
273        if($objectId) {
274          return $response;
275              }
276              else {
277                // TODO: handle this with a generic rpc-debug/pending function
278          $site->session->set('login_error', 'rpc_error');
279          $site->redirect( getlink('/pub/login/') );
280              }
281        //$this->_init($objectId);
282    }    }
283    
284    function get($attr) {    function get($attr) {
285      $this->_doDataReady();      $this->_doDataReady();
286      $hkr = $this->_getHKR($attr);      $deep = new Data_Deep($this->data);
287      $evalstr = 'return $this->data' . $hkr . ';';      return $deep->get($attr);
     $val = eval($evalstr);  
     return $val;  
288    }    }
289    function set($attr, $val) {    function set($attr, $val) {
290      $this->_doDataReady();      $this->_doDataReady();
291      $hkr = $this->_getHKR($attr);      $deep = new Data_Deep($this->data);
292      $evalstr = '$this->data' . $hkr . ' = \'' . $val . '\';';      $deep->set($attr, $val);
     eval($evalstr);  
293    }    }
294        
295    function isLoggedOn() {    function isLoggedOn() {
# Line 157  class User { Line 301  class User {
301    
302    //===== function userlogin =========================    //===== function userlogin =========================
303    function login($user,$passwd) {    function login($user,$passwd) {
304          connectdb();            connectdb();
305          //$sql = "SELECT * FROM td_users WHERE uname='$user'";            //$sql = "SELECT * FROM td_users WHERE uname='$user'";
306          $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'";
307          if ($res=send_sql($sql)) {            if ($res=send_sql($sql)) {
308                  $row = mysql_fetch_array($res,MYSQL_ASSOC);                  $row = mysql_fetch_array($res,MYSQL_ASSOC);
309                  if (!is_array($row)) { return 0; }                  if (!is_array($row)) { return 0; }
310          }            }
311          if ($row[pass] == $passwd) {            if ($row[pass] == $passwd) {
312        //return $uservars_db;        //return $uservars_db;
313        // store 1st priority user-data to state        // store 1st priority user-data to state
314          $this->state[id] = $row[user_oid];          $this->state[id] = $row[user_oid];
315            $this->state[guid] = $row[guid];
316          $this->state[status] = $this->meta[logontoken];          $this->state[status] = $this->meta[logontoken];
317          $this->_saveState();          $this->_saveState();
318        $this->site->log( get_class($this) . "->login ok", LOG_DEBUG );        $this->site->log( get_class($this) . "->login ok", PEAR_LOG_NOTICE );
319        return 1;        return 1;
320      } else {      } else {
321        return 0;        return 0;
322      }      }
323  }    }
324    
325      //===== function guestlogin =========================
326      function loginGuest($data) {
327          // store 1st priority user-data to state
328            $this->state[id] = $data[oid];
329            $this->state[guid] = $data[guid];
330            $this->state[status] = $this->meta[logontoken];
331            $this->_saveState();
332          $this->site->log( get_class($this) . "->guest-login ok", PEAR_LOG_NOTICE );
333          return 1;
334      }
335    
336    function exists($string) {    function exists($string) {
337          connectdb();          connectdb();
# Line 191  class User { Line 347  class User {
347    }    }
348        
349    
350    function update(&$uservars) {    function update($vars) {
351      $this->setPostVars($uservars);      $this->setPostVars($vars);
352      $data = $this->_transformInputData($uservars);      //print Dumper($vars);
353        $data = $this->_transformInputData($vars);
354        //print Dumper($data);
355      $this->_saveData($data);      $this->_saveData($data);
356    }    }
357    
# Line 202  class User { Line 360  class User {
360      $mapping_uservars2data = array_flip($def_mapping[data2uservars]);      $mapping_uservars2data = array_flip($def_mapping[data2uservars]);
361      foreach ($uservars as $key => $val) {      foreach ($uservars as $key => $val) {
362        $key_target = $mapping_uservars2data[$key];        $key_target = $mapping_uservars2data[$key];
363        //print "$key => $key_target: $val<br>";        if($key_target != '') {
364        $target[$key_target] = $val;          //print "$key => $key_target: $val<br>";
365            $target[$key_target] = $val;
366          }
367      }      }
368      return $target;      return $target;
369    }    }
# Line 213  class User { Line 373  class User {
373      global $site;      global $site;
374    
375      // get information about user from site      // get information about user from site
376        $userid = $this->state[id];        $userid = $this->getUserId();
377    
378        $this->site->log( get_class($this) . "->logout: userid $userid", LOG_DEBUG );        $this->site->log( get_class($this) . "->logout: userid $userid", PEAR_LOG_NOTICE );
379            
380      // get information about session from php      // get information about session from php
381        $session_id = session_id();        $session_id = session_id();
# Line 247  class User { Line 407  class User {
407    function getAccountSum() {    function getAccountSum() {
408      // store additional user-data to state      // store additional user-data to state
409        if (!$this->state[accountSum]) {        if (!$this->state[accountSum]) {
410          $this->state[accountSum] = $this->get("financeInfo.accountSum");          $this->state[accountSum] = $this->get("account.amount");
411          $this->_saveState();          $this->_saveState();
412        }        }
413        return $this->state[accountSum];        return $this->state[accountSum];
# Line 256  class User { Line 416  class User {
416    function getAccountCurrency() {    function getAccountCurrency() {
417      // store additional user-data to state      // store additional user-data to state
418        if (!$this->state[accountCurrency]) {        if (!$this->state[accountCurrency]) {
419          $this->state[accountCurrency] = $this->get("financeInfo.currency");          $this->state[accountCurrency] = $this->get("account.currency");
420          $this->_saveState();          $this->_saveState();
421        }        }
422        //print "state: " . $this->get("financeInfo.currency") . "<br>";        //print "state: " . $this->get("financeInfo.currency") . "<br>";
# Line 264  class User { Line 424  class User {
424    }    }
425        
426    function refresh() {    function refresh() {
427      if ($this->pObject) {$this->pObject->flushState();}      if ($this->pObject) {$this->pObject->flush();}
428      $this->meta[data_ready] = 0;      $this->meta[data_ready] = 0;
429    }    }
430        
# Line 292  class User { Line 452  class User {
452      return $this->state[id];      return $this->state[id];
453    }    }
454    
455      function getUserGuid() {
456        return $this->state[guid];
457      }
458    
459    function getPostVars() {    function getPostVars() {
460      return $this->state[postvars];      return $this->state[postvars];
# Line 307  class User { Line 470  class User {
470    }    }
471        
472    function hadTimeout() {    function hadTimeout() {
473      if ($this->state[autologout]) { return 1; }      if ($this->state[autologout]) { $this->state[autologout] = ''; return 1; }
474      }
475      
476      function getNewMessages() {
477        $messages = array();
478        $userguid = $this->getUserGuid();
479        connectdb();
480        $sql = "SELECT * FROM td_message WHERE target_guid='$userguid' AND new='1'";
481              if ($res=send_sql($sql)) {
482          while($row = mysql_fetch_array($res,MYSQL_ASSOC)) {
483                        if (!is_array($row)) { return 0; }
484                        array_push($messages, $row);
485          }
486            }
487            return $messages;
488      }
489      
490      function markNewMessage($mid) {
491        connectdb();
492        $sql = "UPDATE td_message SET new='0' WHERE mid='$mid'";
493              if(!$res=send_sql($sql)) {
494                return "Message not found!";
495              }
496    }    }
497    
498  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.22

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