<?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>Oliver Smith</title>
	<atom:link href="http://chemicaloliver.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://chemicaloliver.net</link>
	<description>experimentation and geekiness</description>
	<lastBuildDate>Sat, 30 Mar 2013 19:54:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Fixing Email Addresses in Git Repos after migration from Mercurial using Fast Export</title>
		<link>http://chemicaloliver.net/linux/fixing-email-addresses-in-git-repos-after-migration-from-mercurial-using-fast-export/</link>
		<comments>http://chemicaloliver.net/linux/fixing-email-addresses-in-git-repos-after-migration-from-mercurial-using-fast-export/#comments</comments>
		<pubDate>Sat, 30 Mar 2013 19:36:20 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1471</guid>
		<description><![CDATA[Migrating repos from Mercurial to Git can be achieved by a variety of methods. The best method I&#8217;ve found is to use fast-export (not HgGit), however regardless of the method  they all borked the importing of my email address on commits. In this post I&#8217;ll detail how to fix this. First I performed the conversion as detailed here. After this [...]]]></description>
				<content:encoded><![CDATA[<p>Migrating repos from Mercurial to Git can be achieved by a variety of methods. The best method I&#8217;ve found is to use fast-export (not HgGit), however regardless of the method  they all borked the importing of my email address on commits. In this post I&#8217;ll detail how to fix this.</p>
<p>First I performed the conversion as detailed <a title="Convert Mercurial Repository into Git" href="http://www.lancejian.com/2012/02/05/convert-mercurial-repository-into-git.html">here</a>.</p>
<p>After this all my commits where shown in gitk as <code>devnull@localhost</code> although this only came to my attention when I tried to push to github and got an <code>invalid-email-address error</code>.</p>
<p>This can be easily fixed using the <code>git filter-branch</code> command:</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash

git filter-branch -f --env-filter '

an=&quot;$GIT_AUTHOR_NAME&quot;
am=&quot;$GIT_AUTHOR_EMAIL&quot;
cn=&quot;$GIT_COMMITTER_NAME&quot;
cm=&quot;$GIT_COMMITTER_EMAIL&quot;

# Repeat this for each user / email which needs fixing
if [ &quot;$GIT_AUTHOR_NAME&quot; = &quot;&lt;Name used on commit&gt;&quot; ]
then
    cn=&quot;&lt;Name used on commit&gt;&quot;
    cm=&quot;&lt;New email address&gt;&quot;
    an=&quot;&lt;Name used on commit&gt;&quot;
    am=&quot;&lt;New email address&gt;&quot;
fi

export GIT_AUTHOR_NAME=&quot;$an&quot;
export GIT_AUTHOR_EMAIL=&quot;$am&quot;
export GIT_COMMITTER_NAME=&quot;$cn&quot;
export GIT_COMMITTER_EMAIL=&quot;$cm&quot;
' -- --all
</pre>
<p>Obviously the placeholders need to be replaced with your values.</p>
<p>This code is based on a <a href="http://stackoverflow.com/questions/6942196/hggit-invalid-email-address-at-github">stackoverflow answer </a>but that only works for the current branch, mine applies to all branches.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/linux/fixing-email-addresses-in-git-repos-after-migration-from-mercurial-using-fast-export/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cropping videos using ffmpeg / libav / avconv</title>
		<link>http://chemicaloliver.net/linux/cropping-videos-using-ffmpeg-libav-avconv/</link>
		<comments>http://chemicaloliver.net/linux/cropping-videos-using-ffmpeg-libav-avconv/#comments</comments>
		<pubDate>Sun, 30 Dec 2012 12:43:19 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1436</guid>
		<description><![CDATA[Explanatory note: Ubuntu (my distro of choice) and others are transitioning from ffmpeg to libav, libav is a fork of ffmpeg and most tools are drop in compatible, the method described in this post should work with recent versions of either, the command line tools ffmpeg and avconv are interchangeable. Old Method Historically ffmpeg had [...]]]></description>
				<content:encoded><![CDATA[<p><em>Explanatory note:</em></p>
<p><em>Ubuntu (my distro of choice) and others are transitioning from ffmpeg to libav, libav is a fork of ffmpeg and most tools are drop in compatible, the method described in this post should work with recent versions of either, the command line tools <code>ffmpeg</code> and <code>avconv</code> are interchangeable.</em></p>
<h2>Old Method</h2>
<p>Historically ffmpeg had <code>-croptop</code>, <code>-cropleft</code> etc. parameters used for cropping videos, these have now been replaced by the <code>-vf</code> or video filter option which is a little more complex.</p>
<h2>Current Method</h2>
<p>The <code>-vf</code> option can be used to specify a section of the source video to use in the output by specifying the size and position of a rectangle to crop to:</p>
<p>The <code>-vf</code> option takes the argument <code>crop=out_w:out_h:x:y</code> - to create a new video file <code>output.mpeg</code> cropped to 720px x 600px and aligned 240px from the top:</p>
<pre class="brush: bash; title: ; notranslate">
avconv -i input.webm -vf crop=720:600:0:240 output.mpeg
</pre>
<p>In the example I&#8217;m also converting a webm video to mpeg along with cropping it, to convert webm to mpeg at the same dimensions just remove the cropping options.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/linux/cropping-videos-using-ffmpeg-libav-avconv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Atomic Counters using Mongodb&#8217;s findAndModify with PHP</title>
		<link>http://chemicaloliver.net/programming/atomic-counters-using-mongodbs-findandmodify-with-php/</link>
		<comments>http://chemicaloliver.net/programming/atomic-counters-using-mongodbs-findandmodify-with-php/#comments</comments>
		<pubDate>Sun, 08 Jul 2012 21:42:15 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1400</guid>
		<description><![CDATA[A common problem when developing web applications is the ability to generate unique sequential numbers, my recent use case was an API which generated an order number on receiving an order but before the order had been stored in a database, normally I would use MySQL auto incrementing keys but I needed to send the order number back to [...]]]></description>
				<content:encoded><![CDATA[<p>A common problem when developing web applications is the ability to generate unique sequential numbers, my recent use case was an API which generated an order number on receiving an order but before the order had been stored in a database, normally I would use MySQL auto incrementing keys but I needed to send the order number back to the client long before the order was stored, as Mongodb was already in the application stack it seemed the natural place to generate a persistent source of sequential numbers.</p>
<p>MongoDB provides a useful function <code>findAndModify</code> which, while MongoDB doesn&#8217;t support transactions, allows a value to be retrieved and updated atomically, making it ideal for this situation where I wanted the order number to increment by one every time it was used.</p>
<p>For those who&#8217;re in the dark about why you&#8217;d need to retrieve and update a value in one operation, the advantage of using this over separate find and updates is that it avoids the race condition where the same value could be retrieved twice if another client attempted to retrieve the counter before the update has taken place.</p>
<p><span id="more-1400"></span></p>
<h2>Example</h2>
<p>The findAndModify command is no use without something to find so I started with this document (in a collection named &#8216;Counters&#8217;:</p>
<pre class="brush: jscript; title: ; notranslate">
db.Counters.save({name : &quot;order_no&quot;, value : 1});
</pre>
<p>Now we have a starting point we can go onto retrieving and modifying the value, from the MongoDB command line client this be done quite simply:</p>
<pre class="brush: jscript; title: ; notranslate">
db.Counters.findAndModify({query : { name : &quot;order_no&quot;}, update : { $inc : {value : 1}}})
</pre>
<p>The first query being the find section and the second the update, here I&#8217;ve used the <code>$inc</code> operator to increment the value by the specified number. By default this query returns the current value then updates it, if <code>new : true.</code></p>
<p>As expected this query then returns the document and updates the value each time:</p>
<pre class="brush: jscript; title: ; notranslate">
{
	&quot;_id&quot; : ObjectId(&quot;4ff9f8d43ddba5fc637aade3&quot;),
	&quot;name&quot; : &quot;order_no&quot;,
	&quot;value&quot; : 1
}
</pre>
<p>then next time&#8230;.:</p>
<pre class="brush: jscript; title: ; notranslate">
{
	&quot;_id&quot; : ObjectId(&quot;4ff9f8d43ddba5fc637aade3&quot;),
	&quot;name&quot; : &quot;order_no&quot;,
	&quot;value&quot; : 2
}
</pre>
<p>etc&#8230;.</p>
<p>In the PHP driver found in the PECL repositories this command is currently not supported so must be executed using the <code>execute()</code> command, in my case I&#8217;m using this inside a Silex based app with the Doctrine Mongo abstraction layer (not ODM) so the syntax may be slightly different to the raw PHP Mongo library:</p>
<pre class="brush: php; title: ; notranslate">
$record = $database-&gt;execute(
	'db.Counters.findAndModify({
		query : { name : &quot;'.$counter_name.'&quot;},
		 update : { $inc : {value : 1}}
		})'
);
</pre>
<p>There is currently an open bug report on the MongoDB PHP client for findAndUpdate support: <a href="https://jira.mongodb.org/browse/PHP-117">https://jira.mongodb.org/browse/PHP-117</a></p>
<h2>Limitations</h2>
<p>One thing to be careful of, which is applicable to auto incrementing in most databases, is to ensure any replication is correctly configured to ensure there is no chance of using an old value after the value has been updated elsewhere.</p>
<h2></h2>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/programming/atomic-counters-using-mongodbs-findandmodify-with-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Styling the HTML5 Meter Tag Using the Shadow DOM</title>
		<link>http://chemicaloliver.net/internet/styling-the-html5-meter-tag-using-the-shadow-dom/</link>
		<comments>http://chemicaloliver.net/internet/styling-the-html5-meter-tag-using-the-shadow-dom/#comments</comments>
		<pubDate>Sat, 19 May 2012 21:27:57 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1380</guid>
		<description><![CDATA[Almost three years ago I read an article on html5doctor describing the meter tag, during the course of the article an example is given of the http://www.justgiving.com/  gauge of how near you are to your target: The article goes on to suggest this would be an ideal use case for the &#60;meter&#62; tag, however at that [...]]]></description>
				<content:encoded><![CDATA[<p>Almost three years ago I read an <a href="http://html5doctor.com/measure-up-with-the-meter-tag/">article on html5doctor </a>describing the meter tag, during the course of the article an example is given of the <a href="http://www.justgiving.com/">http://www.justgiving.com/</a>  gauge of how near you are to your target:</p>
<p style="text-align: center;"><a href="http://chemicaloliver.net/wordpress/wp-content/uploads/2012/05/just-giving-example.jpg"><img class="size-medium wp-image-1381 aligncenter" title="just-giving-example" src="http://chemicaloliver.net/wordpress/wp-content/uploads/2012/05/just-giving-example-300x100.jpg" alt="" width="300" height="100" /></a></p>
<p>The article goes on to suggest this would be an ideal use case for the &lt;meter&gt; tag, however at that time it was impossible to style the meter tag to look like this. This afternoon after discovering that shadow DOM support had made it into Chrome stable I attempted to recreate the Just Giving meter using no images and without a mess of divs.</p>
<p>This is the result, one I&#8217;m pretty happy with, I haven&#8217;t really made much attempt to style the surrounding areas, I just wanted to concentrate on the (frankly rather cool) meter:</p>
<p style="text-align: center;"><a href="http://chemicaloliver.net/code/shad"><img class="size-medium wp-image-1385 aligncenter" title="Justgiving css example" src="http://chemicaloliver.net/wordpress/wp-content/uploads/2012/05/Justgiving-css-300x112.png" alt="" width="300" height="112" /></a></p>
<p>Or see a <a href="http://chemicaloliver.net/code/shad">live demo</a>.</p>
<p><span id="more-1380"></span></p>
<p>My CSS version uses the shadow DOM to remove the default styles from the meter, colour it blue then uses a SVG mask to make the circular centre meter. The surrounding border is an SVG star rendered as the background of the element.</p>
<h3>Code</h3>
<p>Full code can be seen in <a href="https://gist.github.com/2732448">this GIST</a>.</p>
<p>The code used is quite simple:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;html&gt;
&lt;head&gt;
	&lt;style type=&quot;text/css&quot;&gt;

	/**
	 *
	 * General Page CSS Omitted
	 *
	 */

	/* The main meter element and star */
	#raised {
		-webit-style:none;
		position: relative;
		background-image:url(star.svg);
		height:150px;
		width:150px;
		position: absolute;
		right:20px;
	}

	/* The overlay shadow */
	#raised:before {
		content: '';
		background-color:#fff;
		opacity: 0.2;
		height:60;
		width:115;
		position: absolute;
		top:0;
		left:17.5px;
		z-index:5;
		margin: 16px 0 0 0;
		border-radius: 100px;
		border-bottom-right-radius: 10px;
		border-bottom-left-radius: 10px;

	}

	/* The percentage */
	#raised:after {
		content:  &quot;60%&quot;;
		position: absolute;
		top:65px;
		right:36%;
		font-family: Arial, sans-serif;
		font-weight:bold;
		color: #fff;
	}

	/**
	 * Shadow Dom Elements
	 */

	/* The central circle */
	#raised::-webkit-meter-bar {
		-webkit-transform: rotate(270deg);
		-webkit-mask-image: url(circle.svg);
		background:none;
		margin: 25px 0 00px 25;
		width:100px;
		height:100px;

	}

	/* The area of the meter indicating the value */
	#raised::-webkit-meter-optimum-value {
		background:#134A6F;
	}

	&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
	&lt;article&gt;

		&lt;div class=&quot;column left&quot;&gt;
			&lt;dl&gt;
				&lt;dt&gt;Target:&lt;/dt&gt;
				&lt;dd&gt;&amp;pound;200&lt;/dd&gt;
				&lt;dt&gt;Raised so far:&lt;/dt&gt;
				&lt;dd&gt;&amp;pound;120&lt;/dd&gt;
			&lt;/dl&gt;
		&lt;/div&gt;
		&lt;div class=&quot;column right&quot;&gt;
			&lt;meter id=&quot;raised&quot; min=&quot;0&quot; max=&quot;200&quot; value=&quot;120&quot; title=&quot;pounds&quot;&gt;60%&lt;/meter&gt;
		&lt;/div&gt;
	&lt;/article&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>SVG files were generated using inkscape and can be seen in the <a href="http://chemicaloliver.net/code/shad">live demo</a>.</p>
<h3>Limitations</h3>
<ul>
<li>The percentage cannot currently be calculated using CSS, calc() doesn&#8217;t work using two attr() values.</li>
<li>This example requires a recent version of Google Chrome</li>
<li>In this example I&#8217;ve modified the mark-up from the original example to just use one meter tag but preserved the &lt;dl&gt;</li>
</ul>
<p>While this approach would not be suitable for use in a live site yet without a great deal of graceful degradation it is a very good example of the power of HTML5 &amp; CSS3 and shows the ability they have to combine semantic mark-up and amazing presentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/internet/styling-the-html5-meter-tag-using-the-shadow-dom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inspecting the Shadow DOM using Google Chrome</title>
		<link>http://chemicaloliver.net/programming/inspecting-the-shadow-dom-in-google-chrome-inspector/</link>
		<comments>http://chemicaloliver.net/programming/inspecting-the-shadow-dom-in-google-chrome-inspector/#comments</comments>
		<pubDate>Sat, 19 May 2012 14:27:26 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[html5]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1370</guid>
		<description><![CDATA[Over the past year or so there has been much hype surrounding the shadow DOM and the new options it allows for styling standard elements such as &#60;input&#62; or &#60;progress&#62;, it can be used with any element which consists of markup generated by the browser but normally hidden from view. Developers can also create shadow [...]]]></description>
				<content:encoded><![CDATA[<p>Over the past year or so there has been <a href="http://glazkov.com/2011/01/14/what-the-heck-is-shadow-dom/">much hype</a> surrounding the <a href="https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html">shadow DOM</a> and the new options it allows for styling standard elements such as &lt;input&gt; or &lt;progress&gt;, it can be used with any element which consists of markup generated by the browser but normally hidden from view. Developers can also create shadow DOM elements too.</p>
<p>Until recently even where there is support for styling shadow DOM elements it has been difficult to determine what is available, however the webkit inspector in Google Chrome now has support for viewing and rendering the shadow DOM however I&#8217;ve found documentation on this very thin on the ground. I&#8217;ve tested this in the current stable branch (v19.0) on Linux, I believe the same functionality should be present across other platforms too.</p>
<h2>UPDATE</h2>
<p><em><strong>Now Chrome has matured this feature has matured and it is no longer an experiment, step 1 and 2 can be ignored on recent versions and the option to show the Shadow DOM can be found in the general inspector options.</strong></em></p>
<p>To enable support and to inspect shadow DOM elements :</p>
<ol>
<li>Enable the Shadow DOM and Developer Tools experiments in <a href="chrome://flags">chrome://flags</a></li>
<li>Enable the Shadow DOM in the Inspector settings, this panel can be accessed by the cog on the bottom right of the inspector.</li>
<li>Check the &#8216;show shadow DOM&#8217; checkbox.</li>
<li>Restart your browser</li>
</ol>
<p>Elements which have shadow DOM elements will now be displayed in the elements tab:</p>
<p style="text-align: center;"><a href="http://chemicaloliver.net/wordpress/wp-content/uploads/2012/05/shadow-dom.png"><img class="size-full wp-image-1371 aligncenter" title="shadow-dom" alt="" src="http://chemicaloliver.net/wordpress/wp-content/uploads/2012/05/shadow-dom.png" width="237" height="228" /></a></p>
<p>This functionality now makes it possible to explore the different style options without having to take wild stabs in the dark or examine the webkit source code.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/programming/inspecting-the-shadow-dom-in-google-chrome-inspector/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using PHP &#8211; Resque with Silex and the Symfony2 Classloader</title>
		<link>http://chemicaloliver.net/programming/using-php-resque-with-silex-and-the-symfony2-classloader/</link>
		<comments>http://chemicaloliver.net/programming/using-php-resque-with-silex-and-the-symfony2-classloader/#comments</comments>
		<pubDate>Fri, 11 May 2012 20:43:59 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[silex]]></category>
		<category><![CDATA[synfony2]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1360</guid>
		<description><![CDATA[Resque is a popular Redis-backed Ruby library for creating and processing background jobs, this is all well and good if you&#8217;re building applications in Ruby but fortunately for those, like me, who prefer PHP there is also a PHP port - PHP-Resque. This post will describe how to use php-resque in conjunction with the Silex micro framework to queue jobs, initially this [...]]]></description>
				<content:encoded><![CDATA[<p><a href="https://github.com/defunkt/resque">Resque</a> is a popular Redis-backed Ruby library for creating and processing background jobs, this is all well and good if you&#8217;re building applications in Ruby but fortunately for those, like me, who prefer PHP there is also a PHP port -<a href="https://github.com/chrisboulton/php-resque"> PHP-Resque</a>. This post will describe how to use php-resque in conjunction with the Silex micro framework to queue jobs, initially this was not immediately obvious to me as php-resque is not namespaced so wouldn&#8217;t work using the default autoloading configuration but fortunately the class naming seems to follow the PEAR convention where the path to the class is defined by the classname (i.e. Resque_Event would be found in Resque/Event.php). The method described here should work for any similar libraries.</p>
<p>One point to note, there is a Symfony2 bundle that could be easily integrated with Silex which aims to integrate php-resque but it&#8217;s a lot more complicated than I require and in most cases is just not necessary.</p>
<p>The key to using php-resque is to use the registerPrefix method of the symfony classloader (included with Silex) which uses PEAR naming conventions load libraries.</p>
<p>To configure php-resque first clone a copy of the library from github into the vendors folder (or wherever you like to store your external libraries):</p>
<pre class="brush: bash; title: ; notranslate">
git clone https://github.com/chrisboulton/php-resque.git vendors/php-resque
</pre>
<p>Now we have the library in place where ever you configure your application, in my case /src/bootstrap.php which is included in the main application file point the Synfony class loader to the place for classes beginning with the prefix Resque, all the relevant php-resque files are in the lib subdirectory:</p>
<pre class="brush: php; title: ; notranslate">
$app['autoloader']-&gt;registerPrefix('Resque', __DIR__ . '/../vendor/php-resque/lib');
$app['autoloader']-&gt;register();
</pre>
<p>It does need to be before the <code>$app['autoloader']-&gt;register();</code></p>
<p>The library can then be used in your application at will without requiring or manually including it:</p>
<pre class="brush: php; title: ; notranslate">
Resque::setBackend('localhost:6379');

$args = array();

Resque::enqueue('default', 'My_Job', $args);
</pre>
<p>Once you get your head around the different options the synfony2 classloader (and the rest of the components) are wonderful.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/programming/using-php-resque-with-silex-and-the-symfony2-classloader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTTP Basic Auth in Silex</title>
		<link>http://chemicaloliver.net/programming/http-basic-auth-in-silex/</link>
		<comments>http://chemicaloliver.net/programming/http-basic-auth-in-silex/#comments</comments>
		<pubDate>Mon, 07 May 2012 15:08:37 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[silex]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1345</guid>
		<description><![CDATA[Silex is a great platform for building small web applications and APIs, recently I&#8217;ve been using it to build an API with only a couple of routes. As this API will only be used by a couple of users it made sense to use use HTTP basic auth (over SSL of course). HTTP auth could be [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://silex.sensiolabs.org/">Silex</a> is a great platform for building small web applications and APIs, recently I&#8217;ve been using it to build an API with only a couple of routes. As this API will only be used by a couple of users it made sense to use use HTTP basic auth (over SSL of course). HTTP auth could be left to apache/nginx etc. but that wouldn&#8217;t give me the control I&#8217;d like over the output and authentication so I implemented it in Silex, I hope someone finds this useful:</p>
<p>HTTP basic authentication is very simple and just passes a username and password in the headers, PHP has built in functionality to extract these values which can be used in the Silex before hook to ensure it happens before every request is fulfilled, my example is for an API which returns JSON but it would work equally well for a conventional website:</p>
<pre class="brush: php; title: ; notranslate">
$app-&gt;before(function() use ($app)
{
    if (!isset($_SERVER['PHP_AUTH_USER']))
    {
        header('WWW-Authenticate: Basic realm=&quot;&lt;website name&gt;&quot;');
        return $app-&gt;json(array('Message' =&gt; 'Not Authorised'), 401);
    }
    else
    {
        //once the user has provided some details, check them
        $users = array(
            'workflow' =&gt; 'password'
            );

        if($users[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW'])
        {
            //If the password for this user is not correct then resond as such
            return $app-&gt;json(array('Message' =&gt; 'Forbidden'), 403);
        }

        //If everything is fine then the application will carry on as normal
    }
});

</pre>
<p>Full details of implementing HTTP auth in PHP can be found in the <a href="http://php.net/manual/en/features.http-auth.php">PHP manual</a>, this includes how to implement HTTP digest auth.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/programming/http-basic-auth-in-silex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JSON Error / Exception Messages in Codeigniter 2.0+</title>
		<link>http://chemicaloliver.net/programming/json-error-exception-messages-in-codeigniter-2-0/</link>
		<comments>http://chemicaloliver.net/programming/json-error-exception-messages-in-codeigniter-2-0/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 18:39:23 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1325</guid>
		<description><![CDATA[The Problem One of my recent projects required me to build a quick JSON only API to abstract interaction with multiple databases for multiple web applications, as I&#8217;d already got some of the logic in Codeigniter I just added Phil Sturgeon&#8217;s Codeigniter REST Library. However while this handles all method not found errors when URL routing gets as [...]]]></description>
				<content:encoded><![CDATA[<h2>The Problem</h2>
<p>One of my recent projects required me to build a quick JSON only API to abstract interaction with multiple databases for multiple web applications, as I&#8217;d already got some of the logic in Codeigniter I just added <a href="http://philsturgeon.co.uk/blog/2009/06/REST-implementation-for-CodeIgniter">Phil Sturgeon&#8217;s Codeigniter REST Library</a>. However while this handles all method not found errors when URL routing gets as far as the controller all other errors still appear as HTML, when using curl and attempting to parse as JSON this isn&#8217;t helpful.</p>
<h2>The Solution</h2>
<p>My solution was to extend the core Exceptions class which normally deals with these errors. This is done by creating MY_Exceptions.php in application/core and using something similar to the code below:<br />
<span id="more-1325"></span></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Extending the default errors to always give JSON errors
 *
 * @author Oliver Smith
 */

class MY_Exceptions extends CI_Exceptions
{
	function __construct()
	{
		parent::__construct();
	}

	/**
	 * 404 Page Not Found Handler
	 *
	 * @param	string	the page
	 * @param 	bool	log error yes/no
	 * @return	string
	 */
	function show_404($page = '', $log_error = TRUE)
	{
		// By default we log this, but allow a dev to skip it
		if ($log_error)
		{
			log_message('error', '404 Page Not Found --&gt; '.$page);
		}

		header('Cache-Control: no-cache, must-revalidate');
		header('Content-type: application/json');
		header(&quot;HTTP/1.1 404 Not Found&quot;);

		echo json_encode(
			array(
				'status' =&gt; FALSE,
				'error' =&gt; 'Unknown method',
			)
		);

		exit;
	}

	/**
	 * General Error Page
	 *
	 * This function takes an error message as input
	 * (either as a string or an array) and displays
	 * it using the specified template.
	 *
	 * @access	private
	 * @param	string	the heading
	 * @param	string	the message
	 * @param	string	the template name
	 * @param 	int		the status code
	 * @return	string
	 */
	function show_error($heading, $message, $template = 'error_general', $status_code = 500)
	{
		header('Cache-Control: no-cache, must-revalidate');
		header('Content-type: application/json');
		header(&quot;HTTP/1.1 500 Internal Server Error&quot;);

		echo json_encode(
			array(
				'status' =&gt; FALSE,
				'error' =&gt; 'Internal Server Error',
			)
		);

		exit;
	}

	/**
	 * Native PHP error handler
	 *
	 * @access	private
	 * @param	string	the error severity
	 * @param	string	the error string
	 * @param	string	the error filepath
	 * @param	string	the error line number
	 * @return	string
	 */
	function show_php_error($severity, $message, $filepath, $line)
	{
		header('Cache-Control: no-cache, must-revalidate');
		header('Content-type: application/json');
		header(&quot;HTTP/1.1 500 Internal Server Error&quot;);

		echo json_encode(
			array(
				'status' =&gt; FALSE,
				'error' =&gt; 'Internal Server Error',
			)
		);

		exit;
	}
}

?&gt;
</pre>
<h2>Limitations</h2>
<p>This code will only output a basic JSON formatted message, it will not vary the http error codes based on conditions, nor will it take into account accepts headers or similar. Right now this class meets my needs but features such as other formats could easily be added.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/programming/json-error-exception-messages-in-codeigniter-2-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Codeigniter Conference 2012 &#8211; Post Conference Thoughts</title>
		<link>http://chemicaloliver.net/events-2/codeigniter-conference-2012-post-conference-thoughts/</link>
		<comments>http://chemicaloliver.net/events-2/codeigniter-conference-2012-post-conference-thoughts/#comments</comments>
		<pubDate>Sun, 19 Feb 2012 21:38:11 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[ciconf]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1299</guid>
		<description><![CDATA[Having not even got back home yet after the 2012 Codeigniter Conference I thought I&#8217;d write up some of my highlights of a very enjoyable weekend: Testing - John Crepizzi Testing is something I&#8217;ve been working on recently using Simpletest, Jenkins and Codeigniter with moderate success, it was great to see that PHPUnit can be [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-medium wp-image-1305 aligncenter" title="CIConf 2012 Logo" src="http://chemicaloliver.net/wordpress/wp-content/uploads/2012/02/Screenshot-at-2012-02-19-2130531-300x100.png" alt="" width="300" height="100" /></p>
<p>Having not even got back home yet after the 2012 Codeigniter Conference I thought I&#8217;d write up some of my highlights of a very enjoyable weekend:</p>
<p><span id="more-1299"></span></p>
<h3>Testing -<a href="http://twitter.com/seejohnrun" target="_blank"> John Crepizzi</a></h3>
<p>Testing is something I&#8217;ve been working on recently using Simpletest, Jenkins and Codeigniter with moderate success, it was great to see that PHPUnit can be used quite simply, however as full PHPUnit support is planned to be integrated into the core of Codeigniter in v3.0 I think I&#8217;ll wait before transitioning away from Simpletest.</p>
<h3>API Driven Development &#8211; <a href="http://nickjackson.me/" target="_blank">Nick Jackson</a></h3>
<p>Nick did a great job of reassuring me I&#8217;m doing things right. I&#8217;ve just started a project where I&#8217;m essentially building the API first as I know there are going to be multiple apps on top of it. I&#8217;ll definitely be refining my existing API based on some of the tips given regarding response format.</p>
<h3>MongoDB &#8211; <a href="http://www.httpster.org/" target="_blank">Alex Bilbie</a></h3>
<p>MongoDB is something I&#8217;ve played with but not used in anger yet, I&#8217;m not sure I have a use case for it yet but this talk did demonstrate some very cool edge case functions for searching using latitude and longitude where MongoDB will do all the heavy lifting when trying to do box searches on a 3d spherical earth model.</p>
<h3>Who needs Ruby when you&#8217;ve got CodeIgniter? &#8211; <a href="http://twitter.com/jamierumbelow">Jamie Rumbelow</a></h3>
<p>Jamie Rumberlow&#8217;s talk was very practical and showed some very good ideas of how Codeigniter could be improved but demonstrated some really hacky ways to make it work the way he wanted (putting non POST data for validation into $_POST anyone&#8230;.). Really I&#8217;d have preferred to see him talk about how the framework could be improved to fit in with the best practice. That&#8217;s the kind of thing that gives Codeigniter a bad reputation amongst many PHP developers.</p>
<h3>Live coding  - <a href="http://twitter.com/philsturgeon" target="_blank">Phil Sturgeon</a></h3>
<p>Phil demoed setting up a pyrocms site on pagodabox, while I didn&#8217;t find the actual demo very relevant to me, I do like is looking into others coding setups, this lead me to spend half of the next talk configuring my bash prompt and colour scheme to show mercurial or git branch names!</p>
<h3>Other Talks</h3>
<p>All of the talks were very engaging but some not as interesting to me personally, in particular <a href="http://twitter.com/WanWizard" target="_blank">Harro Verton </a>was a great speaker on ORMs but it just further confirmed my views that I&#8217;m not a fan.</p>
<h3>General Thoughts</h3>
<p>Generally the conference went well, the venue was pretty good (apart from the food) and the wifi nothing short of amazing. Oh and the best bit &#8211; a FREE t shirt!</p>
<p>I&#8217;m always in a bit of a love hate relationship with Codeigniter, it&#8217;s very quick and easy but its slightly old fashioned way of doing things and lack of modern features like auto-loading, namespacing and dependency injection make it annoy me and cause me to look at things like Symfony2 or Silex.</p>
<p>Still a great weekend away and thanks to all who were responsible for the planning and organisation. I&#8217;ll hopefully be back next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/events-2/codeigniter-conference-2012-post-conference-thoughts/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Streaming audio from Ubuntu Linux to a DLNA player (Blu Ray or PS3) using Rygel</title>
		<link>http://chemicaloliver.net/linux/streaming-audio-from-ubuntu-linux-to-a-dlna-player-blu-ray-or-ps3/</link>
		<comments>http://chemicaloliver.net/linux/streaming-audio-from-ubuntu-linux-to-a-dlna-player-blu-ray-or-ps3/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 20:15:43 +0000</pubDate>
		<dc:creator>Oliver</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://chemicaloliver.net/?p=1277</guid>
		<description><![CDATA[This project started out of researching how to play sound from spotify or rhythmbox from my laptop running ubuntu 11.10 through my hifi. Initially I set out to see if an airport express would work using raop and pulseaudio but it seems that support for the new 802.11 version is flakey so I didn&#8217;t wish to invest £80 [...]]]></description>
				<content:encoded><![CDATA[<p>This project started out of researching how to play sound from spotify or rhythmbox from my laptop running ubuntu 11.10 through my hifi. Initially I set out to see if an airport express would work using raop and pulseaudio but it seems that support for the new 802.11 version is flakey so I didn&#8217;t wish to invest £80 in a device that might not work. During my research I found that DLNA supported streaming, DLNA is a protocol commonly used for sharing media files with devices such as networked dvd players, internet tvs and consoles like the ps3 so I explored further.</p>
<p>DLNA is supported in Ubuntu (and other modern linux distros) by <a href="http://live.gnome.org/Rygel">Rygel</a>, part of the Gnome project. Rygel provides a DLNA server which also has the capability to capture a pulseaudio sink (an input or output stream) and stream it to a DLNA enabled device.</p>
<p>Below are the steps I took to enable me to stream audio from my computer to my Sony BDP-S370, they should be applicable to any similar device:<br />
<span id="more-1277"></span></p>
<ol>
<li>Install required packages:
<pre class="brush: bash; title: ; notranslate">sudo apt-get install rygel rygel-gst-launch wavpack</pre>
</li>
<li>Find the name of the pulseaudio sink which you wish to capture:<br />
To list the choices, use:
<pre class="brush: bash; title: ; notranslate">pacmd list-sinks</pre>
<p>Then make a note of the name attribute (minus surrounding brackets), in my case it was:</p>
<pre class="brush: bash; title: ; notranslate">alsa_output.usb-C-Media_INC._USB_Sound_Device-00-Device.analog-stereo</pre>
<p>I found that adding .monitor to this was required for the next stage, this can be achieved in one command:</p>
<pre class="brush: bash; title: ; notranslate">pactl list | egrep -A2 '^(\*\*\* )?Source #' | grep 'Name: .*\.monitor$' | awk '{print $NF}' | tail -n1</pre>
</li>
<li>Edit /etc/rygel.conf:<br />
Replace (or comment out)</p>
<pre class="brush: bash; title: ; notranslate">
[GstLaunch]
enabled=true
launch-items=audiotestsrc;videotestsrc;videotestoverlay
audiotestsrc-title=Audiotestsrc
audiotestsrc-mime=audio/x-wav
audiotestsrc-launch=audiotestsrc ! wavenc
videotestsrc-title=Videotestsrc
videotestsrc-mime=video/mpeg
videotestsrc-launch=videotestsrc ! ffenc_mpeg2video ! mpegtsmux
videotestoverlay-title=Videotestsrc with timeoverlay 2
videotestoverlay-mime=video/mpeg
videotestoverlay-launch=videotestsrc ! timeoverlay ! ffenc_mpeg2video ! mpegtsmux
</pre>
<p>with</p>
<pre class="brush: bash; title: ; notranslate">[GstLaunch]
enabled=true
launch-items=mypulseaudiosink
mypulseaudiosink-title=Audio on @HOSTNAME@
mypulseaudiosink-mime=audio/x-wav
mypulseaudiosink-launch=pulsesrc device=alsa_output.usb-C-Media_INC._USB_Sound_Device-00-Device.analog-stereo.monitor ! wavpackenc
</pre>
<p>replacing the device on the last line with the output from the previous stage.</li>
<li>Start Rygel (type rygel in the terminal)</li>
<li>Connect your player to the DLNA device which should have appeared (probably as GstLaunch) and you should hear any audio played on your computer through your DLNA device.</li>
<li>If you wish (I don&#8217;t) add rygel to run at startup.</li>
</ol>
<div><em>This worked perfectly for my desktop but for my laptop I had to fiddle with which hardware output of the soundcard was being used under the standard gnome sound settings, changing the profile for the selected device to an option with no output (ie input only or disabled).</em></div>
<h2>Alternatives</h2>
<ul>
<li>If you just wish to share audio and video files then something like <a href="http://mediatomb.cc/">mediatomb</a> with be much more simple (although rygel also shares files).</li>
<li>There is meant to be a simpler way to link rygel and pulseaudio where everything works out of the box and rygel appears as a separate audio out but it&#8217;s currently broken with the supplied pulseaudio/rygel combination in ubuntu.</li>
</ul>
<h2>Acknowledgements</h2>
<p>I figured all this out with the help of these guys:</p>
<ul>
<li><a href="http://ubuntuforums.org/showpost.php?p=11010331&amp;postcount=4">http://ubuntuforums.org/showpost.php?p=11010331&amp;postcount=4</a></li>
<li><a href="http://www.outflux.net/blog/archives/2009/04/19/recording-from-pulseaudio/">http://www.outflux.net/blog/archives/2009/04/19/recording-from-pulseaudio/</a></li>
<li><a href="http://mpd.wikia.com/wiki/PulseAudio">http://mpd.wikia.com/wiki/PulseAudio</a></li>
</ul>
<p>Thanks.</p>
]]></content:encoded>
			<wfw:commentRss>http://chemicaloliver.net/linux/streaming-audio-from-ubuntu-linux-to-a-dlna-player-blu-ray-or-ps3/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
