gsnedders

I wrote something very risqué today. It's… about… possibly legal sex!

XHTML/HTML Followup

Tags: , August 18, 2005 (2 comments)

First off, to anyone new around here, I recommend you go read the original XHTML/HTML post and all the comments.

Having read several hundred lines more, I have come to the conclusion that converting XHTML 1.1 to HTML 4.01 Strict is an unnecessary complication. While sending the slightly modified XHTML 1.1 as HTML 4.01 is a complex and debatable topic that I'll try and stay out of in this post, we will, however, need to occasionally touch it.

To start off with, let's quote Steven Pemberton, the chair of the W3C HTML Working Group from the W3C mailing list in 2000:

David,

The HTML WG has discussed this issue: the intention was to allow old (HTML-only) browsers to accept XHTML 1.0 documents by following the guidelines, and serving them as text/html. Therefore, documents served as text/html should be treated as HTML and not as XHTML. There should be no sniffing of text/html documents to see if they are really XHTML.

Note that there are some semantic differences between HTML documents and XHTML documents: there are specific CSS rules that only apply to HTML (and not XHTML), and the DOM has different effects (for instance, the element names are returned in uppercase for HTML, and lower case for XHTML).

Best wishes,

Steven Pemberton
Chair, W3C HTML WG

This clearly lays out the fact that sending XHTML as text/html was what was intended for legacy support, however, the W3C note on XHTML Media Types make it plain and obvious that XHTML 1.1 should not be sent as text/html - this leaves us in a dilemma, we're meant to send XHTML as text/html for legacy support, but not send XHTML 1.1 as text/html.

So, taking our conclusion as we're meant to use a doctype switcher to switch between XHTML 1.1 served as application/xhtml+xml for browsers that allow it, as well as the validator, and serve everything else XHTML 1.0 Strict served as text/html, as long as we meet the HTML Compatibility Guidelines (appendix C of the XHTML 1.0 specification).

So, as ever, I'm going to post a PHP version, and ask anyone who can to port this to other serverside languages, and send it to me, so I can post it here giving them credit.

<?phpif ((stristr($_SERVER["HTTP_ACCEPT"], 'application/xhtml+xml'))  || (stristr($_SERVER["HTTP_USER_AGENT"], 'W3C_Validator')) || (stristr($_SERVER["HTTP_USER_AGENT"], 'WDG_Validator'))) {$mime = 'application/xhtml+xml';} else {$mime = 'text/html';}header ("Content-type$mime");if ($mime == "application/xhtml+xml") {echo '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.1//EN"        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';} else {echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';}?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>  <meta http-equiv="content-type" content="<?php echo $mime; ?>; charset=utf-8" />

Recent Comments

Tags: , June 25, 2005 (7 comments)

Disclaimer: This plugin is no longer maintained. Feel free to take what there is under the GPL and do anything you want with it.

Well, I finally think my first WP plugin is stable enough to be released to the public :), after a couple of months of testing. To my knowledge there are no bugs in the current version (1.0.1), and I think I've tested it enough.

Code

Download .zip

<?php/*Plugin Name: Recent CommentsPlugin URI: http://blog.geoffers.uni.cc/archives/2005/06/24/recent-comments/Description: Retrieves a list of the most recent comments.Version: 1.0.1Author: Geoffrey SneddonAuthor URI: http://geoffers.uni.cc*/ 
 function print_gwp_recent_comments ($no_post = 5, $before = '<li>', $after = '</li>'){    global $wpdb;    $comments = $wpdb->get_results("SELECT `comment_ID` , `comment_post_ID` , `comment_author` FROM `$wpdb->comments` WHERE `comment_approved` = '1ORDER BY `comment_date_gmt` DESC LIMIT 0 , $no_post");    $output = '';    if ($comments)    {        foreach ($comments as $comment)        {            $id = $comment->comment_post_ID;            $output .= $before . '<a href="' . get_permalink($id) . '#comment-' . $comment->comment_ID . '">' . $comment->comment_author . '</a><br /><span class="post">(<a href="' . get_permalink($id) . '" title="Permalink: ' . htmlspecialchars(get_the_title($id)) . '">' . get_the_title($id) . '</a>)</span>' . $after . "\n";        }    }    echo $output;} ?>

Usage

<?php print_gwp_recent_comments(number_of_comments, 'before', 'after'); ?>

Example

<?php print_gwp_recent_comments(10, '<p>', '</p>'); ?>

Parameters

number_of_comments
(integer) Number of comments to display. Defaults to 5.
before
(string) Text to place before the title. Defaults to '<li>'.
after
(string) Text to place after the title. Defaults to '</li>'.

Combating Spam

Tags: , May 19, 2005 (3 comments)

Disclaimer: This is deprecated by my key spam plugin (which is no longer maintained).

Stage 1

  1. Open /wp-content/themes/yourtheme/comments.php
  2. Find name="author"
  3. Replace author with any old unlikely string
  4. Make a note of your any old unlikely string
  5. Save and Close /wp-content/themes/yourtheme/comments.php

