mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-30 19:00:10 +02:00
[2.1.5] [MFH] Complete the imagecrash added protection fixes
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/branches/php4@1785 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Length.php';
|
||||
|
||||
/**
|
||||
* Class for converting between different unit-lengths as specified by
|
||||
* CSS.
|
||||
@@ -9,10 +7,6 @@ require_once 'HTMLPurifier/Length.php';
|
||||
class HTMLPurifier_UnitConverter
|
||||
{
|
||||
|
||||
// For documentation purposes: these are magical numbers
|
||||
// const ENGLISH = 1;
|
||||
// const METRIC = 2;
|
||||
|
||||
/**
|
||||
* Minimum bcmath precision for output.
|
||||
*/
|
||||
@@ -23,13 +17,24 @@ class HTMLPurifier_UnitConverter
|
||||
*/
|
||||
var $internalPrecision;
|
||||
|
||||
function HTMLPurifier_UnitConverter($output_precision = 4, $internal_precision = 10) {
|
||||
/**
|
||||
* Whether or not BCMath is available
|
||||
*/
|
||||
var $bcmath;
|
||||
|
||||
function HTMLPurifier_UnitConverter($output_precision = 4, $internal_precision = 10, $force_no_bcmath = false) {
|
||||
$this->outputPrecision = $output_precision;
|
||||
$this->internalPrecision = $internal_precision;
|
||||
$this->bcmath = !$force_no_bcmath && function_exists('bcmul');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a length object of one unit into another unit.
|
||||
* @param HTMLPurifier_Length $length
|
||||
* Instance of HTMLPurifier_Length to convert. You must validate()
|
||||
* it before passing it here!
|
||||
* @param string $to_unit
|
||||
* Unit to convert to.
|
||||
* @note
|
||||
* About precision: This conversion function pays very special
|
||||
* attention to the incoming precision of values and attempts
|
||||
@@ -40,9 +45,6 @@ class HTMLPurifier_UnitConverter
|
||||
* - If a number contains less than four sigfigs ($outputPrecision)
|
||||
* and this causes some decimals to be excluded, those
|
||||
* decimals will be added on.
|
||||
* - Significant digits will be ignored for quantities greater
|
||||
* than one. This is a limitation of BCMath and I don't
|
||||
* feel like coding around it.
|
||||
*/
|
||||
function convert($length, $to_unit) {
|
||||
|
||||
@@ -57,9 +59,10 @@ class HTMLPurifier_UnitConverter
|
||||
*/
|
||||
static $units = array(
|
||||
1 => array(
|
||||
'pt' => 1,
|
||||
'pc' => 12,
|
||||
'in' => 72,
|
||||
'px' => 3, // This is as per CSS 2.1 and Firefox. Your mileage may vary
|
||||
'pt' => 4,
|
||||
'pc' => 48,
|
||||
'in' => 288,
|
||||
2 => array('pt', '0.352777778', 'mm'),
|
||||
),
|
||||
2 => array(
|
||||
@@ -69,34 +72,32 @@ class HTMLPurifier_UnitConverter
|
||||
),
|
||||
);
|
||||
|
||||
if ($length->n === '0' || $length->unit === false) {
|
||||
return new HTMLPurifier_Length('0', $unit);
|
||||
if (!$length->isValid()) return false;
|
||||
|
||||
$n = $length->getN();
|
||||
$unit = $length->getUnit();
|
||||
|
||||
if ($n === '0' || $unit === false) {
|
||||
return new HTMLPurifier_Length('0', false);
|
||||
}
|
||||
|
||||
$state = $dest = false;
|
||||
$state = $dest_state = false;
|
||||
foreach ($units as $k => $x) {
|
||||
if (isset($x[$length->unit])) $state = $k;
|
||||
if (isset($x[$unit])) $state = $k;
|
||||
if (isset($x[$to_unit])) $dest_state = $k;
|
||||
}
|
||||
if (!$state || !$dest_state) return false;
|
||||
|
||||
$n = $length->n;
|
||||
$unit = $length->unit;
|
||||
|
||||
// Some calculations about the initial precision of the number;
|
||||
// this will be useful when we need to do final rounding.
|
||||
$log = (int) floor(log($n, 10));
|
||||
if (strpos($n, '.') === false) {
|
||||
$sigfigs = strlen(trim($n, '0+-'));
|
||||
} else {
|
||||
$sigfigs = strlen(ltrim($n, '0+-')) - 1; // eliminate extra decimal character
|
||||
}
|
||||
$sigfigs = $this->getSigFigs($n);
|
||||
if ($sigfigs < $this->outputPrecision) $sigfigs = $this->outputPrecision;
|
||||
|
||||
// BCMath's internal precision deals only with decimals. Use
|
||||
// our default if the initial number has no decimals, or increase
|
||||
// it by how ever many decimals, thus, the number of guard digits
|
||||
// will always be greater than or equal to internalPrecision.
|
||||
$log = (int) floor(log(abs($n), 10));
|
||||
$cp = ($log < 0) ? $this->internalPrecision - $log : $this->internalPrecision; // internal precision
|
||||
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
@@ -112,12 +113,12 @@ class HTMLPurifier_UnitConverter
|
||||
|
||||
// Do the conversion if necessary
|
||||
if ($dest_unit !== $unit) {
|
||||
$factor = bcdiv($units[$state][$unit], $units[$state][$dest_unit], $cp);
|
||||
$n = bcmul($n, $factor, $cp);
|
||||
$factor = $this->div($units[$state][$unit], $units[$state][$dest_unit], $cp);
|
||||
$n = $this->mul($n, $factor, $cp);
|
||||
$unit = $dest_unit;
|
||||
}
|
||||
|
||||
// Output was zero, so bail out early
|
||||
// Output was zero, so bail out early. Shouldn't ever happen.
|
||||
if ($n === '') {
|
||||
$n = '0';
|
||||
$unit = $to_unit;
|
||||
@@ -138,7 +139,7 @@ class HTMLPurifier_UnitConverter
|
||||
// Pre-condition: $i == 0
|
||||
|
||||
// Perform conversion to next system of units
|
||||
$n = bcmul($n, $units[$state][$dest_state][1], $cp);
|
||||
$n = $this->mul($n, $units[$state][$dest_state][1], $cp);
|
||||
$unit = $units[$state][$dest_state][2];
|
||||
$state = $dest_state;
|
||||
|
||||
@@ -149,20 +150,87 @@ class HTMLPurifier_UnitConverter
|
||||
// Post-condition: $unit == $to_unit
|
||||
if ($unit !== $to_unit) return false;
|
||||
|
||||
// Calculate how many decimals we need ($rp)
|
||||
// Calculations will always be carried to the decimal; this is
|
||||
// a limitation with BC (we can't set the scale to be negative)
|
||||
$new_log = (int) floor(log($n, 10));
|
||||
// Useful for debugging:
|
||||
//echo "<pre>n";
|
||||
//echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n";
|
||||
|
||||
$rp = $sigfigs - $new_log - $log - 1;
|
||||
if ($rp < 0) $rp = 0;
|
||||
|
||||
$n = bcadd($n, '0.' . str_repeat('0', $rp) . '5', $rp + 1);
|
||||
$n = bcdiv($n, '1', $rp);
|
||||
$n = $this->round($n, $sigfigs);
|
||||
if (strpos($n, '.') !== false) $n = rtrim($n, '0');
|
||||
$n = rtrim($n, '.');
|
||||
|
||||
return new HTMLPurifier_Length($n, $unit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of significant figures in a string number.
|
||||
* @param string $n Decimal number
|
||||
* @return int number of sigfigs
|
||||
*/
|
||||
function getSigFigs($n) {
|
||||
$n = ltrim($n, '0+-');
|
||||
$dp = strpos($n, '.'); // decimal position
|
||||
if ($dp === false) {
|
||||
$sigfigs = strlen(rtrim($n, '0'));
|
||||
} else {
|
||||
$sigfigs = strlen(ltrim($n, '0.')); // eliminate extra decimal character
|
||||
if ($dp !== 0) $sigfigs--;
|
||||
}
|
||||
return $sigfigs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds two numbers, using arbitrary precision when available.
|
||||
*/
|
||||
function add($s1, $s2, $scale) {
|
||||
if ($this->bcmath) return bcadd($s1, $s2, $scale);
|
||||
else return $this->scale($s1 + $s2, $scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiples two numbers, using arbitrary precision when available.
|
||||
*/
|
||||
function mul($s1, $s2, $scale) {
|
||||
if ($this->bcmath) return bcmul($s1, $s2, $scale);
|
||||
else return $this->scale($s1 * $s2, $scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides two numbers, using arbitrary precision when available.
|
||||
*/
|
||||
function div($s1, $s2, $scale) {
|
||||
if ($this->bcmath) return bcdiv($s1, $s2, $scale);
|
||||
else return $this->scale($s1 / $s2, $scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds a number according to the number of sigfigs it should have,
|
||||
* using arbitrary precision when available.
|
||||
*/
|
||||
function round($n, $sigfigs) {
|
||||
$new_log = (int) floor(log(abs($n), 10)); // Number of digits left of decimal - 1
|
||||
$rp = $sigfigs - $new_log - 1; // Number of decimal places needed
|
||||
$neg = $n < 0 ? '-' : ''; // Negative sign
|
||||
if ($this->bcmath) {
|
||||
if ($rp >= 0) {
|
||||
$n = bcadd($n, $neg . '0.' . str_repeat('0', $rp) . '5', $rp + 1);
|
||||
$n = bcdiv($n, '1', $rp);
|
||||
} else {
|
||||
// This algorithm partially depends on the standardized
|
||||
// form of numbers that comes out of bcmath.
|
||||
$n = bcadd($n, $neg . '5' . str_repeat('0', $new_log - $sigfigs), 0);
|
||||
$n = substr($n, 0, $sigfigs + strlen($neg)) . str_repeat('0', $new_log - $sigfigs + 1);
|
||||
}
|
||||
return $n;
|
||||
} else {
|
||||
return $this->scale(round($n, $sigfigs - $new_log - 1), $rp + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scales a float to $scale digits right of decimal point, like BCMath.
|
||||
*/
|
||||
function scale($r, $scale) {
|
||||
return sprintf('%.' . $scale . 'f', (float) $r);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user