2024.8.23
Loading...
Searching...
No Matches
class.action.switchPhpParam.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
10/**
11 * Class ActionSwitchPhpParam
12 *
13 * This class handles the switching of PHP parameters on and off.
14 * It modifies the PHP configuration file based on the provided arguments.
15 */
17{
18 const SWITCH_ON = 'on';
19 const SWITCH_OFF = 'off';
20
21 /**
22 * Constructor for ActionSwitchPhpParam.
23 *
24 * @param array $args An array containing the PHP setting name and the desired state ('on' or 'off').
25 */
26 public function __construct($args)
27 {
28 global $bearsamppLang, $bearsamppBins, $bearsamppWinbinder;
29
30 // Check if the required arguments are provided and not empty
31 if (isset($args[0]) && !empty($args[0]) && isset($args[1]) && !empty($args[1])) {
32 // Check if the PHP setting exists
33 if (!$bearsamppBins->getPhp()->isSettingExists($args[0])) {
34 $bearsamppWinbinder->messageBoxError(
35 sprintf($bearsamppLang->getValue(Lang::SWITCH_PHP_SETTING_NOT_FOUND), $args[0], $bearsamppBins->getPhp()->getVersion()),
37 );
38 return;
39 }
40
41 // Retrieve the current settings values
42 $settingsValues = $bearsamppBins->getPhp()->getSettingsValues();
43 if (isset($settingsValues[$args[0]])) {
44 $onContent = $args[0] . ' = ' . $settingsValues[$args[0]][0];
45 $offContent = $args[0] . ' = ' . $settingsValues[$args[0]][1];
46
47 // Read the current PHP configuration file content
48 $phpiniContent = file_get_contents($bearsamppBins->getPhp()->getConf());
49 if ($args[1] == self::SWITCH_ON) {
50 // Replace the off setting with the on setting
51 $phpiniContent = str_replace($offContent, $onContent, $phpiniContent);
52 } elseif ($args[1] == self::SWITCH_OFF) {
53 // Replace the on setting with the off setting
54 $phpiniContent = str_replace($onContent, $offContent, $phpiniContent);
55 }
56
57 // Write the updated content back to the PHP configuration file
58 file_put_contents($bearsamppBins->getPhp()->getConf(), $phpiniContent);
59 }
60 }
61 }
62}
global $bearsamppBins
global $bearsamppLang
const SWITCH_PHP_SETTING_TITLE
const SWITCH_PHP_SETTING_NOT_FOUND