LONTE SHELL EDITION


Dashboard -*- SHOW INFO -*- HASH identify -*- Config -*- Jumping

You Are Here : /var/www/virtual/tvarditsa.org/classes/
Upload File :
Current File : /var/www/virtual/tvarditsa.org/classes/users.class.php

<?php // _$_ users.class.php _ users _ version 1.1 _ valio _$_ //
if (!defined("_USERS_CLASS_")) {
define("_USERS_CLASS_", 1);

// ---  Configuration   --- //

$Users_Configuration  = array (
  'password_encoding'	=> 'md5', // or 'plain'
  'db_users_table'		=> 'users',
  'db_users_fields'     => array (
    'id'        => 'id',
    'login'     => 'login',
    'password'  => 'password',
    'name'      => 'name',
    'groups'    => 'groups',
  ),
  'data_cache'          => 1,
  'anonymous'           => 'Анонимен',
);

// -  Auto Include ADODB  - //

if (!defined("_ADODB_CLASS_")) {
  $this_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR;
  include ($this_directory . 'adodb.class.php');
}

// --  Class Definition  -- //

  class Users {

// ----  Private Vars  ---- //

    var $_cfg;
    var $_data;
    var $_login;

// ----  Constructor   ---- //

    function Users () {
      $this->__construct();
    }

    function __construct() {
      GLOBAL $Users_Configuration;
      // Load configuration
      $this->_cfg = $Users_Configuration;
      // auto start session
    }

// ---  Public Methods  --- //

    function get_property ($id, $field) {
      GLOBAL $db, $pager_class_query_counter;
      
      $id = (int)$id;
      if (($id == 0) && ($field == $this->_cfg['db_users_fields']['name']))
        return $this->_cfg['anonymous'];
      
      if ($this->_cfg['data_cache'] && isset($this->_data[$id])) {
        if (isset($this->_data[$id][$field])) return $this->_data[$id][$field];
        else return NULL;
      }
      
      $sql = 'SELECT ';
      if ($this->_cfg['data_cache']) $sql .= '*';
      else $sql .= $field;
      $sql .= ' FROM '.$this->_cfg['db_users_table']
           .  ' WHERE '.$this->_cfg['db_users_fields']['id'].' = '.$id;
	  
      $rs = $db->Execute($sql);
      $pager_class_query_counter++;
      
      if (!$rs) return NULL;
      
      if ($this->_cfg['data_cache']) {
        $this->_data[$id] = $rs->fields;
        $this->_login[$rs->fields[$this->_cfg['db_users_fields']['login']]] = $id;
      }
      
      $ret = $rs->fields[$field];
	  $rs->Close();
      
      return $ret;
    }

    function get_properties ($id) {
      GLOBAL $db, $pager_class_query_counter;
      
      $id = (int)$id;
      
      if ($this->_cfg['data_cache'] && isset($this->_data[$id]))
        return $this->_data[$id];
      
      $sql = 'SELECT * FROM '.$this->_cfg['db_users_table']
           . ' WHERE '.$this->_cfg['db_users_fields']['id'].' = '.$id;
	  
      $rs = $db->Execute($sql);
      $pager_class_query_counter++;
      
      if ($this->_cfg['data_cache']) {
        $this->_data[$id] = $rs->fields;
        $this->_login[$rs->fields[$this->_cfg['db_users_fields']['login']]] = $id;
      }
      
      $ret = $rs->fields;
      
	  $rs->Close();
      
      return $ret;
    }

    function get_id ($login) {
      GLOBAL $db, $pager_class_query_counter;
      
      if ($this->_cfg['data_cache'] && isset($this->_login[$login]))
        return $this->_login[$login];
      
      $sql  = 'SELECT '.$this->_cfg['db_users_fields']['id'];
      $sql .= ' FROM '.$this->_cfg['db_users_table']
           .  ' WHERE '.$this->_cfg['db_users_fields']['login'].' = '
           . $db->qstr($login);
	  
      $rs = $db->Execute($sql);
      $pager_class_query_counter++;
      
      if ($rs && !$rs->EOF) {
        if ($this->_cfg['data_cache'])
          $this->_login[$login] = $rs->fields[$this->_cfg['db_users_fields']['id']];
        return $rs->fields[$this->_cfg['db_users_fields']['id']];
      }
      
      if ($this->_cfg['data_cache']) $this->_login[$login] = 0;
      
      return 0;
    }

    function verify_password ($login, $password) {
      $id = $this->get_id ($login);
      if ($id <= 0) return false;
      $oldpass = $this->get_property ($id, $this->_cfg['db_users_fields']['password']);
      if ($this->_cfg['password_encoding'] == 'md5')
	    $password = md5($password);
      if ($password == $oldpass) return true;
      return false;
    }

    function is_member_of ($id, $group) {
	  $grp = explode(",", trim($group));
	  $groups = explode(",", trim($this->get_property ($id, 'groups')));
	  foreach ($groups as $g) {
	    foreach ($grp as $gp)
	      if (trim($gp) == trim($g)) return true;
	  }
	  return false;
    }

    function get_prop ($login, $prop = NULL) {
      $id = $this->get_id ($login);
      if ($prop) return $this->get_property ($id, $prop);
      return $this->get_properties ($id);
    }

// --  Private Methods   -- //

// -----  Destructor  ----- //

	function __destruct() {
	}

  }

} // END USERS_CLASS
?>