国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1098 lines
49KB

  1. <?php
  2. if (!defined('DEDEINC')) exit("dedebiz");
  3. /**
  4. * 文档类
  5. *
  6. * @version $Id: arc.archives.class.php 4 15:13 2010年7月7日Z tianya $
  7. * @package DedeBIZ.Libraries
  8. * @copyright Copyright (c) 2022, DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  10. * @link https://www.dedebiz.com
  11. */
  12. require_once(DEDEINC."/typelink.class.php");
  13. require_once(DEDEINC."/channelunit.class.php");
  14. require_once(DEDEINC."/downmix.inc.php");
  15. @set_time_limit(0);
  16. /**
  17. * 主文档类(Archives类)
  18. *
  19. * @package TypeLink
  20. * @subpackage DedeBIZ.Libraries
  21. * @link https://www.dedebiz.com
  22. */
  23. class Archives
  24. {
  25. var $TypeLink;
  26. var $ChannelUnit;
  27. var $dsql;
  28. var $Fields;
  29. var $dtp;
  30. var $ArcID;
  31. var $SplitPageField;
  32. var $SplitFields;
  33. var $NowPage;
  34. var $TotalPage;
  35. var $NameFirst;
  36. var $ShortName;
  37. var $FixedValues;
  38. var $TempSource;
  39. var $IsError;
  40. var $SplitTitles;
  41. var $PreNext;
  42. var $addTableRow;
  43. var $remoteDir;
  44. /**
  45. * php5构造函数
  46. *
  47. * @access public
  48. * @param int $aid 文档ID
  49. * @return string
  50. */
  51. function __construct($aid)
  52. {
  53. global $dsql;
  54. $this->IsError = FALSE;
  55. $this->ArcID = $aid;
  56. $this->PreNext = array();
  57. $this->dsql = $dsql;
  58. $query = "SELECT channel,typeid FROM `#@__arctiny` WHERE id='$aid' ";
  59. $arr = $this->dsql->GetOne($query);
  60. if (!is_array($arr)) {
  61. $this->IsError = TRUE;
  62. } else {
  63. if ($arr['channel'] == 0) $arr['channel'] = 1;
  64. $this->ChannelUnit = new ChannelUnit($arr['channel'], $aid);
  65. $this->TypeLink = new TypeLink($arr['typeid']);
  66. if ($this->ChannelUnit->ChannelInfos['issystem'] != -1) {
  67. //如果当前文档不是系统模型,为单表模型
  68. $query = "SELECT arc.*,tp.reid,tp.typedir,ch.addtable
  69. FROM `#@__archives` arc
  70. LEFT JOIN #@__arctype tp on tp.id=arc.typeid
  71. LEFT JOIN #@__channeltype as ch on arc.channel = ch.id
  72. WHERE arc.id='$aid' ";
  73. $this->Fields = $this->dsql->GetOne($query);
  74. } else {
  75. $this->Fields['title'] = '';
  76. $this->Fields['money'] = $this->Fields['arcrank'] = 0;
  77. $this->Fields['senddate'] = $this->Fields['pubdate'] = $this->Fields['mid'] = $this->Fields['adminid'] = 0;
  78. $this->Fields['ismake'] = 1;
  79. $this->Fields['filename'] = '';
  80. }
  81. if ($this->TypeLink->TypeInfos['corank'] > 0 && $this->Fields['arcrank'] == 0) {
  82. $this->Fields['arcrank'] = $this->TypeLink->TypeInfos['corank'];
  83. }
  84. $this->Fields['tags'] = GetTags($aid);
  85. $this->dtp = new DedeTagParse();
  86. $this->dtp->SetRefObj($this);
  87. $this->SplitPageField = $this->ChannelUnit->SplitPageField;
  88. $this->SplitFields = '';
  89. $this->TotalPage = 1;
  90. $this->NameFirst = '';
  91. $this->ShortName = 'html';
  92. $this->FixedValues = '';
  93. $this->TempSource = '';
  94. $this->remoteDir = '';
  95. if (empty($GLOBALS['pageno'])) {
  96. $this->NowPage = 1;
  97. } else {
  98. $this->NowPage = $GLOBALS['pageno'];
  99. }
  100. //特殊的字段数据处理
  101. $this->Fields['aid'] = $aid;
  102. $this->Fields['id'] = $aid;
  103. $this->Fields['position'] = $this->TypeLink->GetPositionLink(TRUE);
  104. $this->Fields['typeid'] = $arr['typeid'];
  105. //设置一些全局参数的值
  106. foreach ($GLOBALS['PubFields'] as $k => $v) {
  107. $this->Fields[$k] = $v;
  108. }
  109. //为了减少重复查询,这里直接把附加表查询记录放在 $this->addTableRow 中,在 ParAddTable() 不再查询
  110. if ($this->ChannelUnit->ChannelInfos['addtable'] != '') {
  111. $query = "SELECT * FROM `{$this->ChannelUnit->ChannelInfos['addtable']}` WHERE `aid` = '$aid'";
  112. $this->addTableRow = $this->dsql->GetOne($query);
  113. }
  114. //issystem==-1 表示单表模型,单表模型不支持redirecturl这类参数,因此限定内容普通模型才进行下面查询
  115. if ($this->ChannelUnit->ChannelInfos['addtable'] != '' && $this->ChannelUnit->ChannelInfos['issystem'] != -1) {
  116. if (is_array($this->addTableRow)) {
  117. $this->Fields['redirecturl'] = $this->addTableRow['redirecturl'];
  118. $this->Fields['templet'] = $this->addTableRow['templet'];
  119. $this->Fields['userip'] = $this->addTableRow['userip'];
  120. }
  121. $this->Fields['templet'] = (empty($this->Fields['templet']) ? '' : trim($this->Fields['templet']));
  122. $this->Fields['redirecturl'] = (empty($this->Fields['redirecturl']) ? '' : trim($this->Fields['redirecturl']));
  123. $this->Fields['userip'] = (empty($this->Fields['userip']) ? '' : trim($this->Fields['userip']));
  124. } else {
  125. $this->Fields['templet'] = $this->Fields['redirecturl'] = '';
  126. }
  127. } //!error
  128. }
  129. //php4构造函数
  130. function Archives($aid)
  131. {
  132. $this->__construct($aid);
  133. }
  134. /**
  135. * 解析附加表的内容
  136. *
  137. * @access public
  138. * @return void
  139. */
  140. function ParAddTable()
  141. {
  142. //读取附加表信息,并把附加表的资料经过编译处理后导入到$this->Fields中,以方便在模板中用 {dede:field name='fieldname' /} 标记统一调用
  143. if ($this->ChannelUnit->ChannelInfos['addtable'] != '') {
  144. $row = $this->addTableRow;
  145. if ($this->ChannelUnit->ChannelInfos['issystem'] == -1) {
  146. $this->Fields['title'] = $row['title'];
  147. $this->Fields['senddate'] = $this->Fields['pubdate'] = $row['senddate'];
  148. $this->Fields['mid'] = $this->Fields['adminid'] = $row['mid'];
  149. $this->Fields['ismake'] = 1;
  150. $this->Fields['arcrank'] = 0;
  151. $this->Fields['money'] = 0;
  152. $this->Fields['filename'] = '';
  153. }
  154. if (is_array($row)) {
  155. foreach ($row as $k => $v) $row[strtolower($k)] = $v;
  156. }
  157. if (is_array($this->ChannelUnit->ChannelFields) && !empty($this->ChannelUnit->ChannelFields)) {
  158. foreach ($this->ChannelUnit->ChannelFields as $k => $arr) {
  159. if (isset($row[$k])) {
  160. if (!empty($arr['rename'])) {
  161. $nk = $arr['rename'];
  162. } else {
  163. $nk = $k;
  164. }
  165. $cobj = $this->GetCurTag($k);
  166. if (is_object($cobj)) {
  167. foreach ($this->dtp->CTags as $ctag) {
  168. if ($ctag->GetTagName() == 'field' && $ctag->GetAtt('name') == $k) {
  169. //带标识的专题节点
  170. if ($ctag->GetAtt('noteid') != '') {
  171. $this->Fields[$k.'_'.$ctag->GetAtt('noteid')] = $this->ChannelUnit->MakeField($k, $row[$k], $ctag);
  172. }
  173. //带类型的字段节点
  174. else if ($ctag->GetAtt('type') != '') {
  175. $this->Fields[$k.'_'.$ctag->GetAtt('type')] = $this->ChannelUnit->MakeField($k, $row[$k], $ctag);
  176. }
  177. //其它字段
  178. else {
  179. $this->Fields[$nk] = $this->ChannelUnit->MakeField($k, $row[$k], $ctag);
  180. }
  181. }
  182. }
  183. } else {
  184. $this->Fields[$nk] = $row[$k];
  185. }
  186. if ($arr['type'] == 'htmltext' && $GLOBALS['cfg_keyword_replace'] == 'Y' && !empty($this->Fields['keywords'])) {
  187. $this->Fields[$nk] = $this->ReplaceKeyword($this->Fields['keywords'], $this->Fields[$nk]);
  188. }
  189. }
  190. } //End foreach
  191. }
  192. //设置全局环境变量
  193. $this->Fields['typename'] = $this->TypeLink->TypeInfos['typename'];
  194. @SetSysEnv($this->Fields['typeid'], $this->Fields['typename'], $this->Fields['id'], $this->Fields['title'], 'archives');
  195. //文章的图片注释替换为标题
  196. $this->Fields['body'] = str_ireplace(array('alt=""','alt=\'\''),'',$this->Fields['body']);
  197. $this->Fields['body'] = preg_replace("@ [\s]{0,}alt[\s]{0,}=[\"'\s]{0,}[\s\S]{0,}[\"'\s] @isU"," ",$this->Fields['body']);
  198. $this->Fields['body'] = str_ireplace("<img","<img alt=\"".$this->Fields['title']."\" title=\"".$this->Fields['title']."\" style=\"margin:20px 0;box-shadow:0 1px 2px rgba(0,0,0,.1)\"",$this->Fields['body']);
  199. //图集的图片注释替换为标题
  200. $this->Fields['imgurls'] = str_ireplace(array('alt=""','alt=\'\''),'',$this->Fields['imgurls']);
  201. $this->Fields['imgurls'] = preg_replace("@ [\s]{0,}alt[\s]{0,}=[\"'\s]{0,}[\s\S]{0,}[\"'\s] @isU"," ",$this->Fields['imgurls']);
  202. $this->Fields['imgurls'] = str_ireplace("<img","<img alt=\"".$this->Fields['title']."\" title=\"".$this->Fields['title']."\" ",$this->Fields['imgurls']);
  203. //去掉文章图片的宽度和高度
  204. $this->Fields['body'] = preg_replace("/style=\"width\:(.*)\"/","",$this->Fields['body']);
  205. }
  206. //完成附加表信息读取
  207. unset($row);
  208. //处理要分页显示的字段
  209. $this->SplitTitles = array();
  210. if ($this->SplitPageField != '' && $GLOBALS['cfg_arcsptitle'] = 'Y'
  211. && isset($this->Fields[$this->SplitPageField])
  212. ) {
  213. $this->SplitFields = explode("#p#", $this->Fields[$this->SplitPageField]);
  214. $i = 1;
  215. foreach ($this->SplitFields as $k => $v) {
  216. $tmpv = cn_substr($v, 50);
  217. $pos = strpos($tmpv, '#e#');
  218. if ($pos > 0) {
  219. $st = trim(cn_substr($tmpv, $pos));
  220. if ($st == "" || $st == "副标题" || $st == "分页标题") {
  221. $this->SplitFields[$k] = preg_replace("/^(.*)#e#/is", "", $v);
  222. continue;
  223. } else {
  224. $this->SplitFields[$k] = preg_replace("/^(.*)#e#/is", "", $v);
  225. $this->SplitTitles[$k] = $st;
  226. }
  227. } else {
  228. continue;
  229. }
  230. $i++;
  231. }
  232. $this->TotalPage = count($this->SplitFields);
  233. $this->Fields['totalpage'] = $this->TotalPage;
  234. }
  235. //处理默认缩略图等
  236. if (isset($this->Fields['litpic'])) {
  237. if ($this->Fields['litpic'] == '-' || $this->Fields['litpic'] == '') {
  238. $this->Fields['litpic'] = $GLOBALS['cfg_cmspath'].'/static/defaultpic.jpg';
  239. }
  240. if (!preg_match("#^http:\/\/#i", $this->Fields['litpic']) && $GLOBALS['cfg_multi_site'] == 'Y') {
  241. $this->Fields['litpic'] = $GLOBALS['cfg_mainsite'].$this->Fields['litpic'];
  242. }
  243. $this->Fields['picname'] = $this->Fields['litpic'];
  244. //模板里直接使用{dede:field name='image'/}获取缩略图
  245. $this->Fields['image'] = (!preg_match('/jpg|gif|png/i', $this->Fields['picname']) ? '' : "<img src='{$this->Fields['picname']}' />");
  246. }
  247. //处理投票选项
  248. if (isset($this->Fields['voteid']) && !empty($this->Fields['voteid'])) {
  249. $this->Fields['vote'] = '';
  250. $voteid = $this->Fields['voteid'];
  251. $this->Fields['vote'] = "<script language='javascript' src='{$GLOBALS['cfg_cmspath']}/data/vote/vote_{$voteid}.js'></script>";
  252. if ($GLOBALS['cfg_multi_site'] == 'Y') {
  253. $this->Fields['vote'] = "<script language='javascript' src='{$GLOBALS['cfg_mainsite']}/data/vote/vote_{$voteid}.js'></script>";
  254. }
  255. }
  256. if (isset($this->Fields['goodpost']) && isset($this->Fields['badpost'])) {
  257. //digg
  258. if ($this->Fields['goodpost'] + $this->Fields['badpost'] == 0) {
  259. $this->Fields['goodper'] = $this->Fields['badper'] = 0;
  260. } else {
  261. $this->Fields['goodper'] = number_format($this->Fields['goodpost'] / ($this->Fields['goodpost'] + $this->Fields['badpost']), 3) * 100;
  262. $this->Fields['badper'] = 100 - $this->Fields['goodper'];
  263. }
  264. }
  265. }
  266. //获得当前字段参数
  267. function GetCurTag($fieldname)
  268. {
  269. if (!isset($this->dtp->CTags)) {
  270. return '';
  271. }
  272. foreach ($this->dtp->CTags as $ctag) {
  273. if ($ctag->GetTagName() == 'field' && $ctag->GetAtt('name') == $fieldname) {
  274. return $ctag;
  275. } else {
  276. continue;
  277. }
  278. }
  279. return '';
  280. }
  281. /**
  282. * 生成静态HTML
  283. *
  284. * @access public
  285. * @param int $isremote 是否远程
  286. * @return string
  287. */
  288. function MakeHtml($isremote = 0)
  289. {
  290. global $fileFirst, $cfg_basehost;
  291. if ($this->IsError) {
  292. return '';
  293. }
  294. $this->Fields["displaytype"] = "st";
  295. //预编译$th
  296. $this->LoadTemplet();
  297. $this->ParAddTable();
  298. $this->ParseTempletsFirst();
  299. $this->Fields['senddate'] = empty($this->Fields['senddate']) ? '' : $this->Fields['senddate'];
  300. $this->Fields['title'] = empty($this->Fields['title']) ? '' : $this->Fields['title'];
  301. $this->Fields['arcrank'] = empty($this->Fields['arcrank']) ? 0 : $this->Fields['arcrank'];
  302. $this->Fields['ismake'] = empty($this->Fields['ismake']) ? 0 : $this->Fields['ismake'];
  303. $this->Fields['money'] = empty($this->Fields['money']) ? 0 : $this->Fields['money'];
  304. $this->Fields['filename'] = empty($this->Fields['filename']) ? '' : $this->Fields['filename'];
  305. //分析要创建的文件名称
  306. $filename = GetFileNewName(
  307. $this->ArcID,
  308. $this->Fields['typeid'],
  309. $this->Fields['senddate'],
  310. $this->Fields['title'],
  311. $this->Fields['ismake'],
  312. $this->Fields['arcrank'],
  313. $this->TypeLink->TypeInfos['namerule'],
  314. $this->TypeLink->TypeInfos['typedir'],
  315. $this->Fields['money'],
  316. $this->Fields['filename']
  317. );
  318. $filenames = explode(".", $filename);
  319. $this->ShortName = $filenames[count($filenames) - 1];
  320. if ($this->ShortName == '') $this->ShortName = 'html';
  321. $fileFirst = preg_replace("/\.".$this->ShortName."$/i", "", $filename);
  322. $this->Fields['namehand'] = basename($fileFirst);
  323. $filenames = explode("/", $filename);
  324. $this->NameFirst = preg_replace("/\.".$this->ShortName."$/i", "", $filenames[count($filenames) - 1]);
  325. if ($this->NameFirst == '') {
  326. $this->NameFirst = $this->arcID;
  327. }
  328. //获得当前文档的全名
  329. $filenameFull = GetFileUrl(
  330. $this->ArcID,
  331. $this->Fields['typeid'],
  332. $this->Fields["senddate"],
  333. $this->Fields["title"],
  334. $this->Fields["ismake"],
  335. $this->Fields["arcrank"],
  336. $this->TypeLink->TypeInfos['namerule'],
  337. $this->TypeLink->TypeInfos['typedir'],
  338. $this->Fields["money"],
  339. $this->Fields['filename'],
  340. $this->TypeLink->TypeInfos['moresite'],
  341. $this->TypeLink->TypeInfos['siteurl'],
  342. $this->TypeLink->TypeInfos['sitepath']
  343. );
  344. $this->Fields['arcurl'] = $this->Fields['fullname'] = $filenameFull;
  345. //对于已设置不生成HTML的文章直接返回网址
  346. if (
  347. $this->Fields['ismake'] == -1 || $this->Fields['arcrank'] != 0 || $this->Fields['money'] > 0 || ($this->Fields['typeid'] == 0 && $this->Fields['channel'] != -1)) {
  348. return $this->GetTrueUrl($filename);
  349. }
  350. //循环生成HTML文件
  351. else {
  352. $seoUrls = array();
  353. for ($i = 1; $i <= $this->TotalPage; $i++) {
  354. if ($this->TotalPage > 1) {
  355. $this->Fields['tmptitle'] = (empty($this->Fields['tmptitle']) ? $this->Fields['title'] : $this->Fields['tmptitle']);
  356. if ($i > 1) $this->Fields['title'] = $this->Fields['tmptitle']."($i)";
  357. }
  358. if ($i > 1) {
  359. $TRUEfilename = $this->GetTruePath().$fileFirst."_".$i.".".$this->ShortName;
  360. $URLFilename = $fileFirst."_".$i.".".$this->ShortName;
  361. } else {
  362. $TRUEfilename = $this->GetTruePath().$filename;
  363. $URLFilename = $filename;
  364. }
  365. $seoUrls = array_merge($seoUrls, array($cfg_basehost.$URLFilename));
  366. $this->ParseDMFields($i, 1);
  367. $this->dtp->SaveTo($TRUEfilename);
  368. }
  369. }
  370. $this->dsql->ExecuteNoneQuery("Update `#@__archives` SET ismake=1 WHERE id='".$this->ArcID."'");
  371. return $this->GetTrueUrl($filename);
  372. }
  373. /**
  374. * 获得真实连接路径
  375. *
  376. * @access public
  377. * @param string $nurl 连接
  378. * @return string
  379. */
  380. function GetTrueUrl($nurl)
  381. {
  382. return GetFileUrl(
  383. $this->Fields['id'],
  384. $this->Fields['typeid'],
  385. $this->Fields['senddate'],
  386. $this->Fields['title'],
  387. $this->Fields['ismake'],
  388. $this->Fields['arcrank'],
  389. $this->TypeLink->TypeInfos['namerule'],
  390. $this->TypeLink->TypeInfos['typedir'],
  391. $this->Fields['money'],
  392. $this->Fields['filename'],
  393. $this->TypeLink->TypeInfos['moresite'],
  394. $this->TypeLink->TypeInfos['siteurl'],
  395. $this->TypeLink->TypeInfos['sitepath']
  396. );
  397. }
  398. /**
  399. * 获得站点的真实根路径
  400. *
  401. * @access public
  402. * @return string
  403. */
  404. function GetTruePath()
  405. {
  406. $TRUEpath = $GLOBALS["cfg_basedir"];
  407. return $TRUEpath;
  408. }
  409. /**
  410. * 获得指定键值的字段
  411. *
  412. * @access public
  413. * @param string $fname 键名称
  414. * @param object $ctag 标记
  415. * @return string
  416. */
  417. function GetField($fname, $ctag)
  418. {
  419. //所有Field数组 OR 普通Field
  420. if ($fname == 'array') {
  421. return $this->Fields;
  422. }
  423. //指定了ID的节点
  424. else if ($ctag->GetAtt('noteid') != '') {
  425. if (isset($this->Fields[$fname.'_'.$ctag->GetAtt('noteid')])) {
  426. return $this->Fields[$fname.'_'.$ctag->GetAtt('noteid')];
  427. }
  428. }
  429. //指定了type的节点
  430. else if ($ctag->GetAtt('type') != '') {
  431. if (isset($this->Fields[$fname.'_'.$ctag->GetAtt('type')])) {
  432. return $this->Fields[$fname.'_'.$ctag->GetAtt('type')];
  433. }
  434. } else if (isset($this->Fields[$fname])) {
  435. return $this->Fields[$fname];
  436. }
  437. return '';
  438. }
  439. /**
  440. * 获得模板文件位置
  441. *
  442. * @access public
  443. * @return string
  444. */
  445. function GetTempletFile()
  446. {
  447. global $cfg_basedir, $cfg_templets_dir, $cfg_df_style;
  448. $cid = $this->ChannelUnit->ChannelInfos['nid'];
  449. if (!empty($this->Fields['templet'])) {
  450. $filetag = MfTemplet($this->Fields['templet']);
  451. if (!preg_match("#\/#", $filetag)) $filetag = $GLOBALS['cfg_df_style'].'/'.$filetag;
  452. } else {
  453. $filetag = MfTemplet($this->TypeLink->TypeInfos["temparticle"]);
  454. }
  455. $tid = $this->Fields['typeid'];
  456. $filetag = str_replace('{cid}', $cid, $filetag);
  457. $filetag = str_replace('{tid}', $tid, $filetag);
  458. $tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;
  459. if ($cid == 'spec') {
  460. if (!empty($this->Fields['templet'])) {
  461. $tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;
  462. } else {
  463. $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/article_spec.htm";
  464. }
  465. }
  466. if (defined('DEDEMOB')) {
  467. $tmpfile = str_replace('.htm', '_m.htm', $tmpfile);
  468. }
  469. if (!file_exists($tmpfile)) {
  470. $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/".($cid == 'spec' ? 'article_spec.htm' : 'article_default.htm');
  471. if (defined('DEDEMOB')) {
  472. $tmpfile = str_replace('.htm', '_m.htm', $tmpfile);
  473. }
  474. }
  475. if (!preg_match("#.htm$#", $tmpfile)) return FALSE;
  476. return $tmpfile;
  477. }
  478. /**
  479. * 动态输出结果
  480. *
  481. * @access public
  482. * @return void
  483. */
  484. function display()
  485. {
  486. global $htmltype;
  487. if ($this->IsError) {
  488. return '';
  489. }
  490. $this->Fields["displaytype"] = "dm";
  491. if ($this->NowPage > 1) $this->Fields["title"] = $this->Fields["title"]."({$this->NowPage})";
  492. //预编译
  493. $this->LoadTemplet();
  494. $this->ParAddTable();
  495. $this->ParseTempletsFirst();
  496. //跳转网址
  497. $this->Fields['flag'] = empty($this->Fields['flag']) ? "" : $this->Fields['flag'];
  498. if (preg_match("#j#", $this->Fields['flag']) && $this->Fields['redirecturl'] != '') {
  499. if ($GLOBALS['cfg_jump_once'] == 'N') {
  500. $pageHtml = "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$GLOBALS['cfg_soft_lang']."\">\r\n<title>".$this->Fields['title']."</title>\r\n";
  501. $pageHtml .= "<meta http-equiv=\"refresh\" content=\"3;URL=".$this->Fields['redirecturl']."\">\r\n</head>\r\n<body>\r\n";
  502. $pageHtml .= "现在正在转向:".$this->Fields['title'].",请稍候...<br/><br/>\r\n转向内容简介:".$this->Fields['description']."\r\n</body>\r\n</html>\r\n";
  503. echo $pageHtml;
  504. } else {
  505. header("location:{$this->Fields['redirecturl']}");
  506. }
  507. exit();
  508. }
  509. $pageCount = $this->NowPage;
  510. $this->ParseDMFields($pageCount, 0);
  511. $this->dtp->display();
  512. }
  513. /**
  514. * 载入模板
  515. *
  516. * @access public
  517. * @return void
  518. */
  519. function LoadTemplet()
  520. {
  521. if ($this->TempSource == '') {
  522. $tempfile = $this->GetTempletFile();
  523. if (!file_exists($tempfile) || !is_file($tempfile)) {
  524. echo "文档ID:{$this->Fields['id']} - {$this->TypeLink->TypeInfos['typename']} - {$this->Fields['title']}<br />";
  525. echo "模板文件不存在,无法解析文档";
  526. exit();
  527. }
  528. $this->dtp->LoadTemplate($tempfile);
  529. $this->TempSource = $this->dtp->SourceString;
  530. } else {
  531. $this->dtp->LoadSource($this->TempSource);
  532. }
  533. }
  534. /**
  535. * 解析模板,对固定的标记进行初始给值
  536. *
  537. * @access public
  538. * @return void
  539. */
  540. function ParseTempletsFirst()
  541. {
  542. if (empty($this->Fields['keywords'])) {
  543. $this->Fields['keywords'] = '';
  544. }
  545. if (empty($this->Fields['reid'])) {
  546. $this->Fields['reid'] = 0;
  547. }
  548. $GLOBALS['envs']['tags'] = $this->Fields['tags'];
  549. if (isset($this->TypeLink->TypeInfos['reid'])) {
  550. $GLOBALS['envs']['reid'] = $this->TypeLink->TypeInfos['reid'];
  551. }
  552. $GLOBALS['envs']['keyword'] = $this->Fields['keywords'];
  553. $GLOBALS['envs']['typeid'] = $this->Fields['typeid'];
  554. $GLOBALS['envs']['topid'] = GetTopid($this->Fields['typeid']);
  555. $GLOBALS['envs']['aid'] = $GLOBALS['envs']['id'] = $this->Fields['id'];
  556. $GLOBALS['envs']['adminid'] = $GLOBALS['envs']['mid'] = isset($this->Fields['mid']) ? $this->Fields['mid'] : 1;
  557. $GLOBALS['envs']['channelid'] = $this->TypeLink->TypeInfos['channeltype'];
  558. if ($this->Fields['reid'] > 0) {
  559. $GLOBALS['envs']['typeid'] = $this->Fields['reid'];
  560. }
  561. MakeOneTag($this->dtp, $this, 'N');
  562. }
  563. /**
  564. * 解析模板,对内容里的变动进行赋值
  565. *
  566. * @access public
  567. * @param string $pageNo 页码数
  568. * @param string $ismake 是否生成
  569. * @return string
  570. */
  571. function ParseDMFields($pageNo, $ismake = 1)
  572. {
  573. $this->NowPage = $pageNo;
  574. $this->Fields['nowpage'] = $this->NowPage;
  575. if ($this->SplitPageField != '' && isset($this->Fields[$this->SplitPageField])) {
  576. $this->Fields[$this->SplitPageField] = $this->SplitFields[$pageNo - 1];
  577. if ($pageNo > 1) $this->Fields['description'] = trim(preg_replace("/[\r\n\t]/", ' ', cn_substr(html2text($this->Fields[$this->SplitPageField]), 200)));
  578. }
  579. //解析模板
  580. if (is_array($this->dtp->CTags)) {
  581. foreach ($this->dtp->CTags as $i => $ctag) {
  582. if ($ctag->GetName() == 'field') {
  583. $this->dtp->Assign($i, $this->GetField($ctag->GetAtt('name'), $ctag));
  584. } else if ($ctag->GetName() == 'pagebreak') {
  585. if ($ismake == 0) {
  586. $this->dtp->Assign($i, $this->GetPagebreakDM($this->TotalPage, $this->NowPage, $this->ArcID));
  587. } else {
  588. $this->dtp->Assign($i, $this->GetPagebreak($this->TotalPage, $this->NowPage, $this->ArcID));
  589. }
  590. } else if ($ctag->GetName() == 'pagetitle') {
  591. if ($ismake == 0) {
  592. $this->dtp->Assign($i, $this->GetPageTitlesDM($ctag->GetAtt("style"), $pageNo));
  593. } else {
  594. $this->dtp->Assign($i, $this->GetPageTitlesST($ctag->GetAtt("style"), $pageNo));
  595. }
  596. } else if ($ctag->GetName() == 'prenext') {
  597. $this->dtp->Assign($i, $this->GetPreNext($ctag->GetAtt('get')));
  598. }
  599. //二次开发上一篇下一篇{dede:prenextdiy get='pre'}{/dede:prenextdiy}{dede:prenextdiy get='next'}{/dede:prenextdiy}
  600. else if($ctag->GetName()=='prenextdiy')
  601. {
  602. $innertext = trim($ctag->GetInnerText());
  603. if($innertext)
  604. {
  605. $get = $ctag->GetAtt('get');
  606. $diys['diy'] = $this->GetPreNext('diy');
  607. $revalue = '';
  608. $dtp2 = new DedeTagParse();
  609. $dtp2->SetNameSpace('field','[',']');
  610. $dtp2->LoadSource($innertext);
  611. foreach($diys as $row)
  612. {
  613. foreach($dtp2->CTags as $tid=>$ctag2)
  614. {
  615. if(isset($row[$get][$ctag2->GetName()]))
  616. {
  617. $dtp2->Assign($tid,$row[$get][$ctag2->GetName()]);
  618. }
  619. }
  620. $revalue .= $dtp2->GetResult();
  621. }
  622. if($row[$get]['id']) $this->dtp->Assign($i,$revalue);
  623. }
  624. }
  625. else if ($ctag->GetName() == 'fieldlist') {
  626. $innertext = trim($ctag->GetInnerText());
  627. if ($innertext == '') $innertext = GetSysTemplets('tag_fieldlist.htm');
  628. $dtp2 = new DedeTagParse();
  629. $dtp2->SetNameSpace('field', '[', ']');
  630. $dtp2->LoadSource($innertext);
  631. $oldSource = $dtp2->SourceString;
  632. $oldCtags = $dtp2->CTags;
  633. $res = '';
  634. if (is_array($this->ChannelUnit->ChannelFields) && is_array($dtp2->CTags)) {
  635. foreach ($this->ChannelUnit->ChannelFields as $k => $v) {
  636. if (isset($v['autofield']) && empty($v['autofield'])) {
  637. continue;
  638. }
  639. $dtp2->SourceString = $oldSource;
  640. $dtp2->CTags = $oldCtags;
  641. $fname = $v['itemname'];
  642. foreach ($dtp2->CTags as $tid => $ctag2) {
  643. if ($ctag2->GetName() == 'name') {
  644. $dtp2->Assign($tid, $fname);
  645. } else if ($ctag2->GetName() == 'tagname') {
  646. $dtp2->Assign($tid, $k);
  647. } else if ($ctag2->GetName() == 'value') {
  648. if (isset($this->Fields[$k])) {
  649. $this->Fields[$k] = $this->ChannelUnit->MakeField($k, $this->Fields[$k], $ctag2);
  650. @$dtp2->Assign($tid, $this->Fields[$k]);
  651. }
  652. }
  653. }
  654. $res .= $dtp2->GetResult();
  655. }
  656. }
  657. $this->dtp->Assign($i, $res);
  658. } //end case
  659. } //结束模板循环
  660. }
  661. }
  662. /**
  663. * 关闭所占用的资源
  664. *
  665. * @access public
  666. * @return void
  667. */
  668. function Close()
  669. {
  670. $this->FixedValues = '';
  671. $this->Fields = '';
  672. }
  673. /**
  674. * 获取上一篇,下一篇链接
  675. *
  676. * @access public
  677. * @param string $gtype 获取类型
  678. * pre:上一篇 preimg:上一篇图片 next:下一篇 nextimg:下一篇图片
  679. * @return string
  680. */
  681. function GetPreNext($gtype = '')
  682. {
  683. $rs = '';
  684. if (count($this->PreNext) < 2) {
  685. $aid = $this->ArcID;
  686. $preR = $this->dsql->GetOne("Select id From `#@__arctiny` where id<$aid And arcrank>-1 And typeid='{$this->Fields['typeid']}' order by id desc");
  687. $nextR = $this->dsql->GetOne("Select id From `#@__arctiny` where id>$aid And arcrank>-1 And typeid='{$this->Fields['typeid']}' order by id asc");
  688. $next = (is_array($nextR) ? " where arc.id={$nextR['id']} " : ' where 1>2 ');
  689. $pre = (is_array($preR) ? " where arc.id={$preR['id']} " : ' where 1>2 ');
  690. $query = "Select arc.id,arc.title,arc.shorttitle,arc.typeid,arc.ismake,arc.senddate,arc.arcrank,arc.money,arc.filename,arc.litpic,
  691. t.typedir,t.typename,t.namerule,t.namerule2,t.ispart,t.moresite,t.siteurl,t.sitepath
  692. from `#@__archives` arc left join #@__arctype t on arc.typeid=t.id ";
  693. $nextRow = $this->dsql->GetOne($query.$next);
  694. $preRow = $this->dsql->GetOne($query.$pre);
  695. if (is_array($preRow)) {
  696. if (defined('DEDEMOB')) {
  697. $mlink = 'view.php?aid='.$preRow['id'];
  698. } else {
  699. $mlink = GetFileUrl(
  700. $preRow['id'],
  701. $preRow['typeid'],
  702. $preRow['senddate'],
  703. $preRow['title'],
  704. $preRow['ismake'],
  705. $preRow['arcrank'],
  706. $preRow['namerule'],
  707. $preRow['typedir'],
  708. $preRow['money'],
  709. $preRow['filename'],
  710. $preRow['moresite'],
  711. $preRow['siteurl'],
  712. $preRow['sitepath']
  713. );
  714. }
  715. //二次开发上一篇属性
  716. $preRow['litpic'] = (empty($preRow['litpic'])) ? $GLOBALS['cfg_cmspath'].'/images/defaultpic.jpg' : $preRow['litpic'];
  717. $this->PreNext['diy']['pre']['id'] = $preRow['id'];
  718. $this->PreNext['diy']['pre']['arcurl'] = $mlink;
  719. $this->PreNext['diy']['pre']['title'] = $preRow['title'];
  720. $this->PreNext['diy']['pre']['litpic'] = $preRow['litpic'];
  721. $this->PreNext['diy']['pre']['pubdate'] = $preRow['senddate'];
  722. $this->PreNext['pre'] = "上一篇:<a href='$mlink'>{$preRow['title']}</a> ";
  723. $this->PreNext['preimg'] = "<a href='$mlink'><img src=\"{$preRow['litpic']}\" alt=\"{$preRow['title']}\"/></a> ";
  724. } else {
  725. $this->PreNext['pre'] = "上一篇:没有了 ";
  726. $this->PreNext['preimg'] = "<img src=\"/templets/default/images/nophoto.jpg\" alt=\"对不起,没有上一图集了\"/>";
  727. }
  728. if (is_array($nextRow)) {
  729. if (defined('DEDEMOB')) {
  730. $mlink = 'view.php?aid='.$preRow['id'];
  731. } else {
  732. $mlink = GetFileUrl(
  733. $nextRow['id'],
  734. $nextRow['typeid'],
  735. $nextRow['senddate'],
  736. $nextRow['title'],
  737. $nextRow['ismake'],
  738. $nextRow['arcrank'],
  739. $nextRow['namerule'],
  740. $nextRow['typedir'],
  741. $nextRow['money'],
  742. $nextRow['filename'],
  743. $nextRow['moresite'],
  744. $nextRow['siteurl'],
  745. $nextRow['sitepath']
  746. );
  747. }
  748. //二次开发下一篇属性
  749. $nextRow['litpic'] = (empty($nextRow['litpic'])) ? $GLOBALS['cfg_cmspath'].'/images/defaultpic.jpg' : $nextRow['litpic'];
  750. $this->PreNext['diy']['next']['id'] = $nextRow['id'];
  751. $this->PreNext['diy']['next']['arcurl'] = $mlink;
  752. $this->PreNext['diy']['next']['title'] = $nextRow['title'];
  753. $this->PreNext['diy']['next']['litpic'] = $nextRow['litpic'];
  754. $this->PreNext['diy']['next']['pubdate'] = $nextRow['senddate'];
  755. $this->PreNext['next'] = "下一篇:<a href='$mlink'>{$nextRow['title']}</a> ";
  756. $this->PreNext['nextimg'] = "<a href='$mlink'><img src=\"{$nextRow['litpic']}\" alt=\"{$nextRow['title']}\"/></a> ";
  757. } else {
  758. $this->PreNext['next'] = "下一篇:没有了 ";
  759. $this->PreNext['nextimg'] = "<a href='javascript:void(0)' alt=\"\"><img src=\"/templets/default/images/nophoto.jpg\" alt=\"对不起,没有下一图集了\"/></a>";
  760. }
  761. }
  762. //二次开发上一篇下一篇
  763. if($gtype=='diy')
  764. {
  765. return $this->PreNext['diy'];
  766. }
  767. if($gtype=='pre')
  768. {
  769. $rs = $this->PreNext['pre'];
  770. } else if ($gtype == 'preimg') {
  771. $rs = $this->PreNext['preimg'];
  772. } else if ($gtype == 'next') {
  773. $rs = $this->PreNext['next'];
  774. } else if ($gtype == 'nextimg') {
  775. $rs = $this->PreNext['nextimg'];
  776. } else {
  777. $rs = $this->PreNext['pre']." &nbsp; ".$this->PreNext['next'];
  778. }
  779. return $rs;
  780. }
  781. /**
  782. * 获得动态页面分页列表
  783. *
  784. * @access public
  785. * @param int $totalPage 总页数
  786. * @param int $nowPage 当前页数
  787. * @param int $aid 文档id
  788. * @return string
  789. */
  790. function GetPagebreakDM($totalPage, $nowPage, $aid)
  791. {
  792. global $cfg_rewrite;
  793. if ($totalPage == 1) {
  794. return "";
  795. }
  796. $PageList = "<li class='page-item d-none d-sm-block disabled'><span class=\"page-link\">共".$totalPage."页: </span></li>";
  797. $nPage = $nowPage - 1;
  798. $lPage = $nowPage + 1;
  799. if ($nowPage == 1) {
  800. $PageList .= "<li class='page-item d-none d-sm-block disabled'><a class='page-link' href='#'>上一页</a></li>";
  801. } else {
  802. if ($nPage == 1) {
  803. $PageList .= "<li class='page-item'><a class='page-link' href='view.php?aid=$aid'>上一页</a></li>";
  804. if ($cfg_rewrite == 'Y') {
  805. $PageList = preg_replace("#.php\?aid=(\d+)#i", '-\\1-1.html', $PageList);
  806. }
  807. } else {
  808. $PageList .= "<li class='page-item'><a class='page-link' href='view.php?aid=$aid&pageno=$nPage'>上一页</a></li>";
  809. if ($cfg_rewrite == 'Y') {
  810. $PageList = str_replace(".php?aid=", "-", $PageList);
  811. $PageList = preg_replace("#&pageno=(\d+)#i", '-\\1.html', $PageList);
  812. }
  813. }
  814. }
  815. for ($i = 1; $i <= $totalPage; $i++) {
  816. if ($i == 1) {
  817. if ($nowPage != 1) {
  818. $PageList .= "<li class='page-item'><a class='page-link' href='view.php?aid=$aid'>1</a></li>";
  819. if ($cfg_rewrite == 'Y') {
  820. $PageList = preg_replace("#.php\?aid=(\d+)#i", '-\\1-1.html', $PageList);
  821. }
  822. } else {
  823. $PageList .= "<li class=\"page-item active\"><a class='page-link'>1</a></li>";
  824. }
  825. } else {
  826. $n = $i;
  827. if ($nowPage != $i) {
  828. $PageList .= "<li class='page-item'><a class='page-link' href='view.php?aid=$aid&pageno=$i'>".$n."</a></li>";
  829. if ($cfg_rewrite == 'Y') {
  830. $PageList = str_replace(".php?aid=", "-", $PageList);
  831. $PageList = preg_replace("#&pageno=(\d+)#i", '-\\1.html', $PageList);
  832. }
  833. } else {
  834. $PageList .= "<li class=\"page-item active\"><span class='page-link'>{$n}</span></li>";
  835. }
  836. }
  837. }
  838. if ($lPage <= $totalPage) {
  839. $PageList .= "<li class='page-item'><a class='page-link' href='view.php?aid=$aid&pageno=$lPage'>下一页</a></li>";
  840. if ($cfg_rewrite == 'Y') {
  841. $PageList = str_replace(".php?aid=", "-", $PageList);
  842. $PageList = preg_replace("#&pageno=(\d+)#i", '-\\1.html', $PageList);
  843. }
  844. } else {
  845. $PageList .= "<li class='page-item'><span class='page-link'>下一页</span></li>";
  846. }
  847. return $PageList;
  848. }
  849. /**
  850. * 获得静态页面分页列表
  851. *
  852. * @access public
  853. * @param int $totalPage 总页数
  854. * @param int $nowPage 当前页数
  855. * @param int $aid 文档id
  856. * @return string
  857. */
  858. function GetPagebreak($totalPage, $nowPage, $aid)
  859. {
  860. if ($totalPage == 1) {
  861. return "";
  862. }
  863. $PageList = "<li class='page-item d-none d-sm-block disabled'><span class=\"page-link\">共".$totalPage."页: </span></li>";
  864. $nPage = $nowPage - 1;
  865. $lPage = $nowPage + 1;
  866. if ($nowPage == 1) {
  867. $PageList .= "<li class='page-item d-none d-sm-block disabled'><a class='page-link' href='#'>上一页</a></li>";
  868. } else {
  869. if ($nPage == 1) {
  870. $PageList .= "<li class='page-item'><a class='page-link' href='".$this->NameFirst.".".$this->ShortName."'>上一页</a></li>";
  871. } else {
  872. $PageList .= "<li class='page-item'><a class='page-link' href='".$this->NameFirst."_".$nPage.".".$this->ShortName."'>上一页</a></li>";
  873. }
  874. }
  875. for ($i = 1; $i <= $totalPage; $i++) {
  876. if ($i == 1) {
  877. if ($nowPage != 1) {
  878. $PageList .= "<li class='page-item'><a class='page-link' href='".$this->NameFirst.".".$this->ShortName."'>1</a></li>";
  879. } else {
  880. $PageList .= "<li class=\"page-item active\"><span class='page-link'>1</span></li>";
  881. }
  882. } else {
  883. $n = $i;
  884. if ($nowPage != $i) {
  885. $PageList .= "<li class='page-item'><a class='page-link' href='".$this->NameFirst."_".$i.".".$this->ShortName."'>".$n."</a></li>";
  886. } else {
  887. $PageList .= "<li class=\"page-item active\"><span class='page-link'>{$n}</span></li>";
  888. }
  889. }
  890. }
  891. if ($lPage <= $totalPage) {
  892. $PageList .= "<li class='page-item'><a class='page-link' href='".$this->NameFirst."_".$lPage.".".$this->ShortName."'>下一页</a></li>";
  893. } else {
  894. $PageList .= "<li class='page-item'><span class='page-link'>下一页</span></li>";
  895. }
  896. return $PageList;
  897. }
  898. /**
  899. * 获得动态页面小标题
  900. *
  901. * @access public
  902. * @param string $styleName 类型名称
  903. * @param string $pageNo 页码数
  904. * @return string
  905. */
  906. function GetPageTitlesDM($styleName, $pageNo)
  907. {
  908. if ($this->TotalPage == 1) {
  909. return "";
  910. }
  911. if (count($this->SplitTitles) == 0) {
  912. return "";
  913. }
  914. $i = 1;
  915. $aid = $this->ArcID;
  916. if ($styleName == 'link') {
  917. $revalue = "";
  918. foreach ($this->SplitTitles as $k => $v) {
  919. if ($i == 1) {
  920. $revalue .= "<a href='view.php?aid=$aid&pageno=$i'>$v</a> \r\n";
  921. } else {
  922. if ($pageNo == $i) {
  923. $revalue .= " $v \r\n";
  924. } else {
  925. $revalue .= "<a href='view.php?aid=$aid&pageno=$i'>$v</a> \r\n";
  926. }
  927. }
  928. $i++;
  929. }
  930. } else {
  931. $revalue = "<select id='dedepagetitles' onchange='location.href=this.options[this.selectedIndex].value;'>\r\n";
  932. foreach ($this->SplitTitles as $k => $v) {
  933. if ($i == 1) {
  934. $revalue .= "<option value='".$this->Fields['phpurl']."/view.php?aid=$aid&pageno=$i'>{$i}、{$v}</option>\r\n";
  935. } else {
  936. if ($pageNo == $i) {
  937. $revalue .= "<option value='".$this->Fields['phpurl']."/view.php?aid=$aid&pageno=$i' selected>{$i}、{$v}</option>\r\n";
  938. } else {
  939. $revalue .= "<option value='".$this->Fields['phpurl']."/view.php?aid=$aid&pageno=$i'>{$i}、{$v}</option>\r\n";
  940. }
  941. }
  942. $i++;
  943. }
  944. $revalue .= "</select>\r\n";
  945. }
  946. return $revalue;
  947. }
  948. /**
  949. * 获得静态页面小标题
  950. *
  951. * @access public
  952. * @param string $styleName 类型名称
  953. * @param string $pageNo 页码数
  954. * @return string
  955. */
  956. function GetPageTitlesST($styleName, $pageNo)
  957. {
  958. if ($this->TotalPage == 1) {
  959. return "";
  960. }
  961. if (count($this->SplitTitles) == 0) {
  962. return "";
  963. }
  964. $i = 1;
  965. if ($styleName == 'link') {
  966. $revalue = "";
  967. foreach ($this->SplitTitles as $k => $v) {
  968. if ($i == 1) {
  969. $revalue .= "<a href='".$this->NameFirst.".".$this->ShortName."'>$v</a> \r\n";
  970. } else {
  971. if ($pageNo == $i) {
  972. $revalue .= " $v \r\n";
  973. } else {
  974. $revalue .= "<a href='".$this->NameFirst."_".$i.".".$this->ShortName."'>$v</a> \r\n";
  975. }
  976. }
  977. $i++;
  978. }
  979. } else {
  980. $revalue = "<select id='dedepagetitles' onchange='location.href=this.options[this.selectedIndex].value;'>\r\n";
  981. foreach ($this->SplitTitles as $k => $v) {
  982. if ($i == 1) {
  983. $revalue .= "<option value='".$this->NameFirst.".".$this->ShortName."'>{$i}、{$v}</option>\r\n";
  984. } else {
  985. if ($pageNo == $i) {
  986. $revalue .= "<option value='".$this->NameFirst."_".$i.".".$this->ShortName."' selected>{$i}、{$v}</option>\r\n";
  987. } else {
  988. $revalue .= "<option value='".$this->NameFirst."_".$i.".".$this->ShortName."'>{$i}、{$v}</option>\r\n";
  989. }
  990. }
  991. $i++;
  992. }
  993. $revalue .= "</select>\r\n";
  994. }
  995. return $revalue;
  996. }
  997. /**
  998. * 高亮问题修正, 排除alt title <a></a>直接的字符替换
  999. *
  1000. * @param string $kw
  1001. * @param string $body
  1002. * @return string
  1003. */
  1004. function ReplaceKeyword($kw,&$body)
  1005. {
  1006. global $cfg_cmspath;
  1007. $maxkey = 5;
  1008. $kws = explode(",",trim($kw));//以分好为间隔符
  1009. $i=0;
  1010. $karr = $kaarr = $GLOBALS['replaced'] = array();
  1011. //暂时屏蔽超链接
  1012. $body = preg_replace("#(<a(.*))(>)(.*)(<)(\/a>)#isU", '\\1-]-\\4-[-\\6', $body);
  1013. $query = "SELECT * FROM `#@__keywords` WHERE rpurl<>'' ORDER BY `rank` DESC";
  1014. $this->dsql->SetQuery($query);
  1015. $this->dsql->Execute();
  1016. while($row = $this->dsql->GetArray())
  1017. {
  1018. global $cfg_replace_num;
  1019. $key = trim($row['keyword']);
  1020. $key_url=trim($row['rpurl']);
  1021. $karr[] = $key;
  1022. $kaarr[] = "<a href='$key_url' target='_blank'>$key</a>";
  1023. }
  1024. //这里可能会有错误
  1025. if (version_compare(PHP_VERSION, '5.5.0', '>='))
  1026. {
  1027. if($cfg_replace_num > 0)
  1028. {
  1029. $query = "SELECT * FROM #@__keywords WHERE rpurl<>'' ORDER BY rank DESC";
  1030. $this->dsql->SetQuery($query);
  1031. $this->dsql->Execute();
  1032. while($row = $this->dsql->GetArray())
  1033. {
  1034. $key = trim($row['keyword']);
  1035. $key_url=trim($row['rpurl']);
  1036. $body = str_replace_limit($key, "<a href='$key_url' target='_blank'>$key</a>", $body, $cfg_replace_num);
  1037. }
  1038. } else {
  1039. $query = "SELECT * FROM `#@__keywords` WHERE rpurl<>'' ORDER BY `rank` DESC";
  1040. $this->dsql->SetQuery($query);
  1041. $this->dsql->Execute();
  1042. while($row = $this->dsql->GetArray())
  1043. {
  1044. $key = trim($row['keyword']);
  1045. $key_url=trim($row['rpurl']);
  1046. $body = str_replace($key, "<a href='$key_url' target='_blank'>$key</a>", $body);
  1047. }
  1048. }
  1049. } else {
  1050. $body = @preg_replace("#(^|>)([^<]+)(?=<|$)#sUe", "_highlight('\\2', \$karr, \$kaarr, '\\1')", $body);
  1051. }
  1052. //恢复超链接
  1053. $body = preg_replace("#(<a(.*))-\]-(.*)-\[-(\/a>)#isU", '\\1>\\3<\\4', $body);
  1054. return $body;
  1055. }
  1056. }//End Archives
  1057. //指定替换次数功能
  1058. function str_replace_limit($search, $replace, $subject, $limit){
  1059. if(is_array($search)){
  1060. foreach($search as $k=>$v){
  1061. $search[$k] = '`'. preg_quote($search[$k], '`'). '`';
  1062. }
  1063. } else {
  1064. $search = '`'. preg_quote($search, '`'). '`';
  1065. }
  1066. return preg_replace($search, $replace, $subject, $limit);
  1067. }
  1068. //高亮专用, 替换多次时可能不能达到最多次
  1069. function _highlight($string, $words, $result, $pre)
  1070. {
  1071. global $cfg_replace_num;
  1072. if (version_compare(PHP_VERSION, '5.5.0', '>='))
  1073. {
  1074. $string = $string[0];
  1075. $pre = $pre[0];
  1076. }
  1077. $string = str_replace('\"', '"', $string);
  1078. if($cfg_replace_num > 0)
  1079. {
  1080. foreach ($words as $key => $word)
  1081. {
  1082. if($GLOBALS['replaced'][$word] == 1)
  1083. {
  1084. continue;
  1085. }
  1086. $string = preg_replace("#".preg_quote($word)."#", $result[$key], $string, $cfg_replace_num);
  1087. if(strpos($string, $word) !== FALSE)
  1088. {
  1089. $GLOBALS['replaced'][$word] = 1;
  1090. }
  1091. }
  1092. } else {
  1093. $string = str_replace($words, $result, $string);
  1094. }
  1095. return $pre.$string;
  1096. }