<?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>Kerkness.ca &#187; PHP</title>
	<atom:link href="http://www.kerkness.ca/tagged/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerkness.ca</link>
	<description>flexing my kerkness, among other things</description>
	<lastBuildDate>Wed, 14 Oct 2009 15:12:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamically add row to a table using PHP and Mootools</title>
		<link>http://www.kerkness.ca/add-row-to-table/</link>
		<comments>http://www.kerkness.ca/add-row-to-table/#comments</comments>
		<pubDate>Mon, 25 May 2009 19:44:54 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/?p=529</guid>
		<description><![CDATA[This short cross browser example will show you how to dynamically add rows to a table using PHP and Mootools.
The Files

table_form.html  ::  This is our main HTML file.  It contains a HTML Table and a simple Form.
table_form.php :: This is the php file which will process the form and return results back to our HTML [...]]]></description>
			<content:encoded><![CDATA[<p>This short cross browser example will show you how to dynamically add rows to a table using PHP and Mootools.</p>
<h2>The Files</h2>
<ul>
<li>table_form.html  ::  This is our main HTML file.  It contains a HTML Table and a simple Form.</li>
<li>table_form.php :: This is the php file which will process the form and return results back to our HTML page</li>
<li><a href="http://kerkness.ca/moo/table_form/table_form.js">table_form.js</a> :: This is the Javascript file which will handle submitting data to php and handle the response</li>
<li>table_form.css :: basic css file to add styling</li>
</ul>
<p><a href="http://kerkness.ca/moo/table_form/table_form.html">View a Demo</a></p>
<p><a href="http://kerkness.ca/moo/table_form/table_form.zip">Download the full Source</a></p>
<p>I&#8217;ve tried to add detailed comments to the source code but if you have any questions just post them in the comments.</p>
<h2>The Bare Bones : Row Injection</h2>
<p>For those who are only interested in the table row injection here is a function which will get the job done and is crossbrowser. If you find otherwise let me know. The key here is instead of creating individual elements for tr and td and injecting them into the table, you create a full table element with a single row and inject that into your table.</p>
<pre name="code" class"js">
var inject_row = function( table, data )
{
    var str = '';
    data.each(function(item, index){
        str += '&lt;td&gt;'+item+'&lt;/td&gt;';
    });
    var tr = new Element('div', {
        html: '&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;' + str + '&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;'
    }).getElement('tr');
    tr.inject( table );
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/add-row-to-table/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Forgotten PHP : Some useful and simple PHP features that I often forget about.</title>
		<link>http://www.kerkness.ca/forgotten-php-some-useful-and-simple-php-features-that-i-often-forget-about/</link>
		<comments>http://www.kerkness.ca/forgotten-php-some-useful-and-simple-php-features-that-i-often-forget-about/#comments</comments>
		<pubDate>Fri, 01 May 2009 22:06:49 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[number_format]]></category>
		<category><![CDATA[parse_str]]></category>
		<category><![CDATA[sprintf]]></category>
		<category><![CDATA[substr_count]]></category>
		<category><![CDATA[urldecode]]></category>
		<category><![CDATA[urlencode]]></category>
		<category><![CDATA[vsprintf]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/?p=425</guid>
		<description><![CDATA[I&#8217;ve been programming with PHP for many years but regardless how familiar I am with the language I am constantly looking at reference documentation for reminders on how to do things. Here&#8217;s a list of some really handy PHP functions that I often forget about and then will stumble across them in documentation and am [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been programming with PHP for many years but regardless how familiar I am with the language I am constantly looking at <a href="http://ca.php.net/manual/en/funcref.php" target="_blank">reference documentation</a> for reminders on how to do things. Here&#8217;s a list of some really handy PHP functions that I often forget about and then will stumble across them in documentation and am reminded why I shouldn&#8217;t forget about them.</p>
<h2>How to format text to be inserted into a url</h2>
<p>If you need to pass some text or a dynamic string in a URL use <strong><em>urlencode()</em></strong> and <em><strong>urldecode()</strong></em></p>
<pre>// encoding the string
$str = "Jerry's Bedding Plants &amp; Ice Cream";
$url = 'http://www.example.com/page.php?args=' . urlencode( $str );

// decoding the string in page.php
$str = urldecode( $_GET['args'] );</pre>
<h2>Parsing a query string</h2>
<p>Want to automatically take the query string and dump the values into an array ? Use <em><strong>parse_str()</strong></em> . You can easily get the query string from <strong><em>$_SERVER['QUERY_STRING']</em></strong></p>
<pre name="code" class"php">$query = $_SERVER['QUERY_STRING'];  // foo=bar&amp;bar=foo&amp;cool=me&amp;you=ok
$vars = array();
parse_str( $query, $vars );
print_r( $vars );

/* output
Array
(
  [foo] =&gt; bar
  [bar] =&gt; foo
  [cool] =&gt; me
  [you] =&gt; ok
)
*/</pre>
<h2>Creating a formatted string</h2>
<p>Most people are very familiar with <strong><em>printf()</em></strong> in php but if you&#8217;re like me you often forget about <em><strong>sprintf()</strong></em> which returns your formatted string instead of printing it to the screen.</p>
<pre>$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);</pre>
<p>Another one you might forget about is <em><strong>vsprintf()</strong></em> which is similar to <em><strong>sprintf()</strong></em> but works with an array</p>
<pre name="code" class"php">$data = array( $year, $month, $day );
$isodate = vsprintf("%04d-%02d-%02d", $data );</pre>
<h2>Formatting numbers</h2>
<p>For some reason I always forget the arguments for the PHP <em><strong>number_format()</strong></em> function. The function takes one, two or 4 arguments ( will not accept just 3 arguments ).</p>
<pre name="code" class"php">// string number_format  ( float $number  , int $decimals  , string $dec_point  , string $thousands_sep  )
$number = number_format( '32456.82', 2, '.', ',' );</pre>
<h2>Count the number of times a small string occurs in a large string</h2>
<pre name="code" class"php">// how many times do I say rad;
$str = "I've got some rad moves, super rad moves, I've got rad mad bad not sad rad moves.";
$rad_count = substr_count( $str, 'rad' );</pre>
<p>Happy coding nerds !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/forgotten-php-some-useful-and-simple-php-features-that-i-often-forget-about/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get PHP dynamic variables inside Flex</title>
		<link>http://www.kerkness.ca/get-php-dynamic-variables-inside-flex/</link>
		<comments>http://www.kerkness.ca/get-php-dynamic-variables-inside-flex/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 00:52:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=61</guid>
		<description><![CDATA[Let&#8217;s say that before you load your flex application you want to pass some dynamic variables from PHP for it to act upon. This can be done pretty easily by slipping the values into PHP&#8217;s&#160; $_GET array and using Flex&#8217;s ExternalInterface class to get access to them from inside the Flex app.
Here is an example [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say that before you load your flex application you want to pass some dynamic variables from PHP for it to act upon. This can be done pretty easily by slipping the values into PHP&#8217;s&nbsp; <i>$_GET</i> array and using Flex&#8217;s <i>ExternalInterface</i> class to get access to them from inside the Flex app.</p>
<p>Here is an example Flex application which pulls values from the URL Query String.</p>
<pre>&lt;mx:Application creationcomplete="init()" layout="absolute" xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
private function init():void
{
  var qstr:String = ExternalInterface.call("window.location.search.substring", 1) as String;
  var qarr:Array = qstr.split('&amp;');

  var pairs:Array;
  for( var i:Number = 0 ; i &lt; qarr.length; i++ ){
     pairs = String( qarr[i] ).split('=');
     if( pairs[0] == 'myName' ){
           myName = pairs[1] as String;
     }
  }
}
]]&gt;
&lt;/mx:Script&gt;
&lt;mx:Label text="{myName}" /&gt;
&lt;/mx:Application&gt;</pre>
<p>
After you build the application and and Flex Builder has generated the <i>.html</i> file that displays your Flex app&nbsp; you can rename the file <i>.php</i> and then add the following to the top of the file.</p>
<pre>&#038;lt?php $_GET['myName'] = 'Hi I am Kerk'; ?&gt;</pre>
<p>
Now if you upload your SWF file and PHP file to a server that is running PHP and you open it in your browser you&#8217;ll&nbsp; see your value from PHP displayed in your Flex app.</p>
<p>I don&#8217;t know if that is a clear explanation or not,&nbsp; or if this is the best way to pass dynamic variables at load time into your flex application but it works for me.&nbsp; Comments or alternative solutions welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/get-php-dynamic-variables-inside-flex/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP json_encode() and Flex JSON.decode Gotcha</title>
		<link>http://www.kerkness.ca/php-json_encode-and-flex-jsondecode-gotcha/</link>
		<comments>http://www.kerkness.ca/php-json_encode-and-flex-jsondecode-gotcha/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 23:19:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=56</guid>
		<description><![CDATA[I came across a little &#8216;gotcha&#8217; when trying to get Flex JSON.decode to properly decode a JSON string I was producing with PHP. 
The one of the elements in the string contained &#8216; &#38;quote; &#8216; which when being encoded by php and sent as a HTTPService response back to my Flex application was being converted [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a little &#8216;gotcha&#8217; when trying to get Flex <i><b>JSON.decode</b></i> to properly decode a JSON string I was producing with PHP. </p>
<p>The one of the elements in the string contained &#8216; <i><b>&amp;quote;</b></i> &#8216; which when being encoded by php and sent as a <i><b>HTTPService</b></i> response back to my Flex application was being converted back into double quotes ( <i><b>&#8220;</b></i> ). When Flex <i><b>JSON.decode </b></i>attempted to decode the string it would silently fail.  No errors or anything.</p>
<p>In my case my JSON string was already pretty complicated with lots of variable content so it took a while to figure this out.  Hopefully someone else finds this post and saves themselves some time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/php-json_encode-and-flex-jsondecode-gotcha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GmailReader.php : A PHP IMAP Class for Reading your Gmail Account</title>
		<link>http://www.kerkness.ca/gmailreaderphp-a-php-imap-class-for-reading-your-gmail-account/</link>
		<comments>http://www.kerkness.ca/gmailreaderphp-a-php-imap-class-for-reading-your-gmail-account/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 01:09:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gmail]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=54</guid>
		<description><![CDATA[One of the requirements I had for a recent project involved keeping track of email communication from multiple Gmail users. My first thought was to simply forward a copy of all incoming and outgoing email to an email account on my server and accessing the email that way.  There was one problem with this [...]]]></description>
			<content:encoded><![CDATA[<p>One of the requirements I had for a recent project involved keeping track of email communication from multiple Gmail users. My first thought was to simply forward a copy of all incoming and outgoing email to an email account on my server and accessing the email that way.  There was one problem with this approach.  Gmail only allows you to forward a copy of incoming email somewhere else.  Emails composed in Gmail stay in Gmail.</p>
<p>My solution:  Use Gmail&#8217;s IMAP capabilities and monitor the accounts using <a href="http://ca3.php.net/manual/en/ref.imap.php">PHP-IMAP</a>.  This turned out to be a fairly easy to implement solution but I thought I would post my GmailReader class to save other people some initial headaches of getting things rolling.</p>
<p>First of all you&#8217;ll need to enable IMAP for you Gmail Account.  Log into Gmail and goto Settings &gt; Forwarding and POP/IMAP</p>
<p>Next you&#8217;ll need to make sure php is compiled with IMAP enabled.  This can be done on ubuntu with the following command and then restart apache for good measure.</p>
<pre>sudo apt-get install php5-imap
sudo /etc/init.d/apache2 restart</pre>
<p>In my particular scenario I run a script every so often to look for new emails since the last time I checked. So the class file is structured to check for new emails since a specific date string.</p>
<p><span style="font-size:130%;"><span style="font-weight: bold;">Usage Example</span></span></p>
<pre>include_once( 'GmailReader.php' );
$gmail = new GmailReader( 'username@gmail.com', 'password' );
$email = $gmail-&gt;getEmailSince('Fri, 5 Sep 2008 9:00:00');

print_r($email);</pre>
<p>The above example will print out an array of all emails ( still in the Inbox ) which arrived no earlier than 9am on the 5th of September.</p>
<p>You can also check your sent mail using the following example.</p>
<pre>$gmail = new GmailReader( 'username@gmail.com', 'password' );
$gmail-&gt;openSentMail();
$email = $gmail-&gt;getEmailSince('Fri, 5 Sep 2008 9:00:00');</pre>
<p>Also, if you use Gmail to apply labels to certain email you can monitor just these emails as well.</p>
<pre>$gmail = new GmailReader( 'username@gmail.com', 'password' );
$gmail-&gt;openMailBox( 'MYLABEL' );
$email = $gmail-&gt;getEmailSince('Fri, 5 Sep 2008 9:00:00');</pre>
<p><span style="font-size:130%;"><span style="font-weight: bold;">Here is the full GmailReader Class File.</span></span></p>
<pre name="code" class="php">class GmailReader
{
var $mbox;

function GmailReader( $user, $pass )
{
 $this-&gt;mbox = imap_open("{imap.gmail.com:993/imap/ssl}INBOX",$user,$pass)
  or die("can't connect: " . imap_last_error());
}

function openSentMail()
{
 imap_reopen($this-&gt;mbox, "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail" )
  or die("Failed to open Sent Mail: " . imap_last_error());
}

function openMailBox($mailbox)
{
 imap_reopen($this-&gt;mbox, "{imap.gmail.com:993/imap/ssl}$mailbox" )
  or die("Failed to open $mailbox: " . imap_last_error());
}

function getMailboxInfo()
{
 $mc = imap_check($this-&gt;mbox);
 return $mc;
}

/**
 * $date should be a string
 * Example Formats Include:
 * Fri, 5 Sep 2008 9:00:00
 * Fri, 5 Sep 2008
 * 5 Sep 2008
 * I am sure other's work, just test them out.
 */
function getHeadersSince($date)
{
 $uids = $this-&gt;getMessageIdsSinceDate($date);
 $messages = array();
 foreach( $uids as $k=&gt;$uid )
 {
  $messages[] = $this-&gt;retrieve_header($uid);
 }
 return $messages;
}

/**
 * $date should be a string
 * Example Formats Include:
 * Fri, 5 Sep 2008 9:00:00
 * Fri, 5 Sep 2008
 * 5 Sep 2008
 * I am sure other's work, just test them out.
 */
function getEmailSince($date)
{
 $uids = $this-&gt;getMessageIdsSinceDate($date);
 $messages = array();
 foreach( $uids as $k=&gt;$uid )
 {
  $messages[] = $this-&gt;retrieve_message($uid);
 }
 return $messages;
}

function getMessageIdsSinceDate($date)
{
 return imap_search( $this-&gt;mbox, 'SINCE "'.$date.'"');
}

function retrieve_header($messageid)
{
   $message = array();

   $header = imap_header($this-&gt;mbox, $messageid);
   $structure = imap_fetchstructure($this-&gt;mbox, $messageid);

   $message['subject'] = $header-&gt;subject;
   $message['fromaddress'] =   $header-&gt;fromaddress;
   $message['toaddress'] =   $header-&gt;toaddress;
   $message['ccaddress'] =   $header-&gt;ccaddress;
   $message['date'] =   $header-&gt;date;

   return $message;
}

function retrieve_message($messageid)
{
   $message = array();

   $header = imap_header($this-&gt;mbox, $messageid);
   $structure = imap_fetchstructure($this-&gt;mbox, $messageid);

   $message['subject'] = $header-&gt;subject;
   $message['fromaddress'] =   $header-&gt;fromaddress;
   $message['toaddress'] =   $header-&gt;toaddress;
   $message['ccaddress'] =   $header-&gt;ccaddress;
   $message['date'] =   $header-&gt;date;

  if ($this-&gt;check_type($structure))
  {
   $message['body'] = imap_fetchbody($this-&gt;mbox,$messageid,"1"); ## GET THE BODY OF MULTI-PART MESSAGE
   if(!$message['body']) {$message['body'] = '[NO TEXT ENTERED INTO THE MESSAGE]nn';}
  }
  else
  {
   $message['body'] = imap_body($this-&gt;mbox, $messageid);
   if(!$message['body']) {$message['body'] = '[NO TEXT ENTERED INTO THE MESSAGE]nn';}
  }

  return $message;
}

function check_type($structure) ## CHECK THE TYPE
{
  if($structure-&gt;type == 1)
    {
     return(true); ## YES THIS IS A MULTI-PART MESSAGE
    }
 else
    {
     return(false); ## NO THIS IS NOT A MULTI-PART MESSAGE
    }
}

}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/gmailreaderphp-a-php-imap-class-for-reading-your-gmail-account/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Flex Component : Yet Another Flex File Upload Component</title>
		<link>http://www.kerkness.ca/flex-component-yet-another-flex-file-upload-component/</link>
		<comments>http://www.kerkness.ca/flex-component-yet-another-flex-file-upload-component/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 02:42:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Components]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=50</guid>
		<description><![CDATA[There are several Flex upload components floating around and I gave most of them a good look over but I was not able to find one which suited my needs so I decided to make my own.  Here it is for you to enjoy.
Click here to view a demo
(demo lets you upload up to [...]]]></description>
			<content:encoded><![CDATA[<p>There are several Flex upload components floating around and I gave most of them a good look over but I was not able to find one which suited my needs so I decided to make my own.  Here it is for you to enjoy.</p>
<p><a href="http://www.kerkness.ca/flexexamples/uploader/UploadExample.html">Click here to view a demo</a><br />
<span style="font-size:85%;">(demo lets you upload up to 3 files at a time with a max size of 4 megs each)<br />
</span><br />
<a href="http://www.kerkness.ca/flexexamples/uploader/srcview/index.html">Click here to view the source</a></p>
<p>The usage of the component is pretty straight forward. You can set some properties to define what types of files can be uploaded and you direct the &#8216;upload&#8217; towards a server side script.  In my demo I send the files to a basic PHP script ( which I&#8217;ve included below ).  The script handles one file at a time and is expected to report back either the string &#8217;successful&#8217;  or an error message which is displayed.</p>
<p><span style="font-weight: bold;font-size:180%;">Properties</span></p>
<p>The following public properties can be set to customize the use of this component</p>
<ul>
<li><span style="font-style: italic;">uploadButtonLabel</span> : String = &#8216;Upload&#8217;<br />
This is the label used for the &#8216;upload&#8217; button.</li>
<li><span style="font-style: italic;">selectButtonLabel</span> : String = &#8216;Select File(s)&#8217;<br />
This is the label used for the &#8217;select&#8217; button.</li>
<li><span style="font-style: italic;">removeButtonLabel </span>: String = &#8216;Remove Selected File&#8217;<br />
This is the label used for the &#8216;remove&#8217; button.</li>
<li><span style="font-style: italic;">maxFileCount </span>: Number = 3<br />
This sets the number of files a user is allowed to upload</li>
<li><span style="font-style: italic;">maxFileSize </span>: Number = 1<br />
This sets the maximum file size (in megs) for each individual file. Default is 1 meg.</li>
<li><span style="font-style: italic;">requestUrl</span> : String = &#8221;<br />
This is the URL for your PHP/ASP/ColdFusion/Other script which you will send the uploaded files to. THIS PROPERTY MUST BE SET !</li>
<li><span style="font-style: italic;">allowOnlyImages </span>: Boolean = false<br />
If set to &#8216;true&#8217;  the user will only be able to select image files</li>
<li><span style="font-style: italic;">allowOnlyText </span>: Boolean = false<br />
If set to &#8216;true&#8217;  the user will only be able to select text files.</li>
<li><span style="font-style: italic;">allowAllFiles </span>: Boolean = true<br />
If set to &#8216;true&#8217; the user can upload images or text files.</li>
<li><span style="font-style: italic;">barColor </span>: String<br />
Allows you to style the progress bar</li>
</ul>
<p><span style="font-size:180%;"><span style="font-weight: bold;">Events</span><br />
</span><br />
The component has one event &#8216;uploadComplete&#8217;  which is triggered if all files are uploaded successfully.</p>
<p><span style="font-size:180%;"><span style="font-weight: bold;">The PHP Handler</span><br />
</span><br />
Following is the source for the simple PHP script which I am using in the demo. In this script I am just confirming that the file was uploaded successfully and then I am deleting it.  Since this is a public demo I don&#8217;t want people filling up my server with unwanted images.  In your script you would most likely copy the FILE to a more permanent location and possibly do a little more validation.  The script needs to echo the string &#8217;success&#8217;  or echo an error message.</p>
<pre>
$hasError = false;foreach( $_FILES as $i=&gt;$file ){
   if ( $file['error'] ){
      $hasError = true;
  }
  /**
    * Because this is a public example. I am just going to immediately delete
    * the file which was uploaded.  In a regular application you would copy the file
    * from it's temporary location to it's permament location.
    */
   unlink( $file['tmp_name'] );
 }
if ( ! $hasError ){
  echo('success');
} else {
  echo('Stick custom error message here');
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/flex-component-yet-another-flex-file-upload-component/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Setting up a Kiosk Watchdog for your Ubuntu Blackbox Kiosk</title>
		<link>http://www.kerkness.ca/setting-up-a-kiosk-watchdog-for-your-ubuntu-blackbox-kiosk/</link>
		<comments>http://www.kerkness.ca/setting-up-a-kiosk-watchdog-for-your-ubuntu-blackbox-kiosk/#comments</comments>
		<pubDate>Fri, 09 May 2008 02:36:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[kiosk]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=44</guid>
		<description><![CDATA[Previously I put together a post which describes how to build a Kiosk computer using Ubuntu, Blackbox and Firefox.  I&#8217;m following that up with details on how to monitor the kiosk so that you can be notified when/if the computer or services fail.  I&#8217;ll break this post into two sections.
1) Monitoring the computer [...]]]></description>
			<content:encoded><![CDATA[<p>Previously I put together a post which describes how to build a <a href="http://kerkness.blogspot.com/2008/04/creating-touch-screen-kiosk-using-flex.html">Kiosk computer using Ubuntu, Blackbox and Firefox</a>.  I&#8217;m following that up with details on how to monitor the kiosk so that you can be notified when/if the computer or services fail.  I&#8217;ll break this post into two sections.</p>
<p>1) Monitoring the computer services and network<br />2) Monitoring FireFox performance</p>
<p><span style="font-size:180%;"><span style="font-weight: bold;">Monitoring the computer services and network</span></span></p>
<p>Because our Kiosk computer is an Ubuntu server and running apache/php/mysql locally there are several open source network and service monitoring programs.  The one I found most suitable for my solution is called <a href="http://www.tildeslash.com/monit/">Monit</a> and I found a very good post at <a href="http://www.ubuntugeek.com/monitoring-ubuntu-services-using-monit.html">Ubuntu Geek</a> describing how to install monit on ubuntu and configure it.  I basically followed the Ubuntu Geek tutorial but made some modifications to the config file.  My revised process is below.
<ol>
<li>Install Monit
<pre>sudo apt-get install monit</pre>
</li>
<li>Configure Monit. Open <span style="font-style: italic; font-weight: bold;">/etc/monit/monitrc</span> in your favorite text editor.  Below is an example of how I set up my own configuration file. It should be pretty self explanatory.
<pre>## Start monit in background (run as daemon) and check the services at 2-minute## intervals.set daemon  120

## Set syslog logging with the 'daemon' facility.set logfile syslog facility log_daemon

## Set list of mailservers for alert delivery.## I use my ISP's SMTP server for better reliability and means## I don't need an smtp server running on my Kioskset mailserver mail.shawcable.com

## Use event queue if mailserver unavailableset eventqueuebasedir /var/monit  # set the base directory where events will be storedslots 100           # optionaly limit the queue size

## You can set the alert recipient hereset alert someone@domain.com

# Monitor Apachecheck process apache2 with pidfile /var/run/apache2.pid

# Action to be taken when apache failsstart program = "/etc/init.d/apache2 start"stop program = "/etc/init.d/apache2 start"# Admin will notify by mail if below the condition satisfied belowif cpu is greater than 60% for 2 cycles then alertif cpu > 60% for 5 cycles then restartif children > 10 then alertif children > 50 then restartif loadavg(5min) greater than 10 for 8 cycles then stopif 3 restarts within 5 cycles then timeoutgroup servers

# Monitor MySQLcheck process mysql with pidfile /var/run/mysqld/mysqld.pidgroup databasestart program = "/etc/init.d/mysql start"stop program = "/etc/init.d/mysql stop"if failed host 127.0.0.1 port 3306 then restartif failed host 127.0.0.1 port 3306 then alertif 5 restarts within 5 cycles then timeout

# Monitor SSH Servicecheck process sshd with pidfile /var/run/sshd.pidstart program = "/etc/init.d/ssh start"stop program = "/etc/init.d/ssh stop"if failed port 22 protocol ssh then restartif failed port 22 protocol ssh then alertif 5 restarts within 5 cycles then timeoutgroup programs

# Check servicescheck system localhostif loadavg (1min) > 4 then alertif loadavg (5min) > 2 then alertif memory usage > 75% then alertif cpu usage (user) > 70% then alertif cpu usage (system) > 80% then alertif cpu usage (wait) > 20% then alert</pre>
</li>
<li>Set Monit to start automagically.  Open the file <span style="font-style: italic; font-weight: bold;">/etc/default/monit</span> and change the <span style="font-style: italic; font-weight: bold;">startup</span> value to <span style="font-style: italic; font-weight: bold;">1</span>.  You can now start monit with the following command.
<pre>sudo /etc/init.d/monit start</pre>
</li>
</ol>
<p>If you want to be able to access Monit&#8217;s web based interface remotely then check out the Ubuntu Geek tutorial for more information.  I am not enabling this ability in my kiosk at present.<br /><span style="font-size:180%;"><br /><span style="font-weight: bold;">Monitoring FireFox performance</span></span></p>
<p>By using Monit we are able to get good alerts regarding the overall health of our Kiosk.  But Monit doesn&#8217;t tell us is how our Kiosk client (Firefox) is behaving. If Firefox starts to eat up a percentage of your kiosk&#8217;s available memory or CPU power you should be notified early.</p>
<p>I myself am not that great at building BASH scripts so I opted to create a PHP script which tests a few conditions and sends an email if Firefox isn&#8217;t running or is using too many resources.  This script can be run as a cron job every few minutes.  My PHP script requires the open source class <a href="http://sourceforge.net/projects/phpmailer">PHPMailer</a> which makes sending email from PHP a snap.</p>
<p>Here is my php script.
<pre> /*** This script gets the CPU and MEM usage of Firefox*/$cpu = 0;$mem = 0;$failed = false;

// Get the PID of firefox$pid = exec('pidof firefox-bin');

// If firefox is running get memoryif ( $pid ){$status_str = exec('ps aux | grep "'. $pid .'" | grep -v grep');$status = explode( ' ', $status_str );// Strip blanksforeach( $status as $k=>$v ){  if ( trim($v) == '' ) unset($status[$k]);}$status = array_values($status);

$cpu = $status[2];$mem = $status[3];

if ( $cpu >= 60 || $mem >= 60 ){   $failed = true;   $message = "Firefox is using $cpu% of CPU and $mem% of MEM";}

} else {

// Firefox is not running$failed = true;$message = "Firefox is NOT running";

}

if ( $failed ){$body = date('l jS of F Y h:i:s A',time() )."nn";$body .= $message;

// Create and Send Emailrequire_once( "/class/phpmailer/class.phpmailer.php");

$mail = new PHPMailer();$mail->From     = ' firefox@mykiosk ';$mail->FromName = " Firefox Status ";$mail->Host     = 'mail.shawcable.com';$mail->Mailer   = "smtp";

$mail->Subject = "Firefox Issue on ArtTouch";$mail->AddAddress(" someone@domain.com ");

$mail->IsHtml(0);$mail->Body    = $body;

// LOG RESULTSif(! $mail->Send() ) {   error_log("There was an error ending Firefox Performance Alert " . $mail->ErrorInfo );}}</pre>
<p>If anyone wants to turn this into a bash script instead of PHP and share it that would be great.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/setting-up-a-kiosk-watchdog-for-your-ubuntu-blackbox-kiosk/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integrating Grand Theft Auto IV with PHP and Flex Applications</title>
		<link>http://www.kerkness.ca/integrating-grand-theft-auto-iv-with-php-and-flex-applications/</link>
		<comments>http://www.kerkness.ca/integrating-grand-theft-auto-iv-with-php-and-flex-applications/#comments</comments>
		<pubDate>Wed, 07 May 2008 20:32:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[gaming]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=42</guid>
		<description><![CDATA[It can&#8217;t be done.  I&#8217;ve tried.
I haven&#8217;t posted any good tips or code snippets in the past week as I&#8217;ve been a little focused on opening up Manhattan in GTA.  Managing priorities has never really been my problem.
]]></description>
			<content:encoded><![CDATA[<p>It can&#8217;t be done.  I&#8217;ve tried.</p>
<p>I haven&#8217;t posted any good tips or code snippets in the past week as I&#8217;ve been a little focused on opening up Manhattan in GTA.  Managing priorities has never really been my problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/integrating-grand-theft-auto-iv-with-php-and-flex-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up a PHP IDE for Ubuntu 7.10</title>
		<link>http://www.kerkness.ca/setting-up-a-php-ide-for-ubuntu-710/</link>
		<comments>http://www.kerkness.ca/setting-up-a-php-ide-for-ubuntu-710/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 07:07:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=36</guid>
		<description><![CDATA[I&#8217;m just posting this so that I have a record of it. I found myself building a new workstation at home and found this info scattered across several blogs when I looked for a refresher on setting up everything I normally use.
First step. Install Eclipse.
sudo apt-get install eclipse
sudo apt-get install sun-java6-jre
sudo update-java-alternatives -s java-6-sun
Next install [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m just posting this so that I have a record of it. I found myself building a new workstation at home and found this info scattered across several blogs when I looked for a refresher on setting up everything I normally use.</p>
<p>First step. Install Eclipse.</p>
<pre>sudo apt-get install eclipse
sudo apt-get install sun-java6-jre
sudo update-java-alternatives -s java-6-sun</pre>
<p>Next install PHPEclipse. you can go to Help &gt; Software Updates &gt; Find and Install and install new features located at the following URL</p>
<pre>http://phpeclipse.sourceforge.net/update/releases</pre>
<p>Finally. Install the RSE (remote system explorer) Plug In so you can easily access a remote Unix server and edit files willy-nilly.  Note you don&#8217;t need to install everything in the TM project.</p>
<pre>http://download.eclipse.org/dsdp/tm/updates/</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/setting-up-a-php-ide-for-ubuntu-710/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple PHP Captcha</title>
		<link>http://www.kerkness.ca/simple-php-captcha/</link>
		<comments>http://www.kerkness.ca/simple-php-captcha/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 23:40:00 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/blog/?p=33</guid>
		<description><![CDATA[One of the contact forms on a business web site I run has been getting a lot of spam attempts submitted to it.  These attempts don&#8217;t go anywhere but it&#8217;s becoming a bit of a hassle anyway.  Time to add some sort of Captcha to the form.
What is Captcha?  (from Wikipedia) A [...]]]></description>
			<content:encoded><![CDATA[<p>One of the contact forms on a business web site I run has been getting a lot of spam attempts submitted to it.  These attempts don&#8217;t go anywhere but it&#8217;s becoming a bit of a hassle anyway.  Time to add some sort of Captcha to the form.</p>
<p>What is Captcha?  (<a href="http://en.wikipedia.org/wiki/Captcha">from Wikipedia</a>) A Captcha is a type of challenge-response test used in computing to determine that the user is not run by a computer. The process involves one computer (a server) asking a user to complete a simple test which the computer is able to generate and grade. Because other computers are unable to solve the CAPTCHA, any user entering a correct solution is presumed to be human. A common type of CAPTCHA requires that the user type the letters of a distorted image, sometimes with the addition of an obscured sequence of letters or digits that appears on the screen.</p>
<p>The term &#8220;CAPTCHA&#8221; was coined in 2000 by Luis von Ahn, Manuel Blum, Nicholas J. Hopper (all of Carnegie Mellon University), and John Langford (then of IBM). It is a contrived acronym for &#8220;Completely Automated Public Turing test to tell Computers and Humans Apart&#8221;, trademarked by Carnegie Mellon University.</p>
<p><span style="font-size:130%;"><span style="font-weight: bold;">Example with code.</span></span></p>
<p>Here is a simple PHP script which will generates a Captcha and logs the response in a _SESSION variable.  It requires GD support on the server.  The script uses a randomly selected background image (img1.jpg, img2.jpg, img3.jpg or img4.jpg) and adds the pass key over top of background.</p>
<p><span style="font-weight: bold;">PHP Captcha Script.</span>
<pre>// Start sessionssession_start();

// Create a random string$fullhash = md5(microtime());

// Select the first 5 characters of string to use as pass code$captcha = substr($fullhash,0,5);

// Select random image filename for use as background$imgrand = rand(1,4);$imgfile = 'img'.$imgrand.'.jpg';

// Create new image using the background image as a template$image =imagecreatefromjpeg($imgfile);

// Set colors to use for Captcha text$txtcolor = imagecolorallocate($image, 255, 255, 255);$shadowcolor = imagecolorallocate($image, 0, 0, 0);

// Layer Captcha text over imageimagestring($image, 6, 21, 11, $captcha, $shadowcolor);imagestring($image, 5, 20, 10, $captcha, $txtcolor);

// Add pass code to _SESSION variable$_SESSION['captchakey'] = $captcha;

// Adjust header and output imageheader("Content-type: image/jpeg");imagejpeg($image); </pre>
<p><span style="font-weight: bold;">Usage Example</span></p>
<p>Inside a form add the following image tag along with an input field for the user to enter the pass code into.  When validating the form check the user submitted info against the _SESSION variable &#8216;captchakey&#8217;
<pre>&lt;img src="captcha.php"&gt;</pre>
<p><a href="http://www.mayberryfineart.com/captcha/captcha.php">Click here to see a demo.<br /></a><a href="http://www.mayberryfineart.com/captcha">Click here to copy my background images.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/simple-php-captcha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
