-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.php
136 lines (124 loc) · 4.52 KB
/
setup.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
<?php
require_once("common.php");
showHeader('ca-nstab-setup', 'Setup cogestione');
$showForm = TRUE;
if(is_readable(CONFIG_FILE)) {
printSuccess('Il programma è configurato. Vai alla <a href=".">pagina iniziale</a>.');
$showForm = FALSE;
} else {
if (file_exists(CONFIG_FILE)) {
printError('Il file di configurazione esiste, ma non è leggibile.');
} else if (!is_writable('.')) {
printError('Non posso creare il file di configurazione perché la directory corrente non è scrivibile.');
} else {
if(isset($_POST['submit'])) {
/* Resets login */
destroyLogin();
echo "<p>Provo a connettermi al database...";
$db = new mysqli($_POST['host'], $_POST['username'], $_POST['password'], $_POST['dbname']);
if ($db->connect_error) {
printError('Errore nella connessione al database: ' . $db->connect_error);
} else {
echo " <b>OK!</b></p>\n";
/* Initial database setup. Overwrites tables, if they exist. */
echo "<p>Provo a creare il database...</p>";
$sql_setup = file_get_contents('sql/cogestione.sql');
$db->multi_query($sql_setup);
$db->close();
echo "<p>Provo a creare il file di configurazione...";
$template = file_get_contents('config.default.php');
$template = str_replace('DB_HOST', addslashes($_POST['host']), $template);
$template = str_replace('DB_USER', addslashes($_POST['username']), $template);
$template = str_replace('DB_PASSWORD', addslashes($_POST['password']), $template);
$template = str_replace('DB_NAME', addslashes($_POST['dbname']), $template);
$template = str_replace('ADMIN_USER', addslashes($_POST['admin_user']), $template);
$template = str_replace('ADMIN_PASSWORD', addslashes($_POST['admin_password']), $template);
$old = umask();
umask(0037);
$res = file_put_contents(CONFIG_FILE, $template);
umask($old);
if($res === FALSE) {
echo " <b>FAIL!</b></p>\n";
printError('Errore nella creazione del file di configurazione!');
} else {
$showForm = FALSE;
echo " <b>OK!</b></p>\n";
printSuccess('Il file di configurazione è stato creato correttamente. Vai alla <a href=".">pagina iniziale</a>.');
}
}
}
}
if($showForm) {
/* Mostra il form */
showConfigForm();
}
}
showFooter();
function showConfigForm() {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Configura</h3>
</div>
<ul class="list-group">
<li class="list-group-item">
<div class="form-group">
<label for="host">DB host: </label>
<input class="form-control" type="text" name="host" id="host" size="20" placeholder="MySQL server host" value="<?php
if(isset($_POST['host'])) {
echo htmlspecialchars($_POST['host']);
} else {
echo 'localhost';
}
?>" />
</div>
<div class="form-group">
<label for="username">DB user: </label>
<input class="form-control" type="text" name="username" id="username" size="20" placeholder="MySQL username" value="<?php
if(isset($_POST['username'])) {
echo htmlspecialchars($_POST['username']);
}
?>" />
</div>
<div class="form-group">
<label for="password">DB password: </label>
<input class="form-control" type="password" name="password" id="password" size="20" placeholder="MySQL password" value="" />
</div>
<div class="form-group">
<label for="dbname">DB name: </label>
<input class="form-control" type="text" name="dbname" id="dbname" size="20" placeholder="MySQL DB name" value="<?php
if(isset($_POST['dbname'])) {
echo htmlspecialchars($_POST['dbname']);
} else {
echo 'cogestione';
}
?>" />
</div>
</li>
<li class="list-group-item">
<div class="form-group">
<label for="admin_user">Admin user: </label>
<input class="form-control" type="text" name="admin_user" id="admin_user" size="20" placeholder="Admin username" value="<?php
if(isset($_POST['admin_user'])) {
echo htmlspecialchars($_POST['admin_user']);
}
?>" />
</div>
<div class="form-group">
<label for="admin_password">Admin password: </label>
<input class="form-control" type="password" name="admin_password" id="admin_password" size="20" placeholder="Admin password" value="" />
</div>
</li>
<li class="list-group-item">
<!-- Submit button -->
<div class="form-group">
<button class="btn btn-primary" type="submit" name="submit">Configura</button>
</div>
</li>
</ul>
</div>
</form>
<?php
}
?>