Outdated PHP Nuke code

Bulk update scripts to PHP 5.4+

I’ve written a shell script to bulk update PHP pages, replacing ereg, eregi with preg_match, ereg_match with preg_match, split with preg_split and changing the $HTTP globals to their modern versions. I haven’t seen this anywhere else, so I’d thought I should post something here.

#!/bin/sh

################################################################
#                                                              #
#  FIX DEPRECATED PHP FUNCTIONS                                #
#                                                              #
#  Coded by Noah Hearle, Design Extreme                        #
#  http://designextreme.com                                    #
#  With thanks to: Peter Bowey, pbcomp.com.au                  #
#                                                              #
#  Created: 2015/02/27                                         #
#  Modified: 2015/06/26                                        #
#                                                              #
#  Change old PHP functions to their modern equivalent         #
#                                                              #
#  ereg()                 => preg_match()                      #
#  ereg_replace()         => preg_replace()                    #
#  split()                => preg_split()                      #
#  mysql_escape_string()  => mysql_real_escape_string()        #
#  $HTTP_GET_VARS[]       => $_GET[]                           #
#  $HTTP_POST_VARS[]      => $_POST[]                          #
#  $HTTP_REQUEST_VARS[]   => $_REQUEST[]                       #
#  $HTTP_SESSION_VARS[]   => $_SESSION[]                       #
#  $HTTP_COOKIE_VARS[]    => $_COOKIE[]                        #
#  $HTTP_SERVER_VARS[]    => $_SERVER[]                        #
#  $HTTP_POST_FILES[]     => $_FILES[]                         #
#                                                              #
#  Post your comments at:                                      #
#  https://blog.nahoo.co.uk/bulk-update-scripts-to-php-5-4/ #
#                                                              #
#  Usage: sh ./php_update.sh <directory>                       #  
#                                                              #
################################################################

directory="$@"

if [ ! -n "$directory" ]; then
    echo "ERROR: Missing target PHP directory.";
else
    for file in $(find $directory -type f \( -name "*.php" -o -name "*.inc" -o -name "*.install" -o -name "*.module" -o -name "*.html" \));
    do
        file_check=$(grep -El "([^_]eregi?(_replace)?[(]|[^_.]split[(]|mysql_escape_string[(]|\\\$HTTP_(POST_FILES|((GET|POST|REQUEST|SESSION|COOKIE|SERVER|ENV)_VARS)))" "$file");
        if [ -n "$file_check" ]; then
            perl -pi -e "s@\\\$HTTP_(GET|POST|REQUEST|SESSION|COOKIE|SERVER|ENV)_VARS@\\\$_\1@g" $file;
            perl -pi -e "s@\\\$HTTP_POST_(FILES)@\\\$_\1@g" $file;
            perl -pi -e "s@([^_])ereg(i?)\(['\"]([^\/'\"]*)['\"], ?@\1preg_match('\/\3\/\2', @g" $file;
            perl -pi -e "s@([^_])ereg(i?)\(['\"]([^#'\"]*)['\"], ?@\1preg_match('#\3#\2', @g" $file;
            perl -pi -e "s@([^_])ereg(i?)\(['\"]([^~'\"]*)['\"], ?@\1preg_match('~\3~\2', @g" $file;
            perl -pi -e "s@([^_])ereg(i?)\([\"]([^\/]*)[\"], ?@\1preg_match(\"\/\3\/\2\", @g" $file;
            perl -pi -e "s@([^_])ereg(i?)\((\\\$[^\\\$,]+)([^)+])@\1preg_match('\/'.preg_quote(\3, '\/').'\/\2'\4@g" $file;
            perl -pi -e "s@([^_])ereg(i?)_replace\(['\"]([^\/'\"]*?)['\"], ?@\1preg_replace('\/\3\/\2', @g" $file;
            perl -pi -e "s@([^_])ereg(i?)_replace\(['\"]([^#'\"]*?)['\"], ?@\1preg_replace('#\3#\2', @g" $file;
            perl -pi -e "s@([^_])ereg(i?)_replace\(['\"]([^~'\"]*?)['\"], ?@\1preg_replace('~\3~\2', @g" $file;
            perl -pi -e "s@([^_])ereg(i?)_replace\([\"]([^\/]*?)[\"], ?@\1preg_replace("\/\3\/\2", @g" $file;
            perl -pi -e "s@([^_])ereg(i?)_replace\((\\\$[^\\\$,]+)([^)+])@\1preg_replace('\/'.preg_quote(\3, '\/').'\/\2'\4@g" $file;
            perl -pi -e "s@([^_.])(split)\(['\"]([^/'\"]*)['\"], ?@\1preg_\2('/\3/\',@g" $file;
            perl -pi -e "s@([^_.])(split)\(['\"]([^#'\"]*)['\"], ?@\1preg_\2('#\3#\',@g" $file;
            perl -pi -e "s@([^_.])(split)\(['\"]([^~'\"]*)['\"], ?@\1preg_\2('~\3~\',@g" $file;
            perl -pi -e "s@(mysql_)(escape_string)\(@\1real_\2(@g" $file;
            file_check_check=$(grep -El "([^_]eregi?(_replace)?[(]|[^_.]split[(]|\\\$HTTP_(POST_FILES|((GET|POST|REQUEST|SESSION|COOKIE|SERVER|ENV)_VARS)))" "$file");
            if [ -n "$file_check_check" ]; then
                echo "Incomplete update of file: $file";
            else
                echo "Successfully updated file: $file";
            fi
        fi
    done;
