Index: XMPPHP/XMPP.php =================================================================== --- XMPPHP/XMPP.php (revision 51) +++ XMPPHP/XMPP.php (working copy) @@ -85,6 +85,11 @@ protected $use_encryption = true; /** + * @var boolean + */ + protected $use_anonymous = false; + + /** * Constructor * * @param string $host @@ -96,11 +101,17 @@ * @param boolean $printlog * @param string $loglevel */ - public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) { + public function __construct($host, $port, $user = null, $password = null, $resource = null, $server = null, $printlog = false, $loglevel = null) { parent::__construct($host, $port, $printlog, $loglevel); $this->user = $user; $this->password = $password; + if ($user === null) { + $this->use_anonymous = true; + } + if ($resource === null) { + $resource = uniqid('XMPPHP_', true); + } $this->resource = $resource; if(!$server) $server = $host; $this->basejid = $this->user . '@' . $this->host; @@ -125,6 +136,15 @@ public function useEncryption($useEncryption = true) { $this->use_encryption = $useEncryption; } + + /** + * Turn anonymous on/ff + * + * @param boolean $useAnonymous + */ + public function useAnonymous($useAnonymous = true) { + $this->use_anonymous = $useAnonymous; + } /** * Turn on auto-authorization of subscription requests. @@ -237,7 +257,22 @@ $this->send("{$this->resource}"); } else { $this->log->log("Attempting Auth..."); + $msupported = array(); + if ($xml->hasSub('mechanisms')) { + // SASL + $ms = $xml->sub('mechanisms'); + foreach($ms->subs as $m) { + if ($m->name != 'mechanism') { + continue; + } + $msupported[] = strtoupper(trim($m->data)); + } + } + if ($this->use_anonymous and in_array('ANONYMOUS', $msupported)) { + $this->send(""); + } else { $this->send("" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . ""); + } } } @@ -273,6 +308,11 @@ if($xml->attrs['type'] == 'result') { $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data); $this->fulljid = $xml->sub('bind')->sub('jid')->data; + if ($this->user === null) { + // anonymous login, so update these... + $this->user = preg_replace('/@.*/', '', $this->fulljid); + $this->basejid = preg_replace('/\/.*/', '', $this->fulljid); + } } $id = $this->getId(); $this->addIdHandler($id, 'session_start_handler');