-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.php
321 lines (237 loc) · 9.13 KB
/
install.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
require_once('default.inc.php');
$config_file = 'config.php';
$data = array();
$estimated_httpd_path = 'http://' . $_SERVER['HTTP_HOST'] .
substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '/')) . '/';
$http_path = d($_REQUEST['http_path'], $estimated_httpd_path);
$db_host = d($_REQUEST['db_host'], 'localhost');
$db_username = $_REQUEST['db_username'];
$db_password = $_REQUEST['db_password'];
$db_name = d($_REQUEST['db_name'], 'shortur');
$db_exists = $_REQUEST['db_exists'];
$admin_email = $_REQUEST['admin_email'];
$admin_password = $_REQUEST['admin_password'];
$base_url = d($_REQUEST['base_url'], 'http://' . $_SERVER['HTTP_HOST'] . '/');
$external_404_page = $_REQUEST['external_404_page'];
$email_address = $_REQUEST['email_address'];
// can this user write to the config file?
if (!is_writeable($config_file)) {
$data['errors'][] =
"The configuration file, $config_file, is not writeable by the web server user ' " .
`whoami` . " '.";
$data['errors'][] = "The installer cannot continue.";
template($data, false);
exit;
}
// is mod_rewrite enabled?
if (!check_mod_rewrite()) {
$data['errors'][] = "mod_rewrite is not enabled on this server.";
$data['errors'][] = "The installer cannot continue.";
template($data, false);
exit;
}
if ($_REQUEST['submit']) {
if (!$http_path)
$data['errors'][] = "You must specify a valid installation path";
if (!$db_host)
$data['errors'][] = "You must specify a database host.";
if (!$db_username)
$data['errors'][] = "You must specify a database user name.";
if (!$db_password)
$data['errors'][] = "You must specify a database password.";
if (!$admin_email)
$data['errors'][] = "You must enter an email address for the admin user.";
if (!$admin_password)
$data['errors'][] = "You must specify an admin password.";
if (!$base_url)
$data['errors'][] = "You must specify a base URL.";
if (!$email_address)
$data['errors'][] = "You must specify an email address.";
if ($db_host && $db_username && $db_password && $db_name && $admin_password) {
if (!($db = @mysql_connect($db_host, $db_username, $db_password))) {
$data['errors'][] = "Cannot connect to the database with the credentials you supplied.";
} else if ($db_exists && !mysql_select_db($db_name)) {
$data['errors'][] = "The database '$_REQUEST[db_name]' does not exist on $_REQUEST[db_host].";
} else if (!$db_exists) {
$sql = "create database " . $db_name;
$data['errors'][] = "The database credentials you provided do not have sufficient " .
"permissions to create the database. Try creating the database before running this " .
"installer.";
}
}
if (!$data['errors']) {
write_settings($http_path, $db_host, $db_username, $db_password, $db_name, $base_url, $email_address);
mysql_select_db($db_name);
$create_table_entries_sql ="CREATE TABLE `entries` (" .
"`id` int(11) NOT NULL auto_increment," .
"`user_id` int(11) NOT NULL default '0'," .
"`target` text NOT NULL," .
"`short_url` text NOT NULL," .
"PRIMARY KEY (`id`))";
$create_table_users_sql = "CREATE TABLE `users` ( " .
"`id` int(11) NOT NULL auto_increment," .
"`username` text NOT NULL," .
"`email` text NOT NULL," .
"`password` text NOT NULL," .
"`admin` tinyint(1) NOT NULL," .
"PRIMARY KEY (`id`))";
$create_admin_user_sql = "insert into `users` (username, email, password, admin) values " .
"('admin', '" mysql_real_escape_string($admin_email) . "', '" . md5($admin_password) . "', 1)";
q($create_table_entries_sql);
q($create_table_users_sql);
q($create_admin_user_sql);
$data['messages'][] = <<<EOF
Your ShortUr installation is complete! Now, you should:
<ul>
<li>Change your config.php file permissions to be non-writeable</li>
<li>Delete install.php</li>
</ul>
If you previously used <a href="http://get-shorty.com/">Shorty</a>, you can easily
migrate your data from Shorty to ShortUr using the <a href='migrate_from_shorty.php'>
Shorty Migration Tool.
EOF;
template($data, $false);
exit;
}
}
$data['content'] =<<<EOF
<form action='install.php' method='post'>
<div class='table'>
<div class='table_header'>
Install ShortUr
</div>
<div class='line_item'>
<b>Installation Path: </b>
<input type='text' name='http_path' value='$http_path' />
<span class='explaination'>The web-accessible path where ShortUr will be installed. Include the trailing slash.</span>
</div>
<div class='line_item_alt'>
<b>Database Host: </b>
<input type='text' name='db_host' value='$db_host'/>
</div>
<div class='line_item'>
<b>Database Username: </b>
<input type='text' name='db_username' value='$db_username'/>
</div>
<div class='line_item_alt'>
<b>Database Password: </b>
<input type='password' name='db_password' />
</div>
<div class='line_item'>
<b>Database Name: </b>
<input type='text' name='db_name' value='$db_name' /><br/>
<input type='checkbox' name='db_exists' value='1'> This database has already been created.
<span class='explaination'>If the database credentials for your ShortUr installation don't have permission enough to create a databse, create the database using the command line or a tool such as phpmyadmin first, then run this install and check this box.</span>
</div>
<div class='line_item_alt'>
<b>Base URL: </b>
<input type='text' name='base_url' value='$base_url' />
<span class='explaination'>The base URL for target URLs.</span>
</div>
<div class='line_item'>
<b>Admin Password:</b>
<input type='password' name='admin_email' />
<span class='explaination'>The email address for the initial admin user.</span>
</div>
<div class='line_item'>
<b>Admin Password:</b>
<input type='password' name='admin_password' />
<span class='explaination'>The password for the initial admin user.</span>
</div>
<div class='line_item_alt'>
<b>External 404 Page:</b>
<input type='text' name='external_404_page' value='$external_404_page' />
<span class='explaination'>If left blank, ShortUr will generate a generic "Page Not Found" error.</span>
</div>
<div class='line_item'>
<b>Email Address:</b>
<input type='text' name='email_address' value='$email_address' />
<span class='explaination'>Password reset emails will come from this email address.</span>
</div>
<div class='line_item_alt' style='text-align: center;'>
<input type='submit' name='submit' value='Install ShortUr'>
</div>
</div>
</form>
EOF;
template($data, false);
function write_settings($in_http_path=null, $in_db_host=null, $in_db_username=null,
$in_db_password=null, $in_db_name=null, $in_base_url=null, $in_external_404_page=null, $email=null) {
global $config_file, $http_path, $db_host, $db_username, $db_password, $base_url, $cookie_name;
$output =<<<EOF
<?php
\$http_path = '$in_http_path';
\$db_host = '$in_db_host';
\$db_username = '$in_db_username';
\$db_password = '$in_db_password';
\$db_name = '$in_db_name';
\$base_url = '$in_base_url';
\$external_404_page = '$in_external_404_page';
\$cookie_name = 'shortur_auth';
\$per_page = 20;
\$show_pages = 2;
\$from_email = '$email';
?>
EOF;
$f = fopen($config_file, 'w');
fwrite($f, $output);
fclose($f);
$http_path = $in_http_path;
$db_host = $in_db_host;
$db_username = $in_db_username;
$db_password = $in_db_password;
$base_url = $in_base_url;
$cookie_name = $in_cookie_name;
}
function check_mod_rewrite() {
global $estimated_httpd_path;
if (preg_match('/enabled/', wget($estimated_httpd_path . 'test_mod_rewrite')))
return true;
else
return false;
}
function wget($url) {
// strip the http:// off the front and trim the url
$url = trim(preg_replace("/^http:\/\//", "", $url));
// split the URL on the first instance of a slash, if it exists
$host = $path = "";
if (strpos($url, '/')) {
$host = substr($url, 0, strpos($url, '/'));
$path = substr($url, strpos($url, '/'));
} else {
$host = $url;
$path = '/';
}
$socket = @fsockopen($host, 80, $errorNumber, $errorString);
if (!$socket) {
return false;
}
$header = "GET ".$path." HTTP/1.1\r\n";
$header.= "Host: ".$host."\r\n";
$header.= "Connection: close\r\n\r\n";
fwrite($socket, $header);
$response_header = '';
$response_content = '';
do {
$response_header.= fread($socket, 1);
} while (!preg_match('/\\r\\n\\r\\n$/', $response_header));
if (!strstr($response_header, "Transfer-Encoding: chunked")) {
while (!feof($socket)) {
$response_content.= fgets($socket, 128);
}
} else {
while ($chunk_length = hexdec(fgets($socket))) {
$response_content_chunk = '';
$read_length = 0;
while ($read_length < $chunk_length) {
$response_content_chunk .= fread($socket, $chunk_length - $read_length);
$read_length = strlen($response_content_chunk);
}
$response_content.= $response_content_chunk;
fgets($socket);
}
}
return chop($response_content);
}
?>