<?xml version="1.0"?>
<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/"
	>
   <channel>
      <pubDate>Fri, 29 Aug 2008 22:32:26 GMT</pubDate>
      <lastBuildDate>Fri, 29 Aug 2008 22:32:26 GMT</lastBuildDate>
      <language>en</language>
      <docs>http://www.rssboard.org/rss-specification</docs>
      <title>CrunchBang ~ code</title>
      <link>http://crunchbang.org/tags/code/</link>
      <description>Code, Design &amp; GNU/Linux</description>

<item>
    <title>Openbox Logout, Reboot &amp; Shutdown Script</title>
    <link>http://crunchbang.org/archives/2008/04/01/openbox-logout-reboot-and-shutdown-script/</link>
    <pubDate>Tue, 01 Apr 2008 19:23:34 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2008/04/01/openbox-logout-reboot-and-shutdown-script/</guid>
    <description><![CDATA[
    <p>I have been using the latest <a href="http://icculus.org/openbox/index.php/Main_Page " title="Openbox - A standards compliant light-weight extensible window manager.">Openbox</a> release [<em>3.4.7-pre2</em>] for the last few weeks and so far I have been impressed. One of the new features from the latest release is a <a href="http://www.gnome.org/projects/gdm/ " title="GDM - The GNOME Display Manager">GDM</a> control script. The script basically allows for a user to send reboot and shutdown signals to GDM from within the Openbox environment. This provides a means to reboot/shutdown an Openbox system in a clean and efficient manner.</p>

<p><img alt="Screenshot of Openbox logout, reboot, shutdown script." src="http://crunchbang.org/uploads/040108135523-openbox-logout-reboot-shutdown.png" /></p>

<p>I have written the following <a href="http://www.pygtk.org/ " title="PyGTK: GTK+ for Python">PyGTK</a> script to take advantage of the new GDM control. Python is not currently a language that I am too familiar with, so please feel free to <strike>rip the script to bits</strike> improve as you see fit.</p>

<pre><code>#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk
import os

class DoTheLogOut:

    # Cancel/exit
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    # Logout
    def logout(self, widget):
        os.system("openbox --exit")

    # Reboot
    def reboot(self, widget):
        os.system("gdm-control --reboot &amp;&amp; openbox --exit")

    # Shutdown
    def shutdown(self, widget):
        os.system("gdm-control --shutdown &amp;&amp; openbox --exit")

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Exit? Choose an option:")
        self.window.set_resizable(False)
        self.window.set_position(1)
        self.window.connect("delete_event", self.delete_event)
        self.window.set_border_width(20)

        # Create a box to pack widgets into
        self.box1 = gtk.HBox(False, 0)
        self.window.add(self.box1)

        # Create cancel button
        self.button1 = gtk.Button("Cancel")
        self.button1.set_border_width(10)
        self.button1.connect("clicked", self.delete_event, "Changed me mind :)")
        self.box1.pack_start(self.button1, True, True, 0)
        self.button1.show()

        # Create logout button
        self.button2 = gtk.Button("Log out")
        self.button2.set_border_width(10)
        self.button2.connect("clicked", self.logout)
        self.box1.pack_start(self.button2, True, True, 0)
        self.button2.show()

        # Create reboot button
        self.button3 = gtk.Button("Reboot")
        self.button3.set_border_width(10)
        self.button3.connect("clicked", self.reboot)
        self.box1.pack_start(self.button3, True, True, 0)
        self.button3.show()

        # Create shutdown button
        self.button4 = gtk.Button("Shutdown")
        self.button4.set_border_width(10)
        self.button4.connect("clicked", self.shutdown)
        self.box1.pack_start(self.button4, True, True, 0)
        self.button4.show()

        self.box1.show()
        self.window.show()

def main():
    gtk.main()

if __name__ == "__main__":
    gogogo = DoTheLogOut()
    main()
</code></pre>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/crunchbanglinux/" title="Browse all posts tagged with &#8220;crunchbanglinux&#8221;">crunchbanglinux</a>, <a href="http://crunchbang.org/tags/openbox/" title="Browse all posts tagged with &#8220;openbox&#8221;">openbox</a>, <a href="http://crunchbang.org/tags/python/" title="Browse all posts tagged with &#8220;python&#8221;">python</a></p>
    ]]></description>
</item>

