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.

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.

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 *