-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcodes.php
336 lines (318 loc) · 9.52 KB
/
shortcodes.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
// Register shortcode
add_shortcode('btaeon', 'btaeon_shortcode');
/*
* Shortcode syntax:
* [btaeon max=<number of messages to be shown per page> cat=(<category id>|all)]
* Set cat=all to show messages in all categories.
*/
function btaeon_shortcode( $atts )
{
global $wpdb, $wp_query;
extract(shortcode_atts(array(
'max' => 15,
'cat' => 1,
), $atts ));
// Validate
if ( $cat != 'all' && $cat != 'old' )
{
$max = intval($max);
$cat = intval($cat);
}
// What are we doing? displaying table or a single message?
$btp = (empty($_GET['btp'])) ? '' : $wpdb->escape($_GET['btp']);
$mid = (empty($_GET['mid'])) ? '' : $wpdb->escape($_GET['mid']);
if ( !empty($btp) && empty($mid) )
{
// Show the table
$curr_page = intval($wpdb->escape($_GET['btp']));
if ( empty($curr_page) || $curr_page < 1 ) $curr_page = 1;
return shortcode_table( $max, $cat, $curr_page );
} elseif ( empty($btp) && !empty($mid) ) {
// Show single message
$mid = intval($wpdb->escape($_GET['mid']));
if ( empty($mid) || $mid < 1 ) return '<strong>Illegal input</strong>';
return shortcode_single( $mid, $cat);
} else {
return shortcode_table( $max, $cat, 1 );
}
}
function shortcode_table( $max, $cat, $curr_page )
{
global $wpdb, $link;
// Count rows for paging
if ( $cat == 'all' )
{
$numrows = $wpdb->get_var("SELECT COUNT(msg_id) as rows FROM " . WP_BTAEON_TABLE);
} elseif ( COMPAT_MODE == true && $cat == 'old' ) {
if ( function_exists('connect_old_db') )
{
connect_old_db();
// Limit maximum row of data, old messages should not be needed, change when necessary
$result = mysql_query('SELECT pno FROM boards ORDER BY pno DESC LIMIT 500', $link);
$numrows = mysql_num_rows($result);
}
} else {
$numrows = $wpdb->get_var("SELECT COUNT(msg_id) as rows FROM " . WP_BTAEON_TABLE . " WHERE msg_category='$cat'");
}
$numpages = ceil($numrows / $max);
$offset = ($curr_page - 1) * $max;
// Print the links to access each page
$nav = '';
for ( $btpage = 1; $btpage <= $numpages; $btpage++ )
{
// With few pages, print all the links
if ( $numpages < 15 )
{
if ( $btpage == $curr_page ) $nav .= '<span class="nav_page">' . $btpage . '</span>'; // No need to create a link to current page
else $nav .= ' <a href="?btp=' . $btpage . '" class="nav_page">' . $btpage . '</a> ';
} else {
if ( $btpage == $curr_page ) $nav .= '<span class="nav_page">' . $btpage . '</span>'; // No need to create a link to current page
elseif ( $btpage == 1 || $btpage == $numpages ) $nav .= ''; // No need to create first and last (they are created by the first and last links afterwards)
else {
// Print links that are close to the current page (< 10 steps away)
if ( $btpage < ($curr_page + 10) && $btpage > ($curr_page - 10) )
{
$nav .= ' <a href="?btp=' . $btpage . '" class="nav_page">' . $btpage . '</a> ';
}
}
}
}
// print first, last, next, previous links
if ( $curr_page > 1 )
{
$btpage = $curr_page - 1;
if ( $numpages >= 15 )
{
$first = ' <a href="?btp=1" class="nav_page">«首頁</a> ';
} else {
$first = '';
}
} else {
$prev = ' '; // We're on page one, no need to print previous link
if ( $numpages >= 15 )
{
$first = ' ';
} else {
$first = '';
}
}
if ( $curr_page < $numpages )
{
$btpage = $curr_page + 1;
if ( $numpages >= 15 )
{
$last = ' <a href="?btp=' . $numpages . '" class="nav_page">末頁»</a> ';
} else {
$last = '';
}
} else {
$next = ' '; // We're on the last page
if ( $numpages >= 15 )
{
$last = ' ';
} else {
$last = '';
}
}
// Grab from the database
if ( $cat == 'all' )
{
$sql = "SELECT msg_id, msg_time, msg_owner, msg_title FROM " . WP_BTAEON_TABLE . " ORDER BY msg_time DESC LIMIT $offset, $max";
$rows = $wpdb->get_results($sql);
} elseif ( COMPAT_MODE == true && $cat == 'old' ) {
// INNER JOIN `users` to select Chinese usernames
$result = mysql_query("SELECT boards.pno as msg_id, users.cont as msg_owner, boards.topi as msg_title, boards.time as msg_time FROM boards INNER JOIN users ON boards.user=users.user ORDER BY boards.pno DESC LIMIT $offset, $max");
$rows = mysql_fetch_object($result);
} else {
$sql = "SELECT msg_id, msg_time, msg_owner, msg_title FROM " . WP_BTAEON_TABLE . " WHERE msg_category='$cat' ORDER BY msg_time DESC LIMIT $offset, $max";
$rows = $wpdb->get_results($sql);
}
if ( !empty($rows) )
{
$out = '<table id="bt-list">
<colgroup>
<col id=bt-list-time />
<col id=bt-list-owner />
<col id=bt-list-title />
</colgroup>
<thead>
<tr>
<th>張貼時間</th>
<th>公告單位</th>
<th>公告標題</th>
</tr>
</thead>';
if ( $cat == 'old' )
{
while ( $row = mysql_fetch_object($result) )
{
$out .= '<tr>
<td>' . $row->msg_time . '</td>
<td>' . $row->msg_owner . '</td>
<td><a href="?mid=' . $row->msg_id . '">' . htmlspecialchars(stripslashes($row->msg_title)) . '</a></td>
</tr>
';
}
mysql_close($link);
} else {
foreach ( $rows as $row )
{
// Display display_name rather than login name
$userinfo = get_user_by('login', $row->msg_owner);
$owner = ( empty($userinfo) ) ? '' : $userinfo->display_name;
$out .= '<tr>
<td>' . convert_timestamp($row->msg_time) . '</td>
<td>' . $owner . '</td>
<td><a href="?mid=' . $row->msg_id . '">' . htmlspecialchars(stripslashes($row->msg_title)) . '</a></td>
</tr>
';
}
}
$out .= '</table>';
} else {
$out .= '<p>資料庫中沒有公告</p>';
}
return $first . $nav . $last . $out . $first . $nav . $last;
}
function shortcode_single( $mid, $cat)
{
global $wpdb, $link;
if ( $cat == 'all' )
{
$sql = "SELECT * FROM " . WP_BTAEON_TABLE . " WHERE msg_id='$mid'";
$row = $wpdb->get_results($sql);
$row = $row[0];
} elseif ( $cat == 'old' ) {
if ( function_exists('connect_old_db') )
{
connect_old_db();
// INNER JOIN `users` to select Chinese usernames
$result = mysql_query("SELECT boards.pno as msg_id, users.cont as msg_owner, boards.topi as msg_title, boards.cont as msg_content, boards.time as msg_time FROM boards INNER JOIN users ON boards.user=users.user WHERE pno='$mid'");
$row = mysql_fetch_object($result);
}
} else {
$sql = "SELECT * FROM " . WP_BTAEON_TABLE . " WHERE msg_id='$mid' AND msg_category='$cat'";
$row = $wpdb->get_results($sql);
$row = $row[0];
}
if ( !empty($row) )
{
if ( $cat == 'old' )
{
$out = "<table id=\"sh-mid\">
<tr>
<td class=\"sh-mid-left\">公告標題</td>
<td>" . htmlspecialchars(stripslashes($row->msg_title)) . "</td>
</tr>
<tr>
<td class=\"sh-mid-left\">公告時間</td>
<td>$row->msg_time</td>
</tr>
<tr>
<td class=\"sh-mid-left\">公告單位</td>
<td>$row->msg_owner</td>
</tr>
<tr>
<td class=\"sh-mid-left\">內容</td>
<td id=\"sh-mid-content\">" . nl2br(htmlspecialchars(stripslashes($row->msg_content))) . "</td>
</tr>
<tr>
<td class=\"sh-mid-left\"></td>
<td id=\"sh-mid-content\"><a href=\"http://www.hs.ntnu.edu.tw/code.php?id=1&s=news&url=1&p=bulletin/board/postview.php?pno=$row->msg_id\">以舊公告系統檢視</a></td>
</tr>
</table>";
} else {
// Display display_name rather than login name
$userinfo = get_user_by('login', $row->msg_owner);
$owner = ( empty($userinfo) ) ? '' : $userinfo->display_name;
$out = "<table id=\"sh-mid\">
<colgroup>
<col class=\"sh-mid-left\" />
<col/>
</colgroup>
<tr>
<td>公告標題</td>
<td>" . htmlspecialchars(stripslashes($row->msg_title)) . "</td>
</tr>
<tr>
<td>公告時間</td>
<td>" . convert_timestamp($row->msg_time) . "</td>
</tr>
<tr>
<td>公告單位</td>
<td>$owner</td>
</tr>
<tr>
<td>內容</td>
<td id=\"sh-mid-content\">" . nl2br(htmlspecialchars(stripslashes($row->msg_content))) . "</td>
</tr>";
if ( !empty($row->msg_link) )
{
$out .= '<tr>
<td>連結</td>
<td>';
$link_arr = unserialize($row->msg_link);
$loop = '';
foreach ( $link_arr as $key => $l )
{
$uri = $l[0];
$description = $l[1];
if ( empty($uri) && empty($description) )
{
continue;
} else {
if ( empty($description) )
$description = $uri;
$loop .= '<a href="' . $uri . '">' . $description . '</a>, ';
}
}
$loop = rtrim($loop, ', ');
$out .= $loop .'</td>
</tr>
<tr>';
}
if ( !empty($row->msg_file) )
{
$out .= '<td>附件</td>
<td>';
$file_arr = unserialize($row->msg_file);
$loop = '';
foreach ( $file_arr as $i )
{
if ( !empty($i) )
{
$file_link = pathinfo($i);
$name = $file_link['basename'];
$uri = WP_CONTENT_URL . '/' . $file_link['dirname'] . '/' . rawurlencode($file_link['basename']);
if ( empty($uri) && empty($name) )
{
continue;
} else {
if ( in_array($file_link['extension'], array('doc', 'xls', 'pdf')) )
{
$loop .= '<a href="' . $uri . '">' . $name . '</a> [<a href="https://docs.google.com/viewer?url=' . $uri . '">預覽</a>] , ';
} else {
$loop .= '<a href="' . $uri . '">' . $name . '</a> , ';
}
}
}
}
$loop = rtrim($loop, ' , ');
$out .= $loop . '</td>
</tr>';
}
$out .= '</table>';
}
} else {
$out = '<p>該公告已經被刪除了</p>';
}
$out .= '<a href="javascript:history.back()">上一頁</a>';
return $out;
}
function convert_timestamp($timestamp)
{
$date = date('Y-m-d', strtotime($timestamp));
return $date;
}
?>