-
Notifications
You must be signed in to change notification settings - Fork 0
/
dboperator.php
155 lines (144 loc) · 5.42 KB
/
dboperator.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
<?php
/*
* Class to manipulate databases
*/
Class DBOperator Extends Bulletaeon {
function add()
{
global $wpdb, $reqdata;
$sql = "INSERT INTO " . WP_BTAEON_TABLE . " SET
msg_title='" . $reqdata['msg_title'] . "',
msg_owner='" . $reqdata['msg_owner'] . "',
msg_category='" . $reqdata['msg_category'] . "',
msg_content='" . $reqdata['msg_content'] . "',
msg_link='" . $reqdata['msg_link'] . "',
msg_file='" . $reqdata['msg_file'] . "',
msg_time='" . $reqdata['msg_time'] . "'";
$wpdb->get_results($sql);
$sql = "SELECT msg_id FROM " . WP_BTAEON_TABLE . " WHERE
msg_title='" . $reqdata['msg_title'] . "' AND
msg_owner='" . $reqdata['msg_owner'] . "' AND
msg_category='" . $reqdata['msg_category'] . "' AND
msg_content='" . $reqdata['msg_content'] . "' AND
msg_link='" . $reqdata['msg_link'] . "' AND
msg_file='" . $reqdata['msg_file'] . "' AND
msg_time='" . $reqdata['msg_time'] . "' LIMIT 1";
$result = $wpdb->get_results($sql);
if ( empty($result) || empty($result[0]->msg_id) )
{
echo '<div class="error"><p>我找在資料庫中不到您剛剛送出的資料,資料庫可能出問題了</p></div>';
return false;
}
return true;
}
function edit_save()
{
global $wpdb, $reqdata;
$sql = "UPDATE " . WP_BTAEON_TABLE . " SET
msg_title='" . $reqdata['msg_title'] . "',
msg_owner='" . $reqdata['msg_owner'] . "',
msg_category='" . $reqdata['msg_category'] . "',
msg_content='" . $reqdata['msg_content'] . "',
msg_link='" . $reqdata['msg_link'] . "',
msg_time='" . $reqdata['msg_time'] . "',
msg_file='" . $reqdata['msg_file'] . "' WHERE msg_id='" . $reqdata['msg_id'] . "'";
$wpdb->get_results($sql);
$sql = "SELECT msg_id FROM " . WP_BTAEON_TABLE . " WHERE
msg_title='" . $reqdata['msg_title'] . "' AND
msg_owner='" . $reqdata['msg_owner'] . "' AND
msg_category='" . $reqdata['msg_category'] . "' AND
msg_content='" . $reqdata['msg_content'] . "' AND
msg_link='" . $reqdata['msg_link'] . "' AND
msg_time='" . $reqdata['msg_time'] . "' AND
msg_file='" . $reqdata['msg_file'] . "' LIMIT 1";
$result = $wpdb->get_results($sql);
if ( empty($result) || empty($result[0]->msg_id) )
{
echo '<div class="error"><p>我找在資料庫中不到您剛剛送出的資料,資料庫可能出問題了</p></div>';
return false;
}
return true;
}
function delete($msg_id)
{
wp_get_current_user();
global $wpdb, $current_user;
$sql = "SELECT * FROM " . WP_BTAEON_TABLE . " WHERE msg_id='" . $msg_id . "';";
$result = $wpdb->get_results($sql);
// Is current user a power user or owner of this message?
if ( $current_user->user_login == $result[0]->msg_owner || $current_user->user_level >= 8 )
{
if ( empty($result) )
{
echo '<div class="error"><p><strong>錯誤:</strong>您所指定的公告不存在</p></div>';
Renderer::js_redirect(4000);
return false;
} else {
$sql = "DELETE FROM " . WP_BTAEON_TABLE . " WHERE msg_id='" . $msg_id . "';";
//echo '<div class="updated"><p>' . $sql . '</p></div>';
$wpdb->query($sql);
$sql = "SELECT * FROM " . WP_BTAEON_TABLE . " WHERE msg_id='" . $msg_id . "';";
$result = $wpdb->get_results($sql);
if ( !empty($result) || !empty($result[0]->msg_id) )
{
echo '<div class="error"><p><strong>錯誤:</strong>儘管已經發出刪除命令,該公告在資料庫中依然存在。</p></div>';
Renderer::js_redirect(4000);
return false;
}
}
} else {
echo '<div class="error"><p><strong>錯誤:</strong>您沒有權限刪除此公告</p></div>';
Renderer::js_redirect(4000);
return false;
}
return true;
}
/*
* Get message by ID
*/
function get_msg_by_id($msg_id)
{
global $wpdb;
$sql = "SELECT * FROM " . WP_BTAEON_TABLE . " WHERE
msg_id='" . intval($msg_id) . "';";
$result = $wpdb->get_results($sql);
if ( $result ) {
return $result[0];
} else {
return false;
}
}
/*
* Get new messages and sticky messages for displaying on front page
* @max: Maximium number of total messages to get
* @cat: Category to search in
* @include_sticky: Whether to include sticky messages
* return: Array of message objects, empty if no result
*/
function get_newmsg($max, $cat='all', $include_sticky=false )
{
global $wpdb;
if ( !is_int($max) ) $max = 10;
if ( $cat > 0 && $include_sticky == false )
{
$sql = "SELECT msg_id, msg_time, msg_title FROM " . WP_BTAEON_TABLE . " WHERE msg_category='$cat' ORDER BY msg_time DESC LIMIT $max";
$rows = $wpdb->get_results($sql);
} elseif ( $cat == 'all' && $include_sticky == false ) {
$sql = "SELECT msg_id, msg_time, msg_title FROM " . WP_BTAEON_TABLE . " ORDER BY msg_time DESC LIMIT $max";
$rows = $wpdb->get_results($sql);
} elseif ( $cat > 0 && $include_sticky == true ) {
$sql = "(SELECT sticky, msg_id, msg_time, msg_owner, msg_title FROM " . WP_BTAEON_TABLE . " WHERE sticky=1 AND msg_category='$cat')
UNION
(SELECT sticky, msg_id, msg_time, msg_owner, msg_title FROM " . WP_BTAEON_TABLE . " WHERE msg_category='$cat')
ORDER BY sticky DESC, msg_time DESC LIMIT $max";
$rows = $wpdb->get_results($sql);
} elseif ( $cat == 'all' && $include_sticky == true ) {
$sql = "(SELECT sticky, msg_id, msg_owner, msg_title FROM " . WP_BTAEON_TABLE . " WHERE sticky=1)
UNION
(SELECT msg_id, msg_time, msg_title FROM " . WP_BTAEON_TABLE . ") ORDER BY sticky, msg_time DESC LIMIT $max";
$rows = $wpdb->get_results($sql);
}
return $rows;
}
}
?>