2024.8.23
Loading...
Searching...
No Matches
class.action.switchPhpExtension.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 ActionSwitchPhpExtension
12 *
13 * This class is responsible for enabling or disabling PHP extensions by modifying the php.ini configuration file.
14 */
16{
17 const SWITCH_ON = 'on';
18 const SWITCH_OFF = 'off';
19
20 /**
21 * ActionSwitchPhpExtension constructor.
22 *
23 * @param array $args An array containing the extension name and the action (on/off).
24 */
25 public function __construct($args)
26 {
27 global $bearsamppBins;
28
29 if (isset($args[0]) && !empty($args[0]) && isset($args[1]) && !empty($args[1])) {
30 $onContent = 'extension=' . $args[0];
31 $offContent = ';extension=' . $args[0];
32 if (version_compare($bearsamppBins->getPhp()->getVersion(), '7.2', '<')) {
33 $onContent = 'extension=php_' . $args[0] . '.dll';
34 $offContent = ';extension=php_' . $args[0] . '.dll';
35 }
36
37 $phpiniContent = file_get_contents($bearsamppBins->getPhp()->getConf());
38 if ($args[1] == self::SWITCH_ON) {
39 $phpiniContent = str_replace($offContent, $onContent, $phpiniContent);
40 } elseif ($args[1] == self::SWITCH_OFF) {
41 $phpiniContent = str_replace($onContent, $offContent, $phpiniContent);
42 }
43
44 $phpiniContentOr = file_get_contents($bearsamppBins->getPhp()->getConf());
45 if ($phpiniContent == $phpiniContentOr && file_exists($bearsamppBins->getPhp()->getSymlinkPath() . '/ext/php_' . $args[0] . '.dll')) {
46 $extsIni = $bearsamppBins->getPhp()->getExtensionsFromConf();
47 $latestExt = (end($extsIni) == '0' ? ';' : '');
48 if (version_compare($bearsamppBins->getPhp()->getVersion(), '7.2', '<')) {
49 $latestExt .= 'extension=php_' . key($extsIni) . '.dll';
50 } else {
51 $latestExt .= 'extension=' . key($extsIni);
52 }
53 $phpiniContent = str_replace(
54 $latestExt,
55 $latestExt . PHP_EOL . $onContent,
56 $phpiniContent
57 );
58 }
59
60 file_put_contents($bearsamppBins->getPhp()->getConf(), $phpiniContent);
61 }
62 }
63}
global $bearsamppBins