PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/uploads/include/dedecollection.func.php

http://pj-photohost.googlecode.com/
PHP | 584 lines | 444 code | 5 blank | 135 comment | 14 complexity | 557c274354c113a871b69d21d64691a5 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if(!defined('DEDEINC'))
  3. {
  4. exit('dedecms');
  5. }
  6. require_once(DEDEINC."/dedehttpdown.class.php");
  7. require_once(DEDEINC."/dedetag.class.php");
  8. require_once(DEDEINC."/charset.func.php");
  9. function DownImageKeep($gurl,$rfurl,$filename,$gcookie="",$JumpCount=0,$maxtime=30)
  10. {
  11. $urlinfos = GetHostInfo($gurl);
  12. $ghost = trim($urlinfos['host']);
  13. if($ghost=='')
  14. {
  15. return false;
  16. }
  17. $gquery = $urlinfos['query'];
  18. if($gcookie=="" && !empty($rfurl))
  19. {
  20. $gcookie = RefurlCookie($rfurl);
  21. }
  22. $sessionQuery = "GET $gquery HTTP/1.1\r\n";
  23. $sessionQuery .= "Host: $ghost\r\n";
  24. $sessionQuery .= "Referer: $rfurl\r\n";
  25. $sessionQuery .= "Accept: */*\r\n";
  26. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
  27. if($gcookie!=""&&!ereg("[\r\n]",$gcookie))
  28. {
  29. $sessionQuery .= $gcookie."\r\n";
  30. }
  31. $sessionQuery .= "Connection: Keep-Alive\r\n\r\n";
  32. $errno = "";
  33. $errstr = "";
  34. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10);
  35. fwrite($m_fp,$sessionQuery);
  36. $lnum = 0;
  37. //???????
  38. $m_httphead = Array();
  39. $httpstas = explode(" ",fgets($m_fp,256));
  40. $m_httphead["http-edition"] = trim($httpstas[0]);
  41. $m_httphead["http-state"] = trim($httpstas[1]);
  42. while(!feof($m_fp))
  43. {
  44. $line = trim(fgets($m_fp,256));
  45. if($line == "" || $lnum>100)
  46. {
  47. break;
  48. }
  49. $hkey = "";
  50. $hvalue = "";
  51. $v = 0;
  52. for($i=0;$i<strlen($line);$i++)
  53. {
  54. if($v==1)
  55. {
  56. $hvalue .= $line[$i];
  57. }
  58. if($line[$i]==":")
  59. {
  60. $v = 1;
  61. }
  62. if($v==0)
  63. {
  64. $hkey .= $line[$i];
  65. }
  66. }
  67. $hkey = trim($hkey);
  68. if($hkey!="")
  69. {
  70. $m_httphead[strtolower($hkey)] = trim($hvalue);
  71. }
  72. }
  73. //??????
  74. if(ereg("^3",$m_httphead["http-state"]))
  75. {
  76. if(isset($m_httphead["location"]) && $JumpCount<3)
  77. {
  78. $JumpCount++;
  79. DownImageKeep($gurl,$rfurl,$filename,$gcookie,$JumpCount);
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. if(!ereg("^2",$m_httphead["http-state"]))
  87. {
  88. return false;
  89. }
  90. if(!isset($m_httphead))
  91. {
  92. return false;
  93. }
  94. $contentLength = $m_httphead['content-length'];
  95. //????
  96. $fp = fopen($filename,"w") or die("?????{$filename} ???");
  97. $i=0;
  98. $okdata = "";
  99. $starttime = time();
  100. while(!feof($m_fp))
  101. {
  102. $okdata .= fgetc($m_fp);
  103. $i++;
  104. //????
  105. if(time()-$starttime>$maxtime)
  106. {
  107. break;
  108. }
  109. //????????
  110. if($i >= $contentLength)
  111. {
  112. break;
  113. }
  114. }
  115. if($okdata!="")
  116. {
  117. fwrite($fp,$okdata);
  118. }
  119. fclose($fp);
  120. if($okdata=="")
  121. {
  122. @unlink($filename);
  123. fclose($m_fp);
  124. return false;
  125. }
  126. fclose($m_fp);
  127. return true;
  128. }
  129. //????????Cookie??
  130. function RefurlCookie($gurl)
  131. {
  132. global $gcookie,$lastRfurl;
  133. $gurl = trim($gurl);
  134. if(!empty($gcookie) && $lastRfurl==$gurl)
  135. {
  136. return $gcookie;
  137. }
  138. else
  139. {
  140. $lastRfurl=$gurl;
  141. }
  142. if(trim($gurl)=='')
  143. {
  144. return '';
  145. }
  146. $urlinfos = GetHostInfo($gurl);
  147. $ghost = $urlinfos['host'];
  148. $gquery = $urlinfos['query'];
  149. $sessionQuery = "GET $gquery HTTP/1.1\r\n";
  150. $sessionQuery .= "Host: $ghost\r\n";
  151. $sessionQuery .= "Accept: */*\r\n";
  152. $sessionQuery .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n";
  153. $sessionQuery .= "Connection: Close\r\n\r\n";
  154. $errno = "";
  155. $errstr = "";
  156. $m_fp = fsockopen($ghost, 80, $errno, $errstr,10) or die($ghost.'<br />');
  157. fwrite($m_fp,$sessionQuery);
  158. $lnum = 0;
  159. //???????
  160. $gcookie = "";
  161. while(!feof($m_fp))
  162. {
  163. $line = trim(fgets($m_fp,256));
  164. if($line == "" || $lnum>100)
  165. {
  166. break;
  167. }
  168. else
  169. {
  170. if(eregi("^cookie",$line))
  171. {
  172. $gcookie = $line;
  173. break;
  174. }
  175. }
  176. }
  177. fclose($m_fp);
  178. return $gcookie;
  179. }
  180. //?????host?query??
  181. function GetHostInfo($gurl)
  182. {
  183. $gurl = eregi_replace("^http://","",trim($gurl));
  184. $garr['host'] = eregi_replace("/(.*)$","",$gurl);
  185. $garr['query'] = "/".eregi_replace("^([^/]*)/","",$gurl);
  186. return $garr;
  187. }
  188. //HTML?????DEDE??
  189. function TurnImageTag(&$body)
  190. {
  191. global $cfg_album_width,$cfg_ddimg_width;
  192. if(empty($cfg_album_width))
  193. {
  194. $cfg_album_width = 800;
  195. }
  196. if(empty($cfg_ddimg_width))
  197. {
  198. $cfg_ddimg_width = 150;
  199. }
  200. preg_match_all('/src=[\'"](.+?)[\'"]/is',$body,$match);
  201. $ttx = '';
  202. if(is_array($match[1]) && count($match[1])>0)
  203. {
  204. for($i=0;isset($match[1][$i]);$i++)
  205. {
  206. $ttx .= "{dede:img text='' }".$match[1][$i]." {/dede:img}"."\r\n";
  207. }
  208. }
  209. $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;
  210. return $ttx;
  211. }
  212. //HTML????????
  213. function TurnLinkTag(&$body)
  214. {
  215. $ttx = '';
  216. $handid = '???';
  217. preg_match_all("/<a href=['\"](.+?)['\"]([^>]+?)>(.+?)<\/a>/is",$body,$match);
  218. if(is_array($match[1]) && count($match[1])>0)
  219. {
  220. for($i=0;isset($match[1][$i]);$i++)
  221. {
  222. $servername = (isset($match[3][$i]) ? str_replace("'","`",$match[3][$i]) : $handid.($i+1));
  223. if(ereg("[<>]",$servername) || strlen($servername)>40)
  224. {
  225. $servername = $handid.($i+1);
  226. }
  227. $ttx .= "{dede:link text='$servername'} {$match[1][$i]} {/dede:link}\r\n";
  228. }
  229. }
  230. return $ttx;
  231. }
  232. //??XML?CDATA
  233. function RpCdata($str)
  234. {
  235. $str = str_replace('<![CDATA[','',$str);
  236. $str = str_replace(']]>','',$str);
  237. return $str;
  238. }
  239. //??RSS????
  240. function GetRssLinks($rssurl)
  241. {
  242. global $cfg_soft_lang;
  243. $dhd = new DedeHttpDown();
  244. $dhd->OpenUrl($rssurl);
  245. $rsshtml = $dhd->GetHtml();
  246. //????
  247. preg_match("/encoding=[\"']([^\"']*)[\"']/is",$rsshtml,$infos);
  248. if(isset($infos[1]))
  249. {
  250. $pcode = strtolower(trim($infos[1]));
  251. }
  252. else
  253. {
  254. $pcode = strtolower($cfg_soft_lang);
  255. }
  256. if($cfg_soft_lang=='gb2312')
  257. {
  258. if($pcode=='utf-8')
  259. {
  260. $rsshtml = utf82gb($rsshtml);
  261. }
  262. else if($pcode=='big5')
  263. {
  264. $rsshtml = big52gb($rsshtml);
  265. }
  266. }
  267. else if($cfg_soft_lang=='utf-8')
  268. {
  269. if($pcode=='gbk'||$pcode=='gb2312')
  270. {
  271. $rsshtml = gb2utf8($rsshtml);
  272. }
  273. else if($pcode=='big5')
  274. {
  275. $rsshtml = gb2utf8(big52gb($rsshtml));
  276. }
  277. }
  278. $rsarr = array();
  279. preg_match_all("/<item(.*)<title>(.*)<\/title>/isU",$rsshtml,$titles);
  280. preg_match_all("/<item(.*)<link>(.*)<\/link>/isU",$rsshtml,$links);
  281. preg_match_all("/<item(.*)<description>(.*)<\/description>/isU",$rsshtml,$descriptions);
  282. if(!isset($links[2]))
  283. {
  284. return '';
  285. }
  286. foreach($links[2] as $k=>$v)
  287. {
  288. $rsarr[$k]['link'] = RpCdata($v);
  289. if(isset($titles[2][$k]))
  290. {
  291. $rsarr[$k]['title'] = RpCdata($titles[2][$k]);
  292. }
  293. else
  294. {
  295. $rsarr[$k]['title'] = ereg_replace("^(.*)/","",RpCdata($titles[2][$k]));
  296. }
  297. if(isset($descriptions[2][$k]))
  298. {
  299. $rsarr[$k]['image'] = GetddImgFromRss($descriptions[2][$k],$rssurl);
  300. }
  301. else
  302. {
  303. $rsarr[$k]['image'] = '';
  304. }
  305. }
  306. return $rsarr;
  307. }
  308. //?RSS????????
  309. function GetddImgFromRss($descriptions,$refurl)
  310. {
  311. if($descriptions=='')
  312. {
  313. return '';
  314. }
  315. preg_match_all("/<img(.*)src=[\"']{0,1}(.*)[\"']{0,1}[> \r\n\t]{1,}/isU",$descriptions,$imgs);
  316. if(isset($imgs[2][0]))
  317. {
  318. $imgs[2][0] = ereg_replace("[\"']",'',$imgs[2][0]);
  319. $imgs[2][0] = ereg_replace("/{1,}",'/',$imgs[2][0]);
  320. return FillUrl($refurl,$imgs[2][0]);
  321. }
  322. else
  323. {
  324. return '';
  325. }
  326. }
  327. //????
  328. function FillUrl($refurl,$surl)
  329. {
  330. $i = $pathStep = 0;
  331. $dstr = $pstr = $okurl = '';
  332. $refurl = trim($refurl);
  333. $surl = trim($surl);
  334. $urls = @parse_url($refurl);
  335. $basehost = ( (!isset($urls['port']) || $urls['port']=='80') ? $urls['host'] : $urls['host'].':'.$urls['port']);
  336. //$basepath = $basehost.(!isset($urls['path']) ? '' : '/'.$urls['path']);
  337. //???????path??? http://xxxx/nnn/aaa?fdsafd ???????????????????
  338. $basepath = $basehost;
  339. $paths = explode('/',eregi_replace("^http://","",$refurl));
  340. $n = count($paths);
  341. for($i=1;$i < ($n-1);$i++)
  342. {
  343. if(!ereg("[\?]",$paths[$i])) $basepath .= '/'.$paths[$i];
  344. }
  345. if(!ereg("[\?\.]",$paths[$n-1]))
  346. {
  347. $basepath .= '/'.$paths[$n-1];
  348. }
  349. if($surl=='')
  350. {
  351. return $basepath;
  352. }
  353. $pos = strpos($surl,"#");
  354. if($pos>0)
  355. {
  356. $surl = substr($surl,0,$pos);
  357. }
  358. //? '/' ????????
  359. if($surl[0]=='/')
  360. {
  361. $okurl = $basehost.$surl;
  362. }
  363. else if($surl[0]=='.')
  364. {
  365. if(strlen($surl)<=2)
  366. {
  367. return '';
  368. }
  369. else if($surl[1]=='/')
  370. {
  371. $okurl = $basepath.ereg_replace('^.','',$surl);
  372. }
  373. else
  374. {
  375. $okurl = $basepath.'/'.$surl;
  376. }
  377. }
  378. else
  379. {
  380. if( strlen($surl) < 7 )
  381. {
  382. $okurl = $basepath.'/'.$surl;
  383. }
  384. else if( eregi('^http://',$surl) )
  385. {
  386. $okurl = $surl;
  387. }
  388. else
  389. {
  390. $okurl = $basepath.'/'.$surl;
  391. }
  392. }
  393. $okurl = eregi_replace('^http://','',$okurl);
  394. $okurl = 'http://'.eregi_replace('/{1,}','/',$okurl);
  395. return $okurl;
  396. }
  397. //????????????
  398. function GetUrlFromListRule($regxurl='',$handurl='',$startid=0,$endid=0,$addv=1,$usemore=0,$batchrule='')
  399. {
  400. global $dsql,$islisten;
  401. $lists = array();
  402. $n = 0;
  403. $islisten = (empty($islisten) ? 0 : $islisten);
  404. if($handurl!='')
  405. {
  406. $handurls = explode("\n",$handurl);
  407. foreach($handurls as $handurl)
  408. {
  409. $handurl = trim($handurl);
  410. if(eregi("^http://",$handurl))
  411. {
  412. $lists[$n][0] = $handurl;
  413. $lists[$n][1] = 0;
  414. $n++;
  415. if($islisten==1)
  416. {
  417. break;
  418. }
  419. }
  420. }
  421. }
  422. if($regxurl!='')
  423. {
  424. //???(#)?(*)
  425. if(!ereg("\(\*\)",$regxurl) && !ereg("\(#\)",$regxurl))
  426. {
  427. $lists[$n][0] = $regxurl;
  428. $lists[$n][1] = 0;
  429. $n++;
  430. }
  431. else
  432. {
  433. if($addv <= 0)
  434. {
  435. $addv = 1;
  436. }
  437. //??????????
  438. if($usemore==0)
  439. {
  440. while($startid <= $endid)
  441. {
  442. $lists[$n][0] = str_replace("(*)",sprintf('%0'.strlen($startid).'d',$startid),$regxurl);
  443. $lists[$n][1] = 0;
  444. $startid = sprintf('%0'.strlen($startid).'d',$startid + $addv);
  445. $n++;
  446. if($n>2000 || $islisten==1)
  447. {
  448. break;
  449. }
  450. }
  451. }
  452. //??????
  453. //????? [(#)=>(#)?????; (*)=>(*)??????1-20; typeid=>??id; addurl=>?????(?|????)]
  454. else
  455. {
  456. $nrules = explode(']',trim($batchrule));
  457. foreach($nrules as $nrule)
  458. {
  459. $nrule = trim($nrule);
  460. $nrule = ereg_replace("^\[|\]$",'',$nrule);
  461. $nrules = explode(';',$nrule);
  462. if(count($nrules)<3)
  463. {
  464. continue;
  465. }
  466. $brtag = '';
  467. $startid = 0;
  468. $endid = 0;
  469. $typeid = 0;
  470. $addurls = array();
  471. foreach($nrules as $nrule)
  472. {
  473. $nrule = trim($nrule);
  474. list($k,$v) = explode('=>',$nrule);
  475. if(trim($k)=='(#)')
  476. {
  477. $brtag = trim($v);
  478. }
  479. else if(trim($k)=='typeid')
  480. {
  481. $typeid = trim($v);
  482. }
  483. else if(trim($k)=='addurl')
  484. {
  485. $addurl = trim($v);
  486. $addurls = explode('|',$addurl);
  487. }
  488. else if(trim($k)=='(*)')
  489. {
  490. $v = ereg_replace("[ \r\n\t]",'',trim($v));
  491. list($startid,$endid) = explode('-',$v);
  492. }
  493. }
  494. //?????????
  495. if(ereg('[^0-9]',$typeid))
  496. {
  497. $arr = $dsql->GetOne("Select id From `#@__arctype` where typename like '$typeid' ");
  498. if(is_array($arr))
  499. {
  500. $typeid = $arr['id'];
  501. }
  502. else
  503. {
  504. $typeid = 0;
  505. }
  506. }
  507. //??????
  508. $mjj = 0;
  509. if(isset($addurls[0]))
  510. {
  511. foreach($addurls as $addurl)
  512. {
  513. $addurl = trim($addurl);
  514. if($addurl=='')
  515. {
  516. continue;
  517. }
  518. $lists[$n][0] = $addurl;
  519. $lists[$n][1] = $typeid;
  520. $n++;
  521. $mjj++;
  522. if($islisten==1)
  523. {
  524. break;
  525. }
  526. }
  527. }
  528. //???????????????????????
  529. if($islisten!=1 || $mjj==0 )
  530. {
  531. //???????????(#)?????????(*)?
  532. while($startid <= $endid)
  533. {
  534. $lists[$n][0] = str_replace("(#)",$brtag,$regxurl);
  535. $lists[$n][0] = str_replace("(*)",sprintf('%0'.strlen($startid).'d',$startid),$lists[$n][0]);
  536. $lists[$n][1] = $typeid;
  537. $startid = sprintf('%0'.strlen($startid).'d',$startid + $addv);
  538. $n++;
  539. if($islisten==1)
  540. {
  541. break;
  542. }
  543. if($n>20000)
  544. {
  545. break;
  546. }
  547. }
  548. }
  549. }
  550. } //End ?????
  551. } //End?????????
  552. }
  553. return $lists;
  554. }
  555. ?>