/
db-error.php
271 lines (246 loc) · 8.91 KB
/
db-error.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
<?php
/**
* Custom error page rather than database connection error
* source: http://alexphelps.github.io/server-error-pages/
* modifications: Onni Hakala / Geniem Oy
*/
// Prevent direct access
defined('ABSPATH') or die();
/**
* Check dns resolving for DB_HOST
*/
function check_dns() {
if (filter_var(DB_HOST, FILTER_VALIDATE_IP) === false) {
$ips = gethostbynamel(DB_HOST);
if ( empty($ips) ) {
debug_msg('\''.DB_HOST . '\' dns query doesn\'t resolve to any ip address.');
} else {
debug_msg(DB_HOST . ' dns query resolves to: ' . implode(',', $ips) );
}
} else {
// If database is just plain ip address dns doesn't matter
$ips = array(DB_HOST);
}
return $ips;
}
/**
* Check if DB_PORT is open for DB_HOST
* This is needed in case you have multiple dns load balanced databases
*/
function check_open_ports($hosts) {
$result = false;
if ( ! empty($hosts) ) {
$result = true;
$port = ( defined('DB_PORT') ? DB_PORT : 3306 );
foreach ( $hosts as $host ) {
if ( ! check_open_port( $host,$port ) ) {
$result = false;
}
}
}
return $result;
}
/**
* Check if DB_PORT is open for DB_HOST
*/
function check_open_port($host,$port) {
$connection = @fsockopen($host, $port);
if (is_resource($connection)) {
debug_msg($host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') port is open.');
fclose($connection);
return true;
} else {
debug_msg($host . ':' . $port . ' port is not responding.');
return false;
}
}
/**
* Checks if password is correct or database exists
*/
function check_mysql_connection($host,$user,$password,$database) {
$link = mysqli_connect($host, $user, $password, $database);
if (!$link) {
debug_msg('Unable to connect to MySQL, errorno: '.mysqli_connect_errno());
debug_msg(mysqli_connect_error());
return false;
}
mysqli_close($link);
return true;
}
/**
* int main() for debugging
*/
function debug_connection() {
// Don't show warnings here since then we would just have the same messages two times
error_reporting(0);
/*
* Check if dns to database is resolving correctly
*/
$db_ips = check_dns();
/**
* Check if port to database is open
* Check all databases in case this resolves to multiple ones
*/
if ( ! empty($db_ips) ) {
$result = check_open_ports($db_ips);
}
/**
* If mysql is running try to determine what went wrong
*/
if ($result) {
check_mysql_connection($db_ips[0], DB_USER, DB_PASSWORD, DB_NAME);
}
}
/**
* Ensure the same debugging headers
*/
function send_db_error_headers() {
if (!headers_sent()) { // Only send headers if we can, We don't to create more errors
header( 'Content-Type: text/html; charset=utf-8' );
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 300'); // 5 minutes = 300 seconds
nocache_headers();
}
}
/**
* Helpers to output into browser and to the command line
* - Use colorful output to cli
*/
// This outputs into commandline
if ( defined( 'WP_CLI' ) ) {
function error_msg($message) { echo "\033[31mError: \033[0m{$message} \n"; }
function info_msg($message, $type='INFO' ){ echo "\033[33m{$type}: \033[0m{$message} \n"; }
function debug_msg($message, $type='DEBUG' ){ echo "\033[36m{$type}: \033[0m{$message} \n"; }
// This outputs to browser
} elseif ( defined('WP_DEBUG') && WP_DEBUG ) {
function error_msg($message) { echo "ERROR: {$message} \n"; }
function info_msg($message, $type='INFO' ){ echo "{$type}: {$message} \n"; }
function debug_msg($message, $type='DEBUG' ){ echo "{$type}: {$message} \n"; }
// This outputs to error logs
} else {
function error_msg($message) { error_log("DB ERROR: {$message}", 0); }
function info_msg($message, $type='INFO' ){ error_log("DB ERROR {$type}: {$message}", 0); }
function debug_msg($message, $type='DEBUG' ){ error_log("DB ERROR {$type}: {$message}", 0); }
}
// Allow better debugging if this is run from command line
if ( defined( 'WP_CLI' ) ) {
error_msg( 'Error establishing a database connection' );
if ( defined('DB_HOST') ) {
debug_connection();
} else {
error_msg( 'DB_HOST is not defined' );
}
// Also show errors if WP_DEBUG is enabled
} else {
send_db_error_headers();
if ( defined('WP_DEBUG') && WP_DEBUG ) { ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"<?php if ( is_rtl() ) echo ' dir="rtl"'; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php _e( 'Database Error' ); ?></title>
<style>
pre {
background: lightgrey;
padding: 20px;
font-size: 16px;
}
</style>
</head>
<body>
<h1><?php _e( 'Error establishing a database connection' ); ?></h1>
<pre><?php debug_connection(); ?></pre>
</body>
</html>
<?php
} else {
// Debug connection to logs
debug_connection();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="503 Palvelu ei ole saatavilla">
<meta name="author" content="">
<title>503 Palvelu ei ole saatavilla</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<style>
/* Error Page Inline Styles */
body {
padding-top: 20px;
}
/* Layout */
.jumbotron {
font-size: 21px;
font-weight: 200;
line-height: 2.1428571435;
color: inherit;
padding: 10px 0px;
}
/* Everything but the jumbotron gets side spacing for mobile-first views */
.masthead, .body-content, {
padding-left: 15px;
padding-right: 15px;
}
/* Main marketing message and sign up button */
.jumbotron {
text-align: center;
background-color: transparent;
}
.jumbotron .btn {
font-size: 21px;
padding: 14px 24px;
}
/* Colors */
.green {color:#5cb85c;}
.orange {color:#f0ad4e;}
.red {color:#d9534f;}
</style>
<script type="text/javascript">
function loadDomain() {
var display = document.getElementById("display-domain");
display.innerHTML = document.domain;
}
</script>
</head>
<body onload="javascript:loadDomain();">
<!-- Error Page Content -->
<div class="container">
<!-- Jumbotron -->
<div class="jumbotron">
<h1><i class="fa fa-exclamation-triangle orange"></i> 503 Palvelu ei ole saatavilla</h1>
<p class="lead">Sivustolla <em><span id="display-domain"><?php echo $_SERVER['HTTP_HOST']; ?></span></em> tapahtui väliaikainen virhe.</p>
<a href="javascript:document.location.reload(true);" class="btn btn-default btn-lg text-center"><span class="green">Lataa sivu uudelleen tästä</span></a>
</div>
</div>
<div class="container">
<div class="body-content">
<div class="row">
<div class="col-md-6">
<h2>Mitä tapahtui?</h2>
<p class="lead">503 virhekoodi tarkoittaa, että palvelimella on väliaikainen lyhytkestoinen ongelma, tai että sitä ylläpidetään parhaillaan. Virhe häviää normaalisti nopeasti.</p>
</div>
<div class="col-md-6">
<h2>Mitä voin tehdä?</h2>
<p class="lead">Jos olet sivuston käyttäjä</p>
<p>Pahoittelemme käyttökatkosta, valvontamme on havainnut ongelman ja sitä selvitetään parhaillaan.</p>
<p class="lead">Jos olet sivuston omistaja</p>
<p>Ongelma on todennäköisesti väliaikainen, mutta voit varmuudeksi tarkistaa sivuston tilankäytön ja tietokantayhteyden.</p>
</div>
</div>
</div>
</div>
<!-- End Error Page Content -->
<!--Scripts-->
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</body>
</html>
<?php }
}