fi

Save this as a bash file (e.g. php_update.sh) and run using: sh php_update.sh /home/user/directory. And, always backup your site first, it only takes seconds!

If you’ve used this script on your server, please write a comment here.

Last updated on

3 thoughts on “Bulk update scripts to PHP 5.4+”

  1. Many thanks for this great script: “Bulk update scripts to PHP 5.4”

    I have made some changes (and tested them):

    1) Cleaned / corrected some Perl Regular Expression errors with missing escapes on “/” regex.
    2) Allowed the script to work against a CLI specified directory (recursive).
    3) Stop the script from modifying “inline” JavaScript use of typical “..).split()” functions.
    4) Stop the script from modifying those CMS’s that employ replacement PHP functions() for backward compatibility (Joomla is one such beast).


    #!/bin/bash
    #
    # Peter: Fix old PHP scripts to their modern equivalent
    # Usage: ./update-php
    #
    # Script to bulk update older PHP pages, replacing; ereg + eregi with preg_match, ereg_match with preg_match, split with preg_split and chang the $HTTP globals to their modern versions
    # Notes: regex = [^_.] stop changing inline javascript use of ).split()

    directory=”$@”

    if [ ! -n “$directory” ]; then
    echo “ERROR: Missing target PHP directory.”;
    else
    for file in $(find $directory -type f \( -name “*.php” -o -name “*.inc” -o -name “*.install” -o -name “*.module” -o -name “*.html” \));
    do
    file_check=$(grep -El “([^_]eregi?(_replace)?[(]|[^_.]split[(]|\\\$HTTP_(POST_FILES|((GET|POST|REQUEST|SESSION|COOKIE|SERVER|ENV)_VARS)))” “$file”);
    if [ -n “$file_check” ]; then
    echo “Updating File: $file”;
    perl -pi -e “s@([^_])ereg(i?)\([‘\”]([^\/’\”]*)[‘\”], ?@\1preg_match(‘\/\3\/\2’, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)\([‘\”]([^#’\”]*)[‘\”], ?@\1preg_match(‘#\3#\2’, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)\([‘\”]([^~’\”]*)[‘\”], ?@\1preg_match(‘~\3~\2’, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)\([\”]([^\/]*)[\”], ?@\1preg_match(\”\/\3\/\2\”, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)\((\\\$[^\\\$,]+)([^)+])@\1preg_match(‘\/’.preg_quote(\3, ‘\/’).’\/\2’\4@g” $file;
    perl -pi -e “s@([^_])ereg(i?)_replace\([‘\”]([^\/’\”]*?)[‘\”], ?@\1preg_replace(‘\/\3\/\2’, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)_replace\([‘\”]([^#’\”]*?)[‘\”], ?@\1preg_replace(‘#\3#\2’, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)_replace\([‘\”]([^~’\”]*?)[‘\”], ?@\1preg_replace(‘~\3~\2’, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)_replace\([\”]([^\/]*?)[\”], ?@\1preg_replace(“\/\3\/\2″, @g” $file;
    perl -pi -e “s@([^_])ereg(i?)_replace\((\\\$[^\\\$,]+)([^)+])@\1preg_replace(‘\/’.preg_quote(\3, ‘\/’).’\/\2’\4@g” $file;
    perl -pi -e “s@\\\$HTTP_POST_(FILES)@\\\$_\1@g” $file;
    perl -pi -e “s@\\\$HTTP_(GET|POST|REQUEST|SESSION|COOKIE|SERVER|ENV)_VARS@\\\$_\1@g” $file;
    perl -pi -e “s@([^_.])(split)\([‘\”]([^\/’\”]*)[‘\”], ?@\1preg_\2(‘\/\3\/\’, @g” $file;
    perl -pi -e “s@([^_.])(split)\([‘\”]([^#’\”]*)[‘\”], ?@\1preg_\2(‘#\3#\’, @g” $file;
    perl -pi -e “s@([^_.])(split)\([‘\”]([^~’\”]*)[‘\”], ?@\1preg_\2(‘~\3~\’, @g” $file;
    fi
    done;
    fi

  2. Thank you Peter! I have checked your code and integrated this into the existing script. I’ve also added a much needed check of the file’s status after the file modification – to see if the old functions have been replaced successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *