Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

StanzasErrorException is not triggered #65

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/nbproject/
composer.lock
composer.phar
/.idea
57 changes: 57 additions & 0 deletions examples/avatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
require '../vendor/autoload.php';
$config = require('config.inc.php');
error_reporting(-1);

use Fabiang\Xmpp\Client;
use Fabiang\Xmpp\Options;
use Fabiang\Xmpp\Protocol\User\Avatar;
use Fabiang\Xmpp\Protocol\User\AvatarMetadata;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$logger = new Logger('xmpp');
$logger->pushHandler(new StreamHandler('xmpp.log', Logger::DEBUG));

$address = $config['connectionType'] . '://' . $config['host'] . ':' . $config['port'];

$login = $config['login'];//'testuser';
$password = '123456';


$options = new Options($address);
$options->setLogger($logger)
->setUsername($login)
->setPassword($config['password'])
->setVerifyPeer($config['verifyPeer']);

$client = new Client($options);

$client->connect();

$imagePath = 'avatar.png';

$avatar = new Avatar(
$login . '@' . $config['host'],
$imagePath
);

$url = '';
//$url = 'https://avatar.personal-site.com/64x64/testuser.png';


try {
$client->send($avatar);
// update avatar metadata
$meta = new AvatarMetadata(
$login . '@' . $config['host'],
$imagePath,
$url
);
$client->send($meta);
fwrite(STDOUT, 'Avatar was updated.' . PHP_EOL);
} catch (Exception $e) {
fwrite(STDOUT, 'Failed to update user avatar!' . PHP_EOL);
fwrite(STDOUT, $e->getMessage() . PHP_EOL);
}
$client->disconnect();
Binary file added examples/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions examples/config.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'host' => 'xmpp.site.com',
'conference' => 'conference.xmpp.site.com',
'port' => 5222,
'connectionType' => 'tcp',
'verifyPeer' => true,
'login' => 'admin',
'password' => '123456',
];
29 changes: 12 additions & 17 deletions example.php → examples/example.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
<?php

require 'vendor/autoload.php';
require '../vendor/autoload.php';
$config = require('config.inc.php');
error_reporting(-1);

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Fabiang\Xmpp\Options;
use Fabiang\Xmpp\Client;

use Fabiang\Xmpp\Protocol\Roster;
use Fabiang\Xmpp\Protocol\Presence;
use Fabiang\Xmpp\Options;
use Fabiang\Xmpp\Protocol\Message;
use Fabiang\Xmpp\Protocol\Presence;
use Fabiang\Xmpp\Protocol\Roster;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$logger = new Logger('xmpp');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));

$hostname = 'localhost';
$port = 5222;
$connectionType = 'tcp';
$address = "$connectionType://$hostname:$port";
$logger->pushHandler(new StreamHandler('xmpp.log', Logger::DEBUG));

$username = 'xmpp';
$password = 'test';
$address = $config['connectionType'] . '://' . $config['host'] . ':' . $config['port'];

$options = new Options($address);
$options->setLogger($logger)
->setUsername($username)
->setPassword($password);
->setUsername($config['login'])
->setPassword($config['password'])
->setVerifyPeer($config['verifyPeer']);

$client = new Client($options);

Expand Down
47 changes: 47 additions & 0 deletions examples/invitation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
require '../vendor/autoload.php';
$config = require('config.inc.php');
error_reporting(-1);

use Fabiang\Xmpp\Client;
use Fabiang\Xmpp\Exception\Stream\StreamErrorException;
use Fabiang\Xmpp\Options;
use Fabiang\Xmpp\Protocol\Room\DirectInvite;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$logger = new Logger('xmpp');
$logger->pushHandler(new StreamHandler('xmpp.log', Logger::DEBUG));

$address = $config['connectionType'] . '://' . $config['host'] . ':' . $config['port'];

$room = 'new-room';
$password = '';
$newUser = 'testuser';

$options = new Options($address);
$options->setLogger($logger)
->setUsername($config['login'])
->setPassword($config['password'])
->setVerifyPeer($config['verifyPeer']);

$client = new Client($options);

$client->connect();
$invitation = new DirectInvite(
$config['login'] . '@' . $config['host'],
$newUser . '@' . $config['host'],
$room . '@' . $config['conference'],
$password,
'This is a cool party room! Join to it!'
);
try {
$client->send($invitation);

fwrite(STDOUT, 'Invitation sent' . PHP_EOL);
} catch (StreamErrorException $e) {
fwrite(STDOUT, 'Invitation failed' . PHP_EOL);
fwrite(STDOUT, $e->getMessage() . PHP_EOL);
}

$client->disconnect();
80 changes: 80 additions & 0 deletions examples/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
require '../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('xmpp.log', 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'])
->setVerifyPeer($config['verifyPeer']);

$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();
Loading