<item>
    <title>TwitterZoid PHP Script</title>
    <link>http://crunchbang.org/archives/2008/02/20/twitterzoid-php-script/</link>
    <pubDate>Wed, 20 Feb 2008 02:16:55 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2008/02/20/twitterzoid-php-script/</guid>
    <description><![CDATA[
    <p>It&#39;s a rather silly name, I know; however, TwitterZoid is the chosen name of my PHP script for parsing Twitter RSS feeds. I&#39;ve been using <a href="http://twitter.com/ " title="Twitter - What are you doing?">Twitter</a> quite steadily for a couple of weeks now and I thought it might be nice to include <a href="http://crunchbang.org/what-am-i-doing/ " title="What am I doing?">my latest tweets on my blog</a>, so I wrote TwitterZoid to do just that.</p>

<p>TwitterZoid differs to other PHP based Twitter RSS parsers, <em>at least the ones I tried before I wrote it</em>, in that it will automatically link both <a href="http://help.twitter.com/index.php?pg=kb.page&amp;id=63 " title="Back @ you baby! Or: What does @username do?">lexicons</a> and URLs found within individual tweets. It also tries to mimic Twitter&#39;s timestamping, although this could be improved.</p>

<h3>TwitterZoid usage</h3>

<p>I wanted to make TwitterZoid as simple to use as possible. Therefore I decided to write the script to be used as a simple include file which can be used on any PHP page. Basically, to use TwitterZoid all you need to do is set a couple of variables, include <code>twitterzoid.php</code> and then <code>echo</code> the main <code>$TwitterZoid</code> variable where you would like your list of tweets to appear.</p>

<h4>Example set-up:</h4>

<pre><code>$twitter_username = "corenominal";
$twitter_feed = "http://twitter.com/statuses/user_timeline/99713.rss";
require_once('twitterzoid.php');
</code></pre>

<h4>Call on the main TwitterZoid variable to produce the list of tweets:</h4>

<pre><code>echo $TwitterZoid;
</code></pre>

<p>Don&#39;t worry if this reads like gibberish, I&#39;ve included an example page within the download.</p>

<h3>TwitterZoid examples</h3>

<p>There are currently two demonstrations of TwitterZoid in action, see:</p>

<p>My official &#34;What am I doing?&#34; Twitter page:<br />
<a href="http://crunchbang.org/what-am-i-doing/ " title="What am I doing?">http://crunchbang.org/what-am-i-doing/</a></p>

<p>A more stylised version of &#34;What am I doing?&#34;, included within the download:<br />
<a href="http://crunchbang.org/projects/twitterzoid/demo/ " title="What am I doing? - demo version included with download.">http://crunchbang.org/projects/twitterzoid/demo/</a></p>

<h3>Download TwitterZoid</h3>

<p><strong>Location:</strong> <a href="http://crunchbang.org/projects/twitterzoid/twitterzoid-0.1.tar.gz " title="Download TwitterZoid">http://crunchbang.org/projects/twitterzoid/twitterzoid-0.1.tar.gz</a><br />
<strong>MD5:</strong> 99dace9f9872cf1ebf93588bb2d36458</p>

<h3>TwitterZoid license</h3>

<p>This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.</p>

<p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.</p>

<p><a href="http://www.gnu.org/licenses/ " title="GNU General Public License">http://www.gnu.org/licenses/</a></p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/projects/" title="Browse all posts tagged with &#8220;projects&#8221;">projects</a>, <a href="http://crunchbang.org/tags/scripts/" title="Browse all posts tagged with &#8220;scripts&#8221;">scripts</a>, <a href="http://crunchbang.org/tags/twitter/" title="Browse all posts tagged with &#8220;twitter&#8221;">twitter</a>, <a href="http://crunchbang.org/tags/webdesign/" title="Browse all posts tagged with &#8220;webdesign&#8221;">webdesign</a></p>
    ]]></description>
</item>

<item>
    <title>Wicked Cool Shell Scripts</title>
    <link>http://crunchbang.org/archives/2008/02/17/wicked-cool-shell-scripts/</link>
    <pubDate>Sun, 17 Feb 2008 07:41:45 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2008/02/17/wicked-cool-shell-scripts/</guid>
    <description><![CDATA[
    <p>I&#39;ve not read the book, but the <a href="http://www.intuitive.com/wicked/index.shtml " title="Wicked Cool Shell Scripts">Wicked Cool Shell Scripts</a> site and its example shell scripts are, erm, wicked cool. The site offers a whole host of scripts, some of which could quite easily be adapted/hacked into useful tools. If you&#39;re remotely interested in Shell scripting, you should take a look, even people with scripting experience might learn a thing or two.</p>

<p>URL: <a href="http://www.intuitive.com/wicked/index.shtml " title="Wicked Cool Shell Scripts">http://www.intuitive.com/wicked/index.shtml</a></p>

<p>Download the script library: <a href="http://crunchbang.org/misc/wicked-cool-shell-scripts.tgz " title="Wicked Cool Shell Scripts">wicked-cool-shell-scripts.tgz</a></p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/bash/" title="Browse all posts tagged with &#8220;bash&#8221;">bash</a>, <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/linux/" title="Browse all posts tagged with &#8220;linux&#8221;">linux</a>, <a href="http://crunchbang.org/tags/programming/" title="Browse all posts tagged with &#8220;programming&#8221;">programming</a>, <a href="http://crunchbang.org/tags/shell/" title="Browse all posts tagged with &#8220;shell&#8221;">shell</a></p>
    ]]></description>
