The simplest way to create a new ejabberd user with PHP

Hello all!

Can sombody please advise... if there exists a nice, cute, simplest and best :) solution... for create an ejabberd user?

Imagine that in-band registration of users is restricted, and user must register tru web-page, enter his/her sex, age (for example), correctly enter image-capture code, choose nick, password...

And after that PHP script will create eJabberD account with given nick and password.

If web-server and eJabber sharing same machine, You have to make connection with PHP to 127.0.0.1 port 5222, and emulate Jabber client with admin credentials and privileges, and then create new account? Parse result and echo it to webpage ?

Is there more elegant solution?

Also this may concern to create prexistent MUC-room....

Two more ways to register an

Two more ways to register an account in ejabberd:

1) Using ejabberdctl. In the system shell:

$ sudo ejabberdctl register tomcat localhost PaSS556y

$ echo $?
0

$ sudo ejabberdctl register tomcat localhost PaSSff
User "tomcat@localhost" already registered at node ejabberd@localhost

$ echo $?
1

$ sudo ejabberdctl register tom jabberrrr.example.org PaSS556y
Can't register user "tom@jabberrrr.example.org" at node ejabberd@localhost: not_allowed

$ echo $?
1

2) Using XML-RPC calls. You would need to install mod_xmlrpc

Thanx for info

very much...

But... can You tell, if there some guide or manual, how to configure mod_xmlrpc for as much secureness as possible :) and maybe, simple example, how to communicate with web-based scripts (e.g. PHP :)

All what I know is already

All what I know is already documented in the README.txt in the mod_xmlrpc directory.

My solution to create users :)

Hello all!

Finally, I managed to choose the most appropriate solution to automatically create users in ejabberd.

As my needs is web... I'd like to create ejabberd users with PHP script.

I have following restrictions:

1. According with my tasks, inband registration is restricted. User should fill registration info on website, than be approved by local admin (this project for children)... and after that ejabberd account should be set up. Only this way.

2. Due to my tasks, new account should be started with some minimum vcard info.

---

During my research... both methods (ejabberdctl and XML-RPC) is not good.

Because... ejabberdctl, when I called it from PHP, as web-server user have a limitation on file access rules... even when I change ejabberdctl permissions, it failed because permissions to DB-files (as I understand).

XML-RPC - is quite complex method... XML in, XML out.... I should write a parser, etc...

And both things - absolutely unable to create a vcard entries on behalf of new user.

So... I choosed another way - to emulate jabber-client with one Jabber-client class.
I used a "Jabber Client Library" - Version 0.9rc2 - Copyright 2002-2007, Centova Technologies Inc.

So... before use my solution, someone should download it from somewhere :)

Also... to deny inband registration to everyone, but admins (accounts set in ACL as "admin") you should:

%% {access, register, [{allow, all}]}.
{access, register, [{allow, admin}, {deny, all}]}.

And... finally... the PHP script to create new user and fill vcard:

// set your Jabber server hostname, username, and password here
define('JABBER_SERVER','mycoolserver');
define('JABBER_USERNAME','cooladmin');
define('JABBER_PASSWORD','coolpassword');

define('RUN_TIME',5); // set a maximum run time of 5 seconds
define('CBK_FREQ',1); // fire a callback event every second

// This class handles events fired by the first call of CommandJabber client class (to create a user);

class AddMessenger
{

function AddMessenger(&$jab,$name,$pass)
{
$this->jab = &$jab;
$this->jab->NewUserName = $name;
$this->jab->NewUserPass = $pass;
}

// called when a connection to the Jabber server is established
function handleConnected()
{
global $AddUserErrorCode;
$AddUserErrorCode=12002;
// now that we're connected, tell the Jabber class to login
$this->jab->login(JABBER_USERNAME,JABBER_PASSWORD);

}

// called after a login to indicate the the login was successful
function handleAuthenticated()
{
global $AddUserErrorCode;
$AddUserErrorCode=12003;
$this->jab->adduser_init();
}

}
// End of AddMessenger class

/******************************************************************************************************/

// Here is class to handle second call to CommandJabber clase - to fill out vcard

