| 41 | rodolico | 1 | #!/usr/bin/env bash
 | 
        
           |  |  | 2 | RED='\033[0;31m'
 | 
        
           |  |  | 3 | GREEN='\033[0;32m'
 | 
        
           |  |  | 4 | YELLOW='\033[0;33m'
 | 
        
           |  |  | 5 | NC='\033[0m' # No Color
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | echo "************************************************************************************************************"
 | 
        
           |  |  | 8 | echo "WordPress core file quick scan and repair.  (Written by Randell Miller of Creative Technology)";
 | 
        
           |  |  | 9 | echo "Version 1.0.0"
 | 
        
           |  |  | 10 | echo "************************************************************************************************************"
 | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 | whoami=$(whoami)
 | 
        
           |  |  | 13 |   | 
        
           |  |  | 14 | extraflags=""
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | if [[ "$whoami" == "root" ]]; then
 | 
        
           |  |  | 17 |    extraflags=" --allow-root"
 | 
        
           |  |  | 18 |    echo -e "${RED}";
 | 
        
           |  |  | 19 |    echo "************************************************************************************************************"
 | 
        
           |  |  | 20 |    echo "NOTE:  You appear to be root.  Be sure to check all permissions and file ownership after script is complete."
 | 
        
           |  |  | 21 |    echo "************************************************************************************************************"
 | 
        
           |  |  | 22 |    echo -e "${NC}";
 | 
        
           |  |  | 23 | else
 | 
        
           |  |  | 24 |    echo ""
 | 
        
           |  |  | 25 | fi
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | #What version of WP are we using?
 | 
        
           |  |  | 28 | version=$(wp core version $extraflags)
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | echo "Found WordPress version $version.";
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | checkUpdate=$(wp core check-update $extraflags)
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 | if [[ "$checkUpdate" == "Success: WordPress is at the latest version." ]]; then
 | 
        
           |  |  | 35 |    echo -e "${GREEN}WordPress is up to date.${NC}"
 | 
        
           |  |  | 36 | else
 | 
        
           |  |  | 37 |    echo -e "${YELLOW}It appears WordPress is not up to date.${NC}"
 | 
        
           |  |  | 38 | fi
 | 
        
           |  |  | 39 | echo ""
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 | echo "Doing verify-checksums."
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | rm -f "tmp_changed" #clear tmp file
 | 
        
           |  |  | 44 | rm -f "tmp_shouldNotExist" #clear tmp file
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 | wp core verify-checksums $extraflags 2>&1 | while read -r line; do
 | 
        
           |  |  | 47 |    if [[ "${line:0:46}" == "Warning: File doesn't verify against checksum:" ]]; then
 | 
        
           |  |  | 48 |   | 
        
           |  |  | 49 |       #We have a core file that's not right, need to refresh.
 | 
        
           |  |  | 50 |       echo -e "${RED}We have a file that has been changed (${line:47:${#line}}).${NC}"
 | 
        
           |  |  | 51 |       echo ${line:47:${#line}} >> "tmp_changed"
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |    elif [[ "${line:0:31}" == "Warning: File should not exist:" ]]; then
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 |       #This is a file that doesn't belong here.  Set to quarentine.
 | 
        
           |  |  | 56 |       echo -e "${YELLOW}We have a file that shouldn't exist (${line:32:${#line}}).${NC}"
 | 
        
           |  |  | 57 |       echo ${line:32:${#line}} >> "tmp_shouldNotExist"
 | 
        
           |  |  | 58 |    fi
 | 
        
           |  |  | 59 | done
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 | #get our changed files
 | 
        
           |  |  | 62 | if [ -f "tmp_changed" ]; then
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |    while read line; do
 | 
        
           |  |  | 65 |       changed+=($line)
 | 
        
           |  |  | 66 |    done < "tmp_changed"
 | 
        
           |  |  | 67 |    #clean up after ourselves
 | 
        
           |  |  | 68 |    rm -f "tmp_changed"
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 | fi
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | #get our shouldNotExist files
 | 
        
           |  |  | 73 | if [ -f "tmp_shouldNotExist" ]; then
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |    while read line; do
 | 
        
           |  |  | 76 |       shouldNotExist+=($line)
 | 
        
           |  |  | 77 |    done < "tmp_shouldNotExist"
 | 
        
           |  |  | 78 |    #clean up after ourselves
 | 
        
           |  |  | 79 |    rm -f "tmp_shouldNotExist"
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 | fi
 | 
        
           |  |  | 82 |   | 
        
           |  |  | 83 | #Are all of our Core Files ok?
 | 
        
           |  |  | 84 | echo ""
 | 
        
           |  |  | 85 | echo "Number of changed WordPress core files found: ${#changed[@]}";
 | 
        
           |  |  | 86 | if [[ ${#changed[@]} != "0" ]]; then
 | 
        
           |  |  | 87 |    #We have changed files.
 | 
        
           |  |  | 88 |    echo -e "${RED}Changed files detected.  Core file replace suggested.${NC}";
 | 
        
           |  |  | 89 |    read -p "Do you want to download a fresh copy of the WordPress core files? (y/N) " coreReplace
 | 
        
           |  |  | 90 |    if [[ "$coreReplace" == "Y" ]] || [[ "$coreReplace" == "y" ]]; then
 | 
        
           |  |  | 91 |       #Core replace
 | 
        
           |  |  | 92 |       wp core download --version=$version --force $extraflags
 | 
        
           |  |  | 93 |    fi
 | 
        
           |  |  | 94 | else
 | 
        
           |  |  | 95 |    echo -e "${GREEN}No changed core files detected.${NC}";
 | 
        
           |  |  | 96 | fi
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 | #Do we have files that should not exist?
 | 
        
           |  |  | 99 | echo ""
 | 
        
           |  |  | 100 | echo "Number of files that should not exist found: ${#shouldNotExist[@]} ";
 | 
        
           |  |  | 101 | if [[ ${#shouldNotExist[@]} != "0" ]]; then
 | 
        
           |  |  | 102 |    #We have changed files.
 | 
        
           |  |  | 103 |    echo -e "${RED}Files detected where they should not.  Deletion suggested.${NC}";
 | 
        
           |  |  | 104 |    read -p "Do you want to Delete or Quarantine these files? {d/q/N) " handleExtras
 | 
        
           |  |  | 105 |    if [[ "$handleExtras" == "d" ]] || [[ "$handleExtras" == "D" ]]; then
 | 
        
           |  |  | 106 |       #Delete the files
 | 
        
           |  |  | 107 |       echo "Deleting files..."
 | 
        
           |  |  | 108 |       for i in "${shouldNotExist[@]}"
 | 
        
           |  |  | 109 |       do
 | 
        
           |  |  | 110 |          echo "Deleting $i";
 | 
        
           |  |  | 111 |           rm -f $i
 | 
        
           |  |  | 112 |       done
 | 
        
           |  |  | 113 |       echo -e "${GREEN}Files deleted.${NC}"
 | 
        
           |  |  | 114 |    elif [[ "$handleExtras" == "q" ]] || [[ "$handleExtras" == "Q" ]]; then
 | 
        
           |  |  | 115 |       #Quarantine files
 | 
        
           |  |  | 116 |       if [ ! -d "quarentine" ]; then
 | 
        
           |  |  | 117 |          mkdir "quarentine"
 | 
        
           |  |  | 118 |       fi
 | 
        
           |  |  | 119 |       now=`date +"%m_%d_%Y_%s"`
 | 
        
           |  |  | 120 |       qdir="quarentine/$now";
 | 
        
           |  |  | 121 |       if [ ! -d "$qdir" ]; then
 | 
        
           |  |  | 122 |          echo "Creating directory $qdir"
 | 
        
           |  |  | 123 |          mkdir $qdir
 | 
        
           |  |  | 124 |       fi
 | 
        
           |  |  | 125 |   | 
        
           |  |  | 126 |       for i in "${shouldNotExist[@]}"
 | 
        
           |  |  | 127 |       do
 | 
        
           |  |  | 128 |          mv $i "$qdir/${i##*/}"
 | 
        
           |  |  | 129 |          chmod -x "$qdir/${i##*/}"
 | 
        
           |  |  | 130 |       done
 | 
        
           |  |  | 131 |       echo -e "${GREEN}Files quarentined.${NC}"
 | 
        
           |  |  | 132 |    fi
 | 
        
           |  |  | 133 | else
 | 
        
           |  |  | 134 |    echo -e "${GREEN}No files detected where they should not be.${NC}";
 | 
        
           |  |  | 135 | fi
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 | echo ""
 | 
        
           |  |  | 138 |   | 
        
           |  |  | 139 | #plugins
 | 
        
           |  |  | 140 | checkPlugins=$(wp plugin update --all --dry-run $extraflags | sed -n '/\bactive/p')
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 | if [ -z "$checkPlugins" ]; then
 | 
        
           |  |  | 143 |    echo -e "${GREEN}All active plugins appear to be up to date.${NC}"
 | 
        
           |  |  | 144 | else
 | 
        
           |  |  | 145 |    echo -e "${RED}Plugins that have available updates:${NC}"
 | 
        
           |  |  | 146 |    echo "$checkPlugins"
 | 
        
           |  |  | 147 | fi
 | 
        
           |  |  | 148 | echo ""
 | 
        
           |  |  | 149 | #plugin checksums
 | 
        
           |  |  | 150 | rm -f "dirty"
 | 
        
           |  |  | 151 | re1="Checksum does not match"
 | 
        
           |  |  | 152 | wp plugin verify-checksums --all $extraflags 2>&1 | while read -r line; do
 | 
        
           |  |  | 153 |    if [[ "${line:0:45}" == "Warning: Could not retrieve the checksums for" ]]; then
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |       #This file doesn't have check sums.
 | 
        
           |  |  | 156 |       re="plugin\s*(\w+)"
 | 
        
           |  |  | 157 |       [[ $line =~ $re ]] && plugin="${BASH_REMATCH[0]}"
 | 
        
           |  |  | 158 |       echo -e "${YELLOW}Could not retrieve checksums for $plugin.${NC}"
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |    elif [[ "${line:0:7}" == "Success" ]]; then
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |       #Success Message
 | 
        
           |  |  | 164 |       echo -e "${GREEN}All Plugins checksums that could be tested matched.${NC}"
 | 
        
           |  |  | 165 |    elif [[ $line =~ $re1 ]]; then
 | 
        
           |  |  | 166 |       echo -e "${RED}$line${NC}"
 | 
        
           |  |  | 167 |       t=( $line )
 | 
        
           |  |  | 168 |       echo "${t[0]}" >> "dirty"
 | 
        
           |  |  | 169 |    fi
 | 
        
           |  |  | 170 | done
 | 
        
           |  |  | 171 |   | 
        
           |  |  | 172 | echo ""
 | 
        
           |  |  | 173 |   | 
        
           |  |  | 174 | if [ -f "dirty" ]; then
 | 
        
           |  |  | 175 |    mapfile -t myArray < dirty
 | 
        
           |  |  | 176 |    UNIQ_myArray=($(printf "%s\n" "${myArray[@]}" | sort -u))
 | 
        
           |  |  | 177 |    for i in "${UNIQ_myArray[@]}"
 | 
        
           |  |  | 178 |    do
 | 
        
           |  |  | 179 |       echo "The following plugin failed checksum verification:"
 | 
        
           |  |  | 180 |       wp plugin status $i
 | 
        
           |  |  | 181 |       echo ""
 | 
        
           |  |  | 182 |    done
 | 
        
           |  |  | 183 |    echo -e "${RED}At least one plugin failed checksum matching!${NC}"
 | 
        
           |  |  | 184 | else
 | 
        
           |  |  | 185 |    echo -e "${GREEN}All plugins passed checksum matching.${NC}"
 | 
        
           |  |  | 186 | fi
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 | echo ""
 | 
        
           |  |  | 191 | echo "Script complete."
 |