July
7

I have a short code snippet to share, if you want to do simple date calculations using the Smarty template engine. As a very short preamble, I've used Smarty off-and-on for eight years, and it's great. I find its simplicity and power outweigh bloated frameworks on a regular basis.

If you need the date for tomorrow, you can do it in two ways.

The first way is to create the variable in the "code behind", assign it to the template and then you can use it:

In PHP:

$smarty->assign('tomorrow', strtotime('+1 day'));

In Smarty template (or use a date format of your choice):

{$tomorrow|date_format:"%Y-%m-%d"}

The second way is done in the template alone. This is useful if you did not, or did not want to, assign a variable in the code behind. It is also useful if you wanted to use multiple dates (tomorrow, the next day, next week, etc.), or you could even use it in a loop:

In Smarty template:

{math equation=x+y x=$smarty.now y=86400 assign=tomorrow}
{$tomorrow|date_format:"%Y-%m-%d"}

This uses the UNIX timestamp value of $smarty.now, and adds 86,400 seconds to it. Unfortunately you cannot easily add exactly one month - or one year - using this method because each month or year could have a different number of seconds in it.

In future, I plan to say a little more about Smarty and its virtues.

2
June
16

HTML5 and great expectations

It's a busy day full of chatter on the InterWeb about HTML 5 and its proposed features. "Could it kill Flash and Silverlight", as supposed by Paul Krill at InfoWorld?

One thing is certain: it is not vaporware. After a short search, I found this example implementing a coverflow-style animation from Adnan Osmani (nice work!!) http://blarnee.com/wp/an-improved-javascript-coverflow-demo-using-canvas-and-html5/ If you have an HTML 5 compatible browser - it worked in my Firefox version 3.0.11 - then give it a try. It replicates the animation you'd previously require Flash to do.

My prediction is not can it kill Flash, but when will it kill Flash. Open standards, after gaining momentum, are unstoppable. I'm sure companies like Adobe realize this, and the newer Flash application software will just generate HTML 5 compatible elements. What provides a heavy anchor on progress is how slow users are to upgrade their web browser to be compatible with the new features.

Comments Off
June
4

Today I was waiting for files to upload to the server in order to upgrade Drupal (in fact it is Acquia Drupal) and CiviCRM for Drupal, and while I was waiting I started writing this.

If you don't know what those software programs are, I will not explain here - you can go and read about them. What matters is they are web applications, meaning they run on a web server and you use them in your web browser.

Many computer users - hundreds of millions of them - have from time to time installed software or upgraded software on their own computers. On Microsoft Windows, Mac, and others it's a fairly painless procedure of clicking the "Next" button as you confirm some choices. The computer does some work, you may restart the computer, and 19 times out of 20 you're good to go. Web applications are not that simple.

The general process in upgrading web application software is:

  • Backup the files, and the database (basically backup every single thing you can find)
  • Delete the old files, which sometimes means all the files
  • Upload the new files
  • Upload the themes and customizations you had made to your system (sometimes this must be done after running upgrade scripts)
  • Run an upgrade script, to upgrade your database. Sometimes, it may even help you merge the customizations you made to templates and files, such as how phpBB3 does).
  • Add back all the customizations you may have done (if they were wiped out), and confirm everything is running correctly. You may use some tools to help you merge the new files with the ones you customized.

These usually require any combination of FTP, tar, gzip, zip, ssh, svn, SQL, Linux, and lots of specific knowledge to debug problems that may arise. There's typically no one click install, and if there is, it may work correctly 2 out of 3 times.

The web application software, through its available functions and possible customizations, are very complex. I dread having to upgrade the software because of the possible headaches that arise. But, I don't think it should necessarily any easier to upgrade it. Making it easier would lower the bar of expertise required to upgrade the software, and lowering that bar makes those who should not be upgrading the software themselves to think "it's not that hard, why don't I do it myself".

Unfortunately having a difficult upgrade process causes a steep learning curve for software administrators, and ... it's probably really scary for many people. When a security update is released, they are dependent on hard to find experts to help them.

Comments Off
June
2

There are so many adages that apply just as well to computers and software as anywhere else in life: be prepared! An ounce of prevention is.... The wise ones will use prevention to their advantage.

For example, if you run a hosting server, and a users' domain name is about to expire, would you rather:

  1. Send a short email or make a phone call to check it will be renewed by them? Or,
  2. Wait until the weekend, the web site may or may not go down. If it goes down, you may be scrambling to try and fix it.

