Mysql_User_Db.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:8k
- <?php
- class Mysql_User_Db extends BaseObject {
- var $debug;
- var $user_db;
- Function Mysql_User_Db( $db_config = '') {
- $this->BaseObject( 'Mysql_User_Db' );
- $this->user_db = new Mysql_Db();
- if ( is_object( $db_config ) ) {
- $this->user_db->db = $db_config;
- }
- $this->debug = new Debug;
- $this->debug->prefix = 'Mysql_Db::User_Db';
- $this->debug->Off();
- $this->user_db->debug->Off();
- } /* END Mysql_User_Db() */
- Function InitDbConnection() {
- /* Make sure the database handle is available ( if not try to open it ) */
- if ( $this->user_db->connection_init == false) {
- $retuns = $this->user_db->CreateConnection();
- }
- /* Oh no we failed to open the connection */
- if ( $this->user_db->connection_init == false ) {
- return array( false, 'Not connected' );
- }
- return array( true );
- }
- Function ListAll( $domain_id = '0' ) {
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection - listall' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- if ( $domain_id == '' ) { $domain_id = 0 ; }
- $sql = "
- SELECT
- user_id, user_name, password, login_deny, domain_id, last_update
- FROM
- user_table
- WHERE
- domain_id = $domain_id
- ORDER BY
- user_id
- ";
- $select_all_users = $this->user_db->PrepareSql( $sql );
- $i = 0;
- $ResultArray = array();
- $ResultObj = new User;
- $select_all_users->Exec();
- while(
- list( $user_id, $user_name, $password, $login_deny, $domain_id, $last_update ) =
- $select_all_users->FetchRow()
- ) {
- /* Loop through the output and build the array */
- $ResultObj->user_id = 0;
- $ResultObj->user_name = '';
- $ResultObj->password = '';
- $ResultObj->login_deny = true;
- $ResultObj->user_id = $user_id;
- $ResultObj->user_name = $user_name;
- $ResultObj->password = $password;
- $ResultObj->login_deny = $login_deny;
- $ResultObj->domain_id = $domain_id;
- $ResultObj->last_update = $last_update;
- $ResultArray[ $i ] = $ResultObj;
- $i++;
- }
- return array( true, $i, $ResultArray );
- } /* END List All */
- Function Add( $ThisUser ) {
- /* Sanity check on input */
- if ( $ThisUser->user_name == '' ) {
- return array( false, 'NO user name provided' );
- }
- if ( $ThisUser->password == '' ) {
- return array( false, 'NO password provided' );
- }
- if ( $ThisUser->login_deny == true ) {
- $ThisUser->login_deny = 1;
- } else {
- $ThisUser->login_deny = 0;
- }
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection - Add' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- $insert_user = $this->user_db->PrepareSql( '
- INSERT INTO
- user_table ( user_name, password, login_deny, domain_id, last_update )
- VALUES ( ' .
- "'" . $ThisUser->user_name . "', " .
- "'" . $ThisUser->password . "', " .
- "'" . $ThisUser->login_deny . "', " .
- "'" . $ThisUser->domain_id . "', " .
- "'" . $ThisUser->last_update . "'" .
- ' )'
- );
- list( $ret_val, $reason ) = $insert_user->Exec();
- if ( ! $ret_val ) {
- return array( false, 'Insert to table failed.' );
- } else {
- $ThisUser->user_id = $insert_user->GetInsertId();
- return array( true, $ThisUser );
- }
- } /* END Add() */
- Function Delete( $ThisUser ) {
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection - delete' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- $delete_user = $this->user_db->PrepareSql(
- 'DELETE FROM user_table WHERE ' .
- ' user_id = ' . $ThisUser->user_id .
- ' AND ' .
- ' domain_id = ' . $ThisUser->domain_id
- );
- list( $ret_val, $reason ) = $delete_user->Exec();
- if ( ! $ret_val ) {
- return array( false, 'Failed to delete uid : ' . $UserObject->user_id );
- } else {
- return array( true, $ThisUser );
- }
- } /* END Delete */
- Function Get( $ThisUser ) {
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection - get' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- /* Build up the query string */
- $query_string = '';
- $query_string .= '
- SELECT
- user_id, user_name, password, login_deny, domain_id, last_update
- FROM
- user_table
- WHERE
- domain_id = ' . $ThisUser->domain_id . ' ';
- if ( $ThisUser->user_id != '' && $ThisUser->user_id != -1 ) {
- $query_string .= ' AND user_id = ' . $ThisUser->user_id;
- }
- $query_string .= ' AND user_name = "' . $ThisUser->user_name . '"';
- if ( $ThisUser->password != undef ) {
- $query_string .= ' AND password = "' . $ThisUser->password . '"';
- }
- $query_string .= ';';
- $this->debug->Message( $query_string );
- $get_user_info = $this->user_db->PrepareSql( $query_string );
- $get_user_info->Exec();
- $ResultObject = new User;
- if ( ! (
- list(
- $ResultObject->user_id,
- $ResultObject->user_name,
- $ResultObject->password,
- $ResultObject->login_deny,
- $ResultObject->domain_id,
- $ResultObject->last_update )
- = $get_user_info->FetchRow() ) ){
- $this->debug->Message( 'No Match found' );
- $this->debug->Message( $user_id );
- return array( false, 'NO Match found.' );
- }
- /*
- if ( $ResultObject->password == $ThisUser->password ) {
- return array( true, $ResultObject );
- } else {
- return array( false, $ResultObject );
- }
- if ( $ResultObject->password == $ThisUser->EncryptPassword() ) {
- return array( true, $ResultObject );
- } else {
- return array( false, $ResultObject );
- }
- */
- return array( true, $ResultObject );
- //return array( false, 'NO Match found.' );
- } /* END Get() */
- Function Modify( $ThisUser ) {
- if ( $ThisUser->user_id == '' ) {
- return array( false, 'Please provide a user id.' );
- }
- /* Make sure the database handle is available */
- /* Init the database connection and bubble up errors */
- $this->debug->Message( 'Init connection - modify' );
- list( $ret_val, $reason ) = $this->InitDbConnection();
- if ( $ret_val == false ) {
- $this->debug->Message( 'connect failed' );
- return array( $ret_val, $reason );
- }
- $query_string = '';
- $query_string =
- '
- UPDATE
- user_table
- SET
- last_update= NOW() ';
- if ( $ThisUser->user_name != '' ) {
- $query_string .= ' , ';
- $query_string .=
- 'user_name = '' . $ThisUser->user_name . '' ';
- }
- if ( $ThisUser->password != '' ) {
- $query_string .= ' , ';
- $query_string .=
- 'password = '' . $ThisUser->password . '' ';
- }
- if ( $ThisUser->login_deny == 0 || $ThisUser->login_deny == 1) {
- $query_string .= ' , ';
- $query_string .=
- 'login_deny = ' . $ThisUser->login_deny . ' ';
- }
- $query_string .=
- 'WHERE user_id = ' .
- $ThisUser->user_id . ' and domain_id = ' . $ThisUser->domain_id
- ;
- $mod_user_info = $this->user_db->PrepareSql( $query_string );
- return array( true, $mod_user_info->Exec() );
- } /* END Modify */
- }
- ?>