Nerdalicious Sub-Conscious PHP Debugging

I’ve dreamed in binary before, no big deal. When I was learning it (properly) and working with it a bit at university, I got a little carried away and would write my name on things in binary, that sort of ultra-cool nerd thing. Now I’ve stepped up from binary to PHP, and things appear to have accelerated from there;

Last night, I debugged the code that I was writing before I went to bed, in my sleep! I was working on my soon-to-be-released PHP Atom API package last night, as has been the case on many nights recently, and it got to be pretty late. I showered up, went to bed and all was well.

When I woke up this morning – I had memories of dreaming my way through the code that I had been working on the night before (1,500 lines, so no small feat ๐Ÿ˜‰ ), and in my dream, I had found a style of catching errors on some of my processing which wouldn’t actually catch anything, and would always evaluate as if there was no error. I got up this morning and checked it, and sure enough – I was more coherent in my dream than I was while sitting in front of my PC coding.

My friend Ray dubbed this “nerdalicious” and I think that’s a pretty awesomely descriptive word for it, so I’m calling this ‘nerdalicious sub-conscious code de-bugging’.

Random Hex

I needed some PHP code to randomly generate me a hex value for use as a color in a webpage. Here’s what I came up with (WARNING: Some colors are UGLY!)

function random_hex() {
	$color = '';
	for ($c = 0; $c < 6; $c++) {
		$i = rand(0, 15);
		switch ($i) {
			case 10 :
				$i = 'A';
				break;
			case 11 :
				$i = 'B';
				break;
			case 12 :
				$i = 'C';
				break;
			case 13 :
				$i = 'D';
				break;
			case 14 :
				$i = 'E';
				break;
			case 15 :
				$i = 'F';
				break;
			default :
				$i = $i;
				break;
		}
		$color .= $i;
	}
	$color = '#' . $color;
	return $color;
}

XooMLe Download Finally Available!

After a very long wait, a downloadable version of XooMLe is now available!

This means that you can now download XooMLe and install it on your own PHP-enabled server so that you can use it locally for integrating Google search results (and cache and spelling suggestion power) into your web-based applications.

In the near future, a ‘XooMLe In Action’ page will also be added to the site, showcasing some of the ways that people are making use of XooMLe and Google.

No More HTTP Authentication

Well, it’s official. webpad 3.0 will now use integrated, session-based authentication for users, rather than HTTP Authentication. I’ve changed to this in large part to allow me to use it in CGI mode (which, incidentally, works wonderfully), so webpad is even more portable now. In fact, if you have PHP running in CGI mode, I will be reccommending that you run webpad under that mode.

With the new integrated authentication, when you hit webpad you are presented with a log in screen, where you enter a username/password as normal, then continue to the actual application.

