From 6a1ee504cc412e52798106a22037b3d3f6462825 Mon Sep 17 00:00:00 2001
From: Christopher Cope <ccope@netgate.com>
Date: Thu, 28 Jul 2022 13:05:58 -0400
Subject: [PATCH] Add a setting for PHP memory limit in System -> Advanced.
 Feature #13377

---
 src/etc/inc/config.inc                          |  5 +++++
 .../include/www/system_advanced_misc.inc        |  8 ++++++++
 src/usr/local/www/system_advanced_misc.php      | 17 +++++++++++++++++
 3 files changed, 30 insertions(+)

diff --git a/src/etc/inc/config.inc b/src/etc/inc/config.inc
index e5a5b153e5..2b03752e96 100644
--- a/src/etc/inc/config.inc
+++ b/src/etc/inc/config.inc
@@ -154,6 +154,11 @@ if (isset($config['system']['timezone']) &&
 }
 date_default_timezone_set("$timezone");
 
+/* override php memory limit */
+if (isset($config['system']['php_memory_limit']) && !empty($config['system']['php_memory_limit'])) {
+    ini_set("memory_limit", $config['system']['php_memory_limit'] . "M");
+}
+
 if ($config_parsed == true) {
 	/* process packager manager custom rules */
 	if (is_dir("/usr/local/pkg/parse_config")) {
diff --git a/src/usr/local/pfSense/include/www/system_advanced_misc.inc b/src/usr/local/pfSense/include/www/system_advanced_misc.inc
index 2f66a63aee..6812bf876c 100644
--- a/src/usr/local/pfSense/include/www/system_advanced_misc.inc
+++ b/src/usr/local/pfSense/include/www/system_advanced_misc.inc
@@ -70,6 +70,7 @@ function getSystemAdvancedMisc($json = false) {
 	$pconfig['do_not_send_uniqueid'] = isset($config['system']['do_not_send_uniqueid']);
 	$pconfig['pti'] = get_single_sysctl('vm.pmap.pti');
 	$pconfig['mds']= get_single_sysctl('hw.mds_disable_state');
+	$pconfig['php_memory_limit'] = $config['system']['php_memory_limit'];
 
 	$pconfig['powerd_ac_mode'] = "hadp";
 	if (!empty($config['system']['powerd_ac_mode'])) {
@@ -359,6 +360,13 @@ function saveSystemAdvancedMisc($post, $json = false) {
 			}
 		}
 
+		// PHP Memory Limit
+		if (isset($post['php_memory_limit']) && !empty($post['php_memory_limit'])) {
+			$config['system']['php_memory_limit'] = $post['php_memory_limit'];
+		} else {
+			unset($config['system']['php_memory_limit']);
+		}
+
 		// Add/Remove RAM disk periodic backup cron jobs according to settings and installation type.
 		// Remove the cron jobs on full install if not using RAM disk.
 		// Add the cron jobs on all others if the periodic backup option is set.  Otherwise the cron job is removed.
diff --git a/src/usr/local/www/system_advanced_misc.php b/src/usr/local/www/system_advanced_misc.php
index 219625978b..34d935d8a2 100644
--- a/src/usr/local/www/system_advanced_misc.php
+++ b/src/usr/local/www/system_advanced_misc.php
@@ -319,6 +319,23 @@ $section->addInput(new Form_Checkbox(
 	'Enabling this checkbox overrides that behavior.');
 
 $form->add($section);
+
+$section = new Form_Section('PHP Settings');
+
+$group = new Form_Group('Memory Limit');
+
+$group->add(new Form_Input(
+	'php_memory_limit',
+	'Memory Limit',
+	'number',
+	str_replace("M", "", $pconfig['php_memory_limit']),
+	['placeholder' => str_replace("M", "", ini_get('memory_limit'))]
+))->setHelp('PHP memory limit in MiB.');
+
+$section->add($group);
+
+$form->add($section);
+
 $section = new Form_Section('RAM Disk Settings (Reboot to Apply Changes)');
 
 $section->addInput(new Form_Checkbox(
-- 
2.25.1

