0

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;
}

0
0

You could do this more easily via a module that is published on a certain page (in joomla) which listen for such requests. I've scripted something like this already. An important point is to define a "securitytoken" for those requests.

I've played around with and endpoint that returned all joomla articles into an iOS App on request.

After that Ive created a "database changer" module that will do some tasks when triggered by a security token - you can see my solution here: https://github.com/marcorensch/mod_db_data_changer

hope this helps

Edit: I've just seen that you had added the Joomla-1.5 tag to your question i hope you are not still using J1.5?

3
  • It's my first time working with joomla, i don't undertand your solution, can you explicit more or propose me a way to complete my solution, thank you. Yes it's Joomla 1.5, i don't have choice it is an old website and my task consist on just adding users from angular into joomla
    – Yafaa ats
    Jul 2 at 14:08
  • oh wow... omg - I've quick checked if it would be possible to "quickly" adapt my module to J1.5 and yeah its not really possible to do so... Sorry but i HAVE to say - the customer should really put some money aside for a webdev to have Joomla! updated / migrated because the security is not given with such an old version i mean its about 10+ years old... Sorry but i cannot help you further...
    – Marco
    Jul 2 at 14:51
  • @Yafaa ats I would recommend to tell the customer directly and straight that this is not possible with such an old website, what you COULD do is to forgett joomla completly and add the users via custom PHP script directly into the database (also with a security token) into the database - but your are also very limited by the old PHP 5.2 or 5.3 Version J1.5 required... to make this secure is a hard task...
    – Marco
    Jul 2 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.