Archive for April, 2010

PHP Generate UUID

Here’s one way to generate a UUID (GUID) in PHP

function uuid() 
{   
    // The field names refer to RFC 4122 section 4.1.2
 
    return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
        mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
        mt_rand(0, 65535), // 16 bits for "time_mid"
        mt_rand(0, 4095),  // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
        bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
            // 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
            // (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
            // 8 bits for "clk_seq_low"
        mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node" 
    ); 
}

How To Convert Seconds to Minutes in ActionScript 3

Recently, I’ve been developing a game in ActionScript 3 (which I’ll share later). I needed to have a second timer count down, which is easy enough with the Timer class. The hard part for me was formatting it so it made sense to the player. Here’s what I came up with for converting a second count to minutes (formatted string):

public function convertSecondsToMinutes(seconds:Number):String
{
     var secondsString:String = new String;
 
     if (this.time%60 < 10) // checks to see if it needs a leading zero
     {
          secondsString = "0" + this.time%60;
     }
     else
     {
           secondsString = "" + this.time%60;
           // the double quotes are needed to cast this as a string
     }
     var minutes:String=Math.floor(this.time/60) + &#039;:&#039; + secondsString;
 
     return minutes;
}

HttpPostedFile Maximum File Upload Size

I just ran into a file upload issue where file uploads were reaching the default maximum file upload size of 4MB. It was pretty straight forward to increase the HttpPostedFile maximum upload size, and I set it to 10MB for this site using the following in the web.config file:

<configuration>
 <system.web>
  <httpRuntime maxRequestLength="10240" executionTimeout="90" />
 </system.web>
</configuration>

I’m not a huge fan of ASP.NET. The tools are really handy and in general customizable enough for what I need, but the fact that they seem to be massively overhauled (yet miraculously still not on the cutting edge) with every release yields an efficiency problem. I simply cannot learn a massive new library every couple years, it just doesn’t make sense.

Plus, I love to know HOW things work, instead of just THAT things work.

My Browser Crisis

Firefox has always been Omega in my book. I’ve spent most of my life on a Firefox Missionary for all the IE users who didn’t know better. But recently I feel dirty. Deep down I am questioning my alignment. I start looking at my task manager and see Firefox spitting in my face with all the memory its taking. My Firefox was taking over 500M of memory and that was unacceptable to me. Why should the most common computer program, a browser, be running so inefficiently after this many years of tuning. I fought back with tweaks in the browser config, limiting it’s consumption. And like a fight with an ex-girlfriend, it said “Fine, if that’s the way you want it”. Browsing is painfully slow now. I’m hurt and feel betrayed.

Then the voice in my head asks “What else is out there?”. I’ve been hearing more and more good things about Chrome and recently heard the Web Developer plugin is available on that platform now as well. I would love to see some fast Javscript rendering too. And then I thought I would have never thought before: “What about IE?”.

I am conflicted to say the least. I got myself a copy of Chrome. My computer is running it like its a DOS Program. And if Chrome displays the same as Firefox why shouldn’t I set it as default. Well for now it’s because I am a loyalist to Mozilla but at this rate, change may be on the way.

Getting excited for Wordpress 3.0

I’m going to just come out right away and say I am not a big fan of Wordpress MU. There seems to always be plugin compatibility issues and getting multiple real top level domains to work on one system is troublesome to say the least.

But when I heard in the upcoming Wordpress 3.0 release MU will be merged with that of a single blog install, I got goosebumps to the point where my arm said “Thank you Matt Mullenweg” in braille. I am pumped to know all plugins will be for one platform now and even better all questions and support will be found in 1 forum! I hated having a problem in MU since the MU community is so much smaller but hopefully that will bind with wordpress.org. From the sounds of it we may still have to custom map domains which is a bummer but it may turn out to be easier to do.

Other features announced include better menu system and you will be able to choose a username right from the install. Goodbye “admin”!

Creating a new person in Highrise API

Highrise API is run off of REST and is pretty powerful. Here would be an example of how to create a new person to account with the API.

First you need your script to turn the form values into a valid XML format. Then keep that xml in a variable or file.

From there you can run the cURL command like this:

curl -u 605b32dd:X -H ‘Content-Type: application/xml’ \
-d @newperson.xml http://sample.highrisehq.com/people.xml

The first line here should be easy to understand. The first half is authenticating and the second half is setting the header to XML. But the next line is a bit trickier. The first part is the file that your form values in XML form are located. You can use a variable here too I assume. The url at the end is very important in that it is telling there side of the API to create this new resource with the XML given. There is a different url for almost every different aspect of this API manipulation so to find the right one to use for your circumstance you would be best to look it up in the official documentation.

Once the request has been run you do get a response code back which you can also use logically if needed.

Userfly Analytics for Wordpress

If you haven’t heard about Userfly you should go check it out. It basically records what your visitors are doing on your site with javascript then it allows you to watch a replication of that session. This can be a very useful tool in seeing where your visitors maybe bouncing off your site and you then know what to try to improve.

I created a wordpress plugin that will allow you to add the needed userfly code your blog. Simple to install and use.

Pick it up here

Custom Single Page

In a recent comment, we were asked if there is a way to do the custom single page based on the custom author page. Depending on your Wordpress version, this may or may not be implemented already, but either way, the solution can be found below:

global $wp_query;
$curpost = $wp_query->get_queried_object();
 
if(file_exists('single-'.$curpost->ID.'.php'))
{
    include('single-'.$curpost->ID.'.php');
}
else
{
    //Default single page
}

The basic theory is the same, get the page ID. Check if single-ID.php exists. If it does, include it. If not, do default author code.

Howto Find Files Larger Than 10MB

Recently, I was trying to track down large files on a Linux server.  Here’s an easy way to find files larger than a given size in the current directory.

find -size +10M

It doesn’t get much easier than that.