</item>

<item>
    <title>PHP Function: Word Filter</title>
    <link>http://crunchbang.org/archives/2008/01/29/php-function-word-filter/</link>
    <pubDate>Tue, 29 Jan 2008 10:39:54 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2008/01/29/php-function-word-filter/</guid>
    <description><![CDATA[
    <p>So, last night I was mainly experimenting with <a href="http://crunchbang.org/projects/whird/ " title="Whird">Whird</a>, more specifically I was trying to build an internal search feature. This is something I&#39;ve been putting off for long enough, and I&#39;d really like to get it coded up. I didn&#39;t finish it last night, but at least I&#39;ve made a start.</p>

<p>Anyhow, during my experimentation I wrote this little PHP function for filtering words out of a string. Basically the function takes 2 strings as arguments before filtering words out of the first string based on words found in the second. I&#39;ve posted it below for future reference:</p>

<pre><code>function word_filter($string1,$string2){
    $string1 = trim($string1);
    $string1 = preg_replace('/s+/', ' ', $string1);
    $string1 = explode(" ",$string1);
    $wordcount = count($string1);
    $i = 0;
    while ($i &lt; $wordcount) {
        $string = $string1[$i];
        if (strstr(strtolower($string2),strtolower($string))) {
            $string1[$i] = "";
        }
    $i++;
    }
$string1 = implode(" ",$string1);        
return($string1);
}
</code></pre>

<h3>Example usage</h3>

<p>This is probably not the best example, but this:</p>

<pre><code>$poem = &lt;&lt;&lt;EOD
&lt;p&gt;&lt;em&gt;GIVE me women, wine, and snuff &lt;br /&gt;
Untill I cry out "hold, enough!" &lt;br /&gt;
You may do so sans objection &lt;br /&gt;
Till the day of resurrection: &lt;br /&gt;
For, bless my beard, they aye shall be &lt;br /&gt;
My beloved Trinity.&lt;/em&gt;&lt;/p&gt;
EOD;

$common_words = 'and be but do for is it may me my not of the they there so was you';

echo word_filter($poem,$common_words);
</code></pre>

<p>Would output something like this:</p>

<p><em>GIVE  women, wine,  snuff <br /> Untill  cry out &#34;hold, enough!&#34; <br />     sans objection <br /> Till  day resurrection: <br /> For, bless  beard,  aye shall  <br />  beloved Trinity.</em></p>

<p><a href="http://en.wikipedia.org/wiki/John_Keats " title="Wikipedia - John Keats">John Keats</a> would be proud, not! Please feel free to optimise, or let me know if a one line equivalent already exists :)</p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/projects/" title="Browse all posts tagged with &#8220;projects&#8221;">projects</a>, <a href="http://crunchbang.org/tags/whird/" title="Browse all posts tagged with &#8220;whird&#8221;">whird</a></p>
    ]]></description>
</item>

<item>
    <title>Bash Script: MySQL Backup</title>
    <link>http://crunchbang.org/archives/2007/11/18/bash-script-mysql-backup/</link>
    <pubDate>Sun, 18 Nov 2007 22:11:11 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/11/18/bash-script-mysql-backup/</guid>
    <description><![CDATA[
    <p><em>I thought that it might be a good idea to start posting a few of my scripts; it&#39;ll be handy to have them on my site for future reference. Also, I learn  a lot by reading example scripts &#8212; I guess others might be able to learn from mine.</em></p>

<p>I wrote the following Bash script to perform a backup of a remote MySQL database. The script first connects via SSH and performs a MySQL dump, saving the results to file. It then connects via SFTP and downloads the file. Once the file has been downloaded, it restores the database to my local MySQL server.</p>

<p>It is quite a simple Bash script and it should be fairly straightforward to follow.</p>

<pre><code>#!/bin/sh
# Settings
#############################
REMOTEHOST="example.com"
REMOTEBACKUPDIR="backup/sql"
SQLHOST="localhost"
SQLDB="database_name"
SQLUSER="username"
SQLPASS="password"
SQLFILE="database_name.sql"
LOCALBACKUPDIR="backup/sql"
#############################
# Start main
echo "* Connecting via SSH..."
ssh $REMOTEHOST &lt;&lt;**
echo "* Performing SQL dump..."
if [ -d $REMOTEBACKUPDIR ]; then
    cd $REMOTEBACKUPDIR
else
    mkdir $REMOTEBACKUPDIR
    cd $REMOTEBACKUPDIR
fi
mysqldump -h $SQLHOST --user="$SQLUSER" --password="$SQLPASS" $SQLDB &gt; $SQLFILE
echo "* Closing SSH connection..."
exit
**
cd ~
if [ -d $LOCALBACKUPDIR ]; then
    cd $LOCALBACKUPDIR
else
    mkdir $LOCALBACKUPDIR
    cd $LOCALBACKUPDIR
fi
echo "* Connecting via SFTP..."
sftp $REMOTEHOST &lt;&lt;**
cd $REMOTEBACKUPDIR
get $SQLFILE
exit
**
echo "* Restoring SQL dump to local server..."
mysql --user "$SQLUSER" --password="$SQLPASS" $SQLDB &lt; $SQLFILE
echo "* SQL backup complete."
cd ~
exit 0
</code></pre>

<h3>Notes</h3>

<ol>
<li>For automation purposes, this script assumes that SSH and SFTP have been configured for automatic login. See &#34;<a href="http://crunchbang.org/archives/2007/10/19/creating-privatepublic-ssh-keys/ " title="Creating Private/Public SSH Keys">Creating Private/Public SSH Keys</a>&#34;</li>
<li>It also assumes there is a mirrored MySQL server and user account running on the local machine.</li>
<li>The script can be automated using <a href="http://crunchbang.org/archives/2007/10/26/howto-setup-a-crontab-file/ " title="Howto Set-up a Crontab File">Crontab</a>.</li>
<li>Lacks any error handling and/or logging!?</li>
<li>I&#39;ve worked with some commercial hosting providers who do not grant table locking privileges to their MySQL users &#8212; table locking can be bypassed by adding the &#34;<code>--skip-lock-tables</code>&#34; option to the &#34;<code>mysqldump</code>&#34; command. <em>Use with caution.</em>   </li>
</ol>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/bash/" title="Browse all posts tagged with &#8220;bash&#8221;">bash</a>, <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/linux/" title="Browse all posts tagged with &#8220;linux&#8221;">linux</a>, <a href="http://crunchbang.org/tags/programming/" title="Browse all posts tagged with &#8220;programming&#8221;">programming</a>, <a href="http://crunchbang.org/tags/shell/" title="Browse all posts tagged with &#8220;shell&#8221;">shell</a></p>
    ]]></description>
