This repository has been archived by the owner on Jun 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.php
290 lines (219 loc) · 8.64 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* Installs MySQL database
*
* @author Lovell Felix <[email protected]>
* @copyright Copyright © 2009-2012 Lovell Felix
* @link http://labs.lovellfelix.com
*/
include_once('header.php');
$install = new Install();
class Install {
private $error;
private $link;
private $settings = array();
function __construct() {
$this->checkInstall($hideError = true);
if( !empty($_POST) ) :
foreach ($_POST as $key => $value)
$this->settings[$key] = $value;
$this->validate();
endif;
if(!empty($this->error))
echo $this->error;
}
// Run queries
private function query($query) {
$result = mysql_query($query);
if (!$result) {
echo _('Could not run query:') . mysql_error() . '<br/>';
include_once('footer.php');
exit;
}
return $result;
}
// Check for all form fields to be filled out
private function validate() {
if(strlen($this->settings['adminPass']) < 5)
$this->error = '<div class="alert alert-error">'._('Password must be at least 5 characters.').'</div>';
else
$this->settings['adminPass'] = md5($this->settings['adminPass']);
if( empty($this->settings['dbHost']) || empty($this->settings['dbUser']) || empty($this->settings['dbName']) || empty($this->settings['scriptPath']) || empty($this->settings['email']) || empty($this->settings['adminUser']) || empty($this->settings['adminPass'] ))
$this->error = '<div class="alert alert-error">'._('Fill out all the details please').'</div>';
// Check the database connection
$this->dbLink();
}
// Check if there is a connection to the mysql server
private function dbLink() {
if(empty($this->error)) {
$this->link = @mysql_connect($this->settings['dbHost'], $this->settings['dbUser'], $this->settings['dbPass']);
if(!$this->link)
$this->error = '<div class="alert alert-error">'._('Your Database details are incorrect.').'</div>';
else
$this->dbSelect();
}
}
// Check for database selection
private function dbSelect() {
if(empty($this->error)) {
$dbSelect = mysql_select_db($this->settings['dbName'],$this->link);
if(!$dbSelect)
$this->error = '<div class="alert alert-error">'._('Database name doesn\'t exist !').'</div>';
else
$this->existingTables();
}
}
// Check for an existing installation
private function existingTables() {
if(empty($this->error)) :
$this->insertSQL();
$this->writeFile();
$this->checkInstall();
endif;
}
// Insert SQL data
private function insertSQL() {
if(empty($this->error)) {
$this->query("SET NAMES utf8;");
$this->query("
CREATE TABLE IF NOT EXISTS `website_settings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`option_name` varchar(255) NOT NULL,
`option_value` longtext NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
UNIQUE KEY `option_name` (`option_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$ok = $this->query("
INSERT IGNORE INTO `website_settings` (`id`, `option_name`, `option_value`) VALUES
(1, 'site_address', '".$this->settings['scriptPath']."'),
(2, 'admin_email', '".$this->settings['email']."');
");
$this->query("
CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(8) NOT NULL AUTO_INCREMENT,
`username` varchar(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(128) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id` (`user_id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$this->query("
INSERT IGNORE INTO `users` (`user_id`, `username`, `name`, `email`, `password`) VALUES
(1, '".$this->settings['adminUser']."', 'Admin', '".$this->settings['email']."', '".$this->settings['adminPass']."'),
(2, 'lovell', 'Lovell Felix', '[email protected]', '21232f297a57a5a743894a0e4a801fc3');
");
} else $this->error = 'Your tables already exist! I won\'t insert anything.';
}
private function writeFile() {
if($this->error == '') {
/** Write db-config.php if it doesn't exist */
$fp = @fopen("config/db-connect.php", "w");
if( !$fp ) :
echo '<div class="alert alert-warning">'._('Could not create config/db-connect.php, please confirm you have permission to create the file.').'</div>';
return false;
endif;
fwrite($fp, '<?php
////////////////////
// This file contains the database access information.
// This file is needed to establish a connection to MySQL
$host = "'.$this->settings['dbHost'].'"; // localhost normally works, if localhost doesn\'t exist contact your web host
$dbName = "'.$this->settings['dbName'].'"; // Database name
$dbUser = "'.$this->settings['dbUser'].'"; // Username
$dbPass = "'.$this->settings['dbPass'].'"; // Password
?>');
fclose($fp);
}
}
private function checkInstall($hideError = false) {
if (file_exists('config/db-connect.php')) : ?>
<div class="row-fluid">
<div class="span8">
<div class="alert alert-success"><strong>Success!</strong> Installation is completed </div>
<p><span class="label label-important">Important</span>
Delete or rename the install folder to prevent security risk. </p>
</div>
</div> <?php
include('footer.php');
exit();
else :
if (!$hideError) $this->error = '<div class="alert alert-error">'._('Installation is not complete.').'</div>';
endif;
}
}
?>
<div class="row">
<div class="span6">
<form class="form-horizontal" method="post" action="setup.php">
<fieldset>
<legend><small>Enter your Database connections details</small></legend>
<div class="control-group">
<label class="control-label" for="dbHost">Host</label>
<div class="controls">
<input type="text" class="input-large" id="dbHost" name="dbHost" value="<?php if(isset($_POST['dbHost'])) echo $_POST['dbHost']; else echo 'localhost'; ?>" >
</div>
</div>
<div class="control-group">
<label class="control-label" for="dbName">Database name</label>
<div class="controls">
<input type="text" class="input-large" id="dbName" name="dbName" value="<?php if(isset($_POST['dbName'])) echo $_POST['dbName']; else echo 'database_name'; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="dbUser">Username</label>
<div class="controls">
<input type="text" class="input-large" id="dbUser" name="dbUser" value="<?php if(isset($_POST['dbUser'])) echo $_POST['dbUser']; else echo 'db_username'; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="dbPass">Password</label>
<div class="controls">
<input type="text" class="input-large" id="dbPass" name="dbPass" value="<?php if(isset($_POST['dbPass'])) echo $_POST['dbPass']; else echo 'db password'; ?>">
</div>
</div>
</fieldset>
</div>
<div class="span5 offset1">
<fieldset>
<legend><small>Website Settings</small></legend>
<div class="control-group">
<label class="control-label" for="scriptPath">Website address</label>
<div class="controls">
<input type="url" class="input-large" id="scriptPath" name="scriptPath" value="<?php if(isset($_POST['scriptPath'])) echo $_POST['scriptPath']; else echo "http://".$_SERVER['HTTP_HOST'].str_replace("db-installer/setup.php","",str_replace("functions","",str_replace("\\","/",$_SERVER['SCRIPT_NAME']))); ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Admin email</label>
<div class="controls">
<input type="email" class="input-large" id="email" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; else echo 'admin@'.$_SERVER['HTTP_HOST']; ?>">
</div>
</div>
</fieldset>
<fieldset>
<legend><small>Admin Account</small></legend>
<div class="control-group">
<label class="control-label" for="adminUser">Username</label>
<div class="controls">
<input type="text" class="input-large" id="adminUser" name="adminUser" value="<?php if(isset($_POST['adminUser'])) echo $_POST['adminUser']; else echo 'admin'; ?>">
</div>
</div>
<div class="control-group">
<label class="control-label" for="adminPass">Password</label>
<div class="controls">
<input type="text" class="input-large" id="adminPass" name="adminPass" value="<?php if(isset($_POST['adminPass'])) echo $_POST['adminPass']; else echo 'admin'; ?>">
</div>
</div>
</fieldset>
</div>
<div class="form-actions">
<button type="submit" class="pull-right btn btn-primary">Install</button>
</div>
</form>
</div>
</div>
<?php include_once('footer.php'); ?>