-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.php
233 lines (191 loc) · 5.69 KB
/
sql.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
<?php
$link = connect_mysql();
function connect_mysql() {
$host = "127.0.0.1";
$password = "root";
$user = "root";
$db = "pufulist";
$link = mysqli_connect($host, $user, $password, $db);
if (mysqli_connect_errno()) {
die(mysqli_connect_error());
}
return $link;
}
function get_link() {
global $link;
return $link;
}
function escape($string) {
return mysqli_real_escape_string(get_link(), $string);
}
function get_items($user_id, $parent = 0) {
$sql = "SELECT * FROM items WHERE user_id = $user_id AND parent = $parent";
return assoc_items(query($sql));
}
function assoc_items($result) {
$array = array();
while ($row = mysqli_fetch_assoc($result)) {
if($row['type'] == "task")
$array[] = assoc_items_task($row);
elseif ($row['type'] == "serial")
$array[] = assoc_items_serial($row);
else
$array[] = $row;
}
return $array;
}
function assoc_once($result) {
return mysqli_fetch_assoc($result);
}
function assoc_items_task($task) {
$task_id = $task['id'];
$result = get_item_task($task_id);
$task['checked'] = $result['checked'];
return $task;
}
function assoc_items_serial($serial) {
$task_id = $serial['id'];
$result = get_item_serial($task_id);
$serial['current'] = $result['current'];
$serial['last'] = $result['last'];
return $serial;
}
function add_serial($name, $type, $parent, $episodes, $user_id) {
$hash = md5("web" . $name . $type . $parent . time());
$sql = "INSERT INTO items(hash, name, type, parent, user_id) VALUES('$hash', '$name', '$type', $parent, $user_id)";
query($sql);
$new_item = get_item_hash($hash);
$item_id = $new_item['id'];
$sql = "INSERT INTO items_serials(item_id, last) VALUES ($item_id, $episodes)";
query($sql);
}
function add_item($name, $type, $parent, $user_id) {
$hash = md5("web" . $name . $type . $parent . time());
$sql = "INSERT INTO items(hash, name, type, parent, user_id) VALUES ('$hash', '$name', '$type', $parent, $user_id)";
query($sql);
$new_item = get_item_hash($hash);
switch($new_item['type']) {
case "task":
add_item_task($new_item);
break;
case "group":
add_item_group($new_item);
break;
}
}
function add_item_task($item) {
$item_id = $item['id'];
$sql = "INSERT INTO items_tasks(item_id) VALUES ($item_id)";
query($sql);
}
function add_item_group($item) {
$item_id = $item['id'];
$sql = "INSERT INTO items_groups(item_id) VALUES ($item_id)";
query($sql);
}
function get_item($item_id) {
$sql = "SELECT * FROM items WHERE id = $item_id";
return assoc_once(query($sql));
}
function get_item_hash($hash) {
$sql = "SELECT * FROM items WHERE hash = '$hash'";
return assoc_once(query($sql));
}
function get_item_task($task_id) {
$sql = "SELECT * FROM items JOIN items_tasks ON item_id = items.id WHERE item_id = $task_id";
return assoc_once(query($sql));
}
function get_item_serial($task_id) {
$sql = "SELECT * FROM items JOIN items_serials ON item_id = items.id WHERE item_id = $task_id";
return assoc_once(query($sql));
}
function set_item_task_check($task) {
$checked = $task['checked'];
$item_id = $task['item_id'];
$sql = "UPDATE items_tasks SET checked = $checked WHERE item_id = $item_id";
query($sql);
}
function set_item_task_name($task) {
$name = $task['name'];
$item_id = $task['item_id'];
$sql = "UPDATE items SET name = '$name' WHERE id = $item_id";
query($sql);
}
function set_item_serial_current($serial) {
$current = $serial['current'];
$item_id = $serial['item_id'];
$sql = "UPDATE items_serials SET current = $current WHERE item_id = $item_id";
query($sql);
}
function update_serial($serial) {
update_item_name($serial);
$last = $serial['last'];
$serial_id = $serial['id'];
$sql = "UPDATE items_serials SET last = $last WHERE id = $serial_id";
query($sql);
}
function update_item_name($item) {
// FIXME: Make this work with 'id'
$item_id = $item['item_id'];
$name = $item['name'];
$sql = "UPDATE items SET name = '$name' WHERE id = $item_id";
query($sql);
}
function get_item_group($group_id) {
$sql = "SELECT * FROM items JOIN items_groups ON item_id = items.id WHERE item_id = $group_id";
return assoc_once(query($sql));
}
function set_item_group_name($group) {
$name = $group['name'];
$item_id = $group['item_id'];
$sql = "UPDATE items SET name = '$name' WHERE id = $item_id";
query($sql);
}
//FIXME: This thing is not fully recursive
function delete_item($item_id) {
$sql = "DELETE FROM items WHERE id = $item_id";
query($sql);
$sql = "DELETE FROM items WHERE parent = $item_id";
query($sql);
$sql = "DELETE FROM items_groups WHERE item_id = $item_id";
query($sql);
$sql = "DELETE FROM items_tasks WHERE item_id = $item_id";
query($sql);
$sql = "DELETE FROM items_serials WHERE item_id = $item_id";
query($sql);
}
function delete_session($token) {
$sql = "DELETE FROM sessions WHERE token = '$token'";
query($sql);
}
function hash_password($username, $password) {
return md5($username.$password);
}
function get_user($username, $password = null) {
$sql = "SELECT * FROM users WHERE user = '$username'";
if ($password) {
$password = hash_password($username, $password);
$sql = $sql . "AND password = '$password'";
}
return assoc_once(query($sql));
}
function add_user($username, $password) {
$password = hash_password($username, $password);
$sql = "INSERT INTO users(user, password) VALUES('$username', '$password')";
query($sql);
}
function get_new_user($username, $password) {
add_user($username, $password);
return get_user($username, $password);
}
function set_session($token, $user_id) {
$sql = "INSERT INTO sessions(token, user_id) VALUES ('$token', $user_id)";
query($sql);
}
function get_session($token) {
$sql = "SELECT * FROM sessions WHERE token = '$token'";
return assoc_once(query($sql));
}
function query($sql) {
return mysqli_query(get_link(), $sql);
}