splitbrain.org

electronic brain surgery since 2001

Recycling DokuWiki

DokuWiki is probably not for everybody. But it is well designed software I'm pretty proud of. This post is to show off a few parts of DokuWiki's innards that might be quite useful for other PHP developers.

UTF-8 library

The first thing that comes to mind is the UTF-8 library. It is a single file containing PHP-only UTF-8 aware functions for usual string functions. Among others it has replacements for strlen, substr, explode, ltrim, rtrim, trim, strtolower and strtoupper. It also features support for stripping special chars and basic romanization (aka. transliteration) for Japanese, Korean, Russian/Cyrillic, Greek, Thai, Sanskrit, Arabic and Hebrew. When the mbstring extension is available it will be used, without it everything is done in pure PHP. The library is available under the LGPL and an extended version is maintained by Harry Fuecks.

HTTP Client Class

Another part worth mentioning and easily to be used in other applications is the HTTP client. It's a class to do HTTP requests with support for POST and GET, custom headers, redirections, cookies, HTTP 1.1 chunked encoding and gzip compression. It also supports SSL and Proxy connections. You can set timeouts, header restrictions and size limits to avoid misuse. Everything is done in PHP - no need for an extension like CURL. The class is GPL licensed. Using it is simple, here is an example:

function fetchimage($url){
  $http = new HTTPClient();
  $http->proxy_host = 'localhost';
  $http->proxy_port = '8080';
  $http->max_bodysize = 1024*1024;
  $http->timeout = 25; //max. 25 sec
  $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
  return $http->get($url); // simple GET request
}

JavaScript and CSS compressors

Another useful bit worth mentioning are the whitespace compressors/obfuscators for CSS and JavaScript. They are helpful to squeze stylesheets and scripts down to 60 to 80% of the original size. Both functions are GPL licensed and can easily be integrated into other projects.

Mail library

And finally I'd like to point out DokuWiki's mail library which provides methods to send correctly encoded UTF-8 mails. This includes encoding from and to headers, as well as subject lines using quoted printable. Sending a UTF-8 mail is as easy as this:

mail_send('Jörg Smörebrød <js@example.com>',
          '☆ Hæppÿ Børthdą¥ ★',
          'This is your special UTF-8 mail: ☺');

Just feed it with valid UTF-8 encoded input and it will make sure the mail is encoded correctly to send it over the wire. The mail lib is GPL as well.

I hope this little walk through was helpful to other programmers, please let me know if you use any part of DokuWiki.

Tags:
dokuwiki, php, utf-8, http, mail
Similar posts: