<?php
/*
*
*
*
*
*
*/
/**
* See @p StartLine.php @p for details
*/
include_once dirname(dirname(__FILE__)) . "/xml/StartLine.php";
/**
* See @p Element.php @p for details
*/
include_once dirname(dirname(__FILE__)) . "/xml/Element.php";
/**
* See @p Tag.php @p for details
*/
include_once dirname(dirname(__FILE__)) . "/aml/Tag.php";
/**
* See @p SSocket.php @p for details
*/
include_once dirname(dirname(__FILE__)) . "/net/SSocket.php";
/**
* See @p SocketContext.php @p for details
*/
include_once dirname(__FILE__) . "/SocketContext.php";
/**
* See @p SSocketHandler @p for details
*/
include_once dirname(__FILE__) . "/SSocketHandler.php";
/**
*
*
*
*
*/
class XMPPEndpoint {
/*
*
*
*
*
*/
var $targetAddress;
/*
*
*
*
*
*/
var $targetPort;
/*
*
*
*
*
*
*/
var $socket;
/*
*
*
*
*
*
*
*/
var $socketContext;
/*
*
*
*
*
*
*/
var $socketHandler;
/*
*
*
*
*
*/
var $loginID;
/*
*
*
*
*
*
*/
function __construct($targetAddress, $targetPort) {
echo get_class($this)."#__construct(".get_class($targetAddress).", ".get_class($targetPort).")\n";
$this->targetAddress = $targetAddress;
$this->targetPort = $targetPort;
$this->init();
}
/*
*
*
*
*
*/
function init() {
$this->socket = new SSocket("tcp://{$this->targetAddress}:{$this->targetPort}");
}
/*
*
*
*
*
*
*
*/
function connect() {
$this->socket->connect($this->targetAddress, $this->targetPort);
//$sock = $this->socket->getSocket();
$this->socketContext = $this->createSocketContext();
}
function reconnect() {
$this->init();
$this->connect();
}
/*
*
*
*
*
*
*
*
*/
function process() {
$cursor = $this->socketHandler->getCursor();
while (true) {
$firstChild = $cursor->getFirstChild();
//echo "&&&&&&&&&&&&&&&&&&&&&&&&:\n";
//var_dump($cursor);
//stream:features
if ($firstChild != null) {
$name = $firstChild->getName();
$name = strtolower($name);
if ($name == "stream:features") {
break;
}
}
echo "...\n";
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Failure: " . $e->getMessage() . "\n";
return false;
}
}
return true;
}
/*
*
*
*
*
*
*
*/
function toStream() {
$bf = '<stream:stream to="gmail.com" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
//$bf = '<xstream to="gmail.com" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
//$bf = '<stream:stream>';
$outputStream = $this->socket->getOutputStream();
$outputStream->write($bf);
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Failure: " . $e->getMessage() . "\n";
return false;
}
return true;
}
/*
*
*
*
*
*
*
*/
function openStream() {
/*
if (! ($socketContext instanceof SocketContext)) {
throw new Exception("Illegal argument ".get_class($socket));
}
$socket = $socketContext->getSocket();
*/
echo "Open stream...\n";
$b = $this->toStream();
if ($b) {
echo "Success to open stream...Ok\n";
}
$this->process();
return $b;
}
/*
*
*
*
*
*
*
*
*/
function createSocketContext($socket = null) {
if ($socket == null) {
$socket = $this->socket;
}
if ($socket instanceof SSocket) {
$socket = $socket->getSocket();
}
$socketContext = new SocketContext($socket);
$this->socketHandler = new SSocketHandler($socket);
$socketContext->addObserver($this->socketHandler);
return $socketContext;
}
/*
*
*
*
*
*
*
*/
function startTLS() {
/*
if (! ($socketContext instanceof SocketContext)) {
throw new Exception("Illegal argument ".get_class($socket));
}
$socket = $socketContext->getSocket();
*/
echo "Starting TLS...\n";
$bf = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>";
$outputStream = $this->socket->getOutputStream();
$outputStream->write($bf);
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail: " . $e->getMessage() . "\n";
return false;
}
return true;
}
/*
*
*
*
*
*
*
*
*/
function getAuthenticateRequest() {
/*
if (! ($socketContext instanceof SocketContext)) {
throw new Exception("Illegal argument ".get_class($socket));
}
$socket = $socketContext->getSocket();
*/
$this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, true);
$b = $this->toStream();
$this->process();
echo "Attempting Auth...\n";
$auth = base64_encode("\x00"."*********"."\x00"."*********");
$bf = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>".$auth."</auth>";
$outputStream = $this->socket->getOutputStream();
$outputStream->write($bf);
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
return true;
}
function setBind() {
/*
if (! ($socketContext instanceof SocketContext)) {
throw new Exception("Illegal argument ".get_class($socket));
}
$socket = $socketContext->getSocket();
*/
//$this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, false);
//$this->reconnect();
$b = $this->toStream();
$this->process();
echo "Attempting bind...\n";
$bf = '<iq xmlns="jabber:client" type="set" id="1"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>gtalk</resource></bind></iq>';
$outputStream = $this->socket->getOutputStream();
$outputStream->write($bf);
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
/*
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
*/
$cursor = $this->socketHandler->getCurrentNode();
$this->loginID = $cursor->getText();
echo "****************************************************************\n";
//var_dump($cursor);
echo "****************************************************************\n";
return true;
}
/*
* <iq xmlns='jabber:client' type='set' id='2'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>
*
*
*
*
*
*/
function openSession() {
/*
if (! ($socketContext instanceof SocketContext)) {
throw new Exception("Illegal argument ".get_class($socket));
}
$socket = $socketContext->getSocket();
*/
//$this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, false);
//$this->reconnect();
//$b = $this->toStream();
//$this->process();
echo "Open session...\n";
$bf = "<iq xmlns='jabber:client' type='set' id='2'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>";
$outputStream = $this->socket->getOutputStream();
$outputStream->write($bf);
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
/*
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
*/
return true;
}
/*
* <message from='luoix0423@gmail.com/xmpphp81E902FF' to='atm.luo@gmail.com' type='chat'><body>abcdefg!</body></message>
*
*
*
*
*/
function messaging() {
/*
if (! ($socketContext instanceof SocketContext)) {
throw new Exception("Illegal argument ".get_class($socket));
}
$socket = $socketContext->getSocket();
*/
//$this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, false);
//$this->reconnect();
//$b = $this->toStream();
//$this->process();
echo "Open session...\n";
echo "Auth-ID: ", $this->loginID, "\n";
$bf = "<message from='".$this->loginID."' to='atm.luo@gmail.com' type='chat'><body>Hello, This is ".$this->loginID."!</body></message>";
$outputStream = $this->socket->getOutputStream();
$outputStream->write($bf);
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
/*
try {
$this->socketContext->notify();
} catch (Exception $e) {
echo "Fail to request authentication: " . $e->getMessage() . "\n";
return false;
}
*/
return true;
}
/*
//
$bf = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>";
$b = socket_write($socket, $bf);
if ($b == false) {
$error_code = socket_last_error();
$message = socket_strerror($error_code);
echo "Error occurs while writing data: ", $bf, "\n";
}
echo "Ok to write data: ", $bf, "\n";
try {
$socketContext->notify();
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . "\n";
}
//$bf = socket_read($socket, 1024);
//echo "Ok to read data: ", $bf, "\n";
//
$bf = '<stream:stream to="gmail.com" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
$b = socket_write($socket, $bf);
if ($b == false) {
$error_code = socket_last_error();
$message = socket_strerror($error_code);
echo "Error occurs while writing data: ", $bf, "\n";
}
echo "Ok to write data: ", $bf, "\n";
try {
$socketContext->notify();
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . "\n";
}
//$bf = socket_read($socket, 1024);
//echo "Ok to read data: ", $bf, "\n";
//
$bf = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>AGx1b2l4MDQyMwBqeGNvY28xMTI4</auth>";
$b = socket_write($socket, $bf);
if ($b == false) {
$error_code = socket_last_error();
$message = socket_strerror($error_code);
echo "Error occurs while writing data: ", $bf, "\n";
}
echo "Ok to write data: ", $bf, "\n";
try {
$socketContext->notify();
} catch (Exception $e) {
echo "Exception: " . $e->getMessage() . "\n";
}
//$bf = socket_read($socket, 1024);
//echo "Ok to read data: ", $bf, "\n";
*/
}
/*
function connect($targetAddress, $targetPort) {
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (! $socket) {
$error_code = socket_last_error();
$message = socket_strerror($error_code);
echo "Error occurs while create socket... Error, error code: ".$error_code.", error message: ".$message."\n";
return false;
}
echo "Socket create... "."Ok\n";
echo "---------", "---------", "\n";
//
$b = socket_bind($socket, "localhost", 1192);
if (! $b) {
$error_code = socket_last_error();
$message = socket_strerror($error_code);
echo "Failure to bind socket to localhost:", 1192, "... Failure, error code: ".$error_code.", error message: ".$message."\n";
}
echo "Bind socket to localhost:", 1192, "... Ok\n";
//
echo "Connecting to ".$targetAddress.":".$targetPort."...\n";
$b = socket_connect($socket, $targetAddress, $targetPort);
if ($b == false) {
$error_code = socket_last_error();
$message = socket_strerror($error_code);
echo "Failure to connect $targetAddress:", $targetPort, "... Failure, error code: ".$error_code.", error message: ".$message."\n";
return false;
}
return $socket;
}
*/
/**
*
*
*
*
*
*/
class GTalk {
/*
// talk.google.com:5222
// talk.google.com:5223
*
*
*
*
*/
var $targetAddress = "talk.google.com";
/*
*
*
*
*
*
*/
var $targetPort = 5222;
/*
*
*
*
*
*
*
*/
var $endpoint;
/*
*
*
*
*
*
*
*/
function __construct() {
$this->init();
}
/*
*
*
*
*
*
*
*/
function init() {
$this->endpoint = new XMPPEndpoint($this->targetAddress, $this->targetPort);
$this->endpoint->connect();
}
/*
*
*
*
*
*
*
*/
function start() {
$this->endpoint->openStream();
$this->endpoint->startTLS();
$this->endpoint->getAuthenticateRequest();
$this->endpoint->setBind();
$this->endpoint->openSession();
$this->endpoint->messaging();
}
}
/*
*
*
*
*
*
*
*/
$gtalk = new GTalk();
$gtalk->start();
?>
- 浏览: 444522 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (433)
- 编程语言-JAVA/Scala/Clojure/Grovvy (86)
- 编程语言-C (227)
- 编程语言-Rust/C++/Objective-C/Objective-C++ (97)
- 编程语言-PHP (4)
- 编程语言-Perl (4)
- 编程语言-Javascript (6)
- 编程语言-Scheme/Lisp (9)
- 编程语言-lua (5)
- 编程语言-erl (4)
- 编程语言-go (4)
- 编程语言-Ruby (3)
- 编程语言-python (4)
- 编程语言-smalltalk (3)
- 编程语言-guile (3)
- 算法结构 (32)
- 多线程编程 (2)
- 网络编程 (7)
- 并发编程 (21)
- 并行编程 (2)
- 事务 (4)
- 架构/编程-模型/模式/思想 (6)
- 开发框架-Spring (2)
- 开发框架-ibatis (0)
- 开发框架-Struts (0)
- 开发框架-Hibernate (0)
- 开发框架-mybatis (0)
- 分布式 (38)
- 分布式存储 (10)
- 分布式文件系统-dfs (4)
- 分布式计算 (2)
- 分布式事务 (10)
- 分布式数据中间件 (1)
- 分布式服务框架 (12)
- rpc (8)
- 通信 (23)
- 电信 (3)
- 电商 (1)
- 互联网应用 (2)
- 大前端 (5)
- 第三方支付 (2)
- CSS (1)
- android (3)
- ios (0)
- html5 (0)
- kafka (0)
- memcached (2)
- nginx (1)
- 并行计算 (1)
- 实时计算 (0)
- Storm (0)
- 数据库 (7)
- mysql (6)
- oracle (3)
- redis (4)
- mongodb (2)
- hbase (1)
- dal (0)
- handoop (1)
- 机器学习-ML (3)
- 深度学习-DL (0)
- 神经网络 (0)
- netty (4)
- mina (0)
- 大数据 (7)
- 大数据-算法 (0)
- 大数据-框架&平台 (3)
- 人工智能 (7)
- 人工智能-算法 (4)
- 人工智能-框架&平台 (0)
- 协议 (42)
- 安全 (15)
- 消息队列 (2)
- os(linux、windows) (85)
相关推荐
《PHPMailer邮件发送类详解与应用》 在PHP开发中,发送邮件是一项常见的需求,而PHPMailer是一个强大的、广泛使用的邮件发送类库,尤其适用于PHP5及以上版本。它提供了丰富的功能,包括支持SMTP验证、POP3收信以及...
PHP 基础实例 在HTML中嵌入PHP 使用 PHP 标记 PHP 语句 使用 PHP 注释 PHP 语句间的空格问题 添加动态内容 使用 PHP 函数 PHP nl2br()格式化输出 PHP 打印格式化输出 PHP 字符串的整理 PHP 改变字符串中字母的大小写...
phpweb二次开发,phpweb破解版,phpweb后台升级,phpweb2.0.5升级,phpweb成品网站商业版v1.5.0/20111209升级包 phpweb成品网站升级包免费送给爱好phpweb成品网站朋友,已经更新到最新的v2.0.5/20120412 免责声明:此...
php实现RS232串口通信 下载php_ser_5.2.0.zip 解冻把php_ser放到ext目录下, php.ini里加上: extension=php_ser.dll PHP code string ser_version( void ) void ser_open( string port, int baudrate, int databits, ...
【PHP5.4详解】 PHP(Hypertext Preprocessor)是一种广泛使用的开源脚本语言,尤其在Web开发领域。PHP5.4是该语言的一个重要版本,它在2012年发布,引入了许多新特性,提升了性能和开发效率。 1. **命名空间...
标题:PHP基础教程 出处:风流的CG网络日志 时间:Mon, 28 Aug 2006 07:24:34 +0000 作者:yufeng 地址:http:///read.php?38 内容: 提供给新手学习的 PHP新手教程,是一个比较有价值的PHP新手教程! 一、PHP...