YaWK  24.1
Yet another WebKit
checkDatabaseCredentials.php
Go to the documentation of this file.
1 <?php
2 // Check if the request was made using the POST method
3 if ($_SERVER['REQUEST_METHOD'] === 'POST')
4 { // prove given database credentials
5 
6  // check if DB_HOST is set
7  if (!empty($_POST['DB_HOST']))
8  { // DB_HOST is set
9  $host = $_POST['DB_HOST'];
10  }
11  else
12  { // DB_HOST is missing
13  $response = array('error' => true, 'message' => 'DB_HOST is missing');
14  $host = '';
15  }
16 
17  // check if DB_USER is set
18  if(!empty($_POST['DB_USER']))
19  { // DB_USER is set
20  $username = $_POST['DB_USER'];
21  }
22  else
23  { // DB_USER is missing
24  $response = array('error' => true, 'message' => 'DB_USER is missing');
25  $username = '';
26  }
27 
28  // check if DB_PASS is set
29  if(!empty($_POST['DB_PASS']))
30  { // DB_PASS is set
31  $password = $_POST['DB_PASS'];
32  }
33  else
34  { // DB_PASS is missing
35  $response = array('error' => true, 'message' => 'DB_PASS is missing');
36  $password = '';
37  }
38 
39  // check if DB_NAME is set
40  if (!empty($_POST['DB_NAME']))
41  { // DB_NAME is set
42  $dbname = $_POST['DB_NAME'];
43  }
44  else
45  { // DB_NAME is missing
46  $response = array('error' => true, 'message' => 'DB_NAME is missing');
47  $dbname = '';
48  }
49 
50  // check if DB_PORT is set
51  if(!empty($_POST['DB_PORT']))
52  { // DB_PORT is set
53  $port = $_POST['DB_PORT'];
54  }
55  else
56  { // DB_PORT is missing
57  $response = array('error' => true, 'message' => 'DB_PORT is missing (default is 3306)');
58  $port = 3306;
59  }
60 
61  // try to successfully connect to the database
62  try {
63  // Create a new mysqli object with given database credentials
64  $db = new \mysqli($host, $username, $password, $dbname, $port);
65  $response = array('success' => true,
66  'message' => ''.$_POST['DB_CHECK_TO'].' '.$dbname.' @ '.$host.' '.$_POST['DB_CHECK_EST'].'.',
67  'subline' => $_POST['DB_CHECK_DATA'],
68  'importmsg' => $_POST['DB_IMPORT_MSG'],
69  'DB_IMPORT_BTN' => $_POST['DB_IMPORT_BTN']);
70  }
71  // failed to connect to the database
72  catch (Exception $e)
73  { // Credentials are invalid, return an error message
74  $response = array('error' => false,
75  'message' => $_POST['DB_CHECK_FAILED'],
76  'subline' => $e->getMessage(),
77  'checkagain' => $_POST['DB_CHECK_AGAIN']);
78  }
79 
80  // Send the response as JSON
81  header('Content-Type: application/json');
82  // Send the response
83  echo json_encode($response);
84 }
$host
Definition: page-edit.php:65
foreach($updateFilebase as $key=> $value) foreach($localFilebase as $filePath=> $localHash) $response