Merge pull request #3640 from Icinga/bugfix/pdf-export-respect-php-ini-limits
Respect php.ini limits when exporting to PDF
This commit is contained in:
commit
574e7671c3
|
@ -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(),
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Icinga\Util;
|
||||
|
||||
/**
|
||||
* Helper for configuring the PHP environment
|
||||
*/
|
||||
class Environment
|
||||
{
|
||||
/**
|
||||
* Raise the PHP memory_limit
|
||||
*
|
||||
* Unless it is not already set to a higher limit
|
||||
*
|
||||
* @param string|int $minimum
|
||||
*/
|
||||
public static function raiseMemoryLimit($minimum = '512M')
|
||||
{
|
||||
if (is_string($minimum)) {
|
||||
$minimum = Format::unpackShorthandBytes($minimum);
|
||||
}
|
||||
|
||||
if (Format::unpackShorthandBytes(ini_get('memory_limit')) < $minimum) {
|
||||
ini_set('memory_limit', $minimum);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Raise the PHP max_execution_time
|
||||
*
|
||||
* Unless it is not already configured to a higher value.
|
||||
*
|
||||
* @param int $minimum
|
||||
*/
|
||||
public static function raiseExecutionTime($minimum = 300)
|
||||
{
|
||||
if ((int) ini_get('max_execution_time') < $minimum) {
|
||||
ini_set('max_execution_time', $minimum);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */
|
||||
|
||||
namespace Tests\Icinga\Util;
|
||||
|
||||
use Icinga\Util\Environment;
|
||||
use Icinga\Test\BaseTestCase;
|
||||
|
||||
class EnvironmentTest extends BaseTestCase
|
||||
{
|
||||
public function testRaiseMemoryLimit()
|
||||
{
|
||||
// set a low limit
|
||||
ini_set('memory_limit', '128M');
|
||||
|
||||
Environment::raiseMemoryLimit('512M');
|
||||
$this->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');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue