Skip to content
This repository was archived by the owner on Mar 28, 2019. It is now read-only.

Commit 16091c4

Browse files
committed
Merge pull request #31 from devenbhooshan/master
create database config file only if connected to db
2 parents 900b84f + 0a75df5 commit 16091c4

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

install.php

+37-33
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@
88
*/
99
require_once('functions.php');
1010
if(isset($_POST['host'])) {
11-
// create file 'dbinfo.php'
12-
$fp = fopen('dbinfo.php','w');
13-
$l1 = '$host="'.$_POST['host'].'";';
14-
$l2 = '$user="'.$_POST['username'].'";';
15-
$l3 = '$password="'.$_POST['password'].'";';
16-
$l4 = '$database="'.$_POST['name'].'";';
17-
$l5 = '$compilerhost="'.$_POST['chost'].'";';
18-
$l6 = '$compilerport='.$_POST['cport'].';';
19-
fwrite($fp, "<?php\n$l1\n$l2\n$l3\n$l4\n$l5\n$l6\n?>");
20-
fclose($fp);
21-
include('dbinfo.php');
2211
// connect to the MySQL server
23-
mysql_connect($host,$user,$password);
24-
// create the database
25-
mysql_query("CREATE DATABASE $database");
26-
mysql_select_db($database) or die('Error connecting to database.');
27-
// create the preferences table
28-
mysql_query("CREATE TABLE IF NOT EXISTS `prefs` (
12+
$link=mysql_connect($_POST['host'],$_POST['username'],$_POST['password']);
13+
if (!$link) {
14+
die('Could not connect');
15+
} else {
16+
// create file 'dbinfo.php'
17+
$fp = fopen('dbinfo.php','w');
18+
$l1 = '$host="'.$_POST['host'].'";';
19+
$l2 = '$user="'.$_POST['username'].'";';
20+
$l3 = '$password="'.$_POST['password'].'";';
21+
$l4 = '$database="'.$_POST['name'].'";';
22+
$l5 = '$compilerhost="'.$_POST['chost'].'";';
23+
$l6 = '$compilerport='.$_POST['cport'].';';
24+
fwrite($fp, "<?php\n$l1\n$l2\n$l3\n$l4\n$l5\n$l6\n?>");
25+
fclose($fp);
26+
include('dbinfo.php');
27+
// create the database
28+
mysql_query("CREATE DATABASE $database");
29+
mysql_select_db($database) or die('Error connecting to database.');
30+
// create the preferences table
31+
mysql_query("CREATE TABLE IF NOT EXISTS `prefs` (
2932
`name` varchar(30) NOT NULL,
3033
`start` int(11) NOT NULL,
3134
`end` int(11) NOT NULL,
@@ -35,11 +38,11 @@
3538
`python` int(11) NOT NULL,
3639
`formula` text NOT NULL
3740
)");
38-
// fill it with default preferences
39-
mysql_query("INSERT INTO `prefs` (`name`, `start`, 'end', `c`, `cpp`, `java`, `python`, `forumla`) VALUES
41+
// fill it with default preferences
42+
mysql_query("INSERT INTO `prefs` (`name`, `start`, 'end', `c`, `cpp`, `java`, `python`, `forumla`) VALUES
4043
('Codejudge', 01/01/70 00:00:00, 01/01/70 00:00:00, 1, 1, 1,'\$score = \$points / \$attempts')");
41-
// create the problems table
42-
mysql_query("CREATE TABLE IF NOT EXISTS `problems` (
44+
// create the problems table
45+
mysql_query("CREATE TABLE IF NOT EXISTS `problems` (
4346
`sl` int(11) NOT NULL AUTO_INCREMENT,
4447
`name` varchar(200) NOT NULL,
4548
`text` text NOT NULL,
@@ -49,8 +52,8 @@
4952
`points` int(11) NOT NULL,
5053
PRIMARY KEY (`sl`)
5154
)");
52-
// create the solve table
53-
mysql_query("CREATE TABLE IF NOT EXISTS `solve` (
55+
// create the solve table
56+
mysql_query("CREATE TABLE IF NOT EXISTS `solve` (
5457
`sl` int(11) NOT NULL AUTO_INCREMENT,
5558
`problem_id` int(11) NOT NULL,
5659
`username` varchar(25) NOT NULL,
@@ -62,8 +65,8 @@
6265
`time` bigint(20) NOT NULL,
6366
PRIMARY KEY (`sl`)
6467
)");
65-
// create the users table
66-
mysql_query("CREATE TABLE IF NOT EXISTS `users` (
68+
// create the users table
69+
mysql_query("CREATE TABLE IF NOT EXISTS `users` (
6770
`sl` int(11) NOT NULL AUTO_INCREMENT,
6871
`username` varchar(25) NOT NULL,
6972
`salt` varchar(6) NOT NULL,
@@ -73,14 +76,15 @@
7376
`score` float NOT NULL,
7477
PRIMARY KEY (`sl`)
7578
)");
76-
// create the user 'admin' with password 'admin'
77-
$salt=randomAlphaNum(5);
78-
$pass="admin";
79-
$hash=crypt($pass,$salt);
80-
$sql="INSERT INTO `users` ( `username` , `salt` , `hash` , `email` ) VALUES ('$pass', '$salt', '$hash', '".$_POST['email']."')";
81-
mysql_query($sql);
82-
header("Location: install.php?installed=1");
83-
}
79+
// create the user 'admin' with password 'admin'
80+
$salt=randomAlphaNum(5);
81+
$pass="admin";
82+
$hash=crypt($pass,$salt);
83+
$sql="INSERT INTO `users` ( `username` , `salt` , `hash` , `email` ) VALUES ('$pass', '$salt', '$hash', '".$_POST['email']."')";
84+
mysql_query($sql);
85+
header("Location: install.php?installed=1");
86+
}
87+
}
8488
?>
8589
<!DOCTYPE html>
8690
<html lang="en"><head>

0 commit comments

Comments
 (0)