diff --git a/library/Icinga/File/Pdf.php b/library/Icinga/File/Pdf.php index 242e2ddd8..dfe8f74d3 100644 --- a/library/Icinga/File/Pdf.php +++ b/library/Icinga/File/Pdf.php @@ -8,6 +8,7 @@ use Dompdf\Dompdf; use Dompdf\Options; use Icinga\Application\Icinga; use Icinga\Exception\ProgrammingError; +use Icinga\Util\Environment; use Icinga\Web\Hook; use Icinga\Web\Url; @@ -45,8 +46,10 @@ class Pdf public function renderControllerAction($controller) { $this->assertNoHeadersSent(); - ini_set('memory_limit', '384M'); - ini_set('max_execution_time', 300); + + Environment::raiseMemoryLimit('512M'); + Environment::raiseExecutionTime(300); + $viewRenderer = $controller->getHelper('viewRenderer'); $controller->render( $viewRenderer->getScriptAction(), diff --git a/library/Icinga/Util/Environment.php b/library/Icinga/Util/Environment.php new file mode 100644 index 000000000..8d47b84d5 --- /dev/null +++ b/library/Icinga/Util/Environment.php @@ -0,0 +1,42 @@ +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; + } } diff --git a/test/php/library/Icinga/Util/EnvironmentTest.php b/test/php/library/Icinga/Util/EnvironmentTest.php new file mode 100644 index 000000000..5f4c8aedf --- /dev/null +++ b/test/php/library/Icinga/Util/EnvironmentTest.php @@ -0,0 +1,43 @@ +assertEquals('536870912' /* 512M */, ini_get('memory_limit')); + + Environment::raiseMemoryLimit('1G'); + $this->assertEquals('1073741824' /* 1G */, ini_get('memory_limit')); + + Environment::raiseMemoryLimit('512M'); + $this->assertEquals('1073741824' /* 1G */, ini_get('memory_limit')); + + // in phpunit usually there is no limit + ini_set('memory_limit', '-1'); + } + + public function testRaiseExecutionTime() + { + Environment::raiseExecutionTime(300); + $this->assertEquals(300, ini_get('max_execution_time')); + + Environment::raiseExecutionTime(600); + $this->assertEquals(600, ini_get('max_execution_time')); + + Environment::raiseExecutionTime(300); + $this->assertEquals(600, ini_get('max_execution_time')); + + // in phpunit usually there is no limit + ini_set('max_execution_time', '0'); + } +}