Skip to content

Instantly share code, notes, and snippets.

/XmppBosh.php Secret

Created September 21, 2015 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d3f2b5589d1346e57d49 to your computer and use it in GitHub Desktop.
Save anonymous/d3f2b5589d1346e57d49 to your computer and use it in GitHub Desktop.
<?php
namespace common\extensions;
class XmppBosh
{
protected $sid;
protected $rid;
protected $jid;
protected $boshUrl;
protected $user;
protected $tld;
protected $password;
public function __construct($params)
{
foreach ($params as $key => $value)
$this->{$key} = $value;
}
public function getRid()
{
return $this->rid;
}
public function getSid()
{
return $this->sid;
}
public function getJid()
{
return $this->jid;
}
public function connect()
{
$hash = base64_encode("$this->user@$this->tld\0$this->user\0$this->password") . "\n";
$rid = rand();
$jid = "$this->user@$this->tld";
$body = "<body rid='$rid'
xmlns='http://jabber.org/protocol/httpbind'
to='4pro.co' xml:lang='en' wait='60' hold='1'
window='5' content='text/xml; charset=utf-8'
ver='1.6' xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'/>";
$return = $this->__sendBody($body);
$xml = new \SimpleXMLElement($return);
$sid = $xml['sid'];
$rid++;
$body = "<body rid='$rid' xmlns='http://jabber.org/protocol/httpbind' sid='$sid'>
<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>$hash</auth>
</body>";
$this->__sendBody($body);
$rid++;
$body = "<body rid='$rid' xmlns='http://jabber.org/protocol/httpbind' sid='$sid' to='$this->tld' xml:lang='en' xmpp:restart='true' xmlns:xmpp='urn:xmpp:xbosh'/>";
$this->__sendBody($body);
$rid++;
$body = "<body rid='$rid' xmlns='http://jabber.org/protocol/httpbind' sid='$sid'>
<iq type='set' id='_bind_auth_2' xmlns='jabber:client'>
<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>
</iq>
</body>";
$this->__sendBody($body);
$rid++;
$body = "<body rid='$rid' xmlns='http://jabber.org/protocol/httpbind' sid='$sid'>
<iq type='set' id='_session_auth_2' xmlns='jabber:client'>
<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>
</iq>
</body>";
$this->__sendBody($body);
$rid++;
$sid = (array)$sid;
return [
"rid" => $rid,
"sid" => $sid[0],
"jid" => "$jid/messenger"
];
}
private function __sendBody($body)
{
$ch = curl_init($this->boshUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$header = array('Content-Type: text/xml; charset=utf-8');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return ($output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment