forked from Moidea/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
306 lines (260 loc) · 8.37 KB
/
Action.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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
class HighSlide_Action extends Typecho_Widget implements Widget_Interface_Do
{
private $db;
private $options;
private $prefix;
/**
* 添加相册图片
*
* @access public
* @return void
*/
public function insertgallery()
{
if (HighSlide_Plugin::form('insert')->validate()) {
$this->response->goBack();
}
$gallery = $this->request->from('thumb','image','description','sort','name');
$gallery['order'] = $this->db->fetchObject($this->db->select(array('MAX(order)'=>'maxOrder'))->from($this->prefix.'gallery'))->maxOrder+1;
$gallery['gid'] = $this->db->query($this->db->insert($this->prefix.'gallery')->rows($gallery));
//返回原页并提示信息
$this->widget('Widget_Notice')->highlight('gallery-'.$gallery['gid']);
$this->widget('Widget_Notice')->set(_t('相册组%s: 图片%s 添加成功',
$gallery['sort'],$gallery['name']),NULL,'success');
$this->response->redirect(Typecho_Common::url('extending.php?panel=HighSlide%2Fmanage-gallery.php&group='.$gallery['sort'],$this->options->adminUrl));
}
/**
* 更新相册图片
*
* @access public
* @return void
*/
public function updategallery()
{
if (HighSlide_Plugin::form('update')->validate()) {
$this->response->goBack();
}
$gallery = $this->request->from('gid','thumb','image','description','sort','name');
$this->db->query($this->db->update($this->prefix.'gallery')->rows($gallery)->where('gid=?',$gallery['gid']));
//返回原页并提示信息
$this->widget('Widget_Notice')->highlight('gallery-'.$gallery['gid']);
$this->widget('Widget_Notice')->set(_t('相册组%s: 图片%s 更新成功',
$gallery['sort'],$gallery['name']),NULL,'success');
$this->response->redirect(Typecho_Common::url('extending.php?panel=HighSlide%2Fmanage-gallery.php&group='.$gallery['sort'],$this->options->adminUrl));
}
/**
* 移除相册图片
*
* @access public
* @return void
*/
public function deletegallery()
{
$gids = $this->request->filter('int')->getArray('gid');
$deletecount = 0;
if ($gids) {
foreach ($gids as $gid) {
if ($this->db->query($this->db->delete($this->prefix.'gallery')->where('gid=?',$gid))) {
$deletecount ++;
}
}
}
//返回原页并提示信息
$this->widget('Widget_Notice')->set($deletecount>0?_t('图片已从相册移除'):_t('没有图片被移除'),NULL,
$deletecount>0?'success':'notice');
$this->response->goBack();
}
/**
* 排序相册图片
*
* @access public
* @return void
*/
public function sortgallery()
{
$galleries = $this->request->filter('int')->getArray('gid');
if ($galleries) {
foreach ($galleries as $sort=>$gid) {
$this->db->query($this->db->update($this->prefix.'gallery')->rows(array('order'=>$sort+1))->where('gid=?',$gid));
}
}
//返回原页并提示信息
if (!$this->request->isAjax()) {
$this->response->goBack();
} else {
$this->response->throwJson(array('success'=>1,'message'=>_t('图片排序完成')));
}
}
/**
* 执行上传图片
*
* @access public
* @return void
*/
public function uploadimage()
{
if (!empty($_FILES)) {
$file = array_pop($_FILES);
if (0==$file['error']&&is_uploaded_file($file['tmp_name'])) {
// xhr的send无法支持utf8
if ($this->request->isAjax()) {
$file['name'] = urldecode($file['name']);
}
$result = HighSlide_Plugin::uploadhandle($file);
if (false!==$result) {
$this->response->throwJson(array(array(
'name'=>$result['name'],
'title'=>$result['title'],
'bytes'=>number_format(ceil($result['size']/1024)).' Kb'
)));
}
}
}
$this->response->throwJson(false);
}
/**
* 执行删除图片
*
* @access public
* @return void
*/
public function removeimage()
{
$imgname = $this->request->from('imgname');
//获取附件源参数
$path = $this->request->from('path');
$url = $this->request->from('url');
if ($imgname) {
HighSlide_Plugin::removehandle($imgname['imgname'],$path['path'],$url['url']);
}
$this->response->throwJson(false);
}
/**
* 执行裁切图片
*
* @access public
* @return void
*/
public function cropthumbnail()
{
$imgname = $this->request->from('imgname');
$w = $this->request->from('w');
$h = $this->request->from('h');
$x1 = $this->request->from('x1');
$y1 = $this->request->from('y1');
//获取附件源参数
$path = $this->request->from('path');
$url = $this->request->from('url');
if ($imgname) {
$result = HighSlide_Plugin::crophandle($imgname['imgname'],$w['w'],$h['h'],$x1['x1'],$y1['y1'],$path['path'],$url['url']);
$this->response->throwJson(array(
'bytes'=>number_format(ceil($result/1024)).' Kb'
));
}
$this->response->throwJson(false);
}
/**
* 同步插件设置
*
* @access public
* @return void
*/
public function syncsettings()
{
//验证组合表单
$requests = array_merge($this->request->from('fixedwidth'),
$this->request->from('fixedheight'),
$this->request->from('fixedratio')
);
$validator = new Typecho_Validate();
$validator->addRule('fixedwidth','isInteger',_t('固定宽度请输入整数数字'));
$validator->addRule('fixedheight','isInteger',_t('固定高度请输入整数数字'));
$validator->addRule('fixedratio',array(new HighSlide_Plugin,'ratioformat'),_t('固定比例请输入:与数字'));
$validator->addRule('fixedwidth','required',_t('固定宽度不能为空'));
$validator->addRule('fixedheight','required',_t('固定高度不能为空'));
$validator->addRule('fixedratio','required',_t('固定比例不能为空'));
if ($error = $validator->run($requests)) {
$this->widget('Widget_Notice')->set($error,'error');
$this->response->goBack();
}
//构建同步数组
$syncsets = array('qiniubucket','qiniudomain','qiniuaccesskey','qiniusecretkey','qiniuprefix',
'upyunbucket','upyundomain','upyunuser','upyunpwd','upyunkey','upyunprefix',
'bcsbucket','bcsapikey','bcssecretkey','bcsprefix',
'storage','local','thumbfix','fixedwidth','fixedheight','fixedratio','gallery');
foreach ($syncsets as $syncset) {
$result = $this->request->from($syncset);
$datas[$syncset] = $result[$syncset];
}
//返回原页并提示信息
Widget_Plugins_Edit::configPlugin('HighSlide',$datas);
$this->widget('Widget_Notice')->set(_t('相册设置已保存'),NULL,'success');
$this->response->goBack();
}
/**
* 异步附件预览
*
* @access public
* @return void
*/
public function postpreview()
{
$cid = $this->request->filter('int')->cid;
//调用对象
if ($cid) {
Typecho_Widget::widget('Widget_Contents_Attachment_Related','parentId='.$cid)->to($attachment);
} else {
Typecho_Widget::widget('Widget_Contents_Attachment_Unattached')->to($attachment);
}
//还原为数组
while ($attachment->next()) {
$datas[] = unserialize($attachment->attachment);
}
foreach ($datas as $data) {
if (!$data['isImage']) {
return false;
}
$name = basename($data['path']);
$prefix = dirname($data['url']);
$thumb = $prefix.'/thumb_'.$name;
//直接验证缩略图
$headers = @get_headers($thumb,true);
$open = (@fopen($thumb,'r'))?1:0;
$parse[] = array('title'=>$data['name'],
'name'=>$name,
'url'=>$data['url'],
'prefix'=>$prefix,
'path'=>str_replace($name,'',$data['path']),
'size'=>number_format(ceil($data['size']/1024)).' KB',
'thumb'=>$thumb,
'tsize'=>number_format(ceil($headers['Content-Length']/1024)).' KB',
'tstat'=>$open);
}
$parse = json_encode($parse);
$this->response->throwJson($parse);
}
/**
* 绑定动作
*
* @access public
* @return void
*/
public function action()
{
$this->db = Typecho_Db::get();
$this->prefix = $this->db->getPrefix();
$this->options = Typecho_Widget::widget('Widget_Options');
$this->on($this->request->is('do=insert'))->insertgallery();
$this->on($this->request->is('do=update'))->updategallery();
$this->on($this->request->is('do=delete'))->deletegallery();
$this->on($this->request->is('do=sort'))->sortgallery();
$this->on($this->request->is('do=upload'))->uploadimage();
$this->on($this->request->is('do=remove'))->removeimage();
$this->on($this->request->is('do=crop'))->cropthumbnail();
$this->on($this->request->is('do=sync'))->syncsettings();
$this->on($this->request->is('do=preview'))->postpreview();
$this->response->redirect($this->options->adminUrl);
}
}