</item>

<item>
    <title>Ternary Operator in PHP</title>
    <link>http://crunchbang.org/archives/2007/11/04/ternary-operator-in-php/</link>
    <pubDate>Sun, 04 Nov 2007 00:43:41 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/11/04/ternary-operator-in-php/</guid>
    <description><![CDATA[
    <p>Over on <a href="http://www.planet-php.net/ " title="Planet PHP - All news in one place.">Planet PHP</a> there&#39;s a running debate [<em>see <a href="http://www.alexatnet.com/node/100 " title="6 PHP coding tips to write less code.">here</a>, <a href="http://www.phpcult.com/blog/02/less-code-vs-good-code/ " title="Less code vs good code.">here</a> &amp; <a href="http://www.phpguru.org/article.php/154 " title="Less code vs good code - my take.">here</a></em>] over the use of the ternary operator. I love a good debate so I thought I&#39;d chip in with my 2 pennies.</p>

<p>The ternary operator looks attractive and can reduce the amount of code that you write. I know this but I still don&#39;t use it. I think PHP is an awesome language and a major contributing factor to its awesomeness is its simplicity. It&#39;s a relatively easy language to learn and I think that the wide spread use of the ternary operator would only increase the barrier to entry [<em>not a good thing!</em>]</p>

<p>Also, I find it interesting that the ternary operator is hardly ever referenced in the comments and code examples at <a href="http://php.net/ " title="PHP.net">PHP.net</a>. Indeed chapter 16 &#34;<a href="http://uk3.php.net/manual/en/language.control-structures.php " title="PHP - Control Structures">Control Structures</a>&#34; of the manual hardly mentions it at all.</p>

<h3>Ternary operator code example</h3>

<p>In case you&#39;ve no idea what this post is about.</p>

<p><strong>Before:</strong> <em>no ternary operator, easy to understand</em></p>

<pre><code>if ($treat == 'cream') {
    $cat = 'Happy!';
} else {
    $cat = 'Not so happy.';
}
</code></pre>

<p><strong>After:</strong> <em>ternary operator in use, less code but not so easy to understand</em></p>

<pre><code>$cat = ($treat == 'cream') ? 'Happy!' : 'Not so happy.';
</code></pre>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/programming/" title="Browse all posts tagged with &#8220;programming&#8221;">programming</a></p>
    ]]></description>
</item>

