<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Luca&#039;s forge &#187; pear</title>
	<atom:link href="http://lucasforge.bmeme.com/category/pear/feed/" rel="self" type="application/rss+xml" />
	<link>http://lucasforge.bmeme.com</link>
	<description>Experiments from the land of open source</description>
	<lastBuildDate>Wed, 19 Oct 2011 10:29:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using Pear::Auth as Zend_Auth adapter</title>
		<link>http://lucasforge.bmeme.com/2009/07/using-pear-auth-as-zend-auth-adapter/</link>
		<comments>http://lucasforge.bmeme.com/2009/07/using-pear-auth-as-zend-auth-adapter/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 13:57:04 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[authentication]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=63</guid>
		<description><![CDATA[If you use both Pear and Zend framework you may want to extend the <a href="http://framework.zend.com/manual/en/zend.auth.html">Zend_Auth</a> adapters to use all the containers shipped with the <a href="http://pear.php.net/package/Auth">Pear::Auth</a> package.

Let's go to create the class implements the Zend_Auth_Adapter_Interface starting from the Zend_Auth_Adapter_Ldap class.]]></description>
			<content:encoded><![CDATA[<p>If you use both Pear and Zend framework you may want to extend the <a href="http://framework.zend.com/manual/en/zend.auth.html">Zend_Auth</a> adapters to use all the containers shipped with the <a href="http://pear.php.net/package/Auth">Pear::Auth</a> package.</p>
<p>Let&#8217;s go to create the class implements the Zend_Auth_Adapter_Interface starting from the Zend_Auth_Adapter_Ldap class.</p>
<p><strong>My_Zend_Auth_Adapter_PearAuth</strong></p>
<pre class="brush: php">
class My_Zend_Auth_Adapter_PearAuth implements Zend_Auth_Adapter_Interface
{

/**
* The array of arrays of Pear::Auth options passed to the constructor.
*
* @var array
*/
protected $_options = null;

/**
* The username of the account being authenticated.
*
* @var string
*/
protected $_username = null;

/**
* The password of the account being authenticated.
*
* @var string
*/
protected $_password = null;

/**
* The Pear::Auth container.
*
* @var string
*/
protected $_container = null;

protected $_logger = null;

/**
* Constructor
*
* @param  string $container The Pear::Auth container to use
* @param  array  $options  An array of arrays of Pear::Auth options
* @param  string $username The username of the account being authenticated
* @param  string $password The password of the account being authenticated
* @return void
*/
public function __construct($container, array $options = array(), $username = null, $password = null)
{
$this-&gt;setOptions($options);
if ($username !== null) {
$this-&gt;setUsername($username);
}
if ($password !== null) {
$this-&gt;setPassword($password);
}
$this-&gt;_container = $container;
}

public function setLogger($logger)
{
$this-&gt;_logger = $logger;
}

/**
* Returns the array of arrays of Pear::Auth options.
*
* @return array|null
*/
public function getOptions()
{
return $this-&gt;_options;
}

/**
* Sets the array of arrays of Pear::Auth options to be used by
* this adapter.
*
* @param  array $options The array of arrays of of Pear::Auth options
* @return My_Zend_Auth_Adapter_PearAuth Provides a fluent interface
*/
public function setOptions($options)
{
$this-&gt;_options = is_array($options) ? $options : array();
return $this;
}

/**
* Returns the username of the account being authenticated, or
* NULL if none is set.
*
* @return string|null
*/
public function getUsername()
{
return $this-&gt;_username;
}

/**
* Sets the username for binding
*
* @param  string $username The username for binding
* @return My_Zend_Auth_Adapter_PearAuth Provides a fluent interface
*/
public function setUsername($username)
{
$this-&gt;_username = (string) $username;
return $this;
}

/**
* Returns the password of the account being authenticated, or
* NULL if none is set.
*
* @return string|null
*/
public function getPassword()
{
return $this-&gt;_password;
}

/**
* Sets the password for the account
*
* @param  string $password The password of the account being authenticated
* @return My_Zend_Auth_Adapter_PearAuth Provides a fluent interface
*/
public function setPassword($password)
{
$this-&gt;_password = (string) $password;
return $this;
}

/**
* Authenticate the user
*
* @throws Zend_Auth_Adapter_Exception
* @return Zend_Auth_Result
*/
public function authenticate()
{
require_once &#039;Pear/Auth.php&#039;;

$messages = array();
$messages[0] = &#039;&#039;; // reserved
$messages[1] = &#039;&#039;; // reserved

$username = $this-&gt;_username;
$password = $this-&gt;_password;

if (!$username) {
$code = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
$messages[0] = &#039;A username is required&#039;;
return new Zend_Auth_Result($code, &#039;&#039;, $messages);
}
if (!$password) {
/* A password is required because some servers will
* treat an empty password as an anonymous bind.
*/
$code = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
$messages[0] = &#039;A password is required&#039;;
return new Zend_Auth_Result($code, &#039;&#039;, $messages);
}

//Override $_POST variables required by Auth pear package.
$_POST[&#039;username&#039;] = $username;
$_POST[&#039;password&#039;] = $password;

$_auth = new Auth($this-&gt;_container, $this-&gt;_options, &#039;&#039;, false);

//Enable logging
if (isset ($this-&gt;_options[&#039;enableLogging&#039;]) &amp;amp;&amp;amp; $this-&gt;_options[&#039;enableLogging&#039;] &amp;amp;&amp;amp;
!is_null($this-&gt;_logger)) {
$_auth-&gt;logger = $this-&gt;_logger;
}

$_auth-&gt;start();

if ($_auth-&gt;getAuth()) {
//Destroy Pear::Auth session to avoid unespected behaviour
$_auth-&gt;logout();
$messages[] = &quot;$username authentication successful&quot;;
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $username, $messages);
}

$messages[] = &quot;$username authentication failed.&quot;;
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $username, $messages);
}
}
</pre>
<p>and now let&#8217;s go to use it, for example in our controller</p>
<pre class="brush: php">
....
$auth = Zend_Auth::getInstance();
$config = new Zend_Config_Xml(&#039;APPLICATION_HOME/configs/auth_ldap.xml&#039;, &#039;production&#039;);
$options = $config-&gt;ldap-&gt;toArray();
$authAdapter = new Zend_Auth_Adapter_PearAuth(&#039;LDAP&#039;, $options, $username, $password);
$result = $auth-&gt;authenticate($authAdapter);
if(!is_null($result) &amp;amp;&amp;amp; $result-&gt;isValid()) {
$authorized = true;
}
....
</pre>
<p>In the example above I&#8217;ve used the LDAP container but this should be valid for all supported containers by Pear::Auth.</p>
<p>Hope this helps <img src='http://lucasforge.bmeme.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.bmeme.com/2009/07/using-pear-auth-as-zend-auth-adapter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

