I have created a script in Joomla app root folder for creating users which load joomla framework ..
The script works fine with static data (username, password ..) but the task that i have consist on sending those data in a post request from an angular app to that script in joomla.
The problem is the part in my script using joomla doesn't allow processing the data sent. Is there a way to do this ?
Here is the script :
<?php
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$username = $request->username;
$name = $request->ecoleName;
$email = $request->email;
$password = $request->password;
print_r($password);
// Set flag that this is a parent file
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php');
$mainframe = &JFactory::getApplication('site');
$mainframe->initialise();
ini_set('default_charset', 'utf-8');
include('database_settings.php');
if (is_null($email)) {
throw new Exception("Email is required to create new Joomla! user.");
}
$uniqid = uniqid();
if (is_null($name)) {
$name = 'member-' . $uniqid;
}
if (is_null($username)) {
$username = $name;
}
if (is_null($password)) {
$password = $uniqid;
}
try {
// Create new user.
jimport('joomla.application.component.helper');
$params = 'com_users';
$usersParams = JComponentHelper::getParams($params);
$new_user = JFactory::getUser(0);
$config = JComponentHelper::getParams('com_users');
$authorize = &JFactory::getACL();
$defaultUserGroup = $config->get('new_usertype');
if (!$defaultUserGroup) {
$defaultUserGroup = 'Registered';
}
$jdata = array(
"name" => $name,
"username" => $username,
"password" => $password,
"password2" => $password,
"email" => $email,
"sendEmail" => 0,
"groups" => array($defaultUserGroup)
);
print_r($jdata);
// Automatically activate user. Comment this out if you use double
// opt-in method.
$useractivation = $usersParams->get('useractivation');
if ($useractivation === 1) {
jimport('joomla.user.helper');
$jdata['activation'] = JUtility::getHash(JUserHelper::genRandomPassword());
$jdata['block'] = 1; // block the user
} else {
$jdata['block'] = 0; // don't block the user
}
// Write new user to the Joomla! database.
if (!$new_user->bind($jdata)) {
throw new Exception("Could not bind data. Error: " . $new_user->getError());
return false;
}
// Set some initial user values
$new_user->set('id', 0);
$new_user->set('usertype', $defaultUserGroup);
$new_user->set('gid', $authorize->get_group_id('', $defaultUserGroup, 'ARO'));
// If user activation is turned on, we need to set the activation information
$useractivation = $config->get('useractivation');
if ($useractivation == '1') {
jimport('joomla.user.helper');
$new_user->set('activation', JUtility::getHash(JUserHelper::genRandomPassword()));
$new_user->set('block', '1');
}
// If there was an error with registration, set the message and display form
if (!$new_user->save()) {
throw new Exception("Could not save user. Error: " . $new_user->getError());
return false;
}
// Send registration confirmation mail
$password = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
$password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email
UserController::_sendMail($new_user, $password);
// Everything went fine, set relevant message depending upon user activation state and display message
if ($useractivation == 1) {
$message = JText::_('REG_COMPLETE_ACTIVATE');
} else {
$message = JText::_('REG_COMPLETE');
}
// Finally, you can return the new user Id.
$cuser = CFactory::getUser($new_user->id);
return $cuser;
} catch (Exception $ex) {
// consoleLog("could not create user: " . $ex->getMessage());
throw new Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
return false;
}