<?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>The Open Coder</title>
	<atom:link href="http://www.opencoder.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.opencoder.co.uk</link>
	<description>Helping the fellow geek</description>
	<lastBuildDate>Fri, 15 Apr 2011 12:25:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>instanceof is bad in ActionScript</title>
		<link>http://www.opencoder.co.uk/2011/04/instanceof-is-bad-in-actionscript/</link>
		<comments>http://www.opencoder.co.uk/2011/04/instanceof-is-bad-in-actionscript/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 12:25:41 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[General ramblings]]></category>
		<category><![CDATA[actionscript]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=460</guid>
		<description><![CDATA[This is a quick post to let everyone aware that instanceof should be avoided like the plague in your Flex/ActionScript 3 projects. Coming from a Java development background I was quite familiar with using instanceof to test if a class is of a certain type, in Java this also matches interfaces, not so in ActionScript. [...]]]></description>
			<content:encoded><![CDATA[<p>This is a quick post to let everyone aware that <em>instanceof</em> should be avoided like the plague in your Flex/ActionScript 3 projects. Coming from a Java development background I was quite familiar with using <em>instanceof</em> to test if a class is of a certain type, in Java this also matches interfaces, not so in ActionScript.</p>
<p>To illustrate the problem, here are some simple classes and an interface:</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="kw3">public</span> <span class="kw2">class</span> Vehicle <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> <span class="kw3">_name</span>:<span class="kw3">String</span>;</p>
<p>&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> Vehicle<span class="br0">&#40;</span><span class="kw3">name</span>:<span class="kw3">String</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">_name</span> = <span class="kw3">name</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">get</span> <span class="kw3">name</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">String</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw3">_name</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> drive<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;Vehicle named &quot;</span>+<span class="kw3">_name</span>+<span class="st0">&quot; drove.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="kw3">public</span> <span class="kw3">interface</span> IManualGears <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; <span class="kw2">function</span> changeGear<span class="br0">&#40;</span>gear:<span class="kw3">int</span><span class="br0">&#41;</span>:<span class="kw3">void</span>;</p>
<p><span class="br0">&#125;</span></p>
<p><span class="kw3">public</span> <span class="kw2">class</span> ManualCar <span class="kw3">extends</span> Vehicle <span class="kw3">implements</span> IManualGears <span class="br0">&#123;</span></p>
<p>&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> ManualCar<span class="br0">&#40;</span><span class="kw3">name</span>:<span class="kw3">String</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> changeGear<span class="br0">&#40;</span>gear:<span class="kw3">int</span><span class="br0">&#41;</span>:<span class="kw3">void</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">trace</span><span class="br0">&#40;</span><span class="st0">&quot;Vehicle named &quot;</span>+<span class="kw3">_name</span>+<span class="st0">&quot; changed gear to &quot;</span>+gear<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span></div>
</div>
<p>If I were to create a new instance of ManualCar and tested for the interface IManualGears using instanceof it would fail, as shown below;</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="kw2">var</span> car:ManualCar = <span class="kw2">new</span> ManualCar<span class="br0">&#40;</span><span class="st0">&quot;Ford Escort&quot;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw2">var</span> isVehicle:<span class="kw3">Boolean</span> = <span class="br0">&#40;</span>car <span class="kw3">instanceof</span> Vehicle<span class="br0">&#41;</span>; &nbsp;<span class="co1">//isVehicle would be true</span></p>
<p><span class="kw2">var</span> isManualCar:Booelan = <span class="br0">&#40;</span>car <span class="kw3">instanceof</span> ManualCar<span class="br0">&#41;</span>; &nbsp;<span class="co1">//isManualCar would be true</span></p>
<p><span class="kw2">var</span> hasGears:<span class="kw3">Boolean</span> = <span class="br0">&#40;</span>car <span class="kw3">instanceof</span> IManualGears<span class="br0">&#41;</span>; &nbsp;<span class="co1">//hasGears would be false</span></p>
<p>hasGears:<span class="kw3">Boolean</span> = <span class="br0">&#40;</span>car is IManualGears<span class="br0">&#41;</span>; &nbsp;<span class="co1">//hasGears would be true;</span></div>
</div>
<p>To be fair this is stated in the flex documentation, but it did catch me out. So the moral of the story is to always use <em>is</em> instead of <em>instanceof</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2011/04/instanceof-is-bad-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>URL regular expression</title>
		<link>http://www.opencoder.co.uk/2011/01/url-regular-expression/</link>
		<comments>http://www.opencoder.co.uk/2011/01/url-regular-expression/#comments</comments>
		<pubDate>Wed, 05 Jan 2011 13:30:26 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=461</guid>
		<description><![CDATA[This is just a quick post to share a regular expression for a URL I had to come up with when needing to validate a URL in a Flex app. The code below is for Flex, but would only require a few minor changes for another language, the double backslash before the ? appears to [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post to share a regular expression for a URL I had to come up with when needing to validate a URL in a Flex app. The code below is for Flex, but would only require a few minor changes for another language, the double backslash before the ? appears to be required for Flex, using a single backslash does not work, read more about that in this <a title="Flex regular expression issues" href="http://www.opencoder.co.uk/2010/03/regexpvalidator-issues/" target="_blank">older post</a>. This also contains what would be capturing brackets in other languages, I could have used non-capturing brackets but that would have made this already complicated example even more difficult to read.</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">linkValidator = <span class="kw2">new</span> RegExpValidator<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
linkValidator.<span class="me1">expression</span> = <span class="st0">&quot;(http(s)?:<span class="es0">\/</span><span class="es0">\/</span>)?(([a-z]+[a-z0-9<span class="es0">\-</span>]*[.])?([a-z0-9]+[a-z0-9<span class="es0">\-</span>]*[.])+[a-z]{2,3}|localhost)(<span class="es0">\/</span>[a-z0-9_-]+[a-z0-9_ -]*)*<span class="es0">\/</span>?(<span class="es0">\\</span>?[a-z0-9_-]+=[a-z0-9 ',.-]*(&amp;amp;[a-z0-9_-]+=[a-z0-9 ',.-]*)*)?(#[a-z0-9/_-]*)?$&quot;</span>;<br />
linkValidator.<span class="me1">noMatchError</span> = resourceManager.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;lang&quot;</span>, <span class="st0">&quot;invalidURL&quot;</span><span class="br0">&#41;</span>;<br />
linkValidator.<span class="me1">flags</span> = <span class="st0">&quot;i&quot;</span>;<br />
linkValidator.<span class="me1">source</span> = linkTextArea;<br />
linkValidator.<span class="me1">property</span> = <span class="st0">&quot;text&quot;</span>;<br />
linkValidator.<span class="me1">trigger</span> = linkTextArea;<br />
linkValidator.<span class="me1">triggerEvent</span> = Event.<span class="me1">CHANGE</span>;</div>
</div>
<p>I&#8217;ll break it down to the individual sections with a brief explaination.</p>
<pre>
//protocol and subdomain
(http(s)?:\/\/)?(([a-z]+[a-z0-9\-]*[.])?
</pre>
<p>The first part includes the protocol (http:// or https://), I am only dealing with web http urls here and it is optional in my app hence the ? at the end of the first group, the rest includes an optional subdomain which should start with one or more letters followed by zero or more letters/numbers/hyphens and a dot. This first subdomain and dot is also optional. So far this would match: <em>[empty string] http:// https//www. https://ww2</em> etc.</p>
<pre>
//server hostname
([a-z0-9]+[a-z0-9\-]*[.])+[a-z]{2,3}|localhost)
</pre>
<p>This next part includes the rest of the web host, the first grouping (first enclosing brackets) specifies the start of the hostname or a further subdomain which must start with a letter or number followed by a dot (the dot as a character set is how to represent the dot in Flex, you might be able to just use <em>\.</em>). This can be repeated many times, but then should be followed by 2 or three characters. Alternatively the hostname localhost can be used instead, the extra closing bracket matches the additional opening one after the protocol. This section should match: <em>www.example.com localhost example.com example.co.uk co.uk</em> etc.</p>
<pre>
//web path
(\/[a-z0-9_-]+[a-z0-9_ -]*)*\/?
</pre>
<p>This next part consists of the optional path (directory from the web root), it starts with a forward slash and can be any number of letters, numbers, underscores, spaces or hyphens, but can not start with a space (you might need to backslash escape your hypen in a different language. The trailing backslash is also optional as is the entire path. This part should match: <em>[empty string] / /directory /a/b/</em> etc.</p>
<pre>
//query string
(\\?[a-z0-9_-]+=[a-z0-9 ',.-]*(&amp;amp;[a-z0-9_-]+=[a-z0-9 ',.-]*)*)?
</pre>
<p>This part contains the optional query string part of the URL. Starting with a ? (may require only a single backslash in a different language), followed by the first parameter made up of one or more letters/numbers/underscores/hyphens the equals sign, followed by an optional parameter value made up of letters/numbers/spaces/apostrophes/commas/dots/hypens. This parameter=value part of the query string can be repeated several times after that but each extra parameter should be preceded with the ampersand (you would normally use just the &#038; for this, but flex requires the encoded version). This section could match: <em>[empty string] ?a= ?a=bc ?a=b&#038;c=d&#038;e=f</em> etc.</p>
<pre>
//fragment
(#[a-z0-9/_-]*)?$
</pre>
<p>Finally the last part of the expression contains the optional url fragment (the part with a #). In my case I specified zero or more letters/numbers/forward slashes/underscores/hypens (flex does not require escaping the forward slash when it is included in a character set). Then the dollar sign specifies that there should not be anything else after this. This could match: <em>[empty string] # #value #a/b/c</em> etc.</p>
<p>I hope this is useful to those struggling to create their own URL regular expression matchers. Flex devs remember to double escape the ? for the query string part of the URL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2011/01/url-regular-expression/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Eclipse PDT compare view broken</title>
		<link>http://www.opencoder.co.uk/2010/11/eclipse-pdt-compare-view-broken/</link>
		<comments>http://www.opencoder.co.uk/2010/11/eclipse-pdt-compare-view-broken/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 20:32:15 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[pdt]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=422</guid>
		<description><![CDATA[This is a very short post which will probably become irrelevant by the time someone sees it, but if you are a PHP developer using eclipse you might have noticed that your compare viewer is not working. I ran into this a few times before bothering to do a bit of research and found this [...]]]></description>
			<content:encoded><![CDATA[<p>This is a very short post which will probably become irrelevant by the time someone sees it, but if you are a PHP developer using eclipse you might have noticed that your compare viewer is not working. I ran into this a few times before bothering to do a bit of research and found this <a title="PDT bug - Compare completely broken" href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=326194" target="_blank">bug report</a>.</p>
<p>So it looks like the bug has been fixed but has not yet been released. So for those who want their compare viewer working again and don&#8217;t want to read the bug report, the solution is go to Help &#8211; Install New Software &#8211; Add a new site (link below) and select PHP Development Tools (PDT) Runtime Feature, and the SDK if you want it. Heres the link:</p>
<pre><a href="http://download.eclipse.org/tools/pdt/updates/2.2/milestones">http://download.eclipse.org/tools/pdt/updates/2.2/milestones</a></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2010/11/eclipse-pdt-compare-view-broken/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery remote form validation</title>
		<link>http://www.opencoder.co.uk/2010/07/jquery-remote-form-validation/</link>
		<comments>http://www.opencoder.co.uk/2010/07/jquery-remote-form-validation/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 16:06:56 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=260</guid>
		<description><![CDATA[Again another long time between posts, this one is partly a small tutorial and partly about a small issue I encountered and workaround. This post discusses form validation using a cool jQuery form validation plugin created by Jörn Zaefferer, which I highly recommend, you can download the plugin from here. The plugin allows you to [...]]]></description>
			<content:encoded><![CDATA[<p>Again another long time between posts, this one is partly a small tutorial and partly about a small issue I encountered and workaround. This post discusses form validation using a cool jQuery form validation plugin created by Jörn Zaefferer, which I highly recommend, you can download the plugin from <a title="jQuery plugin, form validation" href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">here</a>. The plugin allows you to quickly and easily validate your html forms using the power of jQuery, the basic principle is that you specify the rules for validation on the fields of your form and submitting the form will be disabled until all fields are valid, where validation rules are broken the user is informed with messages next to the invalid fields.  I wouldn&#8217;t be surprised if this plugin became part of jQuery directly since so many people are using it already.</p>
<p>Lets have a look at a quick simple example, lets say I have a form with 3 input fields, name, email and website, we want to have the name and email fields required, we want to check the email is valid and because we are storing these fields in a database with a character limit we need to check the fields are not too long. Below is the html form code and the javascript for the validation.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span><br />
$<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fullname<span class="sy0">:</span> <span class="br0">&#123;</span> required<span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span> maxlength<span class="sy0">:</span> 64 <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> 32 <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; website<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> 32 <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></p>
<p>&lt;form id=&quot;subscribe_test&quot; action=&quot;save_subscriber.php&quot; method=&quot;POST&quot;&gt;<br />
&nbsp; &lt;table&gt;<br />
&nbsp; &nbsp; &lt;tbody&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label for=&quot;fullname&quot;&gt;Full name: *&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;fullname&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label for=&quot;email&quot;&gt;Email: *&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;email&quot; class=&quot;email required&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label for=&quot;website&quot;&gt;Website:&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;website&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &lt;/tbody&gt;<br />
&nbsp; &lt;/table&gt;<br />
&lt;/form&gt;</div>
</div>
<p>In the above example you can see that you can apply the validation rules both in javascript and also using the predefined rule names as the css class for the input. E.g. the email input field is using classes <em>email</em> and <em>required</em>, the validation plugin will look for any class names on the inputs and apply the validation rule if there is one. I could have defined the email rule solely in the script but I wanted to show that there is a quicker way using classes. The working example for the above code is below.</p>
<p><script type="text/javascript">
jQuery(function($) { $("#subscribe_test").validate({ rules: {
     fullname: { required: true, maxlength: 64 },
     email: { maxlength: 32 },
     website: { maxlength: 32 }
 } });
});
</script></p>
<form id="subscribe_test" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label for="fullname">Full name: *</label></td>
<td>
<input id="fullname" name="fullname" type="text" /></td>
</tr>
<tr>
<td><label for="email">Email: *</label></td>
<td>
<input id="email" class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td><label for="website">Website:</label></td>
<td>
<input id="website" name="website" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<p>You can also do remote validation. For example, lets say in our example we did not want someone with the same email address to be subscribed twice. You can use a special validation rule <em>remote</em> which points to a script which should output true or false based on the form input value. We will also give a special message for when the email address is already in use. The changed script code is then:</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span><br />
&nbsp;$<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test2&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span> rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fullname<span class="sy0">:</span> <span class="br0">&#123;</span> required<span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span> maxlength<span class="sy0">:</span> <span class="nu0">64</span> <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;email<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> <span class="nu0">32</span><span class="sy0">,</span> remote<span class="sy0">:</span> <span class="st0">&quot;/check_subscribed.php&quot;</span> <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;website<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> <span class="nu0">32</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; messages<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; email<span class="sy0">:</span><span class="br0">&#123;</span> remote<span class="sy0">:</span> <span class="st0">&quot;Already subscribed&quot;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></div>
</div>
<p>The running example now will not allow you to enter the email <em>test@example.com</em>, for this example there is a script called <em>check_subscribed.php</em> which is hard coded to look for the POST email variable and return false if it equals <em>test@example.com</em>.</p>
<p><script type="text/javascript">
jQuery(function($){      
    $("#subscribe_test2").validate({ 
        rules: {
           fullname: { required: true, maxlength: 64 },
           email: { maxlength: 32, remote: "/check_subscribed.php" },
           website: { maxlength: 32 }
        },
        messages: {
           email: { remote: "Already subscribed"}
        }
     });
});
</script></p>
<form id="subscribe_test2" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label>Full name: *</label></td>
<td>
<input name="fullname" type="text" /></td>
</tr>
<tr>
<td><label>Email: *</label></td>
<td>
<input class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td><label>Website:</label></td>
<td>
<input name="website" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<p>So that is all good everything seems to be validating fine. Now I will show you a small case to watch out for. Lets assume that the subscribe form needs to be submitted using ajax and only accepts a required email address which should be remotely validated. Below is the code and the working example, if you simply enter the already subscribed email <em>test@example.com</em> and submit you see it does not work properly, the form gets submitted even though the validation fails.
<p><strong>Update 29/09/2010</strong> since updating this post and rechecking the code it appears, this actually does work. Perhaps the browsers have changed the order the javascript events occur, I certainly did not change anything, this <em>was</em> an issue previously.</p>
<p>Code and example below.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span><br />
jQuery<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>$<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3&quot;</span><span class="br0">&#41;</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>$<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">valid</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$.<span class="me1">ajax</span><span class="br0">&#40;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type<span class="sy0">:</span> <span class="st0">&quot;POST&quot;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url<span class="sy0">:</span> <span class="st0">&quot;/save_subscriber.php&quot;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data<span class="sy0">:</span> <span class="st0">&quot;email=&quot;</span><span class="sy0">+</span>$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3_email&quot;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;#submit_result&quot;</span><span class="br0">&#41;</span>.<span class="me1">html</span><span class="br0">&#40;</span><span class="st0">&quot;Ajax submit results:<br />
&quot;</span> <span class="sy0">+</span> data<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span> rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;email<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> <span class="nu0">32</span><span class="sy0">,</span> remote<span class="sy0">:</span> <span class="st0">&quot;/check_subscribed.php&quot;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="sy0">,</span> messages<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span> remote<span class="sy0">:</span> <span class="st0">&quot;Already subscribed&quot;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></p>
<p>&lt;form id=&quot;subscribe_test3&quot; action=&quot;save_subscriber.php&quot; method=&quot;POST&quot;&gt;<br />
&nbsp; &lt;table&gt;<br />
&nbsp; &nbsp; &lt;tbody&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label&gt;Email: *&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;subscribe_test3_email&quot; class=&quot;email required&quot; name=&quot;email&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &lt;/tbody&gt;<br />
&nbsp; &lt;/table&gt;<br />
&lt;/form&gt;</div>
</div>
<p><script type="text/javascript">
jQuery(function($){
       $("#subscribe_test3").submit(function(e){
          if ($(this).valid()){
               $.ajax( {
                   type: "POST",
                   url: "/save_subscriber.php",
                   data: "email="+$("#subscribe_test3_email").val(),
                   success: function(data){
                        $("#submit_result").html("Ajax submit results:" + data);
                   }
               });
          }
          return false;
    });
    $("#subscribe_test3").validate({ rules: {
         email: { maxlength: 32, remote: "/check_subscribed.php" }
     }, messages: {
        email: { remote: "Already subscribed"}
     }
     });
});
</script></p>
<form id="subscribe_test3" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label>Email: *</label></td>
<td>
<input id="subscribe_test3_email" class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
</tr>
</tbody>
</table>
</form>
<p>The reason the above example did not work properly is because the remote validation checking is done asynchronously, as is the ajax request. In the above code, the form submit event binding function checks first if the form is valid before doing the ajax submit. The trouble is that the remote validation is done asynchronously. So the valid check is just returning true instead of false. In order to solve this the remote validation needs to be done synchronously instead (or async: false). Below is the relevant fix and the fixed example.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; maxlength<span class="sy0">:</span> <span class="nu0">32</span><span class="sy0">,</span> remote<span class="sy0">:</span> <span class="br0">&#123;</span> url<span class="sy0">:</span> <span class="st0">&quot;/check_subscribed.php&quot;</span><span class="sy0">,</span> async<span class="sy0">:</span> <span class="kw2">false</span> <span class="br0">&#125;</span> <br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; messages<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span> remote<span class="sy0">:</span> <span class="st0">&quot;Already subscribed&quot;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p><script type="text/javascript">
jQuery(function($){
       $("#subscribe_test4").submit(function(e){
          if ($(this).valid()){
               $.ajax( {
                   type: "POST",
                   url: "save_subscriber.php",
                   data: "email="+$("#subscribe_test4_email").val(),
                   success: function(data){
                        $("#submit_result2").html("Ajax submit results:" + data);
                   }
               });
          }
          return false;
    });
    $("#subscribe_test4").validate({ rules: {
         email: { maxlength: 32, remote: { url: "/check_subscribed.php", async: false } }
     }, messages: {
        email: { remote: "Already subscribed"}
     }
     });
});
</script></p>
<form id="subscribe_test4" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label>Email: *</label></td>
<td>
<input id="subscribe_test4_email" class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
</tr>
</tbody>
</table>
</form>
<p><strong>Update 27/09/2010</strong>
<p>It has been asked what the check_subscribed.php script did, so here it is below:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="re0">$email</span> <span class="sy0">=</span> <span class="re0">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&quot;email&quot;</span><span class="br0">&#93;</span><span class="sy0">;</span><br />
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$email</span> <span class="sy0">==</span> <span class="st0">&quot;test@example.com&quot;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="st0">&quot;false&quot;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="st0">&quot;true&quot;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p><strong>Update 01/11/2010</strong>
<p>Someone has asked for the files used in these examples, so here is a <a href="http://www.opencoder.co.uk/wp-content/uploads/2011/03/remote_form_validation.zip">zip file</a> containing a html page and the necessary php scripts. The jquery and validation plugin are not included in the zip but are referenced as external scripts. This is a recommended way of including jQuery in your sites (but obviously a more up to date version).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2010/07/jquery-remote-form-validation/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>FreeBSD secure mailserver</title>
		<link>http://www.opencoder.co.uk/2010/05/freebsd-secure-mailserver/</link>
		<comments>http://www.opencoder.co.uk/2010/05/freebsd-secure-mailserver/#comments</comments>
		<pubDate>Sat, 08 May 2010 18:16:10 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Servers]]></category>
		<category><![CDATA[dynamic dns]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[imap]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[smtp]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=237</guid>
		<description><![CDATA[Very recently I decided to clean my home FreeBSD server  and start from scratch, meaning deleting everything and reinstalling FreeBSD. The fact that I happened to do this after completely messing up my ports system is purely coincidence&#8230; I took the time to try out the new FreeBSD release 8.0, so I downloaded the iso [...]]]></description>
			<content:encoded><![CDATA[<p>Very recently I decided to clean my home FreeBSD server  and start from scratch, meaning deleting everything and reinstalling FreeBSD. The fact that I happened to do this after completely messing up my ports system is purely coincidence&#8230; <img src='http://www.opencoder.co.uk/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I took the time to try out the new FreeBSD release 8.0, so I downloaded the iso from the <a title="FreeBSD.org" href="http://www.freebsd.org/where.html" target="_blank">FreeBSD site</a>, burned it and installed it on my home server (the server I&#8217;m referring to is in this <a title="Creating a home web server" href="http://www.opencoder.co.uk/2009/08/creating-a-home-web-server/" target="_blank">post</a>). After reinstalling all the ports I had on it previously (Apache, MySQL, PHP5, PHP5-extensions, Java 1.6, Openfire etc.), I decided to go one step further and try something I hadn&#8217;t done before, get email working.</p>
<h3>DNS settings</h3>
<p>To be able to receive email I needed a domain, now it happens that I purchased <a title="Chris McDonald - Open Coder" href="http://chrismcdonald.co.uk" target="_blank">chrismcdonald.co.uk</a> at some point last year, got it very cheap for 2 years from 123-reg. The domain wasn&#8217;t really doing anything and was just being used to forward to this blog (and still is), so I decided to get more use out of it and use a subdomain to point to my home server. I set the hostname of my FreeBSD server to <em>server.chrismcdonald.co.uk</em> by adding <em>hostname=&#8221;server.chrismcdonald.co.uk&#8221;</em> to /etc/rc.conf and added an entry in /etc/hosts so that it would resolve locally. So my hosts file now contained:</p>
<pre>127.0.0.1               localhost server.chrismcdonald.co.uk
</pre>
<p>To get the domain to point to my home server I changed my dns records for <em>chrismcdonald.co.uk</em> and added a CNAME record pointing <em>server.chrismcdonald.co.uk</em> to my dynamic dns host <em>chris.is-a-geek.net</em>. I also added an MX record for the root domain which directs email to <em>chrismcdonald.co.uk</em> to <em>chris.is-a-geek.net</em>. Dynamic DNS allows users with a non-static IP (most home internet users) to use a host name to refer to your system&#8217;s ip address, which might change whenever you connect to your ISP. It works by having your home system contact the dynamic dns provider every so often to make sure the ip address mapping is kept up to date. I use <a title="DynDNS.com" href="http://www.dyndns.com/" target="_blank">dyndns.com</a>, you can create a free account with them where you can create some free host names to use. You get to pick your own subdomain from any one of the domains they allow you to use, I chose the domain <em>is-a-geek.net</em> and my subdomain <em>chris</em>. My wireless router has built in support to work with dyndns.com and keep my host ip address updated, but if yours does not, there are some instructions also in my <a title="Creating a home web server part 2" href="http://www.opencoder.co.uk/2009/09/creating-a-home-web-server-part-2/" target="_blank">older post</a> to get this working on FreeBSD.</p>
<h3>Receiving mail</h3>
<p>To ensure I could send and receive email past my wireless router, I added some port forwards for ports 25 (smtp), 465 (secure smtp) and 993 (secure imap), I already have existing port forwards set up for web and ssh access. Sendmail is configured by default to only allow sending of email and receiving mail only from localhost. To enable sendmail to receive mail externally I added this line to /etc/rc.conf and started sendmail again.</p>
<pre>sendmail_enable="YES"

/etc/rc.d/sendmail stop
/etc/rc.d/sendmail start</pre>
<p>OK so with the domain name sorted out and sendmail set to receive external mail I tried testing it out. I logged into another remote server, which was located outside of my local network on the internet. I used telnet to attempt to manually send an email. Below is the transcript, my commands are in bold, square brackets are for your server name and email.</p>
<pre><strong>telnet server.chrismcdonald.co.uk 25</strong>
Connected to chris.is-a-geek.net.
Escape character is '^]'.
220 server.chrismcdonald.co.uk ESMTP Sendmail 8.14.4/8.14.3; Sat, 8 May 2010
12:08:15 +0100 (BST)
<strong>HELO [remote server name]</strong>
250 server.chrismcdonald.co.uk Hello [remote server name and ip] , pleased
to meet you
<strong>MAIL FROM: [user@remoteserver]</strong>
250 2.1.0 [user@remoteserver]... Sender ok
<strong>RCPT TO: [user@server - in this case chris at chrismcdonald.co.uk]</strong>
550 5.7.1 [user@server]... Relaying denied
<strong>QUIT</strong>
221 2.0.0 server.chrismcdonald.co.uk closing connection</pre>
<p>So it appeared that my server was not accepting email for my domain. To fix this I created the file /etc/mail/local-host-names and added the following lines, you would add your own hostnames.</p>
<pre>chrismcdonald.co.uk
server.chrismcdonald.co.uk</pre>
<p>Then when I tried again:</p>
<pre><strong>telnet server.chrismcdonald.co.uk 25</strong>
Connected to chris.is-a-geek.net.
Escape character is '^]'.
220 server.chrismcdonald.co.uk ESMTP Sendmail 8.14.4/8.14.3; Sat, 8 May 2010
12:08:15 +0100 (BST)
<strong>HELO [remote server name]</strong>
250 server.chrismcdonald.co.uk Hello [remote server name and ip], pleased
to meet you
<strong>MAIL FROM: [user@remoteserver]</strong>
250 2.1.0 [user@remoteserver]... Sender ok
<strong>RCPT TO: [user@server - in this case chris at chrismcdonald.co.uk]</strong>
550 5.7.1 [user@server]... Recipient ok
<strong>DATA</strong>
354 Enter mail, end with "." on a line by itself
<strong>Subject: test email again
Just testing
.</strong>
250 2.0.0 o48BLo6R001500 Message accepted for delivery
<strong>QUIT</strong>
221 2.0.0 server.chrismcdonald.co.uk closing connection</pre>
<p>Sure enough I had the new email in my local mailbox (checked by running <em>mail</em>).</p>
<h3>IMAP access</h3>
<p>So now I could receive mail from outside, I looked around for a few guides to get started on setting up secure IMAP and secure SMTP sending. I needed a program that would provide IMAP access to mail and came across this <a title="Dovecot" href="http://www.freebsddiary.org/dovecot.php" target="_blank">blog post</a> on Dovecot on <a title="FreeBSD Diary" href="http://www.freebsddiary.org" target="_blank">freebsddiary.org</a>, this is a great site with loads of tutorials and info on FreeBSD. I followed the steps in the guide to install Dovecot with some minor adjustments.</p>
<pre>cd /usr/ports/mail/dovecot
make install clean</pre>
<p>Below are the make options I used:</p>
<div id="attachment_240" class="wp-caption aligncenter" style="width: 648px"><a href="http://www.opencoder.co.uk/wp-content/uploads/2010/05/dovecot_make_options1.png"><img class="size-full wp-image-240" title="Dovecot make options" src="http://www.opencoder.co.uk/wp-content/uploads/2010/05/dovecot_make_options1.png" alt="Dovecot make options" width="638" height="434" /></a><p class="wp-caption-text">Dovecot make options</p></div>
<p>You can follow those steps from freebsddiary if you are starting from scratch. I ignored the certificate stuff because I already had a self signed one created, I also chose to use PAM authentication, which uses your user account password to authenticate. The important bits which I changed from /usr/local/etc/dovecot.conf are below, change them where they occur in the file:</p>
<pre>protocols = imaps
ssl_cert_file = /etc/ssl/certs/server.chrismcdonald.co.uk.cert
ssl_key_file = /etc/ssl/private/server.chrismcdonald.co.uk.key
ssl_key_password = ******************
mail_location = mbox:~/mail/:INBOX=/var/mail/%u
protocol imap {
listen = *:143
ssl_listen = *:993
# Login executable location.
...
}
auth default {
  # Space separated list of wanted authentication mechanisms:
  #   plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi otp skey
  #   gss-spnego
  # NOTE: See also disable_plaintext_auth setting.
  mechanisms = plain login
...
}
</pre>
<p>Then add the following to /etc/rc.conf and start dovecot.</p>
<pre>dovecot_enable="YES"

/usr/local/etc/rc.d/dovecot start</pre>
<p>Now I access my mail using imap securely over port 993. So far the server is able to receive mail, send mail (only from localhost) and has secure imap access to read mail, if this is suitable for what you need you can stop here. If you want to be able to send mail from another machine using your secure smtp read on.</p>
<h3>SMTP sending</h3>
<p>I found a couple more guides on having secure smtp authentication <a title="Secure SMTP and POP access on FreeBSD" href="http://www.hydrus.org.uk/journal/secure-mail.html" target="_blank">here</a> and <a title="FreeBSD as a secure mailserver" href="http://www.puresimplicity.net/~hemi/freebsd/sendmail.html" target="_blank">here</a>. I did not follow those steps but borrowed some from both guides. The common setup appears to use cyrus sasl (Simple Authentication and Security Layer) for the authentication and to configure sendmail to use it. SASL SASL support is not build into sendmail by default, the guides said to recompile sendmail from its source /usr/src/usr.sbin/sendmail which I couldn&#8217;t find, I guess these things are all done using the ports system now, so here&#8217;s what I installed.</p>
<pre>cd /usr/ports/security/cyrus-sasl2
make install clean
cd /usr/ports/security/cyrus-sasl2-saslauthd
make install clean
cd /usr/ports/mail/sendmail-sasl
make install clean</pre>
<p>Then I needed to enable the sasl daemon by editing /etc/rc.conf and adding this line and starting the sasl daemon.</p>
<pre>saslauthd_enable="YES"

/usr/local/etc/rc.d/saslauthd start</pre>
<p>Finally sendmail needed to be configured to use secure smtp authentication, I also copied my certificate files that I generated a while back into  /etc/mail/certs. I copied the default <em>freebsd.mc</em> and <em>freebsd.submit.mc</em> files located in /etc/mail to <em>server.chrismcdonald.co.uk.mc</em> and <em>server.chrismcdonald.co.uk.submit.mc</em> and edited <em>server.chrismcdonald.co.uk.mc</em>. Then following bits from both guides I added the following:</p>
<pre>dnl set SASL options
TRUST_AUTH_MECH(`GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN')dnl
define(`confAUTH_MECHANISMS', `GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN')dnl
dnl Offer SMTP AUTH only after encryption (STARTTLS) has been negotiated
define(`confAUTH_OPTIONS',`p,y')dnl

define(`CERT_DIR', `/etc/mail/certs')dnl
define(`confCACERT_PATH', `CERT_DIR')dnl
define(`confCACERT', `CERT_DIR/server.chrismcdonald.co.uk.cert')dnl
define(`confSERVER_CERT', `CERT_DIR/server.chrismcdonald.co.uk.cert')dnl
define(`confSERVER_KEY', `CERT_DIR/server.chrismcdonald.co.uk.key')dnl
define(`confCLIENT_CERT', `CERT_DIR/server.chrismcdonald.co.uk.cert')dnl
define(`confCLIENT_KEY', `CERT_DIR/server.chrismcdonald.co.uk.key')dnl
DAEMON_OPTIONS(`Port=smtps, Name=TLSMTA, M=s')dnl</pre>
<p>The lines starting with <em>dnl</em> are comments and those backticks ` are not typos, the opening quote is a backtick and the closing one a normal apostrophe. Finally after running the following in /etc/mail I had secure smtp authentication working:</p>
<pre>make
make install
make restart
</pre>
<p>Well, that&#8217;s it for another post, sorry it was such a long one. I am buying a couple of real servers on eBay that wer e going pretty cheap, the kind they use in datacentres. I will probably have a go at getting internal dns working, so that I won&#8217;t be restricted to a single server working at home. If it works I will do another post on it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2010/05/freebsd-secure-mailserver/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RegExpValidator issues</title>
		<link>http://www.opencoder.co.uk/2010/03/regexpvalidator-issues/</link>
		<comments>http://www.opencoder.co.uk/2010/03/regexpvalidator-issues/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 13:32:46 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[General ramblings]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[mxml]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=170</guid>
		<description><![CDATA[Today I came across an annoying problem involving the RegExpValidator class in Flex. In flex app for one of our projects StickyWorld, we allow users to upload images, pdfs, 3d models and most recently, reference YouTube movies inside a virtual room where people can comment on them, adding sticky notes in context. In our upload [...]]]></description>
			<content:encoded><![CDATA[<p>Today I came across an annoying problem involving the RegExpValidator class in Flex. In flex app for one of our projects <a title="StickyWorld online collaborative design review" href="http://www.stickyworld.com" target="_blank">StickyWorld</a>, we allow users to upload images, pdfs, 3d models and most recently, reference YouTube movies inside a virtual room where people can comment on them, adding sticky notes in context. In our upload window, users can choose what type of media they want to add to the room, in the case of a YouTube video only the video id is required. To make it a bit more flexible we decided to allow the users to specify a url to a video as well as just the video id. The app needed to handle urls in several different formats, e.g.</p>
<ul>
<li>http://www.youtube.com/watch?v=[videoid]</li>
<li>http://www.youtube.com/v/[videoid]</li>
<li>http://www.youtube.com/[userchannel]/[somepath]/[videoid]</li>
</ul>
<p>Sounds like a job for a regular expression right? Since we also wanted to do some validation on the field to make sure we were given a valid YouTube video url or video id, it seemed that the RegExpValidator class was perfect for the job. However after adding the RegExpValidator in MXML I had a problem that took a good while to figure out what was wrong.</p>
<p>Below is the snippet of code containing the regular expression string, its use in the validator and a validation handler function, apologies for the wrapping of the regular expression, I have split it below where the main formats are separated by pipes.</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;"><span class="br0">&#91;</span>Bindable<span class="br0">&#93;</span><br />
<span class="kw3">private</span> <span class="kw2">var</span> youtubeRegExp:<span class="kw3">String</span> = <br />
<span class="st0">&quot;^(?:http:<span class="es0">\/</span><span class="es0">\/</span>(?:www<span class="es0">\.</span>)?youtube<span class="es0">\.</span>com<span class="es0">\/</span>watch(?:<span class="es0">\?</span>|#!)v=(.<span class="es0">\{</span>11<span class="es0">\}</span>)(?:&amp;.*)?<br />
|(.<span class="es0">\{</span>11<span class="es0">\}</span>)<br />
|http:<span class="es0">\/</span><span class="es0">\/</span>(?:www<span class="es0">\.</span>)?youtube<span class="es0">\.</span>com<span class="es0">\/</span>(?:v|[A-Za-z0-9#<span class="es0">\/</span>_<span class="es0">\-</span>]*)<span class="es0">\/</span>(.<span class="es0">\{</span>11<span class="es0">\}</span>))$&quot;</span>;</p>
<p><span class="kw3">private</span> <span class="kw2">function</span> youtubeValid<span class="br0">&#40;</span>ev:ValidationResultEvent<span class="br0">&#41;</span>:<span class="kw3">void</span><br />
<span class="br0">&#123;</span><br />
    <span class="kw1">if</span> <span class="br0">&#40;</span>ev.<span class="kw3">type</span> == ValidationResultEvent.<span class="me1">VALID</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
        <span class="kw1">for</span> <span class="br0">&#40;</span><span class="kw2">var</span> i:<span class="kw3">int</span> = <span class="nu0">0</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i <span class="sy0">&lt;</span> ev.<span class="me1">results</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span>.<span class="me1">matchedSubstrings</span>.<span class="kw3">length</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
             <span class="kw1">if</span> <span class="br0">&#40;</span>ev.<span class="me1">results</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span>.<span class="me1">matchedSubstrings</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">!</span>= <span class="kw2">null</span><span class="br0">&#41;</span><br />
                  youtubeVidId = ev.<span class="me1">results</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span>.<span class="me1">matchedSubstrings</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp;     <span class="br0">&#125;</span><br />
        txtName.<span class="kw3">text</span> = youtubeVidId;<br />
        btnSubmit.<span class="kw3">enabled</span> = txtYouTube.<span class="kw3">text</span>.<span class="me1">length</span><span class="sy0">&gt;</span><span class="nu0">0</span>;<br />
    <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
         btnSubmit.<span class="kw3">enabled</span> = <span class="kw2">false</span>;<br />
    <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></p>
<p><span class="sy0">&lt;</span>mx :RegExpValidator id=<span class="st0">&quot;youtubeValidator&quot;</span> <br />
&nbsp; &nbsp; source=<span class="st0">&quot;{txtYouTube}&quot;</span> property=<span class="st0">&quot;text&quot;</span> <br />
&nbsp; &nbsp; expression=<span class="st0">&quot;{youtubeRegExp}&quot;</span> valid=<span class="st0">&quot;youtubeValid(event)&quot;</span><br />
&nbsp; &nbsp; invalid=<span class="st0">&quot;youtubeValid(event)&quot;</span> <br />
&nbsp; &nbsp; noMatchError=<span class="st0">&quot;YouTube video url/id invalid&quot;</span><br />
&nbsp; &nbsp; trigger=<span class="st0">&quot;{txtYouTube}&quot;</span><span class="sy0">/&gt;</span></div>
</div>
<p>The problem was, nothing was coming out as valid, not even the videoid just by itself. So wrote a test using the regular expression, creating a RegExp object using the expression and testing some urls using the exec function and it appeared to be working fine, so why not with the validator? In the end it turned out to be that the problem was down to several characters needing to be escaped. The correct expression that worked is below:</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">youtubeRegExp = <br />
<span class="st0">&quot;^(?:http:<span class="es0">\/</span><span class="es0">\/</span>(?:www<span class="es0">\.</span>)?youtube<span class="es0">\.</span>com<span class="es0">\/</span>watch(?:<span class="es0">\\</span>?|#!)v=(.<span class="es0">\{</span>11<span class="es0">\}</span>)(?:&amp;;.*)?<br />
|(.<span class="es0">\{</span>11<span class="es0">\}</span>)<br />
|http:<span class="es0">\/</span><span class="es0">\/</span>(?:www<span class="es0">\.</span>)?youtube<span class="es0">\.</span>com<span class="es0">\/</span>(?:v|[A-Za-z0-9#<span class="es0">\/</span>_<span class="es0">\-</span>]*)<span class="es0">\/</span>(.<span class="es0">\{</span>11<span class="es0">\}</span>))$&quot;</span>;</div>
</div>
<p>Since the regular expression used in RegExpValidator needs to be a String and the String is used as a bound property in MXML the curly brackets need to be be preceded with a backslash, because without them in MXML it means a data binding. That didn&#8217;t make much sense to me, since I was binding a String variable which contained the expression, but ok, I can kind of see the problem. What made even less sense was that I needed to double escape the ? when I actually wanted to include a literal ? in the expression. So I suppose since it is a string and I want to include the backslash character before the question mark I need to escape the backslash itself, leaving \\?, but then shouldn&#8217;t I have to do that for all the other times I need to include a backslash in the expression? Well it turns out the answer is no, I do not really understand it and only figured this out after a lot of debugging.</p>
<p>Another pitfall to avoid, if you are binding on a condition in MXML and you need to use logical operators, you will need to encode the logical AND &amp;&amp; should be &amp;amp;&amp;amp;, the less than &lt; should be &amp;lt; or greater than signs &gt; should be &amp;gt;, logical OR || works fine. However in those cases you should really be binding to a function which returns the boolean result you are looking for.</p>
<p>Working with MXML is cool because you can create data-bindable UI components quickly and easily, but it really isn&#8217;t when you have to worry about silly issues like wondering why your regular expressions are not working.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2010/03/regexpvalidator-issues/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Trac adding git, mylyn and post-commit hooks</title>
		<link>http://www.opencoder.co.uk/2010/03/trac-adding-git-mylyn-and-post-commit-hooks/</link>
		<comments>http://www.opencoder.co.uk/2010/03/trac-adding-git-mylyn-and-post-commit-hooks/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 19:14:58 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Servers]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[trac]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=153</guid>
		<description><![CDATA[It has been another long time between posts, but here is as promised, but very delayed, the second part to this post. Working with git repositories in trac It is incredibly simple to use a git repository instead of svn with trac, you just need to have the right plugin installed and enabled. Once again [...]]]></description>
			<content:encoded><![CDATA[<p>It has been another long time between posts, but here is as promised, but very delayed, the second part to <a title="Setting up a svn trac server" href="http://www.opencoder.co.uk/2010/01/setting-up-a-svn-and-trac-server/" target="_blank">this post</a>.</p>
<h3>Working with git repositories in trac</h3>
<p>It is incredibly simple to use a git repository instead of svn with trac, you just need to have the right plugin installed and enabled. Once again I&#8217;ll give install details for FreeBSD and Ubuntu.</p>
<p>Firstly we need to install the trac git plugin. For FreeBSD there is as usual a dedicated port install for what we need. As of writing the plugin version is 0.11.0.2 r7034. As the root user run:</p>
<pre>cd /usr/ports/www/trac-gitplugin
make install clean</pre>
<p>Ubuntu users, as root or using <em>sudo</em>:</p>
<pre>apt-get install trac-git</pre>
<p>With the trac git plugin installed, all we need to do is create a new git repository if we haven&#8217;t got one already and create a new trac project pointing to the git repository (or edit the trac.ini config file of an existing trac project). So following from the FreeBSD logic in the previous post, lets create a new git repository in /usr/local/git as root.</p>
<pre>cd /usr/local
mkdir git
cd git
mkdir MyGitProject
git init MyGitProject</pre>
<p>The git repository above was created by the root user, if you are accessing the git repository over ssh and want to be able to push changes to it, you will need to make sure that the user has read and write access to the folder and everything in the .git folder. What I have done in the past is create a group on the system called <em>developers</em> and add all the user accounts to the group. Then change the group of the git repository to <em>developers</em> and give it group write access. You have to make sure the whole folder and all files are writable by this group or you will get errors trying to push to the repository. An alternative is to just create the git repository using one user account or change the owner of the whole repository to a specific user account and only use that user account to push and pull to the server.</p>
<p>With the repository created, we can now create a new trac project, this is the same steps as in the previous post but we change the repository type from svn to git.</p>
<pre>cd /usr/local/trac
mkdir MyTracGitProject
trac-admin MyTracGitProject initenv</pre>
<p>The trac-admin program runs in interactive mode and will ask you questions to set up your repository, example questions and responses are given below.</p>
<pre>Project Name [My Project]&gt; My Trac Git Project
Database connection string [sqlite:db/trac.db]&gt;
Repository type [svn]&gt; git
Path to repository [/path/to/repos]&gt; /usr/local/git/MyGitProject/.git</pre>
<p>Now we just need to edit the trac.ini, this is in the trac project folder, so in our case /usr/local/trac/MyTracGitProject/conf/trac.ini. Make sure that under the [trac] section repository_type is <em>git</em> and the repository_dir is correctly pointing to the <em>.git</em> folder. Also FreeBSD gets the location of the git binary wrong, under the [git] section change git_bin from <em>/usr/bin/git</em> to <em>/usr/local/bin/git</em>. Finally, add the following if your trac.ini file does not already have it.</p>
<pre>[components]
tracext.git.* = enabled</pre>
<p>Now we just need to change the owner of the new trac project to the web user and edit the apache config for the new trac project location. See the <a title="Setting up a trac server" href="http://www.opencoder.co.uk/2010/01/setting-up-a-svn-and-trac-server/" target="_blank">previous</a> post for how to do that.</p>
<h3>Adding Mylyn support for trac</h3>
<p>Mylyn for Eclipse is great, if you are not using it already, you should be. It basically allows you to create tasks, those tasks you can activate and work on, whilst a task is activated it looks at what files you have open and have been working on. So that the next time you activate the task, you see only the files that are most relevant to the task, for more info on Mylyn go <a title="Mylyn" href="http://www.eclipse.org/mylyn/" target="_blank">here</a>. Mylyn is integrated in the more recent editions of Eclipse, but there is a Mylyn trac plugin that allows you to create and view tickets in your trac repository. How nice is that? If someone reports a new bug, you can see it appear right in your IDE, work on it and close it, all from within Eclipse.</p>
<p>To get the Mylyn support working in trac you need to install and enable the xml-rpc plugin. As usual FreeBSD has a port for it.</p>
<pre>cd /usr/ports/www/trac-xmlrpc
make install clean</pre>
<p>Ubuntu users can run:</p>
<pre>easy_install -Z -U http://trac-hacks.org/svn/xmlrpcplugin/trunk # 0.11</pre>
<p>At the end of the install process keep a look out for the location where the python egg was installed, we need to copy that file to our plugins folder in the trac project. This was /usr/local/lib/python2.6/site-packages/TracXMLRPC-1.0.6-py2.6.egg on FreeBSD.</p>
<p>Edit the trac.ini file, under the [component] section add:</p>
<pre>tracrpc.* = enabled</pre>
<p>Now when you go to the admin section of your trac site you should see the xml-rpc plugin available under <em>Plugins</em>.</p>
<h3>Close and comment on trac tickets in your SVN commit messages</h3>
<p>You can have trac close and add comments to your tickets simply by including certain text triggers in your SVN commit messages, such as <em>fixes #231</em>, r<em>efs bug:29. </em>To get this working you need to include a special script in your svn repository location. First download this <a title="Trac post commit hook" href="http://trac.edgewall.org/browser/branches/0.11-stable/contrib/trac-post-commit-hook?format=txt" target="_blank">python script</a> from the trac web site. Save it in a location which is easily accessible, on FreeBSD I put the script in the trac share folder <em>/usr/local/share/trac</em>.</p>
<p>Now go to your svn repository location in the hooks folder you should see a lot of files with a .tmpl extension. One of them is post-commit.tmpl, we need to copy this and edit it as follows:</p>
<pre>cp post-commit.tmpl post-commit
edit post-commit</pre>
<p>Ignore all the comments, at the end make sure the file reads as below:</p>
<pre>TRAC_ENV="/usr/local/trac/[TracProjectName]"
REPOS="$1"
REV="$2"

/usr/local/bin/python /usr/local/share/trac/trac-post-commit.py -p "$TRAC_ENV" -r "$REV"
#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf</pre>
<p>Replace [TracProjectName] with your trac project name or change the path if yours is different. I specified the full path to the python command, on freebsd when this shell script is triggered the script does not inherit you environment PATH, so absolute paths to commands need to be specified, at least on FreeBSD anyway. Change the path of the trac-post-commit.py file to wherever you saved it. I commented out the mailer.py script, this is another post-commit script that you can run which sends an email detailing the commit. This script should be shipped with your subversion installation, on FreeBSD it is in <em>/usr/local/share/subversion/hook-scripts/mailer/mailer.py</em>.</p>
<p>Finally we need to make sure that the python script is executable and owned by the web user like everything else in the svn repository.</p>
<pre>chmod 755 post-commit
chown www post-commit</pre>
<p>Now when you do your next commit to the repository try closing or referencing one of the trac tickets. You can refer to a ticket by prefixing the ticket number with <em>#</em>, <em>ticket:</em>, <em>issue:</em> or <em>bug:</em>. You can close a ticket using by using a command prefix <em>close, closed, closes, fix, fixed, fixes</em> before the ticket reference. To comment on a ticket but not close it use any of these command prefixes: <em>addresses, re, references, refs</em> or <em>see</em>.</p>
<p>You can customise these commands or ticket references, just edit the trac-post-commit.py file, the following is what you&#8217;ll need to change:</p>
<pre>ticket_prefix = '(?:#|(?:ticket|issue|bug)[: ]?)'

    _supported_cmds = {'close':      '_cmdClose',
                       'closed':     '_cmdClose',
                       'closes':     '_cmdClose',
                       'fix':        '_cmdClose',
                       'fixed':      '_cmdClose',
                       'fixes':      '_cmdClose',
                       'addresses':  '_cmdRefs',
                       're':         '_cmdRefs',
                       'references': '_cmdRefs',
                       'refs':       '_cmdRefs',
                       'see':        '_cmdRefs'}</pre>
<p>So, thats all for now. Apparently you can also do git post-commit hooks as well, but as I am not using git that much I haven&#8217;t tried it out yet. Happy coding with your svn/git trac repositories.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2010/03/trac-adding-git-mylyn-and-post-commit-hooks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up a svn trac server</title>
		<link>http://www.opencoder.co.uk/2010/01/setting-up-a-svn-and-trac-server/</link>
		<comments>http://www.opencoder.co.uk/2010/01/setting-up-a-svn-and-trac-server/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 21:27:24 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Servers]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[freebsd]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[trac]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=118</guid>
		<description><![CDATA[It has been way too long since the last post, so here is something you might find useful. A couple of months back we noticed that our commercially hosted svn and trac repositories were becoming quite unresponsive and slow, which held up development albeit only slightly. Time is money as they say, so we decided [...]]]></description>
			<content:encoded><![CDATA[<p>It has been way too long since the last post, so here is something you might find useful.</p>
<p>A couple of months back we noticed that our commercially hosted svn and trac repositories were becoming quite unresponsive and slow, which held up development albeit only slightly. Time is money as they say, so we decided to cancel our commercial hosting and go for a local svn set up on our development server (just an old networked dell laptop, not very high specs at all). Our dev server is just a freebsd 7.1 install on an old dell laptop, which already had Apache, MySQL and PHP installed (FAMP server). For this guide you will need to have at least Apache installed (plus a few other port installs which I will detail below), but if you&#8217;re going to install Apache you might as well go all the way and have MySQL and PHP, see <a title="Creating a home webserver" href="http://www.opencoder.co.uk/2009/08/creating-a-home-web-server/" target="_blank">here</a> and <a title="Creating a home webserver part 2" href="http://www.opencoder.co.uk/2009/09/creating-a-home-web-server-part-2/" target="_blank">here</a> posts on this. This guide is mainly for FreeBSD, although steps should be similar for Ubuntu, although your installs are very different, where appropriate I&#8217;ll put the ubuntu alternative installation steps.</p>
<p>Assuming you have already set up your FAMP server, next steps are as follows:</p>
<h3>Install subversion</h3>
<p>Use the make options as defined below. At the time of writing I am installing subversion 1.6.6</p>
<pre>cd /usr/ports/devel/subversion
make install clean</pre>
<div id="attachment_119" class="wp-caption aligncenter" style="width: 430px"><img class="size-full wp-image-119" title="Subversion make options" src="http://www.opencoder.co.uk/wp-content/uploads/2010/01/Screen-shot-2010-01-03-at-16.30.05.png" alt="Subversion make options" width="420" height="292" /><p class="wp-caption-text">Subversion make options</p></div>
<p>This might take a while depending on how many dependencies need to be installed.</p>
<h3>Install trac</h3>
<p>Leave the options as they are or copy from below. At time of writing I am installing trac 0.11.5</p>
<pre>cd /usr/ports/www/trac
make install clean</pre>
<div id="attachment_135" class="wp-caption aligncenter" style="width: 428px"><img class="size-full wp-image-135" title="trac make options" src="http://www.opencoder.co.uk/wp-content/uploads/2010/01/Screen-shot-2010-01-08-at-21.33.05.png" alt="trac make options" width="418" height="290" /><p class="wp-caption-text">trac make options</p></div>
<h3>Install mod_python for apache</h3>
<p>From /usr/ports/www/mod_python3. Trac runs on python, the trac website neds to be handled by python so we need to install this apache module for the trac website(s) to work</p>
<pre>cd /usr/ports/www/mod_python3
make install clean</pre>
<p>Apache needs to be set up to enable the new mod_python module, so edit the httpd.conf file located in /usr/local/etc/apache22/httpd.conf add the following to the end of the LoadModule section if it is not there already.</p>
<pre>LoadModule python_module libexec/apache22/mod_python.so</pre>
<h3>Install Git (optional)</h3>
<p>There is a trac plugin to work with git repositiories if you prefer that to svn. I will not go into git now, see my next post for integrating trac with a git repository.</p>
<pre>cd /usr/ports/devel/git
make install clean</pre>
<div id="attachment_136" class="wp-caption aligncenter" style="width: 431px"><img class="size-full wp-image-136" title="git make options" src="http://www.opencoder.co.uk/wp-content/uploads/2010/01/Screen-shot-2010-01-08-at-21.33.36.png" alt="git make options" width="421" height="290" /><p class="wp-caption-text">git make options</p></div>
<p>You do not need perforce or cvs support unless you want it. I have never used either, only subversion. You will not be able to install the GUI Tools without install the X11 windowing system. If you have already got X11 installed, then I would recommend installing the GUI tools as they can help with visualising the current branch on the development tree, but if not then do not bother as X11 takes <em>AGES</em> to install and will bloat your system with hundreds of dependencies.</p>
<h3>Install the trac git plugin (optional)</h3>
<p>You can install the trac git plugin if you want to work with git repositories rather than svn.</p>
<pre>cd /usr/ports/www/trac-gitplugin
make install clean</pre>
<p>Ubuntu install steps for the all of the above are below(they are a lot simpler and quicker! Ubuntu uses pre-compiled files binary files and automatically sets up apache with the relevant modules loaded):</p>
<pre>sudo apt-get install subversion
sudo apt-get install libapache2-mod-python
sudo apt-get install libapache2-svn
sudo apt-get install trac
sudo apt-get install git-core
sudo apt-get install trac-git</pre>
<p>Now that we have all the programs we need lets go on to set up an svn repository.</p>
<h3>Create an SVN repository</h3>
<p>First we need to decide where we are going to have our svn repository, where ever you decide to keep it, you will need to make sure it is readable (and writable if you want to make checkins) by the apache user. For this example I am storing all my svn repositories under one location, /usr/local/svn. Make sure you are root and run the following:</p>
<pre>cd /usr/local
mkdir svn
cd svn
mkdir repositories
cd respositories
mkdir Test
svnadmin create Test
cd /usr/local/svn
htpasswd -c .passwd [Username]
[Enter password, and again]
chown -R www /usr/local/svn</pre>
<p>Ok, the steps above we created a location where we are going to store our svn repositories, we created one respository called Test, then we created an apache basic authentication access file which the svn server will use to authenticate the users. Finally we changed the owner of the entire svn directory over to the apache web user. Ubuntu users can perform the same steps, feel free to choose a different location than /usr/local, this is common practice in a freebsd system.</p>
<h3>Setup Apache to serve our SVN repository</h3>
<p>Now we need to tell apache which directory contains our svn repository. As root create the following file by running:</p>
<pre>touch /usr/local/etc/apache22/Includes/svn.conf</pre>
<p>Ubuntu users:</p>
<pre>touch /etc/apache2/conf.d/svn.conf</pre>
<p>Then edit that file and save the following in it:</p>
<pre>&lt;Location /svn&gt;<br style="padding: 0px; margin: 0px;" />DAV svn<br style="padding: 0px; margin: 0px;" />SVNParentPath /usr/local/svn/repositories<br style="padding: 0px; margin: 0px;" />AuthType Basic<br style="padding: 0px; margin: 0px;" />AuthName "Subversion access"<br style="padding: 0px; margin: 0px;" />AuthUserFile /usr/local/svn/.passwd<br style="padding: 0px; margin: 0px;" />Require valid-user<br style="padding: 0px; margin: 0px;" />&lt;/Location&gt;</pre>
<p>Now if you restart your apache webserver (apachectl restart) and navigate to <em>http://[server address]/svn/Tes</em>t you should be asked for your username and password. Once you have logged in you should see your empty repository!</p>
<h3>Create a trac repository</h3>
<p>Now we will create a trac repository, I will follow the same logic as for the svn repositories and keep all the trac repositories under /usr/local/trac. As root again run:</p>
<pre>cd /usr/local
mkdir trac
cd trac
mkdir Test
trac-admin Test initenv</pre>
<p>trac-admin is an interactive program and you will be asked a series of questions to set up the trac repository. You will have to enter or accept the default value (by pressing return) at the following prompts:</p>
<pre>Project Name [My Project]&gt; Test
Database connection string [sqlite:db/trac.db]&gt;
Repository type [svn]&gt;
Path to repository [/path/to/repos]&gt; /usr/local/svn/repositories/Test</pre>
<p>When you have finished change the owner of the trac folder to the apache web user:</p>
<pre>chown -R www /usr/local/trac</pre>
<p>The steps are again the same for Ubuntu users.</p>
<h3>Setup apache to serve our trac repository</h3>
<p>Apache needs to serve the trac repository using the mod_python module we installed earlier. To set this up, create the configuration file by running the following as root:</p>
<pre>touch /usr/local/etc/apache22/Includes/trac.conf</pre>
<p>Ubuntu users:</p>
<pre>touch /etc/apache2/conf.d/trac.conf</pre>
<p>Save the following text in the new file:</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;Location /trac/MyRepoName&gt;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">SetHandler mod_python</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">PythonHandler trac.web.modpython_frontend</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">PythonOption TracEnv /usr/local/trac/Test</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">PythonOption TracUriRoot /trac/Test</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">AuthType Basic</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">AuthName &#8220;trac access&#8221;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">AuthUserFile /usr/local/svn/.passwd</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">Require valid-user</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 3074px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">&lt;/Location&gt;</div>
<pre>&lt;Location /trac/MyRepoName&gt;
SetHandler mod_python
PythonHandler trac.web.modpython_frontend
PythonOption TracEnv /usr/local/trac/Test
PythonOption TracUriRoot /trac/Test
AuthType Basic
AuthName "trac access"
AuthUserFile /usr/local/svn/.passwd
Require valid-user
&lt;/Location&gt;</pre>
<p>Now if you restart apache again by running apachectl restart and navigate to <em>http://[server address]/trac/Test</em> you should be asked to authenticate and see your trac site!</p>
<h3>Testing the SVN repository</h3>
<p>To test the repository we will checkout a working copy, in your users home directory, try the following:</p>
<pre>mkdir svnworkingcopy
cd svnworkingcopy
svn checkout http://[server address]/svn/Test . --username [username]
[Enter password]
svn mkdir trunk branches tags
svn commit -m "Checking in svn directory structure"</pre>
<p>Now if you go back to the trac site and click the <em>Timeline</em> button you should see the first commit with the message. Click on <em>Browse source</em> to see the directory structure we just created. You now have everything you need to work with your trac and svn repository and to start adding and committing your project code.</p>
<p>Look out for the next post which will detail a few more steps to get more out of your trac server setup including:</p>
<ul>
<li>Using a git repository instead by using the trac git plugin</li>
<li>A few extras steps that will help integrating your trac repository with the Eclipse Mylyn trac connector</li>
<li>A svn post-commit hook to automatically add comments or close your trac tickets, simply by referring to a ticket number in your svn commit messages</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2010/01/setting-up-a-svn-and-trac-server/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Regular expressions in mysql</title>
		<link>http://www.opencoder.co.uk/2009/10/regular-expressions-in-mysql/</link>
		<comments>http://www.opencoder.co.uk/2009/10/regular-expressions-in-mysql/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 21:38:00 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[openfire]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=73</guid>
		<description><![CDATA[It has been a very long time since I posted anything so I thought I would share this small snippet. Recently I had to extract some data which was stored as xml in a field within a mysql database table. To explain briefly, this data was actually saved as part of an XMPP (jabber) chat [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a very long time since I posted anything so I thought I would share this small snippet.</p>
<p>Recently I had to extract some data which was stored as xml in a field within a mysql database table. To explain briefly, this data was actually saved as part of an XMPP (jabber) chat message by an openfire server. <a title="Openfire real time collaboration server" href="http://www.igniterealtime.org/projects/openfire/" target="_blank">Openfire</a> is a real time collaboration server which includes jabber functionality. To store the chat history Openfire records the chat message as an xml chunk in a field named <em>body</em> in a particular database table <em>ofMucConversationLog</em>. I needed to extract certain data that our system had communicated as a chat message through openfire, to do this I used a combination of regular expressions and string manipulation.</p>
<p>Ok, lets say I have messages that are either in format A or B as below:</p>
<pre>&lt;messageformata&gt;
  &lt;child1&gt;some text&lt;/child1&gt;
  &lt;child2&gt;some more text&lt;/child2&gt;
&lt;/messageformata&gt;

&lt;messageformatb&gt;
  &lt;child1&gt;some text&lt;/child1&gt;
  &lt;child2&gt;some more text&lt;/child2&gt;
&lt;/messageformatb&gt;</pre>
<p>Each row in the database can contain xml in the format of A or B and there are many rows. If I need to extract only the contents of the first child of message format a, I could use the following SQL code to do it (assuming I&#8217;ve created a temporary table called <em>temptable</em> to store these values):</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> temptable <span class="br0">&#40;</span>child1value<span class="br0">&#41;</span><br />
<span class="kw1">SELECT</span><br />
SUBSTRING<span class="br0">&#40;</span>body<span class="sy0">,</span> LOCATE<span class="br0">&#40;</span><span class="st0">&#8216;&lt;child1&gt;&#8217;</span><span class="sy0">,</span> body<span class="br0">&#41;</span><span class="sy0">+</span><span class="nu0">8</span><span class="sy0">,</span> <br />
&nbsp; LOCATE<span class="br0">&#40;</span><span class="st0">&#8216;&lt;/child1&gt;&#8217;</span><span class="sy0">,</span> body<span class="br0">&#41;</span> <span class="sy0">-</span> LOCATE<span class="br0">&#40;</span><span class="st0">&#8216;&lt;child1&gt;&#8217;</span><span class="sy0">,</span> body<span class="br0">&#41;</span> <span class="sy0">-</span> 8<span class="br0">&#41;</span><br />
<span class="kw1">FROM</span> ofMucConversationLog <span class="kw1">WHERE</span> body <span class="kw1">REGEXP</span> <span class="st0">&#8216;&lt;messageformata&gt;*&#8217;</span>;</div>
</div>
<p>Unfortunately the REGEXP operator can only be used for testing true or false and so is probably only useful in a WHERE clause. There are no capturing options like in other languages (php, perl java etc.), however you can use it in combination with the substring and locate functions to get the job done, although it is admittedly tedious.</messageformata></child1></p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2009/10/regular-expressions-in-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Bug in Flash Player FileReference browse affecting Macs</title>
		<link>http://www.opencoder.co.uk/2009/09/bug-in-flash-player-filereference-browse-affecting-macs/</link>
		<comments>http://www.opencoder.co.uk/2009/09/bug-in-flash-player-filereference-browse-affecting-macs/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 12:36:40 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[mac]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=58</guid>
		<description><![CDATA[After updating my debugger version of Adobe Flash Player to 10.0.32.18 I noticed that Mouse Events were no longer working correctly. There appears to be a bug in the more recent flash player which appears to break the mouse events. It only appears to affect Macs, I am having the problem on both Safari 4.0.3 [...]]]></description>
			<content:encoded><![CDATA[<p>After updating my debugger version of Adobe Flash Player to 10.0.32.18 I noticed that Mouse Events were no longer working correctly.</p>
<p>There appears to be a bug in the more recent flash player which appears to break the mouse events. It only appears to affect Macs, I am having the problem on both Safari 4.0.3 and FireFox 3.5.3. I made a test app below which adds a few mouse event listeners to the white box and informs you of the events being received. Try it out below and comment on your results.</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_testfilerefbrowse_1072645193"
			class="flashmovie"
			width="625"
			height="313">
	<param name="movie" value="/wp-content/flash/testfilerefbrowse.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/wp-content/flash/testfilerefbrowse.swf"
			name="fm_testfilerefbrowse_1072645193"
			width="625"
			height="313">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>If you do not have a problem, do not upgrade your flash player or you will. This has been reported as a bug to Adobe <a href="https://bugs.adobe.com/jira/browse/FP-2785" target="_blank">here</a> and <a href="https://bugs.adobe.com/jira/browse/FP-2674" target="_blank">here</a>, more people are starting to notice it, hopefully Adobe will fix it soon.</p>
<h4>Update 25/09/09</h4>
<p>So far the results of people trying out the above test on a mac have been varied. The problem seems to be occuring in Snow Leopard 1.6, with most people FireFox being the only browser experiencing the bug. I have witnessed someone running Leopard with the same versions of FireFox and Safari as myself with no problems at all. Most people who are running Snow Leopard appear to only experience the bug in FireFox with safari working fine. However, as I mentioned above,  I am having the bug appear both in Safari and FireFox. I don&#8217;t know what is so different about my set up, apart from that I have installed the latest debugger version of the Flash Player and that my system is 32-bit only.</p>
<p>If you do try out the test above and want to report your results, please provide as much information as you can, what versions of browser you are using, what is your flash player version, what OS version, 32 or 64-bit etc? Some people think the problem is with FireFox but I am not so sure.</p>
<h4>Update 06/10/09</h4>
<p>Ok, I am pretty sure now that this problem is not to do with a flash player upgrade, it is to do with Snow Leopard!</p>
<p>Recently my hard disk died, I reinstalled Snow Leopard and then restored my files from a time machine backup, I did not restore my applications.</p>
<p>Upon reinstalling Flex Builder I noticed that my flash player was now at version 9, which gave me an opportunity to test if this bug was still happening and it is, both in Safari and FireFox for Flash Player 9,0,124,0 as well. This almost definitely means the problem is down to the operating system, either Apple needs to fix it or Adobe need to come up with a fix in Flash Player. I can not run my demo test app above using flash 9 as the demo is set to require 10, but in other test apps I am getting the same broken behaviour with mouse events being lost after loading the FileReference browser. Looks like we will need to put a warning for Snow Leopard users on our flash apps.</p>
<p><strong>Update 22/11/09</strong></p>
<p>Adobe Labs have recently released Flash Player 10.1, which has fixed the issue detailed above. I have tried out the new player, but while the problems above have been fixed, many others new problems are present. For example, it appears that key events are duplicated, every keystroke entered appears twice and filtering by file type does not appear to work with FileReference. I realise this is a pre-release version constantly being improved, so hopefully these will be fixed soon. Unfortunately there is no debugger version of the new player, which is quite strange since the people who are going most likely to try out a pre-release are developers who would require the debugging features.</p>
<p>So far it appears I am the only one that has experienced these problems with the latest player, I think this is somehow related to my system being 32 bit only. It appears that most of the development efforts into Flash on the Mac are on 64 bit support, which makes sense since Apple is shifting eveything to 64 bit. If you too try out the new beta flash player and experience other bugs please report them here and on the adobe bug reporting site. This new flash player update proves that an active Flash community can get flash bugs fixed quicker and help improve the flash platform for all. Thanks to all who commented on their issues experienced with the demo.</p>
<p>You can try out the new 10.1 player at the <a title="Adobe Labs" href="http://labs.adobe.com/technologies/flashplayer10/" target="_blank">Adobe Labs site</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2009/09/bug-in-flash-player-filereference-browse-affecting-macs/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

