Skip to content

Commit

Permalink
Add config defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
frost-nzcr4 committed Sep 12, 2016
1 parent 8c39de4 commit 318d12d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 45 deletions.
97 changes: 53 additions & 44 deletions src/JAXL/jaxl.php
Expand Up @@ -125,36 +125,46 @@ class JAXL extends XMPPStream
*/
public function __construct(array $config)
{
$this->cfg = $config;

$cfg_defaults = array(
'auth_type' => 'PLAIN',
'bosh_hold' => null,
'bosh_rid' => null,
'bosh_url' => null,
'bosh_wait' => null,
'domain' => null,
'force_tls' => false,
'host' => null,
'jid' => null,
'log_colorize' => $this->log_colorize,
'log_level' => $this->log_level,
'log_path' => JAXLLogger::$path,
'multi_client' => false,
'pass' => false,
'port' => null,
'priv_dir' => getcwd().'/.jaxl',
'protocol' => null,
'resource' => null,
'stream_context' => null,
'strict' => true
);
$this->cfg = array_merge($cfg_defaults, $config);

// setup logger
if (isset($this->cfg['log_path'])) {
JAXLLogger::$path = $this->cfg['log_path'];
}
//else { JAXLLogger::$path = $this->log_dir."/jaxl.log"; }
if (isset($this->cfg['log_level'])) {
JAXLLogger::$level = $this->log_level = $this->cfg['log_level'];
} else {
JAXLLogger::$level = $this->log_level;
}
if (isset($this->cfg['log_colorize'])) {
JAXLLogger::$colorize = $this->log_colorize = $this->cfg['log_colorize'];
} else {
JAXLLogger::$colorize = $this->log_colorize;
}

JAXLLogger::$path = $this->cfg['log_path'];
JAXLLogger::$level = $this->log_level = $this->cfg['log_level'];
JAXLLogger::$colorize = $this->log_colorize = $this->cfg['log_colorize'];

// env
$strict = isset($this->cfg['strict']) ? $this->cfg['strict'] : true;
if ($strict) {
if ($this->cfg['strict']) {
$this->add_exception_handlers();
}
$this->mode = PHP_SAPI;
$this->local_ip = gethostbyname(php_uname('n'));
$this->pid = getmypid();

// jid object
$jid = isset($this->cfg['jid']) ? new XMPPJid($this->cfg['jid']) : null;
$jid = ($this->cfg['jid'] !== null) ? new XMPPJid($this->cfg['jid']) : null;

// handle signals
if (extension_loaded('pcntl')) {
pcntl_signal(SIGHUP, array($this, 'signal_handler'));
Expand All @@ -164,7 +174,7 @@ public function __construct(array $config)

// Create .jaxl directory for our /tmp, /run and /log folders
// overwrite these using jaxl config array
$this->priv_dir = isset($this->cfg['priv_dir']) ? $this->cfg['priv_dir'] : getcwd()."/.jaxl";
$this->priv_dir = $this->cfg['priv_dir'];
$this->tmp_dir = $this->priv_dir."/tmp";
$this->pid_dir = $this->priv_dir."/run";
$this->log_dir = $this->priv_dir."/log";
Expand All @@ -184,7 +194,7 @@ public function __construct(array $config)
if (!is_dir($this->sock_dir)) {
mkdir($this->sock_dir);
}

// touch pid file
if ($this->mode == "cli") {
touch($this->get_pid_file_path());
Expand All @@ -195,44 +205,43 @@ public function __construct(array $config)
// service discovery and entity caps
// are recommended for every xmpp entity
$this->require_xep(array('0030', '0115'));

// do dns lookup, update $cfg['host'] and $cfg['port'] if not already specified
$host = isset($this->cfg['host']) ? $this->cfg['host'] : null;
$port = isset($this->cfg['port']) ? $this->cfg['port'] : null;
if ((!$host || !$port) && $jid) {
if (($this->cfg['host'] === null || $this->cfg['port'] === null) && $jid) {
// this dns lookup is blocking
_info("dns srv lookup for ".$jid->domain);
list($host, $port) = JAXLUtil::get_dns_srv($jid->domain);
if ($this->cfg['host'] === null) {
$this->cfg['host'] = $host;
}
if ($this->cfg['port'] === null) {
$this->cfg['port'] = $port;
}
}
$this->cfg['host'] = isset($this->cfg['host']) ? $this->cfg['host'] : $host;
$this->cfg['port'] = isset($this->cfg['port']) ? $this->cfg['port'] : $port;


// choose appropriate transport
// if 'bosh_url' cfg is defined include 0206
if (isset($this->cfg['bosh_url'])) {
_debug("including bosh xep");
$this->require_xep('0206');
$transport = $this->xeps['0206'];
} else {
//list($host, $port) = JAXLUtil::get_dns_srv($jid->domain);
$stream_context = isset($this->cfg['stream_context']) ? $this->cfg['stream_context'] : null;
$transport = new JAXLSocketClient($stream_context);
$transport = new JAXLSocketClient($this->cfg['stream_context']);
}

$this->cfg['multi_client'] = isset($this->cfg['multi_client']) ? $this->cfg['multi_client'] : false;
// lifecycle events callback
$this->ev = new JAXLEvent($this->cfg['multi_client'] ? array(&$this) : array());

// initialize xmpp stream with configured transport
parent::__construct(
$transport,
$jid,
isset($this->cfg['pass']) ? $this->cfg['pass'] : false,
isset($this->cfg['resource']) ? 'jaxl#'.$this->cfg['resource'] : 'jaxl#'.md5(time()),
isset($this->cfg['force_tls']) ? $this->cfg['force_tls'] : false
$this->cfg['pass'],
$this->cfg['resource'] !== null ? 'jaxl#'.$this->cfg['resource'] : 'jaxl#'.md5(time()),
$this->cfg['force_tls']
);
}

public function __destruct()
{
// delete pid file
Expand All @@ -246,16 +255,16 @@ public function __destruct()

parent::__destruct();
}

public function add_exception_handlers()
{
_info("strict mode enabled, adding exception handlers. ' .
'Set 'strict' => TRUE inside JAXL config to disable this");
'Set 'strict' => false inside JAXL config to disable this");
set_error_handler(array('JAXLException', 'error_handler'));
set_exception_handler(array('JAXLException', 'exception_handler'));
register_shutdown_function(array('JAXLException', 'shutdown_handler'));
}

public function get_pid_file_path()
{
return $this->pid_dir."/jaxl_".$this->pid.".pid";
Expand Down Expand Up @@ -406,7 +415,7 @@ public function unsubscribed($to)

public function get_socket_path()
{
if (isset($this->cfg['protocol'])) {
if ($this->cfg['protocol'] !== null) {
$protocol = $this->cfg['protocol'];
} else {
$protocol = ($this->cfg['port'] == 5223 ? "ssl" : "tcp");
Expand Down Expand Up @@ -670,7 +679,7 @@ public function handle_auth_mechs($stanza, $mechanisms)
}

// check if preferred auth type exists in available mechanisms
$pref_auth = isset($this->cfg['auth_type']) ? $this->cfg['auth_type'] : 'PLAIN';
$pref_auth = $this->cfg['auth_type'];
$pref_auth_exists = isset($mechs[$pref_auth]) ? true : false;
_debug("pref_auth ".$pref_auth." ".($pref_auth_exists ? "exists" : "doesn't exists"));

Expand Down
2 changes: 1 addition & 1 deletion src/JAXL/xep/xep_0206.php
Expand Up @@ -243,7 +243,7 @@ public function session_start()
'ver' => '1.10'
);

if (isset($this->jaxl->cfg['jid'])) {
if ($this->jaxl->cfg['jid'] !== null) {
$attrs['from'] = $this->jaxl->cfg['jid'];
}
$body = new JAXLXml('body', self::NS_HTTP_BIND, $attrs);
Expand Down
27 changes: 27 additions & 0 deletions tests/JAXLTest.php
Expand Up @@ -54,4 +54,31 @@ public function testProtocolOption()
$this->assertEquals('tcp://domain.tld:5223', $jaxl->get_socket_path());
$this->assertInstanceOf('JAXLSocketClient', $jaxl->getTransport());
}

public function testConfig()
{
$config = array();
$jaxl = new JAXL($config);

$this->assertEquals('PLAIN', $jaxl->cfg['auth_type']);
$this->assertNull($jaxl->cfg['bosh_hold']);
$this->assertNull($jaxl->cfg['bosh_rid']);
$this->assertNull($jaxl->cfg['bosh_url']);
$this->assertNull($jaxl->cfg['bosh_wait']);
$this->assertNull($jaxl->cfg['domain']);
$this->assertEquals($jaxl->force_tls, $jaxl->cfg['force_tls']);
$this->assertNull($jaxl->cfg['host']);
$this->assertNull($jaxl->cfg['jid']);
$this->assertEquals($jaxl->log_colorize, $jaxl->cfg['log_colorize']);
$this->assertEquals($jaxl->log_level, $jaxl->cfg['log_level']);
$this->assertEquals(JAXLLogger::$path, $jaxl->cfg['log_path']);
$this->assertFalse($jaxl->cfg['multi_client']);
$this->assertFalse($jaxl->cfg['pass']);
$this->assertNull($jaxl->cfg['port']);
$this->assertContains('.jaxl', $jaxl->cfg['priv_dir']);
$this->assertNull($jaxl->cfg['protocol']);
$this->assertNull($jaxl->cfg['resource']);
$this->assertNull($jaxl->cfg['stream_context']);
$this->assertTrue($jaxl->cfg['strict']);
}
}

0 comments on commit 318d12d

Please sign in to comment.