<?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>Geekality &#187; Software Development</title>
	<atom:link href="http://www.geekality.net/category/software-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:15:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JavaScript: Uppercase first letter in a string</title>
		<link>http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/</link>
		<comments>http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 13:21:07 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1052</guid>
		<description><![CDATA[In PHP there is a very handy function called ucfirst which Returns a string with the first character of str capitalized, if that character is alphabetic. Needed that in JavaScript, but discovered there was no such thing. Luckily I quickly &#8230; <a href="http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In PHP there is a very handy function called <a href="http://php.net/ucfirst">ucfirst</a> which </p>
<blockquote><p>Returns a string with the first character of str capitalized, if that character is alphabetic.</p></blockquote>
<p>Needed that in JavaScript, but discovered there was no such thing.</p>
<p><span id="more-1052"></span></p>
<p>Luckily I quickly found <a href="http://stackoverflow.com/questions/1026069/capitalize-first-letter-of-string-in-javascript/1026087#1026087">a function</a> over at StackOverflow that I adjusted slightly and added to the string class:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">String.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">ucfirst</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Can be used like so:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'some text'</span>.<span style="color: #660066;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Alerts: Some text</span></div></div>
<h2>Alternative method</h2>
<p>In some cases it might be good to handle this in CSS instead though. For example to uppercase the first letter in all list items, you can do this:</p>
<div class="codecolorer-container css default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">li<span style="color: #3333ff;">:first-letter<br />
</span><span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">text-transform</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">uppercase</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></div>
<p>Weeee <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='^_^' title='^_^' class='wp-smiley smiley-9' /> Ok, back to work&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/06/30/javascript-uppercase-first-letter-in-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: How to easily provide JSON and JSONP</title>
		<link>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/</link>
		<comments>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 18:03:28 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[JSONP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1021</guid>
		<description><![CDATA[Have some server-side data that you would like to grab through an AJAX call? For example by using the jQuery.ajax method? A really easy way of doing this is by using the JSON format. JSON It is hyper simple. All &#8230; <a href="http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Have some server-side data that you would like to grab through an AJAX call? For example by using the <a href="http://api.jquery.com/jQuery.ajax/">jQuery.ajax</a> method?</p>
<p>A really easy way of doing this is by using the <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> format.</p>
<p><span id="more-1021"></span></p>
<h2>JSON</h2>
<p>It is hyper simple. All you need to do is to set the content-type to application/json and encode your data using the <a href="http://php.net/json_encode">json_encode</a> function.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You can test it out for example in the <a href="http://getfirebug.com/">FireBug</a> console by running this line:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data.php'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Response: [1,2,3,4,5,6,7,8,9]</span></div></div>
<p>You should see the request being done in the console and also in the Net tab. If it didn&#8217;t work, you might be subject to the following:</p>
<blockquote><p>Due to browser security restrictions, most &#8220;Ajax&#8221; requests are subject to the <a title="Same Origin Policy on Wikipedia" href="http://en.wikipedia.org/wiki/Same_origin_policy">same origin policy</a>; the request can not successfully retrieve data from a different domain, subdomain, or protocol.</p></blockquote>
<p>A nice and simple solution to that problem is JSONP.</p>
<h2>JSONP</h2>
<p>As stated on <a href="http://en.wikipedia.org/wiki/JSON#JSONP">Wikipedia</a>,</p>
<blockquote><p>JSONP or &#8220;JSON with padding&#8221; is a complement to the base JSON data format, a usage pattern that allows a page to request and more meaningfully use JSON from a server other than the primary server.</p></blockquote>
<p>I&#8217;m not sure I get everything about JSONP, but I have used it and I know that  it works <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> It&#8217;s almost just as easy as plain JSON actually, and all that&#8217;s needed is to wrap the JSON encoded data in a callback function provided as a GET parameter.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'('</span><span style="color: #339933;">.</span><span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">')'</span><span style="color: #339933;">;</span></div></div>
<p>You can test it by running</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'data.php'</span><span style="color: #339933;">,</span> dataType<span style="color: #339933;">:</span><span style="color: #3366CC;">'jsonp'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #006600; font-style: italic;">// Response: jsonp1277656587731([1,2,3,4,5,6,7,8,9])</span></div></div>
<p>This time you won&#8217;t see the call in the FireBug console though, but only in the Net tab. That&#8217;s because the data is loaded in a script tag instead of through an actual AJAX call. Thankfully jQuery handles all of that though, so you don&#8217;t have to worry about it at all. You&#8217;ll get the same JSON data in your response handler, ready to be used however you want to <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>What if you&#8217;d like to have both options?</p>
<h2>JSON and JSONP</h2>
<p>Providing both is easy as cake. Just check if the callback parameter is set or not:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content-type: application/json; charset=utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'callback'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; ? <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$_GET['callback']}</span>(<span style="color: #006699; font-weight: bold;">$json</span>)&quot;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">:</span> <span style="color: #000088;">$json</span><span style="color: #339933;">;</span></div></div>
<p>That&#8217;s all! Please comment if you have any feedback <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP: Generating transparent PNG fillers</title>
		<link>http://www.geekality.net/2010/06/13/php-generating-transparent-png-fillers/</link>
		<comments>http://www.geekality.net/2010/06/13/php-generating-transparent-png-fillers/#comments</comments>
		<pubDate>Sun, 13 Jun 2010 13:45:38 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[GD]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=1003</guid>
		<description><![CDATA[So, I was fooling around the other day with an HTML table and wanted to make the odd rows slightly darker. Figured I could use for example an 80% transparent black PNG to do that (or could have just assigned &#8230; <a href="http://www.geekality.net/2010/06/13/php-generating-transparent-png-fillers/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So, I was fooling around the other day with an HTML table and wanted to make the odd rows slightly darker. Figured I could use for example an 80% transparent black PNG to do that (or could have just assigned a darker color, but where&#8217;s the fun in that?). Either way, ended up making a little something that I thought I could share just for the fun of it. I learned a few things, so maybe someone else can too <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>So, here&#8217;s how to make a small transparent PNG filler image on-the-fly using PHP <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p><span id="more-1003"></span></p>
<p>I called the script <em>fill.php</em>, and the first line looks like this:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This starts the php script and sets the content-type to image/png which means the web browser will that we are an image and not an html file which usually is the default.</p>
<p>Next up we get the parameters from the user:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span> <span style="color: #339933;">+</span> <span style="color: #990000;">array</span><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'r'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'g'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'b'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Here I create an array with default values and merge it with the <code class="codecolorer php default"><span class="php"><span style="color: #000088;">$_GET</span></span></code> array so that values from the user will override the default values. This means we are sure we have all the values we need and that the user only have to specify the ones he want to.</p>
<p>The r, g and b values should be a number between 0 and 255 and the alpha value should be between 0 and 127. So, next up we need to make sure that that is the case so we don&#8217;t use some other bogus we might have gotten:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990000;">array_walk</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$item</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'a'</span> ? <span style="color: #cc66cc;">127</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>This goes through each parameter and makes sure it is a number between 0 and 255 (or 127 for the alpha).</p>
<p>Since we have what we need now, we can start with creating our image:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$img</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cannot Initialize new GD image stream'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>We also need to set a flag that we want to use full alpha channel information (as opposed to single-color transparency) in this image:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990000;">imagesavealpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>We are now ready to fill our image with the color we are asked for:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocatealpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'g'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'b'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">imagefill</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And finally output the result and destroy the image resource we have been working with:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>And that&#8217;s actually all there is to it!</p>
<p>The complete code looks like this:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">// Replace default parameters with user input</span><br />
<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span> <span style="color: #339933;">+</span> <span style="color: #990000;">array</span><br />
<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'r'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'g'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'b'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Make sure they are within limits</span><br />
<span style="color: #666666; font-style: italic;">// (colors: 0-255, alpha: 0-127)</span><br />
<span style="color: #990000;">array_walk</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'a'</span> ? <span style="color: #cc66cc;">127</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
<br />
<span style="color: #666666; font-style: italic;">// Create image</span><br />
<span style="color: #000088;">$img</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">imagecreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cannot Initialize new GD image stream'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Use full alpha channel information</span><br />
<span style="color: #990000;">imagesavealpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Fill image</span><br />
<span style="color: #000088;">$color</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecolorallocatealpha</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'g'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'b'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <br />
&nbsp; &nbsp; <span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">imagefill</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">// Output result</span><br />
<span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$img</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If you want to check out a working demo version of this, you can test out my little testing tool I created while having fun with this at <a href="http://tools.geekality.net/filling-factory/">tools.geekality.net/filling-factory</a></p>
<p>Let me know what you think! And as usual, please let me know if you see something that is wrong or could be improved or anything like that  <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/06/13/php-generating-transparent-png-fillers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: How to send emails</title>
		<link>http://www.geekality.net/2010/05/16/c-how-to-send-emails/</link>
		<comments>http://www.geekality.net/2010/05/16/c-how-to-send-emails/#comments</comments>
		<pubDate>Sun, 16 May 2010 00:21:02 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Email]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[SSL]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=991</guid>
		<description><![CDATA[Sending a basic email message in a C# application is quite easy thanks to a class called SmptClient. We simply need an address to send to, an address to send from, the message we want to send and the client: &#8230; <a href="http://www.geekality.net/2010/05/16/c-how-to-send-emails/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Sending a basic email message in a C# application is quite easy thanks to a class called <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx">SmptClient</a>.</p>
<p>We simply need an address to send to, an address to send from, the message we want to send and the client:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var <span style="color: #0600FF; font-weight: bold;">from</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;me@example.com&quot;</span>, <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
var to <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;you@example.com&quot;</span>, <span style="color: #666666;">&quot;You&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span>, to<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Subject <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Greetings!&quot;</span>,<br />
&nbsp; &nbsp; Body <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;How are you doing today?&quot;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>SmtpException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>That was pretty simple, wasn&#8217;t it? But what if we need to authenticate with our server? And what if we want to send our message in a more secure manner?</p>
<p><span id="more-991"></span></p>
<h2>Authentication</h2>
<p>You will quite often find that an <a href="http://en.wikipedia.org/wiki/Smtp_server">SMTP server</a> requires you to authenticate yourself before it will let you do anything. This is however very simple to do. All we need, is to provide our credentials to the client:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></div></div>
<p>Now, when you try to send your message like before, it should be sent without problems <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' />  But what if you want to send your email securely?</p>
<h2>Using SSL to encrypt the connection</h2>
<p>To send emails through an encrypted connection, all you have to do is to set the <a href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl.aspx">EnableSSL</a> property of the SmtpClient to true:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; EnableSSL <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></div></div>
<p>When you now try to send your email, it will use the <a href="http://en.wikipedia.org/wiki/Transport_Layer_Security">Secure Socket Layer</a> to encrypt the connection. Like me, you might run into a problem though; an <a href="http://msdn.microsoft.com/en-us/library/system.security.authentication.authenticationexception.aspx">AuthenticationException</a> with the following message: <cite>The remote certificate is invalid according to the validation procedure.</cite></p>
<h2>Imperfect certificates</h2>
<p>After reading about <a href="http://www.verisign.com/ssl/ssl-information-center/how-ssl-security-works/">how these certificates work</a> and some more digging around I found out that the certificate of the SMTP server I use is considered invalid for two reasons: Firstly it was not issued by a trusted Certification Authority (like VeriSign or Thawte), but by my host (<a href="http://www.dreamhost.com/r.cgi?322827">Dreamhost</a>) themselves. And finally there is a mismatch between the domain I&#8217;m connecting to (smtp.example.com) and the domain the certificate was issued to (mail.dreamhost.com).</p>
<h2>Manual certificate validation</h2>
<p>SSL has two purposes: security and authentication. And the security will actually work fine and all the traffic will be encrypted even if the authentication fails. In other words, as long as we can look at the certificate ourselves and be sure that <em>we</em> thrust it, we can go ahead and continue with our work.</p>
<p>To check the certificate ourselves we can provide a <a href="http://msdn.microsoft.com/en-us/library/system.net.security.remotecertificatevalidationcallback.aspx">RemoteCertificateValidationCallback</a> delegate to a class called the <a href="http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.aspx">ServicePointManager</a>.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span><br />
&nbsp; &nbsp; <span style="color: #008000;">=</span> OurCertificateValidation<span style="color: #008000;">;</span></div></div>
<p>But how can we decide if we should trust the certificate or not? Well, I have found three ways that works nicely. Well, actually just two since the first way is to just cover your eyes and let anything pass.</p>
<h3>Pretend validation</h3>
<p>The following implementation is a way to simply say that <cite>I don&#8217;t care</cite>:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>This is of course not very good&#8230; we should at least make <em>some</em> effort and check if the certificate is the one we should be getting. We can for example&#8230;</p>
<h3>Check the certificate fingerprint</h3>
<p>Certificates have something called a fingerprint and if we know the fingerprint of the certificate we <em>should</em> get, then we can compare it with the fingerprint of the one we actually got.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCertHashString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;6B8C79AB966D70277BA86E6F820859A2B5B8CCC0&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// SHA-1 fingerprint</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>If you don&#8217;t think this is enough, you can for example&#8230;</p>
<h3>Check the whole certificate</h3>
<p>If you can export the certificate to a file somehow, you can then later compare the certificate you get with the one you have stored and make sure they are equal.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var actualCertificate <span style="color: #008000;">=</span> X509Certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromCertFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>actualCertificate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>I got a hold of the certificate file by sticking the following snippet inside my validation delegate, ran my code once, and then changed it back to normal again:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var file <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var cert <span style="color: #008000;">=</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Export</span><span style="color: #008000;">&#40;</span>X509ContentType<span style="color: #008000;">.</span><span style="color: #0000FF;">Cert</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; file<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>cert, <span style="color: #FF0000;">0</span>, cert<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>I also successfully exported it from Opera, which I use as an email client, by going to Preferences, Security, Manage Certificates, Approved and then export the one I want. This of course assumes you have have previously approved the certificate for use with Opera.</p>
<h2>Wrap-up</h2>
<p>That&#8217;s all for now! Found it kind of fun to get this working and thought I could share it in case someone else struggles with this. Please comment and let me know if I have misunderstood something or done a big blunder or something like that! I mostly just know that my code is working, so if you know more about how these security issues work and you know something you think i should know as well: Please share! I&#8217;d like to learn <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Here is my final complete code:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Mail</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Authentication</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Cryptography.X509Certificates</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Security</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">namespace</span> Geekality<span style="color: #008000;">.</span><span style="color: #0000FF;">SecureEmail</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">class</span> Program<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> OurCertificateValidation<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var actualCertificate <span style="color: #008000;">=</span> X509Certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateFromCertFile</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;example.com.cert&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> certificate<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>actualCertificate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Register our own certificate validation</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span> <span style="color: #008000;">=</span> OurCertificateValidation<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Message</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var <span style="color: #0600FF; font-weight: bold;">from</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;me@example.com&quot;</span>, <span style="color: #666666;">&quot;Me&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var to <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailAddress<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;you@example.com&quot;</span>, <span style="color: #666666;">&quot;Myself&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span>, to<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Subject <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Greetings!&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Body <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;How are you doing today?&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Create client</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;smtp.example.com&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnableSsl <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Credentials <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NetworkCredential<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;me@example.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Password <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;password&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Try to send</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>client<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Message sent!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>AuthenticationException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>SmtpException e<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadKey</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/05/16/c-how-to-send-emails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kohana PHP framework</title>
		<link>http://www.geekality.net/2010/04/19/kohana-php-framework/</link>
		<comments>http://www.geekality.net/2010/04/19/kohana-php-framework/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 19:59:46 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=956</guid>
		<description><![CDATA[As you might know, I am developing some internal software for LifeStyleTV at the moment. Because of various slightly annoying circumstances I am stuck using PHP and MySQL for this. Not the worst of course, but I must say I &#8230; <a href="http://www.geekality.net/2010/04/19/kohana-php-framework/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.geekality.net/wp-content/uploads/2010/04/Construction-200x300.jpg" alt="" title="Construction" width="200" height="300" class="alignright size-medium wp-image-965" />As you might know, I am developing some internal software for LifeStyleTV at the moment. Because of various slightly annoying circumstances I am stuck using PHP and MySQL for this. Not the worst of course, but I must say I really do prefer C# over PHP for oh so many reasons. But anyways, that wasn&#8217;t the point of this post. The point is that for my life to become hopefully quite a bit easier and hopefully a lot more organized I decided to go looking for a framework I can use as a base for the software I will be developing.</p>
<p>After much looking I finally stumbled over a framework called <a href="http://kohanaphp.com/">Kohana</a>. </p>
<p><span id="more-956"></span></p>
<blockquote><p>Kohana is an open source, <a href="http://wikipedia.org/wiki/Object-Oriented_Programming">object oriented</a> <a href="http://techportal.ibuildings.com/2010/02/22/scaling-web-applications-with-hmvc/">H</a> <a href="http://en.wikipedia.org/wiki/Model_view_controller" title="Model View Controller">MVC</a> <a href="http://wikipedia.org/wiki/Web_Framework">web framework</a> built using <a href="http://php.net/manual/intro-whatis" title="PHP Hypertext Preprocessor">PHP5</a> by a team of volunteers that aims to be swift, secure, and small.</p>
<p>Kohana is licensed under a <a href="http://kohanaphp.com/license">BSD license</a>, so you can legally use it for any kind of open source, commercial, or personal project.</p></blockquote>
<p>Getting started with it was a bit of a hassle because the documentation isn&#8217;t the greatest, but the small community has been helpful and the framework is pretty clear once you grasp the basics. For me it was particularly difficult since I don&#8217;t really have that much experience with MVC frameworks at all. But so far it has been very fun to learn and code with it.</p>
<p>There are currently <a href="http://kerkness.ca/wiki/doku.php?id=what_version_of_kohana_should_i_use">two versions of Kohana</a>, 2 and 3. These are pretty much different frameworks, and I decided to go for version 3 as it seem to be cleaner and support HMVC which <a href="http://techportal.ibuildings.com/2010/02/22/scaling-web-applications-with-hmvc/">seems very interesting</a>.</p>
<p>Here are some very helpful resources that helped me get started:</p>
<ul>
<li><a href="http://dev.kohanaphp.com/projects/kohana3/files">Latest version of Kohana 3</a></li>
<li><a href="http://kerkness.ca/wiki/doku.php">The Unofficial Kohana 3.0 Wiki</a></li>
<li><a href="http://v3.kohanaphp.com/guide">Kohana userguide</a></li>
<li><a href="http://www.dealtaker.com/blog/tag/ko3/">Kohana 3 tutorial</a> at <a href="http://www.dealtaker.com/blog/">Inside DealTaker</a></li>
<li><a href="http://kohana.sher.pl/cs/">Kohana 3 CheatSheet</a></li>
<li>The Kohana <a href="irc://irc.freenode.net/kohana">irc channel</a></li>
</ul>
<p>Alrighty, will blog more about Kohana some other day. Maybe even some tutorials&#8230;? </p>
<p>Are you using any PHP frameworks? What do you use? Please share!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/04/19/kohana-php-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check for duplicates</title>
		<link>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/</link>
		<comments>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 16:57:09 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Extension methods]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=884</guid>
		<description><![CDATA[Say you have an IEnumerable&#60;T&#62; of some sort and you want to check if it contains any duplicates. How do you do that? Terrible solution I needed to do this a while ago and the first solution that hit me &#8230; <a href="http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Say you have an <code class="codecolorer text default"><span class="text">IEnumerable&lt;T&gt;</span></code> of some sort and you want to check if it contains any duplicates. How do you do that?</p>
<p><span id="more-884"></span></p>
<h2>Terrible solution</h2>
<p>I needed to do this a while ago and the first solution that hit me wasn&#8217;t exactly good. Linq has method called Distinct, which returns the distinct items in a sequence (weeds out duplicates). Using that to check if duplicates exists you can do something like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var hasDuplicates <span style="color: #008000;">=</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; subjects<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> subjects<span style="color: #008000;">.</span><span style="color: #0000FF;">Distinct</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>This is of course a <strong><em>really bad</em></strong> solution. So <em>don&#8217;t</em> use it! <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<h2>Good solution</h2>
<p>The much better solution is to use a <code class="codecolorer text default"><span class="text">HashSet&lt;T&gt;</span></code>.</p>
<blockquote><p>The <a href="http://msdn.microsoft.com/en-us/library/bb359438.aspx">HashSet&lt;T&gt;</a> class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.</p></blockquote>
<p>In addition to the fact that it cannot contain duplicate items, it also has a very nice add-method which returns false if the item is already in the set.</p>
<p>To check for duplicates using a hash set we can start out by assuming there is no duplicates and then add items to the hash set until we either run out of items, or we try to add an item that already exists. In the first case our assumption was right and in the second it turned out to be wrong.</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">var set <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HashSet<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
var hasDuplicates <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var s <span style="color: #0600FF; font-weight: bold;">in</span> subjects<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>set<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; hasDuplicates <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">break</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></div>
<h2>Good and handy solution</h2>
<p>Since we don&#8217;t want to type in that stuff over and over again we want this snippet in a nice extension method. Code snippet coming up <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">namespace</span> Geekality<span style="color: #008000;">.</span><span style="color: #0000FF;">Snippets</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> EnumerableExtensions<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> HasDuplicates<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> subjects<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> HasDuplicates<span style="color: #008000;">&#40;</span>subjects, EqualityComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> HasDuplicates<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> subjects, IEqualityComparer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> comparer<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>subjects <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;subjects&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>comparer <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;comparer&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var set <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HashSet<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>comparer<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var s <span style="color: #0600FF; font-weight: bold;">in</span> subjects<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>set<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Simple and handy. You use it for example like this:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>subjects<span style="color: #008000;">.</span><span style="color: #0000FF;">HasDuplicates</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Do something</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p>Hope it can help someone, somewhere, below and/or over the rainbow <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p>Let me know what you think. I&#8217;m always open for improvements and feedback <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt='8)' title='8)' class='wp-smiley smiley-3' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2010/01/19/how-to-check-for-duplicates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test-Driven Development: By Example</title>
		<link>http://www.geekality.net/2009/12/01/test-driven-development-by-example/</link>
		<comments>http://www.geekality.net/2009/12/01/test-driven-development-by-example/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 17:36:16 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Reviews]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=858</guid>
		<description><![CDATA[I earlier wrote about the book, The Art of Unit Testing, which I finished a while ago. That book was very good and was focused on how to write good unit tests. It also mentioned Test-Driven Development, TDD, but not &#8230; <a href="http://www.geekality.net/2009/12/01/test-driven-development-by-example/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.geekality.net/wp-content/uploads/2009/12/Test-Driven-Development-By-Example-Cover-239x300.jpg" alt="Test Driven Development By Example (Cover)" title="Test Driven Development By Example (Cover)" width="239" height="300" class="alignright size-medium wp-image-861" />I earlier wrote about the book, <a href="http://www.geekality.net/2009/11/03/the-art-of-unit-testing/">The Art of Unit Testing</a>, which I finished a while ago. That book was very good and was focused on how to write good unit tests. It also mentioned <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test-Driven Development,</a> TDD, but not too much. The book I read next, which I finished a few days ago, was kind of the other way around. Pretty much only about TDD. And from the title, <em>Test-Driven Development: By Example</em>, that shouldn&#8217;t be much of a shocker <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> </p>
<p><span id="more-858"></span></p>
<p>The book is written by <a href="http://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a> and explains the basics about TDD. It does this pretty well, step by step, by example, just like the title says. The book also talks some about TDD in a more general sense. What it is, how it works, why it works, et cetera.</p>
<p>From the back-cover:</p>
<blockquote><p>Readers will learn to:
<ul>
<li>Solve complicated tasks, beginning with the simple and proceeding to the more complex.</li>
<li>Write automated tests before coding.</li>
<li>Grow a design organically by refactoring to add design decisions one at a time.</li>
<li>Create tests for more complicated logic, including reflection and exceptions.</li>
<li>Use patterns to decide what tests to write.</li>
<li>Create tests using xUnit, the architecture at the heart of many programmer-oriented testing tools.</li>
</ul>
</blockquote>
<p>It pretty much kept the promises <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> The only part I didn&#8217;t really like about this book was that I found much of the coding kind of messy. Could be because the languages used in the examples are Java and Python, which I don&#8217;t really like that much syntax-wise. An even more important reason though, is that I just finished reading The Art of Unit Testing <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':P' title=':P' class='wp-smiley smiley-13' /> So, I suppose I have become a bit demanding when it comes to cleanliness when coding and writing unit-tests. I suppose that is a good thing <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':D' title=':D' class='wp-smiley smiley-8' /> (I do however not claim in any way that the code I currently write is extremely clean in any way! But I do strive for it to be so <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=';)' title=';)' class='wp-smiley smiley-20' /> )</p>
<p>I can really recommend this book to anyone who are curious to get familiar with TDD. I certainly see the value of doing TDD and is in the process of trying to incorporate it into my development habits.</p>
<blockquote><ol>
<li>Red &#8212; Write a little test that doesn&#8217;t work, and perhaps doesn&#8217;t even compile at first.</li>
<li>Green &#8212; Make the test work quicky, committing whatever sins necessary in the process.</li>
<li>Refactor &#8212; Eliminate all of the duplication created in merely getting the test to work.</li>
</ol>
<p>Red/green/refactor &#8212; the TDD mantra.</p></blockquote>
<p>You can get it at <a href="http://amzn.com/0321146530">Amazon</a>, and probably a lot of other places too as it is a pretty well-known book.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2009/12/01/test-driven-development-by-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