<item>
    <title>Image Hotlinking Prevention with .htaccess</title>
    <link>http://crunchbang.org/archives/2007/11/02/image-hotlinking-prevention-with-dot-htaccess/</link>
    <pubDate>Fri, 02 Nov 2007 22:58:07 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/11/02/image-hotlinking-prevention-with-dot-htaccess/</guid>
    <description><![CDATA[
    <p>I&#39;m designing some banners and buttons for <a href="http://ubuntu.com/ " title="Ubuntu Linux">Ubuntu</a> advocacy. I have no problem with giving the images away [<em>indeed this is why I made them,</em>] however I really can&#39;t afford to host them. So before I post them to this site I wanted to ensure that I had some hotlink protection in place.</p>

<p>To accomplish this I&#39;ve created a new directory under my site&#39;s default &#34;images&#34; directory and protected it from hotlinking with an Apache <code>.htaccess</code> file. For future reference here are the contents of the <code>.htaccess</code> file:</p>

<pre><code>RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+.)?example.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*.(jpe?g|gif|bmp|png)$ ../hotlink.gif [L]
</code></pre>

<p>The first line of the above code turns on the <a href="http://en.wikipedia.org/wiki/Rewrite_engine " title="Wikipedia - Rewrite Engine">mod_rewrite engine</a> in Apache. A requirement for the rewrite commands.</p>

<p>The second line matches any requests from the URL example.com [<em>change this to suit your requirements</em>]. The [NC] code means &#34;No Case&#34;, meaning match the URL regardless of case.</p>

<p>The third line allows empty referrals.</p>

<p>Finally, the last line matches any files ending with the extension jpeg, jpg, gif, bmp, or png. This is then replaced by the <code>hotlink.gif</code> file [<em>see below</em>] residing in the above <code>images</code> directory.</p>

<p><img src="http://crunchbang.org/images/hotlink.gif" alt="An example no hotlinking image with message." /></p>

<p>If you need to allow more domains to hotlink to your images you can simply duplicate line two. See the example below:</p>

<pre><code>RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+.)?example.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+.)?another-example.net/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+.)?one-more-example.org/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*.(jpe?g|gif|bmp|png)$ ../hotlink.gif [L]
</code></pre>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/images/" title="Browse all posts tagged with &#8220;images&#8221;">images</a>, <a href="http://crunchbang.org/tags/unitedhosting/" title="Browse all posts tagged with &#8220;unitedhosting&#8221;">unitedhosting</a>, <a href="http://crunchbang.org/tags/webdesign/" title="Browse all posts tagged with &#8220;webdesign&#8221;">webdesign</a></p>
    ]]></description>
</item>

<item>
    <title>PHP 5 &amp; Feed Updates</title>
    <link>http://crunchbang.org/archives/2007/10/29/php-and-feed-updates/</link>
    <pubDate>Mon, 29 Oct 2007 11:32:54 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/10/29/php-and-feed-updates/</guid>
    <description><![CDATA[
    <p>It&#39;s been a long time coming but my website [<em><a href="http://crunchbang.org/ " title="CrunchBang.org">CrunchBang.org</a></em>] is now finally running on PHP 5. My <a href="http://unitedhosting.co.uk " title="United Hosting">hosting provider</a> performed the upgrade earlier this month and made the switch optional on a domain basis. Any domain on their servers can run either PHP version 4 [<em>default</em>] or upgrade to version 5 by adding a script handler in <code>.htaccess</code>:</p>

<pre><code>Action php5-script /interpreters/php5-script
AddHandler php5-script .php
</code></pre>

<p>My development systems all run PHP 5 so the switch was a breeze. Hopefully from now on I&#39;ll be able to avoid <a href="http://crunchbang.org/archives/2007/09/26/php-strrpos-vs-strpos/ " title="strrpos vs strpos">stuff like this</a>.</p>

<p>Also, I&#39;ve been working on my <a href="http://crunchbang.org/tags/whird/ " title="Posts relating to Whird">Whird project</a> over the weekend. I&#39;ve now added feeds for individual tags and updated various features to improve usability. I&#39;ve had to place some URL rewrites for the new feeds and I&#39;m hoping that the various planets [<em><a href="http://ubuntuweblogs.org/ " title="Planet Ubuntu Users">Planet Ubuntu Users</a>, <a href="http://planet.ubuntu-uk.org/ " title="Planet Ubuntu UK">Planet Ubuntu UK</a></em>] don&#39;t get flooded &#8212; I apologise if they do :)</p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/ubuntu/" title="Browse all posts tagged with &#8220;ubuntu&#8221;">ubuntu</a>, <a href="http://crunchbang.org/tags/unitedhosting/" title="Browse all posts tagged with &#8220;unitedhosting&#8221;">unitedhosting</a>, <a href="http://crunchbang.org/tags/whird/" title="Browse all posts tagged with &#8220;whird&#8221;">whird</a></p>
    ]]></description>
</item>

