3 * Old & buggy PCRE-based message parser
5 * @author Ant P <p@cpi.merseine.nu>
6 * @license file://../COPYING
8 * @deprecated because it sucks
10 require_once 'lib/iface.Message3.php';
12 class Post_Default
implements Message3
14 const CHARSET
= 'UTF-8'; // Don't change unless you know what you're doing
15 const URL_DISPLAY_LENGTH
= 80;
18 private $output = null;
20 public static $allowed_html = array(
21 /* These are the order the HTML 5 spec defines them.
22 Yes I know HTML 5 is still a draft. Stop fucking whining. */
26 'ol', 'ul', 'li', 'dl', 'dt', 'dd',
27 'em', 'strong', 'small', 'abbr', 'dfn', 'code', 'var', 'samp', 'kbd', 'sup', 'sub', 'q', 'cite',
31 function __construct($input, $formatting = null)
33 // &-Encode everything by default
34 $temp = htmlspecialchars($input);
36 // Not allowed full HTML
37 foreach ( self
::$allowed_html as $tag ) {
38 $preg1[] = '#<('.$tag.')>(.+)</('.$tag.')>#Usie';
40 $temp = preg_replace($preg1, "'<'.strtolower('$1').'>$2</'.strtolower('$3').'>'", $temp);
43 $temp = preg_replace('#(?<=\b)(https?|ftp|irc|ut2004)://(([0-9a-zA-Z\-_.+?/%=;,~:]|&)+(\#[a-zA-Z][0-9a-zA-Z_]*)?)#ie',
44 "self::makeurl('$1://$2')", $temp);
45 $temp = preg_replace('#(?<=\b)(xmpp|aim):(([0-9a-zA-Z\-_.+?/%=;,~:]|&)+\@[0-9a-zA-Z_.]+)#ie',
46 "self::makeurl('$1:$2')", $temp);
48 // Add <br/>s if the no-autobr option isn't given
49 if ( ! ($formatting & self
::HTML_RAW
) ) {
52 // Trim <br/>s after block elements and other places they shouldn't be
53 $temp = preg_replace('#<(/?(table|tr|[uod]l)|/(t[dh]|d[dt]|li|p|h[1-6]))>(\s*<br />)+#si',
55 // Trim <br/>s within <pre>
56 $temp = preg_replace('#<pre>(.*)</pre>#Usie',
57 "'<pre>'.str_replace('<br />', '', '$1').'</pre>'", $temp);
60 if ( strpos($this->output
, '<script') !== false ) {
61 throw new Exception('No scripts allowed.');
64 $this->output
= str_replace("\r\n", "\n", $temp);
67 private static function makeurl($uri)
69 $uri = html_entity_decode($uri);
71 return '<a href="'.htmlspecialchars($uri).'" rel="external nofollow">'.( strlen($uri) > self
::URL_DISPLAY_LENGTH ?
72 htmlspecialchars(substr($uri, 0, self
::URL_DISPLAY_LENGTH
)).'...' : htmlspecialchars($uri) ).'</a>';
77 // Check for malformed XML, doesn't really do much else
78 $parser = xml_parser_create(self
::CHARSET
);
80 if ( !xml_parse($parser, '<div>'.$this->output
.'</div>', true) ) {
81 throw new InvalidInputException(xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser));