Subversion Repositories sysadmin_scripts

Rev

Rev 48 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
43 rodolico 1
<?php
2
/*
3
 * quick_fix.php
4
 * by Randell Miller of Creatvie Technology Services
5
 * Version 1.0.0
6
 * Aug 9 2019
7
 */
8
 
9
//Start time
10
$start = microtime(true);
11
 
12
//Our color class
13
$color = new Colors();
14
 
15
//Opening banner
16
echo "\n";
17
banner("Quick Fix for WordPress by CTS.  Version 1.0.0");
18
 
19
//Any extra args we need to use based on the environment or supplied arguments
20
$extraargs = "";
21
 
22
//Get info on what user we currently are.
23
$whoami = strip_newline(shell_exec("whoami"));
24
$isroot = false;
25
 
26
if(isset($whoami)) {
27
    if(strtolower($whoami) == strtolower(trim("root"))) {
28
        //We're root.
29
        $extraargs .= " --allow-root";
30
        banner("Root user detected!  Make sure to check permissions and ownership after script completion!","warning");
31
        $isroot = true;
32
    } else {
33
        echo "Running as non-root user ($whoami).\n";
34
    }
35
} else {
36
    //Command did not return anything useable.
37
    //Assume we need to have root priv
38
    banner("Unable to check user.  Assuming root.","error");
39
    echo "\n";
40
    banner("Root user detected!  Make sure to check permissions and ownership after script completion!","warning");
41
    $extraargs += " --allow-root";
42
    $isroot = true;
43
}
44
 
45
//Check current version of WordPress
46
$version = strip_newline(shell_exec("wp core version $extraargs"));
47
 
48
echo "WordPress version $version found.\n";
49
 
50
//Check to see if there is a new version available.
51
$checkUpdate = strip_newline(shell_exec("wp core check-update $extraargs"));
52
if($checkUpdate == "Success: WordPress is at the latest version.") {
53
    echo $color->getColoredString("WordPress is up to date.\n","green");
54
} else {
55
    echo $color->getColoredString("A newer version of WordPress is available.\n","red");
56
}
57
 
58
echo "\n";
59
 
60
echo "Doing verify-checksums.\n";
61
 
62
$coreCheckSums = shell_exec("wp core verify-checksums $extraargs 2>&1");
63
$coreCheckSums = explode("\n", $coreCheckSums);
64
 
65
$failedChecksum = array();
66
$shouldNotExist = array();
67
foreach($coreCheckSums as $line) {
68
    if(empty($line)) {        
69
        continue; 
70
    }
71
 
72
    $line_arr = explode(":",$line);
73
    switch(strtolower($line_arr[0])) {
74
        case "success":
75
            echo "\n";
76
            banner("All WordPress Core files pass checksum validation.","success");
77
            break;
78
        case "warning":
79
            if(trim($line_arr[1])=="File doesn't verify against checksum") {
80
                echo $color->getColoredString("File failed checksum validation: {$line_arr[2]}\n","red");
81
                $failedChecksum[] = trim($line_arr[2]);
82
            } elseif (trim($line_arr[1])=="File should not exist") {
83
                echo $color->getColoredString("File should not exist: {$line_arr[2]}\n","red");
84
                $shouldNotExist[] = trim($line_arr[2]);
85
            }
86
            break;
87
        case "error":
88
            echo "\n";
89
            banner("WordPress Core files did not pass validation.","error");
90
            break;
91
        default:
92
            echo $color->getColoredString("Unknown return string:\n$line\n","red","light_gray");
93
            break;
94
    }
95
}
96
 
97
echo "\n";
98
 
99
//Deal with checksum validation
100
echo "Number of files that failed checksum validation: ";
101
if(count($failedChecksum) > 0) {
102
    echo $color->getColoredString(count($failedChecksum)."\n","red");
103
    $response = readline("Do you wish to replace the core files with fress versions from WordPress.org (y/N)? ");
104
    if(strtolower($response) == "y") {
105
        //Replace 'em!
106
        $replace = shell_exec("wp core download --version=$version --force $extraargs");
107
        echo "\n";
108
        print_r($replace);
109
        echo "\n";
110
 
111
        if($isroot) {
112
            echo "\n";
113
            banner("WordPress Core files have been updated.  Please check ownership and permission after script is finished!","warning");
114
        }
115
    }
116
} else {
117
    echo $color->getColoredString("0\n","green");
118
}
119
echo "\n";
120
 
