added proper phpdoc, added exceptions, added useEncryption() method

git-svn-id: svn://netflint.net/xmpphp@34 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
temp 2008-05-05 19:50:02 +00:00
parent 6383d9b980
commit b440b4d30e
6 changed files with 264 additions and 133 deletions

View File

@ -1,24 +1,36 @@
<?php <?php
/* /**
XMPPHP: The PHP XMPP Library * XMPPHP: The PHP XMPP Library
Copyright (C) 2008 Nathanael C. Fritz * Copyright (C) 2008 Nathanael C. Fritz
This file is part of SleekXMPP. * This file is part of SleekXMPP.
*
XMPPHP is free software; you can redistribute it and/or modify * XMPPHP is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. * (at your option) any later version.
*
XMPPHP is distributed in the hope that it will be useful, * XMPPHP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. * GNU General Public License for more details.
*
You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
along with XMPPHP; if not, write to the Free Software * along with XMPPHP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ *
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
*/
/**
* XMPPHP Log
*
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
* @version $Id$
*/
class XMPPHP_Log { class XMPPHP_Log {
const LEVEL_ERROR = 0; const LEVEL_ERROR = 0;

View File

@ -1,24 +1,37 @@
<?php <?php
/* /**
XMPPHP: The PHP XMPP Library * XMPPHP: The PHP XMPP Library
Copyright (C) 2008 Nathanael C. Fritz * Copyright (C) 2008 Nathanael C. Fritz
This file is part of SleekXMPP. * This file is part of SleekXMPP.
*
* XMPPHP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* XMPPHP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XMPPHP; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
*/
XMPPHP is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
XMPPHP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XMPPHP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* XMPPHP XML Object
*
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
* @version $Id$
*/
class XMPPHP_XMLObj { class XMPPHP_XMLObj {
/** /**
* Tag name * Tag name

View File

@ -1,27 +1,45 @@
<?php <?php
/* /**
XMPPHP: The PHP XMPP Library * XMPPHP: The PHP XMPP Library
Copyright (C) 2008 Nathanael C. Fritz * Copyright (C) 2008 Nathanael C. Fritz
This file is part of SleekXMPP. * This file is part of SleekXMPP.
*
* XMPPHP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* XMPPHP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XMPPHP; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
*/
XMPPHP is free software; you can redistribute it and/or modify /** XMPPHP_Exception */
it under the terms of the GNU General Public License as published by require_once 'Exception.php';
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
XMPPHP is distributed in the hope that it will be useful, /** XMPPHP_XMLObj */
but WITHOUT ANY WARRANTY; without even the implied warranty of require_once 'XMLObj.php';
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License /** XMPPHP_Log */
along with XMPPHP; if not, write to the Free Software require_once 'Log.php';
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once("XMLObj.php");
require_once("Log.php");
/**
* XMPPHP XML Stream
*
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
* @version $Id$
*/
class XMPPHP_XMLStream { class XMPPHP_XMLStream {
/** /**
* @var resource * @var resource
@ -142,6 +160,16 @@ class XMPPHP_XMLStream {
$this->log = new XMPPHP_Log($printlog, $loglevel); $this->log = new XMPPHP_Log($printlog, $loglevel);
} }
/**
* Destructor
* Cleanup connection
*/
public function __destruct() {
if(!$this->disconnected) {
$this->disconnect();
}
}
/** /**
* Return the log instance * Return the log instance
* *
@ -215,6 +243,8 @@ class XMPPHP_XMLStream {
if(!$this->socket) { if(!$this->socket) {
$this->log->log("Could not connect.", XMPPHP_Log::LEVEL_ERROR); $this->log->log("Could not connect.", XMPPHP_Log::LEVEL_ERROR);
$this->disconnected = true; $this->disconnected = true;
throw new XMPPHP_Exception('Could not connect.');
} }
stream_set_blocking($this->socket, 1); stream_set_blocking($this->socket, 1);
if($sendinit) $this->send($this->stream_start); if($sendinit) $this->send($this->stream_start);
@ -235,6 +265,7 @@ class XMPPHP_XMLStream {
* Disconnect from XMPP Host * Disconnect from XMPP Host
*/ */
public function disconnect() { public function disconnect() {
$this->log->log("Disconnecting...", XMPPHP_Log::LEVEL_VERBOSE);
$this->reconnect = false; $this->reconnect = false;
$this->send($this->stream_end); $this->send($this->stream_end);
$this->sent_disconnect = true; $this->sent_disconnect = true;
@ -394,8 +425,9 @@ class XMPPHP_XMLStream {
$name = $name[1]; $name = $name[1];
} }
$obj = new XMPPHP_XMLObj($name, $ns, $attr); $obj = new XMPPHP_XMLObj($name, $ns, $attr);
if($this->xml_depth > 1) if($this->xml_depth > 1) {
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj; $this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
}
$this->xmlobj[$this->xml_depth] = $obj; $this->xmlobj[$this->xml_depth] = $obj;
} }
@ -471,9 +503,10 @@ class XMPPHP_XMLStream {
* @param string $data * @param string $data
*/ */
protected function charXML($parser, $data) { protected function charXML($parser, $data) {
if(array_key_exists($this->xml_depth, $this->xmlobj)) if(array_key_exists($this->xml_depth, $this->xmlobj)) {
$this->xmlobj[$this->xml_depth]->data .= $data; $this->xmlobj[$this->xml_depth]->data .= $data;
} }
}
/** /**
* Event? * Event?
@ -485,7 +518,9 @@ class XMPPHP_XMLStream {
$this->log->log("EVENT: $name", XMPPHP_Log::LEVEL_DEBUG); $this->log->log("EVENT: $name", XMPPHP_Log::LEVEL_DEBUG);
foreach($this->eventhandlers as $handler) { foreach($this->eventhandlers as $handler) {
if($name == $handler[0]) { if($name == $handler[0]) {
if($handler[2] === null) $handler[2] = $this; if($handler[2] === null) {
$handler[2] = $this;
}
call_user_method($handler[1], $handler[2], $payload); call_user_method($handler[1], $handler[2], $payload);
} }
} }

View File

@ -1,35 +1,84 @@
<?php <?php
/* /**
XMPPHP: The PHP XMPP Library * XMPPHP: The PHP XMPP Library
Copyright (C) 2008 Nathanael C. Fritz * Copyright (C) 2008 Nathanael C. Fritz
This file is part of SleekXMPP. * This file is part of SleekXMPP.
*
* XMPPHP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* XMPPHP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XMPPHP; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
*/
XMPPHP is free software; you can redistribute it and/or modify /** XMPPHP_XMLStream */
it under the terms of the GNU General Public License as published by require_once "XMLStream.php";
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
XMPPHP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XMPPHP; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once("XMLStream.php");
/**
* XMPPHP Main Class
*
* @category XMPPHP
* @package XMPPHP
* @copyright Copyright (C) 2008 Nathanael C. Fritz
* @version $Id$
*/
class XMPPHP_XMPP extends XMPPHP_XMLStream { class XMPPHP_XMPP extends XMPPHP_XMLStream {
/**
* @var string
*/
protected $server; protected $server;
/**
* @var string
*/
protected $user; protected $user;
/**
* @var string
*/
protected $password; protected $password;
/**
* @var string
*/
protected $resource; protected $resource;
/**
* @var string
*/
protected $fulljid; protected $fulljid;
/**
* @var string
*/
protected $basejid; protected $basejid;
protected $authed;
public $auto_subscribe = False; /**
* @var boolean
*/
protected $authed = false;
/**
* @var boolean
*/
protected $auto_subscribe = false;
/**
* @var boolean
*/
protected $use_encryption = true;
/** /**
* Constructor * Constructor
@ -54,23 +103,32 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
$this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">'; $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
$this->stream_end = '</stream:stream>'; $this->stream_end = '</stream:stream>';
$this->default_ns = 'jabber:client';
$this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler'); $this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler');
$this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler'); $this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler');
$this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler'); $this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler');
$this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler'); $this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler');
$this->addHandler('message', 'jabber:client', 'message_handler'); $this->addHandler('message', 'jabber:client', 'message_handler');
$this->addHandler('presence', 'jabber:client', 'presence_handler'); $this->addHandler('presence', 'jabber:client', 'presence_handler');
}
$this->default_ns = 'jabber:client'; /**
$this->authed = false; * Turn encryption on/ff
$this->use_encryption = true; *
* @param boolean $useEncryption
*/
public function useEncryption($useEncryption = true) {
$this->use_encryption = $useEncryption;
} }
/** /**
* Turn on auto-authorization of subscription requests. * Turn on auto-authorization of subscription requests.
*
* @param boolean $autoSubscribe
*/ */
public function autoSubscribe() { public function autoSubscribe($autoSubscribe = true) {
$this->auto_subscribe = true; $this->auto_subscribe = $autoSubscribe;
} }
/** /**
@ -164,7 +222,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
* *
* @param string $xml * @param string $xml
*/ */
public function features_handler($xml) { protected function features_handler($xml) {
if($xml->hasSub('starttls') and $this->use_encryption) { if($xml->hasSub('starttls') and $this->use_encryption) {
$this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>"); $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
} elseif($xml->hasSub('bind')) { } elseif($xml->hasSub('bind')) {
@ -182,7 +240,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
* *
* @param string $xml * @param string $xml
*/ */
public function sasl_success_handler($xml) { protected function sasl_success_handler($xml) {
$this->log->log("Auth success!"); $this->log->log("Auth success!");
$this->authed = true; $this->authed = true;
$this->reset(); $this->reset();
@ -193,9 +251,11 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
* *
* @param string $xml * @param string $xml
*/ */
public function sasl_failure_handler($xml) { protected function sasl_failure_handler($xml) {
$this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR); $this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR);
$this->disconnect(); $this->disconnect();
throw new XMPPHP_Exception('Auth failed!');
} }
/** /**
@ -203,7 +263,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
* *
* @param string $xml * @param string $xml
*/ */
public function resource_bind_handler($xml) { protected function resource_bind_handler($xml) {
if($xml->attrs['type'] == 'result') { if($xml->attrs['type'] == 'result') {
$this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data); $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
$this->fulljid = $xml->sub('bind')->sub('jid')->data; $this->fulljid = $xml->sub('bind')->sub('jid')->data;
@ -218,7 +278,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
* *
* @param string $xml * @param string $xml
*/ */
public function session_start_handler($xml) { protected function session_start_handler($xml) {
$this->log->log("Session started"); $this->log->log("Session started");
$this->event('session_start'); $this->event('session_start');
} }
@ -228,7 +288,7 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
* *
* @param string $xml * @param string $xml
*/ */
public function tls_proceed_handler($xml) { protected function tls_proceed_handler($xml) {
$this->log->log("Starting TLS encryption"); $this->log->log("Starting TLS encryption");
stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
$this->reset(); $this->reset();

View File

@ -3,10 +3,13 @@
// activate full error reporting // activate full error reporting
error_reporting(E_ALL & E_STRICT); error_reporting(E_ALL & E_STRICT);
include("XMPPHP/XMPP.php"); include 'XMPPHP/XMPP.php';
$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'user', 'password', 'xmpphp', 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);
$conn->connect(); $conn = new XMPPHP_XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);
while(!$conn->isDisconnected()) {
try {
$conn->connect();
while(!$conn->isDisconnected()) {
$payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start')); $payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start'));
foreach($payloads as $event) { foreach($payloads as $event) {
$pl = $event[1]; $pl = $event[1];
@ -30,4 +33,7 @@ while(!$conn->isDisconnected()) {
break; break;
} }
} }
}
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
} }

View File

@ -3,11 +3,16 @@
// activate full error reporting // activate full error reporting
error_reporting(E_ALL & E_STRICT); error_reporting(E_ALL & E_STRICT);
include("XMPPHP/XMPP.php"); include 'XMPPHP/XMPP.php';
$conn = new XMPPHP_XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO); $conn = new XMPPHP_XMPP('talk.google.com', 5222, 'username', 'password', 'xmpphp', 'gmail.com', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
$conn->connect();
$conn->processUntil('session_start'); try {
$conn->presence(); $conn->connect();
$conn->message('stephan@jabber.wentz.it', 'This is a test message!'); $conn->processUntil('session_start');
$conn->disconnect(); $conn->presence();
$conn->message('stephan@jabber.wentz.it', 'This is a test message!');
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}