<?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>Kristian Lunde &#187; PHP</title>
	<atom:link href="http://www.klunde.net/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.klunde.net</link>
	<description>www.klunde.net</description>
	<lastBuildDate>Sun, 18 Jul 2010 21:00:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Zend_Input_Filter and the Alnum Validator</title>
		<link>http://www.klunde.net/2010/07/18/zend_input_filter-and-the-alnum-validator/</link>
		<comments>http://www.klunde.net/2010/07/18/zend_input_filter-and-the-alnum-validator/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 21:00:28 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend_Input_Filter]]></category>
		<category><![CDATA[Zend_Validate_Alnum]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=518</guid>
		<description><![CDATA[
			
				
			
		
The Zend_Input_Filter is a very useful tool when you need to validate and filter the input to your application. It allows you to both filter and validate the input without a lot of hassle. One of the cool features it has is that it allows you to add the validators you need and meta commands [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2010%2F07%2F18%2Fzend_input_filter-and-the-alnum-validator%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2010_2F07_2F18_2Fzend_input_filter-and-the-alnum-validator_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2010%2F07%2F18%2Fzend_input_filter-and-the-alnum-validator%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>The <a href="http://framework.zend.com/manual/en/zend.filter.input.html" onclick="urchinTracker('/outgoing/framework.zend.com/manual/en/zend.filter.input.html?referer=');">Zend_Input_Filter</a> is a very useful tool when you need to validate and filter the input to your application. It allows you to both filter and validate the input without a lot of hassle. One of the cool features it has is that it allows you to add the validators you need and meta commands to each validator. For instance you can set an <a href="http://framework.zend.com/manual/en/zend.validate.set.html" onclick="urchinTracker('/outgoing/framework.zend.com/manual/en/zend.validate.set.html?referer=');">Alnum</a> validator to allow empty fields, set a default text and so on. </p>
<p>I came over this annoying issue the other day when I tried to setup the Alnum validator to allow white spaces and have a few meta commands attached to the validator chain. The <a href="http://framework.zend.com/manual/en/zend.filter.input.html" onclick="urchinTracker('/outgoing/framework.zend.com/manual/en/zend.filter.input.html?referer=');">manual</a> says that you can do this:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="re1">$validators</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&#39;month&#39;</span> &nbsp; <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Digits&#39;</span><span class="sy0">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// string</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_Validate_Int<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="co1">// object instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;Between&#39;</span><span class="sy0">,</span> <span class="nu0">1</span><span class="sy0">,</span> <span class="nu0">12</span><span class="br0">&#41;</span> &nbsp;<span class="co1">// string with constructor arguments</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>Which I assumed would also work like this:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="re1">$validators</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&#39;name&#39;</span> &nbsp; <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Alnum&#39;</span><span class="sy0">,</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_Validate_Alnum<span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="co1">//allow whitespaces</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;default&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;&#39;</span><span class="sy0">,</span> &nbsp;<span class="co1">//meta command 1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;presence&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;required&#39;</span><span class="sy0">,</span> <span class="co1">//meta command 2</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>This does not work though. You need to remove the validator type string and replace it with an instance of the Zend_Validate_Alnum validator to get it to accept whitespaces and meta commands. This is the right way to do it:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="re1">$validators</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&#39;name&#39;</span> &nbsp; <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_Validate_Alnum<span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="co1">//allow whitespaces</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;default&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;&#39;</span><span class="sy0">,</span> <span class="co1">//meta command 1</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;presence&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;required&#39;</span><span class="sy0">,</span> <span class="co1">//meta command 2</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>The entire script ends up looking like this:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$filters</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;name&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;StringTrim&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$validators</span> <span class="sy0">=</span> &nbsp;<span class="kw3">array</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="st0">&#39;name&#39;</span> &nbsp; <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> Zend_Validate_Alnum<span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">,</span> <span class="co1">//allow whitespaces</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;default&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;presence&#39;</span> <span class="sy0">=&gt;</span> <span class="st0">&#39;required&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$input</span> <span class="sy0">=</span> <span class="kw2">new</span> Zend_Input_Filter<span class="br0">&#40;</span><span class="re1">$filter</span><span class="sy0">,</span> <span class="re1">$input</span><span class="sy0">,</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">getRequest</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">-&gt;</span><span class="me1">getParams</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2010/07/18/zend_input_filter-and-the-alnum-validator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Merging code bases</title>
		<link>http://www.klunde.net/2009/09/16/merging-code-bases/</link>
		<comments>http://www.klunde.net/2009/09/16/merging-code-bases/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 07:22:58 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=367</guid>
		<description><![CDATA[
			
				
			
		
Yesterday I had the &#8220;pleasure&#8221; of merging two code bases of the same application. The code bases had been developed in two different parts of the world, but shared a common foundation. I got access to a development version of the code base a few weeks ago, and the final delivery of the application was [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2009%2F09%2F16%2Fmerging-code-bases%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2009_2F09_2F16_2Fmerging-code-bases_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2009%2F09%2F16%2Fmerging-code-bases%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Yesterday I had the &#8220;pleasure&#8221; of merging two code bases of the same application. The code bases had been developed in two different parts of the world, but shared a common foundation. I got access to a development version of the code base a few weeks ago, and the final delivery of the application was done late last week. No version control system was shared between myself and the contractor which made the merge a bit more cumbersome. Unfortunately I could not wait for the contractor to finish the development before I started to add features and bug fixes to the application; this eventually resulted in two separate versions of the code base. </p>
<p>I was aware that this merge would going to happen from the very start so some precautions were taken before I started my own branch of the source code.</p>
<p>1. I separated all new features out in separate directories and added symbolic links to these in the existing code base. This worked very well and we had no problem at all adding the new features to the final delivery. </p>
<p>2. I tried to be very careful and keep track of all the bug fixes and changes done to the original code.</p>
<h2>Trouble</h2>
<p>I realized that we would have trouble with the final merge not long after I received the first development version, it was cluttered with bugs and issues which made it impossible to even run it in my development version. To get the application up and running I had to make a bunch of changes to the code. </p>
<p>In addition to the initial bugs I soon realized that the front end of the application (read: html and css) was a complete mess. The site was not browser compliant, nothing validated and it was impossible to go through. These issues would not be resolved by the contractor and since the project was on the clock it needed resolving as quickly as possible. This ended up in a complete rebuild of the front end which modified 300+ files. </p>
<h2>1st Attempt &#8211; Failure</h2>
<p>1. Created a git branch of my development code<br />
2. Added the final code from the contractor to the branch</p>
<p>This ended up in a complete mess, nothing working, a complete mess. </p>
<h2>2nd Attempt &#8211; Success</h2>
<p>When the final delivery from the contractor came through the two code bases was in completely different states, most of the bug fixes I had was still needed. </p>
<p>1. Created a git branch of my code base<br />
3. Found the differences from the initial development version we got and the final delivery</p>
<div class="geshi no bash">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">diff</span> -qr dev final <span class="sy0">|</span> <span class="kw2">grep</span> -v -e <span class="st0">&#39;DS_Store&#39;</span> -e <span class="st0">&#39;Thumbs&#39;</span> <span class="sy0">|</span> <span class="kw2">sort</span> <span class="sy0">&gt;</span> changes.txt</div>
</li>
</ol>
</div>
<p>Where <i>dev</i> is the directory of the untouched development version we got access to, and <i>final</i> is the directory of the final delivery. This resulted in a complete list (<i>changes.txt</i>) of files which differed between the two original versions. It also identified files that was obsolete in and new files that was added.  An example of the content in the <i>changes.txt</i> can be seen below.</p>
<div class="geshi no text">
<ol>
<li class="li1">
<div class="de1">Files dev/view/file-1.php and final/view/file-1.php differ</div>
</li>
<li class="li1">
<div class="de1">Files dev/view/file-2.php and final/view/file-2.php differ</div>
</li>
<li class="li1">
<div class="de1">Only in dev/css: css-1.css</div>
</li>
<li class="li1">
<div class="de1">Only in dev/css: css-2.css</div>
</li>
<li class="li1">
<div class="de1">Only in dev/css: css-3.css</div>
</li>
<li class="li1">
<div class="de1">Only in final/css: css.css</div>
</li>
<li class="li1">
<div class="de1">Only in final/: file-a.php</div>
</li>
</ol>
</div>
<p>4. Once I had this overview I added all the new files from the final version into my git branch.<br />
5. Updated all the files I knew had not been changed. I had a list of all the core files which had been changed.<br />
6. I manually had to go through all the core files that had been changed and compare them with the files from the final delivery.<br />
7. Remove all old files which only were present in the development delivery.<br />
8. Manual comparison of all the front end files, updating and merging these files by hand. Diff can not be used here since the entire front end has changed, and it would only result in a complete difference, still there might have been changes that I needed to incorporate with the new front end.</p>
<p>I still have approx 150 front end files to compare, it is time consuming and frustrating labor, but it seems to be the only way to do it. I keep testing the application while doing the update and so far all of the changes and updates has been successful.</p>
<p>The positive flip of this is that I get a good overview of the code and understanding of the application when I have to go through much of the code from the final delivery.</p>
<p>I might not have chosen the best solution and I would love to hear your approach if you have done similar things or have an opinion about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2009/09/16/merging-code-bases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the default option of a ubercart product attribute</title>
		<link>http://www.klunde.net/2009/02/20/getting-the-default-option-of-a-ubercart-product-attribute/</link>
		<comments>http://www.klunde.net/2009/02/20/getting-the-default-option-of-a-ubercart-product-attribute/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 22:02:57 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[Drupal]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[Ubercart]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=262</guid>
		<description><![CDATA[
			
				
			
		
In Orange Bus we are currently busy building a new web shop for a clothing company. We are building this web shop on Drupal 6 and Ubercart 2. While I was doing some tuning of the product page (built as a node template) on this site I suddenly realized that even though you can get [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2009%2F02%2F20%2Fgetting-the-default-option-of-a-ubercart-product-attribute%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2009_2F02_2F20_2Fgetting-the-default-option-of-a-ubercart-product-attribute_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2009%2F02%2F20%2Fgetting-the-default-option-of-a-ubercart-product-attribute%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In Orange Bus we are currently busy building a new web shop for a clothing company. We are building this web shop on Drupal 6 and Ubercart 2. While I was doing some tuning of the product page (built as a node template) on this site I suddenly realized that even though you can get most of the information needed in from the <i>$node</i> object, you are unable to get the default options of each attribute.</p>
<p>In my case this attribute was the sizes of the products (small, medium, large and so on), the node object contained all the attributes but not the default options. It is not at all complicated to get this information but you do need to add some custom code to get a hold of the default options. I would argue that this should be included in the default node object, which really should not be a big deal adding. I guess I should add a patch for this, instead of going around the problem which is what I do and describe here. </p>
<p>To get a hold of this I had to call a ubercart specific function called <i>uc_product_get_attributes</i> function. This function takes a node id as parameter and return all an array of all the attributes related to the node. The array contain a set of attributes objects and these object contain all the information available on each attribute.</p>
<p>My solution was to call the <i>uc_product_get_attributes</i> function and get the <i>default_option</i> variable from the attribute object, see code example below.</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//get all attributes related to the node</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$attributes</span> <span class="sy0">=</span> uc_product_get_attributes<span class="br0">&#40;</span><span class="re1">$node</span><span class="sy0">-&gt;</span><span class="me1">nid</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//get the id of default size of the product</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$default_size</span> <span class="sy0">=</span> <span class="re1">$attributes</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">-&gt;</span><span class="me1">default_option</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>It is simple, but it took me about an 30 minutes to determine the problem and adding a solution. Hopefully this will save someone the job of solving the same problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2009/02/20/getting-the-default-option-of-a-ubercart-product-attribute/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Coding standard, coding style</title>
		<link>http://www.klunde.net/2008/11/11/coding-standard-coding-style/</link>
		<comments>http://www.klunde.net/2008/11/11/coding-standard-coding-style/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 22:53:25 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[Drupal]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[coding style]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=185</guid>
		<description><![CDATA[
			
				
			
		
In Orange Bus we have been looking at our coding style lately. We have created our very own coding standard. Since we base most of our applications on the drupal we chose to use the drupal coding standard as our main source of inspiration.
During our process of creating this coding standard we found a few [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F11%2F11%2Fcoding-standard-coding-style%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F11_2F11_2Fcoding-standard-coding-style_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F11%2F11%2Fcoding-standard-coding-style%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a href="http://www.orangebus.co.uk" onclick="urchinTracker('/outgoing/www.orangebus.co.uk?referer=');">Orange Bus</a> we have been looking at our coding style lately. We have created our very own coding standard. Since we base most of our applications on the drupal we chose to use the <a href="http://www.drupal.org" onclick="urchinTracker('/outgoing/www.drupal.org?referer=');">drupal</a> coding standard as our main source of inspiration.<br />
During our process of creating this coding standard we found a few good links I thought we should share. </p>
<p><a href="http://yeknaa.wordpress.com/" onclick="urchinTracker('/outgoing/yeknaa.wordpress.com/?referer=');">Mike</a> @ <a href="http://www.orangebus.co.uk" onclick="urchinTracker('/outgoing/www.orangebus.co.uk?referer=');">Orange Bus</a> found these two articles about beautiful and practical code, both these are a must read:</p>
<p><a href="http://www.wilshipley.com/blog/2007/05/pimp-my-code-part-14-be-inflexible.html" onclick="urchinTracker('/outgoing/www.wilshipley.com/blog/2007/05/pimp-my-code-part-14-be-inflexible.html?referer=');">http://www.wilshipley.com/blog/2007/05/pimp-my-code-part-14-be-inflexible.html</a></p>
<p><a href="http://www.perforce.com/perforce/papers/prettycode.html" onclick="urchinTracker('/outgoing/www.perforce.com/perforce/papers/prettycode.html?referer=');">http://www.perforce.com/perforce/papers/prettycode.html</a></p>
<p>Another blog post  about line density worth reading is:</p>
<p><a href="http://paul-m-jones.com/?p=276" onclick="urchinTracker('/outgoing/paul-m-jones.com/?p=276&amp;referer=');">http://paul-m-jones.com/?p=276</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/11/11/coding-standard-coding-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Templating with Eclipse</title>
		<link>http://www.klunde.net/2008/08/20/templating-with-eclipse/</link>
		<comments>http://www.klunde.net/2008/08/20/templating-with-eclipse/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 20:18:39 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[Applications]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPEclipse]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=54</guid>
		<description><![CDATA[
			
				
			
		
Today I came over a a post on dzone.com about Eclipse and templating. It turns out that you can write small templates of code snippets you use a lot and bind them to a keyword. When typing the keyword, press &#8220;ctrl&#8221; and &#8220;space&#8221; and a list of possible templates available will appear, select the prefered [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F08%2F20%2Ftemplating-with-eclipse%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F08_2F20_2Ftemplating-with-eclipse_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F08%2F20%2Ftemplating-with-eclipse%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Today I came over a a post on <a href="http://www.dzone.com" onclick="urchinTracker('/outgoing/www.dzone.com?referer=');">dzone.com</a> about Eclipse and templating. It turns out that you can write small templates of code snippets you use a lot and bind them to a keyword. When typing the keyword, press &#8220;ctrl&#8221; and &#8220;space&#8221; and a list of possible templates available will appear, select the prefered template, press &#8220;enter&#8221; and the code snippet is inserted into your working code. I works just the same way code completion. This actually means that you do not have to write the boring &#8220;for&#8221; loops or &#8220;if&#8221; tests anymore, just write a template and become a more efficient developer. </p>
<p>The <a href="http://firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html" onclick="urchinTracker('/outgoing/firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html?referer=');">original post</a> explains how to set up templates, so I will not go into that in this post, but since I am primarily a PHP developer and the post describes templating for Java, I&#8217;ll just give a description of where to go if you use the PHPEclipse.<br />
To edit and create templates for PHP, HTML or css, even javascript you have to use this path:</p>
<p><em>window -> preferences -> PHPeclipse Web Development -> PHP -> Templates.</em></p>
<p>To write and edit the templates, just follow the guidelines from <a href="http://firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html" onclick="urchinTracker('/outgoing/firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html?referer=');">Mr. Graversen</a>, or have a look at the links below.</p>
<p>For further reading have a look these sites.</p>
<ul>
<li><a href="http://firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html" onclick="urchinTracker('/outgoing/firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html?referer=');">http://firstclassthoughts.co.uk/java/eclipse_tip_templates_public_static_final.html</a>
<li>http://www.phpeclipse.de/tiki-index.php?page=Howto+use+the+templates+system</li>
<li><a href="http://help.eclipse.org/help21/index.jsp?topic=/org.eclipse.jdt.doc.user/concepts/ctemplates.htm" onclick="urchinTracker('/outgoing/help.eclipse.org/help21/index.jsp?topic=/org.eclipse.jdt.doc.user/concepts/ctemplates.htm&amp;referer=');">http://help.eclipse.org/help21/index.jsp?topic=/org.eclipse.jdt.doc.user/concepts/ctemplates.htm</a></li>
</ul>
<p>I guess if I had read the PHPEclipse manual when I first started using Eclipse, I would probably been using it from the start <img src='http://www.klunde.net/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>Good luck with your  Eclipse templates, I know I will be using them extensively.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/08/20/templating-with-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The View Helper pattern</title>
		<link>http://www.klunde.net/2008/07/20/the-view-helper-pattern/</link>
		<comments>http://www.klunde.net/2008/07/20/the-view-helper-pattern/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 21:11:58 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Reusability]]></category>
		<category><![CDATA[View Helper pattern]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=37</guid>
		<description><![CDATA[
			
				
			
		
Developing MVC (Model View Controller pattern) applications in PHP or any other language often require a lot from the view tier. The view needs to process data received from the model tier and form it into presentable data, it also has to manage user input and form that into data understandable for the model tier.
This [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F07%2F20%2Fthe-view-helper-pattern%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F07_2F20_2Fthe-view-helper-pattern_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F07%2F20%2Fthe-view-helper-pattern%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Developing MVC (Model View Controller pattern) applications in PHP or any other language often require a lot from the view tier. The view needs to process data received from the model tier and form it into presentable data, it also has to manage user input and form that into data understandable for the model tier.<br />
This might not be a big issue while working with small application, but when it comes to midscale and large applications the view helper pattern can be of great help. The view helper pattern is one of the J2EE core patterns and the documentation can be found on: </p>
<p><a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/ViewHelper.html" onclick="urchinTracker('/outgoing/java.sun.com/blueprints/corej2eepatterns/Patterns/ViewHelper.html?referer=');">http://java.sun.com/blueprints/corej2eepatterns/Patterns/ViewHelper.html</a></p>
<p><strong>What does the helper pattern do?</strong><br />
First of all the helper pattern adds an extra tier to the system, this tier can be seen as a mid tier which has some understanding of the logic of the system, it knows a little bit about the view and a little bit about the model. Another cool thing is that the view helper pattern makes your code more reusable. When moving complex structures from the view and into a view helper it can with ease be used by other views. </p>
<p><strong>Example:</strong></p>
<p>You are writing a web application where the user writes a review of some product, the application should do auto saving of the user input every 20 second using Ajax functionality. The application should of course also save the user input then the user submits the data. The ajax request and the user submit does almost the same thing but the when the user submits the data the view should also store a rating of the product. This require the ajax request and the user submit to be two different views, or one complex view. </p>
<p>In an ordinary MVC system you would have to implement two views with very much of the similar behavior or one complex view. Using the view helper pattern you extract the storage of the user review in a helper which can be reused by both the ajax request view and the user submit view.</p>
<p>I have written a very simple implementation of the example in PHP. The implementation is not complete at all, but it is meant as a proof of concept that the reusability of code in your application can increase using the view helper pattern.</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> ProductReviewHelper</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp;* save the review</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; **/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> save<span class="br0">&#40;</span><span class="re1">$user_input</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//validate input</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$input</span> <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">validate</span><span class="br0">&#40;</span><span class="re1">$user_input</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//saves the review and return the result of the save</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="re1">$review_manager</span><span class="sy0">-&gt;</span><span class="me1">save</span><span class="br0">&#40;</span><span class="re1">$input</span><span class="br0">&#91;</span><span class="st0">&#39;product_id&#39;</span><span class="br0">&#93;</span><span class="sy0">,</span> <span class="re1">$input</span><span class="br0">&#91;</span><span class="st0">&#39;review&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * validate the input </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; **/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> validate<span class="br0">&#40;</span><span class="re1">$user_input</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$filter_args</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;product_id&#39;</span> <span class="sy0">=&gt;</span> FILTER_VALIDATE_INT<span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;review&#39;</span> &nbsp; <span class="sy0">=&gt;</span> FILTER_SANITIZE_STRING<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$input</span> <span class="sy0">=</span> filter_var_array<span class="br0">&#40;</span><span class="re1">$user_input</span><span class="sy0">,</span> <span class="re1">$filter_args</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">//do validation</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="re1">$input</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//Ajax view</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$review_helper</span> <span class="sy0">=</span> <span class="kw2">new</span> ProductReviewHelper<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$result</span> <span class="sy0">=</span> <span class="re1">$review_helper</span><span class="sy0">-&gt;</span><span class="me1">save</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">echo</span> <span class="re1">$result</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">exit</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//User submit view</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$review_helper</span> <span class="sy0">=</span> <span class="kw2">new</span> ProductReviewHelper<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$review_result</span> <span class="sy0">=</span> <span class="re1">$review_helper</span><span class="sy0">-&gt;</span><span class="me1">save</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$rating_helper</span> <span class="sy0">=</span> <span class="kw2">new</span> ProductRatingHelper<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$rating_result</span> <span class="sy0">=</span> <span class="re1">$rating_helper</span><span class="sy0">-&gt;</span><span class="me1">save</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//manage the result from the helpers</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>I am sure that the view helper pattern has helped me to write better and more organized code, which is easily understandable and very reusable.</p>
<p>If you do not use the view helper pattern, and still have solved the problem with reusability of code in the view tier please feel free to leave me a comment describing your solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/07/20/the-view-helper-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Password encryption using PHP</title>
		<link>http://www.klunde.net/2008/07/10/password-encryption-using-php/</link>
		<comments>http://www.klunde.net/2008/07/10/password-encryption-using-php/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 08:28:52 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[web security]]></category>
		<category><![CDATA[password encryption]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=31</guid>
		<description><![CDATA[
			
				
			
		
A recent post on dzone.com linked to a article about &#8220;password encryption using PHP&#8221; written by Stefan Ashwell on total.php.com. In this article he illustrate a how to save user passwords and authenticating users using the sha1 hashing algorithm.
First of all lets all agree that hashing passwords are basics requirements for a secure web application, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F07%2F10%2Fpassword-encryption-using-php%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F07_2F10_2Fpassword-encryption-using-php_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F07%2F10%2Fpassword-encryption-using-php%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>A recent post on <a href="http://www.dzone.com" onclick="urchinTracker('/outgoing/www.dzone.com?referer=');">dzone.com</a> linked to a article about <a href="http://total-php.com/article/5/encrypting-passwords-using-php/" onclick="urchinTracker('/outgoing/total-php.com/article/5/encrypting-passwords-using-php/?referer=');">&#8220;password encryption using PHP&#8221;</a> written by Stefan Ashwell on <a href="http://total-php.com/article/5/encrypting-passwords-using-php/" onclick="urchinTracker('/outgoing/total-php.com/article/5/encrypting-passwords-using-php/?referer=');">total.php.com</a>. In this article he illustrate a how to save user passwords and authenticating users using the sha1 hashing algorithm.</p>
<p>First of all lets all agree that hashing passwords are basics requirements for a secure web application, but is a simple hashing of the password enough? I do not think so.</p>
<p>Here is the scenario, Someone breaks into your system (not through the web application, but for instance through an ssh connection), they get access to your user database or file where you store user account information.  The intruder is now in possession of the password and user name of all your users, but still the passwords are hashed with md5, sha1 or an similar hashing method. If the intruder is determined to get into your system and mess up, he may now try to decrypt the passwords using a  dictionary word file and brute force (also known as rainbow tables). This method is quite common and is not advanced at all, all it does is looping through the dictionary file, which contains all words and common password phrases, do a md5 or sha1 hashing of these words and see if it matches up to the hashed password, if it does it has found a match, and the intruder is able to log into the account.</p>
<p>Even though this brute force method might take some time, he will eventually get the passwords and get full access to the users account. There are however methods to complicate this and even make it impossible for the intruder to get the password using brute force method and that is called salting your password.</p>
<p>Example:</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$salt</span> <span class="sy0">=</span> <span class="st0">&#39;2glkpe895&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$password</span> <span class="sy0">=</span> <span class="re1">$_POST</span><span class="br0">&#91;</span><span class="st0">&#39;password&#39;</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$encrypted_password</span> <span class="sy0">=</span> <span class="kw3">sha1</span><span class="br0">&#40;</span><span class="re1">$salt</span> <span class="sy0">.</span> <span class="re1">$password</span> <span class="sy0">.</span> <span class="re1">$salt</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>As you can see the salt is an secret string which is only used by your application, it is prepended and appended to the password. You could of course also go the extra mile and split the password in two and add the salt in the middle of the password, but there might not be any point in doing that.</p>
<p>This makes the word not like any word you will find in an dictionary and therefore the brute force method will not find the password.</p>
<p>The point is that if the intruder get a partial access to some of your system, for instance the user database, it will not be enough to get access to the total system because the security system is layered, one layer in your code, and one layer in your user database.</p>
<p>I do not say that this method is a 100 percent secure but it is is way more secure than not using a salted password.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/07/10/password-encryption-using-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Vikinger</title>
		<link>http://www.klunde.net/2008/06/23/php-vikinger-2/</link>
		<comments>http://www.klunde.net/2008/06/23/php-vikinger-2/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 18:20:21 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Real Life]]></category>
		<category><![CDATA[PHP vikinger]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=28</guid>
		<description><![CDATA[
			
				
			
		
I attended the PHP vikinger unconference in Skien on Saturday. Rather than writing my own wrap up  of the unconference I choose to link to the wrap up written by Mats Lindh and Derick Rethans
Notes from Mats: http://e-mats.org/2008/06/php-vikinger-notes/
Notes from Derick: http://phpvikinger.org/news/news-2008-05-23
]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F06%2F23%2Fphp-vikinger-2%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F06_2F23_2Fphp-vikinger-2_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F06%2F23%2Fphp-vikinger-2%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>I attended the PHP vikinger unconference in Skien on Saturday. Rather than writing my own wrap up  of the unconference I choose to link to the wrap up written by Mats Lindh and Derick Rethans</p>
<p>Notes from Mats: <a href="http://e-mats.org/2008/06/php-vikinger-notes/" onclick="urchinTracker('/outgoing/e-mats.org/2008/06/php-vikinger-notes/?referer=');">http://e-mats.org/2008/06/php-vikinger-notes/</a></p>
<p>Notes from Derick: <a href="http://phpvikinger.org/news/news-2008-05-23" onclick="urchinTracker('/outgoing/phpvikinger.org/news/news-2008-05-23?referer=');">http://phpvikinger.org/news/news-2008-05-23</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/06/23/php-vikinger-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing SOA applications with PHP</title>
		<link>http://www.klunde.net/2008/06/08/writing-soa-applications-with-php/</link>
		<comments>http://www.klunde.net/2008/06/08/writing-soa-applications-with-php/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 20:28:47 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=23</guid>
		<description><![CDATA[
			
				
			
		
Lately I&#8217;ve been working a lot with data integration between several web applications, and a natural choice for the integration was to use a Service Oriented Architecture (SOA). I&#8217;ve built both SOA servers and clients before using the SOAP approach, which is a superb way of transferring data when not knowing who the user of [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F06%2F08%2Fwriting-soa-applications-with-php%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F06_2F08_2Fwriting-soa-applications-with-php_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F06%2F08%2Fwriting-soa-applications-with-php%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>Lately I&#8217;ve been working a lot with data integration between several web applications, and a natural choice for the integration was to use a Service Oriented Architecture (SOA). I&#8217;ve built both SOA servers and clients before using the SOAP approach, which is a superb way of transferring data when not knowing who the user of the service is, or when you are a client of such a service. However in this setting I knew who the user was, I knew what the service would be used for, also had the chance to write both the client and server.</p>
<p>The choice fell on a simple implementation using the <a onclick="urchinTracker('/outgoing/www.json.org/?referer=');urchinTracker('/outgoing/www.json.org/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/www.json.org/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/www.json.org/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/www.json.org/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://www.json.org/">JSON</a> (JavaScript Object Notation) data structure. This is really easy using the built in json library in php. The server use the <a onclick="urchinTracker('/outgoing/no.php.net/manual/en/function.json-encode.php?referer=');urchinTracker('/outgoing/no.php.net/manual/en/function.json-encode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/no.php.net/manual/en/function.json-encode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/no.php.net/manual/en/function.json-encode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/no.php.net/manual/en/function.json-encode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://no.php.net/manual/en/function.json-encode.php"><strong>json_encode()</strong></a> function and the implemented sub class of the client use the <a onclick="urchinTracker('/outgoing/no.php.net/manual/en/function.json-decode.php?referer=');urchinTracker('/outgoing/no.php.net/manual/en/function.json-decode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/no.php.net/manual/en/function.json-decode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/no.php.net/manual/en/function.json-decode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/no.php.net/manual/en/function.json-decode.php?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://no.php.net/manual/en/function.json-decode.php"><strong>json_decode()</strong></a> function.</p>
<p>To simplify this further I implemented two abstract classes, a ServiceServer class and a ServiceClient class. The service server class contained a very simple<strong> displayJSONResult</strong> function, which sets the correct header, encode the data as json data and echo the data.</p>
<p>The service client class has two main functions, <strong>doGetRequest</strong> and <strong>doPostRequest</strong>, both functions use the <a href="http://no.php.net/manual/en/book.curl.php" onclick="urchinTracker('/outgoing/no.php.net/manual/en/book.curl.php?referer=');">cURL</a> library in PHP.</p>
<h3>Server</h3>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">abstract <span class="kw2">class</span> ServiceServer</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;protected <span class="kw2">function</span> displayJSONResult<span class="br0">&#40;</span><span class="re1">$data</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&#39;Content-type: text/plain&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">echo</span> json_encode<span class="br0">&#40;</span><span class="re1">$data</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">exit</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>Client</h3>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* ServiceClient</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* @author Kristian Lunde</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> ServiceClient</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * do a post request to a service</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * the params parameter must be a string with the format:</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * key=val&amp;key2=val2&amp;key3=val3</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * @param string $url</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * @param string $params</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; */</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;protected <span class="kw2">function</span> doPostRequest<span class="br0">&#40;</span><span class="re1">$url</span><span class="sy0">,</span> <span class="re1">$params</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$ch</span> <span class="sy0">=</span> curl_init<span class="br0">&#40;</span><span class="re1">$url</span><span class="br0">&#41;</span><span class="sy0">;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; curl_setopt<span class="br0">&#40;</span><span class="re1">$ch</span><span class="sy0">,</span> CURLOPT_SSL_VERIFYPEER <span class="sy0">,</span> <span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; curl_setopt<span class="br0">&#40;</span><span class="re1">$ch</span><span class="sy0">,</span> CURLOPT_RETURNTRANSFER <span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; curl_setopt<span class="br0">&#40;</span><span class="re1">$ch</span><span class="sy0">,</span> CURLOPT_POST &nbsp; <span class="sy0">,</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;curl_setopt<span class="br0">&#40;</span><span class="re1">$ch</span><span class="sy0">,</span> CURLOPT_POSTFIELDS &nbsp; &nbsp; <span class="sy0">,</span> <span class="re1">$params</span><span class="br0">&#41;</span><span class="sy0">;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$result</span> <span class="sy0">=</span> curl_exec<span class="br0">&#40;</span><span class="re1">$ch</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; curl_close<span class="br0">&#40;</span><span class="re1">$ch</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="re1">$result</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * do a get request to a service</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * @param string $url</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; * @return mixed</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; */</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;protected <span class="kw2">function</span> doGetRequest<span class="br0">&#40;</span><span class="re1">$url</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$ch</span> <span class="sy0">=</span> curl_init<span class="br0">&#40;</span><span class="re1">$url</span><span class="br0">&#41;</span><span class="sy0">;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; curl_setopt<span class="br0">&#40;</span><span class="re1">$ch</span><span class="sy0">,</span> CURLOPT_SSL_VERIFYPEER <span class="sy0">,</span> <span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; curl_setopt<span class="br0">&#40;</span><span class="re1">$ch</span><span class="sy0">,</span> CURLOPT_RETURNTRANSFER <span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$result</span> <span class="sy0">=</span> curl_exec<span class="br0">&#40;</span><span class="re1">$ch</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; curl_close<span class="br0">&#40;</span><span class="re1">$ch</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="re1">$result</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h3>Example</h3>
<p>To illustrate the easiness and simplicity of this SOA approach I written a small example. In this example the server finds the country of a city using POST parameters or it can find cities using the country as a GET parameter. </p>
<h4>Example server</h4>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Example of using the ServiceServer class</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Returns countries or cities</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* @author Kristian Lunde</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#39;ServiceServer.php&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> ExampleServer <span class="kw2">extends</span> ServiceServer</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">countries</span> <span class="sy0">=</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;norway&#39;</span> &nbsp;<span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;Oslo&#39;</span><span class="sy0">,</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Trondheim&#39;</span><span class="sy0">,</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Bergen&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Halden&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Sarpsborg&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Hammerfest&#39;</span><span class="br0">&#41;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;sweden&#39;</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;Stockholm&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Gothenburg&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Karlstad&#39;</span><span class="br0">&#41;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#39;england&#39;</span> <span class="sy0">=&gt;</span> <span class="kw3">array</span><span class="br0">&#40;</span><span class="st0">&#39;London&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Newcastle&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Bath&#39;</span><span class="sy0">,</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#39;Liverpool&#39;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span> &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$_GET</span><span class="br0">&#91;</span><span class="st0">&#39;country&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">findCitiesByCountry</span><span class="br0">&#40;</span><span class="kw3">trim</span><span class="br0">&#40;</span><span class="re1">$_GET</span><span class="br0">&#91;</span><span class="st0">&#39;country&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$_POST</span><span class="br0">&#91;</span><span class="st0">&#39;city&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">findCountryByCity</span><span class="br0">&#40;</span><span class="kw3">trim</span><span class="br0">&#40;</span><span class="re1">$_GET</span><span class="br0">&#91;</span><span class="st0">&#39;city&#39;</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> findCitiesByCountry<span class="br0">&#40;</span><span class="re1">$country</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$cities</span> <span class="sy0">=</span> <span class="st0">&#39;Not found&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$country</span> <span class="sy0">=</span> <span class="kw3">strtolower</span><span class="br0">&#40;</span><span class="re1">$country</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">countries</span><span class="br0">&#91;</span><span class="re1">$country</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="re1">$cities</span> <span class="sy0">=</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">countries</span><span class="br0">&#91;</span><span class="re1">$country</span><span class="br0">&#93;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">displayJSONResult</span><span class="br0">&#40;</span><span class="re1">$cities</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> findCountryByCity<span class="br0">&#40;</span><span class="re1">$city</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$country</span> <span class="sy0">=</span> <span class="st0">&#39;Not found&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$break</span> <span class="sy0">=</span> <span class="kw2">false</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">countries</span> <span class="kw1">as</span> <span class="re1">$key</span> <span class="sy0">=&gt;</span> <span class="re1">$val</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">for</span><span class="br0">&#40;</span><span class="re1">$i</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="re1">$count</span> <span class="sy0">=</span> <span class="kw3">count</span><span class="br0">&#40;</span><span class="re1">$val</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="re1">$i</span> <span class="sy0">&lt;</span> <span class="re1">$count</span><span class="sy0">;</span> <span class="re1">$i</span><span class="sy0">++</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re1">$city</span> <span class="sy0">==</span> <span class="re1">$val</span><span class="br0">&#91;</span><span class="re1">$i</span><span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="re1">$country</span> <span class="sy0">=</span> <span class="re1">$key</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="re1">$break</span> <span class="sy0">=</span> <span class="kw2">true</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="kw1">break</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span><span class="re1">$break</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">break</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">displayJSONResult</span><span class="br0">&#40;</span><span class="re1">$country</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$obj</span> <span class="sy0">=</span> <span class="kw2">new</span> ExampleServer<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<h4>Example client</h4>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Example of using the ServiceClient class</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* does a request to the ExampleServer</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* @author Kristian Lunde</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#39;ServiceClient.php&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> ExampleClient <span class="kw2">extends</span> ServiceClient</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">getCities</span><span class="br0">&#40;</span><span class="st0">&#39;Norway&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">getCounty</span><span class="br0">&#40;</span><span class="st0">&#39;Bath&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> getCities<span class="br0">&#40;</span><span class="re1">$country</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$url</span> <span class="sy0">=</span> <span class="st0">&#39;http://files.klunde.net/files.klunde.net/ExampleServer.php?country=&#39;</span> <span class="sy0">.</span> <span class="re1">$country</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">echo</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">doGetRequest</span><span class="br0">&#40;</span><span class="re1">$url</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">function</span> getCountry<span class="br0">&#40;</span><span class="re1">$city</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$url</span> <span class="sy0">=</span> <span class="st0">&#39;http://files.klunde.net/files.klunde.net/ExampleServer.php&#39;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="re1">$params</span> <span class="sy0">=</span> <span class="st0">&#39;city=&#39;</span> <span class="sy0">.</span> <span class="re1">$city</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">echo</span> <span class="re1">$this</span><span class="sy0">-&gt;</span><span class="me1">doPostRequest</span><span class="br0">&#40;</span><span class="re1">$url</span><span class="sy0">,</span> <span class="re1">$params</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re1">$obj</span> <span class="sy0">=</span> <span class="kw2">new</span> ExampleClient<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>I&#8217;m quite satisfied with this implementation because it is so easy to maintain and even more important, implementing new services and clients for the services is straight forward and supports rapid development.</p>
<h3>Download</h3>
<p><a onclick="urchinTracker('/outgoing/files.klunde.net/soa_example/ServiceServer.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/files.klunde.net/soa_example/ServiceServer.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://www.klunde.net/downloads/soa_example/ServiceServer.phps">ServiceServer.phps</a></p>
<p><a onclick="urchinTracker('/outgoing/files.klunde.net/soa_example/ServiceClient.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/files.klunde.net/soa_example/ServiceClient.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://www.klunde.net/downloads/soa_example/ServiceClient.phps">ServiceClient.phps</a></p>
<p><a onclick="urchinTracker('/outgoing/files.klunde.net/soa_example/ExampleServer.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/files.klunde.net/soa_example/ExampleServer.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/files.klunde.net/soa_example/ExampleServer.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://www.klunde.net/downloads/soa_example/ExampleServer.phps">ExampleServer.phps</a><a onclick="urchinTracker('/outgoing/files.klunde.net/soa_example/ExampleClient.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/files.klunde.net/soa_example/ExampleClient.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://www.klunde.net/downloads/soa_example/ExampleClient.phps"></a></p>
<p><a onclick="urchinTracker('/outgoing/files.klunde.net/soa_example/ExampleClient.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');urchinTracker('/outgoing/files.klunde.net/soa_example/ExampleClient.phps?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=23&amp;message=4');" href="http://www.klunde.net/downloads/soa_example/ExampleClient.phps">ExampleClient.phps</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/06/08/writing-soa-applications-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing exceptions in PHP</title>
		<link>http://www.klunde.net/2008/05/24/writing-exceptions-in-php/</link>
		<comments>http://www.klunde.net/2008/05/24/writing-exceptions-in-php/#comments</comments>
		<pubDate>Sat, 24 May 2008 21:48:48 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=22</guid>
		<description><![CDATA[
			
				
			
		
Fredrik Johan Holmström has a entry on his blog about writing exceptions in PHP. He points out that several of the large PHP framework assumes that an exception is a fatal error, and that this may be a flaw in the design.
An Exception is not necessarily a fatal error and it should be the responsibility [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F05%2F24%2Fwriting-exceptions-in-php%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F05_2F24_2Fwriting-exceptions-in-php_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F05%2F24%2Fwriting-exceptions-in-php%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p><a onclick="urchinTracker('/outgoing/loveandtheft.org/?referer=');urchinTracker('/outgoing/loveandtheft.org/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=22');urchinTracker('/outgoing/loveandtheft.org/?referer=http://www.klunde.net/wp-admin/post-new.php?posted=22');urchinTracker('/outgoing/loveandtheft.org/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=22&amp;message=4');urchinTracker('/outgoing/loveandtheft.org/?referer=http://www.klunde.net/wp-admin/post-new.php');" href="http://loveandtheft.org/">Fredrik Johan Holmström</a> has a entry on his blog about writing exceptions in PHP. He points out that several of the large PHP framework assumes that an exception is a fatal error, and that this may be a flaw in the design.</p>
<p>An Exception is not necessarily a fatal error and it should be the responsibility of the code that catches the exception to determine if it is a fatal error or not.</p>
<p>The blog entry got me thinking, and yes I&#8217;ve done that mistake a few times myself, I&#8217;ll do it right  the next time I write an exception in PHP,</p>
<p>URL: <a onclick="urchinTracker('/outgoing/loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/?referer=');urchinTracker('/outgoing/loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=22');urchinTracker('/outgoing/loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/?referer=http://www.klunde.net/wp-admin/post-new.php?posted=22');urchinTracker('/outgoing/loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/?referer=http://www.klunde.net/wp-admin/post.php?action=edit&amp;post=22&amp;message=4');urchinTracker('/outgoing/loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/?referer=http://www.klunde.net/wp-admin/post-new.php');" href="http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/">http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/05/24/writing-exceptions-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
