forked from cynial/STBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
330 lines (281 loc) · 8.19 KB
/
upload.php
File metadata and controls
330 lines (281 loc) · 8.19 KB
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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* STBlog Blogging System
*
* 基于Codeigniter的单用户多权限开源博客系统
*
* STBlog is an open source multi-privilege blogging System built on the
* well-known PHP framework Codeigniter.
*
* @package STBLOG
* @author Saturn <[email protected]>
* @copyright Copyright (c) 2009 - 2010, cnsaturn.com.
* @license GNU General Public License 2.0
* @link http://code.google.com/p/stblog/
* @version 0.1.0
*/
// ------------------------------------------------------------------------
/**
* STBLOG Upload Class
*
* 系统采用swf uploader处理上传文件,由于此类由flash触发,故不能采用系统默认的验证方式
*
* @package STBLOG
* @subpackage Libraries
* @category Libraries
* @author Saturn <[email protected]>
* @link http://code.google.com/p/stblog/
*/
class Upload extends CI_Controller {
/**
* 默认上传路径
*
* @access private
* @var string
*/
private $_upload_dir = 'uploads/';
/**
* 默认允许上传后缀名
*
* @access private
* @var string
*/
private $_upload_exts = 'zip|gz|rar|swf|jpg|png|gif|jpeg';
/**
* 常用图片文件后缀名
*
* @access private
* @var string
*/
private $_image_exts = array('png','gif','jpg','jpeg','bmp','tiff','tif', 'ico', 'tga');
/**
* 构造函数
*
* @access public
* @return void
*/
function __construct()
{
parent::__construct();
$this->load->library('upload');
$this->load->model('users_mdl');
$this->_setup_upload_cfgs();
}
/**
* 默认执行方法
*
* 本方法仅允许被Flash触发
* 由于flash触发此方法时会产生一个新的session
* 所以这里采用token来验证上传是否合法
*
* @access public
* @return void
*/
public function index()
{
$uid = $this->input->post('__uid', TRUE);
$token = $this->input->post('__token', TRUE);
if(empty($uid) || empty($token)) show_404();
$user = $this->users_mdl->get_user_by_id($uid);
if($user['token'] == $token && ('contributor' == $user['group'] || 'editor' == $user['group'] || 'administrator' == $user['group']))
{
/** 合法用户,设置执行参数并执行上传 */
$config['upload_path'] = FCPATH.$this->_upload_dir;
$config['allowed_types'] = $this->_upload_exts;
$this->upload->initialize($config);
if(!$this->upload->do_upload($filed = 'Filedata'))
{
log_message('debug', $this->upload->display_errors());
}
else
{
$upload_data = $this->upload->data();
$file = array(
'name' => $upload_data['file_name'],
'path' => $this->_upload_dir. $upload_data['file_name'],
'size' => $upload_data['file_size'],
'mime' => get_mime_by_extension($upload_data['orig_name']),
'isImage' => $this->_is_image($upload_data['file_name'])
);
//DB
$attachment_data = array(
'title' => $upload_data['file_name'],
'slug' => $upload_data['file_name'],
'created' => time(),
'modified' => time(),
'text' => serialize($file),
'order' => 0,
'authorId' => $uid,
'type' => 'attachment',
'status' => 'unattached',
'commentsNum' => 0,
'allowComment' => 0,
'allowPing' => 0,
'allowFeed' => 0
);
$insert_id = $this->posts_mdl->add_post($attachment_data);
if(!empty($insert_id))
{
$this->load->helper('json');
throwJson(array(
'pid' => $insert_id,
'title' => $upload_data['file_name'],
'type' => $this->_get_type($upload_data['file_name']),
'size' => $upload_data['file_size'],
'isImage' => $this->_is_image($upload_data['file_name']),
'url' => base_url().$this->_upload_dir.$upload_data['file_name'],
'permalink' => site_url('attachment'.'/'.$insert_id)
));
}
}
}
show_404();
}
/**
* 替换上传文件
*
* 本方法仅允许被Flash触发
* 由于flash触发此方法时会产生一个新的session
* 所以这里采用token来验证上传是否合法
*
* @access public
* @param int $pid 文章ID
* @return void
*/
public function modify($pid = 0)
{
$uid = $this->input->post('__uid',TRUE);
$token = $this->input->post('__token',TRUE);
if(empty($uid) || empty($token) || empty($pid) || !is_numeric($pid)) show_404();
$user = $this->users_mdl->get_user_by_id($uid);
$attachment = $this->posts_mdl->get_post_by_id('pid', $pid);
if($attachment)
{
$info = unserialize($attachment->text);
}
else
{
show_404();
}
unset($attachment);
if($user['token'] == $token && ('contributor' == $user['group'] || 'editor' == $user['group'] || 'administrator' == $user['group']))
{
/** 合法用户,设置执行参数并执行上传 */
$config['upload_path'] = FCPATH . $this->_upload_dir;
$config['allowed_types'] = $this->_upload_exts;
$config['file_name'] = $info['name'];
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
if(!$this->upload->do_upload($filed = 'Filedata'))
{
log_message('debug', $this->upload->display_errors());
}
else
{
$upload_data = $this->upload->data();
$file = array(
'name' => $upload_data['file_name'],
'path' => $this->_upload_dir. $upload_data['file_name'],
'size' => $upload_data['file_size'],
'mime' => get_mime_by_extension($upload_data['orig_name']),
'isImage' => $this->_is_image($upload_data['file_name'])
);
//DB
$attachment_data = array(
'modified' => time(),
'text' => serialize($file),
'authorId' => $uid,
);
if($this->posts_mdl->update_post($pid, $attachment_data))
{
$this->load->helper('json');
throwJson(array(
'pid' => $pid,
'title' => $upload_data['file_name'],
'type' => $this->_get_type($upload_data['file_name']),
'size' => $upload_data['file_size'],
'isImage' => $this->_is_image($upload_data['file_name']),
'url' => base_url().$this->_upload_dir.$upload_data['file_name'],
'permalink' => site_url('attachment'.'/'.$pid)
));
}
}
}
show_404();
}
/**
* 上传文件是否为图片
*
* 由于SWFUPLOAD所有类型文件的MIME均为application/octet-stream
* 故使用Codeigniter自带的上传类无法准确获取是否为图片
*
* @access private
* @param string $file 文件名
* @return string
*/
private function _is_image($file)
{
$ext = $this->_get_type($file);
return (in_array($ext, $this->_image_exts))?TRUE:FALSE;
}
/**
* 根据文件名获取拓展名
*
* @access private
* @param string $file_name 文件名
* @return string
*/
private function _get_type($file)
{
$ext = '';
$part = explode('.', $file);
if (($length = count($part)) > 1)
{
$ext = strtolower($part[$length - 1]);
}
return $ext;
}
/**
* 初始化上传参数
*
* @access private
* @return void
*/
private function _setup_upload_cfgs()
{
$settings = &get_settings();
if(array_key_exists('upload_dir', $settings) && array_key_exists('upload_exts',$settings))
{
$this->_upload_dir = $settings['upload_dir'];
if (!is_dir(FCPATH.$this->_upload_dir))
{
if(!$this->_make_upload_dir())
{
log_message('debug', '上传目录创建失败');
}
}
$this->_upload_exts = str_replace(';','|',$settings['upload_exts']);
$this->_upload_exts = str_replace('*.','',$this->_upload_exts);
}
}
/**
* 创建上传路径
*
* @access private
* @param string $path 路径
* @return boolean
*/
private function _make_upload_dir($path)
{
if (!@mkdir($path))
{
return false;
}
$stat = @stat($path);
$perms = $stat['mode'] & 0007777;
@chmod($path, $perms);
return true;
}
}
/* End of file Upload.php */
/* Location: ./application/controllers/admin/Upload.php */