November 27th, 2008 at 12:53 pm

Dynamic variables

What is a dynamic variable, or a variable variable, as they are sometimes called? When you create a variable, you give it a name. Consider this PHP example:

<?php
   $foo = 'test';
   echo $foo;
?>

The first line will create the variable $foo and assign the string value ‘test’ to it. In the second line, the content of the variable $foo will be printed. So far, nothing exciting. But now consider this:

<?php
   $bar = 'foo';
   $$bar = 'test';
   echo $foo.$bar;
?>

(Note the double $ sign at the beginning of the second line.) What happens here? First, the value ‘foo’ is assigned to the variable bar. In the second line, a variable is created, and this variable’s name will be the content of the variable $bar. This means, the variable $foo is created. Hence, the third line will output testfoo. You can also have dynamic variables in arrays and objects, even as function aliases:

<?php
   $foobar = $foo[$bar];
   $foobar = $foo->$bar;
 
   // want to match case sensitive or not?
   $stripos_func = ($casesensitive) ? 'strpos' : 'stripos';
   var_dump( $stripos_func('ABC', 'ab') ); // true if $casesensitive==false
?>
November 19th, 2008 at 12:29 pm

Lightweight password generator

Here’s a snippet of PHP code that can create passwords. It can create passwords of different lengths, it can use different sets (uppercase and lowercase letters as well as numbers), and it can be told to exclude potentially confusing characters.

<?php
function passgen($length=8, $reqsets='uln', $noconfusing='true')
{
	$length = (int)$length;
	if ($length > 20 || $length < 3) $length = 8;
 
	// some characters look alike. let's optionally exclude them.
	$confusing = ($noconfusing === 'true') ? 'O01Il' : '';
 
	$randchars = $token = '';
 
	$sets = array(
		'u' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
		'l' => 'abcdefghijklmnopqrstuvwxyz',
		'n' => '0123456789'
		// you can even add an own set
	);
 
	if ($reqsets)
	{
		$reqsets = str_split($reqsets);
		foreach ( array_keys($sets) as $availset )
			if ( in_array($availset, $reqsets) )
				$randchars .= $sets[$availset];
	}
 
	if (!$randchars)
		$randchars = implode($sets);
 
	for ($i=0; $i<=$length; $i++)
	{
		do
		{
			$lpos = mt_rand( 0, strlen($randchars)-1 );
			$letter = substr($randchars, $lpos, 1);
		}
		while ( in_array($letter, str_split($confusing) ) );
 
		$token .= $letter;
	}
	return $token;
}
 
echo passgen($_GET['length'], $_GET['sets'], $_GET['noconfusing']);
?>

If you intend to use this in a script, please keep in mind that mt_rand() needs to be seeded/initialized properly.

August 22nd, 2008 at 4:17 pm

On vacation

From August, 23rd till August, 30th, I’ll be on vacation, without any Internet connection at all, and completely unavailable! :) Let’s see if I will ever get acquainted to the Internet stuff when I’m back.

August 14th, 2008 at 1:31 pm

XXLview — a web-based viewer for large images

XXLview is a new software which allows to conveniently view very large images, such as those found in the Wikimedia Commons. You can zoom in/out, and you can navigate within an image. There are very many usage scenarios, but I especially developed XXLview having a large image viewer in the MediaWiki software in mind, i could also imagine it as an additional feature for WordPress gallery plugins. Check out XXLview as well as the WikiView usage example. XXLview is licensed under the MIT license.

July 16th, 2008 at 9:49 pm

WordPress MultiSiteManager released

MultiSiteManager (MSM) is a WordPress plugin which will perform two of the most important tasks of an administrator: backups and upgrades. MSM can manage any number of WordPress installations from one “master” WP installation. (To manage a WordPress website with MSM, you need FTP access to the site.)

Especially in times of mass hacks and surges of WP exploits, I think this plugin is very good news.

Read on and download »

June 17th, 2008 at 11:58 am

Restore SSH Public Key

Today I did something stupid: I overwrote an SSH public key with a file from a different machine:

scp id_rsa.pub user@example.org:~/.ssh

Obviously, I wanted to place the Public Key for the account from one machine on the other one to append it to the authorized_keys file of the target account. Unfortunately, I didn’t bear in mind that I already had a public key file (id_rsa.pub) in this directory. So I had to re-create the Public Key file from the Private Key. This is quite easy, it appears that the SSH developers already thought of this situation. The following code will restore the Public Key file from the Private Key (example for an RSA key):

cd ~/.ssh; echo "id_rsa" | ssh-keygen -y > id_rsa.pub 2>/dev/null
June 11th, 2008 at 11:03 am

Shell script to optimize all tables in all databases (MySQL)

If you have many databases with many write operations, it may be that the table overhead takes a good amount of space. (This is due to the fact that MySQL doesn’t free the space of deleted entries.) With “optimizing” the tables, you can free that space.

Save the following script as /usr/local/sbin/optimizealltables.sh, make it chmod 700 (very important!), then replace YOURPASSWORD with your root MySQL password.

#!/bin/bash
 
MYSQL_LOGIN='-u root --password=YOURPASSWORD'
 
for db in $(echo "SHOW DATABASES;" | mysql $MYSQL_LOGIN | grep -v -e "Database" -e "information_schema")
do
        TABLES=$(echo "USE $db; SHOW TABLES;" | mysql $MYSQL_LOGIN |  grep -v Tables_in_)
        echo "Switching to database $db"
        for table in $TABLES
        do
                echo -n " * Optimizing table $table ... "
                echo "USE $db; OPTIMIZE TABLE $table" | mysql $MYSQL_LOGIN  >/dev/null
                echo "done."
        done
done
May 7th, 2008 at 10:57 pm

Donations Clouds 0.1.1

[lang_de]Das WordPress-Plugin Donations Clouds ist in der Version 0.1.1 verfügbar, die einige kleine Fehler behebt und das Plugin Widget-fähig macht.[/lang_de][lang_en]The WordPress plugin Donations Clouds is available in version 0.1.1 which fixes some minor bugs and adds widgets capability to the plugin.[/lang_en]

April 17th, 2008 at 2:48 pm

Donations Clouds

Your website has cool stuff and encourages people to donate for it, but nobody ever gives a penny? Give them something that they get only for a donation, e.g. a link on your website. However, maintaining a link list is tedious, and it’s not really appealing. Therefore, we are proud to present the donations cloud (similar to a tag cloud) concept along with an easy to use WordPress plugin. Read on to learn how you really get some donations, and how to go about it with your WordPress blog.

April 16th, 2008 at 11:20 am

Clear a sized select field

If you have have a select form element with a size attribute, one common option for the user is to leave it empty (especially in combination with multiple). The problem is, as soon as one or more options are selected, it is quite impossible in most browsers, to not select anything at all. And even if there is a way, it is not easy to accomplish. Try for yourself to deselect all items:

With a little JavaScript magic, we can offer a button to clear the select field:

function nl_clear_select(element)
{
	var opt_length = document.getElementById(element).options.length;
	for (var i = 0; i < opt_length; i++)
		document.getElementById(element).options[i].selected = false;
}

Now try again to clear the select field. Hint: clicking on "Clear selection" will help a lot. ;)

Clear selection

Neat, eh?