Category Archives: Code Snippets

Base64 Binary Decoding in Javascript

Currently all the base64 decoders in javascript are for strings, not suitable for binary data, the common example is if you uses a canvas element and get their base64 representation ( canvas.toDataURL() ) usually you will upload and in the server do the base64 decode, if you want to process the data in javascript you will find the data will got corrupted since it’s processed as string.

This script is using the new javascript array typed, the basic usage is decoding the base64 into an Uint8Array you can call like:

var uintArray = Base64Binary.decode(base64_string);

The other is wrap the Uint8Array into an ArrayBuffer, this is very helpful for example to upload a file through FormData ( formdata.append("file", arraybuffer, "test.png") ), you can use with:

var byteArray = Base64Binary.decodeArrayBuffer(base64_string);

You can check the code here:


Using php for analyzing apache logs

Apache has a nice feature that is to send the log output through a pipe. This avoid to configure syslog or create a listening server in php for syslog forwarding logs.

The changes in apache are really simple, you just need to write something like:

LogFormat "%v %A %D \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" mylog
CustomLog "|/usr/bin/php5 [PATH_TO_SCRIPT]/apache-stdin.php log" mylog
ErrorLog "|/usr/bin/php5 [PATH_TO_SCRIPT]/apache-stdin.php error"

In apache-stdin.php the flow is very simple, you just need to do:

$fp = fopen('php://stdin', 'r');
do {
	//read a line from apache, if not, will block until have it
	$data = fgets($fp);
	$data = trim($data); //remove line end

	if (empty($data)) {
		break; //no more data so finish it

	//process the data
} while(true);


As you can see it’s basically reading a line and processing.

I’ve built a helper script around this at:

Where you can pass an additional param to specify it’s a normal log, or the error log; like in the apache configuration I’ve posted.

You can also configure the log format you are using in apache to get you a simple description like:

$format = '%v %A %D \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"';

With this, it will give you an array like:

'hostname' => '',
'local_ip' =>,
'time_ms' => 0.0002,
'first_line_request' => 'GET / HTTP/1.1',
'status_last' => 200,
'bytes_sent' => 2048

From there you can use for storing in a file (it’s the common behavior of this script), insert into a db, use simpledb, etc.


Recipes for using SVN library in PHP

The SVN library in PHP allows to work with SVN through a working copy or directly to the SVN repository files:

The problem is there is really few documentation for the direct access functions to repository (svn_repos_* and svn_fs_*) the code is almost the same to working in C the libsvn, but also there are few documentation, most of the svn command.

The good is that is really easy to work with SVN, basically you only need to:

  1. call to svn_repos_open($path) for getting a $repo handler
  2. call to svn_repos_fs($repo) to get a $repo_fs handler
  3. get the latest revision through:  svn_fs_youngest_rev($repo_fs);
  4. Get a $transaction through svn_repos_fs_begin_txn_for_commit($repo, $latest_revision, $commiter, $log_message);
  5. Get a $repo_root resource with svn_fs_txn_root($repo_txn);
    With $repo_root will be the most of operations and the $repo_txn will just work for do a commit or abort
  6. do all the operations (create a file/dir, remove, set contents)
  7. Call to svn_repos_fs_commit_txn($repo_txn); if you want to commit, or svn_fs_abort_txn ( $repo_txn ); if you want to abort the transaction

If you read the part of API for SVN: you will see an example for creating a new dir; this code can be easily changed to PHP since the calls are almost the same (replace  svn_repos_fs_begin_txn_for_commit2 for svn_repos_fs_begin_txn_for_commit) I will show you a script for init the repository creating the common paths (branches, tags, trunk) into an empty repository:

//the path to your repository
$svn_path = "/var/lib/svn/repos/testrepo";

//open the repo
$svn_repo = svn_repos_open($svn_path);

//get a fs handler
$repo_fs = svn_repos_fs($svn_repo);

//get latest revision
$latest_revision = svn_fs_youngest_rev($repo_fs);

//generate a transaction, with user and log message
$repo_txn = svn_repos_fs_begin_txn_for_commit($svn_repo, $latest_revision, 'user-repo-label', 'Creating paths');

//get the root respource to this transaction
$repo_root = svn_fs_txn_root($repo_txn);

//create the dirs
svn_fs_make_dir($repo_root, "trunk");
svn_fs_make_dir($repo_root, "tags");
svn_fs_make_dir($repo_root, "branches");

//finally commit the transaction

This script is called: create-repository-paths.php

You will see at end the list of scripts, the order to use is: (with svn commands in the console to check if worked):

#create SVN repository
danguer@rukia:~$ svnadmin create /var/lib/svn/repos/testrepo
#checking is empty
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo

#create paths
danguer@rukia:~$ php create-repository-paths.php
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo

#creating dirs
danguer@rukia:~$ php create-dirs-recursive.php
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1/subdir2
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/subdir1/subdir2/subdir3

#create a new file
danguer@rukia:~$ php write-contents.php
danguer@rukia:~$ svn ls file:///var/lib/svn/repos/testrepo/test/myfile.txt
danguer@rukia:~$ svn cat file:///var/lib/svn/repos/testrepo/test/myfile.txt
Lorem Ipsum, file generated with svn

#remove file
danguer@rukia:~$ php remove-object.php
danguer@rukia:~$ svn cat file:///var/lib/svn/repos/testrepo/test/myfile.txt
svn: File not found: revision 4, path '/test/myfile.txt'



Flex 4: Getting bitmap data on a spark clipped content

Recently I was working on a project where I need to get the bitmap data from a container which had a scroller. From Flex 3 I tried to simply get the content directly from container; but I noticed that I only got the exact image of the scroller (Viewport) instead the full content.

With some playing, I got to the same easy solution; to draw the entire content of the container is to simply disable the clipAndEnableScrolling property over the container I was going to fetch the bitmap data and after drawing, enable again; as internally it just adjust the view properties, you won’t notice the change.

Here is the snippet:

this.containerDraw.clipAndEnableScrolling = false;
this.containerDraw.clipAndEnableScrolling = true;

Where bmSave it’s a BitmapData, you can see an example here:


IE Fix on Fieldset Background Color

Useful information to patch the problem that your background in the fieldset starts before the content in IE, example:

Community MX - Fix on Fieldset Background Color on IE

Link: The IE Fieldset Background Color Bleed Bug