* @author Stephan Wentz * @copyright 2008 Nathanael C. Fritz */ /** XMPPHP_XMLStream */ require_once "XMLStream.php"; /** * XMPPHP Main Class * * @category xmpphp * @package XMPPHP * @author Nathanael C. Fritz * @author Stephan Wentz * @copyright 2008 Nathanael C. Fritz * @version $Id$ */ class XMPPHP_XMPP extends XMPPHP_XMLStream { /** * @var string */ protected $server; /** * @var string */ protected $user; /** * @var string */ protected $password; /** * @var string */ protected $resource; /** * @var string */ protected $fulljid; /** * @var string */ protected $basejid; /** * @var boolean */ protected $authed = false; protected $session_started = false; /** * @var boolean */ protected $auto_subscribe = false; /** * @var boolean */ protected $use_encryption = true; /** * Constructor * * @param string $host * @param integer $port * @param string $user * @param string $password * @param string $resource * @param string $server * @param boolean $printlog * @param string $loglevel */ public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) { parent::__construct($host, $port, $printlog, $loglevel); $this->user = $user; $this->password = $password; $this->resource = $resource; if(!$server) $server = $host; $this->basejid = $this->user . '@' . $this->host; $this->stream_start = ''; $this->stream_end = ''; $this->default_ns = 'jabber:client'; $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('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('message', 'jabber:client', 'message_handler'); $this->addHandler('presence', 'jabber:client', 'presence_handler'); } /** * Turn encryption on/ff * * @param boolean $useEncryption */ public function useEncryption($useEncryption = true) { $this->use_encryption = $useEncryption; } /** * Turn on auto-authorization of subscription requests. * * @param boolean $autoSubscribe */ public function autoSubscribe($autoSubscribe = true) { $this->auto_subscribe = $autoSubscribe; } /** * Send XMPP Message * * @param string $to * @param string $body * @param string $type * @param string $subject */ public function message($to, $body, $type = 'chat', $subject = null, $payload = null) { if(is_null($type)) { $type = 'chat'; } $to = htmlspecialchars($to); $body = htmlspecialchars($body); $subject = htmlspecialchars($subject); $out = ""; if($subject) $out .= "$subject"; $out .= "$body"; if($payload) $out .= $payload; $out .= ""; $this->send($out); } /** * Set Presence * * @param string $status * @param string $show * @param string $to */ public function presence($status = null, $show = 'available', $to = null, $type='available') { if($type == 'available') $type = ''; $to = htmlspecialchars($to); $status = htmlspecialchars($status); if($show == 'unavailable') $type = 'unavailable'; $out = "send($out); } /** * Message handler * * @param string $xml */ public function message_handler($xml) { if(isset($xml->attrs['type'])) { $payload['type'] = $xml->attrs['type']; } else { $payload['type'] = 'chat'; } $payload['from'] = $xml->attrs['from']; $payload['body'] = $xml->sub('body')->data; $this->log->log("Message: {$xml->sub('body')->data}", XMPPHP_Log::LEVEL_DEBUG); $this->event('message', $payload); } /** * Presence handler * * @param string $xml */ public function presence_handler($xml) { $payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available'; $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type']; $payload['from'] = $xml->attrs['from']; $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : ''; $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG); if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') { if($this->auto_subscribe) { $this->send(""); $this->send(""); } $this->event('subscription_requested', $payload); } elseif(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribed') { $this->event('subscription_accepted', $payload); } else { $this->event('presence', $payload); } } /** * Features handler * * @param string $xml */ protected function features_handler($xml) { if($xml->hasSub('starttls') and $this->use_encryption) { $this->send(""); } elseif($xml->hasSub('bind') and $this->authed) { $id = $this->getId(); $this->addIdHandler($id, 'resource_bind_handler'); $this->send("{$this->resource}"); } else { $this->log->log("Attempting Auth..."); $this->send("" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . ""); } } /** * SASL success handler * * @param string $xml */ protected function sasl_success_handler($xml) { $this->log->log("Auth success!"); $this->authed = true; $this->reset(); } /** * SASL feature handler * * @param string $xml */ protected function sasl_failure_handler($xml) { $this->log->log("Auth failed!", XMPPHP_Log::LEVEL_ERROR); $this->disconnect(); throw new XMPPHP_Exception('Auth failed!'); } /** * Resource bind handler * * @param string $xml */ protected function resource_bind_handler($xml) { if($xml->attrs['type'] == 'result') { $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data); $this->fulljid = $xml->sub('bind')->sub('jid')->data; } $id = $this->getId(); $this->addIdHandler($id, 'session_start_handler'); $this->send(""); } /** * Retrieves the roster * */ public function getRoster() { $id = $this->getID(); $this->addIdHandler($id, 'roster_get_handler'); $this->send(""); } /** * Roster retrieval handler * * @param string $xml */ protected function roster_get_handler($xml) { // TODO: make this work } /** * Session start handler * * @param string $xml */ protected function session_start_handler($xml) { $this->log->log("Session started"); $this->session_started = true; $this->event('session_start'); } /** * TLS proceed handler * * @param string $xml */ protected function tls_proceed_handler($xml) { $this->log->log("Starting TLS encryption"); stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); $this->reset(); } }