-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotenv-deploy-salts.php
69 lines (45 loc) · 1.19 KB
/
dotenv-deploy-salts.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
<?php
/**
* Write the salts to .env file
* Overwrite if they already exist.
* Must have https://github.com/RobDWaller/wordpress-salts-generator/ installed
*
*
*/
include_once( __DIR__ . '/vendor/autoload.php' );
$salts = new WPSalts\Salts;
$salts_string = $salts->dotEnv();
$dotenv = '';
//Look for existing .env
$dotenv_path = __DIR__ . '/.env';
if ( file_exists( $dotenv_path ) ) {
$dotenv = file_get_contents( $dotenv_path );
}
//Look for existing salts in the file
$salts_regex_search = [
'AUTH_KEY',
'SECURE_AUTH_KEY',
'LOGGED_IN_KEY',
'NONCE_KEY',
'AUTH_SALT',
'SECURE_AUTH_SALT',
'LOGGED_IN_SALT',
'NONCE_SALT',
];
$c = 0;
foreach( $salts_regex_search as $salt ) {
$re = sprintf( "/^%s=.+\n/m", $salt );
$sub = '';
//Leave a placeholder to replace after we clear out the rest of the salts
if ( $c == 0 ) {
$sub = "#{salts}\n";
}
$dotenv = preg_replace( $re, $sub, $dotenv );
$c++;
}
$dotenv = str_replace( '#{salts}', $salts_string, $dotenv );
if ( file_put_contents( $dotenv_path, $dotenv ) ) {
echo "\nThe new salts have been written.\n";
} else {
echo "\nThere was an error writing the salts.\n";
}