class AddVcard
{

function AddVcard(&$jab,$name,$pass,$firstn,$lastn,$patro,$sex,$role)
{
$this->jab = &$jab;
$this->jab->NewUserName = $name;
$this->jab->NewUserPass = $pass;
$this->GivenName = iconv('CP1251','UTF-8',$firstn); // conversion from russian charset :)
$this->FamilyName = iconv('CP1251','UTF-8',$lastn);
$this->MiddleName = iconv('CP1251','UTF-8',$patro);
}

function handleConnected()
{
global $AddVcardErrorCode;
$AddVcardErrorCode=14002;
$this->jab->login($this->jab->NewUserName,$this->jab->NewUserPass);
}

function handleAuthenticated()
{
global $AddVcardErrorCode;
$AddVcardErrorCode=14003;
$this->jab->addvcard_request($this->GivenName, $this->FamilyName, $this->MiddleName, $this->UserRole);
}

} // End of AddVcard class

/******************************************************************************************************/

// Including original "Jabber Client Library" - class
require_once($_SERVER['DOCUMENT_ROOT'].'/../sinc/jabber/class_Jabber.php');

/******************************************************************************************************/

// This is extension to basic Jabber class

class CommandJabber extends Jabber
{
var $AddUserDialogID=0;
var $NewUserName, $NewUserPass;

function adduser_init()
{
$this->AddUserDialogID = $this->_unique_id('adduserproc');

$this->_set_iq_handler('_on_adduser_initanswer',$this->AddUserDialogID);

$xml = '<iq from="'.($this->jid).'" id="'.$this->AddUserDialogID.'" to="'.($this->_server_host).'" type="set">
<command xmlns="http://jabber.org/protocol/commands" action="execute" node="http://jabber.org/protocol/admin#add-user"/>
</iq>';
return $this->_send($xml);
}

function _on_adduser_initanswer(&$packet)
{
global $AddUserErrorCode;
$AddUserErrorCode=12004;
if ($this->_node($packet,array('iq','@','type'))=='result') // if isn't an error response
{
$AddUserErrorCode=12005;
$sessionid=$this->_node($packet,array('iq','#','command','0','@','sessionid'));
if (strlen($sessionid) && $this->_node($packet,array('iq','#','command','0','@','status'))=='executing') // response seems to be OK
  {
  $AddUserErrorCode=12006;
  $xml='<iq from="'.($this->jid).'" id="'.$this->AddUserDialogID.'" to="'.($this->_server_host).'" type="set"><command xmlns="http://jabber.org/protocol/commands" node="http://jabber.org/protocol/admin#add-user" sessionid="'.$sessionid.'"><x xmlns="jabber:x:data" type="submit">';
  $fieldsnode=$this->_node($packet,array('iq','#','command','0','#','x','0','#','field'));
  $i=0;
  do
    {
$field_type=$this->_node($fieldsnode,array($i,'@','type'));
$field_var=$this->_node($fieldsnode,array($i,'@','var'));
$field_value=$this->_node($fieldsnode,array($i,'#','value','0','#'));

if ($field_type=='hidden') $xml.='<field type="hidden" var="'.$field_var.'"><value>'.$field_value.'</value></field>';
if ($field_var=='accountjid') $xml.='<field type="'.$field_type.'" var="accountjid"><value>'.$this->NewUserName.'@'.$this->_server_host.'</value></field>';
if ($field_var=='password') $xml.='<field type="'.$field_type.'" var="password"><value>'.$this->NewUserPass.'</value></field>';
if ($field_var=='password-verify') $xml.='<field type="'.$field_type.'" var="password-verify"><value>'.$this->NewUserPass.'</value></field>';
$i++;
}
  while (strlen(trim($field_type)) && $i<20);
 
  $xml.='</x></command></iq>';
  $this->_set_iq_handler('_on_adduser_getresult',$this->AddUserDialogID);
  $this->_send($xml);
  }
}
}

function _on_adduser_getresult(&$packet)
{
global $AddUserErrorCode;
$AddUserErrorCode=12007;
if ($this->_node($packet,array('iq','@','type'))=='result')
{
if ($this->_node($packet,array('iq','#','command','0','@','status'))=='completed');
$AddUserErrorCode=0;
}

$this->terminated = true;
}

// following functions - for fill Vcard only

function addvcard_request($GivenName, $FamilyName, $MiddleName)
{
$DialogID = $this->_unique_id('addvcard');

$this->_set_iq_handler('_on_addvcard_reply',$DialogID);

$xml = '<iq from="'.($this->jid).'" id="'.$DialogID.'" type="set">
<vCard xmlns="vcard-temp">
<N><FAMILY>'.$FamilyName.'</FAMILY><GIVEN>'.$GivenName.'</GIVEN><MIDDLE>'.$MiddleName.'</MIDDLE></N>
</vCard>
</iq>';
return $this->_send($xml);
}

function _on_addvcard_reply(&$packet)
{
global $AddVcardErrorCode;
$AddVcardErrorCode=14004;

if ($this->_node($packet,array('iq','@','type'))=='result') $AddVcardErrorCode=0;

$this->terminated = true;
}

} // End of Jabber class extension