<item>
    <title>Howto Set-up a Crontab File</title>
    <link>http://crunchbang.org/archives/2007/10/26/howto-setup-a-crontab-file/</link>
    <pubDate>Fri, 26 Oct 2007 17:18:28 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/10/26/howto-setup-a-crontab-file/</guid>
    <description><![CDATA[
    <p>Yesterday I mainly worked on some PHP backup scripts [<em>not very exciting, but necessary.</em>] The scripts are run at regular intervals and perform various data backups across numerous domains. To accomplish this I set-up some cron jobs that will automatically execute the scripts at set intervals.</p>

<p>This is not the first time that I&#39;ve had to set-up cron jobs [<em>I&#39;ve got several that date back a few years.</em>] I think that having the ability to set-up scheduled tasks is really quite important. Therefore I thought I&#39;d blog about it for future reference.</p>

<h3>What is cron?</h3>

<p>Cron is a daemon/service that executes shell commands periodically on a given schedule. Cron is driven by a crontab, a configuration file that holds details of what commands are to be run along with a timetable of when to run them.</p>

<h3>Creating a crontab file</h3>

<p>You can create a crontab file by entering the following terminal command:</p>

<pre><code>crontab -e
</code></pre>

<p>Entering the above command will open a terminal editor [<em>Ubuntu uses Nano by default</em>] with a new blank crontab file [<em>or it will open an existing crontab if you already have one.</em>] You can now enter the commands to be executed [<em>see syntax below</em>] before saving the file and exiting the editor. As long as your entries were entered correctly your commands should now be executed at the times/dates you specified. You can see a list of active crontab entries by entering the following terminal command:</p>

<pre><code>crontab -l
</code></pre>

<h3>Crontab syntax</h3>

<p>A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:</p>

<pre><code>*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
</code></pre>

<h3>Crontab examples</h3>

<p>Writing a crontab file can be a somewhat confusing for first time users [<em>and the above table probably doesn&#39;t help much!</em>] Therefore I&#39;ve listed below some crontab examples:</p>

<pre><code>* * * * * &lt;command&gt; #Runs every minute
30 * * * * &lt;command&gt; #Runs at 30 minutes past the hour
45 6 * * * &lt;command&gt; #Runs at 6:45 am every day
45 18 * * * &lt;command&gt; #Runs at 6:45 pm every day
00 1 * * 0 &lt;command&gt; #Runs at 1:00 am every Sunday
00 1 * * 7 &lt;command&gt; #Runs at 1:00 am every Sunday
00 1 * * Sun &lt;command&gt; #Runs at 1:00 am every Sunday
30 8 1 * * &lt;command&gt; #Runs at 8:30 am on the first day of every month
00 0-23/2 02 07 * &lt;command&gt; #Runs every other hour on the 2nd of July
</code></pre>

<p>As well as the above there are also special strings that can be used:</p>

<pre><code>@reboot &lt;command&gt; #Runs at boot
@yearly &lt;command&gt; #Runs once a year [0 0 1 1 *]
@annually &lt;command&gt; #Runs once a year [0 0 1 1 *]
@monthly &lt;command&gt; #Runs once a month [0 0 1 * *]
@weekly &lt;command&gt; #Runs once a week [0 0 * * 0]
@daily &lt;command&gt; #Runs once a day [0 0 * * *]
@midnight &lt;command&gt; #Runs once a day [0 0 * * *]
@hourly &lt;command&gt; #Runs once an hour [0 * * * *]
</code></pre>

<h3>Multiple commands</h3>

<p>A double-ampersand &#34;<code>&amp;&amp;</code>&#34; can be used to run multiple commands consecutively. The following example would run <code>command_01</code> and then <code>command_02</code> once a day:</p>

<pre><code>@daily &lt;command_01&gt; &amp;&amp; &lt;command_02&gt;
</code></pre>

<h3>Disabling email notifications</h3>

<p>By default a cron job will send an email to the user account executing the cronjob. If this is not needed put the following command at the end of the cron job line:</p>

<pre><code>&gt;/dev/null 2&gt;&amp;1
</code></pre>

<h3>Specifying a crontab file to use</h3>

<p>As mentioned at the top of this post, you can create a new crontab file with the &#34;<code>crontab -e</code>&#34; command. However, you may already have a crontab file, if you do you can set it to be used with the following command:</p>

<pre><code>crontab -u &lt;username&gt; &lt;crontab file&gt;
</code></pre>

<p>Therefore the following command&#8230;</p>

<pre><code>crontab -u tux ~/crontab
</code></pre>

<p>&#8230;would set Tux&#39;s crontab file to that of the file named &#34;crontab&#34; residing in Tux&#39;s home directory.</p>

<h3>Removing a crontab file</h3>

<p>To remove your crontab file simply enter the following terminal command:</p>

<pre><code>crontab -r
</code></pre>

<h3>Further information</h3>

<p>Refer to the man page for further information about crontab. Enter the terminal command:</p>

<pre><code>man crontab
</code></pre>

<h3>External links</h3>

<p>Some external links for your browsing pleasure:</p>

<ul>
<li><a href="http://en.wikipedia.org/wiki/Cron " title="Cron article on Wikipedia">Cron article on Wikipedia</a></li>
<li><a href="https://help.ubuntu.com/community/CronHowto " title="CronHowto">Ubuntu Documentation - CronHowto</a></li>
<li><a href="http://www.debianhelp.co.uk/schedulejobs.htm " title="Scheduling Backup Jobs using at and crontab in debian">Debian Help - Scheduling Backup Jobs using at and crontab in debian</a></li>
<li><a href="http://www.mattcutts.com/blog/how-to-fetch-a-url-with-curl-or-wget-silently/ " title="How to fetch a url with curl or wget silently">Matt Cutts - How to fetch a url with curl or wget silently</a></li>
<li><a href="http://www.sitepoint.com/article/introducing-cron " title="Getting PHP and Cron to Work Together">Sitepoint - Getting PHP and Cron to Work Together</a></li>
</ul>

<p>I think that pretty much covers the subject of cron jobs and crontab. Please feel free to comment if I&#39;ve missed anything/made any boobs.</p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/crontab/" title="Browse all posts tagged with &#8220;crontab&#8221;">crontab</a>, <a href="http://crunchbang.org/tags/linux/" title="Browse all posts tagged with &#8220;linux&#8221;">linux</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/ubuntu/" title="Browse all posts tagged with &#8220;ubuntu&#8221;">ubuntu</a></p>
    ]]></description>
</item>

<item>
    <title>User Agent Sniffer</title>
    <link>http://crunchbang.org/archives/2007/10/18/user-agent-sniffer/</link>
    <pubDate>Thu, 18 Oct 2007 08:52:21 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/10/18/user-agent-sniffer/</guid>
    <description><![CDATA[
    <p>I&#39;ve currently got several web projects at various stages of development. One thing that all of these projects have in common is that they all capture and manipulate user-agent stings.</p>

<h3>What are user-agent strings?</h3>

<p>User-agents strings are used by client applications such as web browsers, feed readers, bots and other software to identify themselves to the servers they are connecting to. The strings contain important information such as application type, version, language and operating system. A typical user-agent string might look like this:</p>

<pre><code>Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.7 (like Gecko) (Kubuntu)
</code></pre>

<p>The above example is used to identify that the client is a Mozilla compatible Konqueror web browser running on Kubuntu Linux.</p>

<h3>Collecting user-agent strings</h3>

<p>To run tests on my projects I need some sample data to play around with [<em>a list or database table of user-agent strings.</em>] I figured that the best way to get this sample data would be to collect some user-agent strings from the wild. So, I wrote a quick PHP script to do just that.</p>

<p>I ran the script on this site [<em>crunchbang.org</em>] for ten days, starting on October 7th 2007. During that ten day period the script captured a total of 2272 unique user-agent strings. The captured list included both standard and non-standard strings.</p>

<h3>A few facts about user-agent strings</h3>

<p>After capturing the list I then edited the PHP script so that it would report a few facts. Here is a breakdown of what was returned:</p>

<p><strong>1.</strong> At just 6 characters in length the shortest user-agent string captured was:</p>

<pre><code>NG/2.0
</code></pre>

<p><strong>2.</strong> The longest measured in at 205 characters, it was:</p>

<pre><code>Mozilla/5.0 (compatible; MSIE 7.0; Windows; HTMLAB; .NET CLR 1.1.4322; 
MEGAUPLOAD 1.0; Seekmo; ZangoToolbar4.8.2; Alexa Toolbar; Hotbar 4.2.8.0) (compatible; 
Googlebot/2.1; +http://www.google.com/bot.html)
</code></pre>

<p><strong>3.</strong> The average computed length of the user-agent strings was 91.2750880282 characters.</p>

<p><strong>4.</strong> Most strings contained some non-alphanumeric characters, these were:</p>

<pre><code>/ . ( ; - : ) + _ ! = , @ &amp;  ' [ ] * ~ ? { }
</code></pre>

<p><strong>5.</strong> The strangest user-agent string was:</p>

<pre><code>Mmm.... Brains....
</code></pre>

<h3>View the whole report</h3>

<p>You can view the whole report here: <a href="http://crunchbang.org/misc/user-agent-report-2007-10-17.txt " title="View the User Agent Sniffer report.">http://crunchbang.org/misc/user-agent-report-2007-10-17.txt</a></p>

<h3>Get the sample data</h3>

<p>I thought it would be good to share the sample data. There&#39;s no private or confidential information in the data and I figure it may come in handy for other developers working on similar projects.</p>

<p>You can get the data as an ASCII file [<em>one user-agent string per line</em>] here: <a href="http://crunchbang.org/misc/sample-user-agents-ascii.txt " title="Get the sample data as a flat ascii file.">http://crunchbang.org/misc/sample-user-agents-ascii.txt</a></p>

<p>Or, as an SQL statement here: <a href="http://crunchbang.org/misc/sample-user-agents-mysql.txt " title="Insert the sample data into a MySQL database using this SQL statement.">http://crunchbang.org/misc/sample-user-agents-mysql.txt</a></p>

<h3>Get the PHP script</h3>

<p>If you fancy having a go at collecting your own samples you can grab my PHP script here: <a href="http://crunchbang.org/misc/ua-sniffer.txt " title="Get the User Agent Sniffer PHP script.">http://crunchbang.org/misc/ua-sniffer.txt</a></p>

<p>The script requires the use of MySQL. Other than that it&#39;s a fairly straightforward affair. Just edit the four settings to define your database name, address, username and password.</p>

<p>I ran the script by calling it with a <code>require_once</code> statement. Note that the script also sets and reads a cookie so you&#39;ll need to call on it before outputting any data to the client.</p>

<pre><code>require_once("ua-sniffer.php");
</code></pre>

<p>Once the script has collected some user-agent strings it is possible to query it and have it produce a basic report. You can do this by accessing the script through your browser like so:</p>

<pre><code>http://www.example.com/ua-sniffer.php?report=true
</code></pre>

<h3>Links to external references</h3>

<ul>
<li><a href="http://en.wikipedia.org/wiki/User_agent " title="User Agent article on Wikipedia">User Agent article on Wikipedia</a></li>
<li><a href="http://www.user-agents.org/ " title="List of User-Agents at user-agents.org">List of User-Agents at user-agents.org</a></li>
<li><a href="http://www.zytrax.com/tech/web/browser_ids.htm " title="Browser ID strings at zytrax.com">Another list of browser ID strings</a></li>
<li><a href="http://www.useragentstring.com/ " title="User agent string analysis">User agent string analysis</a></li>
</ul>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/programming/" title="Browse all posts tagged with &#8220;programming&#8221;">programming</a>, <a href="http://crunchbang.org/tags/projects/" title="Browse all posts tagged with &#8220;projects&#8221;">projects</a>, <a href="http://crunchbang.org/tags/scripts/" title="Browse all posts tagged with &#8220;scripts&#8221;">scripts</a></p>
    ]]></description>
</item>

<item>
    <title>301 Redirect with PHP</title>
    <link>http://crunchbang.org/archives/2007/10/02/301-redirect-with-php/</link>
    <pubDate>Tue, 02 Oct 2007 03:09:28 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/10/02/301-redirect-with-php/</guid>
    <description><![CDATA[
    <p>I&#39;ve just had to permanently redirect an entire site. The site consisted of several hundred pages and on the face of it, it seemed like a daunting task. However two lines of PHP came to the rescue :-)</p>

<pre><code> Header( "HTTP/1.1 301 Moved Permanently" );
 Header( "Location: http://www.new-location.com" );
</code></pre>

<p>Luckily, each page on the site referred to a single configuration file and this made the process painless.</p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/php/" title="Browse all posts tagged with &#8220;php&#8221;">php</a>, <a href="http://crunchbang.org/tags/webdesign/" title="Browse all posts tagged with &#8220;webdesign&#8221;">webdesign</a></p>
    ]]></description>
</item>

<item>
    <title>Silly Variable Names</title>
    <link>http://crunchbang.org/archives/2007/09/30/silly-variable-names/</link>
    <pubDate>Sun, 30 Sep 2007 06:19:05 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/09/30/silly-variable-names/</guid>
    <description><![CDATA[
    <p>They&#39;re not big and they&#39;re not clever. So how come they keep slipping into my code?</p>

<pre><code>if(!empty($bum)){
    unload($bum);
}
</code></pre>

<p><strong>Note to self:</strong> stop with the silly variable names already!</p>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/fun/" title="Browse all posts tagged with &#8220;fun&#8221;">fun</a>, <a href="http://crunchbang.org/tags/programming/" title="Browse all posts tagged with &#8220;programming&#8221;">programming</a></p>
    ]]></description>
</item>

<item>
    <title>robots.txt Adventure</title>
    <link>http://crunchbang.org/archives/2007/09/29/robotstxt-adventure/</link>
    <pubDate>Sat, 29 Sep 2007 04:42:17 GMT</pubDate>
    <dc:creator>Philip Newborough</dc:creator>
    <guid>http://crunchbang.org/archives/2007/09/29/robotstxt-adventure/</guid>
    <description><![CDATA[
    <p>If you&#39;re looking for information about robot.txt files you should read &#34;<a href="http://www.nextthing.org/archives/2007/03/12/robotstxt-adventure " title="nextthing.org: robots.txt Adventure">robots.txt Adventure</a>&#34; at nextthing.org. The article is packed with original research and features some interesting facts and figures.</p>

<p>As for my own robots.txt, it&#39;s a pretty standard two line affair:</p>

<pre><code>User-agent: *
Disallow:
</code></pre>

    <p style="font-size:smaller;">Tags: <a href="http://crunchbang.org/tags/code/" title="Browse all posts tagged with &#8220;code&#8221;">code</a>, <a href="http://crunchbang.org/tags/webdesign/" title="Browse all posts tagged with &#8220;webdesign&#8221;">webdesign</a></p>
    ]]></description>
</item>

 </channel>
</rss>