Last active
August 29, 2015 14:06
-
-
Save markoheijnen/1fb8aef0b3069d813ec7 to your computer and use it in GitHub Desktop.
Cleaning up deprecated options in php.ini. Add this file in the root directory and call it from a browser. This script does the rest.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PHP_Ini_Fixes { | |
private $deprecated_options = array( | |
// Can result in issue in different PHP versions | |
'browscap', | |
//PHP 5.4 removed | |
'register_globals', 'register_long_arrays', | |
'magic_quotes_gpc', 'magic_quotes_runtime', 'magic_quotes_sybase', | |
'allow_call_time_pass_reference', | |
'mbstring.script_encoding', | |
'safe_mode', 'safe_mode_gid', 'safe_mode_include_dir', 'safe_mode_exec_dir', | |
'safe_mode_allowed_env_vars', 'safe_mode_protected_env_vars', | |
); | |
private $fixed_files = array(); | |
public function __construct( $dir ) { | |
if ( ! $dir ) { | |
$dir = dirname( __FILE__ ); | |
} | |
$this->crawl_dir( $dir . '/' ); | |
} | |
public function show_fixed_files() { | |
if ( ! $this->fixed_files ) { | |
echo '<p>All files are already fixed. You are ready to go to use the latest PHP version.</p>'; | |
} | |
else { | |
echo '<ul>'; | |
foreach( $this->fixed_files as $file ) { | |
echo '<li>' . $file . '</li>'; | |
} | |
echo '</ul>'; | |
} | |
} | |
private function crawl_dir( $dir ) { | |
$opendir = opendir( $dir ); | |
while ( false !== ( $file = readdir( $opendir ) ) ) { | |
if ( '.' == $file || '..' == $file ) { | |
continue; | |
} | |
if ( is_dir( $dir . $file ) ) { | |
$this->crawl_dir( $dir . $file . '/' ); | |
} | |
if ( 'php.ini' == $file ) { | |
$this->fix_php_ini_file( $dir . $file ); | |
} | |
} | |
closedir( $opendir ); | |
} | |
private function fix_php_ini_file( $file ) { | |
$changed = false; | |
$content = file_get_contents( $file ); | |
$content_parts = explode( PHP_EOL, $content ); | |
foreach ( $content_parts as $key => $part ) { | |
$needle = strtok( $part, ' ' ); | |
if ( in_array( $needle, $this->deprecated_options ) ) { | |
$changed = true; | |
unset( $content_parts[ $key ] ); | |
} | |
} | |
$content = implode( PHP_EOL, $content_parts ); | |
if ( $changed ) { | |
$this->fixed_files[] = $file; | |
file_put_contents( $file, $content ); | |
} | |
} | |
} | |
echo '<h1>php.ini file fixed</h1>'; | |
$fixer = new PHP_Ini_Fixes(false); | |
$fixer->show_fixed_files(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment