-
Notifications
You must be signed in to change notification settings - Fork 10
/
wordpress-cli-installer.php
340 lines (324 loc) · 11.2 KB
/
wordpress-cli-installer.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
<?php
/**
* Nexcess.net Wordpress CLI Installer
* Copyright (C) 2011 Nexcess.net L.L.C.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @author Alex Headley <[email protected]>
*/
// Suppress Deprecated and PHP Strict Messages
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
// Set SERVER_NAME explicitly otherwise phpmailer fails to
// determine the hostname to use for the sender email when
// installing. If we don't do this, it ends up as something
// like "wordpress@" which fails validation and aborts
// the installation.
if( function_exists( 'gethostname' ) ) {
$_SERVER['SERVER_NAME'] = gethostname();
} else {
$_SERVER['SERVER_NAME'] = php_uname('n');
}
/**
* Simple message logging helper
*
* @param string $message
*/
function _wpi_log( $message ) {
printf( '%s %s' . PHP_EOL, @date( 'c' ), trim( $message, PHP_EOL ) );
}
/**
* Log debug messages (hidden without the -v option)
*
* @param string $message
*/
function _wpi_debug( $message ) {
if( _WPI_VERBOSE ) {
_wpi_log( 'DEBUG: ' . $message );
}
}
/**
* Die with an error message + optional exit code
*
* @param string $message
* @param int $code
*/
function _wpi_die( $message, $code = 1 ) {
print 'ERROR: ' . rtrim( $message, PHP_EOL ) . PHP_EOL;
exit( $code );
}
/**
* Print usage information and die (2)
*/
function _wpi_usage() {
print 'Usage: wordpress-cli-installer.sh [-hPv] -b base-url -e email-address [-p admin-password]
[-T blog-title] [-u admin-user] [-l lang] [--dbuser=database-user] [--dbpass=database-pass]
[--dbname=database-name] [--dbhost=database-host] path/to/wp/files/
General options:
-b <base-url>
Base URL for the blog since wordpress can\'t detect it from a CLI
install, should be a fully qualified URL (ex: http://example.com/)
REQUIRED
-e <email-address>
Admin user\'s email address
REQUIRED
-h
Display this help text
-p <admin-password>
Admin users\'s password
default: randomly generated
-P
Toggle whether the blog is public or not (visible to search engines, etc)
default: public (on)
-s
Toggle whether the blog requires an SSL connection for the admin section
default: off
-T <blog-title>
Set the blog\'s title, this should probably be short (and quoted)
default: Change Me
-u <admin-user>
Admin user\'s username
default: admin
-l <lang>
Language of this wordpress blog
default: <empty> (en-US)
-v
Verbose flag, enable more output
wp-config options:
These options are only used if wp-config.php isn\'t found, in which case
they are required.
--dbuser=<database-user>
Database user\'s username
--dbpass=<database-pass>
Database user\'s password
--dbname=<database-name>
Database name
--dbhost=<database-host>
Database hostname. Passing host:port or /path/to/socket.sock might also
work
default: localhost'. PHP_EOL;
exit( 2 );
}
/**
* Generate a random alphanumeric string
*
* @param int $length
* @return string
*/
function _wpi_random_string( $length = 12 ) {
$validChars = array_merge(
range( 'a', 'z' ),
range( 'A', 'Z' ),
// here twice so that numbers are more common
range( '0', '9' ),
range( '0', '9' ) );
$validCharCount = count( $validChars );
$pass = '';
while( strlen( $pass ) < $length ) {
$pass .= $validChars[rand() % $validCharCount];
}
return $pass;
}
/**
* Create a wp-config.php file, will overwrite an existing one
*
* @param string $dbName
* @param string $dbUser
* @param string $dbPass
* @param string $dbHost
* @param string $lang
*/
function _wpi_create_wp_config( $dbName, $dbUser, $dbPass, $dbHost, $secureAdmin, $lang ) {
_wpi_debug( 'Creating wp-config.php' );
if( is_null( $dbName ) || is_null( $dbUser ) || is_null( $dbPass ) ) {
_wpi_die( 'Database name, user and password are required to create the wp-config.php file', 9 );
}
if( $fp = fopen( 'wp-config.php', 'w' ) ) {
fwrite( $fp, '<?php' . PHP_EOL );
//configurable database stuff
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, 'DB_NAME', $dbName );
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, 'DB_USER', $dbUser );
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, 'DB_PASSWORD', $dbPass );
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, 'DB_HOST', $dbHost );
//non-configurable database stuff
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, 'DB_CHARSET', 'utf8' );
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, 'DB_COLLATE', '' );
fprintf( $fp, '$table_prefix = \'%s\';' . PHP_EOL, 'wp_' );
//secret key stuff
$authKeys = array(
'AUTH_KEY',
'SECURE_AUTH_KEY',
'LOGGED_IN_KEY',
'NONCE_KEY',
'AUTH_SALT',
'SECURE_AUTH_SALT',
'LOGGED_IN_SALT',
'NONCE_SALT',
);
foreach( $authKeys as $authKey ) {
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, $authKey,
_wpi_random_string( 32 ) );
}
//some more wp configs
fprintf( $fp, 'define( \'%s\', \'%s\' );' . PHP_EOL, 'WPLANG', $lang );
fprintf( $fp, 'define( \'%s\', %s );' . PHP_EOL, 'WP_DEBUG', 'false' );
if( $secureAdmin ) {
fprintf( $fp, 'define( \'%s\', %s );' . PHP_EOL, 'FORCE_SSL_ADMIN', 'true' );
}
//finish up
fwrite( $fp, 'if ( !defined(\'ABSPATH\') )
define(\'ABSPATH\', dirname(__FILE__) . \'/\');
require_once(ABSPATH . \'wp-settings.php\');' . PHP_EOL );
fclose( $fp );
} else {
_wpi_die( 'Could not open wp-config.php for writing', 3 );
}
}
/**
* Parse the options or print the usage text if parsing failed
*
* @param array $result
* @return array
*/
function _wpi_clean_opts( $result ) {
if( !is_array( $result ) ) {
_wpi_die( 'Failed parsing options: ' . $result->getMessage(), 6 );
} else {
list( $opts, $args ) = $result;
$parsed = array(
'baseurl' => null,
'email' => null,
'pass' => _wpi_random_string(),
'public' => true,
'title' => 'Change Me',
'secure' => false,
'lang' => '',
'user' => 'admin',
'dbuser' => null,
'dbpass' => null,
'dbname' => null,
'dbhost' => 'localhost',
);
foreach( $opts as $opt ) {
switch( $opt[0] ) {
case 'b':
//if we define this here, wp_guess_url will use it
// which avoids a few issues
if( !defined( 'WP_SITEURL' ) ) {
define( 'WP_SITEURL', rtrim( $opt[1], '/' ) );
$parsed['baseurl'] = WP_SITEURL;
} else {
_wpi_debug( 'WP_SITEURL already defined, skipping another baseurl' );
}
break;
case 'e':
$parsed['email'] = $opt[1];
break;
case 'h':
_wpi_usage();
case 'p':
$parsed['pass'] = $opt[1];
break;
case 'l':
$parsed['lang'] = $opt[1];
break;
case 'P':
$parsed['public'] = false;
break;
case 'T':
$parsed['title'] = $opt[1];
break;
case 's':
$parsed['secure'] = true;
break;
case 'u':
$parsed['user'] = $opt[1];
break;
case 'v':
if( !defined( '_WPI_VERBOSE' ) ) {
define( '_WPI_VERBOSE', true );
}
break;
default:
if( strstr( $opt[0], '--' ) !== false ) {
$parsed[ltrim( $opt[0], '-' )] = $opt[1];
} else {
_wpi_die( 'Unrecognized option: ' . $opt[1], 5 );
}
break;
}
}
if( !defined( '_WPI_VERBOSE' ) ) {
define( '_WPI_VERBOSE', false );
}
if( !defined( 'WP_SITEURL' ) ) {
_wpi_die( '-b option is required', 8 );
}
if( is_null( $parsed['email'] ) ) {
_wpi_die( '-e option is required', 8 );
}
if( count( $args ) === 1 ) {
$parsed['path'] = realpath( $args[0] );
if( is_dir( $parsed['path'] ) ) {
//parse the rest
_wpi_debug( 'Read options: ' . print_r( $parsed, true ) );
return $parsed;
} else {
_wpi_die( 'Path is not a directory: ' . $parsed['path'], 4 );
}
} else {
_wpi_die( 'Incorrect arg count: ' . count( $args ), 4 );
}
}
}
// remove crap we don't care about
array_shift( $argv );
$argc--;
$shortOptions = 'b:e:hp:PT:su:vl:';
$longOptions = array(
'dbuser=',
'dbpass=',
'dbname=',
'dbhost=',
);
require_once 'Console/Getopt.php';
$reader = new Console_Getopt;
$parsed = _wpi_clean_opts( $reader->getopt( $argv, $shortOptions, $longOptions ) );
_wpi_debug( 'Moving to path: ' . $parsed['path'] );
chdir( $parsed['path'] );
if( !is_readable( 'wp-config.php' ) ) {
_wpi_create_wp_config( $parsed['dbname'], $parsed['dbuser'],
$parsed['dbpass'], $parsed['dbhost'], $parsed['secure'], $parsed['lang'] );
}
_wpi_debug( 'Running installer' );
$deprecated = null;
define( 'WP_INSTALLING', true );
require_once 'wp-load.php';
require_once 'wp-admin/includes/upgrade.php';
require_once 'wp-includes/wp-db.php';
wp_install( $parsed['title'], $parsed['user'], $parsed['email'],
$parsed['public'], $deprecated, $parsed['pass'] );
printf( 'Blog URL: %4$s
Admin URL: %1$s
Username: %2$s
Password: %3$s' . PHP_EOL,
WP_SITEURL . '/wp-admin/', $parsed['user'],
$parsed['pass'], WP_SITEURL . '/' );
//there are die() calls in the wp_install routine so we can't be sure that
// we installed correctly unless we use some weird return value (bad installs
// will exit 0 if it was in wp_install and 1-$something_low if it was in this
// script, then we exit 128 if successful
exit( 128 );