国内流行的内容管理系统(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.

1099 lines
49KB

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