WTF! Output buffer for the sake of output buffer

Output buffer control is so cool in PHP that some enjoy using it for the reason of just simply using it.

// some code preparing output content
$outputBuffer = 'some content';
 
// WTF?!
ob_start();
echo $outputBuffer;
ob_flush();
ob_end_clean();

WTF! Superglobals in function declaration

With this post I start new category WTF – What The F**k. I will post here code samples which are raising only one question in my head every time i see them: “WTF?!”

The first WTF is using superglobals as argument names in function declaration in PHP. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
/**
 * Sample function
 *
 * Argument names equal to superglobal variables names which makes this
 * function behavior very unusual... and it`s look as well :)
 */
function my($_GET, $_POST)
{
    var_dump($_GET, $_POST);
}
 
// calling this method with arguments works as expected and actually
// overwrites superglobals inside function making them unavailable
// var_dump($_GET) inside function will dump array passed via arguments
my(array('fake' => 'var'), array());
 
// calling method without arguments leads to a few missing arguments
// warnings, but instead of NULL arguments superglobals get hit
// var_dump($_GET) will dump GET variables from supergloabs
my();

Redirecting output into STDERR in PHP console script

Recently I needed to send output from PHP console script into STDERR instead of STDOUT. Usually you would do it with an I/O redirection “COMMAND >&2″, but in this case it was not possible. A solution was pretty easy though – output buffer callback function which were redirecting output into STDERR and returning zero output for STDOUT.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Output calback function
 * Writes buffer into STDERR instead of STDOUT
 * @link http://de3.php.net/manual/en/function.ob-start.php
 * @param string $output
 * @return string
 */
function SvnHook_outputCallback($output)
{
    fwrite(STDERR, $output);
    return '';
}
 
// set output callback redirecting STDOUT to STDERR
ob_start('SvnHook_outputCallback');

Amazon S3 Reduced Redundancy Storage (RRS)

I’ve just got email from Amazon introducing new storage option they have added to the highly popular S3 service called Reduced Redundancy Storage (RRS). This options actually downgrades S3 by removing data redundancy and providing lower cost than redundant S3. This option absolutely makes sense providing cost-effective solution for storing data which is not important (e.g. cache data) or is securely replicated somewhere else.

The funny thing about RRS introduction email from Amazon is the comparison numbers:

Amazon S3 standard storage is designed to provide 99.999999999% durability and to sustain the concurrent loss of data in two facilities, while RRS is designed to provide 99.99% durability and to sustain the loss of data in a single facility.

So RRS provides *only* 99.99% durability while standard S3 *all* 99.999999999%!

FishEye instant repository update on commit

FishEye is a great piece of software. It is web based repository browser from Atlassian which integrates nicely with other products from this company like JIRA, Crucible, Bamboo etc.

FishEye scans continuously pools repository for new changes and index new revisions when they are available. There is a pooling delay you can configure separately for every repository, in other words there always will be some gap between new commit made and revision got indexed by FishEye. In most cases it should be ok, but sometimes you may need instant repository update after new commit. For example if you are sending notification emails containing link to revision diff in FishEye.

FishEye comes with fisheyectl command line tool which may be used for forcing repository update. If you use Subversion to achieve instant repository update you need to add following line into hooks/post-commit script:

fisheyectl.sh scannow REPOSITORY_NAME

Please note that REPOSITORY_NAME is the name specified in FishEye repository details.

Migrating and syncing samba users

Samba maintains it’s own user and password database. If you want to synchronize samba users with system users pam_smbpass will to the trick.

Debian/Ubuntu:

sudo apt-get install libpam-smbpass

This package installs pam_smbpass.so and modifies pam.d common-auth and common-password scripts to keep system and samba users in sync.

JavaScript is the language of our future… on the backend!

Check out this presentation about successful use of comet server based on node.js in highly scalable realtime web application.

Audio Podcast: Unlearn Your MBA

A very very interesting audio podcast from David Heineimeier Hansson, the creator of Ruby on Rails about his IT business approach and why you should unlearn what you have study in universities.

PHP preg_replace URL

To search for urls and convert them into html links you may use preg_replace() in PHP. Though writing regular expression might be not very easy. Here is an example of regular expression which will catch and replace urls which may be:

  • in the beginning of string,
  • in the end of string,
  • configurable prefix/suffix delimiter,
  • with configurable scheme.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
 
// configurable delimiters and schemes
$delimiters = '\\s"\\.\',';
$schemes = 'https?|ftps?';
 
$pattern = sprintf('#(^|[%s])((?:%s)://\\S+[^%1$s])([%1$s]?)#i', $delimiters, $schemes);
$replacement = '$1<a href="$2">$2</a>$3';
 
$subjects = array(
    'http://localhost',
    'https://example.com',
    ' ftp://example.com',
    'ftps://example.com ',
    'prefix http://example.com',
    'http://example.com suffix',
    'prefix http://example.com/path suffix',
    'prefix http://example.com/path&var=val suffix',
    'prefix http://example.com/path&var=val, suffix',
    'prefix http://example.com/path&var=val. suffix',
    'prefix "http://example.com/path&var=val" suffix',
    'prefix \'http://example.com/path&var=val\' suffix',
);
 
foreach ($subjects as $subject) {
    var_dump(preg_replace($pattern, $replacement, $subject));
}

Continuous Integration and development environment approach