forked from phpback/phpback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install1.php
213 lines (195 loc) · 8.37 KB
/
install1.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
<?php
/**
* First step of setup: database creation (action of index.php)
* @copyright Copyright (c) 2014 PHPBack
* @author Ivan Diaz <ivan@phpback.org>
* @author Benjamin BALET<benjamin.balet@gmail.com>
* @license http://opensource.org/licenses/GPL-3.0 GPL-3.0
* @link https://github.com/ivandiazwm/phpback
* @since 1.0
*/
define('APPLICATION_LOADED', true);
define('BASEPATH', '.'); //Make this script work with nginx
error_reporting(E_ALL);
ini_set('display_errors', 1);
include "pretty_message.php";
/**
* Redirect to the initial form and pass to the page an array containing :
* 1. Previous entered values
* 2. Error messages if any
* @param string $url URL of the action page
* @param array $data array containing the previous posted values in the form
*/
function redirectPost($url, array $data) {
echo '<html><head>
<script type="text/javascript">
function close() {
document.forms["redirectpost"].submit();
}
</script>
<title>Please Wait...</title>
</head>
<body onload="close();">
Please Wait...<br />
<form name="redirectpost" method="post" action="' . $url . '">';
if (!is_null($data)) {
foreach ($data as $k => $v) {
echo '<input type="hidden" name="' . $k . '" value="' . $v . '"> ';
}
}
echo "</form></body></html>";
exit(1);
}
/**
* An error was encountered, so send back to the initial form
* @param string $errorMessage Error message sent back by the database driver
*/
function exitOnError($errorMessage) {
$data['error'] = $errorMessage;
redirectPost('index.php', $data);
}
/**
* Create the CodeIgniter database configuration file
* @param type $hostname
* @param type $username
* @param type $password
* @param type $database
*/
function createDbConfigFile($hostname, $username, $password, $database) {
@chmod('../application/config', 0777);
if (($file = fopen('../application/config/database.php', 'w+')) == FALSE) {
exitOnError('ERROR #1: Config file could not be created');
}
$content = '<?php ' . PHP_EOL;
$content .= '//Configuration generated by install script' . PHP_EOL;
$content .= '$active_group = \'default\';' . PHP_EOL;
$content .= '$active_record = TRUE;' . PHP_EOL;
$content .= '$db[\'default\'][\'hostname\'] = \'' . $hostname . '\';' . PHP_EOL;
$content .= '$db[\'default\'][\'username\'] = \'' . $username . '\';' . PHP_EOL;
$content .= '$db[\'default\'][\'password\'] = \'' . $password . '\';' . PHP_EOL;
$content .= '$db[\'default\'][\'database\'] = \'' . $database . '\';' . PHP_EOL;
$content .= '$db[\'default\'][\'dbdriver\'] = \'mysqli\';' . PHP_EOL;
$content .= '$db[\'default\'][\'dbprefix\'] = \'\';' . PHP_EOL;
$content .= '$db[\'default\'][\'pconnect\'] = TRUE;' . PHP_EOL;
$content .= '$db[\'default\'][\'db_debug\'] = TRUE;' . PHP_EOL;
$content .= '$db[\'default\'][\'cache_on\'] = FALSE;' . PHP_EOL;
$content .= '$db[\'default\'][\'cachedir\'] = \'\';' . PHP_EOL;
$content .= '$db[\'default\'][\'char_set\'] = \'utf8\';' . PHP_EOL;
$content .= '$db[\'default\'][\'dbcollat\'] = \'utf8_general_ci\';' . PHP_EOL;
$content .= '$db[\'default\'][\'swap_pre\'] = \'\';' . PHP_EOL;
$content .= '$db[\'default\'][\'autoinit\'] = TRUE;' . PHP_EOL;
$content .= '$db[\'default\'][\'stricton\'] = FALSE;' . PHP_EOL;
if (fwrite($file, $content) == FALSE) {
fclose($file);
exitOnError('ERROR #1: Config file could not be created');
}
fclose($file);
}
/**
* Generate some random bytes by using openssl, dev/urandom or random
* @param int $count length of the random string
* @return string a string of pseudo-random bytes (must be encoded)
* @author Benjamin BALET <benjamin.balet@gmail.com>
*/
function getRandomBytes($length) {
if(function_exists('openssl_random_pseudo_bytes')) {
$rnd = openssl_random_pseudo_bytes($length, $strong);
if ($strong === TRUE)
return $rnd;
}
$sha =''; $rnd ='';
if (file_exists('/dev/urandom')) {
$fp = fopen('/dev/urandom', 'rb');
if ($fp) {
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($fp, 0);
}
$sha = fread($fp, $length);
fclose($fp);
}
}
for ($i=0; $i<$length; $i++) {
$sha = hash('sha256',$sha.mt_rand());
$char = mt_rand(0,62);
$rnd .= chr(hexdec($sha[$char].$sha[$char+1]));
}
return $rnd;
}
/**
* Hash a password using BCRYPT algo and 8 iterations
* @param string $password Clear password to be hashed
* @return string Hashed password
*/
function hashPassword($password) {
//Hash the clear password using bcrypt (8 iterations)
$salt = '$2a$08$' . substr(strtr(base64_encode(getRandomBytes(16)), '+', '.'), 0, 22) . '$';
$hash = crypt($password, $salt);
return $hash;
}
/* if started from commandline, wrap parameters to $_POST */
if (!isset($_SERVER["HTTP_HOST"]))
parse_str($argv[1], $_POST);
if ($_POST['adminpass'] != $_POST['adminrpass'])
exitOnError('Admin passwords do not match');
$server = new mysqli($_POST['hostname'], $_POST['username'], $_POST['password']);
if ($server->connect_error) {
$str = mb_convert_encoding($server->connect_error, "UTF-8", "auto");
exitOnError('ERROR #2: Server connection error (' . $server->connect_errno . ') ' . $str);
}
if ($_POST['database'] != "") {
if (!file_exists('../application/config/database.php'))
createDbConfigFile($_POST['hostname'], $_POST['username'], $_POST['password'], $_POST['database']);
include '../application/config/database.php';
if (!($_POST['hostname'] == $db['default']['hostname'] && $_POST['username'] == $db['default']['username'] && $_POST['password'] == $db['default']['password'] && $_POST['database'] == $db['default']['database']))
exitOnError('Config file does not match with the given information');
if ($server->select_db($_POST['database']) === FALSE)
exitOnError("ERROR #3: Couldn't connect to database");
$query = file_get_contents('database_tables.sql');
if ($server->multi_query($query) === FALSE)
exitOnError("ERROR #4: Couldn't create the tables");
}else {
if (!file_exists('../application/config/database.php'))
createDbConfigFile($_POST['hostname'], $_POST['username'], $_POST['password'], 'phpback');
if ($server->select_db('phpback') === TRUE)
exitOnError("ERROR #5: You already have a phpback database, please create another manually");
if (!$server->query("CREATE DATABASE IF NOT EXISTS phpback CHARACTER SET utf8 COLLATE utf8_general_ci")) {
exitOnError("ERROR #6: Could not create database");
}
if ($server->select_db('phpback') === FALSE)
exitOnError("ERROR #5: Generated database connection error");
$sql = file_get_contents('database_tables.sql');
if ($server->multi_query($sql) === FALSE)
exitOnError("ERROR #4: Couldn't create the tables");
}
do {
if ($r = $server->store_result())
$r->free();
}while ($server->more_results() && $server->next_result());
$result = $server->query("SELECT id FROM settings WHERE name='title'");
if ($result->num_rows == 1) {
if (!@chmod('../install', 0777)) {
echo "PLEASE DELETE install/ FOLDER MANUALLY. THEN GO TO yourwebsite.com/feedback/admin/ TO LOG IN.";
exit;
}
//In case of success (by using previously set parameters), delete the content of installation folder
unlink('index.php');
unlink('install1.php');
unlink('database_tables.sql');
unlink('index2.php');
unlink('install2.php');
header('Location: ../admin');
exit;
} else {
$server->query("INSERT INTO users(id,name,email,pass,votes,isadmin,banned) VALUES('','" . $_POST['adminname'] . "','" . $_POST['adminemail'] . "','" . hashPassword($_POST['adminpass']) . "', 20, 3,0)");
if (!@chmod('../install', 0777)) {
$url = getBaseUrl();
displayMessage("PLEASE DELETE install/index.php, install/install1.php AND install/database_tables.sql FILES MANUALLY.<br />
THEN GO TO <a href='" . $url . "/install/index2.php'>yourwebsite.com/feedback/install/index2.php</a> TO CONTINUE THE INSTALLATION.");
exit;
}
//In case of success, delete the installation files of the first step
unlink('index.php');
unlink('install1.php');
unlink('database_tables.sql');
header('Location: index2.php');
}