PHP

UTF-8 in LAMP

Software

Here's a handy function returning an array of UTF-8 characters associated to a simple ASCII character. Can be useful when offering users an interface to browse by first character (0-9, A-Z) while actual data retrieval should obviously also include entries starting with a special character.

For example, browsableUtf8Characters("e") returns an array containing e. è, é, ê and ë which can easily be joined and then used in SQL like-queries.

Download browsableUtf8Characters.php or view the source first. (License: none really, enjoy!)

Good i18n code for websites

Software

Today I registered the capsi.nl domain just to make sure I will put some effort into writing some internationalisation code and start offering translated versions of Capsi.

Dutch seemed like the right choice for the first translation, me being able to translate into it myself.

First I will write a prototype i18n function, then use it for menus and other relatively static content and finally I'll have to tackle the dynamic content.

As a sidenote, the reason I won't rely on GeoIP, or at least not solely, are search engine crawlers. They offer no method for content negotiation other than separate URLs and I'd rather score high in results for non-English queries than.. well, not at all.

Technorati pings

Software

Unless I messed up my code, this journal should be sending Technorati pings again. I'm still getting reasonable traffic from the site for older entries so it should be worth it to get listed there again.

(On that topic, I'm considering to return to a more distributed blogging effort, with different blogs for different areas of interest, in order to build more dedicated audiences for each topic. The new profile/music site already is a step towards such a setup, after all.)

Anyway, here's the PHP code that (should) send a proper ping to Technorati:

class Ping
{
function httpPost( $host, $port, $uri, $data, $userAgent = "KikiCMS/0.99" )
{
$url = "http://${host}:${port}${uri}";

$fp = @fsockopen( $host, $port, $errno, $errstr, 5);
if (!$fp)
return false;

$out = "POST ${uri} HTTP/1.1\r\n";
$out .= "Host: ${host}\r\n";
$out .= "Content-Type: text/xml\r\n";
$out .= "Content-Length: ". strlen($data). "\r\n";

fwrite($fp, $out);
fwrite($fp, "\r\n");
fwrite($fp, $data);
fwrite($fp, "\r\n");
fpassthru($fp);

fclose($fp);
return true;
}

function technoRati( $title, $url )
{
$uTitle = htmlspecialchars($title);
$uUrl = htmlspecialchars($url);
$xml = "<?xml version=\"1.0\"?>\n"
$xml .= "<methodCall>\n";
$xml .= "<methodName>weblogUpdates.ping</methodName>\n";
$xml .= "<params>\n";
$xml .= "<param>\n<value>$uTitle</value>\n</param>\n";
$xml .= "<param>\n<value>$uUrl</value>\n</param>\n";
$xml .= "</params>\n";
$xml .= "</methodCall>\n";

Ping::httpPost( "rpc.technorati.com", 80, "/rpc/ping", $xml );
}

Include the class when you save (insert) an entry with Ping::technoRati( "My blog", "myurl" ) and you should be ready to go.

Discovering imagecopyresampled

Software

I wonder how many people are aware that when PHP is compiled with version 2 of the GD library, it supports imagecopyresampled. For some reason I never discovered it until tonight when looking for use smooth scaling for thumbnails. And it was just what I was looking for.. all this time I was still using imagecopyresized ad bitching about it while the solution directly preceeds it in PHP's GD library function reference. Silly me.

Obviously I have instantly applied some updates to Kiki: from now images in user profiles and my photography thumbnails will look much nicer. In fact, I also changed the image quality to 85 from the default 75. Enjoy! (Improvements not retroactively applied, so upload your user image again to enhance it.)

Put HTML in your code!

Software

Most web developers will tell you to separate your HTML from your code. I'm going to defend an unorthodox view: put HTML in your code!

Let us be honest here: HTML is not going away. Web sites aren't going away. The ones from 1993 still render and most sites haven't even upgraded to the latest standard yet, let alone that they would abandon it alltogether for something radically different. So who cares if there are bits of HTML in your web site application? One could argue that separation would make it easier to expand the engine to radically different presentation layers, but in practice.. who seriously needs that? HTML works pretty well for most people. Add CSS and (now that's useful to normalise and separate, also for the sake of graphic designers), and JavaScript (also easy to separate with includes as dynamic JavaScript code should not be necessary with the right hooks) for xmlHttpRequest and web sites can do very much on many media (screen, print, etcetera).

I also don't think that separate HTML templates make maintenance easier. If the code base is properly normalised, that's sufficient aid to a developer to find what needs editing. HTML templates can contain references to variables, arrays, other templates, Javascript calls and often some simple scripting logic (if/else, loops).and so on. Anyone using them will need to have some skills in working with the code base anyway. Might as well keep it in one language without the proxy template scripting and echo the necessary HTML.

In fact, when a change of logic alters the desired output, it's a lot easier to see bit of HTML within code than both having to look for a template and needing to adjust to the new scripting rules in it.

And just look at the state of affairs: most template systems I've come across tend to lead to extreme amounts of duplication. I have one single line in all of my code which is responsible for, say, the <body> tag. If I've called $page->onLoad() with a string, it will attach onLoad="string()" to the tag and the JavaScript will load. This line is in Page::header() which I need to call on all pages. Most template systems don't even normalise these basics and those who do become a game of constant guessing which string is where.

A method in my User class:

function userPage( $urlOnly = false )
{
$url = "/users/". urlencode( strtolower( $this->login ) ). "/";
if ( $urlOnly )
return $url;
else
return "<a href="$url">". htmlspecialchars( $this->login ). "</a>";
}

Perhaps I could move the "/users/" part to a User::baseURL() variable. I believe there is a wrapper that prepends http:// and the domain name that could be handled in a nicer way. But other than that.. why, for a freaking web site, is this considered to be ugly? I bet my web site code and HTML are far easier to maintain than the majority of template-based systems or other attempts to separate code and output.

Seriously.. my code is full of echo statements, even though a lot of output is first pre-generated in strings by class methods. Definitely beats having a gazillion tiny templates or worse, template scripting in yet another hacked together language.

It's wonderful that some people use Java, PHP or Perl to write a scripting language for their web site, but don't try to tell me it's a silly idea to just write the web site directly.

Jani Taskinen Wrap-up

Software

The Baheyeldin Dynasty has a nice wrap up of Jani Taskinen's departure from PHP over Israel, although not providing much more new information than my initial reaction.

It does however offer some open source and geopolitics musings, including Herbert Xu's Debian departure over the Taiwanese flag. Another good example, although not noted, would be Neil Steven's KDE departure over anti-Americanism. I maintain that Jani's case is the most interesting because he's also known for his UN peacekeeping activies, although so far the repercussions of the drama appear limited to the open source community.

PHP turns ugly to anti-semite Jani Taskinen

Software

Jani Taskinen, one of the lead developers of the PHP project, and former UN peacekeeper, has announced to immediately quit the project:

Thank you all for the last 6 years or so. It has been fun (sometimes) and many times not so much fun. Unfortunately I have had enough and I don't want to be associated with this project anymore.

I'm sure most people (the ones who matter) can understand why. If someone doesn't, I could not care less. Take care.

Okay, shit happens. Software development sure can get tedious and we all need a break, or move on to other projects. So why did Jani snap? Rude co-workers? Slipping deadlines? Ever-growing bug lists? Insane development requests? No, none of that.

Four UN peacekeepers were killed by Israeli fire last week, including a Finnish co-worker or even friend of his, so Jani decided he hates Jews:

<_sniper_> hehehehe..
<_sniper_> all other members of the UN security council wanted to condemn Israel for attacking the UN post but USA (freedom and democracy) vetoed it....Israel says the resolution was fair.
<_sniper_> hell yeah..
<_sniper_> NUKE ISRAEL!
<_sniper_> I'm so full of that fucking country..
<Shai-Tan> indeed
<_sniper_> Eye for an eye..I'll kill one Israel officer for one of ours, is that fair?
<_sniper_> I bet I'll be hanged for that.
<_sniper_> They kill one of my brother-in-arms-for-peace..I think I'm entitled to kill one of their nazis.
<_sniper_> Hezbollah, where can I enlist?
<_sniper_> FYI: I don't care at all what anybody thinks about me. I'm going to be openly anti-Israel from now on. This was the last straw for me. Fuck you jews.
<_sniper_> I will also quit this project. As long as it's backed by some Israel company, I don't want to have anything to do with it.
<_sniper_> Good bye.
<-- _sniper_ (~jani@a88-112-115-63.elisa-laajakaista.fi) has left #php.pecl

If that IRC log is correct: what a loser.

First there is a claim that Israel said the resolution was fair. So then why hate the country when it was the US who vetoed? Whatever.

Secondly there is a call for nuclear destruction of Israel. Not cool.

Thirdly note that Hezbollah was very active near the UN positions, so there is ample reason to believe this was a mere accident on Israel's part, possibly unavoidable even.

Last but not least there is support for Hezbollah, more hatred of Jews and finally a decision that PHP has become tainted merely because it is backed by a company (Zend) having to do fuck all with all this yet happens to be Israeli. Obviously making the project as undesired on this planet as those Jews themselves?

Well, good riddance Jani. I know you couldn't care less about what people think of you now, but as long as you don't start to blow up civilians Hezbollah-style.. we couldn't care less.

Still playing with AJAX

Software

If you wonder what's up with me: not a lot, currently. I'm still playing with Asynchronous JavaScript and XML and am slowly deploying some usability changes based on my progress. I'm currently working on a new login form. And don't worry, I won't remove the simple 1995-era POST forms just yet. Some people don't like JavaScript and that shouldn't prevent them from accessing the site.

The most important AJAX lessons I've learned so far: integrating tiny bits of JavaScript into XHTML pages and then handling their XmlHttpRequest mojo properly on the server end in PHP is easy as Paris Hiltonpie. However integrating the actual presentation is a bit more of a challenge. It seems almost impossible to resist the urge to redesign major elements of a web site when going from a stateful to (partially) stateless interface. So far most of the time I spend on AJAX ends up in tweaking public user texts and CSS style definitions.

Don't expect major changes to the site or profiles until I've figured out where I want to take my new-found abilities. I might decide to not bother changing anything at all, but I feel close to shutting down my social life for a few weeks in order to get a complete overhaul of this site out of the way.

© Copyright 1995-2008 Robert John Kaper. All rights reserved.

Tom has more friends but mine are prettier! (#1/1)