📄 正在查看:admin/control/photo_control.class.php
大小:23,758 字节 · 修改:2014-04-10 07:30:54 · 行数:710
1<?php
2/**
3 * (C)2012-2014 twcms.com TongWang Inc.
4 * Author: wuzhaohuan <kongphp@gmail.com>
5 */
6
7defined('TWCMS_PATH') or exit;
8
9class photo_control extends admin_control {
10 // 内容管理
11 public function index() {
12 // hook admin_photo_control_index_before.php
13
14 $cid = intval(R('cid'));
15 $keyword = empty($_POST) ? R('keyword') : R('keyword', 'P');
16 if($keyword) {
17 $keyword = urldecode($keyword);
18 $keyword = safe_str($keyword);
19 }
20 $this->assign('keyword', $keyword);
21
22 // 获取分类下拉框
23 $cidhtml = $this->category->get_cidhtml_by_mid(4, $cid, '所有图集');
24 $this->assign('cidhtml', $cidhtml);
25
26 // 初始模型表名
27 $this->cms_content->table = 'cms_photo';
28
29 // 初始分页
30 $pagenum = 20;
31 if($keyword) {
32 $where = array('title'=>array('LIKE'=>$keyword));
33 $total = $this->cms_content->find_count($where);
34 $urlstr = '-keyword-'.urlencode($keyword);
35 }elseif($cid) {
36 $where = array('cid' => $cid);
37 $categorys = $this->category->read($cid);
38 $total = isset($categorys['count']) ? $categorys['count'] : 0;
39 $urlstr = '-cid-'.$cid;
40 }else{
41 $where = array();
42 $total = $this->cms_content->count();
43 $urlstr = '';
44 }
45 $maxpage = max(1, ceil($total/$pagenum));
46 $page = min($maxpage, max(1, intval(R('page'))));
47 $pages = pages($page, $maxpage, 'index.php?u=photo-index'.$urlstr.'-page-{page}');
48 $this->assign('total', $total);
49 $this->assign('pages', $pages);
50
51 // 读取内容列表
52 $flag_arr = array(1=>'推荐', 2=>'热点', 3=>'头条', 4=>'精选', 5=>'幻灯');
53 $cms_photo_arr = $this->cms_content->list_arr($where, 'id', -1, ($page-1)*$pagenum, $pagenum, $total);
54 foreach($cms_photo_arr as &$v) {
55 $this->cms_content->format($v, 2);
56
57 // 属性
58 $v['flagstr'] = '';
59 if(!empty($v['imagenum'])) {
60 $v['flagstr'] .= ' [图片]';
61 }
62 if(!empty($v['flags'])) {
63 $flags = explode(',', $v['flags']);
64 foreach($flags as $flag) {
65 $flag = intval($flag);
66 if($flag) $v['flagstr'] .= ' ['.$flag_arr[$flag].']';
67 }
68 }
69 if($v['flagstr']) $v['flagstr'] = '<font color="BC0B0B">'.$v['flagstr'].'</font>';
70 }
71 $this->assign('cms_photo_arr', $cms_photo_arr);
72
73 // hook admin_photo_control_index_after.php
74
75 $this->display();
76 }
77
78 // 发布图集
79 public function add() {
80 // hook admin_photo_control_add_before.php
81
82 $uid = $this->_user['uid'];
83 if(empty($_POST)) {
84 $this->_pkey = 'content';
85 $this->_ukey = 'photo-add';
86 $this->_title = '发布图集';
87 $this->_place = '内容 &#187; 内容管理 &#187 发布图集';
88
89 $habits = (array)$this->kv->get('user_habits_uid_'.$uid);
90 $cid = isset($habits['last_add_cid']) ? (int)$habits['last_add_cid'] : 0;
91
92 $data = $this->kv->get('auto_save_photo_uid_'.$uid);
93 if($data) {
94 !empty($data['cid']) && $cid = $data['cid'];
95 $data['pic_src'] = empty($data['pic']) ? '../static/img/nopic.gif' : '../'.$data['pic'];
96 empty($data['author']) && $data['author'] = $this->_user['username'];
97 $data['flags'] = empty($data['flag']) ? array() : $data['flag'];
98 !empty($data['images']) && $data['images'] = (array)$data['images'];
99 $data['content'] = htmlspecialchars($data['content']);
100 }else{
101 $data['flags'] = array();
102 $data['pic_src'] = '../static/img/nopic.gif';
103 $data['author'] = $this->_user['username'];
104 $data['views'] = 0;
105 }
106 $this->assign('data', $data);
107
108 $cidhtml = $this->category->get_cidhtml_by_mid(4, $cid);
109 $this->assign('cidhtml', $cidhtml);
110
111 $edit_cid_id = '&mid=4';
112 $this->assign('edit_cid_id', $edit_cid_id);
113
114 $this->display('photo_set.htm');
115 }else{
116 $cid = intval(R('cid', 'P'));
117 $title = trim(strip_tags(R('title', 'P')));
118 $alias = trim(R('alias', 'P'));
119 $flags = (array)R('flag', 'P');
120 $views = intval(R('views', 'P'));
121 $images = (array)R('images', 'P');
122 $contentstr = trim(R('content', 'P'));
123 $intro = trim(R('intro', 'P'));
124 $dateline = trim(R('dateline', 'P'));
125 $isremote = intval(R('isremote', 'P'));
126 $pic = trim(R('pic', 'P'));
127
128 empty($cid) && E(1, '亲,您没有选择分类哦!');
129 empty($title) && E(1, '亲,您的标题忘了填哦!');
130 empty($images) && E(1, '亲,您的图集忘上传图片了!');
131
132 $categorys = $this->category->read($cid);
133 if(empty($categorys)) E(1, '分类ID不存在!');
134
135 $mid = $this->category->get_mid_by_cid($cid);
136 $table = $this->models->get_table($mid);
137
138 // 防止提交到其他模型的分类
139 if($table != 'photo') E(1, '分类ID非法!');
140
141 // 检测别名是否能用
142 if($alias && $err_msg = $this->only_alias->check_alias($alias)) {
143 E(1, $err_msg);
144 }
145
146 // 标签预处理,最多支持5个标签
147 $tags = trim(R('tags', 'P'), ", \t\n\r\0\x0B");
148 $tags_arr = explode(',', $tags);
149 $this->cms_content_tag->table = 'cms_'.$table.'_tag';
150 $tagdatas = $tags = array();
151 for($i=0; isset($tags_arr[$i]) && $i<5; $i++) {
152 $name = trim($tags_arr[$i]);
153 if($name) {
154 $tagdata = $this->cms_content_tag->find_fetch(array('name'=>$name), array(), 0, 1);
155 if($tagdata) {
156 $tagdata = current($tagdata);
157 }else{
158 $tagid = $this->cms_content_tag->create(array('name'=>$name, 'count'=>0, 'content'=>''));
159 if(!$tagid) E(1, '写入标签表出错');
160 $tagdata = $this->cms_content_tag->get($tagid);
161 }
162
163 $tagdata['count']++;
164 $tagdatas[] = $tagdata;
165 $tags[$tagdata['tagid']] = $tagdata['name'];
166 }
167 }
168
169 // 远程图片本地化
170 $endstr = '';
171 $this->cms_content_attach->table = 'cms_'.$table.'_attach';
172 if($isremote) {
173 $endstr .= $this->get_remote_img($table, $contentstr, $uid);
174 }
175
176 // 计算图片数,和非图片文件数
177 $imagenum = $this->cms_content_attach->find_count(array('id'=>0, 'uid'=>$uid, 'isimage'=>1));
178 $filenum = $this->cms_content_attach->find_count(array('id'=>0, 'uid'=>$uid, 'isimage'=>0));
179
180 // 如果缩略图为空,并且内容含有图片,则将第一张图片设置为缩略图
181 if(empty($pic) && $imagenum) {
182 $pic = $this->auto_pic($table, $uid);
183 }
184
185 // 如果摘要为空,自动生成摘要
186 $intro = $this->auto_intro($intro, $contentstr);
187
188 // 写入内容表
189 $data = array(
190 'cid' => $cid,
191 'title' => $title,
192 'color' => trim(R('color', 'P')),
193 'alias' => $alias,
194 'tags' => _json_encode($tags),
195 'intro' => $intro,
196 'pic' => $pic,
197 'uid' => $uid,
198 'author' => trim(R('author', 'P')), // 可以不等于发布用户
199 'source' => trim(R('source', 'P')),
200 'dateline' => $dateline ? strtotime($dateline) : $_ENV['_time'],
201 'lasttime' => $_ENV['_time'],
202 'ip' => ip2long($_ENV['_ip']),
203 'iscomment' => intval(R('iscomment', 'P')),
204 'comments' => 0,
205 'imagenum' => $imagenum,
206 'filenum' => $filenum,
207 'flags' => implode(',', $flags),
208 'seo_title' => trim(strip_tags(R('seo_title', 'P'))),
209 'seo_keywords' => trim(strip_tags(R('seo_keywords', 'P'))),
210 'seo_description' => trim(strip_tags(R('seo_description', 'P'))),
211 );
212 $this->cms_content->table = 'cms_'.$table;
213 $id = $this->cms_content->create($data);
214 if(!$id) {
215 E(1, '写入内容表出错');
216 }
217
218 // 写入全站唯一别名表
219 if($alias && !$this->only_alias->set($alias, array('mid' => $mid, 'cid' => $cid, 'id' => $id))) {
220 E(1, '写入全站唯一别名表出错');
221 }
222
223 // 写入内容数据表
224 $this->cms_content_data->table = 'cms_'.$table.'_data';
225 if(!$this->cms_content_data->set($id, array('content' => $contentstr, 'images' => json_encode($images)))) {
226 E(1, '写入内容数据表出错');
227 }
228
229 // 写入内容查看数表
230 $this->cms_content_views->table = 'cms_'.$table.'_views';
231 if(!$this->cms_content_views->set($id, array('cid' => $cid, 'views' => $views))) {
232 E(1, '写入内容查看数表出错');
233 }
234
235 // 更新附件归宿 cid 和 id
236 if($imagenum || $filenum) {
237 if(!$this->cms_content_attach->find_update(array('id'=>0, 'uid'=>$uid), array('cid'=>$cid, 'id'=>$id))) {
238 E(1, '更新内容附件表出错');
239 }
240 }
241
242 // 写入内容属性标记表
243 $this->cms_content_flag->table = 'cms_'.$table.'_flag';
244 foreach($flags as $flag) {
245 if(!$this->cms_content_flag->set(array($flag, $id), array('cid'=>$cid))) {
246 E(1, '写入内容属性标记表出错');
247 }
248 }
249
250 // 如果内容含有图片附件,则标记图片属性
251 if($imagenum && !$this->cms_content_flag->set(array(0, $id), array('cid'=>$cid))) {
252 E(1, '写入内容属性标记表出错');
253 }
254
255 // 写入内容标签表
256 $this->cms_content_tag_data->table = 'cms_'.$table.'_tag_data';
257 foreach($tagdatas as $tagdata) {
258 $this->cms_content_tag->update($tagdata);
259 $this->cms_content_tag_data->set(array($tagdata['tagid'], $id), array('id'=>$id));
260 }
261
262 // 更新用户发布的内容条数
263 $this->_user['contents']++;
264 $this->user->update($this->_user);
265
266 // 更新分类的内容条数
267 $categorys['count']++;
268 $this->category->update($categorys);
269 $this->category->update_cache($cid);
270
271 $data = $this->kv->delete('auto_save_photo_uid_'.$uid);
272
273 // 记住最后一次发布的分类ID,感觉这样人性化一些吧。
274 $habits = (array) $this->kv->get('user_habits_uid_'.$uid);
275 $habits['last_add_cid'] = $cid;
276 $habits = $this->kv->set('user_habits_uid_'.$uid, $habits);
277
278 // hook admin_photo_control_add_after.php
279
280 E(0, '发布完成'.$endstr);
281 }
282 }
283
284 // 编辑图集
285 public function edit() {
286 // hook admin_photo_control_edit_before.php
287
288 if(empty($_POST)) {
289 $id = intval(R('id'));
290 $cid = intval(R('cid'));
291
292 $this->_pkey = 'content';
293 $this->_ukey = 'photo-edit-cid-'.$cid.'-id-'.$id;
294 $this->_title = '编辑图集';
295 $this->_place = '内容 &#187; 内容管理 &#187 编辑图集';
296
297 $cidhtml = $this->category->get_cidhtml_by_mid(4, $cid);
298 $this->assign('cidhtml', $cidhtml);
299
300 $table = 'photo';
301
302 // 读取内容
303 $this->cms_content->table = 'cms_'.$table;
304 $this->cms_content_data->table = 'cms_'.$table.'_data';
305 $this->cms_content_views->table = 'cms_'.$table.'_views';
306 $data = $this->cms_content->get($id);
307 if(empty($data)) $this->message(0, '内容不存在!', -1);
308
309 $data2 = $this->cms_content_data->get($id);
310 $data3 = $this->cms_content_views->get($id);
311 $data = array_merge($data, $data2, $data3);
312 $data['images'] = (array)_json_decode($data['images']);
313 $data['content'] = htmlspecialchars($data['content']);
314 $data['tags'] = implode(',', (array)_json_decode($data['tags']));
315 $data['intro'] = str_replace('<br />', "\n", strip_tags($data['intro'], '<br>'));
316 $data['pic_src'] = empty($data['pic']) ? '../static/img/nopic.gif' : '../'.$data['pic'];
317 $data['flags'] = explode(',', $data['flags']);
318 $data['dateline'] = date('Y-m-d H:i:s', $data['dateline']);
319 $this->assign('data', $data);
320
321 $edit_cid_id = '&mid=4&cid='.$data['cid'].'&id='.$data['id'];
322 $this->assign('edit_cid_id', $edit_cid_id);
323
324 $this->display('photo_set.htm');
325 }else{
326 $id = intval(R('id', 'P'));
327 $cid = intval(R('cid', 'P'));
328 $title = trim(strip_tags(R('title', 'P')));
329 $alias = trim(R('alias', 'P'));
330 $flags = (array)R('flag', 'P');
331 $views = intval(R('views', 'P'));
332 $images = (array)R('images', 'P');
333 $contentstr = trim(R('content', 'P'));
334 $intro = trim(R('intro', 'P'));
335 $isremote = intval(R('isremote', 'P'));
336 $pic = trim(R('pic', 'P'));
337 $uid = $this->_user['uid'];
338
339 empty($id) && E(1, 'ID不能为空!');
340 empty($cid) && E(1, '亲,您没有选择分类哦!');
341 empty($title) && E(1, '亲,您的标题忘了填哦!');
342 empty($images) && E(1, '亲,您的图集忘上传图片了!');
343
344 $categorys = $this->category->read($cid);
345 if(empty($categorys)) E(1, '分类ID不存在!');
346
347 $mid = $this->category->get_mid_by_cid($cid);
348 $table = $this->models->get_table($mid);
349
350 // 防止提交到其他模型的分类
351 if($table != 'photo') E(1, '分类ID非法!');
352
353 $this->cms_content->table = 'cms_'.$table;
354 $data = $this->cms_content->get($id);
355 if(empty($data)) E(1, '内容不存在!');
356
357 // 检测别名是否能用
358 $alias_old = $data['alias'];
359 if($alias && $alias != $alias_old && $err_msg = $this->only_alias->check_alias($alias)) {
360 E(1, $err_msg);
361 }
362
363 // 比较属性变化
364 $flags_old = array();
365 if($data['flags']) {
366 $flags_old = explode(',', $data['flags']);
367 foreach($flags as $flag) {
368 $key = array_search($flag, $flags_old);
369 if($key !== false) unset($flags_old[$key]);
370 }
371 }
372
373 // 比较标签变化
374 $tags = trim(R('tags', 'P'), ", \t\n\r\0\x0B");
375 $tags_new = explode(',', $tags);
376 $tags_old = (array)_json_decode($data['tags']);
377 $tags_arr = $tags = array();
378 foreach($tags_new as $tagname) {
379 $key = array_search($tagname, $tags_old);
380 if($key === false) {
381 $tags_arr[] = $tagname;
382 }else{
383 $tags[$key] = $tagname;
384 unset($tags_old[$key]);
385 }
386 }
387
388 // 标签预处理,最多支持5个标签
389 $this->cms_content_tag->table = 'cms_'.$table.'_tag';
390 $tagdatas = array();
391 for($i=0; isset($tags_arr[$i]) && $i<5; $i++) {
392 $name = trim($tags_arr[$i]);
393 if($name) {
394 $tagdata = $this->cms_content_tag->find_fetch(array('name'=>$name), array(), 0, 1);
395 if($tagdata) {
396 $tagdata = current($tagdata);
397 }else{
398 $tagid = $this->cms_content_tag->create(array('name'=>$name, 'count'=>0, 'content'=>''));
399 if(!$tagid) E(1, '写入标签表出错');
400 $tagdata = $this->cms_content_tag->get($tagid);
401 }
402
403 $tagdata['count']++;
404 $tagdatas[] = $tagdata;
405 $tags[$tagdata['tagid']] = $tagdata['name'];
406 }
407 }
408
409 // 远程图片本地化
410 $endstr = '';
411 $this->cms_content_attach->table = 'cms_'.$table.'_attach';
412 if($isremote) {
413 $endstr .= $this->get_remote_img($table, $contentstr, $uid, $cid, $id);
414 }
415
416 // 计算图片数,和非图片文件数
417 $imagenum = $this->cms_content_attach->find_count(array('id'=>$id, 'uid'=>$uid, 'isimage'=>1));
418 $filenum = $this->cms_content_attach->find_count(array('id'=>$id, 'uid'=>$uid, 'isimage'=>0));
419
420 // 如果缩略图为空,并且内容含有图片,则将第一张图片设置为缩略图
421 if(empty($pic) && $imagenum) {
422 $pic = $this->auto_pic($table, $uid, $id = 0);
423 }
424
425 // 如果摘要为空,自动生成摘要
426 $intro = $this->auto_intro($intro, $contentstr);
427
428 // 如果分类ID发生变化,更新分类内容数
429 if($cid != $data['cid']) {
430 // 旧的分类内容数减1
431 $categorys_old = $this->category->read($data['cid']);
432 $categorys_old['count'] = max(0, $categorys_old['count']-1);
433 $this->category->update($categorys_old);
434
435 // 新的分类内容数加1
436 $categorys['count']++;
437 $this->category->update($categorys);
438
439 $this->category->delete_cache();
440 }
441
442 // 编辑时,别名有三种情况需要处理
443 if($alias && $alias_old && $alias != $alias_old) {
444 // 写入新别名
445 if(!$this->only_alias->set($alias, array('mid' => $mid, 'cid' => $cid, 'id' => $id))) {
446 E(1, '写入全站唯一别名表出错');
447 }
448
449 // 删除旧别名
450 if(!$this->only_alias->delete($alias_old)) {
451 E(1, '删除别名表数据时出错');
452 }
453 }elseif($alias && empty($alias_old)) {
454 // 写入新别名
455 if(!$this->only_alias->set($alias, array('mid' => $mid, 'cid' => $cid, 'id' => $id))) {
456 E(1, '写入全站唯一别名表出错');
457 }
458 }elseif(empty($alias) && $alias_old) {
459 // 删除旧别名
460 if(!$this->only_alias->delete($alias_old)) {
461 E(1, '删除别名表数据时出错');
462 }
463 }
464
465 // 写入内容表
466 $data['cid'] = $cid;
467 $data['id'] = $id;
468 $data['title'] = $title;
469 $data['color'] = trim(R('color', 'P'));
470 $data['alias'] = $alias;
471 $data['tags'] = _json_encode($tags);
472 $data['intro'] = $intro;
473 $data['pic'] = $pic;
474 $data['uid'] = $uid;
475 $data['author'] = trim(R('author', 'P')); // 可以不等于发布用户
476 $data['source'] = trim(R('source', 'P'));
477 $data['dateline'] = strtotime(trim(R('dateline', 'P')));
478 $data['lasttime'] = $_ENV['_time'];
479 $data['iscomment'] = intval(R('iscomment', 'P'));
480 $data['imagenum'] = $imagenum;
481 $data['filenum'] = $filenum;
482 $data['flags'] = implode(',', $flags);
483 $data['seo_title'] = trim(strip_tags(R('seo_title', 'P')));
484 $data['seo_keywords'] = trim(strip_tags(R('seo_keywords', 'P')));
485 $data['seo_description'] = trim(strip_tags(R('seo_description', 'P')));
486 if(!$this->cms_content->update($data)) {
487 E(1, '更新内容表出错');
488 }
489
490 // 写入内容数据表
491 $this->cms_content_data->table = 'cms_'.$table.'_data';
492 if(!$this->cms_content_data->set($id, array('content' => $contentstr, 'images' => json_encode($images)))) {
493 E(1, '写入内容数据表出错');
494 }
495
496 // 写入内容查看数表
497 $this->cms_content_views->table = 'cms_'.$table.'_views';
498 if(!$this->cms_content_views->set($id, array('cid' => $cid, 'views' => $views))) {
499 E(1, '写入内容查看数表出错');
500 }
501
502 // 写入内容属性标记表
503 $this->cms_content_flag->table = 'cms_'.$table.'_flag';
504 foreach($flags as $flag) {
505 if(!$this->cms_content_flag->set(array($flag, $id), array('cid'=>$cid))) {
506 E(1, '写入内容属性标记表出错');
507 }
508 }
509
510 // 如果内容含有图片附件,则标记图片属性,否则删除图片属性
511 if($imagenum) {
512 $this->cms_content_flag->set(array(0, $id), array('cid'=>$cid));
513 }else{
514 $this->cms_content_flag->delete(0, $id);
515 }
516
517 // 删除去掉的属性
518 foreach($flags_old as $flag) {
519 $flag = intval($flag);
520 if($flag) $this->cms_content_flag->delete($flag, $id);
521 }
522
523 // 写入内容标签表
524 $this->cms_content_tag_data->table = 'cms_'.$table.'_tag_data';
525 foreach($tagdatas as $tagdata) {
526 $this->cms_content_tag->update($tagdata);
527 $this->cms_content_tag_data->set(array($tagdata['tagid'], $id), array('id'=>$id));
528 }
529
530 // 删除不用的标签
531 foreach($tags_old as $tagid => $tagname) {
532 $tagdata = $this->cms_content_tag->get($tagid);
533 $tagdata['count']--;
534 $this->cms_content_tag->update($tagdata);
535 $this->cms_content_tag_data->delete($tagid, $id);
536 }
537
538 // hook admin_photo_control_edit_after.php
539
540 E(0, '编辑完成'.$endstr);
541 }
542 }
543
544 // 删除图集
545 public function del() {
546 // hook admin_photo_control_del_before.php
547
548 $id = (int) R('id', 'P');
549 $cid = (int) R('cid', 'P');
550
551 empty($id) && E(1, '内容ID不能为空!');
552 empty($cid) && E(1, '分类ID不能为空!');
553
554 // hook admin_photo_control_del_after.php
555
556 $err = $this->cms_content->xdelete('photo', $id, $cid);
557 if($err) {
558 E(1, $err);
559 }else{
560 E(0, '删除成功!');
561 }
562 }
563
564 // 批量删除图集
565 public function batch_del() {
566 // hook admin_photo_control_batch_del_before.php
567
568 $id_arr = R('id_arr', 'P');
569 if(!empty($id_arr) && is_array($id_arr)) {
570 $err_num = 0;
571 foreach($id_arr as $v) {
572 $err = $this->cms_content->xdelete('photo', $v[0], $v[1]);
573 if($err) $err_num++;
574 }
575
576 if($err_num) {
577 E(1, $err_num.' 篇图集删除失败!');
578 }else{
579 E(0, '删除成功!');
580 }
581 }else{
582 E(1, '参数不能为空!');
583 }
584 }
585
586 // 删除单个附件
587 public function del_attach() {
588 // hook admin_photo_control_del_attach_before.php
589
590 $aid = (int) R('aid', 'P');
591
592 empty($aid) && E(1, 'AID不能为空!');
593
594 // hook admin_photo_control_del_attach_after.php
595
596 $this->cms_content_attach->table = 'cms_photo_attach';
597 if($this->cms_content_attach->xdelete($aid)) {
598 E(0, '删除成功!');
599 }else{
600 E(1, '删除失败!');
601 }
602 }
603
604 // 单独保存图集
605 public function save_images() {
606 $id = intval(R('id', 'P'));
607 $images = (array)R('images', 'P');
608
609 empty($id) && E(1, 'ID不能为空!');
610 empty($images) && E(1, '亲,您的图集忘上传图片了!');
611
612 // 写入内容数据表
613 $this->cms_content_data->table = 'cms_photo_data';
614 $data = $this->cms_content_data->read($id);
615 if(empty($data)) {
616 E(1, '内容不存在!');
617 }
618 $data['images'] = json_encode($images);
619 if($this->cms_content_data->set($id, $data)) {
620 E(0, '保存成功!');
621 }else{
622 E(1, '写入内容数据表出错!');
623 }
624 }
625
626 // 自动保存图集
627 public function auto_save() {
628 $this->kv->set('auto_save_photo_uid_'.$this->_user['uid'], $_POST) ? E(0, '自动保存成功!') : E(1, '自动保存失败!');
629 }
630
631 // 自动生成摘要
632 private function auto_intro($intro, &$content) {
633 if(empty($intro)) {
634 $intro = preg_replace('/\s{2,}/', ' ', strip_tags($content));
635 return trim(utf8::cutstr_cn($intro, 255, ''));
636 }else{
637 return str_replace(array("\r\n", "\r", "\n"), '<br />', strip_tags($intro));
638 }
639 }
640
641 // 自动生成缩略图
642 private function auto_pic($table, $uid, $id = 0) {
643 $pic_arr = $this->cms_content_attach->find_fetch(array('id'=>$id, 'uid'=>$uid, 'isimage'=>1), array(), 0, 1);
644 $pic_arr = current($pic_arr);
645 $cfg = $this->runtime->xget();
646 $path = 'upload/'.$table.'/'.$pic_arr['filepath'];
647 $pic = image::thumb_name($path);
648 $src_file = TWCMS_PATH.$path;
649 $dst_file = TWCMS_PATH.$pic;
650 if(!is_file($dst_file)) {
651 image::thumb($src_file, $dst_file, $cfg['thumb_'.$table.'_w'], $cfg['thumb_'.$table.'_h'], $cfg['thumb_type'], $cfg['thumb_quality']);
652 }
653 return $pic;
654 }
655
656 // 获取远程图片
657 private function get_remote_img($table, &$content, $uid, $cid = 0, $id = 0) {
658 function_exists('set_time_limit') && set_time_limit(0);
659 $cfg = $this->runtime->xget();
660 $updir = 'upload/'.$table.'/';
661 $_ENV['_prc_err'] = 0;
662 $_ENV['_prc_arg'] = array(
663 'hosts'=>array('127.0.0.1', 'localhost', $_SERVER['HTTP_HOST'], $cfg['webdomain']),
664 'uid'=>$uid,
665 'cid'=>$cid,
666 'id'=>$id,
667 'maxSize'=>10000,
668 'upDir'=>TWCMS_PATH.$updir,
669 'preUri'=>$cfg['weburl'].$updir,
670 'cfg'=>$cfg,
671 );
672 $content = preg_replace_callback('#\<img [^\>]*src=["\']((?:http|ftp)\://[^"\']+)["\'][^\>]*\>#iU', array($this, 'img_replace'), $content);
673 unset($_ENV['_prc_arg']);
674 return $_ENV['_prc_err'] ? ',但远程抓取图片失败 '.$_ENV['_prc_err'].' 张!' : '';
675 }
676
677 // 远程图片处理 (如果抓取失败则不替换)
678 // $conf 用到4个参数 hosts preUri cfg upDir
679 private function img_replace($mat) {
680 static $uris = array();
681 $uri = $mat[1];
682 $conf = &$_ENV['_prc_arg'];
683
684 // 排除重复保存相同URL图片
685 if(isset($uris[$uri])) return str_replace($uri, $uris[$uri], $mat[0]);
686
687 // 根据域名排除本站图片
688 $urls = parse_url($uri);
689 if(in_array($urls['host'], $conf['hosts'])) return $mat[0];
690
691 $file = $this->cms_content_attach->remote_down($uri, $conf);
692 if($file) {
693 $uris[$uri] = $conf['preUri'].$file;
694 $cfg = $conf['cfg'];
695
696 // 是否添加水印
697 if(!empty($cfg['watermark_pos'])) {
698 image::watermark($conf['upDir'].$file, TWCMS_PATH.'static/img/watermark.png', null, $cfg['watermark_pos'], $cfg['watermark_pct']);
699 }
700
701 return str_replace($uri, $uris[$uri], $mat[0]);
702 }else{
703 $_ENV['_prc_err']++;
704 return $mat[0];
705 }
706 }
707
708 // hook admin_photo_control_after.php
709}
710