Stage 2

  1. Open /wp-comments-post.php
  2. Find trim($_POST['author']);
  3. Replace author with your any old unlikely string (must be the same string as in stage 1)
  4. Save and Close /wp-comments-post.php

Easy CSV

Tags: March 24, 2005 (0 comments)

This more or less does exactly what it says on the tin: parse CSV into a PHP array. It has serveral advantages over fgetcsv, such as, even if auto_detect_line_endings is set as false in php.ini, it can still cope with all Windows, Mac and *nix line breaks. As basslines at gmail dot com says, it also can't cope with the PHP escape character in the CSV, while this can. That's all I can think of off the top of my head...

To call the function, you can use something like easy_csv (file_get_contents('2005.csv')); if your CSV uses the default "," delimiter and is at 2005.csv, to specify a non-default delimiter, you would use something like easy_csv (file_get_contents('2005.csv'), ';'); if you're CSV uses the ";" delimiter.

You can, of course, put the CSV directly in the function, like easy_csv ('Australia, Malaysia, Bahrain, San Marino, Spain, Monaco');

As for the code itself, here you go:

<?php // Define the function easy_csvfunction easy_csv ($csv, $delimiter = ',') { // If it's got a \n line break (Windows style) then seperate lines into an enumerated arrayif (strpos($csv, "\r\n")) {$csv_lines = explode("\r\n", $csv); // If it's got a \r line break (Mac style) then seperate lines into an enumerated array} elseif (strpos($csv, "\r")) {$csv_lines = explode("\r", $csv); // If it's got a \n line break (*nix style) then seperate lines into an enumerated array} elseif (strpos($csv, "\n")) {$csv_lines = explode("\n", $csv); // If it's not got any of the above line breaks, then just dump the whole thing into $cvs_lines} else {$csv_lines = $csv;} // If $cvs_lines is an array (aka. has more than one line)if (is_array($csv_lines)) { // Set $i to 0$i = 0; // Loop through the linesforeach ($csv_lines as $csv_line) { // Put each piece of data as part of the array$csv_lines[$i] = explode($delimiter, $csv_line); // Add 1 to $i$i++; // End foreach ($csv_lines as $csv_line)} // If it isn't an array (aka. has one line)} else { // Put each piece of data as part of the array$csv_lines = explode($delimiter, $csv_lines);} // Return the array of datareturn $csv_lines; // End the function easy_csv} ?>

It produces an array like:

Array
(
[0] => Array
(
[0] => Australia
[1] => Malaysia
[2] => Bahrain
[3] => San Marino
[4] => Spain
[5] => Monaco
[6] => Europe
[7] => Canada
[8] => USA
[9] => France
)

[1] => Array
(
[0] => Britain
[1] => Germany
[2] => Hungary
[3] => Turkey
[4] => Italy
[5] => Belgium
[6] => Brazil
[7] => Japan
[8] => China
[9] => Total
)

)

MyDB

Tags: March 11, 2005 (0 comments)

This is the first real project I've started working on, although it is still primarily for myself, it doesn't mean I'll be compromising things for other people using it just for me. Although this is only version 0.8.1, it already has some features that I wouldn't of put in if I was doing it just for myself.

Anyhow, time for more detail, like what the hell it is. MyDB is a lightweight, yet powerful, database class. It grew out of part of Location Unknown, I was looking at various database engines, but there was always something wrong, I ended up deciding that I'd write my own. It took me around a day to plan and code the original MySQL version. After a couple of days of thinking about it, I decided to untie it from MySQL, adding support for other databases, such as PostgreSQL and SQLite. This lead to the class getting two extra functions, to keep one of the principals I had set myself when I decided to untie it from MySQL - Make sure as little as possible has to be changed when changing what database server you're using.

That pretty much tells you almost everything you need to know about it in one paragraph. But that doesn't mean I've got nothing more to say.

One of the principles I set out right in the very beginning was it would have very clear, commented code, as to make it easy for other people to build upon it, and hopefully release it to the web developer community. Modify it to your need, and if you're kind, release your code (in accordance with the license, of course).

Personally, I doubt I'll ever need anything more than MySQL, but I'm going to try and support PostgreSQL (anyone who can give me PostgreSQL hosting will be thanked) and SQLite, one of the main principles I set out when I decided to make it support more than one database server, was I'd make it so you have to change as little as possible, and that's likely to just be the connect function...

What licence am I going to release it under? I want this to be open source, yet I still want control over it, I decided to go with the Creative Commons Attribution-NonCommercial-ShareAlike 2.0 License. I'll explain my reasons for each part:

Attribution
I don't want this script to be slightly modified, and then be given no credit at all
Noncommercial
Many complex reasons, although, I'd like to say, anybody who wants to use it commercially, please do not hesitate to contact me
Share Alike
To stop people from changing the license to one that I count as inappropriate

Please, check it out, download it, or at very least visit it's website at http://mydb.geoffers.uni.cc/.

Page:  1 2 3