-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.php
260 lines (210 loc) · 6.55 KB
/
function.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
<?php
/**
* Short description for function.php
*
* Copyright (C) 2013 KuoE0 <[email protected]>
*
* Distributed under terms of the MIT license.
*
* @package function
* @author KuoE0 <[email protected]>
* @version 0.1
* @copyright (C) 2013 KuoE0 <[email protected]>
*/
function getURL() {
$protocol = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://';
$port = $_SERVER['SERVER_PORT'] == '80' ? '' : $_SERVER['SERVER_PORT'];
return $protocol . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}
function is_setup($db) {
if (get_attr($db, 'setup') == '') {
return false;
}
return true;
}
function cleanup_db($db) {
try {
$db->exec("DELETE FROM `attributes`");
$db->exec("DELETE FROM `presenters`");
$db->exec("DELETE FROM `groups`");
$db->exec("DELETE FROM `timeslots`");
$db->exec("DELETE FROM `presentations`");
}
catch (Exception $e) {
echo $e->getMessage() . '\n';
}
}
function get_attr($db, $attr) {
try {
$sql = "SELECT `value` FROM `attributes` WHERE `attr` = :attr";
$stmt = $db->prepare($sql);
$stmt->execute(array(':attr' => $attr));
$data_row = $stmt->fetch();
return $data_row ? $data_row['value'] : '';
}
catch (Exception $e) {
echo $e->getMessage();
}
return '';
}
function add_presentation($db, $title, $time_id, $group_id, $passwd) {
try {
$sql = "INSERT INTO `presentations` (`title`, `group_id`, `time_id`, `reg_time`) VALUES (:title, :group_id, :time_id, datetime('now'))";
$stmt = $db->prepare($sql);
$stmt->execute(array(':title' => $title, ':group_id' => $group_id, ':time_id' => $time_id));
$sql = "UPDATE `timeslots` SET `occupied` = '1' WHERE `time_id` = :time_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(':time_id' => $time_id));
$sql = "UPDATE `groups` SET `registered` = '1', `password` = :password WHERE `group_id` = :group_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(':group_id' => $group_id, ':password' => sha1($passwd . $group_id)));
}
catch (Exception $e) {
echo $e->getMessage();
return false;
}
return true;
}
function delete_presentation($db, $presentation_id) {
try {
$sql = "SELECT * FROM `presentations` WHERE `id` = :presentation_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(":presentation_id" => $presentation_id));
$data_row = $stmt->fetch();
$group_id = $data_row['group_id'];
$time_id = $data_row['time_id'];
$sql = "DELETE FROM `presentations` WHERE `id` = :presentation_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(":presentation_id" => $presentation_id));
$sql = "UPDATE `groups` SET `registered` = '0', `password` = '' WHERE `group_id` = :group_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(":group_id" => $group_id));
$sql = "UPDATE `timeslots` SET `occupied` = '0' WHERE `time_id` = :time_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(":time_id" => $time_id));
}
catch (Exception $e) {
echo $e->getMessage();
return false;
}
return true;
}
function password_check($db, $group_id, $passwd) {
try {
$hash_passwd = sha1($passwd . $group_id);
$sql = "SELECT `password` FROM `groups` WHERE `group_id` = :group_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(':group_id' => $group_id));
$data_row = $stmt->fetch();
if ($data_row['password'] == $hash_passwd) {
return true;
}
}
catch (Exception $e) {
echo $e->getMessage();
}
return false;
}
function add_timeslot_by_rule($db, $rule) {
// calculate interval
$interval = 'P1D';
if ($rule['repeat_mode'] == 'weekly') {
$interval = 'P7D';
}
else if ($rule['repeat_mode'] == 'monthly') {
$interval = 'P1M';
}
else if ($rule['repeat_mode'] == 'none') {
$rule['end_date'] = clone $rule['start_date'];
}
try {
$sql = "INSERT INTO `timeslots` (`date`, `start_time`, `end_time`, `time_order`, `occupied`) VALUES (:date, :start_time, :end_time, :time_order, 0)";
$stmt = $db->prepare($sql);
for ($itr_date = clone $rule['start_date']; $itr_date <= $rule['end_date']; $itr_date->add(new DateInterval($interval))) {
for ($j = 0; $j < $rule['quota']; ++$j) {
$stmt->execute(array(':date' => $itr_date->format('Y-m-d'), ':start_time' => $rule['start_time']->format('H:i'), ':end_time' => $rule['end_time']->format('H:i'), ':time_order' => ($j + 1)));
}
}
}
catch (Exception $e) {
echo $e->getMessage();
return false;
}
return true;
}
function add_new_group($db, $group_name) {
$insert_group_sql = "INSERT INTO `groups` (`group_name`, `registered`) VALUES (:group_name, 0)";
try {
$stmt = $db->prepare($insert_group_sql);
$stmt->execute(array(':group_name' => $group_name));
}
catch (Exception $e) {
echo $e->getMessage();
}
}
function get_group_id_by_name($db, $group_name) {
$select_group_sql = "SELECT `group_id` FROM `groups` WHERE `group_name` = :group_name";
try {
$stmt = $db->prepare($select_group_sql);
$stmt->execute(array(':group_name' => $group_name));
$data_row = $stmt->fetch();
}
catch (Exception $e) {
echo $e->getMessage();
}
if (!$data_row) {
add_new_group($db, $group_name);
return get_group_id_by_name($db, $group_name);
}
return $data_row['group_id'];
}
function get_title($db) {
$sql = "SELECT `value` FROM `attributes` WHERE `attr` = 'title'";
try {
$stmt = $db->prepare($sql);
$stmt->execute();
$data_row = $stmt->fetch();
if ($data_row) {
return $data_row['value'];
}
}
catch (Exception $e) {
echo $e->getMessage();
}
return '';
}
function get_members_by_group_id($db, $group_id) {
$sql = "SELECT `name` FROM `presenters` WHERE `group_id` = :group_id ORDER BY `presenter_id` ASC";
$stmt = $db->prepare($sql);
$stmt->execute(array(':group_id' => $group_id));
$ret = array();
while (($data_row = $stmt->fetch()) != FALSE) {
array_push($ret, $data_row['name']);
}
return $ret;
}
function get_presentation_info_by_time_id($db, $time_id) {
try {
$sql = "SELECT * FROM `presentations` WHERE `time_id` = :time_id";
$stmt = $db->prepare($sql);
$stmt->execute(array(':time_id' => $time_id));
$data_row = $stmt->fetch();
if ($data_row)
return $data_row;
}
catch (Exception $e) {
echo $e->getMessage();
}
return '';
}
function get_quota($db, $date, $begin_time, $end_time) {
$sql = "SELECT COUNT(*) FROM `timeslots` WHERE `date` = :date AND `begin_time` = :begin_time AND `end_time` = :end_time";
$stmt = $db->prepare($sql);
$stmt->execute(array(':date' => $date, ':begin_time' => $begin_time, ':end_time' => $end_time));
return $stmt->fetch()['COUNT(*)'];
}
function gen_random_password($len=8) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return substr(str_shuffle($chars), 0, $len);
}
?>