121
//deal with files that should not exist.
122
echo "Number of files that should not exist: ";
123
if(count($shouldNotExist) > 0) {
124
    echo $color->getColoredString(count($shouldNotExist)."\n","red");
125
    $response = readline("Do you wish to delete these files (y/N)? ");
126
    if(strtolower($response) == "y") {
127
        //Delete 'em
128
        foreach($shouldNotExist as $target) {
129
            if(unlink($target)) {
130
                echo $color->getColoredString("File $target deleted.","green");
131
            } else {
132
                echo $color->getColoredString("Unable to delete $target.","red");
133
            }
134
        }
135
        echo "\n";
136
    }
137
} else {
138
    echo $color->getColoredString("0\n","green");
139
}
140
echo "\n";
141
 
142
//Plugins
143
$checkPluginVersions = shell_exec("wp plugin list $extraargs");
144
$arr_checkPluginVersions = explode("\n",$checkPluginVersions);
145
$plugins = array();
146
foreach($arr_checkPluginVersions as $p) {
147
    $arr_p = explode("\t",$p);
148
    if(!isset($arr_p[0]) ||  empty($arr_p[0]) || trim($arr_p[0]) == "name") {
149
        continue; //Ignore this line
150
    }
151
    $plugins[$arr_p[0]] = array("name"=>$arr_p[0],"active"=>$arr_p[1],"update"=>$arr_p[2],"version"=>$arr_p[3]);   
152
}
153
 
154
//filter the plugins
155
$active_plugins = array_filter($plugins,function($plugin) {
156
    return ($plugin['active']=="active");
157
});
158
 
159
$inactive_plugins = array_filter($plugins,function($plugin) {
160
    return ($plugin['active']=="inactive");
161
});
162
 
163
//Active plugins
164
echo "Total number of active plugins: ".count($active_plugins)."\n";
165
if(count($active_plugins) > 0) {
166
    //Active plugins
167
    $result = readline("Do you wish to check active plugins (y/N)? ");
168
    if(strtolower($result) == "y") {
169
        //Parse 'em
170
        foreach($active_plugins as $p) {
171
            check_plugin($p);
172
        }
173
    }
174
} 
175
 
176
echo "";
177
 
178
//Inactive plugins
179
echo "Total number of inactive plugins: ".count($inactive_plugins)."\n";
180
if(count($inactive_plugins) > 0) {
181
    //Active plugins
182
    $result = readline("Do you wish to check inactive plugins (y/N)? ");
183
    if(strtolower($result) == "y") {
184
        //Parse 'em
185
        foreach($inactive_plugins as $p) {
186
            check_plugin($p);
187
        }
188
    }
189
} 
190
 
191
echo "";
192
$time_elapsed_secs = microtime(true) - $start;
193
banner("Script Complete.  Total run time: $time_elapsed_secs sec");
194
 
195
function check_plugin($p) {
196
    global $color;
197
    global $extraargs;
198
    echo "\nPlugin: {$p['name']}\n";
199
    if($p['update'] == "available") {
200
        echo $color->getColoredString("This plugin is out of date!","red")."\n";
201
        $response = readline("Do you want to update this plugin (y/N)? ");
202
        if(strtolower($response) == "y") {
203
            echo "Updating {$p['name']}...\n";
204
            $result = shell_exec("wp plugin update {$p['name']} $extraargs 2>&1");
205
            print_r($result);
206
            echo "\n";
207
        }
208
    }
209
    $cmd = "wp plugin verify-checksums {$p['name']} $extraargs 2>&1";
210
    $result = explode("\n",shell_exec($cmd));
211
    $state = null;
212
    foreach($result as $r) {
213
        if(!empty($r)) {
214
            if (strpos($r, 'Warning: Could not retrieve the checksums') !== false) {
215
                $state = "nocheck";
216
            }
217
            if (strpos($r, 'Success:') !== false && $state != "nocheck") {
218
                $state = "success";
219
            }
220
            if (strpos($r, 'Error:') !== false && $state != "nocheck") {
221
                $state = "fail";
222
            }
223
        }
224
    }
225
    switch($state) {
226
        case "success":
227
            echo $color->getColoredString("Plugin passed checksum validation.","green")."\n";
228
            break;
229
        case "nocheck":
230
            echo $color->getColoredString("Unable to retrieve checksum info for plugin.","yellow")."\n";
231
            break;
232
        case "fail":
233
            echo $color->getColoredString("Plugin failed checksum validation!","red")."\n";
234
            break;
235
        default:
236
            echo $color->getColoredString("Unknown result of checksum validation!","red")."\m".print_r($result,true)."\m";
237
            break;
238
    }
239
    if($state == "fail") {
240
        $response = readline("Do you want to install a fresh copy of this plugin (y/N)? ");
241
        if(strtolower($response) == "y") {
242
            $result = shell_exec("wp plugin install {$p['name']} --force --version={$p['version']} $extraargs 2>&1");
243
            print_r($result);
244
            echo "\n";
245
        }
246
    }
247
 
248
 
249
}
250
 
