<?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>Andrew's Tech Musings &#187; Tips</title>
	<atom:link href="http://andrewcurioso.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrewcurioso.com</link>
	<description>Tech, Social Media, PHP, Opinions</description>
	<lastBuildDate>Wed, 12 Oct 2011 19:43:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>RIA: Desktop Notifications in Google Chrome</title>
		<link>http://andrewcurioso.com/2011/01/ria-desktop-notifications-in-google-chrome/</link>
		<comments>http://andrewcurioso.com/2011/01/ria-desktop-notifications-in-google-chrome/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 17:37:36 +0000</pubDate>
		<dc:creator>Andrew Curioso</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[early adopters]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://andrewcurioso.com/?p=596</guid>
		<description><![CDATA[I&#8217;ve seen a number of blog posts floating around today about GMail Desktop Notifications (here, here, here, here and here &#8212; did I miss anyone?). I tried them out myself and they are very useful. Being a rich web applications developer I, of course, wanted to figure out how it works and how I could [...]]]></description>
			<content:encoded><![CDATA[<pre style="display:none"><script type="text/javascript">
<!--
function demoCheck() {
  var c = ( window.webkitNotifications !== undefined );
  if ( !c ) alert("Your browser does not support desktop notifications using this method. Try out the demos in Google Chrome!");
  return c;
}

function demoCheckPermission() {
  if ( !demoCheck() ) return;
  switch ( webkitNotifications.checkPermission() )
  {
    case 0: // PERMISSION_ALLOWED
      alert( "Permission: allowed" );
      break;
    case 1: // PERMISSION_NOT_ALLOWED
      alert( "Permission: not allowed" );
      break;
    case 2: // PERMISSION_DENIED
      alert( "Permission: denied" );
      break;
  }
}

function demoRequestPermission() {
  if ( !demoCheck() ) return;
  webkitNotifications.requestPermission();
}

function demoNotify() {
  if ( !demoCheck() ) return;
  if ( webkitNotifications.checkPermission() == 0 )
  {
    var iconImageUrl = "http://0.gravatar.com/avatar/25c07794aa15e4173b0f8b5c3f66c66b?s=64&#038;d=http://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536%3Fs%3D32&#038;r=G";
    var title = "Andrew sent you a message";
    var subTitle = "Hello world.";

    var notification = webkitNotifications.createNotification( iconImageUrl, title, subTitle );
    notification.show();
  }
  else
  {
    alert( "Please request permissions first." );
  }
}

function demoHtmlNotify() {
  if ( !demoCheck() ) return;
  if ( webkitNotifications.checkPermission() == 0 )
  {
    var url = "http://andrewcurioso.com/wp-content/custom/2011/notification.html";
    var notification = webkitNotifications.createHTMLNotification( url );
    notification.show();
  }
  else
  {
    alert( "Please request permissions first." );
  }
}

function demoTimerNotify() {
  if ( !demoCheck() ) return;
  if ( webkitNotifications.checkPermission() == 0 )
  {
    var iconImageUrl = "http://0.gravatar.com/avatar/25c07794aa15e4173b0f8b5c3f66c66b?s=64&#038;d=http://0.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536%3Fs%3D32&#038;r=G";
    var title = "Andrew sent you another message";
    var subTitle = "This notification will disappear in 10 seconds.";

    var notification = webkitNotifications.createNotification( iconImageUrl, title, subTitle );
    notification.show();
    setTimeout( function() { notification.cancel() }, 10000 );
  }
  else
  {
    alert( "Please request permissions first." );
  }
}
//--></script></pre>
<p>I&#8217;ve seen a number of blog posts floating around today about GMail Desktop Notifications (<a href="http://downloadsquad.switched.com/2011/01/26/gmail-enables-html5-powered-desktop-notifications/?utm_source=feedburner&#038;utm_medium=feed&#038;utm_campaign=Feed%3A+weblogsinc%2Fdownloadsquad+%28Download+Squad%29&#038;utm_content=Google+Reader">here</a>, <a href="http://lifehacker.com/5744356/gmail-now-has-desktop-notifications-baked-in-for-chrome">here</a>, <a href="http://mashable.com/2011/01/27/gmail-desktop-notifications/">here</a>, <a href="http://www.engadget.com/2011/01/27/google-adds-html5-gmail-and-gtalk-notifications-for-the-desktop/">here</a> and <a href="http://googlesystem.blogspot.com/2011/01/gmail-desktop-notifications.html">here</a> &#8212; did I miss anyone?). I tried them out myself and they are very useful. Being a rich web applications developer I, of course, wanted to figure out how it works and how I could use it for my own apps. Here&#8217;s a quick overview of what I found.<br />
<span id="more-596"></span></p>
<p>There is a proposed web notification standard over at the W3C that Google submitted earlier this month (you can find it on the <a href="http://dev.w3.org/2006/webapi/WebNotifications/publish/Notifications.html">W3C site</a>) but from what I can tell, that draft isn&#8217;t implemented in any browsers (including Google Chrome!). But  you can get desktop notifications today without the need for any third-party browser extensions, if you&#8217;re using Google Chrome. You can find the documentation <a href="http://www.chromium.org/developers/design-documents/desktop-notifications/api-specification">over on the Chromium developer site</a>. The rest of this post is a collection of quick examples I put together for using notifications:</p>
<h3>Checking for permission</h3>
<p>It is pretty easy to check to see if the user has allowed notifications for your website. The first example displays an alert depending on the permission level.</p>
<pre class="brush: jscript; title: ; notranslate">
switch ( webkitNotifications.checkPermission() )
{
  case 0: // PERMISSION_ALLOWED
    alert( &quot;Permission: allowed&quot; );
    break;
  case 1: // PERMISSION_NOT_ALLOWED
    alert( &quot;Permission: not allowed&quot; );
    break;
  case 2: // PERMISSION_DENIED
    alert( &quot;Permission: denied&quot; );
    break;
}
</pre>
<p>Try it: <a href="javascript: demoCheckPermission()">Check Permission</a></p>
<p>Unless you skipped ahead and tried the next bit of code, the answer is &#8220;No&#8221; my site doesn&#8217;t have permission to display notifications.</p>
<h3>Requesting Permission</h3>
<p>So before we do anything we need to ask for permission.</p>
<p>This method will only work when responding to a user gesture. Which means you can call it as a direct response to an action taken by the user; but you can&#8217;t call it (for example) on the response to an Ajax function. So if you wanted to call it when your Ajax call returns or when a timer fires, you&#8217;re out of luck. You need to request permission before then.</p>
<pre class="brush: jscript; title: ; notranslate">
webkitNotifications.requestPermission();
</pre>
<p>Try it: <a href="javascript: demoRequestPermission()">Request Permission</a></p>
<h3>Creating and Showing Notification</h3>
<p>Notably, showing a standard browser alert dialog when permission is denied is a terrible user experience. Don&#8217;t do that! This is just for demo purposes.</p>
<pre class="brush: jscript; title: ; notranslate">
if ( webkitNotifications.checkPermission() == 0 )
{
  var iconImageUrl = &quot;http://www.example.com/foo.png&quot;;
  var title = &quot;Hello World&quot;;
  var subTitle = &quot;This is a sample desktop notification.&quot;

  var notification = webkitNotifications.createNotification( iconImageUrl, title, subTitle );
  notification.show();
}
else
{
  alert( &quot;Please request permissions first.&quot; );
}
</pre>
<p>Try it: <a href="javascript: demoNotify()">Show Notification</a></p>
<p>The new notification looks something like this (for those of you not using Chrome and want to see what I am talking about):</p>
<div id="attachment_620" class="wp-caption alignnone" style="width: 318px"><a href="http://andrewcurioso.com/wp-content/uploads/2011/01/Screen-shot-2011-01-27-at-11.30.06-AM.png"><img src="http://andrewcurioso.com/wp-content/uploads/2011/01/Screen-shot-2011-01-27-at-11.30.06-AM.png" alt="Andrew sent you a message" title="Example Google Chrome Notification" width="308" height="80" class="size-full wp-image-620" /></a><p class="wp-caption-text">Example Notification (Google Chrome on OS X)</p></div>
<h3>Creating and Showing an HTML Notification</h3>
<p>You can also create a notification by passing in a URL.</p>
<pre class="brush: jscript; title: ; notranslate">
if ( webkitNotifications.checkPermission() == 0 )
{
  var url = &quot;http://www.example.com/notification.html&quot;;
  var notification = webkitNotifications.createHTMLNotification( url );
  notification.show();
}
else
{
  alert( &quot;Please request permissions first.&quot; );
}
</pre>
<p>Try it: <a href="javascript: demoHtmlNotify()">Show Notification</a></p>
<p>At first this seems like a terrible idea. It could be yet another way to have annoying advertisements pop up on a site. But it&#8217;s not as bad as it sounds since the user has to explicitly allow the notifications and they can be turned off at any time.</p>
<h3>One Final Example</h3>
<p>Now, you probably don&#8217;t want the notification to pop up and stay there forever. So, you can auto-hide it using a timer.</p>
<pre class="brush: jscript; title: ; notranslate">
if ( webkitNotifications.checkPermission() == 0 )
{
  var iconImageUrl = &quot;http://www.example.com/foo.png&quot;;
  var title = &quot;Hello World&quot;;
  var subTitle = &quot;This is a sample desktop notification.&quot;

  var notification = webkitNotifications.createNotification( iconImageUrl, title, subTitle );
  notification.show();
  setTimeout( function() { notification.cancel() }, 10000 );
}
else
{
  alert( &quot;Please request permissions first.&quot; );
}
</pre>
<p>Try it: <a href="javascript: demoTimerNotify()">Show Notification</a></p>
<h3>Wrapping it Up</h3>
<p>I hope someone found this useful. If you do anything cool with this library please let me know in the comments.</p>
<p class="pitfall">Note: this is an experimental API that only works in Google Chrome right now. The API may change at any time and may be different when other browsers decide to implement similar functionality. Use with caution. Always check to make sure <code>window.webkitNotifications !== undefined</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewcurioso.com/2011/01/ria-desktop-notifications-in-google-chrome/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Detecting file size overflow in PHP</title>
		<link>http://andrewcurioso.com/2010/06/detecting-file-size-overflow-in-php/</link>
		<comments>http://andrewcurioso.com/2010/06/detecting-file-size-overflow-in-php/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 03:55:06 +0000</pubDate>
		<dc:creator>Andrew Curioso</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://andrewcurioso.com/?p=232</guid>
		<description><![CDATA[One of the things that separates a good web application from a great one is how gracefully they handle failures. One of the often overlooked cases is when a user attempts to upload a file that exceeds the set PHP upload file size. This article shows how to detect when the user tries to upload [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that separates a good web application from a great one is how gracefully they handle failures. One of the often overlooked cases is when a user attempts to upload a file that exceeds the set PHP upload file size. This article shows how to detect when the user tries to upload a file that is too large and display an appropriate message.<br />
<span id="more-232"></span><br />
This article assumes that you have already set upload_max_filesize, post_max_size, and memory_limit in your php.ini file to appropriate values. It also assumes that you already have a working file upload form. There are plenty of tutorials out there already to get you started. </p>
<p>If you can, you may want to set post_max_size to a low value (say &#8220;1M&#8221;) to make testing easier. </p>
<p>First test to see how your script behaves. Try uploading a file that is larger than post_max_size. If you do you will get a message like this in your error log:</p>
<pre class="brush: plain; title: ; notranslate">
[09-Jun-2010 19:28:01] PHP Warning:  POST Content-Length of 30980857 bytes exceeds the limit of 2097152 bytes in Unknown on line 0
</pre>
<p>If you&#8217;re not careful this can lead to unexpected behavior in your application. The end result can range from silent failure all the way to lost customers.</p>
<h2>Solving the problem</h2>
<p>The PHP documentation provides a hack to solve this problem:</p>
<blockquote><p>If the size of post data is greater than post_max_size, the $_POST and $_FILES  superglobals  are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. &lt;form action=&#8221;edit.php?processed=1&#8243;&gt;, and then checking if $_GET['processed'] is set.<br />
<a href="http://php.net/manual/en/ini.core.php" class="citation">Source: PHP manual</a></p></blockquote>
<p>To be clear, it is suggesting that you pass a value in the query string along with your form. If the value is in the $_GET superglobal and both $_FILE and $_POST are empty then the maximum upload size is exceeded. There are two problems with this approach: it adds extra complexity on the front-end and it can potential give a false positive.</p>
<p>Extra complexity on the front-end means extra documentation and more room for mistakes. And if there is a mistake it may not be caught for a long time (does your QA team routinely upload large files?).  In this case we already have all the data that we need to determine if the maximum file size was exceeded without adding extra complexity and headache for developers.</p>
<p>We know what type of request is being processed, we have the $_POST and $_FILES arrays, and we have the content length as it was passed to the HTTP server from the client.  From that we get this code:</p>
<pre class="brush: php; title: ; notranslate">
if ( $_SERVER['REQUEST_METHOD'] == 'POST' &amp;&amp; empty($_POST) &amp;&amp;
     empty($_FILES) &amp;&amp; $_SERVER['CONTENT_LENGTH'] &gt; 0 )
{
  $displayMaxSize = ini_get('post_max_size');

  switch ( substr($displayMaxSize,-1) )
  {
    case 'G':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'M':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'K':
       $displayMaxSize = $displayMaxSize * 1024;
  }

  $error = 'Posted data is too large. '.
           $_SERVER[CONTENT_LENGTH].
           ' bytes exceeds the maximum size of '.
           $displayMaxSize.' bytes.&quot;;
}
</pre>
<p>The important thing to notice is the &#8220;if&#8221; statement on lines one and two. The example code just sets an error string. Production code might display a message to the user, execute some Javascript (for asynchronous uploads), or pass back a XML or Json object for Flash clients.</p>
<p>I&#8217;ve tested this code with Apache as both a module and as CGI. As far as I know it should work fine with IIS as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewcurioso.com/2010/06/detecting-file-size-overflow-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A method called &#8216;delete&#8217; in Flex and AS3</title>
		<link>http://andrewcurioso.com/2009/07/a-method-called-delete-in-flex-and-as3/</link>
		<comments>http://andrewcurioso.com/2009/07/a-method-called-delete-in-flex-and-as3/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 05:26:30 +0000</pubDate>
		<dc:creator>Andrew Curioso</dc:creator>
				<category><![CDATA[Flex and AS3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://andrewcurioso.com/?p=137</guid>
		<description><![CDATA[You are in for a headache if you have try to call a method or create a member variable with the name of a reserved word in Actionscript. It can lead to such fun situations as having variables called: insert; update; deleteSomething. Because calling the third variable &#8220;delete&#8221; may be logical but it is a [...]]]></description>
			<content:encoded><![CDATA[<p>You are in for a headache if you have try to call a method or create a member variable with the name of a reserved word in Actionscript. It can lead to such fun situations as having variables called: insert; update; deleteSomething. Because calling the third variable &#8220;delete&#8221; may be logical but it is a reserved word so that is out of the question. It gets hairier when you don&#8217;t necessarily have control over the object format (such is often the case with remote calls). I ran into this today when trying to call the &#8220;node_delete&#8221; (or &#8220;node.delete&#8221;) method in Drupal via Services and AMFPHP. This is frustrating so I&#8217;m going to show two situations where you could run into this problem and how I fixed them.<br />
<span id="more-137"></span></p>
<h3>Situation #1: A variable named &#8220;new&#8221; in a dynamic class</h3>
<p>You are creating a dynamic object and you need to use a reserved word as a member variable name but you can&#8217;t.<br />
<span class="syntaxhighlighterContainer">
<pre class="brush: jscript; title: ; notranslate">
var x:Object = new Object;
x.new = &quot;this doesn't work&quot;;
x['new'] = &quot;this works&quot;;
</pre>
<p></span><br />
The first method is a nice way to get <em>1084: Syntax error: expecting identifier before new</em> when you try to compile. Remove that line and use just the second one and you are all set. It is OK to mix and match access methods, as long as you never use dot notation for reserved words.</p>
<h3>Situation #2: A RPC method named delete</h3>
<p>You are making a remote call via the RemoteObject class (such as I was doing with Drupal) and you need to call a method named &#8220;delete&#8221; (or &#8220;new&#8221; for that matter). You naturally try this:<br />
<span class="syntaxhighlighterContainer">
<pre class="brush: jscript; title: ; notranslate">
var ro:RemoteObject = new RemoteObject;
ro.endpoint = &quot;http://www.example.com/amfphp&quot;;
ro.delete( 1234 );
</pre>
<p></span><br />
You will be promptly greeted by the now familiar 1084 error when you try to compile and using a different notation won&#8217;t work. I&#8217;ll break the solution into multiple parts although it could just as easily be chained together:<br />
<span class="syntaxhighlighterContainer">
<pre class="brush: jscript; title: ; notranslate">
var op:AbstractOperation = ro.getOperation('delete');
op.send( 12345 );
</pre>
<p></span><br />
Incidentally the result of the &#8220;send&#8221; method is a AsyncToken object (the same object that &#8220;ro.delete()&#8221; would return if delete were not reserved) which can then be used to add responders.</p>
<p>There you have it. Two quick and easy ways to get around methods and properties with reserved words for names.</p>
]]></content:encoded>
			<wfw:commentRss>http://andrewcurioso.com/2009/07/a-method-called-delete-in-flex-and-as3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Drop shadow tricks in Flex</title>
		<link>http://andrewcurioso.com/2009/06/drop-shadow-tricks-in-flex/</link>
		<comments>http://andrewcurioso.com/2009/06/drop-shadow-tricks-in-flex/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 15:20:11 +0000</pubDate>
		<dc:creator>Andrew Curioso</dc:creator>
				<category><![CDATA[Flex and AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://andrewcurioso.com/?p=53</guid>
		<description><![CDATA[Every display markup language has its frustrating moments. Adobe Flex isn&#8217;t any exception. I ran into a little trouble a while back when trying to put a drop shadow on a HBox component. Here is the effect that I wanted to achieve: It seems easy enough so let&#8217;s give this a try: Usually the dropShadowEnabled: [...]]]></description>
			<content:encoded><![CDATA[<p>Every display markup language has its frustrating moments. Adobe Flex isn&#8217;t any exception. I ran into a little trouble a while back when trying to put a drop shadow on a HBox component. Here is the effect that I wanted to achieve:</p>
<div id="attachment_57" class="wp-caption alignnone" style="width: 321px"><img class="size-full wp-image-57" title="Flex form with drop shadow header" src="http://andrewcurioso.com/wp-content/uploads/2009/05/picture-6.png" alt="Flex form with drop shadow header" width="311" height="216" /><p class="wp-caption-text">Flex form with drop shadow header</p></div>
<p><span id="more-53"></span></p>
<p>It seems easy enough so let&#8217;s give this a try:<br />
<span class="syntaxhighlighterContainer">
<pre class="brush: xml; title: ; notranslate">
&lt;mx:Style&gt;
HBox {
  paddingTop: 5;
  paddingBottom: 5;
  paddingLeft: 5;
  paddingRight: 5;
  dropShadowEnabled: true;
}
&lt;/mx:Style&gt;
</pre>
<p></span></p>
<p>Usually the <em>dropShadowEnabled: true</em> would be enough but in this case you may be surprised if you put the above style into an MXML file with an HBox in it. Drop shadows don&#8217;t work on HBox components without some more tweaking. They don&#8217;t work for VBox, FormItem, Canvas, Grid, and Tile either. But dropShadowEnabled works beautifully to create Flex drop shadows on a component like TextInput.</p>
<p>What sets TextInput apart? It has a border by default. That is the key. Put <em>borderStyle: solid</em> in our style sheet then all the sudden our HBox has a drop shadow. Unfortunately it also has a nice one pixel border as well. To fix that we can set the borderThickness to zero and you&#8217;re done.</p>
<p><span class="syntaxhighlighterContainer">
<pre class="brush: xml; title: ; notranslate">
&lt;mx:Style&gt;
HBox {
  paddingTop: 5;
  paddingBottom: 5;
  paddingLeft: 5;
  paddingRight: 5;
  dropShadowEnabled: true;
  borderThickness: 0;
  borderStyle: solid;
}
&lt;/mx:Style&gt;
</pre>
<p></span></p>
<h3>Quick Two Point Summary</h3>
<ul>
<li>Drop shadows will be hidden if the component doesn&#8217;t have a border.</li>
<li>The border can be zero pixels.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://andrewcurioso.com/2009/06/drop-shadow-tricks-in-flex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

