4 class InvalidMessageException
extends Exception
8 function __construct($msg = null, $code = 0, $line = 0)
10 parent
::__construct($msg, $code);
11 $this->xmlline
= $line;
16 return $this->xmlline
;
23 const M_HTML_NONE
= 1;
24 const M_HTML_FILTERED
= 2;
25 const M_NO_NEWLINES
= 4;
26 const M_URL_LENGTH
= 60;
34 public $error_message;
35 public $error_description;
38 public static $allowed_html = array(
39 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
40 'p', 'blockquote', 'q', 'ins', 'del', 'em', 'strong',
42 'ol', 'ul', 'li', 'dl', 'dt', 'dd'
45 function __construct($input, $formatting = null)
47 if ( is_null($formatting) )
48 $formatting = self
::M_HTML_FILTERED
;
50 $this->temp_text
= str_replace("\r\n", "\n", $input);
51 $this->temp_text
= htmlspecialchars($this->temp_text
);
53 if ( $formatting & self
::M_HTML_NONE
) { //no html
55 } elseif ( $formatting & self
::M_HTML_FILTERED
) { //some html
57 foreach ( self
::$allowed_html as $tag )
58 $preg1[] = '#<('.$tag.')>(.+)</('.$tag.')>#Usie';
59 $this->temp_text
= preg_replace($preg1, "'<'.strtolower('$1').'>$2</'.strtolower('$3').'>'", $this->temp_text
);
62 $this->temp_text
= html_entity_decode($this->temp_text
);
63 $this->temp_text
= preg_replace('#(^|\s)(<|>)_(<|>)($|\s)#e', 'htmlspecialchars(\'$0\')', $this->temp_text
);
66 if ( ! ($formatting & self
::M_HTML_NONE
) ) { //anything except plain
67 $this->temp_text
= preg_replace('#(?<!")(http|ftp|irc)://(([0-9a-zA-Z\-_.+?/%=;:]|&)+)#ie',
68 "message::makeurl('$1://$2', ".$formatting.")",
72 if ( ! ($formatting & self
::M_NO_NEWLINES
) ) {
73 $this->temp_text
= nl2br($this->temp_text
);
74 //Trim <br/>s after block elements
75 $this->temp_text
= preg_replace('#<(/?table|/?tr|/t[dh]|/?[duo]l|/d[dt]|/li|/p|/h[1-6])>(\s*<br />)+#si',
76 '<$1>', $this->temp_text
);
79 $this->xml_validate();
80 $this->output
= $this->temp_text
;
83 private function makeurl($uri, $formatting)
85 if ( $formatting & self
::M_HTML_FILTERED
)
86 $uri = html_entity_decode($uri);
87 return '<a href="'.htmlentities($uri).'" rel="nofollow">'.
88 ( strlen($uri) > self
::M_URL_LENGTH ?
89 htmlentities(substr($uri, 0, self
::M_URL_LENGTH
)).'<strong>[...]</strong>' : $uri ).
93 private function xml_validate()
95 // Check for broken XML, doesn't really do much else
96 $this->xml
= xml_parser_create('UTF-8');
97 $this->xml_string
= '<xhtml>'.$this->temp_text
.'</xhtml>';
99 if ( !xml_parse($this->xml
, $this->xml_string
, true) ) {
100 throw new InvalidMessageException(
101 xml_error_string(xml_get_error_code($this->xml
)),
102 xml_get_error_code($this->xml
),
103 xml_get_current_line_number($this->xml
)