Making Flash and Javascript speaks to each other

I needed a Flash app and my Javascript code to be able to communicate. The Flash needed to call some methods in the Javascript, and in turn the Javascript needed to inject back some data into the Flash.

I let my Flash coder colleague took care of the Flash side, he needed to implement ExternalInterface, register some callbacks methods that I could call and more importantly change some security settings to make it work.

On my side, I just needed to get the objectelement holding the Flash (thanks to swfobject.getObjectById it was pretty easy) and call the callback method registered in Flash on it.

Piece of cake.

Have a look at this blog post from CircleCube, it does a pretty neat job at explaining all of that.

Update :

I finally had some issues with this implementation. It was working perfectly on some servers but not on others. We finally found that calling Security.allowDomain('*.mainhost.com') wasn't working.

You have to explicitly allow each domain/subdomain.

As our code would be deployed on various duplicate domains, we had to manually pass as a flash var the domain to allow.

Migrating from Windows to Ubuntu : KeePass

I'm slowly trying to move my development machine from a desktop Windows Xp to a laptop running Ubuntu. This is quite a long task because there are so much softwares I depend on on my day to day work. Most of them are crossplatform but I often need o find a Ubuntu equivalent, and sometimes it does not exists and I have to run the Windows app using an emulator.

This is exactly the case with KeePass. KeePass 2.0 is an improved version of KeePass 1.0 (obviously), that allows one to save all password in one place. The 2.0 version also permit to save more than single password, virtually anything, and adding little icons to each entry (when you start having 200+ entries, icons are a must have).

Enter Ubuntu world

The official 2.0 version is not supported under linux. There is a linux port, named KeePassX, but it does not support the .kdbx file format used by the official 2.0 version.

So I tried to run the Windows .exe file with Wine at first (I already had Wine installed to run another app), but it wasn't working with KeePass. I tried instead an other emulator, Mono, and running mono KeePass.exe totally worked.

Little tweaks ahead

First of all, I had to install two additionnal packages to make it open without errors : System.Windows.Forms and System.Runtime. So far, so good.

But, I quickly discovered that one of the most usefull features of KeePass, auto-type (Ctrl+V automatically fills a login/password field) wasn't working correctly. There was one more missing dependency : xdotool.

If you are running Ubuntu like me, do not install it from the official packages, this version is completly outdated and won't work. Instead, you have to manually install the latest version. If you are lucky and running a 64bits system, you can download directly the .deb file from here.

If you are running under a 32bits system like me, you'll have to manually install it. First grab the files from this link. Then, install the libxtst-dev package (it is needed for the install to complete), export the content of the .tar.gz file into a temporary directory and execute sudo make install.

Finally !

Now, you got KeePass 2.0 working under Ubuntu, and installed all dependencies needed to run the auto-type feature.

In a Windows environment, one can change KeePass preferences so that closing it only send it to the tray bar.This option does not seem to be working under Linux, KeePass still shows in the task bar even when minimized. I spent some time trying to fix that too, tetsing AllTray and other tray softs for linux but didn't managed to get what I wanted.

Differences between Mercurial and Subversion

People at my current job are using Subversion, and our project will be tracked using it. I never used Subversion before, the only versionning system I ever used was Mercurial. And moving from one to the other meant changing a lot of reflexs I had.

Here is a little list of changes, mostly as a reminder for myself.

Directories

Mercurial uses a single .hg directory at the root of your project to store all your project history while Subversion adds multiple .svn directories in each directory, to track history changes to that directory only.

I prefer the Mercurial approach, it keeps all changes centralized in one place. You can simply remove the .hg directory to transform your versionned version into a stand-alone one.

While the Subversion approach litters your app with countless hidden directories, making copy and pasting a real pain.

Centralised vs Distributed

Subversion is centralised while Mercurial is distributed.

As far as I understand the difference, it means that Subversion uses one main directory to store the versionned version and can deploy (export) a specific revision at anytime.

