-
Notifications
You must be signed in to change notification settings - Fork 84
/
makedb.php
102 lines (84 loc) · 2.18 KB
/
makedb.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
<?php
// Args: 0 => makedb.php, 1 => "$JOOMLA_DB_HOST", 2 => "$JOOMLA_DB_USER", 3 => "$JOOMLA_DB_PASSWORD", 4 => "$JOOMLA_DB_NAME", 5 => "$JOOMLA_DB_TYPE"
$stderr = fopen('php://stderr', 'w');
fwrite($stderr, "\nEnsuring Joomla database is present\n");
if (strpos($argv[1], ':') !== false)
{
list($host, $port) = explode(':', $argv[1], 2);
}
else
{
$host = $argv[1];
$port = null;
}
$user = $argv[2];
$password = $argv[3];
$db = $argv[4];
$dbType = strtolower($argv[5]);
if ($dbType === 'mysqli')
{
$port = $port ? (int)$port : 3306;
$maxTries = 10;
// set original default behaviour for PHP 8.1 and higher
// see https://www.php.net/manual/en/mysqli-driver.report-mode.php
mysqli_report(MYSQLI_REPORT_OFF);
do {
$mysql = new mysqli($host, $user, $password, '', $port);
if ($mysql->connect_error)
{
fwrite($stderr, "\nMySQL Connection Error: ({$mysql->connect_errno}) {$mysql->connect_error}\n");
--$maxTries;
if ($maxTries <= 0)
{
exit(1);
}
sleep(3);
}
} while ($mysql->connect_error);
if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($db) . '`'))
{
fwrite($stderr, "\nMySQL 'CREATE DATABASE' Error: " . $mysql->error . "\n");
$mysql->close();
exit(1);
}
fwrite($stderr, "\nMySQL Database Created\n");
$mysql->close();
}
elseif ($dbType === 'pgsql')
{
$port = $port ? (int)$port : 5432;
$maxTries = 10;
do {
$connection = "host={$host} port={$port} user={$user} password={$password} dbname=postgres";
$dbconn = @pg_connect($connection);
if (!$dbconn)
{
fwrite($stderr, "\nPostgreSQL Connection Error\n");
--$maxTries;
if ($maxTries <= 0)
{
exit(1);
}
sleep(3);
}
} while (!$dbconn);
$query = "SELECT 1 FROM pg_database WHERE datname = '$db'";
$result = pg_query($dbconn, $query);
if (pg_num_rows($result) == 0)
{
$createDbQuery = "CREATE DATABASE \"$db\"";
if (!pg_query($dbconn, $createDbQuery))
{
fwrite($stderr, "\nPostgreSQL 'CREATE DATABASE' Error\n");
pg_close($dbconn);
exit(1);
}
}
fwrite($stderr, "\nPostgreSQL Database Created\n");
pg_close($dbconn);
}
else
{
fwrite($stderr, "\nInvalid database type. Please provide 'pgsql' or 'mysqli'.\n");
exit(1);
}