Modern version of XMPPHP extlib
Original XMPPHP is no longer maintained Therefore I've done some optimizations and imported some commits from birkner and zorn-v forks. None of the forks really looked ready to be adopted...
This commit is contained in:
@@ -1,56 +1,117 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file: XMPPHP Cli example
|
||||
*
|
||||
* @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;
|
||||
*/
|
||||
|
||||
session_start();
|
||||
header('content-type', 'plain/text');
|
||||
// activate full error reporting
|
||||
//error_reporting(E_ALL & E_STRICT);
|
||||
|
||||
include 'XMPPHP/BOSH.php';
|
||||
print "<pre>";
|
||||
require '../vendor/autoload.php';
|
||||
|
||||
#Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports
|
||||
#If this doesn't work, are you running 64-bit PHP with < 5.2.6?
|
||||
$conn = new XMPPHP_BOSH('server.tld', 5280, 'user', 'password', 'xmpphp', 'server.tld', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);
|
||||
$conf = array(
|
||||
'server' => 'talk.google.com',
|
||||
'port' => 5222,
|
||||
'username' => 'username',
|
||||
'password' => 'password',
|
||||
'proto' => 'xmpphp',
|
||||
'domain' => 'gmail.com',
|
||||
'printlog' => true,
|
||||
'loglevel' => XMPPHP\Log::LEVEL_VERBOSE,
|
||||
);
|
||||
|
||||
// Easy and simple for access to variables with their names
|
||||
extract($conf);
|
||||
|
||||
$conn = new XMPPHP\XMPP($server, $port, $username, $password, $proto, $domain, $printlog, $loglevel);
|
||||
$conn->autoSubscribe();
|
||||
|
||||
try {
|
||||
if(isset($_SESSION['messages'])) {
|
||||
foreach($_SESSION['messages'] as $msg) {
|
||||
print $msg;
|
||||
flush();
|
||||
}
|
||||
}
|
||||
$conn->connect('http://server.tld:5280/xmpp-httpbind', 1, true);
|
||||
#while(true) {
|
||||
$payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start'));
|
||||
foreach($payloads as $event) {
|
||||
$pl = $event[1];
|
||||
switch($event[0]) {
|
||||
case 'message':
|
||||
if(!isset($_SESSION['messages'])) $_SESSION['message'] = Array();
|
||||
$msg = "---------------------------------------------------------------------------------\n{$pl['from']}: {$pl['body']}\n";
|
||||
print $msg;
|
||||
$_SESSION['messages'][] = $msg;
|
||||
flush();
|
||||
$conn->message($pl['from'], $body="Thanks for sending me \"{$pl['body']}\".", $type=$pl['type']);
|
||||
if($pl['body'] == 'quit') $conn->disconnect();
|
||||
if($pl['body'] == 'break') $conn->send("</end>");
|
||||
break;
|
||||
case 'presence':
|
||||
print "Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n";
|
||||
break;
|
||||
case 'session_start':
|
||||
print "Session Start\n";
|
||||
$conn->getRoster();
|
||||
$conn->presence($status="Cheese!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
#}
|
||||
} catch(XMPPHP_Exception $e) {
|
||||
if (isset($_SESSION['messages'])) {
|
||||
foreach ($_SESSION['messages'] as $message) {
|
||||
echo $message;
|
||||
flush();
|
||||
}
|
||||
} else {
|
||||
$_SESSION['messages'] = array();
|
||||
}
|
||||
|
||||
$conn->connect('http://server.tld:5280/xmpp-httpbind', 1, true);
|
||||
$events = array('message', 'presence', 'end_stream', 'session_start', 'vcard');
|
||||
$payloads = $conn->processUntil($events);
|
||||
|
||||
foreach ($payloads as $result) {
|
||||
list($event, $data) = $result;
|
||||
|
||||
if (isset($data)) {
|
||||
extract($data);
|
||||
}
|
||||
|
||||
switch ($event) {
|
||||
|
||||
case 'message':
|
||||
|
||||
if (!$body) {
|
||||
break;
|
||||
}
|
||||
|
||||
$cmd = explode(' ', $body);
|
||||
$msg = str_repeat('-', 80);
|
||||
$msg .= "\nMessage from: $from\n";
|
||||
|
||||
if (isset($subject)) {
|
||||
$msg .= "Subject: $subject\n";
|
||||
}
|
||||
|
||||
$msg .= $body . "\n";
|
||||
$msg .= str_repeat('-', 80);
|
||||
echo "<pre>$msg</pre>";
|
||||
|
||||
if (isset($cmd[0])) {
|
||||
if ($cmd[0] == 'quit') {
|
||||
$conn->disconnect();
|
||||
}
|
||||
|
||||
if ($cmd[0] == 'break') {
|
||||
$conn->send('</end>');
|
||||
}
|
||||
}
|
||||
$_SESSION['messages'][] = $msg;
|
||||
flush();
|
||||
break;
|
||||
|
||||
case 'presence':
|
||||
|
||||
echo "Presence: $from [$show] $status\n";
|
||||
break;
|
||||
|
||||
case 'session_start':
|
||||
|
||||
echo "Session start\n";
|
||||
$conn->getRoster();
|
||||
$conn->presence('Quasar!');
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (XMPPHP\Exception $e) {
|
||||
die($e->getMessage());
|
||||
}
|
||||
|
||||
$conn->saveSession();
|
||||
|
||||
print "</pre>";
|
||||
print "<img src='http://xmpp.org/images/xmpp.png' onload='window.location.reload()' />";
|
||||
?>
|
||||
echo '<img src="http://xmpp.org/images/xmpp.png" onload="window.location.reload()" />';
|
||||
|
Reference in New Issue
Block a user