<?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; zend</title>
	<atom:link href="http://lucasforge.bmeme.com/category/zend/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>
		<item>
		<title>ReCaptcha with Zend Form</title>
		<link>http://lucasforge.bmeme.com/2009/04/recaptcha-with-zend-form/</link>
		<comments>http://lucasforge.bmeme.com/2009/04/recaptcha-with-zend-form/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 13:43:03 +0000</pubDate>
		<dc:creator>Luca Corbo</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Form]]></category>
		<category><![CDATA[ReCaptcha]]></category>

		<guid isPermaLink="false">http://lucasforge.2bopen.org/?p=38</guid>
		<description><![CDATA[Requirements Zend Framework (1.7.8 at writing time) Valid ReCaptcha keys (public &#38; private) Simple ReCaptcha Zend Form Class source: application/forms/ReCaptcha.php &#60;?php class Form_ReCaptcha extends Zend_Form { public function init() { $this-&#62;setMethod(&#039;post&#039;); //Add your elements here... $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey); $captcha = new Zend_Form_Element_Captcha(&#039;challenge&#039;, array(&#039;captcha&#039; =&#62; &#039;ReCaptcha&#039;, &#039;captchaOptions&#039; =&#62; array(&#039;captcha&#039; =&#62; &#039;ReCaptcha&#039;, &#039;service&#039; =&#62; $recaptcha))); [...]]]></description>
			<content:encoded><![CDATA[<h3>Requirements</h3>
<ul>
<li><a href="http://framework.zend.com/download/" target="_blank">Zend Framework</a> (1.7.8 at writing time)</li>
<li>Valid <a href="http://recaptcha.net/" target="_blank">ReCaptcha</a> keys (public &amp; private)</li>
</ul>
<h3>Simple ReCaptcha</h3>
<h4>Zend Form Class</h4>
<p>source: <em>application/forms/ReCaptcha.php</em></p>
<pre class="brush: php">
&lt;?php
class Form_ReCaptcha extends Zend_Form
{
    public function init()
    {
        $this-&gt;setMethod(&#039;post&#039;);
        //Add your elements here...

        $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

        $captcha = new Zend_Form_Element_Captcha(&#039;challenge&#039;,
              array(&#039;captcha&#039;        =&gt; &#039;ReCaptcha&#039;,
                    &#039;captchaOptions&#039; =&gt; array(&#039;captcha&#039; =&gt; &#039;ReCaptcha&#039;, &#039;service&#039; =&gt; $recaptcha)));

        $this-&gt;addElement($captcha);

        // Add the submit button
        $this-&gt;addElement(&#039;submit&#039;, &#039;submit&#039;, array(&#039;label&#039; =&gt; &#039;Submit&#039;));
    }
}
?&gt;
</pre>
<h4>Zend Controller Class</h4>
<p>source: <em>application/controller/ReCaptchaController.php</em></p>
<pre class="brush: php">
&lt;?php
class ReCaptchaController extends Zend_Controller_Action
{
    public function indexAction()
    {
        require_once APPLICATION_PATH . &#039;/forms/Contact.php&#039;;

        $form = new Form_ReCaptcha();

        if ($this-&gt;_request-&gt;isPost()) {
            $formData = $this-&gt;_request-&gt;getPost();
            if ($form-&gt;isValid($formData)) {
                $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

                $result = $recaptcha-&gt;verify($this-&gt;_getParam(&#039;recaptcha_challenge_field&#039;),
                                             $this-&gt;_getParam(&#039;recaptcha_response_field&#039;));
                if (!$result-&gt;isValid()) {
                    //ReCaptcha validation error
                    //Your action here...
               }
            }
        }
        $this-&gt;view-&gt;form = $form;
    }
}
?&gt;
</pre>
<h3>Customized ReCaptcha</h3>
<p>You may also want to internationalizing or change colors to ReCaptcha, to do it you need to specify some options for the  Zend_Service_ReCaptcha object.<br />
See the <a href="http://wiki.recaptcha.net/index.php/Main_Page" target="_blank">ReCaptcha wiki</a> for a complete list of available options. </p>
<h4>Zend Form Class</h4>
<p>source: <em>application/forms/ReCaptcha.php</em></p>
<pre class="brush: php">
&lt;?php
class Form_ReCaptcha extends Zend_Form
{
    public function init()
    {
        $this-&gt;setMethod(&#039;post&#039;);
        //Add your elements here...
        $recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);

        //Translate in your language
        $recaptcha_it_translation =
            array(&#039;visual_challenge&#039; =&gt; &quot;Verifica video&quot;,
                  &#039;audio_challenge&#039; =&gt; &quot;Verifica audio&quot;,
                  &#039;refresh_btn&#039; =&gt; &quot;Effettua una nuova verifica&quot;,
                  &#039;instructions_visual&#039; =&gt; &quot;Scrivi le due parole&quot;,
                  &#039;instructions_audio&#039; =&gt; &quot;Scrivi quello che ascolti&quot;,
                  &#039;help_btn&#039; =&gt; &quot;Aiuto&quot;,
                  &#039;play_again&#039; =&gt; &quot;Riascolto di nuovo l&#039;audio&quot;,
                  &#039;cant_hear_this&#039; =&gt; &quot;Scarica l&#039;audio come MP3&quot;,
                  &#039;incorrect_try_again&#039; =&gt; &quot;Incorretto. Prova ancora.&quot;);

        $recaptcha-&gt;setOption(&#039;custom_translations&#039;, $recaptcha_it_translation);
        //Change theme
        $recaptcha-&gt;setOption(&#039;theme&#039;, &#039;clean&#039;);

        $captcha = new Zend_Form_Element_Captcha(&#039;challenge&#039;,
              array(&#039;captcha&#039;        =&gt; &#039;ReCaptcha&#039;,
                    &#039;captchaOptions&#039; =&gt; array(&#039;captcha&#039; =&gt; &#039;ReCaptcha&#039;,
                                             &#039;service&#039; =&gt; $recaptcha)));

        $this-&gt;addElement($captcha);

        // Add the submit button
        $this-&gt;addElement(&#039;submit&#039;, &#039;submit&#039;, array(&#039;label&#039; =&gt; &#039;Submit&#039;));
    }
}
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://lucasforge.bmeme.com/2009/04/recaptcha-with-zend-form/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

