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

register user by form and catch Stanza errors, change user password #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/nbproject/
composer.lock
composer.phar
/phpunit.xml
.idea
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# CHANGELOG

## 0.7.0 (2017-05-23)

- [MAJOR]: [PR #46](https://github.com/fabiang/xmpp/pull/46) Added support for password-protected chatrooms
- [MAJOR]: [PR #44](https://github.com/fabiang/xmpp/pull/44) Added anonymous authentication method
- [MAJOR]: [PR #34](https://github.com/fabiang/xmpp/pull/34) Added support for registereing user
- [MAJOR]: [PR #34](https://github.com/fabiang/xmpp/pull/34) Added vCard support
- [MAJOR]: [PR #34](https://github.com/fabiang/xmpp/pull/34) Added support for blocking and unblocking an user
- [MAJOR]: Drop support for PHP lower than 5.6
- [MAJOR]: [PR #31](https://github.com/fabiang/xmpp/pull/31): Possibility to set context for SocketClient

## 0.6.1 (2014-11-20)

- [PATCH] [Issue #4](https://github.com/fabiang/xmpp/issues/4): Incomplete buffer response
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Simplified BSD License
======================

Copyright 2014 Fabian Grutschus.
Copyright 2014-2017 Fabian Grutschus.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# fabiang/xmpp

[![Latest Stable Version](https://poser.pugx.org/fabiang/xmpp/v/stable.svg)](https://packagist.org/packages/fabiang/xmpp) [![Total Downloads](https://poser.pugx.org/fabiang/xmpp/downloads.svg)](https://packagist.org/packages/fabiang/xmpp) [![Latest Unstable Version](https://poser.pugx.org/fabiang/xmpp/v/unstable.svg)](https://packagist.org/packages/fabiang/xmpp) [![License](https://poser.pugx.org/fabiang/xmpp/license.svg)](https://packagist.org/packages/fabiang/xmpp)
[![Build Status](https://travis-ci.org/fabiang/xmpp.png?branch=master)](https://travis-ci.org/fabiang/xmpp) [![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/fabiang/xmpp/badges/quality-score.png?s=2605ad2bc987ff8501b8f749addff43ec1ac7098)](https://scrutinizer-ci.com/g/fabiang/xmpp/) [![Coverage Status](https://img.shields.io/coveralls/fabiang/xmpp.svg)](https://coveralls.io/r/fabiang/xmpp?branch=master) [![Dependency Status](https://gemnasium.com/fabiang/xmpp.png)](https://gemnasium.com/fabiang/xmpp) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/a535cd82-788d-4506-803e-02ede44a9e74/mini.png)](https://insight.sensiolabs.com/projects/a535cd82-788d-4506-803e-02ede44a9e74)

Library for XMPP protocol connections (Jabber) for PHP.

[![License](https://poser.pugx.org/fabiang/xmpp/license.svg)](https://packagist.org/packages/fabiang/xmpp)
[![Latest Stable Version](https://poser.pugx.org/fabiang/xmpp/v/stable.svg)](https://packagist.org/packages/fabiang/xmpp)
[![Total Downloads](https://poser.pugx.org/fabiang/xmpp/downloads.svg)](https://packagist.org/packages/fabiang/xmpp)
[![Dependency Status](https://gemnasium.com/fabiang/xmpp.svg)](https://gemnasium.com/fabiang/xmpp)
[![Build Status](https://travis-ci.org/fabiang/xmpp.png?branch=master)](https://travis-ci.org/fabiang/xmpp)
[![Coverage Status](https://img.shields.io/coveralls/fabiang/xmpp.svg)](https://coveralls.io/r/fabiang/xmpp?branch=master)
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/fabiang/xmpp/badges/quality-score.png?s=2605ad2bc987ff8501b8f749addff43ec1ac7098)](https://scrutinizer-ci.com/g/fabiang/xmpp/)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/a535cd82-788d-4506-803e-02ede44a9e74/mini.png)](https://insight.sensiolabs.com/projects/a535cd82-788d-4506-803e-02ede44a9e74)

## SYSTEM REQUIREMENTS

- PHP >= 5.3.3
- PHP minimum 5.6 or minimum 7.0
- psr/log
- psr/log-implementation - like monolog/monolog for logging (optional)
- (optional) psr/log-implementation - like monolog/monolog for logging

## INSTALLATION

New to Composer? Read the [introduction](https://getcomposer.org/doc/00-intro.md#introduction). Add the following to your composer file:

```json
{
"require": {
"fabiang/xmpp": "*"
}
}
```bash
composer require fabiang/xmpp
```

## DOCUMENTATION
Expand Down Expand Up @@ -73,6 +75,7 @@ $client->send($message);
// join a channel
$channel = new Presence;
$channel->setTo('channelname@conference.myjabber.com')
->setPassword('channelpassword')
->setNickName('mynick');
$client->send($channel);

Expand All @@ -95,14 +98,14 @@ $client->disconnect();
If you like this library and you want to contribute, make sure the unit-tests and integration tests are running.
Composer will help you to install the right version of PHPUnit and [Behat](http://behat.org/).

composer install --dev
composer install

After that:

./vendor/bin/phpunit -c tests
./vendor/bin/behat --config=tests/behat.yml --strict
./vendor/bin/phpunit
./vendor/bin/behat

New features should allways tested with Behat.
New features should always tested with Behat.

## LICENSE

Expand All @@ -112,5 +115,4 @@ BSD-2-Clause. See the [LICENSE](LICENSE.md).

- Better integration of channels
- Factory method for server addresses
- Add support von vCard
- improve documentation
6 changes: 6 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
default:
paths:
features: %behat.paths.base%/tests/features
bootstrap: %behat.paths.features%/bootstrap
context:
class: Fabiang\Xmpp\Integration\FeatureContext
21 changes: 15 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@
}
},
"require": {
"php": ">=5.3.3",
"psr/log": "~1.0"
"php": "^5.6 || ^7.0",
"psr/log": "^1.0"
},
"require-dev": {
"monolog/monolog": "~1.11",
"phpunit/phpunit": "~4.3",
"behat/behat": "~2.5",
"satooshi/php-coveralls": "~0.6"
"behat/behat": "^2.5.5",
"monolog/monolog": "^1.22",
"phpunit/phpunit": "^5.7.20",
"satooshi/php-coveralls": "^1.0",
"symfony/console": "^2.8",
"symfony/dependency-injection": "^2.8",
"symfony/finder": "^2.8"
},
"suggest": {
"psr/log-implementation": "Allows more advanced logging of the xmpp connection"
Expand All @@ -43,6 +46,12 @@
"dev-master": "1.0.x-dev"
}
},
"config": {
"platform": {
"php": "5.6"
},
"sort-packages": true
},
"archive": {
"exclude": [
".gitignore",
Expand Down
10 changes: 10 additions & 0 deletions examples/config.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
return [
'host' => 'xmpp.nko.home',
'conference' => 'conference.xmpp.nko.home',
'port' => 5222,
'connectionType' => 'tcp',
'verifyPeer' => false,
'login' => 'admin',
'password' => 'pass1view',
];
47 changes: 47 additions & 0 deletions examples/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

require __DIR__ . '/../vendor/autoload.php';
$config = require('config.inc.php');

error_reporting(-1);

use Fabiang\Xmpp\Client;
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 = $config['connectionType'] . '://' . $config['host'] . ':' . $config['port'];


$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();
$client->send(new Roster);
$client->send(new Presence);
$client->send(new Message);

$client->disconnect();
91 changes: 91 additions & 0 deletions examples/register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
64 changes: 64 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!--
/**
* Copyright 2014 Fabian Grutschus. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the copyright holders.
*
* @author Fabian Grutschus <f.grutschus@lubyte.de>
* @copyright 2014 Fabian Grutschus. All rights reserved.
* @license BSD
* @link http://github.com/fabiang/xmpp
*/
-->

<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="true">

<testsuites>
<testsuite name="Xmpp">
<directory>tests/src/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>

<php>
<ini name="error_reporting" value="-1"/>
<ini name="display_errors" value="On"/>
<ini name="log_errors" value="Off"/>
</php>

</phpunit>
Loading