Respect php.ini limits when exporting to PDF

This commit is contained in:
Eric Lippmann 2018-12-03 15:29:19 +01:00 committed by Markus Frosch
parent a95e645236
commit 87da1e76cd
2 changed files with 45 additions and 4 deletions

View File

@ -8,6 +8,7 @@ use Dompdf\Dompdf;
use Dompdf\Options;
use Icinga\Application\Icinga;
use Icinga\Exception\ProgrammingError;
use Icinga\Util\Format;
use Icinga\Web\Hook;
use Icinga\Web\Url;
@ -45,8 +46,17 @@ class Pdf
public function renderControllerAction($controller)
{
$this->assertNoHeadersSent();
ini_set('memory_limit', '384M');
ini_set('max_execution_time', 300);
$memLimit = 512 * 1024 ** 2;
if (Format::unpackShorthandBytes(ini_get('memory_limit')) < $memLimit) {
ini_set('memory_limit', $memLimit);
}
$maxExecTime = 300;
if ((int) ini_get('max_execution_time') < $maxExecTime) {
ini_set('max_execution_time', $maxExecTime);
}
$viewRenderer = $controller->getHelper('viewRenderer');
$controller->render(
$viewRenderer->getScriptAction(),

View File

@ -4,8 +4,6 @@
namespace Icinga\Util;
use DateTime;
use Icinga\Date\DateFormatter;
use Icinga\Exception\ProgrammingError;
class Format
{
@ -141,4 +139,37 @@ class Format
return $dt->format('L') == 1;
}
/**
* Unpack shorthand bytes PHP directives to bytes
*
* @param string $subject
*
* @return int
*/
public static function unpackShorthandBytes($subject)
{
$base = (int) $subject;
if ($base <= -1) {
return INF;
}
switch (strtoupper($subject[strlen($subject) - 1])) {
case 'K':
$multiplier = 1024;
break;
case 'M':
$multiplier = 1024 ** 2;
break;
case 'G':
$multiplier = 1024 ** 3;
break;
default:
$multiplier = 1;
break;
}
return $base * $multiplier;
}
}