dedecollection.func.php
18 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php if(!defined('DEDEINC')) exit('dedecms');
/**
* 采集小助手
*
* @version $Id: charset.helper.php 1 2010-07-05 11:43:09Z tianya $
* @package DedeCMS.Helpers
* @copyright Copyright (c) 2007 - 2010, DesDev, Inc.
* @license http://help.dedecms.com/usersguide/license.html
* @link http://www.dedecms.com
*/
require_once(DEDEINC."/dedehttpdown.class.php");
require_once(DEDEINC."/dedetag.class.php");
require_once(DEDEINC."/charset.func.php");
/**
* 下载图片
*
* @access public
* @param string $gurl 地址
* @param string $rfurl 来源地址
* @param string $filename 文件名
* @param string $gcookie 调整cookie
* @param string $JumpCount 跳转计数
* @param string $maxtime 最大次数
* @return string
*/
function DownImageKeep($gurl, $rfurl, $filename, $gcookie="", $JumpCount=0, $maxtime=30)
{
$urlinfos = GetHostInfo($gurl);
$ghost = trim($urlinfos['host']);
if($ghost=='')
{
return FALSE;
}
$gquery = $urlinfos['query'];
if($gcookie=="" && !empty($rfurl))
{
$gcookie = RefurlCookie($rfurl);
}
$sessionQuery = "GET $gquery HTTP/1.1\r\n";
$sessionQuery .= "Host: $ghost\r\n";
$sessionQuery .= "Referer: $rfurl\r\n";
$sessionQuery .= "Accept: */*\r\n";
$sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
if($gcookie!="" && !preg_match("/[\r\n]/", $gcookie))
{
$sessionQuery .= $gcookie."\r\n";
}
$sessionQuery .= "Connection: Keep-Alive\r\n\r\n";
$errno = "";
$errstr = "";
$m_fp = fsockopen($ghost, 80, $errno, $errstr,10);
fwrite($m_fp,$sessionQuery);
$lnum = 0;
//获取详细应答头
$m_httphead = Array();
$httpstas = explode(" ",fgets($m_fp,256));
$m_httphead["http-edition"] = trim($httpstas[0]);
$m_httphead["http-state"] = trim($httpstas[1]);
while(!feof($m_fp))
{
$line = trim(fgets($m_fp,256));
if($line == "" || $lnum>100)
{
break;
}
$hkey = "";
$hvalue = "";
$v = 0;
for($i=0; $i<strlen($line); $i++)
{
if($v==1)
{
$hvalue .= $line[$i];
}
if($line[$i]==":")
{
$v = 1;
}
if($v==0)
{
$hkey .= $line[$i];
}
}
$hkey = trim($hkey);
if($hkey!="")
{
$m_httphead[strtolower($hkey)] = trim($hvalue);
}
}
//分析返回记录
if(preg_match("/^3/", $m_httphead["http-state"]))
{
if(isset($m_httphead["location"]) && $JumpCount<3)
{
$JumpCount++;
DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount);
}
else
{
return FALSE;
}
}
if(!preg_match("/^2/", $m_httphead["http-state"]))
{
return FALSE;
}
if(!isset($m_httphead))
{
return FALSE;
}
$contentLength = $m_httphead['content-length'];
//保存文件
$fp = fopen($filename,"w") or die("写入文件:{$filename} 失败!");
$i=0;
$okdata = "";
$starttime = time();
while(!feof($m_fp))
{
$okdata .= fgetc($m_fp);
$i++;
//超时结束
if(time()-$starttime>$maxtime)
{
break;
}
//到达指定大小结束
if($i >= $contentLength)
{
break;
}
}
if($okdata!="")
{
fwrite($fp,$okdata);
}
fclose($fp);
if($okdata=="")
{
@unlink($filename);
fclose($m_fp);
return FALSE;
}
fclose($m_fp);
return TRUE;
}
/**
* 获得某页面返回的Cookie信息
*
* @access public
* @param string $gurl 调整地址
* @return string
*/
function RefurlCookie($gurl)
{
global $gcookie,$lastRfurl;
$gurl = trim($gurl);
if(!empty($gcookie) && $lastRfurl==$gurl)
{
return $gcookie;
}
else
{
$lastRfurl=$gurl;
}
if(trim($gurl)=='')
{
return '';
}
$urlinfos = GetHostInfo($gurl);
$ghost = $urlinfos['host'];
$gquery = $urlinfos['query'];
$sessionQuery = "GET $gquery HTTP/1.1\r\n";
$sessionQuery .= "Host: $ghost\r\n";
$sessionQuery .= "Accept: */*\r\n";
$sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
$sessionQuery .= "Connection: Close\r\n\r\n";
$errno = "";
$errstr = "";
$m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.'<br />');
fwrite($m_fp,$sessionQuery);
$lnum = 0;
//获取详细应答头
$gcookie = "";
while(!feof($m_fp))
{
$line = trim(fgets($m_fp,256));
if($line == "" || $lnum>100)
{
break;
}
else
{
if(preg_match("/^cookie/i", $line))
{
$gcookie = $line;
break;
}
}
}
fclose($m_fp);
return $gcookie;
}
/**
* 获得网址的host和query部份
*
* @access public
* @param string $gurl 调整地址
* @return string
*/
function GetHostInfo($gurl)
{
$gurl = preg_replace("/^http:\/\//i", "", trim($gurl));
$garr['host'] = preg_replace("/\/(.*)$/i", "", $gurl);
$garr['query'] = "/".preg_replace("/^([^\/]*)\//i", "", $gurl);
return $garr;
}
/**
* HTML里的图片转DEDE格式
*
* @access public
* @param string $body 文章内容
* @return string
*/
function TurnImageTag(&$body)
{
global $cfg_album_width,$cfg_ddimg_width;
if(empty($cfg_album_width))
{
$cfg_album_width = 800;
}
if(empty($cfg_ddimg_width))
{
$cfg_ddimg_width = 150;
}
$patten = "/<\\s*img\\s.*?src\\s*=\\s*([\"\\'])?(?(1)(.*?)\\1|([^\\s\\>\"\\']+))/isx";
preg_match_all($patten,$body,$images);
$returnArray1 = $images[2];
$returnArray2 = $images[3];
foreach ( $returnArray1 as $key => $value )
{
if ($value)
{
$ttx .= "{dede:img ddimg='$litpicname' text='图 ".($key+1)."'}".$value."{/dede:img}"."\r\n";
}
else
{
$ttx .= "{dede:img ddimg='$litpicname' text='图 ".($key+1)."'}".$returnArray2[$key]."{/dede:img}"."\r\n";
}
}
$ttx = "\r\n{dede:pagestyle maxwidth='{$cfg_album_width}' ddmaxwidth='{$cfg_ddimg_width}' row='3' col='3' value='2'/}\r\n{dede:comments}图集类型会采集时生成此配置是正常的,不过如果后面没有跟着img标记则表示规则无效{/dede:comments}\r\n".$ttx;
return $ttx;
}
/**
* HTML里的网址格式转换
*
* @access public
* @param string $body 文章内容
* @return string
*/
function TurnLinkTag(&$body)
{
$ttx = '';
$handid = '服务器';
preg_match_all("/<a href=['\"](.+?)['\"]([^>]+?)>(.+?)<\/a>/is",$body,$match);
if(is_array($match[1]) && count($match[1])>0)
{
for($i=0;isset($match[1][$i]);$i++)
{
$servername = (isset($match[3][$i]) ? str_replace("'","`",$match[3][$i]) : $handid.($i+1));
if(preg_match("/[<>]/", $servername) || strlen($servername)>40)
{
$servername = $handid.($i+1);
}
$ttx .= "{dede:link text='$servername'} {$match[1][$i]} {/dede:link}\r\n";
}
}
return $ttx;
}
/**
* 替换XML的CDATA
*
* @access public
* @param string $str 字符串
* @return string
*/
function RpCdata($str)
{
$str = str_replace('<![CDATA[', '', $str);
$str = str_replace(']]>', '', $str);
return $str;
}
/**
* 分析RSS里的链接
*
* @access public
* @param string $rssurl rss地址
* @return string
*/
function GetRssLinks($rssurl)
{
global $cfg_soft_lang;
$dhd = new DedeHttpDown();
$dhd->OpenUrl($rssurl);
$rsshtml = $dhd->GetHtml();
//分析编码
preg_match("/encoding=[\"']([^\"']*)[\"']/is",$rsshtml,$infos);
if(isset($infos[1]))
{
$pcode = strtolower(trim($infos[1]));
}
else
{
$pcode = strtolower($cfg_soft_lang);
}
if($cfg_soft_lang=='gb2312')
{
if($pcode=='utf-8')
{
$rsshtml = utf82gb($rsshtml);
}
else if($pcode=='big5')
{
$rsshtml = big52gb($rsshtml);
}
}
else if($cfg_soft_lang=='utf-8')
{
if($pcode=='gbk'||$pcode=='gb2312')
{
$rsshtml = gb2utf8($rsshtml);
}
else if($pcode=='big5')
{
$rsshtml = gb2utf8(big52gb($rsshtml));
}
}
$rsarr = array();
preg_match_all("/<item(.*)<title>(.*)<\/title>/isU",$rsshtml,$titles);
preg_match_all("/<item(.*)<link>(.*)<\/link>/isU",$rsshtml,$links);
preg_match_all("/<item(.*)<description>(.*)<\/description>/isU",$rsshtml,$descriptions);
if(!isset($links[2]))
{
return '';
}
foreach($links[2] as $k=>$v)
{
$rsarr[$k]['link'] = RpCdata($v);
if(isset($titles[2][$k]))
{
$rsarr[$k]['title'] = RpCdata($titles[2][$k]);
}
else
{
$rsarr[$k]['title'] = preg_replace("/^(.*)\//i", "", RpCdata($titles[2][$k]));
}
if(isset($descriptions[2][$k]))
{
$rsarr[$k]['image'] = GetddImgFromRss($descriptions[2][$k],$rssurl);
}
else
{
$rsarr[$k]['image'] = '';
}
}
return $rsarr;
}
/**
* 从RSS摘要获取图片信息
*
* @access public
* @param string $descriptions 描述
* @param string $refurl 来源地址
* @return string
*/
function GetddImgFromRss($descriptions,$refurl)
{
if($descriptions=='')
{
return '';
}
preg_match_all("/<img(.*)src=[\"']{0,1}(.*)[\"']{0,1}[> \r\n\t]{1,}/isU",$descriptions,$imgs);
if(isset($imgs[2][0]))
{
$imgs[2][0] = preg_replace("/[\"']/", '', $imgs[2][0]);
$imgs[2][0] = preg_replace("/\/{1,}/", '/', $imgs[2][0]);
return FillUrl($refurl,$imgs[2][0]);
}
else
{
return '';
}
}
/**
* 补全网址
*
* @access public
* @param string $refurl 来源地址
* @param string $surl 站点地址
* @return string
*/
function FillUrl($refurl,$surl)
{
$i = $pathStep = 0;
$dstr = $pstr = $okurl = '';
$refurl = trim($refurl);
$surl = trim($surl);
$urls = @parse_url($refurl);
$basehost = ( (!isset($urls['port']) || $urls['port']=='80') ? $urls['host'] : $urls['host'].':'.$urls['port']);
//$basepath = $basehost.(!isset($urls['path']) ? '' : '/'.$urls['path']);
//由于直接获得的path在处理 http://xxxx/nnn/aaa?fdsafd 这种情况时会有错误,因此用其它方式处理
$basepath = $basehost;
$paths = explode('/',preg_replace("/^http:\/\//i", "", $refurl));
$n = count($paths);
for($i=1;$i < ($n-1);$i++)
{
if(!preg_match("/[\?]/", $paths[$i])) $basepath .= '/'.$paths[$i];
}
if(!preg_match("/[\?\.]/", $paths[$n-1]))
{
$basepath .= '/'.$paths[$n-1];
}
if($surl=='')
{
return $basepath;
}
$pos = strpos($surl, "#");
if($pos>0)
{
$surl = substr($surl, 0, $pos);
}
//用 '/' 表示网站根的网址
if($surl[0]=='/')
{
$okurl = $basehost.$surl;
}
else if($surl[0]=='.')
{
if(strlen($surl)<=2)
{
return '';
}
else if($surl[1]=='/')
{
$okurl = $basepath.preg_replace('/^./', '', $surl);
}
else
{
$okurl = $basepath.'/'.$surl;
}
}
else
{
if( strlen($surl) < 7 )
{
$okurl = $basepath.'/'.$surl;
}
else if( preg_match("/^http:\/\//i",$surl) )
{
$okurl = $surl;
}
else
{
$okurl = $basepath.'/'.$surl;
}
}
$okurl = preg_replace("/^http:\/\//i", '', $okurl);
$okurl = 'http://'.preg_replace("/\/{1,}/", '/', $okurl);
return $okurl;
}
/**
* 从匹配规则中获取列表网址
*
* @access public
* @param string $regxurl 正则地址
* @param string $handurl 操作地址
* @param string $startid 开始ID
* @param string $endid 结束ID
* @param string $addv 增值
* @param string $usemore 使用更多
* @param string $batchrule 列表规则
* @return string
*/
function GetUrlFromListRule($regxurl='',$handurl='',$startid=0,$endid=0,$addv=1,$usemore=0,$batchrule='')
{
global $dsql,$islisten;
$lists = array();
$n = 0;
$islisten = (empty($islisten) ? 0 : $islisten);
if($handurl!='')
{
$handurls = explode("\n",$handurl);
foreach($handurls as $handurl)
{
$handurl = trim($handurl);
if(preg_match("/^http:\/\//i", $handurl))
{
$lists[$n][0] = $handurl;
$lists[$n][1] = 0;
$n++;
if($islisten==1)
{
break;
}
}
}
}
if($regxurl!='')
{
//没指定(#)和(*)
if(!preg_match("/\(\*\)/i", $regxurl) && !preg_match("/\(#\)/", $regxurl))
{
$lists[$n][0] = $regxurl;
$lists[$n][1] = 0;
$n++;
}
else
{
if($addv <= 0)
{
$addv = 1;
}
//没指定多栏目匹配规则
if($usemore==0)
{
while($startid <= $endid)
{
$lists[$n][0] = str_replace("(*)",sprintf('%0'.strlen($startid).'d',$startid),$regxurl);
$lists[$n][1] = 0;
$startid = sprintf('%0'.strlen($startid).'d',$startid + $addv);
$n++;
if($n>2000 || $islisten==1)
{
break;
}
}
}
//匹配多个栏目
//规则表达式 [(#)=>(#)匹配的网址; (*)=>(*)的范围,如:1-20; typeid=>栏目id; addurl=>附加的网址(用|分开多个)]
else
{
$nrules = explode(']',trim($batchrule));
foreach($nrules as $nrule)
{
$nrule = trim($nrule);
$nrule = preg_replace("/^\[|\]$/", '', $nrule);
$nrules = explode(';',$nrule);
if(count($nrules)<3)
{
continue;
}
$brtag = '';
$startid = 0;
$endid = 0;
$typeid = 0;
$addurls = array();
foreach($nrules as $nrule)
{
$nrule = trim($nrule);
list($k,$v) = explode('=>',$nrule);
if(trim($k)=='(#)')
{
$brtag = trim($v);
}
else if(trim($k)=='typeid')
{
$typeid = trim($v);
}
else if(trim($k)=='addurl')
{
$addurl = trim($v);
$addurls = explode('|',$addurl);
}
else if(trim($k)=='(*)')
{
$v = preg_replace("/[ \r\n\t]/", '', trim($v));
list($startid,$endid) = explode('-',$v);
}
}
//如果栏目用栏目名称
if(preg_match('/[^0-9]/', $typeid))
{
$arr = $dsql->GetOne("SELECT id FROM `#@__arctype` WHERE typename LIKE '$typeid' ");
if(is_array($arr))
{
$typeid = $arr['id'];
}
else
{
$typeid = 0;
}
}
//附加网址优先
$mjj = 0;
if(isset($addurls[0]))
{
foreach($addurls as $addurl)
{
$addurl = trim($addurl);
if($addurl=='')
{
continue;
}
$lists[$n][0] = $addurl;
$lists[$n][1] = $typeid;
$n++;
$mjj++;
if($islisten==1)
{
break;
}
}
}
//如果为非监听模式或监听模式没手工指定的附加网址
if($islisten!=1 || $mjj==0 )
{
//匹配规则里的网址,注:(#)的网址是是允许使用(*)的
while($startid <= $endid)
{
$lists[$n][0] = str_replace("(#)",$brtag,$regxurl);
$lists[$n][0] = str_replace("(*)",sprintf('%0'.strlen($startid).'d',$startid),$lists[$n][0]);
$lists[$n][1] = $typeid;
$startid = sprintf('%0'.strlen($startid).'d',$startid + $addv);
$n++;
if($islisten==1)
{
break;
}
if($n>20000)
{
break;
}
}
}
}
} //End 匹配多栏目
} //End使用规则匹配的情况
}
return $lists;
}//End