<?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>JanJan</title>
	<atom:link href="http://blog.jandorsman.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jandorsman.com</link>
	<description>Blogging IT nerd</description>
	<lastBuildDate>Fri, 29 Mar 2013 09:21:50 +0000</lastBuildDate>
	<language>nl</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Using CakePHP Code Sniffer with PhpStorm</title>
		<link>http://blog.jandorsman.com/2013/03/using-cakephp-code-sniffer-with-phpstorm/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-cakephp-code-sniffer-with-phpstorm</link>
		<comments>http://blog.jandorsman.com/2013/03/using-cakephp-code-sniffer-with-phpstorm/#comments</comments>
		<pubDate>Thu, 28 Mar 2013 22:32:57 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[codesniffer]]></category>
		<category><![CDATA[phpstorm]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=293</guid>
		<description><![CDATA[When working on development, there are two things that I use on a daily basis: The CakePHP web framework and the PhpStorm IDE. Unfortunately PhpStorm has no &#8220;native&#8221; support for CakePHP yet (as it does for Symfony2 and Yii), although &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2013/03/using-cakephp-code-sniffer-with-phpstorm/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>When working on development, there are two things that I use on a daily basis: The <a title="CakePHP web framework" href="http://cakephp.org/">CakePHP web framework</a> and the <a title="PhpStorm" href="http://www.jetbrains.com/phpstorm/">PhpStorm</a> IDE. Unfortunately PhpStorm has no &#8220;native&#8221; support for CakePHP yet (as it does for Symfony2 and Yii), although there is a <a href="http://youtrack.jetbrains.com/issue/WI-760">pending issue</a> that can still use more votes <img src='http://blog.jandorsman.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>However, the latest PhpStorm 6.0 came with support for PHP Mess Detector and PHP Code Sniffer. The latter one also has an <a href="https://github.com/cakephp/cakephp-codesniffer">official CakePHP extension</a> available, to inspect if your code meets the CakePHP defined <a href="http://book.cakephp.org/2.0/en/contributing/cakephp-coding-conventions.html">coding standards</a>. You can load the CakePHP template for Code Sniffer into PhpStorm and it will then highlight any code you have that does not comply with the Cake coding standards.</p>
<div id="attachment_294" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.jandorsman.com/wp-content/uploads/phpstorm_codesniffer.png"><img class="size-medium wp-image-294 " alt="CakePHP Code Sniffer in action in PhpStorm 6.0" src="http://blog.jandorsman.com/wp-content/uploads/phpstorm_codesniffer-300x181.png" width="300" height="181" /></a><p class="wp-caption-text">CakePHP Code Sniffer in action in PhpStorm 6.0</p></div>
<p>Neat, right? In order to get this up and running, you will need to complete some steps, which I will describe here. First of all, we will need the base package PHP_CodeSniffer. This is available through the PEAR repository. Just open up a command-line console and go to your PHP directory (unless the PHP binary is in your Environment variables (PATH), in which case it&#8217;ll work from any directory) and run:</p>
<pre>pear install PHP_CodeSniffer</pre>
<p>Now we have the base package, also retrieve the CakePHP template, so we can verify our code against the CakePHP standards. Run the following commands to obtain it:</p>
<pre>pear channel-discover pear.cakephp.org
pear install cakephp/CakePHP_CodeSniffer</pre>
<p>OK, now we&#8217;re all set to integrate CakePHP CodeSniffer into our PhpStorm installation. Fire up the IDE and open a CakePHP project. Now open the Settings page (CTRL+ALT+S). Under the Project Settings, expand the PHP item and click Code Sniffer.</p>
<p>Here you must set the path to the PHP_CodeSniffer binary (or when on a Windows machine, it&#8217;s batch launcher). It should be installed in your PHP directory. You can verify that you have the correct binary selected by clicking the Validate button. It should show you the installed version of PHP_CodeSniffer.</p>
<div id="attachment_295" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.jandorsman.com/wp-content/uploads/codesniffer_validation.png"><img class="size-medium wp-image-295" alt="PHP_CodeSniffer Validation" src="http://blog.jandorsman.com/wp-content/uploads/codesniffer_validation-300x130.png" width="300" height="130" /></a><p class="wp-caption-text">Validating the PHP_CodeSniffer installation.</p></div>
<p>Now that we have linked our Code Sniffer installation to our PhpStorm project, we still need to tell it to use the CakePHP template, so it will only raise &#8220;relevant&#8221; warnings. To do this, we stay in the Project Settings, but go the Inspections screen. Expand the PHP item and place a checkmark in front of the &#8220;PHP Code Sniffer validation&#8221; option. This will enable Code Sniffing in general, but still doesn&#8217;t enforce the Cake standards. To achieve this, we need to do one final step. After the &#8220;Coding standard&#8221; dropdown, click the refresh button. This will retrieve a list of all supported standards. CakePHP should be right at the top of it. Select it, click Apply and enjoy clean code!</p>
<div id="attachment_296" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.jandorsman.com/wp-content/uploads/codesniffer_inspection.png"><img class="size-medium wp-image-296" alt="Setting the proper inspection settings." src="http://blog.jandorsman.com/wp-content/uploads/codesniffer_inspection-300x209.png" width="300" height="209" /></a><p class="wp-caption-text">Setting the proper inspection settings.</p></div>
<p>If you also set the CakePHP Coding Standards under the Code Style settings, combining the Code Sniffer with the &#8220;Reformat Code&#8221; (CTRL+ALT+L) command in PhpStorm, you have very easy and powerful code refactoring tools.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2013/03/using-cakephp-code-sniffer-with-phpstorm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Catching errors on AJAX calls with CakePHP</title>
		<link>http://blog.jandorsman.com/2013/02/catching-errors-on-ajax-calls-with-cakephp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=catching-errors-on-ajax-calls-with-cakephp</link>
		<comments>http://blog.jandorsman.com/2013/02/catching-errors-on-ajax-calls-with-cakephp/#comments</comments>
		<pubDate>Tue, 26 Feb 2013 20:46:36 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=264</guid>
		<description><![CDATA[In many of todays web applications, AJAX requests have a crucial role in the usability. You don&#8217;t want users to have to refresh the page in order to get the data they requested, especially not if it&#8217;s in the middle &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2013/02/catching-errors-on-ajax-calls-with-cakephp/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>In many of todays web applications, AJAX requests have a crucial role in the usability. You don&#8217;t want users to have to refresh the page in order to get the data they requested, especially not if it&#8217;s in the middle of a form. However, I often see bad error handling practices when it comes to AJAX requests (read: no error handling at all).</p>
<p>Today, I want to share a method with you that I use in my CakePHP projects. The key is in using Cake&#8217;s CakeResponse class, to throw an error and have your AJAX call catch it and run the proper action in response. In my case, I had an AJAX method in my InvoiceLinesController to allow users to add a line to an invoice by using an AJAX call (through a jQuery UI dialog). It essentially does this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">public</span> <span class="kw2">function</span> ajax_add<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="co1">// Save the data passed in the POST request</span>
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">InvoiceLine</span><span class="sy0">-&gt;</span><span class="me1">create</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">InvoiceLine</span><span class="sy0">-&gt;</span><span class="me1">save</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">request</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p><br/></p>
<p>Pretty simple, right? Just take in the passed data and save it as an invoice line. Now, this works well if the user enters proper data. But what if the user enters bad data into the form that doesn&#8217;t pass the validation rules you have set in your Model? That&#8217;s right, the save will fail. But how are you going to tell your user that?</p>
<p>I extended the method by checking the return status of the save operation and throw the validation errors if it failed:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">public</span> <span class="kw2">function</span> ajax_add<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="co1">// Save the data passed in the POST request</span>
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">InvoiceLine</span><span class="sy0">-&gt;</span><span class="me1">create</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">InvoiceLine</span><span class="sy0">-&gt;</span><span class="me1">save</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">request</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="co1">// Throw a HTTP 400 status</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">response</span><span class="sy0">-&gt;</span><span class="me1">statusCode</span><span class="br0">&#40;</span><span class="nu0">400</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">// Set the message to display in our element</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set</span><span class="br0">&#40;</span><span class="st_h">'message'</span><span class="sy0">,</span> __<span class="br0">&#40;</span><span class="st_h">'The invoice line could not be saved due to these errors:'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">// Loop over all the validationErrors</span>
        <span class="re0">$errors</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">InvoiceLine</span><span class="sy0">-&gt;</span><span class="me1">validationErrors</span> <span class="kw1">as</span> <span class="re0">$field</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$field</span> <span class="kw1">as</span> <span class="re0">$rule</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw3">array_push</span><span class="br0">&#40;</span><span class="re0">$errors</span><span class="sy0">,</span> <span class="re0">$rule</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set</span><span class="br0">&#40;</span><span class="kw3">compact</span><span class="br0">&#40;</span><span class="st_h">'errors'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="co1">// Render the error_dialog element</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">render</span><span class="br0">&#40;</span><span class="st_h">'/Elements/error_dialog'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p><br/></p>
<p>Now, whenever validation fails, the AJAX call will get the validation errors as returned data. Furthermore (and this is the important bit), it throws a HTTP 400 (Bad Request) status, so the AJAX request will fail and it&#8217;s error clause is triggered, rather than it&#8217;s success clause as it would on a 200 OK status, which would happen if you don&#8217;t set the statusCode with the CakeResponse class.</p>
<p>I also created an element called &#8220;error_dialog&#8221; to hold and display the HTML content to the user. It&#8217;s a very simple element that just ouputs our message and an ordered list using the HtmlHelper nestedList method:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span>
<span class="kw1">echo</span> <span class="re0">$message</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Html</span><span class="sy0">-&gt;</span><span class="me1">nestedList</span><span class="br0">&#40;</span><span class="re0">$errors</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="st_h">'ol'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p><br/></p>
<p>OK, so now you can use the data from your AJAX call to display to the user somehow. I chose to use jQuery UI Modal Dialog message, so it&#8217;s bound to get the users&#8217; attention.</p>
<div id="attachment_284" class="wp-caption aligncenter" style="width: 319px"><img src="http://blog.jandorsman.com/wp-content/uploads/dialog_errors1.png" alt="The error dialog" width="309" height="266" class="size-full wp-image-284" /><p class="wp-caption-text">The error dialog</p></div>
<p>I added the following error clause to my AJAX call:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="javascript"><pre class="de1">error<span class="sy0">:</span> <span class="kw1">function</span><span class="br0">&#40;</span>jqXHR<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    $<span class="br0">&#40;</span><span class="st0">&quot;#NewInvoiceLineErrorDialog&quot;</span><span class="br0">&#41;</span>.<span class="me1">html</span><span class="br0">&#40;</span><span class="st0">&quot;&lt;p&gt;&lt;span class=<span class="es0">\&quot;</span>ui-icon ui-icon-alert<span class="es0">\&quot;</span> style=<span class="es0">\&quot;</span>float: left; margin: 0 7px 50px 0;<span class="es0">\&quot;</span>&gt;&lt;/span&gt;&quot;</span> <span class="sy0">+</span> jqXHR.<span class="me1">responseText</span> <span class="sy0">+</span> <span class="st0">&quot;&lt;/p&gt;&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    $<span class="br0">&#40;</span><span class="st0">&quot;#NewInvoiceLineErrorDialog&quot;</span><span class="br0">&#41;</span>.<span class="me1">dialog</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
        modal<span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span>
        buttons<span class="sy0">:</span> <span class="br0">&#123;</span>
            <span class="st0">&quot;OK&quot;</span><span class="sy0">:</span> <span class="br0">&#123;</span>
                <span class="kw5">class</span><span class="sy0">:</span> <span class="st0">&quot;btn btn-primary&quot;</span><span class="sy0">,</span>
                text<span class="sy0">:</span> <span class="st0">&quot;OK&quot;</span><span class="sy0">,</span>
                click<span class="sy0">:</span> <span class="kw1">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">dialog</span><span class="br0">&#40;</span><span class="st0">&quot;close&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p><br/></p>
<p>This populates the dialog div with the message thrown by our action. In order to properly display this dialog, you need to add a hidden div to your view (I called it NewInvoiceLineErrorDialog), like this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="html"><pre class="de1">&lt;div id=&quot;NewInvoiceLineErrorDialog&quot; title=&quot;&lt;?php echo __('Error'); ?&gt;&quot; style=&quot;display: none;&quot;&gt;&lt;/div&gt;</pre></div></div></div></div></div></div></div>


<p><br/></p>
<p>Now this div will stay hidden until our error clause is triggered, which will open a popup with the error message. At this point, whenever a user enters bad data, the dialog will pop-up telling them what they have done wrong, so they can fix it and make the save succeed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2013/02/catching-errors-on-ajax-calls-with-cakephp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgrading a CakePHP project from 1.3 to 2.x</title>
		<link>http://blog.jandorsman.com/2013/01/upgrading-a-cakephp-project-from-1-3-to-2-x/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=upgrading-a-cakephp-project-from-1-3-to-2-x</link>
		<comments>http://blog.jandorsman.com/2013/01/upgrading-a-cakephp-project-from-1-3-to-2-x/#comments</comments>
		<pubDate>Tue, 15 Jan 2013 15:27:13 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=253</guid>
		<description><![CDATA[I am currently working on upgrading a large CakePHP 1.3 application to CakePHP 2.2. Although the upgrade shell takes care of the most basics, there are still quite a lot of things to be done manually. I was told that &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2013/01/upgrading-a-cakephp-project-from-1-3-to-2-x/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>I am currently working on upgrading a large CakePHP 1.3 application to CakePHP 2.2. Although the upgrade shell takes care of the most basics, there are still quite a lot of things to be done manually. I was told that upgrading is eventually worth the pain it brings, let&#8217;s hope so!</p>
<p>Up until now I have been mostly debugging stuff from my error logs (both Cake and Apache), fixing deprecated syntaxes, upgrading used plugins, clearing out and repopulating my ACL tables and now I&#8217;m working on finding/replacing some deprecated stuff. Up until now, I have come up with 2 massive find/replace operations that I found pretty useful and would like to write up here for my own future reference, maybe helping other people facing the same upgrade issues as well.</p>
<p>When working with querystring parameters, with 1.3 these used to be under:
<pre>$this->request->params['url']</pre>
<p> But they are now under:
<pre>$this->request->query</pre>
<p> which makes more sense anyway. Therefor I did a massive find/replace on the above.</p>
<p>Furthermore, the Paginator&#8217;s sort method now takes it&#8217;s arguments in reverse order, which causes all your paginated views to have their raw model/field names displayed, rather than the label you once provided. I&#8217;m not sure why the upgrade shell does not fix this, but using this find/replace regexes (for PhpStorm) made life easier for me:</p>
<p>Find:
<pre>\$this->Paginator->sort\('([^']+)', '([^']+)'\);</pre>
<p>Replace:
<pre>\\$this->Paginator->sort('\$2', '\$1');</pre>
<p>This basically finds the two arguments of the sort method (matching anything that&#8217;s not the closing quote) and then swapping capture groups 2 and 1. Works like a charm. Using phpStorm&#8217;s &#8220;Replace in path&#8221; function you can replace all instances in your entire project in less than a minute.</p>
<p>The final step is to inspect all my views for any flaws I might see and then have my colleagues help me test everything to see if it all went well. Fingers crossed. If I encounter any other issues of general interest, I will update this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2013/01/upgrading-a-cakephp-project-from-1-3-to-2-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX requests with CakePHP&#8217;s SecurityComponent</title>
		<link>http://blog.jandorsman.com/2012/10/ajax-requests-with-cakephps-securitycomponent/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ajax-requests-with-cakephps-securitycomponent</link>
		<comments>http://blog.jandorsman.com/2012/10/ajax-requests-with-cakephps-securitycomponent/#comments</comments>
		<pubDate>Fri, 19 Oct 2012 19:48:47 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[securitycomponent]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=241</guid>
		<description><![CDATA[For one of my current CakePHP projects, I am builing an invoice module, in which I want the users to be able to add new invoice lines to an invoice using AJAX requests, so the page doesn&#8217;t have to be &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2012/10/ajax-requests-with-cakephps-securitycomponent/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>For one of my current CakePHP projects, I am builing an invoice module, in which I want the users to be able to add new invoice lines to an invoice using AJAX requests, so the page doesn&#8217;t have to be reloaded for every new line.</p>
<p><strong>The problem</strong></p>
<p>In itself, that&#8217;s not a very hard task. But with Cake&#8217;s SecurityComponent with <a href="http://book.cakephp.org/2.0/en/core-libraries/components/security-component.html#csrf-protection" rel="nofollow">CSRF protection</a> enabled, this becomes a different story. You see, the SecurityComponent generates a token for each form that is created with the FormHelper, to prevent CSRF attacks. This becomes problematic when you want to allow a user to use the same form more then once (in my case, have them add more than 1 invoice line). Because the second time you submit the (same) form with an AJAX request, the SecurityComponent will notice you are using an expired CSRF token and thus blackholes the request (and rightly so!). Now, as you can tell, I am actually pretty fond of this security measure and even though you can disable it for certain controllers or (since CakePHP 2.3) even specific actions within a controller, this didn&#8217;t feel right to me. I wanted to keep this feature enabled and use it for my AJAX calls as well.</p>
<p><strong>The solution</strong></p>
<p>It took me some tries to figure out there is a pretty simple and effective solution to get the SecurityComponent to allow your form more than once. In my case, I used the <a href="http://jqueryui.com/dialog/#modal-form" rel="nofollow">jQueryUI modal dialog</a> functionality to present the form to the user. So, I took the content of this dialog (which is basically just the form) and created a seperate view for it (InvoiceLines/add). At first, I was thinking of an element, but the problem is that you can&#8217;t call an element directly by URL, so I couldn&#8217;t use jQuery&#8217;s load() method to retrieve it. After creating the seperate view, I was now able to &#8220;reload&#8221; the content after a successful submission by using jQuery:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">$<span class="br0">&#40;</span><span class="st0">&quot;#NewInvoiceLineDialog&quot;</span><span class="br0">&#41;</span><span class="sy0">.</span>load<span class="br0">&#40;</span><span class="st0">&quot;' . <span class="es4">$this-&gt;webroot</span> . 'invoice_lines/add/' . <span class="es4">$invoice</span>['Invoice']['id'] . '&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>By reloading the view, I am firing a new request to the FormHelper and SecurityComponent, making it create a new form with a fresh token that will be accepted! The add action in the InvoiceLines controller can be very simplistic:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">public</span> <span class="kw2">function</span> add<span class="br0">&#40;</span><span class="re0">$invoice_id</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="co1">// Use AJAX layout</span>
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">layout</span> <span class="sy0">=</span> <span class="st_h">'ajax'</span><span class="sy0">;</span>
&nbsp;
        <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set</span><span class="br0">&#40;</span><span class="kw3">compact</span><span class="br0">&#40;</span><span class="st_h">'invoice_id'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>And in the view, we just add the form:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">create</span><span class="br0">&#40;</span><span class="st_h">'InvoiceLine'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'action'</span> <span class="sy0">=&gt;</span> <span class="st_h">'add'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'invoice_id'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'type'</span> <span class="sy0">=&gt;</span> <span class="st_h">'hidden'</span><span class="sy0">,</span> <span class="st_h">'value'</span> <span class="sy0">=&gt;</span> <span class="re0">$invoice_id</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'amount'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'product_id'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'label'</span> <span class="sy0">=&gt;</span> __<span class="br0">&#40;</span><span class="st_h">'Product'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'description'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'price'</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="kw3">end</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Then, finally in the success clause of our AJAX call, we reload the form in the dialog box and we&#8217;re all set. My complete dialog code looked like this in the end:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Js</span><span class="sy0">-&gt;</span><span class="me1">buffer</span><span class="br0">&#40;</span><span class="st_h">'
    // Load the view intially, so it is visible for the first line
    $(&quot;#NewInvoiceLineDialog&quot;).load(&quot;'</span> <span class="sy0">.</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">webroot</span> <span class="sy0">.</span> <span class="st_h">'invoice_lines/add/'</span> <span class="sy0">.</span> <span class="re0">$invoice</span><span class="br0">&#91;</span><span class="st_h">'Invoice'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'id'</span><span class="br0">&#93;</span> <span class="sy0">.</span> <span class="st_h">'&quot;);
    $(&quot;#NewInvoiceLineDialog&quot;).dialog({
        autoOpen: false,
        buttons: {
            &quot;'</span> <span class="sy0">.</span> __<span class="br0">&#40;</span><span class="st_h">'Add line'</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st_h">'&quot;: function() {
                $.ajax({
                    type: &quot;POST&quot;,
                    // The ajax_add method is just a plain save() operation
                    // With $this-&gt;autoRender = false; (since it has no seperate view).
                    url: &quot;'</span> <span class="sy0">.</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">webroot</span> <span class="sy0">.</span> <span class="st_h">'invoice_lines/ajax_add&quot;,
                    data: $(&quot;#InvoiceLineAddForm&quot;).serialize(),
                    success: function() {
                        // To make the user actually see the new line without reloading, append the HTML
                        $(&quot;#lines tbody&quot;).append(
                            &quot;&lt;tr&gt;&quot; +
                                &quot;&lt;td&gt;&quot; + $(&quot;#InvoiceLineAmount&quot;).val() + &quot;&lt;/td&gt;&quot; +
                                &quot;&lt;td&gt;&quot; + $(&quot;#InvoiceLineProductId&quot;).val() + &quot;&lt;/td&gt;&quot; +
                                &quot;&lt;td&gt;&quot; + $(&quot;#InvoiceLineDescription&quot;).val() + &quot;&lt;/td&gt;&quot; +
                                &quot;&lt;td&gt;'</span> <span class="sy0">.</span> CURRENCY_SYMBOL <span class="sy0">.</span> <span class="st_h">'&amp;nbsp;&quot; + $(&quot;#InvoiceLinePrice&quot;).val() + &quot;&lt;/td&gt;&quot; +
                                &quot;&lt;td&gt;'</span> <span class="sy0">.</span> CURRENCY_SYMBOL <span class="sy0">.</span> <span class="st_h">'&amp;nbsp;&quot; + parseInt($(&quot;#InvoiceLineAmount&quot;).val() * $(&quot;#InvoiceLinePrice&quot;).val()) + &quot;&lt;/td&gt;&quot; +
                            &quot;&lt;/tr&gt;&quot;
                        );
                        // Close the dialog
                        $(&quot;#NewInvoiceLineDialog&quot;).dialog(&quot;close&quot;);
                        // And here, we load a brand spanking new form with a fresh token
                        $(&quot;#NewInvoiceLineDialog&quot;).load(&quot;'</span> <span class="sy0">.</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">webroot</span> <span class="sy0">.</span> <span class="st_h">'invoice_lines/add/'</span> <span class="sy0">.</span> <span class="re0">$invoice</span><span class="br0">&#91;</span><span class="st_h">'Invoice'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'id'</span><span class="br0">&#93;</span> <span class="sy0">.</span> <span class="st_h">'&quot;);
                    }
                });
            },
            &quot;'</span> <span class="sy0">.</span> __<span class="br0">&#40;</span><span class="st_h">'Close'</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st_h">'&quot;: function() { $(this).dialog(&quot;close&quot;); }
        },
        closeText: &quot;'</span> <span class="sy0">.</span> __<span class="br0">&#40;</span><span class="st_h">'Close window'</span><span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st_h">'&quot;,
        hide: &quot;clip&quot;,
        modal: true
    });
'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>And that&#8217;s it! I hope it helps others facing the same issue. Like always, drop any questions or comments you might have in the comment box below!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2012/10/ajax-requests-with-cakephps-securitycomponent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache Warning: DocumentRoot does not exist</title>
		<link>http://blog.jandorsman.com/2012/09/apache-warning-documentroot-does-not-exist/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apache-warning-documentroot-does-not-exist</link>
		<comments>http://blog.jandorsman.com/2012/09/apache-warning-documentroot-does-not-exist/#comments</comments>
		<pubDate>Wed, 26 Sep 2012 20:11:52 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[httpd]]></category>
		<category><![CDATA[selinux]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=234</guid>
		<description><![CDATA[After updating my server today, I noticed that Apache hadn&#8217;t come back up after the reboot. When I tried to start Apache manually, I got a whole bunch of errors for all of my vhosts, stating: Warning: DocumentRoot [/var/www/vhosts/example.com/httpdocs] does &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2012/09/apache-warning-documentroot-does-not-exist/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>After updating my server today, I noticed that Apache hadn&#8217;t come back up after the reboot. When I tried to start Apache manually, I got a whole bunch of errors for all of my vhosts, stating:</p>
<blockquote><p>Warning: DocumentRoot [/var/www/vhosts/example.com/httpdocs] does not exist</p></blockquote>
<p>While in fact, the directories did exist and the apache user had read/write permissions to it. As it turned out after some searching around, a new SELinux policy (that was among the server updates) was responsible for this. I found all kind of answers on the web, varying from the most stupid ones:</p>
<blockquote><p>&#8220;I had the same problem. Turned out to be SELINUX.<br />
Edit /etc/sysconfig/selinux and change it to disabled then reboot.&#8221;</p></blockquote>
<p>To some more serious ones stating that you should make sure that the DocumentRoot had the <em>httpd_sys_content_t</em> context. But, even setting this context did not work for me.</p>
<p>As it turned out, after running <em>audit2allow</em> on my audit.log file, access was denied because the root folder for the vhosts are in most of my cases also home directories for local users, so that when I add them, my local vsftpd install allows them to connect using their credentials and by setting their home directories to their vhost, their ftp user enters at that folder level. All I needed was just a SELinux setting that allows httpd to read user&#8217;s home dirs:</p>
<blockquote><p>setsebool -P httpd_enable_homedirs 1</p></blockquote>
<p>Whether or not this a very good idea security-wise, I&#8217;m not too sure. But there were pretty much just hobby and testing vhosts on this specific server, so I didn&#8217;t mind too much in this case. If you&#8217;re running a pretty serious production server, I would reconsider if there are any better alternatives available to you.</p>
<p>SELinux can be a pain sometimes, but it&#8217;s things like this that also make me appreciate the barriers it throws up that make you think over your server&#8217;s security settings once more.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2012/09/apache-warning-documentroot-does-not-exist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP 2.1 RSS Component</title>
		<link>http://blog.jandorsman.com/2012/03/cakephp-2-1-rss-component/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cakephp-2-1-rss-component</link>
		<comments>http://blog.jandorsman.com/2012/03/cakephp-2-1-rss-component/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 00:02:44 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=190</guid>
		<description><![CDATA[Currently, I&#8217;m getting familiar with the &#8220;latest&#8221; (at the time of writing) version of CakePHP, 2.1 and am building a project with it. For this project I also needed a helper that can read RSS files (rather than write them, &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2012/03/cakephp-2-1-rss-component/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 105px"><img class="" title="RSS Component" src="http://www.xmlboard.nl/images/rss.jpg" alt="" width="95" height="225" /><p class="wp-caption-text">RSS Component</p></div>
<p>Currently, I&#8217;m getting familiar with the <em>&#8220;latest&#8221;</em> (at the time of writing) version of CakePHP, 2.1 and am building a project with it. For this project I also needed a helper that can read RSS files (rather than write them, what Cake&#8217;s core RssHelper does). I have <a title="CakePHP RSS Helper" href="http://blog.jandorsman.com/2011/05/simple-cakephp-rss-helper/">done this before</a> for CakePHP 1.3, but that code was not really compatible with 2.1.</p>
<p>During the development I came to the conclusion that the cleanest way to achieve my goal was to actually create a component, rather than a helper, because there is still some logic that needs to be done with the resulting object. And obviously, logic goes in the Controller and not in the View. So, a component it is!</p>
<p>The component is pretty simple and straight forward. Here is the entire source code (should go under app\Controller\Component\RssComponent.php), which we will discuss in a bit.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span>
App<span class="sy0">::</span><span class="me2">uses</span><span class="br0">&#40;</span><span class="st_h">'Xml'</span><span class="sy0">,</span> <span class="st_h">'Utility'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="kw2">class</span> RssComponent <span class="kw2">extends</span> Component <span class="br0">&#123;</span>
&nbsp;
    <span class="co4">/**
     * Reads an (external) RSS feed and returns it's items.
     *
     * @param $feed - The URL to the feed.
     * @param int $items - The amount of items to read.
     * @return array
     * @throws InternalErrorException
     */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> read<span class="br0">&#40;</span><span class="re0">$feed</span><span class="sy0">,</span> <span class="re0">$items</span> <span class="sy0">=</span> <span class="nu0">5</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        try <span class="br0">&#123;</span>
            <span class="co1">// Try to read the given RSS feed</span>
            <span class="re0">$xmlObject</span> <span class="sy0">=</span> Xml<span class="sy0">::</span><span class="me2">build</span><span class="br0">&#40;</span><span class="re0">$feed</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> catch <span class="br0">&#40;</span>XmlException <span class="re0">$e</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="co1">// Reading XML failed, throw InternalErrorException</span>
            <span class="kw1">throw</span> <span class="kw2">new</span> InternalErrorException<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
&nbsp;
        <span class="re0">$output</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="kw1">for</span><span class="br0">&#40;</span><span class="re0">$i</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span><span class="re0">$i</span> <span class="sy0">&lt;</span> <span class="re0">$items</span><span class="sy0">;</span><span class="re0">$i</span><span class="sy0">++</span><span class="br0">&#41;</span><span class="sy0">:</span>
            <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">is_object</span><span class="br0">&#40;</span><span class="re0">$xmlObject</span><span class="sy0">-&gt;</span><span class="me1">channel</span><span class="sy0">-&gt;</span><span class="me1">item</span><span class="sy0">-&gt;</span><span class="re0">$i</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="re0">$output</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$xmlObject</span><span class="sy0">-&gt;</span><span class="me1">channel</span><span class="sy0">-&gt;</span><span class="me1">item</span><span class="sy0">-&gt;</span><span class="re0">$i</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="kw1">endfor</span><span class="sy0">;</span>
&nbsp;
        <span class="kw1">return</span> <span class="re0">$output</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>First off, we&#8217;ll be using the <a title="XML Utility" href="http://book.cakephp.org/2.0/en/core-utility-libraries/xml.html" target="_blank">Xml Utility</a>, that is shipped with CakePHP, which we include using:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">App<span class="sy0">::</span><span class="me2">uses</span><span class="br0">&#40;</span><span class="st_h">'Xml'</span><span class="sy0">,</span> <span class="st_h">'Utility'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>The component itself contains just 1 method, the read method. It takes 1 required argument ($feed), which holds the URL to the feed and 1 optional argument ($items) to determine the amount of items to retrieve from the RSS feed. The default is set to 5 here, but you can obviously change that if you want to.</p>
<p>The method itself is pretty simple, it just calls the <em>Xml::build</em> method to create a <em>SimpleXMLElement</em> of the feed. It catches any error that the <em>Xml::build</em> method might throw (e.g. if the feed URL is dead). We can catch that error from our controller.</p>
<p>In my case, the feed is part of the layout, so I called the component from the <em>AppController</em> in my <em>beforeRender</em> method, like this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">public</span> <span class="kw2">function</span> beforeRender<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    try <span class="br0">&#123;</span>
        <span class="re0">$newsItems</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Rss</span><span class="sy0">-&gt;</span><span class="me1">read</span><span class="br0">&#40;</span><span class="st_h">'http://example.com/feed'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span> catch<span class="br0">&#40;</span>InternalErrorException <span class="re0">$e</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="re0">$newsItems</span> <span class="sy0">=</span> <span class="kw4">null</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">set</span><span class="br0">&#40;</span><span class="st_h">'news'</span><span class="sy0">,</span> <span class="re0">$newsItems</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>So, basically we tell the AppController to try and fetch the news items and pass it to our views as the <em>$news</em> variable. If the reading of the items fails, <em>$news</em> will be set to null, which you can then check in your View. Also, make sure you don&#8217;t forget to load up the Component in your controller by putting it in the <em>$components</em> array.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">public</span> <span class="re0">$components</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'Rss'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></div></div></div></div></div>


<p>Finally, this is what my layout bit looks like. It will be different in your case, but just to give you an idea.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span>
<span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$news</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span><span class="kw3">is_null</span><span class="br0">&#40;</span><span class="re0">$news</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="re0">$newsItems</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$news</span> <span class="kw1">as</span> <span class="re0">$newsItem</span><span class="br0">&#41;</span><span class="sy0">:</span>
        <span class="re0">$newsItems</span><span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Html</span><span class="sy0">-&gt;</span><span class="kw3">link</span><span class="br0">&#40;</span>
            <span class="re0">$newsItem</span><span class="sy0">-&gt;</span><span class="me1">title</span><span class="sy0">,</span>
            <span class="re0">$newsItem</span><span class="sy0">-&gt;</span><span class="kw3">link</span><span class="sy0">,</span>
            <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'class'</span> <span class="sy0">=&gt;</span> <span class="st_h">'new_window'</span><span class="br0">&#41;</span>
        <span class="br0">&#41;</span> <span class="sy0">.</span> <span class="st_h">'&lt;p&gt;'</span> <span class="sy0">.</span> <span class="re0">$newsItem</span><span class="sy0">-&gt;</span><span class="me1">description</span> <span class="sy0">.</span> <span class="st_h">'&lt;/p&gt;'</span><span class="sy0">;</span>
    <span class="kw1">endforeach</span><span class="sy0">;</span>
&nbsp;
    <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Html</span><span class="sy0">-&gt;</span><span class="me1">nestedList</span><span class="br0">&#40;</span><span class="re0">$newsItems</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'class'</span> <span class="sy0">=&gt;</span> <span class="st_h">'news'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
    <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Html</span><span class="sy0">-&gt;</span><span class="me1">nestedList</span><span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'&lt;p&gt;News unavailable.&lt;/p&gt;'</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'class'</span> <span class="sy0">=&gt;</span> <span class="st_h">'news'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="sy1">?&gt;</span></pre></div></div></div></div></div></div></div>


<p>This bit checks whether we have the <em>$news</em> variable set and if it&#8217;s not null (so, it contains items). Then, the foreach loop prepares the items that will be passed to the <a title="nestedList" href="http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::nestedList" target="_blank">nestedList</a> method of the <a title="HtmlHelper" href="http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html" target="_blank">HtmlHelper</a>. This way, we&#8217;ll get a nice unordered list containing the news items, which is exactly what I needed in my case to fit it into my layout. Again, your View/Layout will be different, no doubt. But, this should help you get going on your way with the RssComponent.</p>
<p>If you have any question or comments feel free to post yout comments below!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2012/03/cakephp-2-1-rss-component/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adjusting Parallels/Plesk greylisting patterns</title>
		<link>http://blog.jandorsman.com/2012/01/adjusting-parallelsplesk-greylisting-patterns/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=adjusting-parallelsplesk-greylisting-patterns</link>
		<comments>http://blog.jandorsman.com/2012/01/adjusting-parallelsplesk-greylisting-patterns/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 20:44:41 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[IT]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=187</guid>
		<description><![CDATA[Today I got a mail from a customer saying that a mail that someone tried to send them kept bouncing from my Plesk/Qmail mailserver. Upon inspection of the maillog, I noticed this error at the specific e-mail message: Jan 17 &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2012/01/adjusting-parallelsplesk-greylisting-patterns/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>Today I got a mail from a customer saying that a mail that someone tried to send them kept bouncing from my Plesk/Qmail mailserver. Upon inspection of the maillog, I noticed this error at the specific e-mail message:</p>
<blockquote><p>Jan 17 14:31:08 servername greylisting filter[539]: Starting greylisting filter&#8230;<br />
Jan 17 14:31:08 servername greylisting filter[539]: list type: black, from: ch1outboundpool.messaging.microsoft.com, match string: dsl|pool|broadband|hsd<br />
Jan 17 14:31:08 servername qmail-queue-handlers[538]: handlers_stderr: REJECT</p></blockquote>
<p>Apparently, there was some kind of blacklist setting in the greylisting filter for all domains matching the regex &#8220;<em>dsl|pool|broadband|hsd</em>&#8220;, in layman&#8217;s terms: a domain which has either &#8220;dsl&#8221;, &#8220;pool&#8221;, &#8220;broadband&#8221; or &#8220;hsd&#8221; in them. Since our sender seemed to be using Microsoft&#8217;s cloud services and was using <em>ch1outboundpool.messaging.microsoft.com </em>as SMTP server, it matched the pattern.</p>
<p>But it&#8217;s a ridiculous pattern, as it would match perfectly valid domains like <em>some-dslprovider.com</em>,<em> poolparty.com</em> or <em>broadbandstore.com</em>. Obivously, I didn&#8217;t want this pattern to be in my greylisting filter and thus my quest to remove it had begun.</p>
<p>Some Googling got me to this command to inspect the greylisting configuration:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="sy0">/</span>usr<span class="sy0">/</span>local<span class="sy0">/</span>psa<span class="sy0">/</span>bin<span class="sy0">/</span>grey_listing <span class="re5">--info-server</span></pre></div></div></div></div></div></div></div>


<p>It turned out that this pattern was indeed effectively blacklisted:</p>
<blockquote><p>Black domains patterns list:<br />
*[0-9][0-9]-[0-9][0-9]-[0-9][0-9]*<br />
*[0-9][0-9].[0-9][0-9].[0-9][0-9]*<br />
*[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]*<br />
*[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9[0-9]][0-9]*<br />
<strong>dsl|pool|broadband|hsd</strong><br />
dynamic|static|ppp|dyn-ip|dial-up</p></blockquote>
<p>Now, there were some forum topics around suggesting that this command would remove it: <em>/usr/local/psa/bin/grey_listing &#8211;update-server -blacklist del:&#8221;dsl|pool|broadband|hsd&#8221;</em>. Unfortunately, that was not working:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="sy0">/</span>usr<span class="sy0">/</span>local<span class="sy0">/</span>psa<span class="sy0">/</span>bin<span class="sy0">/</span>grey_listing <span class="re5">--update-server</span> <span class="re5">-blacklist</span> del:<span class="st0">&quot;dsl|pool|broadband|hsd&quot;</span>
unable parse pattern list: incorrect pattern <span class="st0">&quot;dsl|pool|broadband|hsd&quot;</span></pre></div></div></div></div></div></div></div>


<p>It turns out that the -<em>domains-blacklist</em> option should be used instead and then it works fine (I went into the /usr/local/psa/bin directory before running this):</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">.<span class="sy0">/</span>grey_listing <span class="re5">--update-server</span> <span class="re5">-domains-blacklist</span> del:<span class="st0">&quot;dsl|pool|broadband|hsd&quot;</span>
SUCCESS: Update of server-wide settings complete.</pre></div></div></div></div></div></div></div>


<p>Now, checking the greylisting config again showed that the pattern was gone:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">.<span class="sy0">/</span>grey_listing <span class="re5">-i</span></pre></div></div></div></div></div></div></div>


<p>Which gave the following output:</p>
<blockquote><p>Black domains patterns list:<br />
*[0-9][0-9]-[0-9][0-9]-[0-9][0-9]*<br />
*[0-9][0-9].[0-9][0-9].[0-9][0-9]*<br />
*[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9]*<br />
*[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9[0-9]][0-9]*<br />
dynamic|static|ppp|dyn-ip|dial-up</p></blockquote>
<p>And then finally restarted Qmail to make sure the new config was applied properly:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="sy0">/</span>etc<span class="sy0">/</span>init.d<span class="sy0">/</span>qmail restart
<span class="re1">$Starting</span> qmail: <span class="kw1">done</span></pre></div></div></div></div></div></div></div>


<p>That was that, now the pattern is no longer applied to incoming mail and the valid mails get delivered again.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2012/01/adjusting-parallelsplesk-greylisting-patterns/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Copying a binary and it&#8217;s shared libraries to a chrooted directory</title>
		<link>http://blog.jandorsman.com/2011/10/copying-a-binary-and-its-shared-libraries-to-a-chrooted-directory/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=copying-a-binary-and-its-shared-libraries-to-a-chrooted-directory</link>
		<comments>http://blog.jandorsman.com/2011/10/copying-a-binary-and-its-shared-libraries-to-a-chrooted-directory/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 12:31:54 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=178</guid>
		<description><![CDATA[Today, I needed to construct a command for copying the rsync binary and it&#8217;s library dependencies to a chrooted directory. Since this might come in handy in the future, and might help other people to achieve the same as well, &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2011/10/copying-a-binary-and-its-shared-libraries-to-a-chrooted-directory/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>Today, I needed to construct a command for copying the rsync binary and it&#8217;s library dependencies to a chrooted directory. Since this might come in handy in the future, and might help other people to achieve the same as well, I decided to post it on here.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="kw2">cp</span> <span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>rsync <span class="sy0">/</span>var<span class="sy0">/</span>www<span class="sy0">/</span>vhosts<span class="sy0">/</span>example.com<span class="sy0">/</span>bin<span class="sy0">/</span>;\
<span class="kw2">ldd</span> <span class="sy0">/</span>usr<span class="sy0">/</span>bin<span class="sy0">/</span>rsync<span class="sy0">|</span><span class="kw2">awk</span> <span class="st_h">'{print $3}'</span><span class="sy0">|</span><span class="kw2">grep</span> <span class="re5">-e</span> <span class="st_h">'^/'</span><span class="sy0">|</span><span class="kw2">xargs</span> -I<span class="br0">&#123;</span><span class="br0">&#125;</span> <span class="kw2">cp</span> <span class="re5">-v</span> <span class="re5">--parent</span> <span class="st_h">'{}'</span> <span class="sy0">/</span>var<span class="sy0">/</span>www<span class="sy0">/</span>vhosts<span class="sy0">/</span>example.com<span class="sy0">/</span></pre></div></div></div></div></div></div></div>


<p>A quick breakdown. At first, we just do a plain copy of the rsync binary. After that, we fire up ldd on the rsync binary, then use awk to only return the third word of each output line of ldd (which should be the location to the binary). To make sure it&#8217;s a full path, a grep -e on that result should make sure the output starts with a slash (if ldd returns only a name rather than a full path, the binary will probably not work in the chrooted environment, so this method is not 100% fool-proof). Finally, we copy the matches into our chrooted environment using the &#8211;parent option (and -v for verbosity so you can see what is actually copied), so that the paths are entirely copied, e.g.: /lib64/somelib.so.1 would become /var/www/vhosts/example.com/lib64/somelib.so.1. Rather than putting it hardcoded into /lib or /usr/lib, the &#8211;parent option adopts the relative path the library should have within the chrooted environment.</p>
<p>In my case, this command worked perfectly for getting rsync to run. But again, it might not be 100% fool-proof for everything. Any additions/suggestions to make it so are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2011/10/copying-a-binary-and-its-shared-libraries-to-a-chrooted-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically resizing an iframe</title>
		<link>http://blog.jandorsman.com/2011/08/dynamically-resizing-an-iframe/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dynamically-resizing-an-iframe</link>
		<comments>http://blog.jandorsman.com/2011/08/dynamically-resizing-an-iframe/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 19:18:39 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=154</guid>
		<description><![CDATA[For a website I&#8217;m currently working on, I have an iframe in place for implementing a forum into the site, so the forum always fits nicely into the layout, also whenever the layout changes. There was only one problem with &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2011/08/dynamically-resizing-an-iframe/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>For a website I&#8217;m currently working on, I have an iframe in place for implementing a forum into the site, so the forum always fits nicely into the layout, also whenever the layout changes. There was only one problem with this setup, the height of the iframe was statically set and whenever a visitor was viewing a larger page/topic a scrollbar would appear within the iframe, which looks pretty nasty.</p>
<p>The ideal situation would be for the iframe to dynamically change it&#8217;s size whenever another page was loaded. It took a bit of trial and error, but eventually I found a way to get it done. I created this JavaScript function (inspired by various similar examples out there) to get the job done for me.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="javascript"><pre class="de1"><span class="kw1">function</span> frameResize<span class="br0">&#40;</span>frameObj<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">var</span> frameHeight<span class="sy0">;</span>
&nbsp;
    <span class="co1">// Reset the height back to it's original (shrink the frame)</span>
    frameObj.<span class="me1">height</span> <span class="sy0">=</span> <span class="st0">'200px'</span><span class="sy0">;</span>
&nbsp;
    <span class="co1">// Set the frame height to match the content</span>
    frameHeight <span class="sy0">=</span> frameObj.<span class="me1">contentWindow</span>.<span class="me1">document</span>.<span class="me1">body</span>.<span class="me1">scrollHeight</span><span class="sy0">;</span>
    frameObj.<span class="me1">height</span> <span class="sy0">=</span> frameHeight <span class="sy0">+</span> <span class="st0">'px'</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>In order for this to work, don&#8217;t forget to add the onLoad attribute to your iframe which calls the function, like this:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="html"><pre class="de1">&lt;iframe src=&quot;some_page.html&quot; width=&quot;100%&quot; height=&quot;200px&quot; id=&quot;iframe&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;
onLoad=&quot;frameResize(this);&quot;&gt;&lt;/iframe&gt;</pre></div></div></div></div></div></div></div>


]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2011/08/dynamically-resizing-an-iframe/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CakePHP contact form with validation rules</title>
		<link>http://blog.jandorsman.com/2011/05/cakephp-contact-form-with-validation-rules/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cakephp-contact-form-with-validation-rules</link>
		<comments>http://blog.jandorsman.com/2011/05/cakephp-contact-form-with-validation-rules/#comments</comments>
		<pubDate>Thu, 26 May 2011 22:50:32 +0000</pubDate>
		<dc:creator>Jan</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.jandorsman.com/?p=146</guid>
		<description><![CDATA[Recently I&#8217;ve discussed this code in the #cakephp IRC channel to someone who was looking to achieve to create a CakePHP contact form, but was struggling with it a little. I then put my code on the pastebin and decided &#8230;<p class="read-more"><a href="http://blog.jandorsman.com/2011/05/cakephp-contact-form-with-validation-rules/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[<p>Recently I&#8217;ve discussed this code in the <a href="irc://irc.freenode.org/cakephp">#cakephp</a> IRC channel to someone who was looking to achieve to create a CakePHP contact form, but was struggling with it a little. I then put my code on the pastebin and decided to post it here as well. Might help other people with the same issue.</p>
<p>What we&#8217;ll be doing is creating a simple contact form following CakePHP conventions and using the benefit of Cake&#8217;s validation rules. It&#8217;s actually quite simple to achieve this.</p>
<p>The first thing you&#8217;ll want to be doing is to determine what fields your form will need to have and which of those are required and optional. Once you&#8217;re clear on that, you can start writing up your model. The model will not actually save the message to the database (although it could if you&#8217;d want to save a copy there), but it will just validate the submitted data. In my case the required fields were <em>name</em>, <em>email</em> and <em>message</em>. Here&#8217;s the model that goes with it.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">class</span> Contact <span class="kw2">extends</span> AppModel <span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">var</span> <span class="re0">$name</span> <span class="sy0">=</span> <span class="st_h">'Contact'</span><span class="sy0">;</span>
    <span class="kw2">var</span> <span class="re0">$useTable</span> <span class="sy0">=</span> <span class="kw4">false</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">var</span> <span class="re0">$validate</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span>
        <span class="st_h">'name'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
            <span class="st_h">'rule'</span> <span class="sy0">=&gt;</span> <span class="st_h">'notEmpty'</span><span class="sy0">,</span>
            <span class="st_h">'message'</span> <span class="sy0">=&gt;</span> <span class="st_h">'You have not entered your name.'</span>
        <span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st_h">'email'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
            <span class="st_h">'rule'</span> <span class="sy0">=&gt;</span> <span class="st_h">'email'</span><span class="sy0">,</span>
            <span class="st_h">'message'</span> <span class="sy0">=&gt;</span> <span class="st_h">'You have entered an invalid e-mail address.'</span>
        <span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st_h">'message'</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span>
            <span class="st_h">'rule'</span> <span class="sy0">=&gt;</span> <span class="st_h">'notEmpty'</span><span class="sy0">,</span>
            <span class="st_h">'message'</span> <span class="sy0">=&gt;</span> <span class="st_h">'You did not enter a message.'</span>
        <span class="br0">&#41;</span>
    <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>Next up, in order to actually get our validation rules to work, we&#8217;ll need to tell our controller to validate the data before e-mailing it. Furthermore, we&#8217;ll need to tell our controller to use Cake&#8217;s <a href="http://book.cakephp.org/view/1283/Email">Email component</a> and set the required parameters to send it. If you&#8217;re not familiar with the Email component, I suggest your read the cookbook&#8217;s article on it. Here&#8217;s what the controller looks like.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">class</span> ContactsController <span class="kw2">extends</span> Controller <span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">var</span> <span class="re0">$components</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'Email'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    <span class="kw2">function</span> send<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span><span class="kw3">empty</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Contact</span><span class="sy0">-&gt;</span><span class="me1">set</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
            <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Contact</span><span class="sy0">-&gt;</span><span class="me1">validates</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span><span class="kw3">empty</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#91;</span><span class="st_h">'Contact'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'company'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Email</span><span class="sy0">-&gt;</span><span class="me1">from</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#91;</span><span class="st_h">'Contact'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'company'</span><span class="br0">&#93;</span> <span class="sy0">.</span> <span class="st_h">' - '</span> <span class="sy0">.</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#91;</span><span class="st_h">'Contact'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'name'</span><span class="br0">&#93;</span> <span class="sy0">.</span> <span class="st_h">' &lt;'</span> <span class="sy0">.</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#91;</span><span class="st_h">'Contact'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'email'</span><span class="br0">&#93;</span> <span class="sy0">.</span> <span class="st_h">'&gt;'</span><span class="sy0">;</span>
                <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                    <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Email</span><span class="sy0">-&gt;</span><span class="me1">from</span> <span class="sy0">=</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#91;</span><span class="st_h">'Contact'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'name'</span><span class="br0">&#93;</span> <span class="sy0">.</span> <span class="st_h">' &lt;'</span> <span class="sy0">.</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#91;</span><span class="st_h">'Contact'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'email'</span><span class="br0">&#93;</span> <span class="sy0">.</span> <span class="st_h">'&gt;'</span><span class="sy0">;</span>
                <span class="br0">&#125;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Email</span><span class="sy0">-&gt;</span><span class="me1">to</span> <span class="sy0">=</span> <span class="st_h">'email@example.com'</span><span class="sy0">;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Email</span><span class="sy0">-&gt;</span><span class="me1">subject</span> <span class="sy0">=</span> <span class="st_h">'Website request'</span><span class="sy0">;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Email</span><span class="sy0">-&gt;</span><span class="me1">send</span><span class="br0">&#40;</span><span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">data</span><span class="br0">&#91;</span><span class="st_h">'Contact'</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st_h">'message'</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Session</span><span class="sy0">-&gt;</span><span class="me1">setFlash</span><span class="br0">&#40;</span><span class="st_h">'Your message has been sent.'</span><span class="br0">&#41;</span><span class="sy0">;</span>
                <span class="co1">// Display the success.ctp page instead of the form again</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">render</span><span class="br0">&#40;</span><span class="st_h">'success'</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">render</span><span class="br0">&#40;</span><span class="st_h">'index'</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw2">function</span> index<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="co1">// Placeholder for index. No actual action here, everything is submitted to the send function.</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span></pre></div></div></div></div></div></div></div>


<p>Finally, we need our views to show the form and the success message if the message was sent. This is most likely to look different in your case, but just for the complete picture, I&#8217;ll share them with you anyway.</p>
<p><b>app/views/contacts/index.ctp</b></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1">&lt;p&gt;Thank you for your interest in our company. Leave a message.&lt;/p&gt;
<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">create</span><span class="br0">&#40;</span><span class="st_h">'Contact'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'action'</span> <span class="sy0">=&gt;</span> <span class="st_h">'send'</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>
&lt;table style=&quot;border:none;&quot;&gt;
    &lt;tr&gt;
        &lt;td&gt;Name&lt;/td&gt;
        &lt;td&gt;<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'Contact.name'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'label'</span> <span class="sy0">=&gt;</span> <span class="kw4">false</span><span class="sy0">,</span> <span class="st_h">'maxlength'</span> <span class="sy0">=&gt;</span> <span class="nu0">100</span><span class="sy0">,</span> <span class="st_h">'size'</span> <span class="sy0">=&gt;</span> <span class="nu0">40</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;Company&lt;/td&gt;
        &lt;td&gt;<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'Contact.company'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'label'</span> <span class="sy0">=&gt;</span> <span class="kw4">false</span><span class="sy0">,</span> <span class="st_h">'maxlength'</span> <span class="sy0">=&gt;</span> <span class="nu0">100</span><span class="sy0">,</span> <span class="st_h">'size'</span> <span class="sy0">=&gt;</span> <span class="nu0">40</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;E-Mail&lt;/td&gt;
        &lt;td&gt;<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'Contact.email'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'label'</span> <span class="sy0">=&gt;</span> <span class="kw4">false</span><span class="sy0">,</span> <span class="st_h">'maxlength'</span> <span class="sy0">=&gt;</span> <span class="nu0">100</span><span class="sy0">,</span> <span class="st_h">'size'</span> <span class="sy0">=&gt;</span> <span class="nu0">40</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td style=&quot;vertical-align: top;&quot;&gt;Your comment&lt;/td&gt;
        &lt;td&gt;<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="me1">input</span><span class="br0">&#40;</span><span class="st_h">'Contact.message'</span><span class="sy0">,</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st_h">'label'</span> <span class="sy0">=&gt;</span> <span class="kw4">false</span><span class="sy0">,</span> <span class="st_h">'cols'</span> <span class="sy0">=&gt;</span> <span class="nu0">50</span><span class="sy0">,</span> <span class="st_h">'rows'</span> <span class="sy0">=&gt;</span> <span class="nu0">10</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td colspan=&quot;2&quot; align=&quot;center&quot;&gt;&lt;br&gt;<span class="kw2">&lt;?php</span> <span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Form</span><span class="sy0">-&gt;</span><span class="kw3">end</span><span class="br0">&#40;</span><span class="st_h">'Send'</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="sy1">?&gt;</span>&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;</pre></div></div></div></div></div></div></div>


<p><b>app/views/contacts/success.ctp</b></p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="php"><pre class="de1"><span class="kw2">&lt;?php</span>
<span class="kw1">echo</span> <span class="re0">$this</span><span class="sy0">-&gt;</span><span class="me1">Session</span><span class="sy0">-&gt;</span><span class="me1">flash</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="sy1">?&gt;</span>
&lt;p&gt;Thank you, we'll get back to you shortly.&lt;/p&gt;</pre></div></div></div></div></div></div></div>


<p>That&#8217;s all! A nice and simple contact form with proper field validation.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jandorsman.com/2011/05/cakephp-contact-form-with-validation-rules/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
