|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Webman\Console\Commands; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Input\InputInterface; |
| 7 | +use Symfony\Component\Console\Output\OutputInterface; |
| 8 | + |
| 9 | + |
| 10 | +class FixDisbaleFunctionsCommand extends Command |
| 11 | +{ |
| 12 | + protected static $defaultName = 'fix-disable-functions'; |
| 13 | + protected static $defaultDescription = 'Fix disbale_functions in php.ini'; |
| 14 | + |
| 15 | + /** |
| 16 | + * @return void |
| 17 | + */ |
| 18 | + protected function configure() |
| 19 | + { |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @param InputInterface $input |
| 25 | + * @param OutputInterface $output |
| 26 | + * @return int |
| 27 | + */ |
| 28 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 29 | + { |
| 30 | + $php_ini_file = php_ini_loaded_file(); |
| 31 | + if (!$php_ini_file) { |
| 32 | + $output->writeln('<error>Can not find php.ini</error>'); |
| 33 | + return self::FAILURE; |
| 34 | + } |
| 35 | + $output->writeln("Location $php_ini_file"); |
| 36 | + $disable_functions_str = ini_get("disable_functions"); |
| 37 | + if (!$disable_functions_str) { |
| 38 | + $output->writeln('<success>Ok</success>'); |
| 39 | + } |
| 40 | + |
| 41 | + $functions_required = [ |
| 42 | + "stream_socket_server", |
| 43 | + "stream_socket_accept", |
| 44 | + "stream_socket_client", |
| 45 | + "pcntl_signal_dispatch", |
| 46 | + "pcntl_signal", |
| 47 | + "pcntl_alarm", |
| 48 | + "pcntl_fork", |
| 49 | + "posix_getuid", |
| 50 | + "posix_getpwuid", |
| 51 | + "posix_kill", |
| 52 | + "posix_setsid", |
| 53 | + "posix_getpid", |
| 54 | + "posix_getpwnam", |
| 55 | + "posix_getgrnam", |
| 56 | + "posix_getgid", |
| 57 | + "posix_setgid", |
| 58 | + "posix_initgroups", |
| 59 | + "posix_setuid", |
| 60 | + "posix_isatty", |
| 61 | + "proc_open", |
| 62 | + "proc_get_status", |
| 63 | + "proc_close", |
| 64 | + "shell_exec", |
| 65 | + ]; |
| 66 | + |
| 67 | + $has_disbaled_functions = false; |
| 68 | + foreach ($functions_required as $func) { |
| 69 | + if (strpos($disable_functions_str, $func) !== false) { |
| 70 | + $has_disbaled_functions = true; |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $disable_functions = explode(",", $disable_functions_str); |
| 76 | + $disable_functions_removed = []; |
| 77 | + foreach ($disable_functions as $index => $func) { |
| 78 | + $func = trim($func); |
| 79 | + foreach ($functions_required as $func_prefix) { |
| 80 | + if (strpos($func, $func_prefix) === 0) { |
| 81 | + $disable_functions_removed[$func] = $func; |
| 82 | + unset($disable_functions[$index]); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + $php_ini_content = file_get_contents($php_ini_file); |
| 88 | + if (!$php_ini_content) { |
| 89 | + $output->writeln("<error>$php_ini_file content empty</error>"); |
| 90 | + return self::FAILURE; |
| 91 | + } |
| 92 | + |
| 93 | + $new_disable_functions_str = implode(",", $disable_functions); |
| 94 | + $php_ini_content = preg_replace("/\ndisable_functions *?=[^\n]+/", "\ndisable_functions = $new_disable_functions_str", $php_ini_content); |
| 95 | + |
| 96 | + file_put_contents($php_ini_file, $php_ini_content); |
| 97 | + |
| 98 | + foreach ($disable_functions_removed as $func) { |
| 99 | + $output->write(str_pad($func, 30)); |
| 100 | + $output->writeln('<info>enabled</info>'); |
| 101 | + } |
| 102 | + |
| 103 | + $output->writeln('<info>Succes</info>'); |
| 104 | + return self::SUCCESS; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments