Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
<?php
/*
* quick_fix.php
* by Randell Miller of Creatvie Technology Services
* Version 1.0.0
* Aug 9 2019
*/
//Start time
$start = microtime(true);
//Our color class
$color = new Colors();
//Opening banner
echo "\n";
banner("Quick Fix for WordPress by CTS. Version 1.0.0");
//Any extra args we need to use based on the environment or supplied arguments
$extraargs = "";
//Get info on what user we currently are.
$whoami = strip_newline(shell_exec("whoami"));
$isroot = false;
if(isset($whoami)) {
if(strtolower($whoami) == strtolower(trim("root"))) {
//We're root.
$extraargs .= " --allow-root";
banner("Root user detected! Make sure to check permissions and ownership after script completion!","warning");
$isroot = true;
} else {
echo "Running as non-root user ($whoami).\n";
}
} else {
//Command did not return anything useable.
//Assume we need to have root priv
banner("Unable to check user. Assuming root.","error");
echo "\n";
banner("Root user detected! Make sure to check permissions and ownership after script completion!","warning");
$extraargs += " --allow-root";
$isroot = true;
}
//Check current version of WordPress
$version = strip_newline(shell_exec("wp core version $extraargs"));
echo "WordPress version $version found.\n";
//Check to see if there is a new version available.
$checkUpdate = strip_newline(shell_exec("wp core check-update $extraargs"));
if($checkUpdate == "Success: WordPress is at the latest version.") {
echo $color->getColoredString("WordPress is up to date.\n","green");
} else {
echo $color->getColoredString("A newer version of WordPress is available.\n","red");
}
echo "\n";
echo "Doing verify-checksums.\n";
$coreCheckSums = shell_exec("wp core verify-checksums $extraargs 2>&1");
$coreCheckSums = explode("\n", $coreCheckSums);
$failedChecksum = array();
$shouldNotExist = array();
foreach($coreCheckSums as $line) {
if(empty($line)) {
continue;
}
$line_arr = explode(":",$line);
switch(strtolower($line_arr[0])) {
case "success":
echo "\n";
banner("All WordPress Core files pass checksum validation.","success");
break;
case "warning":
if(trim($line_arr[1])=="File doesn't verify against checksum") {
echo $color->getColoredString("File failed checksum validation: {$line_arr[2]}\n","red");
$failedChecksum[] = trim($line_arr[2]);
} elseif (trim($line_arr[1])=="File should not exist") {
echo $color->getColoredString("File should not exist: {$line_arr[2]}\n","red");
$shouldNotExist[] = trim($line_arr[2]);
}
break;
case "error":
echo "\n";
banner("WordPress Core files did not pass validation.","error");
break;
default:
echo $color->getColoredString("Unknown return string:\n$line\n","red","light_gray");
break;
}
}
echo "\n";
//Deal with checksum validation
echo "Number of files that failed checksum validation: ";
if(count($failedChecksum) > 0) {
echo $color->getColoredString(count($failedChecksum)."\n","red");
$response = readline("Do you wish to replace the core files with fress versions from WordPress.org (y/N)? ");
if(strtolower($response) == "y") {
//Replace 'em!
$replace = shell_exec("wp core download --version=$version --force $extraargs");
echo "\n";
print_r($replace);
echo "\n";
if($isroot) {
echo "\n";
banner("WordPress Core files have been updated. Please check ownership and permission after script is finished!","warning");
}
}
} else {
echo $color->getColoredString("0\n","green");
}
echo "\n";
//deal with files that should not exist.
echo "Number of files that should not exist: ";
if(count($shouldNotExist) > 0) {
echo $color->getColoredString(count($shouldNotExist)."\n","red");
$response = readline("Do you wish to delete these files (y/N)? ");
if(strtolower($response) == "y") {
//Delete 'em
foreach($shouldNotExist as $target) {
if(unlink($target)) {
echo $color->getColoredString("File $target deleted.","green");
} else {
echo $color->getColoredString("Unable to delete $target.","red");
}
}
echo "\n";
}
} else {
echo $color->getColoredString("0\n","green");
}
echo "\n";
//Plugins
$checkPluginVersions = shell_exec("wp plugin list $extraargs");
$arr_checkPluginVersions = explode("\n",$checkPluginVersions);
$plugins = array();
foreach($arr_checkPluginVersions as $p) {
$arr_p = explode("\t",$p);
if(!isset($arr_p[0]) || empty($arr_p[0]) || trim($arr_p[0]) == "name") {
continue; //Ignore this line
}
$plugins[$arr_p[0]] = array("name"=>$arr_p[0],"active"=>$arr_p[1],"update"=>$arr_p[2],"version"=>$arr_p[3]);
}
//filter the plugins
$active_plugins = array_filter($plugins,function($plugin) {
return ($plugin['active']=="active");
});
$inactive_plugins = array_filter($plugins,function($plugin) {
return ($plugin['active']=="inactive");
});
//Active plugins
echo "Total number of active plugins: ".count($active_plugins)."\n";
if(count($active_plugins) > 0) {
//Active plugins
$result = readline("Do you wish to check active plugins (y/N)? ");
if(strtolower($result) == "y") {
//Parse 'em
foreach($active_plugins as $p) {
check_plugin($p);
}
}
}
echo "";
//Inactive plugins
echo "Total number of inactive plugins: ".count($inactive_plugins)."\n";
if(count($inactive_plugins) > 0) {
//Active plugins
$result = readline("Do you wish to check inactive plugins (y/N)? ");
if(strtolower($result) == "y") {
//Parse 'em
foreach($inactive_plugins as $p) {
check_plugin($p);
}
}
}
echo "";
$time_elapsed_secs = microtime(true) - $start;
banner("Script Complete. Total run time: $time_elapsed_secs sec");
function check_plugin($p) {
global $color;
global $extraargs;
echo "\nPlugin: {$p['name']}\n";
if($p['update'] == "available") {
echo $color->getColoredString("This plugin is out of date!","red")."\n";
$response = readline("Do you want to update this plugin (y/N)? ");
if(strtolower($response) == "y") {
echo "Updating {$p['name']}...\n";
$result = shell_exec("wp plugin update {$p['name']} $extraargs 2>&1");
print_r($result);
echo "\n";
}
}
$cmd = "wp plugin verify-checksums {$p['name']} $extraargs 2>&1";
$result = explode("\n",shell_exec($cmd));
$state = null;
foreach($result as $r) {
if(!empty($r)) {
if (strpos($r, 'Warning: Could not retrieve the checksums') !== false) {
$state = "nocheck";
}
if (strpos($r, 'Success:') !== false && $state != "nocheck") {
$state = "success";
}
if (strpos($r, 'Error:') !== false && $state != "nocheck") {
$state = "fail";
}
}
}
switch($state) {
case "success":
echo $color->getColoredString("Plugin passed checksum validation.","green")."\n";
break;
case "nocheck":
echo $color->getColoredString("Unable to retrieve checksum info for plugin.","yellow")."\n";
break;
case "fail":
echo $color->getColoredString("Plugin failed checksum validation!","red")."\n";
break;
default:
echo $color->getColoredString("Unknown result of checksum validation!","red")."\m".print_r($result,true)."\m";
break;
}
if($state == "fail") {
$response = readline("Do you want to install a fresh copy of this plugin (y/N)? ");
if(strtolower($response) == "y") {
$result = shell_exec("wp plugin install {$p['name']} --force --version={$p['version']} $extraargs 2>&1");
print_r($result);
echo "\n";
}
}
}
function banner($msg,$type = "message") {
global $color;
switch($type) {
case "error":
$forecolor = "red";
break;
case "warning":
$forecolor = "yellow";
break;
case "success":
$forecolor = "green";
break;
default:
$forecolor = null; //Default color
break;
}
echo $color->getColoredString(str_repeat("*", strlen($msg))."\n",$forecolor);
echo $color->getColoredString($msg."\n",$forecolor);
echo $color->getColoredString(str_repeat("*", strlen($msg))."\n",$forecolor);
echo "\n"; //Extra spacing
}
class Colors {
private $foreground_colors = array();
private $background_colors = array();
public function __construct() {
// Set up shell colors
$this->foreground_colors['black'] = '0;30';
$this->foreground_colors['dark_gray'] = '1;30';
$this->foreground_colors['blue'] = '0;34';
$this->foreground_colors['light_blue'] = '1;34';
$this->foreground_colors['green'] = '0;32';
$this->foreground_colors['light_green'] = '1;32';
$this->foreground_colors['cyan'] = '0;36';
$this->foreground_colors['light_cyan'] = '1;36';
$this->foreground_colors['red'] = '0;31';
$this->foreground_colors['light_red'] = '1;31';
$this->foreground_colors['purple'] = '0;35';
$this->foreground_colors['light_purple'] = '1;35';
$this->foreground_colors['brown'] = '0;33';
$this->foreground_colors['yellow'] = '1;33';
$this->foreground_colors['light_gray'] = '0;37';
$this->foreground_colors['white'] = '1;37';
$this->background_colors['black'] = '40';
$this->background_colors['red'] = '41';
$this->background_colors['green'] = '42';
$this->background_colors['yellow'] = '43';
$this->background_colors['blue'] = '44';
$this->background_colors['magenta'] = '45';
$this->background_colors['cyan'] = '46';
$this->background_colors['light_gray'] = '47';
}
// Returns colored string
public function getColoredString($string, $foreground_color = null, $background_color = null) {
$colored_string = "";
// Check if given foreground color found
if (isset($this->foreground_colors[$foreground_color])) {
$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
}
// Check if given background color found
if (isset($this->background_colors[$background_color])) {
$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
}
// Add string and end coloring
$colored_string .= $string . "\033[0m";
return $colored_string;
}
// Returns all foreground color names
public function getForegroundColors() {
return array_keys($this->foreground_colors);
}
// Returns all background color names
public function getBackgroundColors() {
return array_keys($this->background_colors);
}
}
function strip_newline($input) {
$output = str_replace(array("\n", "\r"), '', $input);
return $output;
}