]> git.armaanb.net Git - dockerfiles.git/blob - wordpress-recon/docker-entrypoint.sh
add recon
[dockerfiles.git] / wordpress-recon / docker-entrypoint.sh
1 #!/bin/bash
2
3 set -e
4
5
6
7 if [ -n "$MYSQL_PORT_3306_TCP" ]; then
8
9         if [ -z "$WORDPRESS_DB_HOST" ]; then
10
11                 WORDPRESS_DB_HOST='mysql'
12
13         else
14
15                 echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP found'
16
17                 echo >&2 "  Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)"
18
19                 echo >&2 '  instead of the linked mysql container'
20
21         fi
22
23 fi
24
25
26
27 if [ -z "$WORDPRESS_DB_HOST" ]; then
28
29         echo >&2 'error: missing WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP environment variables'
30
31         echo >&2 '  Did you forget to --link some_mysql_container:mysql or set an external db'
32
33         echo >&2 '  with -e WORDPRESS_DB_HOST=hostname:port?'
34
35         exit 1
36
37 fi
38
39
40
41 # if we're linked to MySQL, and we're using the root user, and our linked
42
43 # container has a default "root" password set up and passed through... :)
44
45 : ${WORDPRESS_DB_USER:=root}
46
47 if [ "$WORDPRESS_DB_USER" = 'root' ]; then
48
49         : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
50
51 fi
52
53 : ${WORDPRESS_DB_NAME:=wordpress}
54
55
56
57 if [ -z "$WORDPRESS_DB_PASSWORD" ]; then
58
59         echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable'
60
61         echo >&2 '  Did you forget to -e WORDPRESS_DB_PASSWORD=... ?'
62
63         echo >&2
64
65         echo >&2 '  (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)'
66
67         exit 1
68
69 fi
70
71
72
73 if ! [ -e index.php -a -e wp-includes/version.php ]; then
74
75         echo >&2 "WordPress not found in $(pwd) - copying now..."
76
77         if [ "$(ls -A)" ]; then
78
79                 echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
80
81                 ( set -x; ls -A; sleep 10 )
82
83         fi
84
85         tar cf - --one-file-system -C /usr/src/wordpress . | tar xf -
86
87         echo >&2 "Complete! WordPress has been successfully copied to $(pwd)"
88
89         if [ ! -e .htaccess ]; then
90
91                 # NOTE: The "Indexes" option is disabled in the php:apache base image
92
93                 cat > .htaccess <<-'EOF'
94
95                         # BEGIN WordPress
96
97                         <IfModule mod_rewrite.c>
98
99                         RewriteEngine On
100
101                         RewriteBase /
102
103                         RewriteRule ^index\.php$ - [L]
104
105                         RewriteCond %{REQUEST_FILENAME} !-f
106
107                         RewriteCond %{REQUEST_FILENAME} !-d
108
109                         RewriteRule . /index.php [L]
110
111                         </IfModule>
112
113                         # END WordPress
114
115                 EOF
116
117                 chown www-data:www-data .htaccess
118
119         fi
120
121 fi
122
123
124
125 # TODO handle WordPress upgrades magically in the same way, but only if wp-includes/version.php's $wp_version is less than /usr/src/wordpress/wp-includes/version.php's $wp_version
126
127
128
129 if [ ! -e wp-config.php ]; then
130
131         awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
132
133 // If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
134
135 // see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
136
137 if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
138
139         $_SERVER['HTTPS'] = 'on';
140
141 }
142
143 EOPHP
144
145         chown www-data:www-data wp-config.php
146
147 fi
148
149
150
151 set_config() {
152
153         key="$1"
154
155         value="$2"
156
157         php_escaped_value="$(php -r 'var_export($argv[1]);' "$value")"
158
159         sed_escaped_value="$(echo "$php_escaped_value" | sed 's/[\/&]/\\&/g')"
160
161         sed -ri "s/((['\"])$key\2\s*,\s*)(['\"]).*\3/\1$sed_escaped_value/" wp-config.php
162
163 }
164
165
166
167 set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
168
169 set_config 'DB_USER' "$WORDPRESS_DB_USER"
170
171 set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD"
172
173 set_config 'DB_NAME' "$WORDPRESS_DB_NAME"
174
175
176
177 # allow any of these "Authentication Unique Keys and Salts." to be specified via
178
179 # environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY")
180
181 UNIQUES=(
182
183         AUTH_KEY
184
185         SECURE_AUTH_KEY
186
187         LOGGED_IN_KEY
188
189         NONCE_KEY
190
191         AUTH_SALT
192
193         SECURE_AUTH_SALT
194
195         LOGGED_IN_SALT
196
197         NONCE_SALT
198
199 )
200
201 for unique in "${UNIQUES[@]}"; do
202
203         eval unique_value=\$WORDPRESS_$unique
204
205         if [ "$unique_value" ]; then
206
207                 set_config "$unique" "$unique_value"
208
209         else
210
211                 # if not specified, let's generate a random value
212
213                 current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
214
215                 if [ "$current_set" = 'put your unique phrase here' ]; then
216
217                         set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)"
218
219                 fi
220
221         fi
222
223 done
224
225
226
227 TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP'
228
229 <?php
230
231 // database might not exist, so let's try creating it (just to be safe)
232
233 $stderr = fopen('php://stderr', 'w');
234
235 list($host, $port) = explode(':', $argv[1], 2);
236
237 $maxTries = 10;
238
239 do {
240
241         $mysql = new mysqli($host, $argv[2], $argv[3], '', (int)$port);
242
243         if ($mysql->connect_error) {
244
245                 fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
246
247                 --$maxTries;
248
249                 if ($maxTries <= 0) {
250
251                         exit(1);
252
253                 }
254
255                 sleep(3);
256
257         }
258
259 } while ($mysql->connect_error);
260
261 if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) {
262
263         fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n");
264
265         $mysql->close();
266
267         exit(1);
268
269 }
270
271 $mysql->close();
272
273 EOPHP
274
275
276
277 exec "$@"