Fixed bug 2040 relating to the last character being chopped off in the

html2text conversion.

While there I tidied up the source and fixed up a problem where the links
were being printed BEFORE the text they refer to (now they are directly after)
This commit is contained in:
moodler 2005-01-19 12:12:34 +00:00
parent fe32b4f6d7
commit 00666d4f25

View File

@ -23,16 +23,14 @@ function html2text( $badStr ) {
$is_open_dq = false;
$is_open_sq = false;
//remove PHP if it exists
// while ( substr_count( $badStr, '<'.'?' ) && substr_count( $badStr, '?'.'>' ) && strpos( $badStr, '?'.'>', strpos( $badStr, '<'.'?' ) ) > strpos( $badStr, '<'.'?' ) ) {
// $badStr = substr( $badStr, 0, strpos( $badStr, '<'.'?' ) ) . substr( $badStr, strpos( $badStr, '?'.'>', strpos( $badStr, '<'.'?' ) ) + 2 );
// } // Moodle
//remove comments
while ( substr_count( $badStr, '<!--' ) && substr_count( $badStr, '-->' ) && strpos( $badStr, '-->', strpos( $badStr, '<!--' ) ) > strpos( $badStr, '<!--' ) ) {
$badStr = substr( $badStr, 0, strpos( $badStr, '<!--' ) ) . substr( $badStr, strpos( $badStr, '-->', strpos( $badStr, '<!--' ) ) + 3 );
while (substr_count($badStr, '<!--') &&
substr_count($badStr, '-->') &&
strpos($badStr, '-->', strpos($badStr, '<!--' ) ) > strpos( $badStr, '<!--' ) ) {
$badStr = substr( $badStr, 0, strpos( $badStr, '<!--' ) ) .
substr( $badStr, strpos( $badStr, '-->',
strpos( $badStr, '<!--' ) ) + 3 );
}
//now make sure all HTML tags are correctly written (> not in between quotes)
@ -41,31 +39,50 @@ function html2text( $badStr ) {
$chr = $badStr{0}; // Moodle
$goodStr = ''; // Moodle
if($len > 0) { // Moodle
for ( $x = 0, $is_open_tb = false, $is_open_sq = false, $is_open_dq = false; $x < $len - 1; $chr = $badStr{++$x} ) { // Moodle
//take each letter in turn and check if that character is permitted there
if ($len > 0) { // Moodle
for ($x=0; $x < $len; $x++ ) { // Moodle
$chr = $badStr{$x}; //take each letter in turn and check if that character is permitted there
switch ( $chr ) {
case '<':
if ( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 5 ) ) == 'style' ) {
$badStr = substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '</style>', $x ) + 7 ); $chr = '';
} elseif( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 6 ) ) == 'script' ) {
$badStr = substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '</script>', $x ) + 8 ); $chr = '';
} elseif( !$is_open_tb ) { $is_open_tb = true; } else { $chr = '&lt;'; }
$badStr = substr( $badStr, 0, $x ) .
substr( $badStr, strpos( strtolower( $badStr ), '</style>', $x ) + 7 );
$chr = '';
} else if ( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 6 ) ) == 'script' ) {
$badStr = substr( $badStr, 0, $x ) .
substr( $badStr, strpos( strtolower( $badStr ), '</script>', $x ) + 8 );
$chr = '';
} else if (!$is_open_tb) {
$is_open_tb = true;
} else {
$chr = '&lt;';
}
break;
case '>':
if ( !$is_open_tb || $is_open_dq || $is_open_sq ) { $chr = '&gt;'; } else { $is_open_tb = false; }
if ( !$is_open_tb || $is_open_dq || $is_open_sq ) {
$chr = '&gt;';
} else {
$is_open_tb = false;
}
break;
case '"':
if ( $is_open_tb && !$is_open_dq && !$is_open_sq ) { $is_open_dq = true; }
elseif( $is_open_tb && $is_open_dq && !$is_open_sq ) { $is_open_dq = false; }
else { $chr = '&quot;'; }
if ( $is_open_tb && !$is_open_dq && !$is_open_sq ) {
$is_open_dq = true;
} else if ( $is_open_tb && $is_open_dq && !$is_open_sq ) {
$is_open_dq = false;
} else {
$chr = '&quot;';
}
break;
case "'":
if( $is_open_tb && !$is_open_dq && !$is_open_sq ) { $is_open_sq = true; }
elseif( $is_open_tb && !$is_open_dq && $is_open_sq ) { $is_open_sq = false; }
if ( $is_open_tb && !$is_open_dq && !$is_open_sq ) {
$is_open_sq = true;
} else if ( $is_open_tb && !$is_open_dq && $is_open_sq ) {
$is_open_sq = false;
}
break;
}
$goodStr .= $chr;
@ -81,22 +98,14 @@ function html2text( $badStr ) {
$badStr = preg_split( "/<\/?pre[^>]*>/i", $goodStr );
for ( $x = 0; isset($badStr[$x]) && is_string( $badStr[$x] ); $x++ ) { // Moodle: added isset() test
if ( $x % 2 ) { $badStr[$x] = '<pre>'.$badStr[$x].'</pre>'; } else {
$goodStr = preg_split( "/<\/?textarea[^>]*>/i", $badStr[$x] );
for ( $z = 0; isset($goodStr[$z]) && is_string( $goodStr[$z] ); $z++ ) { // Moodle: added isset() test
if ( $z % 2 ) { $goodStr[$z] = '<textarea>'.$goodStr[$z].'</textarea>'; } else {
$goodStr[$z] = preg_replace( "/\s+/", ' ', $goodStr[$z] );
}
}
$badStr[$x] = implode('',$goodStr);
}
}
@ -122,8 +131,8 @@ function html2text( $badStr ) {
$goodStr = preg_replace( "/<(th|td)[^>]*>/i", "\t", $goodStr );
// $goodStr = preg_replace( "/<a[^>]* href=(\"((?!\"|#|javascript:)[^\"#]*)(\"|#)|'((?!'|#|javascript:)[^'#]*)('|#)|((?!'|\"|>|#|javascript:)[^#\"'> ]*))[^>]*>/i", "[LINK: $2$4$6] ", $goodStr ); // Moodle
$goodStr = preg_replace( "/<a[^>]* href=(\"((?!\"|#|javascript:)[^\"#]*)(\"|#)|'((?!'|#|javascript:)[^'#]*)('|#)|((?!'|\"|>|#|javascript:)[^#\"'> ]*))[^>]*>/i", "[$2$4$6] ", $goodStr );
// $goodStr = preg_replace( "/<a[^>]* href=(\"((?!\"|#|javascript:)[^\"#]*)(\"|#)|'((?!'|#|javascript:)[^'#]*)('|#)|((?!'|\"|>|#|javascript:)[^#\"'> ]*))[^>]*>/i", "[LINK: $2$4$6] ", $goodStr ); // Moodle
$goodStr = preg_replace( "/<a[^>]* href=(\"((?!\"|#|javascript:)[^\"#]*)(\"|#)|'((?!'|#|javascript:)[^'#]*)('|#)|((?!'|\"|>|#|javascript:)[^#\"'> ]*))[^>]*>([^<]*)<\/a>/i", "$7 [$2$4$6]", $goodStr );
// $goodStr = preg_replace( "/<img[^>]* alt=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "[IMAGE: $2$3$4] ", $goodStr ); // Moodle
$goodStr = preg_replace( "/<img[^>]* alt=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "[$2$3$4] ", $goodStr );