Create ejabberd user from PHP

后端 未结 7 1672
说谎
说谎 2020-12-08 17:37

I need to create an ejabberd user from a PHP script. I also need to be able to add the new user to a predefined shared roster.

Should I just call ejabberdctl<

7条回答
  •  一个人的身影
    2020-12-08 18:21

    I came upon this question in 2016, there are much easier ways to implement this than the accepted answer and the highest voted one.

    1. Use an XMPP PHP library, the most common one being:

    https://github.com/fabiang/xmpp

    1. While this library doesn't support adding a user out of the box, you can very easily extend it

    here is the class I wrote for adding a user:

    use Fabiang\Xmpp\Util\XML;
    
    /**
     * Register new user
     * @param string $username
     * @param string $password
     * @param string $email
     * @package XMPP\Protocol
     * @category XMPP
     */
    class Register implements ProtocolImplementationInterface
    {  
        protected $username;
        protected $password;
        protected $email;
    
        /**
         * Constructor.
         *
         * @param string $username
         * @param string $password
         * @param string $email
         */
        public function __construct($username, $password, $email)
        {
            $this->username = $username;
            $this->password = $password;
            $this->email = $email;
        }
    
        /**
         * Build XML message
         * @return type
         */
        public function toString()
        {
            $query = "%s%s%s";        
            return XML::quoteMessage($query, XML::generateId(), (string) $this->username, (string) $this->password, (string) $this->email);
        }
    }
    
    1. You must enable in-band registration in the ejabberd.cfg file, as it's denied by default:

    {access, register, [{allow, all}]}.

    Finally here is a sample code for using this class:

    private function registerChatUser($name, $password, $email)
        {       
            $address = 'tcp://yourserverip:5222';
            $adminUsername = 'youradmin';
            $adminPassword = 'youradminpassword';
    
            $options = new Options($address);
            $options->setUsername($adminUsername)->setPassword($adminPassword);
    
            $client = new Client($options);         
            $client->connect();             
    
            $register = new Register($name, $password, $email);                 
            $client->send($register);   
    
            $client->disconnect();
        }
    

    The library call will fail if the server does not have a valid SSL certificate. Either place a valid certificate, or replace this part in SocketClient.php with the below snippet

    // call stream_socket_client with custom error handler enabled
    $handler = new ErrorHandler(
        function ($address, $timeout, $flags) {
            $options = [
                'ssl' => [
                    'allow_self_signed' => true,
                    'verify_peer_name' => false,
                ],
            ];
            $context = stream_context_create($options);
            return stream_socket_client($address, $errno, $errstr, $timeout, $flags, $context);
        },
        $this->address,
        $timeout,
        $flags
    );
    

提交回复
热议问题