-
Notifications
You must be signed in to change notification settings - Fork 36
/
wp-remote-install.php
536 lines (483 loc) · 16.6 KB
/
wp-remote-install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<?php
// Global Configuration
set_time_limit( 0 );
error_reporting( E_ALL );
// GitHub Information
define( 'GITHUB_USERNAME' , 'lucanos' );
define( 'GITHUB_PROJECT' , 'WordPress-Remote-Installer' );
// Version Information
define( 'WPRI_VERSION' , '0.5.2' );
// Suggested Plugins and Themes
$suggestions = array(
# Can be an Array of URLs for each Plugin, or a string URL for a text file with URLs for each Plugin on a new line
'plugins' => 'https://' . GITHUB_USERNAME . '.github.io/' . GITHUB_PROJECT .'/list-plugin.txt' ,
# Can be an Array of URLs for each Theme, or a string URL for a text file with URLs for each Theme on a new line
'themes' => 'https://' . GITHUB_USERNAME . '.github.io/' . GITHUB_PROJECT .'/list-theme.txt'
);
// Delete Directory and Contents
function deleteAll( $dir ){
$directory_contents = scandir( $dir );
foreach( array_diff( array( '.' , '..' ) , scandir( $dir ) ) as $k => $item ){
if( is_dir( $item ) ){
deleteAll( $item );
}else{
unlink( $item );
}
}
rmdir( $dir );
}
// Function for Extraction
function extractSubFolder( $zipFile , $target = null , $subFolder = null ){
if( is_null( $target ) )
$target = dirname( __FILE__ );
$zip = new ZipArchive;
$res = $zip->open( $zipFile );
if( $res === TRUE ){
if( is_null( $subFolder ) ){
$zip->extractTo( $target );
}else{
for( $i = 0 , $c = $zip->numFiles ; $i < $c ; $i++ ){
$entry = $zip->getNameIndex( $i );
//Use strpos() to check if the entry name contains the directory we want to extract
if( $entry!=$subFolder.'/' && strpos( $entry , $subFolder.'/' )===0 ){
$stripped = substr( $entry , 9 );
if( substr( $entry , -1 )=='/' ){
// Subdirectory
$subdir = $target.'/'.substr( $stripped , 0 , -1 );
if( !is_dir( $subdir ) )
mkdir( $subdir );
}else{
$stream = $zip->getStream( $entry );
$write = fopen( $target.'/'.$stripped , 'w' );
while( $data = fread( $stream , 1024 ) ){
fwrite( $write , $data );
}
fclose( $write );
fclose( $stream );
}
}
}
}
$zip->close();
return true;
}
die( 'Unable to open '.$zipFile );
return false;
}
// Function to Cleanse Webroot
function rrmdir( $dir ){
if( is_dir( $dir ) ){
$objects = scandir( $dir );
foreach( $objects as $object ){
if( $object!='.' && $object!='..' ){
if( filetype( $dir.'/'.$object )=='dir' )
rrmdir( $dir.'/'.$object );
else
unlink( $dir.'/'.$object );
}
}
reset( $objects );
rmdir( $dir );
}else{
unlink( $dir );
}
}
function cleanseFolder( $exceptFiles = null ){
if( $exceptFiles == null )
$exceptFiles[] = basename( __FILE__ );
$contents = glob('*');
foreach( $contents as $c ){
if( !in_array( $c , $exceptFiles ) )
rrmdir( $c );
}
}
function downloadFromURL( $url = null , $local = null ){
$result = null;
if( is_null( $local ) )
$local = basename( $url );
if( $content = @file_get_contents( $url ) ){
$result = @file_put_contents( $local , $content );
}elseif( function_exists( 'curl_init' ) ){
$fp = fopen( dirname(__FILE__) . '/' . $local , 'w+' );
$ch = curl_init( str_replace( ' ' , '%20' , $url ) );
curl_setopt($ch , CURLOPT_TIMEOUT , 50 );
curl_setopt($ch , CURLOPT_FILE , $fp );
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true );
$result = curl_exec( $ch );
curl_close( $ch );
fclose( $fp );
}else{
$result = false;
}
return $result;
}
function getGithubVersion(){
$versionURL = 'https://' . GITHUB_USERNAME . '.github.io/' . GITHUB_PROJECT .'/version.txt';
$remoteVersion = null;
if( !( $remoteVersion = @file_get_contents( $versionURL ) )
&& function_exists( 'curl_init' ) ){
$ch = curl_init( str_replace( ' ' , '%20' , $versionURL ) );
curl_setopt($ch , CURLOPT_TIMEOUT , 50 );
curl_setopt($ch , CURLOPT_RETURNTRANSFER , true );
curl_setopt($ch , CURLOPT_HEADER , false );
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true );
$remoteVersion = curl_exec( $ch );
curl_close( $ch );
}
return $remoteVersion;
}
// Declare Parameters
$step = 0;
if( isset( $_POST['step'] ) )
$step = (int) $_POST['step'];
?><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WordPress > Remote Installer</title>
<link rel="stylesheet" id="combined-css" href="//<?php echo GITHUB_USERNAME; ?>.github.io/<?php echo GITHUB_PROJECT; ?>/stylesheets/combined.css" type="text/css" media="all">
</head>
<body class="wp-core-ui">
<h1 id="logo"><a href="http://wordpress.org/">WordPress Remote Installer</a></h1>
<?php
switch( $step ){
default :
case 0 :
?>
<!-- STEP 0 //-->
<h1>WordPress Remote Installer</h1>
<p>The WordPress Remote Installer is a script designed to streamline the installation of the WordPress Content Management System. Some users have limited experience using FTP, some webhosts do not allow files to be decompressed after being uploaded, and some people want to make their WordPress installs faster and simpler.</p>
<p>Using the WordPress Remote Installer is simple - upload a single PHP file to your server, access it via a web-browser and simply follow the prompts through 7 easy steps, at the end of which, the Wordpress Installer will commence.</p>
<?php
if( version_compare( WPRI_VERSION , $githubVersion = getGithubVersion() , '<' ) ){
?>
<p class="version_alert">You are using Version <?php echo WPRI_VERSION; ?>. Version <?php echo $githubVersion; ?> is available through <a href="https://github.com/<?php echo GITHUB_USERNAME; ?>/<?php echo GITHUB_PROJECT; ?>">Github</a>.</p>
<?php
}
?>
<form method="post">
<input type="hidden" name="step" value="1" />
<input type="submit" name="submit" value="Let's Get Started!" class="button button-large" />
</form>
<?php
break;
case 1 :
if( isset( $_POST['action'] ) && $_POST['action']=='cleanse' )
cleanseFolder();
$tests = array(
array(
'result' => ini_get( 'allow_url_fopen' ) ,
'pass' => '<strong>allow_url_open</strong> is Enabled' ,
'fail' => '<strong>allow_url_open</strong> is Disabled'
) ,
array(
'result' => !count( array_diff( glob( '*' ) , array( basename( __FILE__ ) , 'version.txt' ) ) ) ,
'pass' => 'The server is empty (apart from this file)' ,
'fail' => 'The server is not empty.'
)
);
?>
<!-- STEP 1 //-->
<h1>Step 1/7: Pre-Install Checks</h1>
<?php
if( isset( $_POST['action'] ) && $_POST['action']=='cleanse' ){
?>
<p>All Files Deleted from the Directory as requested.</p>
<?php
}
?>
<ul>
<?php
$proceed = true;
foreach( $tests as $t ){
if( !$t['result'] )
$proceed = false;
?>
<li class="<?php echo ( $t['result'] ? 'pass' : 'fail' ); ?>"><?php echo $t[( $t['result'] ? 'pass' : 'fail' )]; ?></li>
<?php
}
?>
</ul>
<?php
if( !$proceed ){
?>
<p>NOTE: We are unable to proceed until the above issue(s) are resolved.</p>
<form method="post">
<input type="hidden" name="step" value="1" />
<input type="hidden" name="action" value="cleanse" />
<input type="submit" name="submit" value="Delete All Files from Directory to Proceed" class="button button-large confirm" data-msg="Are you sure? All files, Wordpress-related or not, will be removed. Delete files are unrecoverable." />
</form>
<?php
}else{
?>
<form method="post">
<input type="hidden" name="step" value="2" />
<input type="submit" name="submit" value="Commence Install of WordPress" class="button button-large" />
</form>
<?php
}
break;
case 2 :
?>
<!-- STEP 2 //-->
<h1>Step 2/7: Installing Wordpress</h1>
<ul>
<?php
$proceed = true;
if( downloadFromURL( 'https://wordpress.org/latest.zip' , 'wordpress.zip' ) ){
?>
<li class="pass">Downloading Latest WordPress from Wordpress.org - OK</li>
<?php
}else{
$proceed = false;
?>
<li class="fail">Downloading Latest WordPress from Wordpress.org - FAILED</li>
<?php
}
if( !$proceed ){
?>
<li class="skip">Extract WordPress - SKIPPED</li>
<?php
}elseif( extractSubFolder( 'wordpress.zip' , null , 'wordpress' ) ){
?>
<li class="pass">Extract WordPress - OK</li>
<?php
}else{
$proceed = false;
?>
<li class="fail">Extract WordPress - FAILED</li>
<?php
}
if( !$proceed ){
?>
<li class="skip">Delete WordPress ZIP - SKIPPED</li>
<?php
}elseif( unlink( 'wordpress.zip' ) ){
?>
<li class="pass">Delete WordPress ZIP - OK</li>
<?php
}else{
$proceed = false;
?>
<li class="fail">Delete WordPress ZIP - FAILED</li>
<?php
}
?>
</ul>
<?php
if( !$proceed ){
?>
<p>NOTE: We are unable to proceed until the above issue(s) are resolved.</p>
<?php
}else{
?>
<form method="post">
<input type="hidden" name="step" value="3" />
<input type="submit" name="submit" value="Next Step - Plugins" class="button button-large" />
</form>
<?php
}
break;
case 3 :
$suggest = '';
if( is_array( $suggestions['plugins'] ) ){
$suggest = implode( "\n" , $suggestions['plugins'] );
}elseif( is_string( $suggestions['plugins'] ) ){
if( !( $suggest = @file_get_contents( $suggestions['plugins'] ) ) )
$suggest = '';
}
?>
<!-- STEP 3 //-->
<h1>Step 3/7: Installing Plugins</h1>
<p>List the Download URLs, or the slugs (e.g "classic-editor"), for all WordPress Plugins, one per line</p>
<form method="post">
<textarea name="plugins"><?php echo $suggest; ?></textarea>
<input type="hidden" name="step" value="4" />
<input type="submit" name="submit" value="Install Plugins" class="button button-large" />
</form>
<?php
break;
case 4 :
?>
<!-- STEP 4 //-->
<h1>Step 4/7: Installing Plugins</h1>
<ul>
<?php
$plugin_result = ( !file_exists( @unlink( dirname( __FILE__ ).'/wp-content/plugins/hello.php' ) || dirname( __FILE__ ).'/wp-content/plugins/hello.php' ) );
?>
<li class="<?php echo ( $plugin_result ? 'pass' : 'fail' ); ?>">Delete Unneeded "Hello Dolly" Plugin - <?php echo ( $plugin_result ? 'OK' : 'FAILED' ); ?></li>
<?php
$plugin_result = ( !is_dir( @deleteAll( dirname( __FILE__ ).'/wp-content/plugins/akismet' ) || dirname( __FILE__ ).'/wp-content/plugins/akismet' ) );
?>
<li class="<?php echo ( $plugin_result ? 'pass' : 'fail' ); ?>">Delete Unneeded "Akismet Anti-Spam" Plugin - <?php echo ( $plugin_result ? 'OK' : 'FAILED' ); ?></li>
<?php
if( isset( $_POST['plugins'] ) ){
$plugins = array_filter( explode( "\n" , $_POST['plugins'] ) );
foreach( $plugins as $url ){
$plugin_result = false;
$plugin_message = 'UNKNOWN';
$url = trim( $url );
if( !$url ) continue;
$title = $url;
if( preg_match( '/\/plugins\/([^\/]+)\//' , $url , $bits ) )
$url = $bits[1];
if( strpos( $url , 'http' )!==0 )
$url = 'https://downloads.wordpress.org/plugin/'.$url.'.zip';
if( preg_match( '/(?:([^\.\/]+)\.)(?:((?:\d+\.)*\d*)\.)?zip$/' , $url , $bits ) )
$title = $bits[1].' ('.( isset( $bits[2] ) ? 'v'.$bits[2] : '<em>latest version</em>' ).')';
$get = @file_get_contents( $url );
if( !$get ){
$plugin_message = 'FAILED TO DOWNLOAD';
}else{
file_put_contents( 'temp_plugin.zip' , $get );
if( !extractSubFolder( 'temp_plugin.zip' , dirname( __FILE__ ).'/wp-content/plugins' ) ){
$plugin_message = 'FAILED TO EXTRACT';
}else{
$plugin_result = true;
$plugin_message = 'OK';
}
@unlink( 'temp_plugin.zip' );
}
?>
<li class="<?php echo ( $plugin_result ? 'pass' : 'fail' ); ?>">Installing <strong><?php echo $title; ?></strong> - <?php echo $plugin_message; ?></li>
<?php
}
}
?>
</ul>
<form method="post">
<input type="hidden" name="step" value="5" />
<input type="submit" name="submit" value="Next Step - Themes" class="button button-large" />
</form>
<?php
break;
case 5 :
$suggest = '';
if( is_array( $suggestions['themes'] ) ){
$suggest = implode( "\n" , $suggestions['themes'] );
}elseif( is_string( $suggestions['themes'] ) ){
if( !( $suggest = @file_get_contents( $suggestions['themes'] ) ) )
$suggest = '';
}
?>
<!-- STEP 5 //-->
<h1>Step 5/7: Installing Themes</h1>
<p>List the Download URLs for all WordPress Themes, one per line</p>
<form method="post">
<textarea name="themes"><?php echo $suggest; ?></textarea>
<input type="hidden" name="step" value="6" />
<input type="submit" name="submit" value="Install Themes" class="button button-large" />
</form>
<?php
break;
case 6 :
?>
<!-- STEP 6 //-->
<h1>Step 6/7: Installing Themes</h1>
<ul>
<?php
if( isset( $_POST['themes'] ) ){
$themes = array_filter( explode( "\n" , $_POST['themes'] ) );
foreach( $themes as $url ){
$theme_result = false;
$theme_message = 'UNKNOWN';
$url = trim( $url );
if( !$url ) continue;
$title = $url;
if( preg_match( '/\/themes\/([^\/]+)\//' , $url , $bits ) )
$url = $bits[1];
if( strpos( $url , 'http' )!==0 )
$url = 'https://downloads.wordpress.org/theme/'.$url.'.zip';
if( preg_match( '/(?:([^\.\/]+)\.)(?:((?:\d+\.)*\d*)\.)?zip$/' , $url , $bits ) )
$title = $bits[1].' ('.( isset( $bits[2] ) ? 'v'.$bits[2] : '<em>latest version</em>' ).')';
$get = @file_get_contents( $url );
if( !$get ){
$theme_message = 'FAILED TO DOWNLOAD';
}else{
file_put_contents( 'temp_theme.zip' , $get );
if( !extractSubFolder( 'temp_theme.zip' , dirname( __FILE__ ).'/wp-content/themes' ) ){
$theme_message = 'FAILED TO EXTRACT';
}else{
$theme_result = true;
$theme_message = 'OK';
}
?>
<li class="<?php echo ( $theme_result ? 'pass' : 'fail' ); ?>">Installing <strong><?php echo $title; ?></strong> - <?php echo $theme_message; ?></li>
<?php
@unlink( 'temp_theme.zip' );
}
echo '</li>';
}
}
?>
</ul>
<form method="post">
<input type="hidden" name="step" value="7" />
<input type="submit" name="submit" value="Next Step - Clean Up" class="button button-large" />
</form>
<?php
break;
case 7 :
?>
<!-- STEP 7 //-->
<h1>Step 7/7: Cleaning Up</h1>
<ul>
<?php
$tests = array(
array(
'result' => ( !file_exists( 'wordpress.zip' ) || @unlink( 'wordpress.zip' ) ) ,
'pass' => 'Remove WordPress Installer - OK' ,
'fail' => 'Remove WordPress Installer - FAILED'
) ,
array(
'result' => ( !file_exists( 'temp_plugin.zip' ) || @unlink( 'temp_plugin.zip' ) ) ,
'pass' => 'Remove Temporary Plugin File - OK' ,
'fail' => 'Remove Temporary Plugin File - FAILED'
) ,
array(
'result' => ( !file_exists( 'temp_theme.zip' ) || @unlink( 'temp_theme.zip' ) ) ,
'pass' => 'Remove Temporary Theme File - OK' ,
'fail' => 'Remove Temporary Theme File - FAILED'
) ,
array(
'result' => ( !file_exists( __FILE__ ) || @unlink( __FILE__ ) ) ,
'pass' => 'Remove WordPress Remote Installer - OK' ,
'fail' => 'Remove WordPress Remote Installer - FAILED'
) ,
);
foreach( $tests as $t ){
?>
<li class="<?php echo ( $t['result'] ? 'pass' : 'fail' ); ?>"><?php echo $t[( $t['result'] ? 'pass' : 'fail' )]; ?></li>
<?php
}
?>
</ul>
<form method="post" action="./wp-admin/setup-config.php">
<input type="submit" name="submit" value="Launch WordPress Installer" class="button button-large" />
</form>
<?php
break;
}
?>
<div id="footer">
<a href="https://github.com/<?php echo GITHUB_USERNAME; ?>/<?php echo GITHUB_PROJECT; ?>" class="github">View on GitHub</a>
Created by <a href="http://lucanos.com">Luke Stevenson</a><br/>
<div class="legal">
<strong>NOTE:</strong> This script is not an official WordPress product.<br/>
The WordPress logo is the property of the WordPress Foundation.
</div>
</div>
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//<?php echo GITHUB_USERNAME; ?>.github.io/<?php echo GITHUB_PROJECT; ?>/javascripts/installer.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-238524-33');
ga('send', 'pageview', 'step<?php echo $step; ?>');
ga('send', 'event', 'step', '<?php echo $step; ?>');
</script>
</body>
</html>