You can think of it in terms of a game of chance. Let's say sending the reminder will take 10% of the time and headache a downtime causes. 50% of the time, the domain name doesn't get renewed, and the downtime is caused. Then that ounce of prevention wins.

It's not magic to keep computers running and keep bugs to a minimum, though to some it seems like the "black arts". Developers and IT professionals experience  much they wish never to repeat again. The good ones will learn from their experiences, and if there's a known cause, next time that situation appears they prevent the bad experience from happening again.

As the years go by, this experience and knowledge starts to gel into some kind of wisdom. Wisdom is not just about fixing all the problems you've seen before , but is more like a "gut feeling" about what is a good choice and what is a not so good choice, and what you should or shouldn't do.

A couple years ago I was discussing this with a someone. Well, I was probably just blathering on about how I had fixed something that I noticed was wrong in some software, but this error was not my responsibility. Funny enough, she has done the same thing before, and for the same reason. We didn't fix the error primarily because we care about the customer. We fixed the errors because when it goes disastrously wrong, we will have to fix it, and it will be far more time consuming and frustrating to fix it than to ensure it never happens in the first place.

In software development, this wisdom works in an ongoing basis. We anticipate how the application will evolve over time. Perhaps we'll add a field in a database table for the date a record was created, because there's a good chance we may need it in the future. I'm sure there are hundreds of ways wisdom allows software and computers to run more smoothly. Unfortunately wisdom cannot be taught, so to gain more wisdom always be one the lookout for ways to anticipate disaster (and prevent it).

Comments Off
May
29

Spambayes knows about Acai berry

Posted In: Misc by graham

I've been using the terrific anti-spam plug-in Spambayes for years now in Outlook. It takes a very short time to install and configure. What I like is how I tell it how to "train" itself, pointing it to my good email folders - the "ham" - and keeping my "spam" in a separate folder. After the initial training, I just have to help it make some decisions about junk email suspects, by making the final "ham" versus "spam" decision. In the process, my Spambayes gets even smarter.

Only today did I try the option "Show spam clues for the current message"

The results really show me what Spambayes is using behind the scenes to score my messages, based on my personal "ham" and "spam" folders. Not so surprisingly, not one legitimate email I've received has included the term "Acai", and 506 junk email messages have included it. A little surprising to me, 4222 junk emails have been sent using Microsoft Outlook Express 6.00.2900.2180 (or at least identifying themselves as using that software), but only 6 legitimate emails I've received come from that software.

1
May
1