/******************************************************************************************************/
// NOW WE START TO USE ALL CLASSES ABOVE :)

// create an instance of the Jabber class
$display_debug_info = false;
$AddUserErrorCode = 12000;
$UserLogin='test100'; $UserPass='test100';
$FirstName='Philip'; $LastName='J.'; $Patronymic='Ivanovich :)';

$jab = new CommandJabber($display_debug_info);
$addmsg = new AddMessenger($jab,$UserLogin,$UserPass);

// set handlers for the events we wish to be notified about
$jab->set_handler("connected",$addmsg,"handleConnected");
$jab->set_handler("authenticated",$addmsg,"handleAuthenticated");
//$jab->set_handler("error",$addmsg,"handleError");

// connect to the Jabber server
if ($jab->connect(JABBER_SERVER))
{
$AddUserErrorCode=12001;
$jab->execute(CBK_FREQ,RUN_TIME);
}

$jab->disconnect();

unset($jab,$addmsg);

echo '<P>******** Exit of User Creation! ErrorCode='.$AddUserErrorCode.' ********</P>';

// If AddUserErrorCode is 0, we can try to fill user's Vcard, using brand new credentials :)

$AddVcardErrorCode = 14000;
$jab = new CommandJabber($display_debug_info);
$avcard = new AddVcard($jab,$UserLogin,$UserPass,$FirstName,$LastName,$Patronymic);

$jab->set_handler("connected",$avcard,"handleConnected");
$jab->set_handler("authenticated",$avcard,"handleAuthenticated");

if ($jab->connect(JABBER_SERVER))
{
$AddVcardErrorCode=14001;
$jab->execute(CBK_FREQ,RUN_TIME);
}

$jab->disconnect();

unset($jab,$avcard);

echo '<P>******** Exit of Add Vcard! ErrorCode='.$AddVcardErrorCode.' ********</P>';

Of course, You can extend this, with class-functions to change user's password... or to delete user completely...

Hope it helps somebody..

I edited this script, delete some important my project specific data... So I can't guarantee, that ready for 100%.

BTW - question to gurus :) - if I, as admin, delete user - all user data (vcard, offline messages) also deleted as well?

All info is deleted, except privacy lists

philipj wrote:

if I, as admin, delete user - all user data (vcard, offline messages) also deleted as well?

Using ejabberd SVN with internal (Mnesia) storage. I created an account, filled some data in several places, and then deleted the account using the Web Admin. Here are the results:

  • last_activity: it is deleted
  • offline_msg: it is deleted
  • passwd: it is deleted, obviously :)
  • privacy: it is NOT deleted!
  • private_storage: it is deleted
  • roster: it is deleted
  • vcard: it is deleted

So, all info is correctly deleted from Mnesia tables except the privacy lists. I've reported this bug here: Privacy lists of a user are not removed when his account is deleted

Doesn't do what it says...

Anyone stumbling across this:

Also... to deny inband registration to everyone, but admins (accounts set in ACL as "admin") you should:

%% {access, register, [{allow, all}]}.
{access, register, [{allow, admin}, {deny, all}]}.

doesn't actually do that. It would simply allow any user to register the 'admin' username.

Hello, I have tried to apply

Hello,
I have tried to apply your code as shown above.But its not working for me. there is showing me that STARTTLS error. so would you like to suggest any other debug points ?

i can't able to create user

i can't able to create user in ejabberd using this php scirpt, whenever i call this script i got this message:

******** Exit of User Creation! ErrorCode=12001 ********

******** Exit of Add Vcard! ErrorCode=14001 ********

my steps are:
1.i install ejabberd 2.1.8 windows installer.
2.i also dowload "Jabber Client Library" - Version 0.9rc2 - Copyright 2002-2007, Centova Technologies Inc
3.i copy your script from http://www.ejabberd.im/node/3126
4.then i made changes to JABBER_SERVER to localhost,JABBER_USERNAME,JABBER_PASSWORD
5. set $UserLogin='test100'; $UserPass='test100'

so please guide me where i m going wrong.also elobrate about the error code.

Syndicate content