Skip to content

Commit 15cc6f5

Browse files
authored
Issue #165 Proper check for memory_limit config (#180)
* fix #165 : Handle shorthand notation for PHP memory_limit check * fix config default memory threshold.
1 parent 364c056 commit 15cc6f5

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

lib/config_default.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public function __construct() {
504504

505505
$this->default->session['memorylimit'] = array(
506506
'desc'=>'Set the PHP memorylimit warning threshold.',
507-
'default'=>24);
507+
'default'=>'24M');
508508

509509
$this->default->session['timelimit'] = array(
510510
'desc'=>'Set the PHP timelimit.',

lib/functions.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,38 @@ function check_config($config_file) {
333333
$config->setServers($servers);
334334

335335
# Check the memory limit parameter.
336-
if ((ini_get('memory_limit') > -1) && ini_get('memory_limit') < $config->getValue('session','memorylimit'))
337-
system_message(array(
338-
'title'=>_('Memory Limit low.'),
339-
'body'=>sprintf('Your php memory limit is low - currently %s, you should increase it to atleast %s. This is normally controlled in /etc/php.ini.',
340-
ini_get('memory_limit'),$config->getValue('session','memorylimit')),
341-
'type'=>'error'));
342-
336+
$limit = memory_str_to_int(ini_get('memory_limit'));
337+
if ($limit != -1) {
338+
$threshold = memory_str_to_int($config->getValue('session','memorylimit'));
339+
if ($limit < $threshold) {
340+
system_message(array(
341+
'title' => _('Memory Limit low.'),
342+
'body' => sprintf('Your php memory limit is low - currently %s (%s), you should increase it to atleast %s (%s). This is normally controlled in /etc/php.ini.',
343+
ini_get('memory_limit'), $limit, $config->getValue('session','memorylimit'), $threshold),
344+
'type'=>'error'
345+
));
346+
}
347+
}
343348
return $config;
344349
}
345350

351+
/**
352+
* Converts shorthand memory notation string to an integer that represents the
353+
* given amount in bytes (ie. "128M" -> 134217728).
354+
*
355+
* @param string|int $value
356+
* @return int
357+
*/
358+
function memory_str_to_int($value) {
359+
$value = trim(strtolower($value));
360+
if (intval($value) > 0 && preg_match('/^(\d+)([kmg])?$/', $value, $match, PREG_UNMATCHED_AS_NULL)) {
361+
[$int, $mod] = [intval($match[1]), $match[2]];
362+
$pow = [NULL => 0, 'k' => 1, 'm' => 2, 'g' => 3][$mod];
363+
return $int * 1024 ** $pow;
364+
}
365+
return intval($value);
366+
}
367+
346368
/**
347369
* Commands available in the control_panel of the page
348370
*

0 commit comments

Comments
 (0)