As a follow-up to my earlier post, recover a FileZilla password online, it only applies to version 2 of FileZilla. FileZilla 3 now does not obsfucate the passwords you save in the software, and relies on the operating system security to protect the plain text passwords (there's a scary thought).

At the time, I didn't quickly find an online form that decrypted a password nice and quickly, though since then I found at least one online form implemented in JavaScript. I have also found other versions of the decryptor function ported to PHP, so I'm adding mine below. It's a complete rewrite of the function in FileZilla. I think it's more staightforward than other solutions, but maybe that's just because I'm familiar with it because... I wrote it. I'll let you, the reader, be the judge of how readable it is. If you get a reason to use it somewhere, let me know.

function filezillaDecrypt ($password) {
  $key = "FILEZILLA1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $keyArray = str_split($key, 1);
  $clearText = '';

  // Remove everything but digits from the password
  $password = preg_replace('/\D/', '', $password);
  // Only continue if a password was supplied
  if($password != '')
  {
    // Split the password into groups of three characters
    $passwordArray = str_split($password, 3);
    $passwordLength = count($passwordArray);
    $keyLength =  count($keyArray);

    $i = 0;
    // The fun decryption happens below, using bitwise operator
    foreach($passwordArray as $char)
    {
      $keyIndex = ($passwordLength+$i)%$keyLength;
      $clearText .= chr($char ^ ord($keyArray[$keyIndex]));
      $i++;
    }
  }
  return $clearText;
}
2
May
1

Last week I attended the MySQL Conference & Expo in Santa Clara. I came away with attaining my Certified MySQL 5.0 Developer designation, a relief once the testing was over. I was MySQL Core certified years ago, and now I'm upgraded.

Overall, the sessions got me thinking outside of the little box I work in - the office - to explore all the great things people are doing that I don't get to see everyday. Some things I really enjoyed hearing about:

  • Memcached
  • Gearman
  • The giving of the "MySQL Acquirer of the Year" award to Oracle, from the previous year's recipient Sun
  • Drizzle
  • RightScale presentation about automation of load balancing in Amazon EC2, to pay an "average" use rate, and not get out of bed to launch new servers
  • Michael "Monty" Widenius' presentation of MariaDB, including opinionated barbs about the new Sun acquision and release announcement of MySQL 5.4 (very entertaining)

Aside from technology, I was in the right place at the right time later in the week in Berkeley to see a documentary film: Anvil! The Story of Anvil (trailer). I was blown away by how good it was, and the trailer does not do it justice. I, and the other thirty or forty viewers in the small theatre, was fortunate the director Sacha Gervasi answered questions after the "sneak preview". He conveyed some stories about his history with Anvil, about the filming, and Dustin Hoffman at the Los Angeles premiere, etc. The trailer portrays it like a real-life Spinal Tap (a good way to get people in the door), but it is funny and serious. I think anyone would enjoy it, so go and see it if you can (i.e. you do not need to be a big heavy metal music fan).

What was so inspiring was to see how Steve 'Lips' Kudlow and Robb Reiner had not given up after so many decades. I wish I was in the Bay area this weekend, because on Sunday (May 3rd), the showing at the Bridge Theatre in San Francisco will conclude with a live Anvil performance! The film is now opening on more screens now, you can check out the web site to find where to see it if you don't live in the select theatres.

Comments Off
April
3

Working as a professional software developer, you will end up with some "legacy" projects. One could call them inherited software; another developer created it, they've moved on, and now you've inherited it.

Sometimes it may be stable and built on sound principles. More often than you'd like, they are buggy and poorly designed, and your challenge as the inheritor is to just keep it running. Starting over from scratch isn't possible, so manage with what you've been given.

This morning, a recent inherited software project finally showed signs of improving. How did it communicate this to me? Instead of generating an average of ten errors overnight during a batch upload, it generated 106 errors. Some people may think more errors is a bad thing, but I do not see it that way. It is a sign that through the small changes I've made to the software - precision pokes and prods - has finally dusted the cobwebs.

It's a little like a house that looks presentable at first, but when you look more closely you find the floor looks clean because the dirt was swept under the rugs, rooms look tidy because all the junk is crammed into the closets, and the paint on the walls hide the mouldering structure. Until you see the flood of error messages, you haven't scratched the surface of the reconstruction.

Comments Off
March
18

IE 4.x minimum

Posted In: Software Development by graham

Though Scott Hanselman, in IE6 Warning - Stop Living In The Past - Get off of IE6, and others would like to see users upgrade to a newer version of Internet Explorer, it may take a long time to happen with some users.

This Government agency - to remain nameless - offers no incentive to upgrade according to this pop-up warning in which requires at least Internet Explorer version 4.0:

Internet Explorer 4.x required

How they continue to keep their application compatible with a 12 year old browser I do not know, but they must be geniuses!

Comments Off
March
13

The elegance of code is still important, for those of us who read and write code. If you use WYSIWYG editors or a code generating IDE then your code will never be elegant, but I digress.

The problem I wanted to solve today was to change the case of strings. For example, to change 'HELLO WORLD' to 'Hello World'. My challenge is to see whether I can do it with MySQL and just the built-in string functions. If it's not possible, it's no matter. I can fall back on a simple PHP script to do this one-time conversion very easily. This is because I've already written code in PHP to do this in the application for all new records that are created, and I just want to change the existing data stored in the table.

Googling a possible MySQL solution, I came across an article on Experts Exchange for the exact same problem, and they also agreed PHP is a better option than using just SQL. What surprised me on reading it was how inelegant the suggested solution was. Let me illustrate:

Their solution:

$field = strtolower($field);
$array=explode(" ",$field);
for ($i=0;$i<count($array);$i++)
{
  substr($array[$i],0,1) = strtoupper(substr($array[$i],0,1));
}
$field = implode(" ",$array);

My solution:

$field = ucwords(strtolower($field));

Which would you rather write? And more importantly, which would you rather read if you had to read someone else's code? Now my solution does have one important difference: I reviewed the PHP manual to see if there was a built-in function that capitalizes the first letter of each word. In simple terms it satisfies the DRY principle, code reuse and using as little code as possible. Sometimes all it takes is a little time and thought to create something so much more beautiful, elegant and readable - time well spent.

Comments Off