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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.20

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