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