Special Ops 2.50
[specialops2.git] / lib / Post_Default.php
blob3cc4ce3bd6c9ba8bfc1c30a65de4102c2febd345
1 <?php
2 /**
3 * Old & buggy PCRE-based message parser
5 * @author Ant P <p@cpi.merseine.nu>
6 * @license file://../COPYING
7 * @version 2.15
8 * @deprecated because it sucks
9 */
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;
17 private $temp_text;
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. */
23 'blockquote',
24 'p',
25 'pre',
26 'ol', 'ul', 'li', 'dl', 'dt', 'dd',
27 'em', 'strong', 'small', 'abbr', 'dfn', 'code', 'var', 'samp', 'kbd', 'sup', 'sub', 'q', 'cite',
28 'ins', 'del'
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[] = '#&lt;('.$tag.')&gt;(.+)&lt;/('.$tag.')&gt;#Usie';
40 $temp = preg_replace($preg1, "'<'.strtolower('$1').'>$2</'.strtolower('$3').'>'", $temp);
42 // Auto-link URLs
43 $temp = preg_replace('#(?<=\b)(https?|ftp|irc|ut2004)://(([0-9a-zA-Z\-_.+?/%=;,~:]|&amp;)+(\#[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\-_.+?/%=;,~:]|&amp;)+\@[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) ) {
50 $temp = nl2br($temp);
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',
54 '<$1>', $temp);
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>';
75 function validate()
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));
85 function getOutput()
87 return $this->output;
90 function __toString()
92 return $this->output;