This repository was archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathregister.php
More file actions
91 lines (76 loc) · 2.63 KB
/
register.php
File metadata and controls
91 lines (76 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = require('config.inc.php');
error_reporting(-1);
use Fabiang\Xmpp\Client;
use Fabiang\Xmpp\Exception\Stream\StanzasErrorException;
use Fabiang\Xmpp\Options;
use Fabiang\Xmpp\Protocol\User\ChangeUserPassword;
use Fabiang\Xmpp\Protocol\User\RegisterUser;
use Fabiang\Xmpp\Protocol\User\RequestChangePasswordForm;
use Fabiang\Xmpp\Protocol\User\RequestUserRegisterForm;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
$logger = new Logger('xmpp');
$logger->pushHandler(new StreamHandler( 'php://stdout', Logger::DEBUG));
$address = $config['connectionType'] . '://' . $config['host'] . ':' . $config['port'];
$newUser = 'testuser';
$newPassword = '123456';
$options = new Options($address);
$options->setLogger($logger)
->setUsername($config['login'])
->setPassword($config['password']);
if ($config['verifyPeer'] === false) {
$options->setContextOptions([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
],
]);
}
$client = new Client($options);
$client->connect();
$request = new RequestUserRegisterForm($config['login'] . '@' . $config['host'], $config['host']);
$client->send($request);
$form = $client->getOptions()->getForm();
$user = new RegisterUser(
$newUser . '@' . $config['host'],
$newPassword,
$config['login'] . '@' . $config['host'],
$config['host'],
$form
);
try {
$client->send($user);
print 'User is registered' . PHP_EOL;
} catch (StanzasErrorException $e) {
/**
* @see https://xmpp.org/extensions/xep-0086.html#sect-idm139696314152720
*/
if ($e->getCode() == StanzasErrorException::ERROR_CONFLICT) { // conflict
fwrite(STDOUT, 'User already exists. Try to change password' . PHP_EOL);
$request = new RequestChangePasswordForm(
$config['login'] . '@' . $config['host'],
$config['host']
);
try {
$client->send($request);
$form = $client->getOptions()->getForm();
$user = new ChangeUserPassword(
$newUser . '@' . $config['host'],
$newPassword,
$config['login'] . '@' . $config['host'],
$config['host'],
$form
);
} catch (StanzasErrorException $e) {
fwrite(STDOUT, 'Failed to change password of user!' . PHP_EOL);
fwrite(STDOUT, $e->getMessage() . PHP_EOL);
}
} else {
fwrite(STDOUT, 'Failed to register user!' . PHP_EOL);
fwrite(STDOUT, $e->getMessage() . PHP_EOL);
}
}
$client->disconnect();