1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[SOLVED] Having trouble with external registration.

Discussion in 'XenForo Development Discussions' started by Colbydude, Jan 12, 2014.

  1. Colbydude

    Colbydude New Member

    Hey all, I'm having a bit of trouble trying to register a user by external means.

    I'm currently using the class posted in this thread: http://xenforo.com/community/threads/how-to-create-a-bridge.28515/#post-332676, trying to use the createUser function.

    Exact function:
    PHP:
    public static function createUser($sUsername$sEmail$sPassword, array $aAdditionalData = array())
    {
        
    // Create the username from the person's name.
        
    $sUsername str_replace(' '"_"$sUsername);

        
    // Set User Data.
        
    $cWriter XenForo_DataWriter::create('XenForo_DataWriter_User');
        
    $cWriter->set('username'$sUsername);
        
    $cWriter->set('email'$sEmail);
        
    $cWriter->setPassword($sPassword);
        
    $cWriter->set('user_group_id'XenForo_Model_User::$defaultRegisteredGroupId);
        
    $cWriter->set('user_state''email_confirm');
        foreach (
    $aAdditionalData AS $data => $key)
        {
            
    $cWriter->set($data$key);
        }
        
    $cWriter->save();
        
    $cUser $cWriter->getMergedData();

        
    // Login new user: Log the ip of the user registering.
        
    XenForo_Model_Ip::log($cUser['user_id'], 'user'$cUser['user_id'], 'register');
        
    // Set the user back to the browser session.
        
    XenForo_Application::get('session')->changeUserId($cUser['user_id']);
        
    XenForo_Visitor::setup($cUser['user_id']);

        return 
    $cUser['user_id'];
    }
    I'm currently running it using:
    PHP:
    // Create the XenForo User.
    $user_id $xenApp->createUser($_POST["USERNAME"], $_POST["EMAIL"], $_POST["PASSWORD"], array("dob_day"=>$_POST["DAY"], "dob_month"=>$_POST["MONTH"], "dob_year"=>$_POST["YEAR"]));
    Just a simple call using some values after a form has been posted, for testing purposes.

    However I'm getting the following error:
    What I find to be a little confusing is that it's saying that "user_id" doesn't have a default value. But isn't this supposed to be creating a new "user_id" for the created user? Or am I not understanding this correctly?

    Any information on this would be great, thanks.
     
  2. Liam W

    Liam W Well-Known Member

    Yes, it is supposed to, yes that is very confusing!

    Does the same error occur when trying to register inside xenForo?

    Liam
     
  3. Colbydude

    Colbydude New Member

    Ah yes, looks like it does. I haven't noticed that before.

    Here's the stack trace for error from within XenForo:
     
  4. Liam W

    Liam W Well-Known Member

    Very strange...

    Are there any addons installed to xenForo overriding/extending the user datawriter?

    Liam
     
  5. Colbydude

    Colbydude New Member

    No there's no addons being used at all in the forum.

    However I did manually import users from our old system using a custom script before I had discovered the class in the first post.

    Here's the script:
    PHP:
    <?php
        
    //LAST RUN DATE: 01/11/2014, LAST ID: 1157
        
    $conn = new PDO("mysql:host=localhost;dbname=<removed>",'<removed>','<removed>');
        
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        
    $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
        
    $conn->exec("SET NAMES utf8");
      
        function 
    convertToUtf8($string$entities null)
        {
            if(
    preg_match('/[\x80-\xff]/'$string))
            {
                if(
    function_exists('mb_convert_encoding')){ $string mb_convert_encoding($string'utf-8'$this->_charset); }
                else if(
    function_exists('iconv')){ $string = @iconv($this->_charset'utf-8//IGNORE'$string); }
            }

            
    //$string = utf8_unhtml($string, $entities);
            
    $string preg_replace('/[\xF0-\xF7].../'''$string);
            
    $string preg_replace('/[\xF8-\xFB]..../'''$string);
            return 
    $string;
        }
      
        
    $bunchofarrays = array(); $extradata = array();
      
        
    $query $conn->query("SELECT * FROM users");
      
        while(
    $row $query->fetch(PDO::FETCH_ASSOC)){
            
    $bunchofarrays[] = array(
                
    'id' => $row['ID'],
                
    'username' => convertToUtf8($row['USERNAME']),
                
    'email' => convertToUtf8($row['EMAIL']),
                
    'gender' => strtolower($row['GENDER']),
                
    'register_date' => strtotime($row['DATE_REGISTERED']),
                
    'last_activity' => strtotime($row['LAST_DATE']),
                
    'user_group_id' => '2',
                
    'user_state' => 'valid'
            
    );
                  
            
    $parts explode('-'$row['DOB']);
            if(
    count($parts)==3){
                
    $dob_day trim($parts[2]);
                
    $dob_month trim($parts[1]);
                
    $dob_year trim($parts[0]);
            }
          
            
    $extradata[] = array(
                
    'id' => $row['ID'],
                
    'location' => convertToUtf8($row['COUNTRY']),
                
    'dob_day' => $dob_day,
                
    'dob_month' => $dob_month,
                
    'dob_year' => $dob_year
            
    );
        }
      
        
    $conn null;

        
    $conn = new PDO("mysql:host=localhost;dbname=<removed>",'<removed>','<removed>');
        
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        
    $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
        
    $conn->exec("SET NAMES utf8");
      
        echo 
    '<h2>Core data</h2>';
      
        foreach(
    $bunchofarrays as $array){
            
    $array array_values($array);
            echo 
    '<p>Starting to port '.$array[0].'<br />';
            
    $query $conn->prepare("INSERT INTO xf_user(`user_id`,`username`,`email`,`gender`,`register_date`,`last_activity`,`user_group_id`,`user_state`) VALUES(?,?,?,?,?,?,?,?)");
            echo 
    "INSERT INTO xf_user(`user_id`,`username`,`email`,`gender`,`register_date`,`last_activity`,`user_group_id`,`user_state`) VALUES(".$array[0].",".$array[1].",".$array[2].",".$array[3].",".$array[4].",".$array[5].",".$array[6].",".$array[7].")".'<br />';
                
    $query->execute($array);
            
    $query $conn->prepare("INSERT INTO xf_user_authenticate(`user_id`,`scheme_class`) VALUES(?,'XenForo_Authentication_Core12')");
                
    $query->execute(array($array[0]));
            
    $query $conn->prepare("INSERT INTO xf_user_group_relation(`user_id`,`user_group_id`,`is_primary`) VALUES(?,?,?)");
                
    $query->execute(array($array[0],2,1));
            
    $query $conn->prepare("INSERT INTO xf_user_option(`user_id`,`show_dob_year`,`show_dob_date`,`content_show_signature`,`receive_admin_email`,`email_on_conversation`,`is_discouraged`,`enable_rte`,`enable_flash_uploader`) VALUES(?,0,1,1,1,1,0,1,1)");
                
    $query->execute(array($array[0]));
            
    $query $conn->prepare("INSERT INTO xf_user_privacy(`user_id`,`allow_view_profile`,`allow_post_profile`,`allow_send_personal_conversation`,`allow_view_identities`,`allow_receive_news_feed`) VALUES(?,'everyone','everyone','everyone','everyone','everyone')");
                
    $query->execute(array($array[0]));
            echo 
    'Done.</p>';
        }
      
        echo 
    '<h2>Additional data</h2>';
      
        foreach(
    $extradata as $array){
            
    $array array_values($array);
            echo 
    '<p>Starting to port '.$array[0].'<br />';
            
    $query $conn->prepare("INSERT INTO xf_user_profile(`user_id`,`location`,`dob_day`,`dob_month`,`dob_year`) VALUES(?,?,?,?,?)");
                
    $query->execute($array);
            echo 
    'Done.</p>';
        }

        echo 
    'All done!';
    ?>
    Then the user would just have to reset their password in order to log in. Looking at this now, I'd imagine this has something to do with it since I had IDs that already existed and input them into the XF database.
     
  6. Liam W

    Liam W Well-Known Member

    I think you may have fuddled up the internal auto_incremement counter...

    Try truncating the table, and go again.

    Liam
     
  7. Colbydude

    Colbydude New Member

    Ah, that was it! Not sure how the auto-increment got turned off. I must have done it before I ran the script and forgot about it.

    Thanks for the help!
     

Share This Page