2007-06-28 23:01:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTML Purifier Phorum Mod. Filter your HTML the Standards-Compliant Way!
|
|
|
|
*
|
|
|
|
* This Phorum mod enables users to post raw HTML into Phorum. But never
|
|
|
|
* fear: with the help of HTML Purifier, this HTML will be beat into
|
|
|
|
* de-XSSed and standards-compliant form, safe for general consumption.
|
|
|
|
* It is not recommended, but possible to run this mod in parallel
|
|
|
|
* with other formatters (in short, please DISABLE the BBcode mod).
|
|
|
|
*
|
|
|
|
* For help migrating from your previous markup language to pure HTML
|
|
|
|
* please check the migrate.bbcode.php file.
|
|
|
|
*
|
2007-06-29 00:48:55 +00:00
|
|
|
* If you'd like to use this with a WYSIWYG editor, make sure that
|
|
|
|
* editor sets $PHORUM['mod_htmlpurifier']['wysiwyg'] to true. Otherwise,
|
|
|
|
* administrators who need to edit other people's comments may be at
|
|
|
|
* risk for some nasty attacks.
|
|
|
|
*
|
2008-01-13 05:28:39 +00:00
|
|
|
* Tested with Phorum 5.2.6.
|
2007-06-28 23:01:27 +00:00
|
|
|
*/
|
|
|
|
|
2007-06-29 03:41:21 +00:00
|
|
|
// Note: Cache data is base64 encoded because Phorum insists on flinging
|
|
|
|
// to the user and expecting it to come back unharmed, newlines and
|
|
|
|
// all, which ain't happening. It's slower, it takes up more space, but
|
|
|
|
// at least it won't get mutilated
|
|
|
|
|
2007-06-28 23:01:27 +00:00
|
|
|
/**
|
|
|
|
* Purifies a data array
|
|
|
|
*/
|
2007-06-29 00:28:07 +00:00
|
|
|
function phorum_htmlpurifier_format($data)
|
2007-06-28 23:01:27 +00:00
|
|
|
{
|
|
|
|
$PHORUM = $GLOBALS["PHORUM"];
|
|
|
|
|
|
|
|
$purifier =& HTMLPurifier::getInstance();
|
|
|
|
$cache_serial = $PHORUM['mod_htmlpurifier']['body_cache_serial'];
|
|
|
|
|
|
|
|
foreach($data as $message_id => $message){
|
2007-06-29 00:28:07 +00:00
|
|
|
if(isset($message['body'])) {
|
2007-06-29 15:43:23 +00:00
|
|
|
|
2007-06-29 18:25:13 +00:00
|
|
|
if ($message_id) {
|
|
|
|
// we're dealing with a real message, not a fake, so
|
|
|
|
// there a number of shortcuts that can be taken
|
|
|
|
|
|
|
|
if (isset($message['meta']['htmlpurifier_light'])) {
|
|
|
|
// format hook was called outside of Phorum's normal
|
|
|
|
// functions, do the abridged purification
|
|
|
|
$data[$message_id]['body'] = $purifier->purify($message['body']);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($PHORUM['args']['purge'])) {
|
|
|
|
// purge the cache, must be below the following if
|
|
|
|
unset($message['meta']['body_cache']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
isset($message['meta']['body_cache']) &&
|
|
|
|
isset($message['meta']['body_cache_serial']) &&
|
|
|
|
$message['meta']['body_cache_serial'] == $cache_serial
|
|
|
|
) {
|
|
|
|
// cached version is present, bail out early
|
|
|
|
$data[$message_id]['body'] = base64_decode($message['meta']['body_cache']);
|
|
|
|
continue;
|
|
|
|
}
|
2007-06-28 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// migration might edit this array, that's why it's defined
|
|
|
|
// so early
|
|
|
|
$updated_message = array();
|
|
|
|
|
|
|
|
// create the $body variable
|
|
|
|
if (
|
2007-06-29 18:25:13 +00:00
|
|
|
$message_id && // message must be real to migrate
|
2007-06-28 23:01:27 +00:00
|
|
|
!isset($message['meta']['body_cache_serial'])
|
|
|
|
) {
|
|
|
|
// perform migration
|
|
|
|
$fake_data = array();
|
2007-06-29 18:25:13 +00:00
|
|
|
list($signature, $edit_message) = phorum_htmlpurifier_remove_sig_and_editmessage($message);
|
2007-06-28 23:01:27 +00:00
|
|
|
$fake_data[$message_id] = $message;
|
|
|
|
$fake_data = phorum_htmlpurifier_migrate($fake_data);
|
|
|
|
$body = $fake_data[$message_id]['body'];
|
2008-01-13 01:58:34 +00:00
|
|
|
$body = str_replace("<phorum break>\n", "\n", $body);
|
2007-06-28 23:01:27 +00:00
|
|
|
$updated_message['body'] = $body; // save it in
|
2007-06-29 18:25:13 +00:00
|
|
|
$body .= $signature . $edit_message; // add it back in
|
2007-06-28 23:01:27 +00:00
|
|
|
} else {
|
|
|
|
// reverse Phorum's pre-processing
|
|
|
|
$body = $message['body'];
|
|
|
|
// order is important
|
|
|
|
$body = str_replace("<phorum break>\n", "\n", $body);
|
2008-01-18 07:04:30 +00:00
|
|
|
$body = str_replace(array('<','>','&', '"'), array('<','>','&','"'), $body);
|
2007-06-29 20:00:38 +00:00
|
|
|
if (!$message_id && defined('PHORUM_CONTROL_CENTER')) {
|
|
|
|
// we're in control.php, so it was double-escaped
|
|
|
|
$body = str_replace(array('<','>','&', '"'), array('<','>','&','"'), $body);
|
|
|
|
}
|
2007-06-28 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$body = $purifier->purify($body);
|
|
|
|
|
2007-06-29 18:25:13 +00:00
|
|
|
// dynamically update the cache (MUST BE DONE HERE!)
|
|
|
|
// this is inefficient because it's one db call per
|
|
|
|
// cache miss, but once the cache is in place things are
|
|
|
|
// a lot zippier.
|
2007-06-28 23:01:27 +00:00
|
|
|
|
2007-06-29 18:25:13 +00:00
|
|
|
if ($message_id) { // make sure it's not a fake id
|
|
|
|
$updated_message['meta'] = $message['meta'];
|
|
|
|
$updated_message['meta']['body_cache'] = base64_encode($body);
|
|
|
|
$updated_message['meta']['body_cache_serial'] = $cache_serial;
|
|
|
|
phorum_db_update_message($message_id, $updated_message);
|
|
|
|
}
|
2007-06-28 23:01:27 +00:00
|
|
|
|
2007-06-29 18:25:13 +00:00
|
|
|
// must not get overloaded until after we cache it, otherwise
|
|
|
|
// we'll inadvertently change the original text
|
2007-06-28 23:01:27 +00:00
|
|
|
$data[$message_id]['body'] = $body;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2007-06-29 18:25:13 +00:00
|
|
|
// -----------------------------------------------------------------------
|
2008-01-13 05:28:39 +00:00
|
|
|
// This is fragile code, copied from read.php:596 (Phorum 5.2.6). Please
|
|
|
|
// keep this code in-sync with Phorum
|
2007-06-29 18:25:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a signature based on a message array
|
|
|
|
*/
|
|
|
|
function phorum_htmlpurifier_generate_sig($row) {
|
|
|
|
$phorum_sig = '';
|
|
|
|
if(isset($row["user"]["signature"])
|
|
|
|
&& isset($row['meta']['show_signature']) && $row['meta']['show_signature']==1){
|
|
|
|
$phorum_sig=trim($row["user"]["signature"]);
|
|
|
|
if(!empty($phorum_sig)){
|
|
|
|
$phorum_sig="\n\n$phorum_sig";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $phorum_sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an edit message based on a message array
|
|
|
|
*/
|
|
|
|
function phorum_htmlpurifier_generate_editmessage($row) {
|
|
|
|
$PHORUM = $GLOBALS['PHORUM'];
|
|
|
|
$editmessage = '';
|
|
|
|
if(isset($row['meta']['edit_count']) && $row['meta']['edit_count'] > 0) {
|
|
|
|
$editmessage = str_replace ("%count%", $row['meta']['edit_count'], $PHORUM["DATA"]["LANG"]["EditedMessage"]);
|
2008-01-13 01:58:34 +00:00
|
|
|
$editmessage = str_replace ("%lastedit%", phorum_date($PHORUM["short_date_time"],$row['meta']['edit_date']), $editmessage);
|
2007-06-29 18:25:13 +00:00
|
|
|
$editmessage = str_replace ("%lastuser%", $row['meta']['edit_username'], $editmessage);
|
2008-01-13 01:58:34 +00:00
|
|
|
$editmessage = "\n\n\n\n$editmessage";
|
2007-06-29 18:25:13 +00:00
|
|
|
}
|
|
|
|
return $editmessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End fragile code
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the signature and edit message from a message
|
|
|
|
* @param $row Message passed by reference
|
|
|
|
*/
|
|
|
|
function phorum_htmlpurifier_remove_sig_and_editmessage(&$row) {
|
|
|
|
$signature = phorum_htmlpurifier_generate_sig($row);
|
|
|
|
$editmessage = phorum_htmlpurifier_generate_editmessage($row);
|
2008-01-13 01:58:34 +00:00
|
|
|
$replacements = array();
|
2008-01-13 05:28:39 +00:00
|
|
|
// we need to remove add <phorum break> as that is the form these
|
|
|
|
// extra bits are in.
|
2008-01-13 01:58:34 +00:00
|
|
|
if ($signature) $replacements[str_replace("\n", "<phorum break>\n", $signature)] = '';
|
|
|
|
if ($editmessage) $replacements[str_replace("\n", "<phorum break>\n", $editmessage)] = '';
|
|
|
|
$row['body'] = strtr($row['body'], $replacements);
|
2007-06-29 18:25:13 +00:00
|
|
|
return array($signature, $editmessage);
|
|
|
|
}
|
|
|
|
|
2007-06-28 23:01:27 +00:00
|
|
|
/**
|
2007-06-29 18:25:13 +00:00
|
|
|
* Indicate that data is fully HTML and not from migration, invalidate
|
|
|
|
* previous caches
|
2008-01-13 05:28:39 +00:00
|
|
|
* @note This function could generate the actual cache entries, but
|
2008-01-13 01:58:34 +00:00
|
|
|
* since there's data missing that must be deferred to the first read
|
2007-06-28 23:01:27 +00:00
|
|
|
*/
|
|
|
|
function phorum_htmlpurifier_posting($message) {
|
|
|
|
$PHORUM = $GLOBALS["PHORUM"];
|
2007-06-29 18:25:13 +00:00
|
|
|
unset($message['meta']['body_cache']); // invalidate the cache
|
2007-06-28 23:01:27 +00:00
|
|
|
$message['meta']['body_cache_serial'] = $PHORUM['mod_htmlpurifier']['body_cache_serial'];
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overload quoting mechanism to prevent default, mail-style quote from happening
|
|
|
|
*/
|
|
|
|
function phorum_htmlpurifier_quote($array) {
|
|
|
|
$PHORUM = $GLOBALS["PHORUM"];
|
|
|
|
$purifier =& HTMLPurifier::getInstance();
|
|
|
|
$text = $purifier->purify($array[1]);
|
2008-01-13 01:58:34 +00:00
|
|
|
$source = htmlspecialchars($array[0]);
|
|
|
|
return "<blockquote cite=\"$source\">\n$text\n</blockquote>";
|
2007-06-28 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-06-29 00:28:07 +00:00
|
|
|
* Ensure that our format hook is processed last. Also, loads the library.
|
2007-06-28 23:01:27 +00:00
|
|
|
* @credits <http://secretsauce.phorum.org/snippets/make_bbcode_last_formatter.php.txt>
|
|
|
|
*/
|
|
|
|
function phorum_htmlpurifier_common() {
|
|
|
|
|
2007-06-29 20:34:19 +00:00
|
|
|
require_once(dirname(__FILE__).'/htmlpurifier/HTMLPurifier.auto.php');
|
|
|
|
require(dirname(__FILE__).'/init-config.php');
|
2007-06-28 23:01:27 +00:00
|
|
|
|
2007-06-29 20:34:19 +00:00
|
|
|
$config = phorum_htmlpurifier_get_config();
|
2007-06-29 00:28:07 +00:00
|
|
|
HTMLPurifier::getInstance($config);
|
2007-06-29 00:48:55 +00:00
|
|
|
|
2007-06-29 00:28:07 +00:00
|
|
|
// increment revision.txt if you want to invalidate the cache
|
|
|
|
$GLOBALS['PHORUM']['mod_htmlpurifier']['body_cache_serial'] = $config->getSerial();
|
2007-06-29 00:48:55 +00:00
|
|
|
|
2007-06-29 00:28:07 +00:00
|
|
|
// load migration
|
|
|
|
if (file_exists(dirname(__FILE__) . '/migrate.php')) {
|
|
|
|
include(dirname(__FILE__) . '/migrate.php');
|
|
|
|
} else {
|
|
|
|
echo '<strong>Error:</strong> No migration path specified for HTML Purifier, please check
|
|
|
|
<tt>modes/htmlpurifier/migrate.bbcode.php</tt> for instructions on
|
|
|
|
how to migrate from your previous markup language.';
|
|
|
|
exit;
|
2007-06-28 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2008-02-28 05:35:45 +00:00
|
|
|
if (!function_exists('phorum_htmlpurifier_migrate')) {
|
|
|
|
// Dummy function
|
|
|
|
function phorum_htmlpurifier_migrate($data) {return $data;}
|
|
|
|
}
|
|
|
|
|
2007-06-28 23:01:27 +00:00
|
|
|
}
|
|
|
|
|
2007-06-29 00:48:55 +00:00
|
|
|
/**
|
|
|
|
* Pre-emptively performs purification if it looks like a WYSIWYG editor
|
|
|
|
* is being used
|
|
|
|
*/
|
|
|
|
function phorum_htmlpurifier_before_editor($message) {
|
|
|
|
if (!empty($GLOBALS['PHORUM']['mod_htmlpurifier']['wysiwyg'])) {
|
|
|
|
if (!empty($message['body'])) {
|
|
|
|
$body = $message['body'];
|
|
|
|
// de-entity-ize contents
|
|
|
|
$body = str_replace(array('<','>','&'), array('<','>','&'), $body);
|
|
|
|
$purifier =& HTMLPurifier::getInstance();
|
2008-01-13 05:28:39 +00:00
|
|
|
$body = $purifier->purify($body);
|
2007-06-29 00:48:55 +00:00
|
|
|
// re-entity-ize contents
|
|
|
|
$body = htmlspecialchars($body, ENT_QUOTES, $GLOBALS['PHORUM']['DATA']['CHARSET']);
|
2008-01-13 05:28:39 +00:00
|
|
|
$message['body'] = $body;
|
2007-06-29 00:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2007-06-29 15:43:23 +00:00
|
|
|
function phorum_htmlpurifier_editor_after_subject() {
|
2007-06-29 18:25:13 +00:00
|
|
|
// don't show this message if it's a WYSIWYG editor, since it will
|
|
|
|
// then be handled automatically
|
2008-01-13 05:28:39 +00:00
|
|
|
if (!empty($GLOBALS['PHORUM']['mod_htmlpurifier']['wysiwyg'])) {
|
|
|
|
$i = $GLOBALS['PHORUM']['DATA']['MODE'];
|
|
|
|
if ($i == 'quote' || $i == 'edit' || $i == 'moderation') {
|
|
|
|
?>
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
<strong>Notice:</strong> HTML has been scrubbed for your safety.
|
|
|
|
If you would like to see the original, turn off WYSIWYG mode
|
|
|
|
(consult your administrator for details.)
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!empty($GLOBALS['PHORUM']['mod_htmlpurifier']['suppress_message'])) return;
|
2008-01-13 04:20:54 +00:00
|
|
|
?><div class="htmlpurifier-help">
|
2007-11-02 01:55:45 +00:00
|
|
|
<p>
|
|
|
|
<strong>HTML input</strong> is enabled. Make sure you escape all HTML and
|
|
|
|
angled brackets with <code>&lt;</code> and <code>&gt;</code>.
|
|
|
|
</p><?php
|
|
|
|
$purifier =& HTMLPurifier::getInstance();
|
|
|
|
$config = $purifier->config;
|
|
|
|
if ($config->get('AutoFormat', 'AutoParagraph')) {
|
|
|
|
?><p>
|
|
|
|
<strong>Auto-paragraphing</strong> is enabled. Double
|
|
|
|
newlines will be converted to paragraphs; for single
|
|
|
|
newlines, use the <code>pre</code> tag.
|
|
|
|
</p><?php
|
|
|
|
}
|
|
|
|
$html_definition = $config->getDefinition('HTML');
|
|
|
|
$allowed = array();
|
|
|
|
foreach ($html_definition->info as $name => $x) $allowed[] = "<code>$name</code>";
|
|
|
|
sort($allowed);
|
|
|
|
$allowed_text = implode(', ', $allowed);
|
|
|
|
?><p><strong>Allowed tags:</strong> <?php
|
|
|
|
echo $allowed_text;
|
|
|
|
?>.</p><?php
|
|
|
|
?>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
For inputting literal code such as HTML and PHP for display, use
|
|
|
|
CDATA tags to auto-escape your angled brackets, and <code>pre</code>
|
|
|
|
to preserve newlines:
|
|
|
|
</p>
|
|
|
|
<pre><pre><![CDATA[
|
|
|
|
<em>Place code here</em>
|
|
|
|
]]></pre></pre>
|
|
|
|
<p>
|
|
|
|
Power users, you can hide this notice with:
|
|
|
|
<pre>.htmlpurifier-help {display:none;}</pre>
|
|
|
|
</p>
|
2008-01-13 04:20:54 +00:00
|
|
|
</div><?php
|
2007-06-29 15:43:23 +00:00
|
|
|
}
|
|
|
|
|