This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
topic.php
143 lines (123 loc) · 4.11 KB
/
topic.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
<?php
require "loginheader.php";
require_once 'header.php';
$page_title = '專題設定';
$op = isset($_REQUEST['op']) ? filter_var($_REQUEST['op']) : '';
$sn = isset($_REQUEST['sn']) ? (int) $_REQUEST['sn'] : 0;
switch ($op) {
case 'add_topic':
//新增類別
//$sn = insert_topic();
//header("location: index.php?sn={$sn}");
break;
case 'insert':
$sn = insert_topic();
header("location: topic.php");
exit;
case 'update':
update_topic($sn);
header("location: topic.php");
exit;
case 'modify_topic':
//修改類別
show_topic($sn);
break;
case 'delete_topic':
//刪除類別
delete_topic($sn);
header("location: topic.php");
exit;
//預設動作
default:
list_topic(); //$action_id
break;
/* if ($sn) {
$op = 'show_topic';
show_topic($sn);
list_topic($sn);
} else {
$op = 'list_topic';
list_topic();
}
break; */
}
require_once 'footer.php';
/*************函數區**************/
//儲存類別
function insert_topic()
{
global $db;
$topic_title = $db->real_escape_string($_POST['topic_title']);
$topic_type = $db->real_escape_string($_POST['topic_type']);
$topic_description = $db->real_escape_string($_POST['topic_description']);
$topic_status = $db->real_escape_string($_POST['topic_status']);
if ($topic_type == "類別") {
$topic_status = '';
}
$sql = "INSERT INTO `topic` (`topic_title`, `topic_type`, `topic_description`, `topic_status`, `username`) VALUES ('{$topic_title}', '{$topic_type}', '{$topic_description}', '{$topic_status}' ,'{$_SESSION['username']}')";
$db->query($sql) or die($db->error);
$sn = $db->insert_id;
upload_pic($sn);
return $sn;
}
//刪除類別
function delete_topic($sn)
{
global $db;
$sql = "DELETE FROM `topic` WHERE topic_sn='{$sn}'";
$db->query($sql) or die($db->error);
}
//更新類別
function update_topic($sn)
{
global $db;
$topic_title = $db->real_escape_string($_POST['topic_title']);
$topic_type = $db->real_escape_string($_POST['topic_type']);
$topic_description = $db->real_escape_string($_POST['topic_description']);
$topic_status = $db->real_escape_string($_POST['topic_status']);
if ($topic_type == "類別") {
$topic_status = '';
}
$sql = "UPDATE `topic` SET `topic_title`='{$topic_title}', `topic_type`='{$topic_type}', `topic_description`='{$topic_description}',`topic_status`='{$topic_status}' WHERE `topic_sn`='{$sn}' ";
$db->query($sql) or die($db->error);
upload_pic($sn);
}
//讀出單一類別
function show_topic($sn)
{
global $db, $smarty;
require_once 'HTMLPurifier/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$sql = "SELECT * FROM `topic` WHERE `topic_sn`='$sn'";
$result = $db->query($sql) or die($db->error);
$data = $result->fetch_assoc();
$data['topic_description'] = $purifier->purify($data['topic_description']);
$smarty->assign('topic', $data);
}
function upload_pic($sn)
{
if (isset($_FILES)) {
require_once 'class.upload.php';
$foo = new Upload($_FILES['pic']);
if ($foo->uploaded) {
// save uploaded image with a new name
$foo->file_new_name_body = 'topic_cover_' . $sn;
$foo->file_overwrite = true;
$foo->image_resize = true;
$foo->image_convert = png;
$foo->image_x = 1200;
$foo->image_ratio_y = true;
$foo->Process('uploads/');
if ($foo->processed) {
$foo->file_new_name_body = 'topic_thumb_' . $sn;
$foo->file_overwrite = true;
$foo->image_resize = true;
$foo->image_convert = png;
$foo->image_x = 400;
$foo->image_ratio_y = true;
$foo->Process('uploads/');
}
}
}
}