forked from tad0616/reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
108 lines (96 loc) · 3.13 KB
/
admin.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
<?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 'insert':
$sn = insert_article();
header("location: index.php?sn={$sn}");
exit;
case "article_form":
article_form($sn);
break;
case "delete_article":
delete_article($sn);
header("location: index.php");
exit;
case 'update':
update_article($sn);
header("location: index.php?sn={$sn}");
exit;
default:
$op = "admin";
break;
}
require_once 'footer.php';
/*************函數區**************/
//儲存文章
function insert_article()
{
global $db;
$title = $db->real_escape_string($_POST['title']);
$content = $db->real_escape_string($_POST['content']);
$username = $db->real_escape_string($_POST['username']);
$sql = "INSERT INTO `article` (`title`, `content`, `create_time`, `update_time`, `username`) VALUES ('{$title}', '{$content}', NOW(), NOW(), '{$username}')";
$db->query($sql) or die($db->error);
$sn = $db->insert_id;
upload_pic($sn);
return $sn;
}
//儲存文章
function delete_article($sn)
{
global $db;
$sql = "DELETE FROM `article` WHERE sn='{$sn}' AND username='{$_SESSION['username']}'";
$db->query($sql) or die($db->error);
if (file_exists("uploads/cover_{$sn}.jpg")) {
unlink("uploads/cover_{$sn}.jpg");
unlink("uploads/thumb_{$sn}.jpg");
}
}
function article_form($sn)
{
show_article($sn);
}
//儲存文章
function update_article($sn)
{
global $db;
$title = $db->real_escape_string($_POST['title']);
$content = $db->real_escape_string($_POST['content']);
$username = $db->real_escape_string($_POST['username']);
$sql = "UPDATE `article` SET `title`='{$title}', `content`='{$content}',`update_time`=NOW() WHERE sn='$sn' AND username='{$_SESSION['username']}'";
$db->query($sql) or die($db->error);
upload_pic($sn);
return $sn;
}
function upload_pic($sn)
{
if (isset($_FILES['pic']['name'])) {
$ext = pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION);
if (!is_dir("uploads")) {
mkdir("uploads");
}
require_once 'class.upload.php';
$upload = new Upload($_FILES['pic']);
if ($upload->uploaded) {
// save uploaded image with a new name
$upload->file_new_name_body = 'cover_' . $sn;
$upload->image_resize = true;
$upload->image_convert = 'jpg';
$upload->image_x = 1200;
$upload->image_ratio_y = true;
$upload->Process('uploads/');
if ($upload->processed) {
$upload->file_new_name_body = 'thumb_' . $sn;
$upload->image_resize = true;
$upload->image_convert = 'jpg';
$upload->image_x = 400;
$upload->image_ratio_y = true;
$upload->Process('uploads/');
}
}
}
}