That revision do not hold any history information, it is only a copy of your project at a given time.

On the other hand, each Mercurial repository holds both the current public version and all the history. You do not have to deploy anything anywhere, justĀ update your current repo with data from one of the revision.

Tortoise

I am using both TortoiseSVN and TortoiseHg. When you commit with TortoiseSVN it displays the list of files that where updated since the last commit. If you added new files, they won't show unless the "Show unversionned files" is checked.

In TortoiseHg, all new files are automatically seen in the commit window, as well as a diff. It allows me the easily see what changes where made, and help me write my commit message.

I really like the TortoiseHg vision better.

Tracking directories

Subversion can track empty directory, just by adding them. Mercurial can't. You have to add an empty file in each to allow tracking.

Also, when doing a commit in a specific directory in Mercurial, it will commit the whole repo, while with Subversion it will only commit the current repo. I can see the benefits of both and I'm not quite sure which is better.

Subversion will allow me to do a commit of one special feature by committing only one directory, but the TortoiseHg integration help me doing commits more easily no matter where I'm browsing.

Ignoring files

I have some files in my project I don't want to track (like auto-generated cache files). In Mercurial, all I need is editing the .hgignore file with regexps. The syntax can be a little strange sometimes, it took me a while to correcly understand it, but it definitely works.

On Subversion, I can add files to the ignore list so they don't show as "unversionned", but I can also add a svn:ignoreproperty to a specific directory to set regexp to files that I don't want to track. The end result is the same, but the way Mercurial handle it with one file is more appealing to me.

Conclusion

Having all history in one dir and all ignore rules in one files seems a better approach to me. I guess on some aspects Mercurial is still more centralised than Subversion.

cakePHP wrapper for EmailComponent::send()

As I already wrote in a previous post, the cakePHP EmailComponentseems to have some quirks, forcing me to add more code to send an email.

I stumbled upon one more issue today : a View poisoning coming from the EmailComponent. Some vars I was passing to my view through the use of a custom Helperwere wipe away if I send a mail in the same request.

It took me some time to track it down, but I finally decided that I'll now wrap calls to $this->Email->send() into a __sendEmail() custom method where I'll add my tweaks.

This will help migrating to a newer cake version easier when those bugs will be fixed.

Easy debug

Debugging emails is not an easy task, but is greatly eased by cake. One can set the delivery type to debugand the mail will be generated like a normal mail, but won't be sent. Instead, its content will be saved in Session.

My wrapper will allow switching from normal to debug mail thanks to an argument.

View cleaning

I'll also clear the ClassRegistryfrom the Viewcreated by the EmailComponent. The Emailview is shared with the display view and this can result in vars being lost or not correctly set. I think all those troubles are gone in cake 2.0 but I haven't tested it yet.

The __sendMail method

Here is the method code. You should add it to your AppController :

function __sendMail($sendAsDebug = false) {
  if (empty($this->Email)) {
    return false;
  }
  // Debug mode
  if (!empty($sendAsDebug)) {
    $this->Email->delivery = 'debug';
  }
  // We force adding the boundaries and header otherwise some webmail (like SquirelMail) won't correctly display them
  $this->Email->_createboundary();
  $this->Email->__header[] = 'MIME-Version: 1.0';
  $this->Email->send();
  // We display debug info
  if (!empty($sendAsDebug)) {
    debug($this->Session->read('Message.email.message'), true);
  }
  // We also need to clear the generated view so our display does not get poisoned by the Email display
  ClassRegistry::removeObject('view');
}

Getting raw JSON POST in PHP

Sometimes you need to post raw POSTdata without following the key/value convention, like when you need to post to some webservices.

Accessing it from PHP can be a little obsure at first, but all that is needed is to read the input buffer by doing : file_get_contents("php://input")

This can be useful when posting raw JSON without adding the overhead of all the keys.