Index: trunk/extensions/HTMLets/HTMLets.php |
— | — | @@ -31,21 +31,21 @@ |
32 | 32 | /** |
33 | 33 | * Pass file content unchanged. May get mangeled by late server pass. |
34 | 34 | **/ |
35 | | -define('HTMLETS_NO_HACK', 'none'); |
| 35 | +define( 'HTMLETS_NO_HACK', 'none' ); |
36 | 36 | |
37 | 37 | /** |
38 | 38 | * Normalize whitespace, apply special stripping and escaping to avoid mangeling. |
39 | 39 | * This will break pre-formated text (pre tags), and may interfere with JavaScript |
40 | 40 | * code under some circumstances. |
41 | 41 | **/ |
42 | | -define('HTMLETS_STRIP_HACK', 'strip'); |
| 42 | +define( 'HTMLETS_STRIP_HACK', 'strip' ); |
43 | 43 | |
44 | 44 | /** |
45 | 45 | * bypass late parser pass using ParserAfterTidy. |
46 | 46 | * This will get the file content safely into the final HTML. |
47 | 47 | * There's no obvious trouble with it, but it just might interfere with other extensions. |
48 | 48 | **/ |
49 | | -define('HTMLETS_BYPASS_HACK', 'bypass'); |
| 49 | +define( 'HTMLETS_BYPASS_HACK', 'bypass' ); |
50 | 50 | |
51 | 51 | $wgHTMLetsHack = HTMLETS_BYPASS_HACK; #hack to use to work around bug #8997. see constant declarations. |
52 | 52 | |
— | — | @@ -54,73 +54,88 @@ |
55 | 55 | $wgExtensionFunctions[] = "wfHTMLetsExtension"; |
56 | 56 | |
57 | 57 | function wfHTMLetsExtension() { |
58 | | - global $wgParser; |
59 | | - $wgParser->setHook( "htmlet", "wfRenderHTMLet" ); |
| 58 | + global $wgParser; |
| 59 | + $wgParser->setHook( "htmlet", "wfRenderHTMLet" ); |
60 | 60 | } |
61 | 61 | |
62 | 62 | # The callback function for converting the input text to HTML output |
63 | 63 | function wfRenderHTMLet( $name, $argv, $parser ) { |
64 | | - global $wgHTMLetsDirectory, $wgHTMLetsHack, $IP; |
| 64 | + global $wgHTMLetsDirectory, $wgHTMLetsHack, $IP; |
65 | 65 | |
66 | | - if (@$argv['nocache']) $parser->disableCache(); |
| 66 | + if ( @$argv['nocache'] ) { |
| 67 | + $parser->disableCache(); |
| 68 | + } |
67 | 69 | |
68 | | - #HACKs for bug 8997 |
69 | | - $hack = @$argv['hack']; |
70 | | - if ( $hack == 'strip' ) $hack = HTMLETS_STRIP_HACK; |
71 | | - else if ( $hack == 'bypass' ) $hack = HTMLETS_BYPASS_HACK; |
72 | | - else if ( $hack == 'none' || $hack == 'no' ) $hack = HTMLETS_NO_HACK; |
73 | | - else $hack = $wgHTMLetsHack; |
| 70 | + #HACKs for bug 8997 |
| 71 | + $hack = @$argv['hack']; |
| 72 | + if ( $hack == 'strip' ) { |
| 73 | + $hack = HTMLETS_STRIP_HACK; |
| 74 | + } elseif ( $hack == 'bypass' ) { |
| 75 | + $hack = HTMLETS_BYPASS_HACK; |
| 76 | + } else if ( $hack == 'none' || $hack == 'no' ) { |
| 77 | + $hack = HTMLETS_NO_HACK; |
| 78 | + } else { |
| 79 | + $hack = $wgHTMLetsHack; |
| 80 | + } |
74 | 81 | |
75 | | - $dir = $wgHTMLetsDirectory; |
76 | | - if (!$dir) $dir = "$IP/htmlets"; |
| 82 | + $dir = $wgHTMLetsDirectory; |
| 83 | + if ( !$dir ) { |
| 84 | + $dir = "$IP/htmlets"; |
| 85 | + } |
77 | 86 | |
78 | | - $name = preg_replace('@[\\\\/!]|^\.+?&#@', '', $name); #strip path separators and leading dots. |
79 | | - $name .= '.html'; #append html ending, for added security and conveniance |
| 87 | + $name = preg_replace( '@[\\\\/!]|^\.+?&#@', '', $name ); #strip path separators and leading dots. |
| 88 | + $name .= '.html'; #append html ending, for added security and conveniance |
80 | 89 | |
81 | | - $f = "$dir/$name"; |
| 90 | + $f = "$dir/$name"; |
82 | 91 | |
83 | | - if (!preg_match('!^\w+://!', $dir) && !file_exists($f)) { |
84 | | - $output = '<div class="error">Can\'t find html file '.htmlspecialchars($name).'</div>'; |
85 | | - } |
86 | | - else { |
87 | | - $output = file_get_contents($f); |
88 | | - if ($output === false) $output = '<div class="error">Failed to load html file '.htmlspecialchars($name).'</div>'; |
89 | | - } |
| 92 | + if ( !preg_match('!^\w+://!', $dir ) && !file_exists( $f ) ) { |
| 93 | + $output = '<div class="error">Can\'t find html file ' . htmlspecialchars( $name ) . '</div>'; |
| 94 | + } else { |
| 95 | + $output = file_get_contents( $f ); |
| 96 | + if ( $output === false ) { |
| 97 | + $output = '<div class="error">Failed to load html file ' . htmlspecialchars( $name ) . '</div>'; |
| 98 | + } |
| 99 | + } |
90 | 100 | |
91 | | - #HACKs for bug 8997 |
92 | | - if ( $hack == HTMLETS_STRIP_HACK ) { |
93 | | - $output = trim( preg_replace( '![\r\n\t ]+!', ' ', $output ) ); //normalize whitespace |
94 | | - $output = preg_replace( '!(.) *:!', '\1:', $output ); //strip blanks before colons |
| 101 | + #HACKs for bug 8997 |
| 102 | + if ( $hack == HTMLETS_STRIP_HACK ) { |
| 103 | + $output = trim( preg_replace( '![\r\n\t ]+!', ' ', $output ) ); //normalize whitespace |
| 104 | + $output = preg_replace( '!(.) *:!', '\1:', $output ); //strip blanks before colons |
95 | 105 | |
96 | | - if ( strlen($output) > 0 ) { //escape first char if it may trigger wiki formatting |
97 | | - $ch = substr( $output, 0, 1); |
| 106 | + if ( strlen( $output ) > 0 ) { //escape first char if it may trigger wiki formatting |
| 107 | + $ch = substr( $output, 0, 1 ); |
98 | 108 | |
99 | | - if ( $ch == '#' ) $output = '#' . substr( $output, 1); |
100 | | - else if ( $ch == '*' ) $output = '*' . substr( $output, 1); |
101 | | - else if ( $ch == ':' ) $output = ':' . substr( $output, 1); |
102 | | - else if ( $ch == ';' ) $output = ';' . substr( $output, 1); |
103 | | - } |
104 | | - } |
105 | | - else if ( $hack == HTMLETS_BYPASS_HACK ) { |
106 | | - global $wgHooks; |
| 109 | + if ( $ch == '#' ) { |
| 110 | + $output = '#' . substr( $output, 1 ); |
| 111 | + } elseif ( $ch == '*' ) { |
| 112 | + $output = '*' . substr( $output, 1 ); |
| 113 | + } elseif ( $ch == ':' ) { |
| 114 | + $output = ':' . substr( $output, 1 ); |
| 115 | + } elseif ( $ch == ';' ) { |
| 116 | + $output = ';' . substr( $output, 1 ); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + elseif ( $hack == HTMLETS_BYPASS_HACK ) { |
| 121 | + global $wgHooks; |
107 | 122 | |
108 | | - if ( !isset($wgHooks['ParserAfterTidy']) || !in_array('wfRenderHTMLetHackPostProcess', $wgHooks['ParserAfterTidy']) ) { |
109 | | - $wgHooks['ParserAfterTidy'][] = 'wfRenderHTMLetHackPostProcess'; |
110 | | - } |
| 123 | + if ( !isset($wgHooks['ParserAfterTidy']) || !in_array('wfRenderHTMLetHackPostProcess', $wgHooks['ParserAfterTidy']) ) { |
| 124 | + $wgHooks['ParserAfterTidy'][] = 'wfRenderHTMLetHackPostProcess'; |
| 125 | + } |
111 | 126 | |
112 | | - $output = '<!-- @HTMLetsHACK@ '.base64_encode($output).' @HTMLetsHACK@ -->'; |
113 | | - } |
| 127 | + $output = '<!-- @HTMLetsHACK@ '.base64_encode($output).' @HTMLetsHACK@ -->'; |
| 128 | + } |
114 | 129 | |
115 | | - return $output; |
| 130 | + return $output; |
116 | 131 | } |
117 | 132 | |
118 | 133 | function wfRenderHTMLetHackPostProcess( $parser, &$text ) { |
119 | | - $text = preg_replace( |
120 | | - '/<!-- @HTMLetsHACK@ ([0-9a-zA-Z\\+\\/]+=*) @HTMLetsHACK@ -->/esm', |
121 | | - 'base64_decode("$1")', |
122 | | - $text |
123 | | - ); |
| 134 | + $text = preg_replace( |
| 135 | + '/<!-- @HTMLetsHACK@ ([0-9a-zA-Z\\+\\/]+=*) @HTMLetsHACK@ -->/esm', |
| 136 | + 'base64_decode("$1")', |
| 137 | + $text |
| 138 | + ); |
124 | 139 | |
125 | | - return true; |
| 140 | + return true; |
126 | 141 | } |
127 | 142 | |