251
function banner($msg,$type = "message") {
252
    global $color;
253
    switch($type) {
254
        case "error":
255
            $forecolor = "red";
256
            break;
257
        case "warning":
258
            $forecolor = "yellow";
259
            break;
260
        case "success":
261
            $forecolor = "green";
262
            break;
263
        default:
264
            $forecolor = null; //Default color
265
            break;
266
    }
267
 
268
    echo $color->getColoredString(str_repeat("*", strlen($msg))."\n",$forecolor);
269
    echo $color->getColoredString($msg."\n",$forecolor);
270
    echo $color->getColoredString(str_repeat("*", strlen($msg))."\n",$forecolor);
271
    echo "\n"; //Extra spacing
272
}
273
 
274
 
275
 
276
class Colors {
277
        private $foreground_colors = array();
278
        private $background_colors = array();
279
 
280
        public function __construct() {
281
                // Set up shell colors
282
                $this->foreground_colors['black'] = '0;30';
283
                $this->foreground_colors['dark_gray'] = '1;30';
284
                $this->foreground_colors['blue'] = '0;34';
285
                $this->foreground_colors['light_blue'] = '1;34';
286
                $this->foreground_colors['green'] = '0;32';
287
                $this->foreground_colors['light_green'] = '1;32';
288
                $this->foreground_colors['cyan'] = '0;36';
289
                $this->foreground_colors['light_cyan'] = '1;36';
290
                $this->foreground_colors['red'] = '0;31';
291
                $this->foreground_colors['light_red'] = '1;31';
292
                $this->foreground_colors['purple'] = '0;35';
293
                $this->foreground_colors['light_purple'] = '1;35';
294
                $this->foreground_colors['brown'] = '0;33';
295
                $this->foreground_colors['yellow'] = '1;33';
296
                $this->foreground_colors['light_gray'] = '0;37';
297
                $this->foreground_colors['white'] = '1;37';
298
 
299
                $this->background_colors['black'] = '40';
300
                $this->background_colors['red'] = '41';
301
                $this->background_colors['green'] = '42';
302
                $this->background_colors['yellow'] = '43';
303
                $this->background_colors['blue'] = '44';
304
                $this->background_colors['magenta'] = '45';
305
                $this->background_colors['cyan'] = '46';
306
                $this->background_colors['light_gray'] = '47';
307
        }
308
 
309
        // Returns colored string
310
        public function getColoredString($string, $foreground_color = null, $background_color = null) {
311
                $colored_string = "";
312
 
313
                // Check if given foreground color found
314
                if (isset($this->foreground_colors[$foreground_color])) {
315
                        $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
316
                }
317
                // Check if given background color found
318
                if (isset($this->background_colors[$background_color])) {
319
                        $colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
320
                }
321
 
322
                // Add string and end coloring
323
                $colored_string .=  $string . "\033[0m";
324
 
325
                return $colored_string;
326
        }
327
 
328
        // Returns all foreground color names
329
        public function getForegroundColors() {
330
                return array_keys($this->foreground_colors);
331
        }
332
 
333
        // Returns all background color names
334
        public function getBackgroundColors() {
335
                return array_keys($this->background_colors);
336
        }
337
}
338
 
339
function strip_newline($input) {
340
    $output = str_replace(array("\n", "\r"), '', $input);
341
    return $output;
342
}