I’m also currently looking at templating (thanks to a previous hack that Brad Choate made to webpad 2.0 which allowed it to selectively edit the contents of a file, only within certain regions (denoted by webpad tags of some sort). I will have this functionality included in the official release of webpad 3.0 Personal Edition, and it will definitely be a part of the Professional release.

Things may have been quiet, but they’re not completely dead! ๐Ÿ™‚

File Append Function

Here’s a useful function that I wrote for PHP – it just opens a specified file and appends a string to it. It’s very good for logging things.

<?php
/**
 * @return boolean
 * @param $file FileNameToWriteTo
 * @param $string StringToWriteToFile
 * @desc Writes specified string to the end of the file with a linefeed attached
 */
function file_append($file, $string) {
	if (is_file($file) && is_writable($file)) {
		$fh = fopen($file, 'a');
		if ($fh) {
			fwrite($fh, $string . "\n");
			fclose($fh);
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}
?>

You might want to consider making the fopen() flags ‘ab’ for binary-safe (now reccommended on php.net) and also changing the ‘\n’ part to the appropriate line-endings for your operating system (\n = *NIX, \r\n = Windows, \r = Mac).

Blosxconfig: PHP Tool To Configure blosxom Plugins

The most common complaints/questions on the blosxom developer’s mailing list all relate to difficulties with plugins, so I have been trying to think of a way to make them a little easier to work with for people. What I came up with is blosxconfig, a single PHP script which works with very basic configuration files to determine what variables a user should have access to and what format they should be in. It will also confirm the existence and readability of the plugin files, and write the modified variables directly into the plugin files.

Download blosxconfig BETA version now!

Here’s the details that I posted to the blosxom list;

WARNING: Back up all you plugins before saving changes via blosxconfig!!!

configure.php is a PHP script (duh!) so you’ll need PHP on your server to run it. You can pass a path to it to look for a specific config file via either a GET or POST request (GET use ?conf=path-to-conf.conf on the querystring, POST use variable name ‘conf’ with a value of a path to a config file). if you don’t pass a value, then blosxconfig will look for a file called blosxom.conf in the same directory as it, or die trying ๐Ÿ˜‰

The config files are very basic, and look a little bit like a Windows ini file, but it’s a custom style; The first line is similar to a unix ‘shebang’ line, and should look like this;

#@/path/to/plugins

where obviously the /path/to/plugins is the full path to where your plugins live – this should be the same as the $blosxom::plugin_dir variable configured in your blosxom cgi executable.

After that line, you have plugin sections which are defined using [plugin_name] – be careful that you use the *exact* name of the plugin, because the value is used for confirming the plugin’s existence ๐Ÿ™‚

Under each plugin section, you define definable variables on a line each, in this format;

variable_name|type|default

variable_name is the actual name of the variable (as written in the script), without the ‘$’ or ‘@’ (in the case of arrays)

type can be one of ‘string’, ‘boolean’ or ‘code’. Arrays should be defined as ‘code’. Strings are enclosed automatically in quotes (“) and all types are followed by a semi colon when written to file. Booleans are presented to the user as a Yes/No select box (0/1 passed as values) and other types are presented as simple text input fields.

default is a value to use by default. make sure this makes sense (so for a boolean make sure it’s 0/1, for a string it’s a string etc). You don’t need to wrap it in quotes or anything, this is handled automatically.


Now, assuming you have a valid config file and blosxconfig can find it, it will load the file, and present you with an interface where you should be able to modify all the values that you spcified as being editable in the config file. *hopefully* blosxconfig will also tell you at this point if plugin files are available (file exists in plugin dir) and if they are not readable or writeable. when you have made you changes – click ‘save’ and the following will happen;

WARNING: Back up all you plugins before saving changes via blosxconfig!!!

  1. All plugin files which are referenced in your config file will be MODIFIED – they will technically be overwritten entirely, since the contents of the file are processed, then the lines that look like they define the variables in question are replaced with new lines, with the new values.
  2. The config file will be updated with the new details that you specified and written back to file.
  3. blosxconfig will load again, with all your new values in place, and messages telling you what happened.

Assuming all goes well, you’ve just used an external interface to reach into your plugin files and modify certain details. The hope is that authors of plugins (or community-minded participants) can supply the simple, 1 – 10 lines worth of config required to add to this configuration file, which can then be distributed with this little application to help people configure their plugins.

Please be aware that I have *NOT* tested this extensively at all, it was hacked together and seemed to work for a couple of plugins that I was running (the ones in the config file included) on a Windows test box, other than that I can’t make any promises. I’d LOVE if some people could try it out on some other plugins, on other systems, and let me know (directly if
you like) how it goes. I’ll fix any problems and then release it as public so that other people can use it.

Hope it’s useful for someone out there, and can perhaps eventually make life easier for some newbies (and pro’s alike ๐Ÿ™‚

Oh yeah – did I mention; BACK UP YOUR PLUGINS BEFORE YOU TRY THIS!

On the suggestion of Andreas Banze, I’ve removed one small section which checked to make sure that the plugin files were executable, since as he pointed out, this is not necessary, they are just included in the main operation of the blosxom script, not executed individually.

Download blosxconfig BETA version now!

If You Have A New Version of PHP…

webpad was written quite a while ago now, and the new installations of PHP come by default with some settings which will mean that webpad doesn’t work straight away. The easiest way to get around this (on an Apache server) is to put an “.htaccess” file in your webpad installation directory which contains something like this;

php_flag register_globals on

php_flag magic_quotes_gpc on

The next version of webpad will not require this, but for now it’s the easiest way around the problem.

If anyone knows of any other problems and/or fixes to problems with webpad, please email them in to me using the contact form or email here.

Interest From SearchLoggers Group

I’ve had some initial interest from some of the other members of the searchloggers group in the work that I am doing with the search system on this site. The integration of a controlled vocabulary, best bets and a stand-aloone search engine backend appears to be of interest to others as well ๐Ÿ™‚

My hope is that this can be developed into a stand-alone product that could be wrapped around nearly any search system, simply adding to the functionality of said search system. So far things are looking good, but only really for something written in PHP. I would need to add other options for systems written in Perl, C etc.

In the near future, I will be posting database schemas, some notes on implementation and other bits and pieces about how I am attacking the whole process. Keep an eye on things if you are interested ๐Ÿ™‚

So — PortaBlosx

I was thinking, blosxom is very cool, but I’d like to be able to do a few things in regards to getting something on my blog when I am out and about;

  1. Be able to post from anywhere (i.e. I want a web-based interface of some sort)
  2. Be able to post from my Palm (I built AvantBlog for this reason, why not have AvantBlosx? ๐Ÿ™‚ )
  3. A nifty little dialog-thingo allowing me to make quick posts (very similar to the Palm version) would be nice as well, something I could bookmarklet easily.

So I had a look around, and the first one appears to be pretty much available already, care of PHPetal. After that tho, there doesn’t appear to be a simple, clean interface available that could be hacked to use on Palm and as a simple little bookmarklet dialog.

My idea is basically to just provide the person with a very simple interface, at this stage it would consist of a select box containing a recursed listing of their blog directories, and a textarea where they could enter their post. As per blosxom, the first line would be used as the title, and I would either make up a filename based on the title, or just generate one from a timestamp.

What do you reckon?