cvs: php-news-web / article.php common.inc group.php
| From: | Wez Furlong | Date: | Wed, 11 Sep 2002 10:04:52 +0000 |
| Subject: | cvs: php-news-web / article.php common.inc group.php | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-12414@lists.php.net to get a copy of this message | ||
wez Wed Sep 11 06:04:52 2002 EDT
Modified files:
/php-news-web article.php common.inc group.php
Log:
Add charset support by presenting everything in utf-8.
Should detect an appropriate charset conversion function automagically.
Index: php-news-web/article.php diff -u php-news-web/article.php:1.15 php-news-web/article.php:1.16 --- php-news-web/article.php:1.15 Wed Sep 11 05:22:21 2002 +++ php-news-web/article.php Wed Sep 11 06:04:52 2002 @@ -27,11 +27,6 @@ if ($line == ".\r\n") break; if ($inheaders && ($line == "\n" || $line == "\r\n")) { $inheaders = 0; - if (!$started) { - head("$group: ".format_subject($headers[subject])); - start_article($group,$headers); - $started = 1; - } if ($headers['content-type'] && preg_match("/charset=(\"|'|)(.+)\\1/s", $headers['content-type'], $m)) { $charset = trim($m[2]); @@ -45,6 +40,11 @@ && preg_match("/([^;]+)(;|\$)/", $headers['content-type'], $m)) { $mimetype = strtolower($m[1]); } + if (!$started) { + head("$group: ".format_subject($headers[subject], $charset)); + start_article($group,$headers,$charset); + $started = 1; + } $encoding = strtolower(trim($headers['content-transfer-encoding'])); if (strlen($mimetype) @@ -115,11 +115,13 @@ $line = base64_decode($line); break; } + if (strlen($charset)) + $line = to_utf8($line, $charset); # this is some amazingly simplistic code to color quotes/signatures # differently, and turn links into real links. it actually appears # to work fairly well, but could easily be made more sophistimicated. - $line = htmlentities($line,ENT_NOQUOTES,$charset); + $line = htmlentities($line,ENT_NOQUOTES,"utf-8"); $line = preg_replace("/((mailto|http|ftp|nntp|news):.+?)(>|\\s|\\)|\\.\\s|$)/","<a href=\"\\1\">\\1</a>\\3",$line); if (!$insig && $line == "-- \r\n") { echo "<span class=\"signature\">"; @@ -139,20 +141,20 @@ } if ($inheaders && !$started) { head("$group: ".format_subject($headers[subject])); - start_article($group,$headers); + start_article($group,$headers,$charset); } if ($insig) echo "</span>"; echo "</pre></blockquote>"; -function start_article ($group,$headers) { +function start_article ($group,$headers,$charset) { echo "<blockquote>\n"; echo '<table border="0" cellpadding="2" cellspacing="2" width="100%">'; # from - echo '<tr><td class="headerlabel">From:</td><td class="headervalue">'.format_author($headers[from])."</td>\n"; + echo '<tr><td class="headerlabel">From:</td><td class="headervalue">'.format_author($headers[from], $charset)."</td>\n"; # date echo '<td class="headerlabel">Date:</td><td class="headervalue">'.format_date($headers["date"])."</td></tr>\n"; # subject - echo '<tr><td class="headerlabel">Subject:</td><td class="headervalue" colspan="3">'.format_subject($headers["subject"])."</td></tr>\n"; + echo '<tr><td class="headerlabel">Subject:</td><td class="headervalue" colspan="3">'.format_subject($headers["subject"], $charset)."</td></tr>\n"; echo "<tr>"; # references if ($headers["references"] || $headers["in-reply-to"]) { Index: php-news-web/common.inc diff -u php-news-web/common.inc:1.14 php-news-web/common.inc:1.15 --- php-news-web/common.inc:1.14 Mon May 20 09:28:57 2002 +++ php-news-web/common.inc Wed Sep 11 06:04:52 2002 @@ -1,11 +1,12 @@ <?php /* vim: set ft=php : */ function head($title="php news") { - header("Content-type: text/html; charset=iso-8859-1"); + header("Content-type: text/html; charset=utf-8"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><?echo $title?></title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> @@ -65,19 +66,45 @@ <?php } -# this is pretty cheesy, since it doesn't do real charset handling +if (function_exists("mb_convert_encoding")) { + function to_utf8($str, $charset) + { + return mb_convert_encoding($str, "utf-8", strlen($charset) ? $charset : "iso-8859-1"); + } +} else if (function_exists("recode_string")) { + function to_utf8($str, $charset) + { + if (strlen($charset) == 0) + $charset = "iso-8859-1"; + return recode_string("$charset..utf-8", $str); + } +} else if (function_exists("iconv")) { + function to_utf8($str, $charset) + { + return iconv(strlen($charset) ? $charset : "iso-8859-1", "utf-8", $str); + } +} + function decode_header($charset,$encoding,$text) { if (strtolower($encoding) == "b") { - return base64_decode($text); + $text = base64_decode($text); } else { - return quoted_printable_decode($text); + $text = quoted_printable_decode($text); } + return to_utf8($text, $charset); +} + +function recode_header($header, $basecharset) +{ + if (strpos($header, "=?") === false) + return to_utf8($header, $basecharset); + return preg_replace("/=\\?(.+?)\\?([qb])\\?(.+?)\\?=/ie", "decode_header('\\1','\\2','\\3')", $header); } # this turns some common forms of email addresses into mailto: links -function format_author($a) { - $a = preg_replace("/=\\?(.+?)\\?([qb])\\?(.+?)\\?=/ie", "decode_header('\\1','\\2','\\3')", $a); +function format_author($a, $charset) { + $a = recode_header($a, $charset); if (preg_match("/^\s*(.+)\s+\\(\"?(.+?)\"?\\)\s*$/",$a,$ar)) { return "<a href=\"mailto:".htmlspecialchars(urlencode($ar[1]))."\">".str_replace(" ", " ", htmlspecialchars($ar[2]))."</a>"; } @@ -90,9 +117,9 @@ return str_replace(" ", " ", htmlspecialchars($a)); } -function format_subject($s) { +function format_subject($s, $charset) { global $article; - $s = preg_replace("/=\\?(.+?)\\?([qb])\\?(.+?)\\?=/ie", "decode_header('\\1','\\2','\\3')", $s); + $s = recode_header($s, $charset); $s = preg_replace("/^(Re: *)?\[(PHP|PEAR)(-.*)?\] /i", "\\1", $s); // make this look better on the preview page.. if (strlen($s) > 150 && !isset($article)) { Index: php-news-web/group.php diff -u php-news-web/group.php:1.12 php-news-web/group.php:1.13 --- php-news-web/group.php:1.12 Mon Aug 19 14:08:15 2002 +++ php-news-web/group.php Wed Sep 11 06:04:52 2002 @@ -18,7 +18,7 @@ switch($format) { case 'rss': header("Content-type: text/xml"); - echo '<?xml version="1.0" encoding="iso-8859-1"?>';?> + echo '<?xml version="1.0" encoding="utf-8"?>';?> <rss version="0.93"> <channel> <title>news.php.net: <?echo $group?></title> @@ -27,7 +27,7 @@ <? break; case 'rdf': header("Content-type: text/xml"); - echo '<?xml version="1.0" encoding="iso-8859-1"?>'; + echo '<?xml version="1.0" encoding="utf-8"?>'; ?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -51,6 +51,9 @@ # list of articles $class = "even"; +# TODO: somehow determine the correct charset +$charset = ""; + while ($line = fgets($s, 4096)) { if ($line == ".\r\n") break; $line = chop($line); @@ -63,16 +66,16 @@ case 'rss': echo "<item>\n"; echo "<link>http://news.php.net/article.php?group=$group&article=$n</link>\n"; - echo "<title>", format_subject($subj), "</title>\n"; + echo "<title>", format_subject($subj, $charset), "</title>\n"; echo "<description>", htmlspecialchars(format_author($author)), "</description>\n"; echo "<pubDate>$date822</pubDate>\n"; echo "</item>\n"; break; case 'rdf': echo "<item>\n"; - echo "<title>", format_subject($subj), "</title>\n"; + echo "<title>", format_subject($subj, $charset), "</title>\n"; echo "<link>http://news.php.net/article.php?group=$group&article=$n</link>\n"; - echo "<description>", htmlspecialchars(format_author($author)), "</description>\n"; + echo "<description>", htmlspecialchars(format_author($author, $charset)), "</description>\n"; echo "<pubDate>$date822</pubDate>\n"; echo "</item>\n"; break; @@ -81,9 +84,9 @@ echo "<tr>"; echo "<td class=\"$class\"><a href=\"article.php?group=$group&article=$n\">$n</a></td>"; echo "<td class=\"$class\">"; - echo format_subject($subj); + echo format_subject($subj, $charset); echo "</td>"; - echo "<td class=\"$class\">".format_author($author)."</td>\n"; + echo "<td class=\"$class\">".format_author($author, $charset)."</td>\n"; echo "<td align=\"center\" class=\"$class\"><tt>" . format_date($odate) . "</tt></td>\n"; echo "<td align=\"right\" class=\"$class\">$lines</td>\n"; }
Index: php-news-web/article.php diff -u php-news-web/article.php:1.15 php-news-web/article.php:1.16 --- php-news-web/article.php:1.15 Wed Sep 11 05:22:21 2002 +++ php-news-web/article.php Wed Sep 11 06:04:52 2002 @@ -27,11 +27,6 @@ if ($line == ".\r\n") break; if ($inheaders && ($line == "\n" || $line == "\r\n")) { $inheaders = 0; - if (!$started) { - head("$group: ".format_subject($headers[subject])); - start_article($group,$headers); - $started = 1; - } if ($headers['content-type'] && preg_match("/charset=(\"|'|)(.+)\\1/s", $headers['content-type'], $m)) { $charset = trim($m[2]); @@ -45,6 +40,11 @@ && preg_match("/([^;]+)(;|\$)/", $headers['content-type'], $m)) { $mimetype = strtolower($m[1]); } + if (!$started) { + head("$group: ".format_subject($headers[subject], $charset)); + start_article($group,$headers,$charset); + $started = 1; + } $encoding = strtolower(trim($headers['content-transfer-encoding'])); if (strlen($mimetype) @@ -115,11 +115,13 @@ $line = base64_decode($line); break; } + if (strlen($charset)) + $line = to_utf8($line, $charset); # this is some amazingly simplistic code to color quotes/signatures # differently, and turn links into real links. it actually appears # to work fairly well, but could easily be made more sophistimicated. - $line = htmlentities($line,ENT_NOQUOTES,$charset); + $line = htmlentities($line,ENT_NOQUOTES,"utf-8"); $line = preg_replace("/((mailto|http|ftp|nntp|news):.+?)(>|\\s|\\)|\\.\\s|$)/","<a href=\"\\1\">\\1</a>\\3",$line); if (!$insig && $line == "-- \r\n") { echo "<span class=\"signature\">"; @@ -139,20 +141,20 @@ } if ($inheaders && !$started) { head("$group: ".format_subject($headers[subject])); - start_article($group,$headers); + start_article($group,$headers,$charset); } if ($insig) echo "</span>"; echo "</pre></blockquote>"; -function start_article ($group,$headers) { +function start_article ($group,$headers,$charset) { echo "<blockquote>\n"; echo '<table border="0" cellpadding="2" cellspacing="2" width="100%">'; # from - echo '<tr><td class="headerlabel">From:</td><td class="headervalue">'.format_author($headers[from])."</td>\n"; + echo '<tr><td class="headerlabel">From:</td><td class="headervalue">'.format_author($headers[from], $charset)."</td>\n"; # date echo '<td class="headerlabel">Date:</td><td class="headervalue">'.format_date($headers["date"])."</td></tr>\n"; # subject - echo '<tr><td class="headerlabel">Subject:</td><td class="headervalue" colspan="3">'.format_subject($headers["subject"])."</td></tr>\n"; + echo '<tr><td class="headerlabel">Subject:</td><td class="headervalue" colspan="3">'.format_subject($headers["subject"], $charset)."</td></tr>\n"; echo "<tr>"; # references if ($headers["references"] || $headers["in-reply-to"]) { Index: php-news-web/common.inc diff -u php-news-web/common.inc:1.14 php-news-web/common.inc:1.15 --- php-news-web/common.inc:1.14 Mon May 20 09:28:57 2002 +++ php-news-web/common.inc Wed Sep 11 06:04:52 2002 @@ -1,11 +1,12 @@ <?php /* vim: set ft=php : */ function head($title="php news") { - header("Content-type: text/html; charset=iso-8859-1"); + header("Content-type: text/html; charset=utf-8"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><?echo $title?></title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> @@ -65,19 +66,45 @@ <?php } -# this is pretty cheesy, since it doesn't do real charset handling +if (function_exists("mb_convert_encoding")) { + function to_utf8($str, $charset) + { + return mb_convert_encoding($str, "utf-8", strlen($charset) ? $charset : "iso-8859-1"); + } +} else if (function_exists("recode_string")) { + function to_utf8($str, $charset) + { + if (strlen($charset) == 0) + $charset = "iso-8859-1"; + return recode_string("$charset..utf-8", $str); + } +} else if (function_exists("iconv")) { + function to_utf8($str, $charset) + { + return iconv(strlen($charset) ? $charset : "iso-8859-1", "utf-8", $str); + } +} + function decode_header($charset,$encoding,$text) { if (strtolower($encoding) == "b") { - return base64_decode($text); + $text = base64_decode($text); } else { - return quoted_printable_decode($text); + $text = quoted_printable_decode($text); } + return to_utf8($text, $charset); +} + +function recode_header($header, $basecharset) +{ + if (strpos($header, "=?") === false) + return to_utf8($header, $basecharset); + return preg_replace("/=\\?(.+?)\\?([qb])\\?(.+?)\\?=/ie", "decode_header('\\1','\\2','\\3')", $header); } # this turns some common forms of email addresses into mailto: links -function format_author($a) { - $a = preg_replace("/=\\?(.+?)\\?([qb])\\?(.+?)\\?=/ie", "decode_header('\\1','\\2','\\3')", $a); +function format_author($a, $charset) { + $a = recode_header($a, $charset); if (preg_match("/^\s*(.+)\s+\\(\"?(.+?)\"?\\)\s*$/",$a,$ar)) { return "<a href=\"mailto:".htmlspecialchars(urlencode($ar[1]))."\">".str_replace(" ", " ", htmlspecialchars($ar[2]))."</a>"; } @@ -90,9 +117,9 @@ return str_replace(" ", " ", htmlspecialchars($a)); } -function format_subject($s) { +function format_subject($s, $charset) { global $article; - $s = preg_replace("/=\\?(.+?)\\?([qb])\\?(.+?)\\?=/ie", "decode_header('\\1','\\2','\\3')", $s); + $s = recode_header($s, $charset); $s = preg_replace("/^(Re: *)?\[(PHP|PEAR)(-.*)?\] /i", "\\1", $s); // make this look better on the preview page.. if (strlen($s) > 150 && !isset($article)) { Index: php-news-web/group.php diff -u php-news-web/group.php:1.12 php-news-web/group.php:1.13 --- php-news-web/group.php:1.12 Mon Aug 19 14:08:15 2002 +++ php-news-web/group.php Wed Sep 11 06:04:52 2002 @@ -18,7 +18,7 @@ switch($format) { case 'rss': header("Content-type: text/xml"); - echo '<?xml version="1.0" encoding="iso-8859-1"?>';?> + echo '<?xml version="1.0" encoding="utf-8"?>';?> <rss version="0.93"> <channel> <title>news.php.net: <?echo $group?></title> @@ -27,7 +27,7 @@ <? break; case 'rdf': header("Content-type: text/xml"); - echo '<?xml version="1.0" encoding="iso-8859-1"?>'; + echo '<?xml version="1.0" encoding="utf-8"?>'; ?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -51,6 +51,9 @@ # list of articles $class = "even"; +# TODO: somehow determine the correct charset +$charset = ""; + while ($line = fgets($s, 4096)) { if ($line == ".\r\n") break; $line = chop($line); @@ -63,16 +66,16 @@ case 'rss': echo "<item>\n"; echo "<link>http://news.php.net/article.php?group=$group&article=$n</link>\n"; - echo "<title>", format_subject($subj), "</title>\n"; + echo "<title>", format_subject($subj, $charset), "</title>\n"; echo "<description>", htmlspecialchars(format_author($author)), "</description>\n"; echo "<pubDate>$date822</pubDate>\n"; echo "</item>\n"; break; case 'rdf': echo "<item>\n"; - echo "<title>", format_subject($subj), "</title>\n"; + echo "<title>", format_subject($subj, $charset), "</title>\n"; echo "<link>http://news.php.net/article.php?group=$group&article=$n</link>\n"; - echo "<description>", htmlspecialchars(format_author($author)), "</description>\n"; + echo "<description>", htmlspecialchars(format_author($author, $charset)), "</description>\n"; echo "<pubDate>$date822</pubDate>\n"; echo "</item>\n"; break; @@ -81,9 +84,9 @@ echo "<tr>"; echo "<td class=\"$class\"><a href=\"article.php?group=$group&article=$n\">$n</a></td>"; echo "<td class=\"$class\">"; - echo format_subject($subj); + echo format_subject($subj, $charset); echo "</td>"; - echo "<td class=\"$class\">".format_author($author)."</td>\n"; + echo "<td class=\"$class\">".format_author($author, $charset)."</td>\n"; echo "<td align=\"center\" class=\"$class\"><tt>" . format_date($odate) . "</tt></td>\n"; echo "<td align=\"right\" class=\"$class\">$lines</td>\n"; }