Commit bb3d2fc0 authored by mikeytown2's avatar mikeytown2

Issue #1983624 by mikeytown2: Fix UTF-8 encoding & limit print lenght.

parent 8e662c9f
......@@ -122,6 +122,11 @@ define('HTTPRL_HOST_NOT_FOUND', -11001);
*/
define('HTTPRL_MULTIPART_BOUNDARY', '---------------------------' . str_replace('.', '', microtime(TRUE)));
/**
* Max length of a string inside of httprl_pr(). Default is 256KB.
*/
define('HTTPRL_PR_MAX_STRING_LENGTH', 262144);
/**
* Implement hook_menu().
*/
......@@ -1301,7 +1306,7 @@ function httprl_send_request($results = NULL) {
$n = stream_select($read, $write, $except, $stream_select_timeout, 25000);
$stream_select_timeout = 0;
// An error occured with the streams. Remove bad ones.
// An error occurred with the streams. Remove bad ones.
if ($n === FALSE) {
$merged = array_unique(array_merge($read, $write, $except));
foreach ($merged as $m) {
......@@ -2145,7 +2150,7 @@ function httprl_decode_data(&$result) {
}
}
// Cut up respose for one sided Range requests.
// Cut up response for one sided Range requests.
if (array_key_exists('max_data_size', $result->options)) {
$result->code = 206;
......@@ -2439,9 +2444,14 @@ function httprl_print_empty(&$data, $level = 0) {
$values = httprl_print_empty($values, $level);
}
}
elseif (is_bool($data) || strlen($data) == 0) {
elseif (is_bool($data) || strlen((string) $data) == 0) {
$data = strtoupper(var_export($data, TRUE));
}
elseif (is_string($data) && strlen($data) > HTTPRL_PR_MAX_STRING_LENGTH) {
// Do not try to print a string longer than 256KB.
// Some browsers have issues with very long documents.
$data = substr($data, 0, HTTPRL_PR_MAX_STRING_LENGTH);
}
}
return $data;
}
......@@ -2455,6 +2465,8 @@ function httprl_print_empty(&$data, $level = 0) {
* Human readable HTML version of the data.
*/
function httprl_pr($data) {
$old_setting = ini_set('mbstring.substitute_character', '"none"');
// Get extra arguments passed in.
$data = func_get_args();
......@@ -2466,8 +2478,46 @@ function httprl_pr($data) {
$data = array_pop($data);
}
// Remove non UTF-8 Characters, escape HTML markup, remove extra new lines.
$output = array_filter(explode("\n", htmlentities(iconv('utf-8', 'utf-8//IGNORE', print_r($data, TRUE)), ENT_QUOTES, 'UTF-8')), 'strlen');
// Print_r the input.
$output = print_r($data, TRUE);
// Remove non UTF-8 Characters.
if (function_exists('mb_convert_encoding')) {
$translated = mb_convert_encoding($output, 'UTF-8', 'auto');
}
else {
$translated = @iconv('utf-8', 'utf-8//TRANSLIT//IGNORE', $output);
}
// Convert html entities.
$options = ENT_QUOTES;
if (defined('ENT_SUBSTITUTE')) {
$options = ENT_QUOTES | ENT_SUBSTITUTE;
}
elseif (defined('ENT_IGNORE')) {
$options = ENT_QUOTES | ENT_IGNORE;
}
$translated = htmlentities($translated, $options, 'UTF-8');
// Make sure the UTF-8 translation didn't kill the output.
$original_size = strlen($output);
$translated_size = strlen($translated);
$ratio = 0;
if ($original_size != 0) {
$ratio = ($original_size - $translated_size) / $original_size;
}
// Decide to use the original output or the translated one.
if (!empty($translated_size) && !empty($ratio) && $ratio < 0.5) {
$html_output = TRUE;
$output = $translated;
}
else {
$output = '<pre>' . str_replace(array('<', '>'), array('&lt;', '&gt;'), $output) . '</pre>';
}
// Remove extra new lines.
$output = array_filter(explode("\n", $output), 'strlen');
// Whitespace compression.
foreach ($output as $key => $value) {
......@@ -2478,7 +2528,12 @@ function httprl_pr($data) {
}
// Replace whitespace with html markup.
$output = str_replace(' ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(implode("\n", $output))) . '<br />';
$output = implode("\n", $output);
if (!empty($html_output)) {
$output = str_replace(' ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br($output)) . '<br />';
}
ini_set('mbstring.substitute_character', $old_setting);
return $output;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment