2019-06-22 22:23:21 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file: XMPPHP Cli example BOSH
|
|
|
|
*
|
|
|
|
* @info: If this script doesn't work, are you running 64-bit PHP with < 5.2.6?
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activate full error reporting
|
|
|
|
* error_reporting(E_ALL & E_STRICT);
|
|
|
|
*
|
|
|
|
* XMPPHP Log levels:
|
|
|
|
*
|
|
|
|
* LEVEL_ERROR = 0;
|
|
|
|
* LEVEL_WARNING = 1;
|
|
|
|
* LEVEL_INFO = 2;
|
|
|
|
* LEVEL_DEBUG = 3;
|
|
|
|
* LEVEL_VERBOSE = 4;
|
|
|
|
*/
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
$conf = [
|
|
|
|
'host' => 'server.tld',
|
|
|
|
'port' => 5280,
|
|
|
|
'username' => 'username',
|
|
|
|
'password' => 'password',
|
|
|
|
'resource' => 'xmpphp',
|
|
|
|
'server' => 'http://server.tld:5280/xmpp-httpbind',
|
|
|
|
'print_log' => true,
|
|
|
|
'log_level' => XMPPHP\Log::LEVEL_VERBOSE,
|
|
|
|
];
|
2019-06-22 22:23:21 +01:00
|
|
|
|
|
|
|
// Easy and simple for access to variables with their names
|
|
|
|
extract($conf);
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
$conn = new XMPPHP\BOSH($host, $port, $username, $password, $proto, $server, $print_log, $log_level);
|
2019-06-22 22:23:21 +01:00
|
|
|
$conn->autoSubscribe();
|
|
|
|
|
|
|
|
try {
|
2019-11-16 16:51:01 +00:00
|
|
|
$conn->connect();
|
2019-06-22 22:23:21 +01:00
|
|
|
|
|
|
|
while (!$conn->isDisconnected()) {
|
2019-11-16 16:51:01 +00:00
|
|
|
$events = ['message', 'presence', 'end_stream', 'session_start'];
|
2019-06-22 22:23:21 +01:00
|
|
|
$payloads = $conn->processUntil($events);
|
|
|
|
|
|
|
|
foreach ($payloads as $result) {
|
|
|
|
list($event, $data) = $result;
|
|
|
|
|
|
|
|
if (isset($data)) {
|
|
|
|
extract($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($event) {
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
case 'message':
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
if (!$body) {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
echo str_repeat('-', 80);
|
|
|
|
echo "Message from: $from";
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
if (isset($subject)) {
|
|
|
|
echo "Subject: $subject";
|
|
|
|
}
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
echo $body;
|
|
|
|
echo str_repeat('-', 80);
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
$cmd = explode(' ', $body);
|
|
|
|
$body = "Mi no entender! '$body'";
|
|
|
|
$conn->message($from, $body, $type);
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
if (isset($cmd[0])) {
|
|
|
|
if ($cmd[0] == 'quit') {
|
|
|
|
$conn->disconnect();
|
|
|
|
}
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
if ($cmd[0] == 'break') {
|
|
|
|
$conn->send('</end>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
case 'presence':
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
echo "Presence: $from [$show] $status\n";
|
|
|
|
break;
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
case 'session_start':
|
2019-06-22 22:23:21 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
echo "Session start\n";
|
|
|
|
$conn->getRoster();
|
|
|
|
$conn->presence('Quasar!');
|
|
|
|
break;
|
|
|
|
}
|
2019-06-22 22:23:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (XMPPHP\Exception $e) {
|
|
|
|
die($e->getMessage());
|
|
|
|
}
|