<?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>Tom&#039;s Blog &#187; PHP</title>
	<atom:link href="http://www.encapsulated.org/blog/category/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.encapsulated.org/blog</link>
	<description>An analog guy in a digital world</description>
	<lastBuildDate>Tue, 15 Dec 2009 20:46:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Keeping your code DRY is also in the details</title>
		<link>http://www.encapsulated.org/blog/2009/11/27/keeping-your-code-dry-is-also-in-the-details/</link>
		<comments>http://www.encapsulated.org/blog/2009/11/27/keeping-your-code-dry-is-also-in-the-details/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 20:01:05 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[constants]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=86</guid>
		<description><![CDATA[What you should know already&#8230;
If you have been a developer for a while, chances are you&#8217;ll have come across the term DRY. It stands for Don&#8217;t Repeat Yourself, and it is actually a form of art. Keeping your code DRY ensures you have to think well before you start coding.  Instead of randomly throwing [...]]]></description>
			<content:encoded><![CDATA[<h1>What you should know already&#8230;</h1>
<p>If you have been a developer for a while, chances are you&#8217;ll have come across the term DRY. It stands for Don&#8217;t Repeat Yourself, and it is actually a form of art. Keeping your code DRY ensures you have to think well before you start coding.  Instead of randomly throwing code in your IDE, you should create the reflex of thinking how you can achieve what you want, without having to type too much code.</p>
<p>Not out of laziness, but because you&#8217;re smart. If you ever have to change your code, you&#8217;ll be glad you decided to write that particular bit only in that particular file. Sometimes it&#8217;s much easier to just copy/paste code from one class into another. But if you have to modify that piece of code, you&#8217;ll have to remember all the places that same code appears. Unless you kept it DRY of course.</p>
<p>Usually when an article talks about DRY, it will be in the context of Object Oriented Programming and model abstracts/inheritance/whatnot and making sure you don&#8217;t repeat the same code in different places. But the same principle can also be applied to other aspects of our code.</p>
<p><span id="more-86"></span></p>
<h1>Other aspects that can be kept DRY</h1>
<h2>Use class constants</h2>
<p>Not only does the DRY principle apply to OOP and methods in classes specificly. Other aspects of your classes should be kept DRY too. Recently, I&#8217;ve become a big fan of Class Constants in PHP. I use them to replace almost anything that is prone to change. I&#8217;ll just give you some examples:</p>
<p><strong>Status checking</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;initialized&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">&quot;initialized&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do some stuff</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If it is decided that the status &#8220;initialized&#8221; should be renamed to &#8220;init&#8221;, then I have to change it everywhere I used that string literally.<br />
Here&#8217;s how you could do it better:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">const</span> STATUS_INITIALIZED <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;initialized&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">STATUS_INITIALIZED</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> MyClass<span style="color: #339933;">::</span><span style="color: #004000;">STATUS_INITIALIZED</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// do some stuff</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If we now have to change &#8220;initialized&#8221; to &#8220;init&#8221;, then we only have to change the constant. All other code is left unchanged, and will still work. Now that is DRY.</p>
<p><strong>Database metadata</strong></p>
<p>In a database, the ENUM datatype is often used to restrict the content of a field to a specific set of data it can contain. Excellent examples are the status of the previous example, flags, &#8230; Basically, anything you want to have full control of what could be a possible entry in that field. Keep these values also in Class Constants. If you change the database, then the only other manual thing you have to do, is update the associated constant.</p>
<p><strong>Configuration</strong></p>
<p>Since I use the <a href="http://framework.zend.com">Zend Framework</a> on a daily basis, here&#8217;s an example of how Zend Framework uses constants. First let&#8217;s see how it should not be done:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$translate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Translate<span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'gettext'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'my_it.mo'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'auto'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'scan'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #0000ff;">'filename'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Instead, they use constants:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$translate</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Translate<span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'gettext'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'my_it.mo'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'auto'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'scan'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> Zend_Translate<span style="color: #339933;">::</span><span style="color: #004000;">LOCALE_FILENAME</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Keep your templates DRY</h2>
<p>Not only is it important to keep your business logic DRY, the templates you write should benefit from the same principle. For your website basic, you won&#8217;t rewrite the entire HTML structure for each page on your site. Even if you don&#8217;t use PHP in an OOP way, you will probably still use includes to include parts on your site that will return on every page. Like the menu, or the header and footer. That&#8217;s DRY. But there&#8217;s more.</p>
<p><strong>Use partials (in partials)</strong></p>
<p>Partials is a term from Zend Framework. They are small templates that can be reused anywhere in your larger templates. Say you have a community site, with a messaging feature. Users can send each other messages, users can receive site notices, friend invitations, &#8230; In the inbox of a user, according to the type of message, a different partial can be shown:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$messagesList</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>partial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/inbox_'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$message</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>__toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.phtml'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For each messagetype that can be shown in the inbox, we create a new template. Inside that template, we do the markup for that specific message. Now in the inbox, we want to show the avatar and username of the person who sent you the message, or who invited you to become his friend. It is very tempting to write that part of the template once in inbox_message.phtml, and then copy/paste it in inbox_invitation.phtml and inbox_notice.phtml. It usually happens in a flurry of &#8220;yaaay it works !!&#8221;, and you&#8217;re so happy that you don&#8217;t care if your code becomes sloppy.</p>
<p>Of course, the avatar and username should be put in a new template. That new template is then used in each inbox_* template. Hence the partials inside partials. You also want to add the user ranking? No problem, only 1 file to update.</p>
<h1>Conclusion</h1>
<p>The DRY principle isn&#8217;t only reserved for the big chunks of your business logic. It is also an important part of the details. The small things that can easily lead to a search-and-replace rampage throughout your files. The templates are also often forgotten to be kept DRY.</p>
<p>As with many things: think before you act <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/11/27/keeping-your-code-dry-is-also-in-the-details/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to let DomPDF and Zend Framework play along</title>
		<link>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/</link>
		<comments>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 18:06:28 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[dompdf]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=77</guid>
		<description><![CDATA[For a recent project, I had to generate a PDF for a catalog. Since I&#8217;m using Zend Framework for my development, first thing that sprang in mind was Zend_Pdf. After some investigation, I found it too &#8220;difficult&#8221; to use. Flexible though that component is, I didn&#8217;t feel much for using coordinates to draw each and [...]]]></description>
			<content:encoded><![CDATA[<p>For a recent project, I had to generate a PDF for a catalog. Since I&#8217;m using Zend Framework for my development, first thing that sprang in mind was <a href="http://framework.zend.com/manual/en/zend.pdf.html">Zend_Pdf</a>. After some investigation, I found it too &#8220;difficult&#8221; to use. Flexible though that component is, I didn&#8217;t feel much for using coordinates to draw each and every line and text of that PDF. So I went to find another solution: <a href="http://www.digitaljunkies.ca/dompdf/">DomPDF</a>. DomPDF can generates PDF output, from HTML input. Exactly what I wanted and needed. It was however quite tricky to get it working. If you also have problems with this, read on!<br />
<span id="more-77"></span><br />
DomPDF isn&#8217;t part of the Zend Framework library, and as such doesn&#8217;t really follow the ZF conventions. That&#8217;s what caused me trouble: I couldn&#8217;t load the needed classes for it to work.</p>
<p>So how do you set everything up properly?</p>
<p><strong>First step: set include path</strong></p>
<p>When you&#8217;re setting your include paths, just add one for DomPDF.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span>APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/../../library/dompdf-0.5.1&quot;</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Second step:  make ZF &amp; DomPDF play along</strong></p>
<p>DomPDF has an autoloader which should take care of autoloading the classes for you. All you need to do, is make Zend Framework aware of that autoloader. The solution I found the most was this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'dompdf_config.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">spl_autoload_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DOMPDF_autoload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>However this may not work if you use Zend Framework 1.8 or newer. The real solution I found on a forum, where <a href="http://weierophinney.net/matthew/">Matthew Weier O&#8217;Phinney</a> enlightened another soul with the same problem:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'dompdf_config.inc.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$autoloader</span> <span style="color: #339933;">=</span> Zend_Loader_Autoloader<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// assuming we're in a controller</span>
<span style="color: #000088;">$autoloader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pushAutoloader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DOMPDF_autoload'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Final step: generate the PDF</strong></p>
<p>That&#8217;s the simplest one, as I found no problems with that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dompdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMPDF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_paper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a4&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;portrait&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load_html</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_base_path</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'DOCUMENT_ROOT'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dompdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;document.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There you go, this should set you up to get DomPDF up and running.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/08/27/how-to-let-dompdf-and-zend-framework-play-along/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Leveraging Zend_Auth for building your authentication</title>
		<link>http://www.encapsulated.org/blog/2009/05/07/leveraging-zend_auth-for-building-your-authentication/</link>
		<comments>http://www.encapsulated.org/blog/2009/05/07/leveraging-zend_auth-for-building-your-authentication/#comments</comments>
		<pubDate>Thu, 07 May 2009 21:06:09 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[codebase]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend_auth]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=63</guid>
		<description><![CDATA[In a series of posts, I will address the issue of authentication and authorization of users into your application. When you build a website with any form of back office, you will need to grant users access to the back office (authentication), and determine what actions they are allowed to take (authorization). The Zend Framework [...]]]></description>
			<content:encoded><![CDATA[<p>In a series of posts, I will address the issue of authentication and authorization of users into your application. When you build a website with any form of back office, you will need to grant users access to the back office (authentication), and determine what actions they are allowed to take (authorization). The Zend Framework has two tools just for that job: <a href="http://framework.zend.com/manual/en/zend.auth.html">Zend_Auth</a> and <a href="http://framework.zend.com/manual/en/zend.acl.html">Zend_ACL</a>. In this first part, I will build a custom User class, that will allow the programmer to perform a simple authentication of a user. This User class will be integrated into a so called &#8220;code base&#8221; or &#8220;framework&#8221; that you can use for your own applications. As of this moment, that framework doesn&#8217;t exist yet. I will gradually build it as my blog posts are added. Please look for the tag &#8220;<a href="http://www.encapsulated.org/blog/tag/codebase/">codebase</a>&#8221; if you want all of these posts.<br />
<span id="more-63"></span></p>
<p>First things first: if you have read my<a href="http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/"> previous posts about securing your Flex &#8211; PHP services</a>, you will have seen that I have wrapped the bootstrapping process of that application in a class named &#8220;CB_Application&#8221;. That class is still under very heavy development. Since the release of Zend Framework 1.8, they have introduced <a href="http://framework.zend.com/manual/en/zend.application.html">Zend_Application</a>, just for that purpose. So it is very likely that in the future, I will replace my CB_Application class with something that makes use of Zend_Application. For now, I will focus on the other functionality, and leave CB_Application as it is, without bothering you with the details. But full disclosure will come, somewhere in the future.</p>
<p><strong>Structure</strong></p>
<p>I haven&#8217;t decided on some kind of structure for this framework yet. I think it will evolve as I add more classes and functionality to it. Below is a screenshot of how it currently looks like.</p>
<div id="attachment_67" class="wp-caption alignnone" style="width: 287px"><a href="http://www.encapsulated.org/blog/wp-content/uploads/2009/05/screenshot-documents-file-browser1.png"><img class="size-full wp-image-67" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/05/screenshot-documents-file-browser1.png" alt="Codebase Structure" width="277" height="208" /></a><p class="wp-caption-text">Codebase Structure</p></div>
<p>As you can see, quite empty. What we will be constructing during this blog post, is the CB_User class, the CB_Table_Users class and the CB_Auth_Adapter_Chap class. Once that is complete, you should include the Code Base folder into your include paths in your bootstrap. If that is done, you can just simple use your framework anywhere in your models or controllers.</p>
<p><strong>Database tables for the User class</strong></p>
<p>I&#8217;m going to concentrate on the authentication of the users, so I will keep the amount of data small. It will be the base for the User class, and will be extended with functionality and other data as the framework evolves. Right now, the only properties for a user we need, are: a username, a password, an e-mail address, and a flag to indicate if the user is active or not.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`cb_users`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`username`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`password`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`email`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`active`</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;</pre></td></tr></table></div>

<p>Small remark:All passwords are encrypted via the AES encryption method. I won&#8217;t use a hashing algorithm, because during the authentication process, I would like to know the exact password. In case I want to build PHP services for Flex, I will need that password, and I can&#8217;t come from the client directly. But that&#8217;s nothing to worry about now; just remember that the password will be encrypted via AES.</p>
<p>For testing purposes, we will add a dummy user via SQL now. Normally this will be done during some sort of registration process, but that&#8217;s for the future. Let&#8217;s add the user &#8220;test&#8221; with password &#8220;password&#8221; into the database. As key for the AES encryption, I have used &#8220;c0d3ba53&#8243;. You know, l33t speak as they say <img src='http://www.encapsulated.org/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  You should remember that key. I will hard code it into the code, as this is just a demonstration. In my real code, I have this key in a configuration file, and it is fetched by CB_Application. (I know, it&#8217;s irritating, but it will come up here an there during this blog post. Sorry!)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> cb_users <span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">,</span>password<span style="color: #66cc66;">,</span>email<span style="color: #66cc66;">,</span>active<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'test'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'S		í‹ôÚ×ýfÕ‘ˆ?	'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'test@example.com'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Building the authentication</strong></p>
<p>Authenticating a user will be very straightforward. There is only 1 condition: I want to be able to use CHAP and regular authentication. CHAP requires 3 parameters (username, challenge, signature) and regular authentication requires only 2 (username, password). I will incorporate that into 1 authentication function that will dynamically decide which authentication we will be doing, via the amount of parameters.</p>
<p>As I said before, the authentication process will make use of the Zend_Auth component of the Zend Framework. There are many different ways you could authenticate a user. Most commonly is against a database. Zend_Auth can accommodate to all these different ways, by making use of adapters. An auth adapter basically does the authentication, and Zend_Auth uses it. There are already some predefined adapters, but for the CHAP authentication, we will have to create our own.</p>
<p>To access all the data from the cb_users table in the database, We extend the regular Zend_Db_Table class with our own information:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> CB_Table_Users <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Abstract <span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cb_users'</span><span style="color: #339933;">;</span>
	protected <span style="color: #000088;">$_primary</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Nothing fancy, we just specify our table name, and the primary key, and then we&#8217;re good to go.</p>
<p>Before we can do the authentication, we first need to create our custom CHAP authentication adapter. The Zend Framework is designed to be extensible, and there is in interface for creating your own authentication adapters. The only method you need to implement is the authenticate() method. But you can add other methods if you deem that necessary. I will implement some setters for the credentials, and then a method that mimics the behaviour of the getResultRowObject() method from Zend_Auth_Adapter_DbTable. I added this extra functionality, because that will make it easier to process everything in the CB_User class. I have overdone myself documenting all the code, so I won&#8217;t explain it in depth anymore. If you are considering using this code, please replace all references to CB_application with your own code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * CB_Auth_Adapter_Chap class
 *
 * @author Tom Van Herreweghe
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CB_Auth_Adapter_Chap implements Zend_Auth_Adapter_Interface <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_signature</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_challenge</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_data</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set the username
     *
     * @param string $username
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUsername<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set the challenge, used to calculate the signature
     *
     * @param string $challenge
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setChallenge<span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_challenge <span style="color: #339933;">=</span> <span style="color: #000088;">$challenge</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set the client-side calculated signature
     *
     * @param string $signature
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSignature<span style="color: #009900;">&#40;</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_signature <span style="color: #339933;">=</span> <span style="color: #000088;">$signature</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Authenticate the user
     *
     * @return Zend_Auth_Result
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> authenticate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// First, we need to retrieve the data, associated with the given username.</span>
        <span style="color: #666666; font-style: italic;">// The password will be decrypted for later use:</span>
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_Table_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">,</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quoteInto</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AES_DECRYPT(password,?) AS password&quot;</span><span style="color: #339933;">,</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username = ?&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username<span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;active = 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchAll</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$authenticated</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// If we found a match, verify the other credentials</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// store the database data locally</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Compare calculated signature with given signature:</span>
            <span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_challenge<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// if the given signature and the calculated signature match, the user is authenticated:</span>
            <span style="color: #000088;">$authenticated</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_signature<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// We prefill the authentication result with a FAILURE</span>
        <span style="color: #000088;">$authResult</span> <span style="color: #339933;">=</span> Zend_Auth_Result<span style="color: #339933;">::</span><span style="color: #004000;">FAILURE_UNCATEGORIZED</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$authMessages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$authenticated</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// user is authenticated, overwrite the auth result:</span>
            <span style="color: #000088;">$authResult</span> <span style="color: #339933;">=</span> Zend_Auth_Result<span style="color: #339933;">::</span><span style="color: #004000;">SUCCESS</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Couldn't authenticate the user, set a message:</span>
            <span style="color: #000088;">$authMessages</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Login failed'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// return the result:</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Result<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$authResult</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_username<span style="color: #339933;">,</span> <span style="color: #000088;">$authMessages</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Mimics (actually almost copies) the behaviour of the getResultRowObjec() from Zend_Auth_Adapter_DbTable
     *
     * @param array $returnColumns
     * @param array $omitColumns
     * @return stdClass
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResultRowObject<span style="color: #009900;">&#40;</span><span style="color: #000088;">$returnColumns</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$omitColumns</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// If no data is set, return false:</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$returnObject</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$returnColumns</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Process only the specified columns:</span>
&nbsp;
            <span style="color: #000088;">$availableColumns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$returnColumns</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$returnColumn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$returnColumn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$availableColumns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$returnObject</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$returnColumn</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#91;</span><span style="color: #000088;">$returnColumn</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$returnObject</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$omitColumns</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Process all columns, except the ones specified:</span>
&nbsp;
            <span style="color: #000088;">$omitColumns</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$omitColumns</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #b1b100;">as</span> <span style="color: #000088;">$resultColumn</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$resultValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$resultColumn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$omitColumns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$returnObject</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$resultColumn</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resultValue</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$returnObject</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Process all columns, without restrictions:</span>
&nbsp;
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #b1b100;">as</span> <span style="color: #000088;">$resultColumn</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$resultValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$returnObject</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$resultColumn</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resultValue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$returnObject</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The puzzle is almost complete now. Let&#8217;s put the pieces together in the CB_User class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * CB_User class
 *
 * @author Tom Van Herreweghe
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> CB_User <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Getter magical function to get properties
     *
     * @param string $key
     * @return mixed
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown property '<span style="color: #006699; font-weight: bold;">{$key}</span>'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Setter magical function to set properties
     *
     * @param string $key
     * @param mixed $value
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$allowed</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #339933;">,</span><span style="color: #000088;">$allowed</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Authenticate a user via credentials
     * The amount of parameters is variable, and are therefore not explicitly declared
     *
     * @return Zend_Auth_Result
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> authenticate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Get all the arguments:</span>
        <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Initialize an authAdapter, but set it to null</span>
        <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 2 arguments: We assume they are username &amp;amp; password. This means regular authentication</span>
            <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Adapter_DbTable<span style="color: #009900;">&#40;</span>
                CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// This returns an instance of a database adapter</span>
                CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// this will return a prefix, defined in a config.</span>
                <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'password'</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// set the necessary credentials</span>
            <span style="color: #000088;">$authAdapter</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredential</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentialTreatment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AES_ENCRYPT(?, '&quot;</span><span style="color: #339933;">.</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;') AND active = 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// CredentialTreatment says we'll use AES_Encrypt to encrypt the given password, and the user must be active.</span>
                <span style="color: #666666; font-style: italic;">// CB_application::getInstance()-&gt;getKey() returns a predefined key from a config file.</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 3 arguments: We assume username, challenge and signature. This means custom digest authentication</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Here, we use our custom created CB_Auth_Adapter_DbTable:</span>
            <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_Auth_Adapter_Chap<span style="color: #009900;">&#40;</span>
                    CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDbAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                    CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'users'</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">'password'</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// set the necessary credentials:</span>
            <span style="color: #000088;">$authAdapter</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUsername</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setChallenge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSignature</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$authAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No authAdapter specified. Please check all the arguments&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Zend_Auth will now try to authenticate, using our auth adapter:</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$authAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">/*
             * When authentication is succesfull, we will write an instance of CB_User to the storage
             * Later on, when we want to know which user is authenticated, we will retrieve this CB_User from the storage
             * This all hapens quite transparently via regular Zend Framework code
             */</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// get the resulting line from the database, except the &quot;password&quot; column:</span>
            <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResultRowObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// retrieve an an instance of CB_User via the user id:</span>
            <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// write our CB_User object to the storage of Zend_Auth</span>
            <span style="color: #666666; font-style: italic;">// Zend_Auth::getInstance()-&gt;getIdentity() will return that CB_User object</span>
            Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStorage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// Return the Zend_Auth result:</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Return an instance of the CB_User class, via the user_id
     *
     * @param int $user_id
     * @return CB_User
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user_id</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_Table_Users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">from</span><span style="color: #009900;">&#40;</span>CB_application<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTablePrefix</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cb&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$select</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id = ?&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$user_id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">where</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;active = 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$select</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No user found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Transform the raw data from the database into a CB_User object
     *
     * @param Zend_Db_Table_Row $data
     * @return CB_User
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> factory<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_a</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Zend_Db_Table_Row&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CB_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span>       <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span>    <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$user</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Wrong datatype for the CB_User::factory() method&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we have written our classes, we want to use them. Mind you, this is just a demonstration. We will first log the user in via the CHAP method, then we do a logout, and then we log the user in via the regular method.:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Layout&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ViewRenderer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNoRender</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Verify if we aren't logged in yet</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Oops, we were still logged in from a previous demonstration..</span>
            <span style="color: #666666; font-style: italic;">// Let's log out:</span>
            Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">// Alright, at this point we're not logged in.. Let's proceed:</span>
&nbsp;
        <span style="color: #000088;">$challenge</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'challenge'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &lt;- this should be a random string</span>
        <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Calculate the signature:</span>
        <span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Authenticate via CHAP:</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> CB_User<span style="color: #339933;">::</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Show the result</span>
        <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Logout again for another demonstration:</span>
        Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Lets try the other authentication method:</span>
        <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> CB_User<span style="color: #339933;">::</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;demonstration is over !&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it for now. We have completed a first part of a framework: create a User class, and allow a user to authenticate. Right now, we can&#8217;t do much with it yet, but it will come in handy. We still need a way of registering users, but most importantly, we have to determine what that user can do now that he&#8217;s logged in. I will write about that in another blog post about Zend_ACL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/05/07/leveraging-zend_auth-for-building-your-authentication/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Security with Zend_AMF and Flex &#8211; Part 2: Practise</title>
		<link>http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/</link>
		<comments>http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 21:58:45 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[air]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend_amf]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=31</guid>
		<description><![CDATA[In my previous post &#8220;Security with Zend_AMF and Flex &#8211; Part 1: Theory&#8220;, I explained the theory behind securing your Flex-PHP calls. After the theory comes the practise. I will only provide snippets for the PHP side of this story, as I&#8217;m totally ignorant about Flex and ActionScript. I used Zend_AMF, written by Wade Arnold, [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post &#8220;<a title="Security with Zend_AMF and Flex - Part 1: Theory" href="http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/">Security with Zend_AMF and Flex &#8211; Part 1: Theory</a>&#8220;, I explained the theory behind securing your Flex-PHP calls. After the theory comes the practise. I will only provide snippets for the PHP side of this story, as I&#8217;m totally ignorant about Flex and ActionScript. I used Zend_AMF, written by <a title="Wade Arnold" href="http://wadearnold.com/blog/">Wade Arnold</a>, to handle all the communications between Flex and PHP. If you need to know the basics, please <a href="http://framework.zend.com/manual/en/zend.amf.html">read the documentation</a> first.<br />
<span id="more-31"></span></p>
<p><strong>Project structure</strong></p>
<p>For the project, I used a slightly modified <a title="Application layout" href="http://framework.zend.com/wiki/display/ZFDEV/Choosing+Your+Application%27s+Directory+Layout#ChoosingYourApplication%27sDirectoryLayout-ConventionalModular">Conventional Modular</a> layout. The basics remains the same, but I have added support for so called versions of the application. The layout I maintain is not really important for the project. However, at the end of the article, you&#8217;ll be able to download everything, and I thought it necessary to explain the structure first. All code is part of the default module, and all classes are located in the &#8220;models&#8221; directory.</p>
<div id="attachment_51" class="wp-caption alignnone" style="width: 486px"><img class="size-full wp-image-51" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/screenshot.png" alt="Application layout" width="476" height="729" /><p class="wp-caption-text">Application layout</p></div>
<p><strong>The remoting endpoint</strong></p>
<p>Flex and AIR need to connect to an endpoint. A URL you provide, the point of access to your API. I have created only 1 endpoint. Ideally, you should make 2 endpoints: one for logging in, and one for the rest of the calls. That way, in the future, you could do the login process via an SSL secured line. The remoting endpoint is located in the <em>IndexController.php</em>, since that file is the door to the outside world for the project.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #666666; font-style: italic;">// Explicitly loading dependencies</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies_AMF_System&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies_AMF_Login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AMF_VO_Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">loadClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AMF_VO_Message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> IndexController <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action<span style="color: #009900;">&#123;</span>
		<span style="color: #009933; font-style: italic;">/**
		 * Everything to handle the rest of the API requests
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Layout&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHelper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ViewRenderer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setNoRender</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$server</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Amf_Server<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;../models/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setClassMap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MessageVO&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;AMF_VO_Message&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setClassMap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ErrorVO&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;AMF_VO_Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProduction</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$server</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Basically, the indexAction is where my endpoints is. There I set up the Zend_AMF server. After setting it up, the server is ready to listen to requests. All requests are handled by the classes from the folder specified with <em>addDirectory()</em>. In our case, the Proxy classes will handle all the requests. After specifying the directory of the classes, we set up the class mapping. Here, we tell Zend_AMF, how the mapping should be between our PHP classes on the server side, and the Flex classes on the client side. In my case, all Flex classes end with &#8220;VO&#8221;. Once the mapping is complete, our server is ready to give a response to the client. Note that I explicitly load every class in IndexController.php. For some reason, Zend_AMF isn&#8217;t able to use autoloading, which is a too bad.</p>
<p><strong>Responses to the client</strong></p>
<p>To have a cleaner interface, I have opted to return structured responses to the Flex client. When the client makes a call, there are only 2 kind of responses he can expect. When the call is successful, the response will be an object of the type <em>AMF_VO_Message</em> that&#8217;s being mapped to a <em>MessageVO</em> in Flex. Or it will be an object of the type <em>AMF_VO_Error</em>, mapped to <em>ErrorVO</em>, when an exception is thrown. This is very easy for the client side coder. All other data is encapsulated in the <em>AMF_VO_Message</em> object, and that makes it easy to process everything. It&#8217;s also easier for the PHP developer, since it is now possible to return a lot more information to the client. If you decide to use caching, then the <em>AMF_VO_Message</em> response can contain information about that; timestamps; transaction id&#8217;s; &#8230; Anything you would expect an API to return.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #009933; font-style: italic;">/**
     * This class is used to wrap data for output to the AMF adapter
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">class</span> AMF_VO_Message <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Properties:</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$status</span>		<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$timestamp</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$transaction</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$data</span>		<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// mixed</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cachehit</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// boolean</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The class is basically a collection of public properties, where I can add data that is useful for the client, like timestamps, transaction-id&#8217;s, &#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #009933; font-style: italic;">/**
     * This class is used to wrap data for output to the AMF adapter
     *
     */</span>
    <span style="color: #000000; font-weight: bold;">class</span> AMF_VO_Error <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Properties:</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$timestamp</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$errorCode</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$file</span>		<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$linenumber</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// integer</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$messages</span>	<span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// string</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Again, some properties that are useful for error tracking.</p>
<p><strong>First phase: login security</strong></p>
<p>In the first part of this series, I explained how securing the login process works. To initialize a login procedure, the client first needs to notify the server that an authentication attempt will be made. This is done by means of the<em> challenge()</em> function from the <em>Proxies_AMF_Login</em> class. Remember, all available calls are exposed via my so called Proxy classes. This is not mandatory, but a convention I want to impose on the users of the API I&#8217;m going to write. In Flex, the front-end developer can specify the class (proxy) and method (API call) to invoke.</p>
<p>When a client request a challenge from the server, I don&#8217;t necessarily want to know. Therefore, I can keep this function clean and simple. All code is explained with in-line comments.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Login proxy class to expose classes to AMF
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Proxies_AMF_Login <span style="color: #000000; font-weight: bold;">extends</span> Proxies_AMF <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/**
         * Start the authentication session, by requesting a challenge.
         * The client then uses this challenge to calculate a signature (or digest)
         * When signing on, the username &amp;amp; signature is used, instead of the password
         *
         * @return AMF_VO_Message
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> challenge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$arguments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        protected <span style="color: #000000; font-weight: bold;">function</span> _challenge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 1. generate the challenge</span>
            <span style="color: #000088;">$challengeString</span> <span style="color: #339933;">=</span> AMF_Login<span style="color: #339933;">::</span><span style="color: #004000;">generateChallenge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// 2. remember the challenge !</span>
            <span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$challengeString</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// 3. return the challenge to the client</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$challengeString</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/**
         * Authenticate the user
         *
         * @param string $username
         * @param string $signature
         * @return bool|AMF_VO_Error
         */</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> signOn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$arguments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span><span style="color: #000088;">$arguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> _signOn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// 1. Digg up the challenge from our memory:</span>
            <span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$challenge</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// 2. Verify if we can authenticate the user</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>AMF_Login<span style="color: #339933;">::</span><span style="color: #004000;">signOn</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            	<span style="color: #666666; font-style: italic;">// User authenticated, let's forget the challenge</span>
                <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            	<span style="color: #666666; font-style: italic;">// Failed to authenticate the user</span>
            	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">clearIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not authenticate the user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A <em>challenge()</em> call is logically followed by a <em>signOn()</em> request (<em>verifySignature()</em> will be explained later on) to complete the login process. As you can see, for both <em>challenge()</em> and <em>signOn()</em>, there is a protected counterpart <em>_challenge()</em> and <em>_signOn()</em>. This has to do with the <em>processRequest()</em> call in both of the methods. More about that call later on, but for now, you only need to know that <em>processRequest()</em> does some administrative tasks, and wraps the responses for Zend_AMF server in a structured response.</p>
<p>An attentive reader will have noticed that I make use of an AMF_Login class, and I forgot to explain what and how. No wories, it&#8217;s not very mystical. I have just created that class to bundle some functionality. However, it does use some functionality from classes I have not written myself. They fall under the copyright of my employer, and I don&#8217;t want to infringe on that. So I won&#8217;t show you how AMF_User is written, but from the comments, it should be obvious what the purpose is. In the downloadable source, you&#8217;ll find a simplified version of AMF_User, which doesn&#8217;t really do anything, except illustrating the purpose.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AMF_Login <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Returns a random string, that is used as Challenge for the sign-on
	 * procedure
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> generateChallenge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">uniqid</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> signOn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMF_User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span> <span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Method to verify if each signature for an API call is valid
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> verifySignature<span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// 1. get the password from the session (it was put there during authentication)</span>
		<span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 2. calculate signature with given $challenge</span>
		<span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// 3. compare calculated signature, with given signature</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Incorrect signature given&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This concludes the first part of the security measures: logging in. By now, a user can be logged in, and stay logged in, by means of the browser session.</p>
<p><strong>Second phase: API call security</strong></p>
<p>In the second phase, I want to verify all API calls. What we need, is a way to process every API call, and do a number of actions for each of those call. I want to be able to:</p>
<ul>
<li>Log what calls were made, by what user, what arguments, and what was the response</li>
<li>Have the possibility to cache certain calls</li>
<li>Use Zend_ACL to verify if a certain user has access to certain functionality</li>
<li>Create my structured response</li>
<li>Trap errors (yes, they should be trapped in the <em>ErrorController.php</em>, but I haven&#8217;t fully investigated that option yet)</li>
<li>Write code once, use it multiple times</li>
</ul>
<p>As you can see, there are plenty of requirements. There is one option to fulfil them all: Make sure all calls invoke 1 function that handles all the &#8220;administration&#8221;. The perfect solution exists, and is named <em>__call()</em> in PHP5. If a method does not exist in a class, then the <em>__call()</em> magic function is called with the exact same arguments. There, you can create all functionality for the administration, and then request another function that will do the real work. I know, quite the abstract explanation, but I won&#8217;t go any further on that road because using <em>__call()</em> fails. Zend_AMF uses Zend_Reflection, a <a title="PHP Reflection" href="http://us3.php.net/reflection">PHP reflection class</a>. Simply put, a reflection calls detects all publicly accessible methods and properties of a given class. Since <em>__call()</em> is a magic function, it isn&#8217;t publicly accessible. Hence, it won&#8217;t be discovered by Zend_Reflection, and consequently, Zend_AMF will not be able to make use of your functionality if you rely on <em>__call()</em>. That took me a while to discover, but if you think about it, it&#8217;s quite logical.</p>
<p>To work around this problem, I have decided to create my own <em>__call()</em> method, and I have named it <em>processRequest()</em>. This will do the exact same as I have explained in the paragraph above: handle the administration (logging, caching, &#8230;). The diagram below will immediately shine a light on the working.</p>
<div id="attachment_49" class="wp-caption alignnone" style="width: 398px"><img class="size-full wp-image-49" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/diagram12.png" alt="Diagram: handle requests" width="388" height="102" /><p class="wp-caption-text">Diagram: handle requests</p></div>
<p>A request comes in via <em>helloWorld()</em>. It then travels to <em>processRequest()</em> for the administrative tasks. Next it travels to<em> _helloWorld()</em> that will do what needs to be done and give a response. The response then will travel back from <em>_helloWorld()</em> to <em>processRequest()</em>. There, the response is packed in the structured response, and then that structured response is sent back to <em>helloWorld()</em>. There, it is delivered to the Zend_AMF server, and ultimately, to the Flex client.</p>
<p>To write the code only once and use it many times, the only option is to place the <em>processRequest()</em> function in the parent of our Proxy classes: <em>Proxies_AMF.php</em>. Below, you will find a simplified version of the code I have written for a recent project. Everything is explained in in-line comments.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * AMF proxy class to expose classes to AMF
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Proxies_AMF <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Some classes do not need verification, like Login. Only the other API calls should be verified</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$exemptVerification</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Proxies_AMF_Login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> processRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$method</span><span style="color: #339933;">,</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		try <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Retrieve the parameters to verify the identiy of the requesting party:</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exemptVerification</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$challenge</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Verify the given signature, before executing the call, unless the request is to a method from an exempt class:</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #339933;">,</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exemptVerification</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> AMF_Login<span style="color: #339933;">::</span><span style="color: #004000;">verifySignature</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// Verify if the requested method actually exists:</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;_<span style="color: #006699; font-weight: bold;">{$method}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$cachehit</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// support for caching could be added, but not now.</span>
					<span style="color: #000088;">$returnValue</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;_<span style="color: #006699; font-weight: bold;">{$method}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$arrArguments</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #666666; font-style: italic;">// wrap the response of the API call in an AMF_VO_Message object, and return it:</span>
					<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wrapOutput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$returnValue</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #339933;">,</span><span style="color: #000088;">$cachehit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The method '<span style="color: #006699; font-weight: bold;">{$method}</span>' does not exist.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// An exception is already thrown in AMF::Login::verifySignature()</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$errorVO</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMF_VO_Error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Get the currently logged in user</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timestamp</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y-%m-<span style="color: #009933; font-weight: bold;">%d</span> %H:%M:%S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorCode</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// usually, this will be &quot;Exception&quot;, unless we introduce custom Exceptions</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">file</span>			<span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">linenumber</span>		<span style="color: #339933;">=</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">messages</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$errorVO</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">stacktrace</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">debug_backtrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// return response</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$errorVO</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> wrapOutput<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #000088;">$transaction</span><span style="color: #339933;">,</span><span style="color: #000088;">$cachehit</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$message</span>				<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMF_VO_Message<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">status</span>		<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timestamp</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%Y-%m-<span style="color: #009933; font-weight: bold;">%d</span> %H:%M:%S&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">transaction</span>	<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$transaction</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$transaction</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;no_transaction&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span>		<span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cachehit</span>		<span style="color: #339933;">=</span> <span style="color: #000088;">$cachehit</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The function <em>processRequest()</em> is now very basic. It only incorporates authentication of the requester, and catching errors. I will not implement the rest of the requirements during this article, since that would lead me outside the scope of this article. If you feel like implementing extra functionality for all your calls, then <em>processRequest()</em> is the method to elaborate on. For a business project, I have extended it with everything mentioned in my list earlier.</p>
<p>The hard part is over now. Most of the functionality has been written. Each API call will be accompanied by 2 extra parameters: a client-chosen challenge, and a calculated signature. The given challenge and signature are just the first two parameters of each of our service calls. These parameters will be verified in <em>processRequest()</em>.</p>
<p>The actual verification is done by the <em>verifySignature() </em>method in AMF_Login class. To make it easy, below is a copy of that method again. The parameters are the challenge, chosen by the client, and the signature, calculated by the client. The method will then use that same challenge, and the password stored in a session object, and then calculate the accompanying signature. If both signatures match, then we have a winner! If they don&#8217;t match, then somebody is trying to tamper with the requests. It&#8217;s up to you how you deal with that. I just output an AMF_VO_Error object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Method to verify if each signature for an API call is valid
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> verifySignature<span style="color: #009900;">&#40;</span><span style="color: #000088;">$challenge</span><span style="color: #339933;">,</span><span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// 1. get the password from the session (it was put there during authentication)</span>
	<span style="color: #000088;">$amfNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AMF_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 2. calculate signature with given $challenge</span>
	<span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amfNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// 3. compare calculated signature, with given signature</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$calcSignature</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$signature</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Incorrect signature given&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>With this last functionality explained, I think I have everything covered. Only thing left, is giving you an example on how our processRequest() method is used:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * System proxy class to expose classes to AMF
 *
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Proxies_AMF_System <span style="color: #000000; font-weight: bold;">extends</span> Proxies_AMF <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Say hello to the world
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> helloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">processRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FUNCTION__</span><span style="color: #339933;">,</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> _helloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Hello World !!&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I mentioned the <em>helloWorld()</em> function earlier, and here is the implementation. The Flex client calls the <em>helloWorld()</em> function (without the underscore !!), and gives as paremeters the challenge and signature. You&#8217;ll see that those parameters aren&#8217;t explicitly mentioned in the function definition, but that is no problem. In that function, <em>processRequest()</em> is called, which will do the verificiation and then call<em> _helloWorld()</em>. The response from <em>_helloWorld()</em> will then travel back, over <em>processRequest()</em>, via <em>helloWorld()</em>, to Zend_AMF, stopping at the Flex client.</p>
<p><strong>Ammendments</strong></p>
<p>This article is the fruit of a quite lengthy development process at the company I work at. During this period, I have made many mistakes regarding Zend_AMF, and learned from them. To illustrate that development is a continuous learning process, I will end with a &#8220;mistake&#8221; I just recently discovered:</p>
<p>When I first implemented these security measures, I used the URL to transport the challenge and signature for each API call. This way, could keep my interfaces clean, without having to take into account the challenge and signature. In Flex, I had the front-end developer create a dynamical endpoint for each call. Each endpoint connection only made one call, since in the URL, the challenge and signature were added. However, when an endpoint connection is established, it is standard procedure of AMF to ping the server once. As a result, each call generated a new ping request, effectively doubling the amount of calls. I don&#8217;t have to explain that this is quite a big overhead. To overcome this, I had to extend each API call to accept the challenge and signature as a parameter of that call. And the result of my mistake is that you can enjoy a &#8220;better&#8221; implementation than what I had 2 days ago.</p>
<p>What I want to say: This method is not fool proof, and I have no doubt that there are probably many ways of improving the security. If you happen to have suggestions, by all means, let me know. I&#8217;m happy to learn from you.</p>
<p><strong>Download all files</strong>: <a href="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/amfsecurity.zip">zip</a> | <a href="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/amfsecurity.tar.gz">tar</a><br />
<strong>Credits</strong> where credits are due: Thanks <a href="http://www.vicr.be/blog/">Vic Rau</a> for the small <a href="http://www.encapsulated.org/dev/flex/application/v1/public/images/UnitTesting.html">UnitTest</a> (yes, it is also in the downloadable archive)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/04/15/security-with-zend_amf-and-flex-part-2-practise/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Security with Zend_AMF and Flex &#8211; Part 1: Theory</title>
		<link>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/</link>
		<comments>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 18:24:46 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[chap]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=24</guid>
		<description><![CDATA[In a series of two posts, I will explain how to secure the communication between a Flex client and PHP server architecture. The first part will explain how I envision that security, and in the second part I will show snippets of PHP code for the practical implementation.
I&#8217;m currently working together with my friend and [...]]]></description>
			<content:encoded><![CDATA[<p>In a series of two posts, I will explain how to secure the communication between a Flex client and PHP server architecture. The first part will explain how I envision that security, and in the second part I will show snippets of PHP code for the practical implementation.</p>
<p>I&#8217;m currently working together with my friend and colleague <a title="VICR" href="http://www.vicr.be">Vic</a> on a client-server application that involves <a title="Flex" href="http://www.adobe.com/products/flex/">Flex</a> on the front-side, and PHP (<a href="http://zendframework.com/">Zend Framework</a>) on the back. Since I&#8217;m the PHP guy, I&#8217;m in charge of creating the API for his Flex application. For the moment, the project will only be accessed locally from the client&#8217;s network. But there is a possibility that in a later stage, it might open up to the general public. One of my main concerns was how we could make every API call as secure as possible. This without making it too complicated, or involve too many service calls that might slow everything down.<br />
<span id="more-24"></span></p>
<p><strong>First login&#8230;</strong></p>
<p>The first aspect of securing the application, is authentication of users. The application is accessible to multiple users, and those are grouped in various groups. Each group has different privileges. To identify every user, they are given usernames and passwords. My biggest concern with the login process, is that we didn&#8217;t have access to a secure connection via SSL. This means that every communication between the client and server software can be monitored by network sniffers. Flex uses the <a href="http://en.wikipedia.org/wiki/Action_Message_Format">AMF protocol</a> to communicate with Zend_AMF. Even though this is a binary stream, the specifications are open to everyone. So it is possible to intercept communications, and read everything out.</p>
<p>I wanted to make sure that during the login process, the password was not exposed to the outside world via the communications line. Because once the password and username are intercepted, then it would be easy to start making fake API calls to the server. The solution I had in mind, is called CHAP &#8211; <a title="CHAP protocol" href="http://en.wikipedia.org/wiki/Challenge-handshake_authentication_protocol">Challenge-Handshake Authentication Protocol</a>. Here&#8217;s a diagram about how this protocol works.</p>
<p><img class="aligncenter size-full wp-image-26" src="http://www.encapsulated.org/blog/wp-content/uploads/2009/04/diagram1.png" alt="CHAP diagram" width="350" height="217" />The main purpose of using CHAP, is to authenticate users, without having to transmit the password between the client and the server. To accomplish this, we have to replace the password with something else, but still allowing the server to recognize the correct user, and log him in. I call this password replacer the &#8220;signature&#8221;.</p>
<p>In the first phase of the diagram, the client requests a &#8220;challenge&#8221; from the server. A challenge is a key that will be used by both the client and the server, to create the signature. During the first phase, the client asks the server what key will be used to encrypt the password. It is important that both the client and the server use the exact same challenge.</p>
<p>During the second phase, an authentication request is being transmitted. Instead of using the username and password, we use the username and signature. The final signature is calculated like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$signature</span> <span style="color: #339933;">=</span> <span style="color: #990000;">MD5</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">MD5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$challenge</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The client calculates this signature, and sends it to the server. The server then looks up the given username in the list of possible users. The corresponding password is then used by the server, to also calculate a signature, with the earlier given challenge. The outcome of that comparison is given during the third and last phase of the CHAP authentication.</p>
<p>This is not a very difficult protocol to implement, but according to me, it still provides a reasonable amount of security during the login process.</p>
<p><strong>&#8230; and then the rest of the calls.</strong></p>
<p>Now that a user is logged in, the most important stuff still has to happen: interactivity with the server by calling API methods. Once the user is authenticated, he remains authenticated by using the browser session. So now we have to keep in mind that other people might try to steal the session, and invoke undesired API calls. During each call, the server needs to be sure that it was still the same client making the call, and not some <a title="Man-in-the-middle attack" href="http://en.wikipedia.org/wiki/Man-in-the-middle_attack">man-in-the-middle</a>.</p>
<p>For this, I decided to elaborate on the CHAP protocol, used for authentication. How about we could keep the system, where the client signs its calls with a signature. For obvious reasons, it is not possible to recycle the signature from the login procedure. But I wanted to force the client to generate a new signature for each API request. There are still two variables to generate the signature: the password and the challenge.</p>
<p>It was not a practical option to ask the user&#8217;s password for each and every API call. Instead, once the user was authenticated, I had the Flex application remember the password in its memory. This way, it could be recycled to generate new signatures.</p>
<p>The only problem was the challenge. A first option was to have the client request a challenge from the server. But this would mean that for each regular API call, there would be an additional call for a challenge. That would double the amount of API calls. I was afraid that in the long run, this would generate too much calls for the server. That&#8217;s why I chose a second option: have the client generate the challenge and transmit it alongside the signature. The server would then use this challenge, and the password from the session, to calculate the signature. If they matched, then the call was permitted to be executed.</p>
<p>As far as I could see, this wouldn&#8217;t impose a bigger security risk. Of course, a man-in-the-middle could generate fake challenges. But he still wouldn&#8217;t be able to generate fake signatures. For that, he still needs the correct password. Once he has that, then well&#8230; all security checks fail. The responsibility of keeping the password secret lies entirely at the client now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/04/05/security-with-zend_amf-and-flex-part-1-theory/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Garbage collector problem after foreach loop ?</title>
		<link>http://www.encapsulated.org/blog/2009/04/01/garbage-collector-problem-after-foreach-loop/</link>
		<comments>http://www.encapsulated.org/blog/2009/04/01/garbage-collector-problem-after-foreach-loop/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 09:45:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[by-reference]]></category>
		<category><![CDATA[foreach]]></category>

		<guid isPermaLink="false">http://www.encapsulated.org/blog/?p=3</guid>
		<description><![CDATA[In PHP5, it is possible to loop through an array, and alter each item on the fly via the &#8220;by-reference&#8221; operator: the &#38;-symbol. However, you should be on the lookout for unexpected behaviour.

First the initial data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	$items = array&#40;
		array&#40;
			&#34;field&#34;		=&#62; &#34;field1&#34;,
			&#34;value&#34;		=&#62; &#34;value1&#34;,
			&#34;options&#34;	=&#62; array&#40;
				&#34;operator&#34;	=&#62; &#34;=&#34;,
				&#34;logic&#34;		=&#62; &#34;OR&#34;
			&#41;
		&#41;,
		array&#40;
			&#34;field&#34;		=&#62; &#34;field2&#34;,
			&#34;value&#34;		=&#62; &#34;value2&#34;,
			&#34;options&#34;	=&#62; array&#40;
				&#34;operator&#34;	=&#62; &#34;=&#34;,
				&#34;logic&#34;		=&#62; &#34;OR&#34;
			&#41;
		&#41;,
		array&#40;
			&#34;field&#34;		=&#62; &#34;field3&#34;,
			&#34;value&#34;		=&#62; &#34;value3&#34;,
			&#34;options&#34;	=&#62; array&#40;
				&#34;operator&#34;	=&#62; &#34;=&#34;,
				&#34;logic&#34;		=&#62; &#34;OR&#34;
			&#41;
		&#41;,
		array&#40;
			&#34;field&#34;		=&#62; &#34;field4&#34;,
			&#34;value&#34;		=&#62; &#34;value4&#34;,
			&#34;options&#34;	=&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP5, it is possible to loop through an array, and alter each item on the fly via the &#8220;by-reference&#8221; operator: the &amp;-symbol. However, you should be on the lookout for unexpected behaviour.<br />
<span id="more-3"></span><br />
First the initial data:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field1&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value1&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field2&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value2&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field3&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value3&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;field&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;field4&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;value&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value4&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;options&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
				<span style="color: #0000ff;">&quot;operator&quot;</span>	<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;=&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #0000ff;">&quot;logic&quot;</span>		<span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;OR&quot;</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It is a simple array. It could be an array of where-clauses you wish to define for your own search module (like this example), or it could just simply be test data for a unit test.</p>
<p>In my case, this data comes into a function, and in that function I want to strip some of the data. The &#8220;logic&#8221; index under &#8220;options&#8221; is deprecated, and I want to remove it. To make my code compact, I have decided to alter each filter on the fly, by using the by-reference operator.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;options&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;logic&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This results in the following altered version of my initial data:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field1
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value1
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field2
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value2
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field3
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value3
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field4
            <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value4
            <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>As you can see, I have successfully removed the &#8220;logic&#8221; index by using an ampersand in my foreach loop. This is potentially very useful functionality. It&#8217;s very easy to manipulate your data like this. No more fiddling around with indexes, or even create temporary arrays to house your data. Just edit it on the fly. There is however a (big) flaw, and I&#8217;ll illustrate it below.</p>
<p>In the previous loop, I have altered my array, and now I want to loop through it again, and just output each item in the array. Nothing more. But things go awry quite unexpectantly.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$counter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Item <span style="color: #006699; font-weight: bold;">{$counter}</span>: &quot;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$counter</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The output of this loop is:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Item <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field1
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value1
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span>
&nbsp;
Item <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field2
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value2
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span>
&nbsp;
Item <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field3
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value3
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span>
&nbsp;
Item <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field3
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value3
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The last output isn&#8217;t what I expected it to be. As you can see, it is the same output as the previous item &#8220;3&#8243;.</p>
<p>I have encountered this problem in production code, and only by rigorously outputting all my data, I have discovered that this was my problem. I am not a PHP expert by far. So I don&#8217;t always know how things technically work. My guess is that after a foreach loop, with the by-reference operator, the garbage collector doesn&#8217;t do its job well. But I&#8217;m not sure this is the correct explanation. I have found a workaround though. After the first foreach, where you use the &amp; symbol, you just unset that variable. Like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;options&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;logic&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now the output should be what you expect it to be. Just to be sure, here&#8217;s what I got:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ...</span>
Item <span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span>
&nbsp;
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>field<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> field4
    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> value4
    <span style="color: #009900;">&#91;</span>options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>operator<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">=</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>If anyone has an idea to whether or not this is a bug, please let me know. I have so far not filed a bug (yet), as I feel I need to do a bit more research first. It could be something by-design, and then I&#8217;m making quite a fool of myself. I guess that&#8217;s just part of the learning process</p>
]]></content:encoded>
			<wfw:commentRss>http://www.encapsulated.org/blog/2009/04/01/garbage-collector-problem-after-foreach-loop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
