Page 1 of 1

Help with a function

Posted: Mon Jan 12, 2009 12:44 am
by KrazyKanuk
I am trying to create a gaming site using the CMS (Content Management System) Drupal (5.14) with mysql (5.0.32) running on the Apache web server (2.2.3). What I want to do is have it so when you create an account on Drupal it will write the information to the drupal database and then write it to a second database (game database). In drupal you do this by creating modules which in most cases is a collection of functions. For the most part I have this working, with the exception that it doesn't take the information collected about the type of game client and write it to the game database. I have asked on the drupal forum but nothing suggested seems to help my situation. Due to my inexperience in php I am unable to find and fix the error. I believe the error is in the "insert" section of the function. I am including the function, if needed I can post the complete file (263 lines).

Code: Select all

 
function thunderfist_user($op, &$edit, &$account, $catagory = NULL) {
  global $user;
  switch( $op ) {
    // The User is Registering
    case 'register':
      // Add a fieldset containing radio buttons
      // to the user registration form.
      $fields['wow_client'] = array(
        '#type' => 'fieldset',
        '#title' => 'WOW Client Selection'
      );
      $fields['wow_client']['decision'] = array(
        '#type' => 'radios',
        '#description' => 'Select your WOW client',
        '#required' => 'TRUE',
        '#default_value' => 1,
        '#options' => array( 'WOW', 'WOW - TBC', 'WOW - WOTLK' )
      );
      return $fields;
      break;
      // Check for duplicate IP's and deny if found.
      case 'validate':
        if( !$user->uid ) {
          $msg = '';
          if( isset( $edit['decision'] ) && $edit['decision'] == '0' ) {
            $client_flags = '0';
          }
          elseif( isset( $edit['decision'] ) && $edit['decision'] == '1' ) {
            $client_flags = '8';
          }
          if( isset( $edit['decision'] ) && $edit['decision'] == '2' ) {
            $client_flags = '44';
          }
        }
      return;
      break;
      // Export form data and create a <span style='color:red;text-decoration:blink' title='Alert a moderator!'>grilled spam</span>
      // Grab password before it's encrypted so we can use SHA1
      case 'insert':
        foreach ( $edit as $key => $value ) {
          switch ( $key ) {
            case 'pass':
              $wowpass = $value;
              break;
            case 'roles':
              if ( $user->uid ) {
                $gm = thunderfist_roles( $value );
              }
              break;
            }
      }
      // Insert account information into the logon database
      db_set_active( 'logon' );
      db_query( "INSERT INTO TFlogon.accounts (acct, login, password, encrypted_password, gm, lastip, email, flags) values (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        $account->uid, $account->name, $wowpass, SHA1(strtoupper(
        $account->name).':'.strtoupper( $wowpass ) ), $gm, $user->hostname,
        $account->mail, $user->flags, $client_flags );
      db_set_active( 'default' );
      db_query( " UPDATE users SET access = %d where uid = %d", time(), $account->uid );
      db_set_active( 'default' );
        break;
      // Update the login, password, banned info, wow client in the logon when user updates drupal information.
      case 'update':
        if( isset( $edit['name'] ) ) {
          $login = $edit['name'];
        }
        if( isset( $edit['pass'] ) && !empty( $edit['pass'] ) ) {
        $newpass = $edit['pass'];
        }
        if( isset( $edit['decision'] ) ) {
          $client_flags = $edit['decision'];
        }
        else {
          db_set_active( 'logon' );
          $newpass = db_result( db_query( "SELECT password FROM TFlogon.accounts where acct = %d", $account->uid ) );
          db_set_active( 'default' );
        }
        if( $edit['status'] ) {
          $banned = 0;
        }
        else {
          $banned = $edit['status'];
        }
        if( isset( $edit['roles'] ) ) {
          $rid = 0;
          foreach( $edit['roles'] as $role ) {
            if( $role > $rid ) {
              $rid = $role;
            }
          }
          $gm = thunderfist_roles( $rid );
        }
        else {
          $rid = db_result( db_query( "SELECT max(rid) from users_roles where uid = %d", $account->uid ) );
          $gm = thunderfist_roles( $rid );
        }
        thunderfist_updateinfo( $login, $newpass, $gm, $banned, $flags, $account->uid );
        break;
        // Delete user account from the logon database
      case 'delete':
        thunderfist_deleteaccount( $account->uid );
        break;
      }
}
 
Any help on this would be greatly appreciated.

Re: Help with a function

Posted: Mon Jan 12, 2009 10:02 am
by jthayne
KrazyKanuk wrote:I am trying to create a gaming site using the CMS (Content Management System) Drupal (5.14) with mysql (5.0.32) running on the Apache web server (2.2.3). What I want to do is have it so when you create an account on Drupal it will write the information to the drupal database and then write it to a second database (game database). In drupal you do this by creating modules which in most cases is a collection of functions. For the most part I have this working, with the exception that it doesn't take the information collected about the type of game client and write it to the game database. I have asked on the drupal forum but nothing suggested seems to help my situation. Due to my inexperience in php I am unable to find and fix the error. I believe the error is in the "insert" section of the function. I am including the function, if needed I can post the complete file (263 lines).

