forked from tad0616/photos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
230 lines (193 loc) · 6.47 KB
/
index.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
<?php
require_once '_header.php';
$page_title = '首頁';
// die(var_dump($_SESSION));/*檢查有沒有session*/
$op = isset($_REQUEST['op']) ? filter_var($_REQUEST['op']) : '';
$sn = isset($_REQUEST['sn']) ? (int) $_REQUEST['sn'] : 0;
/***************** ↓ 流程控制區 ↓ ********************/
switch ($op) {
case 'submission':
$op = 'submission';
break;
case 'insert':
$sn = data_insert();
header("location: index.php?sn={$sn}");
exit;
case 'list_photo':
show_classify($sn);
list_classify();
list_photo($sn);
break;
case 'list_allphoto':
list_classify();
list_allphoto();
break;
case 'upload_modify': //修改的表單
list_classify();
show_photo($sn);
break;
case 'modify': //執行修改動作
upload_modify($sn);
header("location: index.php?sn={$sn}");
exit;
case 'upload_delete':
upload_delete($sn);
header("location: index.php");
exit;
case 'upload': //上傳照片的表單
require "loginheader.php";
$op = 'upload';
list_classify(); //列出下拉選單show分類
break;
default:
if ($sn) {
show_photo($sn);
$op = 'show_photo';
} else {
$op = 'home';
list_classify();
}
break;
}
require_once '_footer.php';
/******************* ↓ 函式區 ↓ **********************/
//新增資料
function data_insert()
{
global $db;
$title = $db->real_escape_string($_POST['title']);
$description = $db->real_escape_string($_POST['description']);
$username = $db->real_escape_string($_POST['username']);
$classify_sn = $db->real_escape_string($_POST['classify_sn']);
$sql = "INSERT INTO `photo` (`title`, `description`, `username`, `classify_sn`, `create_time`, `update_time`) VALUES ('{$title}', '{$description}', '{$username}', '{$classify_sn}', NOW(), NOW())";
$db->query($sql) or die($db->error);
$sn = $db->insert_id;
upload_pic($sn);
return $sn;
}
//修改資料
function upload_modify($sn)
{
global $db;
$title = $db->real_escape_string($_POST['title']);
$description = $db->real_escape_string($_POST['description']);
$username = $db->real_escape_string($_POST['username']);
$classify_sn = $db->real_escape_string($_POST['classify_sn']);
$sql = "UPDATE `photo` SET `title`='{$title}', `description`='{$description}', `classify_sn`= '{$classify_sn}', `update_time`= NOW() WHERE `sn`='{$sn}'";
$db->query($sql) or die($db->error);
return $sn;
}
function upload_delete($sn)
{
global $db;
$sql = "DELETE FROM `photo` 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 list_classify()
{
global $db, $smarty;
$sql = "SELECT * FROM `classify` ORDER BY `sort` DESC";
$result = $db->query($sql) or die($db->error);
$all = array();
$i = 0;
while ($data = $result->fetch_assoc()) {
$all[$i] = $data;
// $all[$i]['summary'] = mb_substr(strip_tags($data['content']), 0, 90);
$i++;
}
// die(var_export($all));
$smarty->assign('list_classify', $all);
}
//讀出所有照片
function list_allphoto()
{
global $db, $smarty;
$sql = "SELECT * FROM `photo` ORDER BY `create_time` DESC limit 0,9";
$result = $db->query($sql) or die($db->error);
$all = array();
$i = 0;
while ($data = $result->fetch_assoc()) {
$all[$i] = $data;
// $all[$i]['summary'] = mb_substr(strip_tags($data['content']), 0, 90);
$i++;
}
// die(var_export($all));
$smarty->assign('list_allphoto', $all);
}
//讀出某則分類裡的所有照片
function list_photo($sn)
{
global $db, $smarty;
$sql = "SELECT * FROM `photo` WHERE `classify_sn`='$sn' ORDER BY `create_time` DESC limit 0,9";
$result = $db->query($sql) or die($db->error);
$all = array();
$i = 0;
while ($data = $result->fetch_assoc()) {
$all[$i] = $data;
// $all[$i]['summary'] = mb_substr(strip_tags($data['content']), 0, 90);
$i++;
}
// die(var_export($all));
$smarty->assign('all', $all);
}
//讀出一則分類資料
function show_classify($sn)
{
global $db, $smarty;
$sql = "SELECT * FROM `classify` WHERE `sn`='$sn'";
$result = $db->query($sql) or die($db->error);
$data = $result->fetch_assoc();
$data['summary'] = mb_substr(strip_tags($data['description']), 0, 30);
$smarty->assign('item_classify', $data);
}
//讀出一則照片資料
function show_photo($sn)
{
global $db, $smarty;
// require_once 'HTMLPurifier/HTMLPurifier.auto.php';
// $config = HTMLPurifier_Config::createDefault();
// $purifier = new HTMLPurifier($config);
$sql = "SELECT * FROM `photo` WHERE `sn`='$sn'";
$result = $db->query($sql) or die($db->error);
$data = $result->fetch_assoc();
// $data['content'] = $purifier->purify($data['content']);
$data['description_n2br'] = str_replace("\n", '<br />', $data['description']);
$data['summary'] = mb_substr(strip_tags($data['description']), 0, 90);
$data['display_time'] = date("d M Y", strtotime($data['update_time']));
$smarty->assign('photo', $data);
}
//上傳作品照片
function upload_pic($sn)
{
if (isset($_FILES)) {
require_once 'class.upload.php';
$foo = new Upload($_FILES['uploadpic']);
if ($foo->uploaded) {
// save uploaded image with a new name
$foo->file_new_name_body = 'cover_' . $sn;
$foo->image_resize = true;
$foo->image_convert = jpg;
$foo->image_x = 1920;
$foo->image_ratio_y = true;
$foo->Process('uploads/');
if ($foo->processed) {
$foo->file_new_name_body = 'thumb_' . $sn;
$foo->image_resize = true;
$foo->image_convert = jpg;
$foo->image_x = 480;
$foo->image_ratio_y = true;
$foo->Process('uploads/');
}
}
}
}
//取得JPG檔EXIF資訊
function exif_display($sn)
{
$exif = exif_read_data("'uploads/cover_'.$sn'.jpg'", 0, true);
}