-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.php
148 lines (133 loc) · 4.94 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
<h1>Uni-API</h1>
<?php
require_once('config.php');
require_once('core/data_type.php');
$error_flag = FALSE;
$link = mysqli_connect($host, $user, $pass);
$models = file_get_contents('models.json');
$encoded_models = json_decode($models, true);
if(!$link) {
die('Connection failed: ' . $link->connect_error);
}
echo('<ul>');
// create DB
$sql = 'CREATE DATABASE IF NOT EXISTS `' . $dbname .'`';
if($link->query($sql) === TRUE) {
echo("<li>Database <b>$dbname</b> created successfully!</li>");
} else {
echo('Error creating database: ' . $link -> error);
echo($sql);
$error_flag = TRUE;
return;
}
$sql = "";
// create TABLES
foreach ($encoded_models['models'] as $model_name => $value) {
echo("Creating $model_name ...");
$sql .= " CREATE TABLE IF NOT EXISTS `$dbname`.`$model_name` ( ";
foreach($value as $data => $data_type) {
$sql .= "`$data`" . ($MYSQL_DATA_TYPE[$data_type["type"]]) . (isset($data_type["length"]) ? "(".$data_type["length"].")" : "") . " NOT NULL, ";
}
$sql .= " `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`update_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )
ENGINE = InnoDB AUTO_INCREMENT = 1;";
if($link->query($sql) === TRUE) {
echo("<li>Table <b>$model_name</b> created successfully" . '</li>');
$sql = "";
} else {
echo("Error creating table <b>$model_name</b>: " . $link -> error);
echo $sql;
$sql = "";
echo('<br>');
$error_flag = TRUE;
}
}
// create USER TABLE
$sql = "CREATE TABLE IF NOT EXISTS `$dbname`.`user` (
`username` VARCHAR(32) NOT NULL,
`password` VARCHAR(60) NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`token` VARCHAR(32),
`token_expiring_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
ENGINE = InnoDB AUTO_INCREMENT = 1;";
if($link->query($sql) === TRUE) {
echo("<li>Table <b>user</b> created successfully" . '</li>');
$sql = "";
} else {
echo("Error creating table <b>user</b>: " . $link -> error);
$sql = "";
echo('<br>');
$error_flag = TRUE;
}
// create RELATIONS
foreach ($encoded_models['relations'] as $table => $relations) {
if($relations['hasOne'] != []) {
foreach ($relations['hasOne'] as $table_to_relate) {
$sql = "ALTER TABLE `$dbname`.`$table` ADD `". $table_to_relate ."_id` INT";
if($link->query($sql) === TRUE) {
echo("Field <b>" . $table_to_relate."_id</b> added");
echo('<br>');
$sql = "ALTER TABLE `$dbname`.`$table` ADD FOREIGN KEY (" . $table_to_relate . "_id) REFERENCES $dbname.$table_to_relate(id) ON UPDATE CASCADE ON DELETE SET NULL";
if($link->query($sql) === TRUE) {
echo("<li>Relation between <b>$table</b> and <b>$table_to_relate</b> created successfully".'</li>');
echo('<br>');
}
else {
echo("Error creating foreign key: " . $link -> error);
echo('<br>');
}
} else {
echo("Error creating relations between <b>$table</b> and <b>$table_to_relate</b> : " . $link -> error);
$sql = "";
echo('<br>');
$error_flag = TRUE;
}
}
}
if($relations['hasMany'] != []) {
foreach ($relations['hasMany'] as $table_to_relate) {
$sql = "CREATE TABLE IF NOT EXISTS `$dbname`.`$table". '_' . "$table_to_relate` (
`$table".'_id`'." INT,
`$table_to_relate".'_id`'." INT,
FOREIGN KEY (" . $table . "_id) REFERENCES $dbname.$table(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (" . $table_to_relate . "_id) REFERENCES $dbname.$table_to_relate(id) ON UPDATE CASCADE ON DELETE CASCADE
)
ENGINE = InnoDB;";
if($link->query($sql) === TRUE) {
echo("<li>Table <b>$table _ $table_to_relate</b> created successfully" . '</li>');
$sql = "";
} else {
echo("Error creating table <b>user</b>: " . $link -> error);
echo($sql);
$sql = "";
echo('<br>');
$error_flag = TRUE;
}
echo($sql);
}
}
}
// create TRIGGERS (MAYBE ARE NOT NECESSARY)
// create MAIN USER
$sql = "INSERT INTO `$dbname`.`user` (`username`, `password`)
SELECT * FROM (SELECT 'admin', '" . password_hash("admin", PASSWORD_BCRYPT) . "') AS tmp
WHERE NOT EXISTS (
SELECT username FROM `$dbname`.`user` WHERE `username` = 'admin'
) LIMIT 1;";
if($link->query($sql) === TRUE) {
echo("<li><b>Main user</b> created successfully: <li>username: <i>admin</i></li><li>password: <i>admin</i></li>" . '</li>');
$sql = "";
} else {
echo("Error creating <b>user</b>: " . $link -> error);
$sql = "";
echo('<br>');
$error_flag = TRUE;
}
echo('</ul>');
if($error_flag) {
echo("<br><br><i><b>An error occurred!</b></i>");
} else {
echo("<br><br><i><b>All Done!</b></i>");
}
$link -> close();