[SOLVED] Creating user through api AJAX

12 years 1 month ago - 12 years 1 month ago #194976 by sfraise
I'm trying to create a new user through the api but it seems to break when it gets to the "if($row->store()" line.

It's passing the params ok as I've echoed them all out within the function before it breaks and all output fine. I've called the external api and all seems fine there. I've tried setting an alert as well as simply echoing a $row->getError() right above the two "return false" lines but I don't get anything. I'm doing this through AJAX and all I get in my console is a 500 internal server error.

NOW...
If I use this function directly on the parent page instead of doing it through the AJAX it works like a champ so something about doing this through AJAX breaks it, maybe the way I'm calling the api is wrong for this scenario? I wish I could just throw this function on the parent page but I have to be able to fire this onclick and doing it through ajax is the only way I know of to get a php function to work this way, but I'm all ears on ideas to fire the function from the parent page.

I tried setting the params in the function call one by one and I get an output of "please enter a last name" if the last name is left blank, and so on and so forth until all of the params are set. I did a check of the has function by echoing cbHashPassword( $password ) and I got a hashed password ok. I also changed the approve and confirm values in the params and confirmed that it was pulling those values ok.

I also echoed $row->firstname and verified that it output the first name so the $row variable seems legit which basically leaves the store() function as the issue here.

If I remove the "if($row->store()" statement I no longer get an error, but of course nothing happens either, just mentioned this to show this is where it breaks. If I echo something right above that statement it outputs, but I get nothing if I do it within or below it.

The scenario here is a logged in member with special permissions can register a new user other than themselves, they complete needed inputs, then an onclick event on a button fires the ajax function that passes the values over to a php file which grabs them and passes them down into the registerUser function. It should create the user, then update a div in the parent window with that new user.

Here's what I've got, I'm under a super tight deadline to have this working (like today lol) so hopefully this can get solved super fast, any help on what the issue is will be GREATLY appreciated lol...
// THE API
/* Initialize Joomla framework */
define( '_JEXEC', 1 );
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] );
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'config.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'base'.DS.'observable.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'base'.DS.'object.php' );

/* Create the Application */
$mainframe =& JFactory::getApplication('site');

global $pagination, $total, $limitstart, $limit;

/* Load CB Foundation */
global $_CB_framework, $mainframe;
 
if ( defined( 'JPATH_ADMINISTRATOR' ) ) {
	if ( ! file_exists( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' ) ) {
		echo 'CB not installed!';
		return;
	}
 
	include_once( JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php' );
} else {
	if ( ! file_exists( $mainframe->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php' ) ) {
		echo 'CB not installed!';
		return;
	}
 
	include_once( $mainframe->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php' );
}
cbimport( 'cb.database' );
cbimport( 'cb.plugins' );
cbimport( 'language.front' );
cbimport( 'cb.snoopy' );
cbimport( 'cb.imgtoolbox' );
cbimport( 'cb.html' );

// THE FUNCTION
function registerUser( $fname, $lname, $email, $username, $password, $approve = 0, $confirm = 0 ) {
	global $_CB_framework, $_CB_database, $ueConfig;
 
	$approval			=	( $approve == 2 ? $ueConfig['reg_admin_approval'] : $approve );
	$confirmation			=	( $confirm == 2 ? $ueConfig['reg_confirmation'] : $confirm );
 	$usertype			=	$_CB_framework->getCfg( 'new_usertype' );
	$row				=	new moscomprofilerUser( $_CB_database );
	$row->usertype			=	( $usertype ? $usertype : 'Registered' );
	$row->gid			=	$_CB_framework->acl->get_group_id( $row->usertype, 'ARO' );
	$row->sendEmail			=	0;
	$row->registerDate		=	date( 'Y-m-d H:i:s', $_CB_framework->now() );
	$row->name			=	$fname . ' ' . $lname;
	$row->firstname			=	$fname;
	$row->lastname			=	$lname;
	$row->username			=	$username;
	$row->email			=	$email;
	$row->password			=	cbHashPassword( $password );
	$row->registeripaddr		=	cbGetIPlist();
	if ( $approval == 0 ) {
		$row->approved		=	1;
	} else {
		$row->approved		=	0;
	}
	if ( $confirmation == 0 ) {
		$row->confirmed		=	1;
	} else {
		$row->confirmed		=	0;
	}
	if ( ( $row->confirmed == 1 ) && ( $row->approved == 1 ) ) {
		$row->block		=	0;
	} else {
		$row->block		=	1;
	}
	if ( $row->store() ) {
		if ( ( $row->confirmed == 0 ) && ( $confirmation != 0 ) ) {
			$row->_setActivationCode();
 
			if ( ! $row->store() ) {
				return false;
			}
		}
		return true;
	}
	return false;
}
registerUser($nmfname, $nmlname, $nmemail, $nmusername, $nmpass, 0, 0);

*********
Solved the issue by setting a task in the main component file and setting the path to the php file with index.php?option=blablabla in order to get within Joomla it's self.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.174 seconds

Facebook Twitter LinkedIn