Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSL support #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.svn
.idea
.idea
/nbproject/private/
104 changes: 61 additions & 43 deletions lib/XmppPrebind.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class XmppPrebind {
* @param string $jabberHost Jabber Server Host
* @param string $boshUri Full URI to the http-bind
* @param string $resource Resource identifier
* @param bool $useSsl Use SSL (not working yet, TODO)
* @param bool $useSsl Use SSL (working already)
* @param bool $debug Enable debug
*/
public function __construct($jabberHost, $boshUri, $resource, $useSsl = false, $debug = false) {
Expand Down Expand Up @@ -133,62 +133,75 @@ public function __construct($jabberHost, $boshUri, $resource, $useSsl = false, $
* @param string $username Username without jabber host
* @param string $password Password
* @param string $route Route
* @param integer $force_encryption_method
*/
public function connect($username, $password, $route = false) {
$this->jid = $username . '@' . $this->jabberHost;
public function connect($username, $password, $route = false, $force_encryption_method = null)
{
$this->jid = $username . '@' . $this->jabberHost;

if($this->resource) {
$this->jid .= '/' . $this->resource;
}
if($this->resource) {
$this->jid .= '/' . $this->resource;
}

$this->password = $password;
$this->password = $password;

$response = $this->sendInitialConnection($route);
$response = $this->sendInitialConnection($route);
if(empty($response)) {
throw new XmppPrebindConnectionException("No response from server.");
throw new XmppPrebindConnectionException("No response from server.");
}

$body = self::getBodyFromXml($response);
if ( empty( $body ) )
throw new XmppPrebindConnectionException("No body could be found in response from server.");
$this->sid = $body->getAttribute('sid');

// set the Bosh Attributes
$this->wait = $body->getAttribute('wait');
$this->requests = $body->getAttribute('requests');
$this->ver = $body->getAttribute('ver');
$this->polling = $body->getAttribute('polling');
$this->inactivity = $body->getAttribute('inactivity');
$this->hold = $body->getAttribute('hold');
$this->to = $body->getAttribute('to');
$this->accept = $body->getAttribute('accept');
$this->maxpause = $body->getAttribute('maxpause');

$this->debug($this->sid, 'sid');
$body = self::getBodyFromXml($response);
if ( empty( $body ) ) {
throw new XmppPrebindConnectionException("No body could be found in response from server.");
}

$this->sid = $body->getAttribute('sid');

// set the Bosh Attributes
$this->wait = $body->getAttribute('wait');
$this->requests = $body->getAttribute('requests');
$this->ver = $body->getAttribute('ver');
$this->polling = $body->getAttribute('polling');
$this->inactivity = $body->getAttribute('inactivity');
$this->hold = $body->getAttribute('hold');
$this->to = $body->getAttribute('to');
$this->accept = $body->getAttribute('accept');
$this->maxpause = $body->getAttribute('maxpause');

$this->debug($this->sid, 'sid');

if(empty($body->firstChild) || empty($body->firstChild->firstChild)) {
throw new XmppPrebindConnectionException("Child not found in response from server.");
throw new XmppPrebindConnectionException("Child not found in response from server.");
}
$mechanisms = $body->getElementsByTagName('mechanism');
$mechanisms = $body->getElementsByTagName('mechanism');

foreach ($mechanisms as $value) {
$this->mechanisms[] = $value->nodeValue;
}
foreach ($mechanisms as $value) {
$this->mechanisms[] = $value->nodeValue;
}

if (in_array(self::ENCRYPTION_DIGEST_MD5, $this->mechanisms)) {
$this->encryption = self::ENCRYPTION_DIGEST_MD5;
} elseif (in_array(self::ENCRYPTION_CRAM_MD5, $this->mechanisms)) {
$this->encryption = self::ENCRYPTION_CRAM_MD5;
} elseif (in_array(self::ENCRYPTION_PLAIN, $this->mechanisms)) {
$this->encryption = self::ENCRYPTION_PLAIN;
} else {
throw new XmppPrebindConnectionException("No encryption supported by the server is supported by this library.");
}
if ($force_encryption_method !== null) {
if (in_array($force_encryption_method, $this->mechanisms)) {
$this->encryption = $force_encryption_method;
} else {
throw new XmppPrebindConnectionException("Forced encryption is not supported by the server.");
}
} else {

if (in_array(self::ENCRYPTION_DIGEST_MD5, $this->mechanisms)) {
$this->encryption = self::ENCRYPTION_DIGEST_MD5;
} elseif (in_array(self::ENCRYPTION_CRAM_MD5, $this->mechanisms)) {
$this->encryption = self::ENCRYPTION_CRAM_MD5;
} elseif (in_array(self::ENCRYPTION_PLAIN, $this->mechanisms)) {
$this->encryption = self::ENCRYPTION_PLAIN;
} else {
throw new XmppPrebindConnectionException("No encryption supported by the server is supported by this library.");
}
}

$this->debug($this->encryption, 'encryption used');
$this->debug($this->encryption, 'encryption used');

// Assign session creation response
$this->response = $body;
// Assign session creation response
$this->response = $body;
}

/**
Expand Down Expand Up @@ -520,6 +533,11 @@ protected function send($xml) {
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

if ($this->useSsl) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}

curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Expand Down
7 changes: 7 additions & 0 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include.path=${php.global.include.path}
php.version=PHP_56
source.encoding=UTF-8
src.dir=.
tags.asp=false
tags.short=false
web.root=.
9 changes: 9 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.php.project</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/php-project/1">
<name>xmpp-prebind</name>
</data>
</configuration>
</project>