Code: Select all

...
Any help on this would be greatly appreciated.
I was about to recommend you print out the SQL statements onscreen so you can see what they are and make sure they are correct, but as I was formatting the code to put it in here, I noticed a problem with your queries. You are not replacing any values. You have it formatted as an sprintf() function, but do not call the function. Change the following lines as indicated in red:

Code: Select all

 
      db_query([color=red]sprintf([/color]"INSERT INTO TFlogon.accounts (acct, login, password, encrypted_password, gm, lastip, email, flags) values (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        $account->uid, $account->name, $wowpass, SHA1(strtoupper(
        $account->name).':'.strtoupper( $wowpass ) ), $gm, $user->hostname,
        $account->mail, $user->flags, $client_flags )[color=red])[/color];
 
      db_query([color=red]sprintf([/color]"UPDATE users SET access = %d where uid = %d", time(), $account->uid )[color=red])[/color];
 
          $newpass = db_result( db_query([color=red]sprintf([/color]"SELECT password FROM TFlogon.accounts where acct = %d", $account->uid ) )[color=red])[/color];
 
          $rid = db_result( db_query([color=red]sprintf([/color]"SELECT max(rid) from users_roles where uid = %d", $account->uid ) )[color=red])[/color];
 

Re: Help with a function

Posted: Tue Jan 13, 2009 8:33 am
by KrazyKanuk
jthayne wrote:
I was about to recommend you print out the SQL statements onscreen so you can see what they are and make sure they are correct, but as I was formatting the code to put it in here, I noticed a problem with your queries. You are not replacing any values. You have it formatted as an sprintf() function, but do not call the function. Change the following lines as indicated in red:

Code: Select all

 
      db_query([color=red]sprintf([/color]"INSERT INTO TFlogon.accounts (acct, login, password, encrypted_password, gm, lastip, email, flags) values (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        $account->uid, $account->name, $wowpass, SHA1(strtoupper(
        $account->name).':'.strtoupper( $wowpass ) ), $gm, $user->hostname,
        $account->mail, $user->flags, $client_flags )[color=red])[/color];
 
      db_query([color=red]sprintf([/color]"UPDATE users SET access = %d where uid = %d", time(), $account->uid )[color=red])[/color];
 
          $newpass = db_result( db_query([color=red]sprintf([/color]"SELECT password FROM TFlogon.accounts where acct = %d", $account->uid ) )[color=red])[/color];
 
          $rid = db_result( db_query([color=red]sprintf([/color]"SELECT max(rid) from users_roles where uid = %d", $account->uid ) )[color=red])[/color];
 
I think I have found the problem but my inexperience prevents me from fixing it. In your reply you said to change the following that is in red:

Code: Select all

 
db_query([color=red]sprintf([/color]"INSERT INTO TFlogon.accounts (acct, login, password, encrypted_password, gm, lastip, email, flags) values (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        $account->uid, $account->name, $wowpass, SHA1(strtoupper(
        $account->name).':'.strtoupper( $wowpass ) ), $gm, $user->hostname,
        $account->mail, $user->flags, $client_flags )[color=red])[/color];
 
Being new with both drupal and php I could be wrong but all the information I am getting says I don't need the sprintf in the queries and I think that is because drupal has something in it to handle it. I was able to borrow a book on drupal modules "Learning Drupal 6 module development" and in this book I been able to find my errors.

Code: Select all

 
db_query("INSERT INTO TFlogon.accounts (acct, login, password, encrypted_password, gm, lastip, email, flags) values (%d, '%s', '%s', '%s', '%s', '%s', '%s', [color=red]'%s'[/color])",
        $account->uid, $account->name, $wowpass, SHA1(strtoupper(
        $account->name).':'.strtoupper( $wowpass ) ), $gm, $user->hostname,
        $account->mail, [color=red]$user->flags, $client_flags[/color] );
 
The red %s should be either %d or %b and $client_flags is getting unset somewhere between where it gets selected and where it gets written to the database. When it gets written to the database the value is always 0 no matter what is selected. how do I get the value of $client_flags set before it gets written to the database then the code can be changed to:

Code: Select all

 
db_query("INSERT INTO TFlogon.accounts (acct, login, password, encrypted_password, gm, lastip, email, flags) 
  values (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%d')",
  $account->uid, $account->name, $wowpass, 
  SHA1(strtoupper($account->name).':'.strtoupper( $wowpass ) ), 
  $gm, $user->hostname,
  $account->mail, 
  $client_flags );