Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
77.78% covered (success)
77.78%
7 / 9
CRAP
87.50% covered (success)
87.50%
21 / 24
Ldap
0.00% covered (danger)
0.00%
0 / 1
77.78% covered (success)
77.78%
7 / 9
16.50
87.50% covered (success)
87.50%
21 / 24
 __construct
0.00% covered (danger)
0.00%
0 / 1
5.27
77.78% covered (success)
77.78%
7 / 9
 setOption
0.00% covered (danger)
0.00%
0 / 1
2.06
75.00% covered (success)
75.00%
3 / 4
 setOptions
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 getHost
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getPort
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getOption
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 getOptions
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 resource
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 authenticate
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
<?php
/**
 * Pop PHP Framework (http://www.popphp.org/)
 *
 * @link       https://github.com/popphp/popphp-framework
 * @author     Nick Sagona, III <dev@nolainteractive.com>
 * @copyright  Copyright (c) 2009-2017 NOLA Interactive, LLC. (http://www.nolainteractive.com)
 * @license    http://www.popphp.org/license     New BSD License
 */
/**
 * @namespace
 */
namespace Pop\Auth;
/**
 * Ldap auth class
 *
 * @category   Pop
 * @package    Pop\Auth
 * @author     Nick Sagona, III <dev@nolainteractive.com>
 * @copyright  Copyright (c) 2009-2017 NOLA Interactive, LLC. (http://www.nolainteractive.com)
 * @license    http://www.popphp.org/license     New BSD License
 * @version    3.0.1
 */
class Ldap extends AbstractAuth
{
    /**
     * Ldap host
     * @var string
     */
    protected $host = null;
    /**
     * Ldap port
     * @var string
     */
    protected $port = null;
    /**
     * Ldap options
     * @var array
     */
    protected $options = [];
    /**
     * Ldap resource
     * @var resource
     */
    protected $resource = null;
    /**
     * Constructor
     *
     * Instantiate the Ldap auth adapter object
     *
     * @param  string $host
     * @param  string $port
     * @param  array  $options
     */
    public function __construct($host, $port = null, array $options = null)
    {
        $this->host = $host;
        if (null !== $port) {
            $this->port = $port;
        }
        if ($host != '') {
            $host = (null !== $this->port) ? $this->host . ':' . $this->port : $this->host;
            $this->resource = ldap_connect($host);
        }
        if (null !== $options) {
            $this->setOptions($options);
        }
    }
    /**
     * Set an option
     *
     * @param  mixed $option
     * @param  mixed $value
     * @return Ldap
     */
    public function setOption($option, $value)
    {
        $this->options[$option] = $value;
        if (is_resource($this->resource)) {
            ldap_set_option($this->resource, $option, $value);
        }
        return $this;
    }
    /**
     * Set options
     *
     * @param  array $options
     * @return Ldap
     */
    public function setOptions(array $options)
    {
        foreach ($options as $option => $value) {
            $this->setOption($option, $value);
        }
        return $this;
    }
    /**
     * Get the host
     *
     * @return string
     */
    public function getHost()
    {
        return $this->host;
    }
    /**
     * Get the port
     *
     * @return string
     */
    public function getPort()
    {
        return $this->port;
    }
    /**
     * Get an option
     *
     * @param  mixed $option
     * @return mixed
     */
    public function getOption($option)
    {
        return (isset($this->options[$option])) ? $this->options[$option] : null;
    }
    /**
     * Get options
     *
     * @return array
     */
    public function getOptions()
    {
        return $this->options;
    }
    /**
     * Get the Ldap resource
     *
     * @return resource
     */
    public function resource()
    {
        return $this->resource;
    }
    /**
     * Method to authenticate
     *
     * @param  string $username
     * @param  string $password
     * @return int
     */
    public function authenticate($username, $password)
    {
        parent::authenticate($username, $password);
        $this->result = (int)(@ldap_bind($this->resource, $this->username, $this->password));
        return $this->result;
    }
}