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

353 lines
14KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /**
  4. * 动态分页类
  5. *
  6. * @version $Id: datalistcp.class.php 3 17:02 2010年7月9日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.'/dedetemplate.class.php');
  13. $codefile = (isset($needCode) ? $needCode : $cfg_soft_lang);
  14. $codefile = preg_replace("#[^\w-]#", '', $codefile);
  15. if (file_exists(DEDEINC.'/code/datalist.'.$codefile.'.inc')) {
  16. require_once(DEDEINC.'/code/datalist.'.$codefile.'.inc');
  17. } else {
  18. $lang_pre_page = '上页';
  19. $lang_next_page = '下页';
  20. $lang_index_page = '首页';
  21. $lang_end_page = '末页';
  22. $lang_record_number = '篇';
  23. $lang_page = '页';
  24. $lang_total = '共';
  25. }
  26. /**
  27. * DataListCP
  28. *
  29. * @package DedeBIZ.Libraries
  30. */
  31. class DataListCP
  32. {
  33. var $dsql;
  34. var $tpl;
  35. var $pageNO;
  36. var $totalPage;
  37. var $totalResult;
  38. var $pagesize;
  39. var $getValues;
  40. var $sourceSql;
  41. var $isQuery;
  42. var $queryTime;
  43. /**
  44. * 用指定的文档ID进行初始化
  45. *
  46. * @access public
  47. * @param string $tplfile 模板文件
  48. * @return string
  49. */
  50. function __construct($tplfile = '')
  51. {
  52. global $dsql;
  53. $this->sourceSql = '';
  54. $this->pagesize = 30;
  55. $this->queryTime = 0;
  56. $this->getValues = array();
  57. $this->isQuery = false;
  58. $this->totalResult = 0;
  59. $this->totalPage = 0;
  60. $this->pageNO = 0;
  61. $this->dsql = $dsql;
  62. $this->SetVar('ParseEnv', 'datalist');
  63. $this->tpl = new DedeTemplate();
  64. if ($GLOBALS['cfg_tplcache'] == 'N') {
  65. $this->tpl->isCache = false;
  66. }
  67. if ($tplfile != '') {
  68. $this->tpl->LoadTemplate($tplfile);
  69. }
  70. }
  71. /**
  72. * 兼容PHP4版本
  73. *
  74. * @access private
  75. * @param string $tplfile 模板文件
  76. * @return void
  77. */
  78. function DataListCP($tplfile = '')
  79. {
  80. $this->__construct($tplfile);
  81. }
  82. //设置SQL语句
  83. function SetSource($sql)
  84. {
  85. $this->sourceSql = $sql;
  86. }
  87. //设置模板
  88. //如果想要使用模板中指定的pagesize,必须在调用模板后才调用 SetSource($sql)
  89. function SetTemplate($tplfile)
  90. {
  91. $this->tpl->LoadTemplate($tplfile);
  92. }
  93. function SetTemplet($tplfile)
  94. {
  95. $this->tpl->LoadTemplate($tplfile);
  96. }
  97. /**
  98. * 对config参数及get参数等进行预处理
  99. *
  100. * @access public
  101. * @return void
  102. */
  103. function PreLoad()
  104. {
  105. global $totalresult, $pageno;
  106. if (empty($pageno) || preg_match("#[^0-9]#", $pageno)) {
  107. $pageno = 1;
  108. }
  109. if (empty($totalresult) || preg_match("#[^0-9]#", $totalresult)) {
  110. $totalresult = 0;
  111. }
  112. $this->pageNO = $pageno;
  113. $this->totalResult = $totalresult;
  114. if (isset($this->tpl->tpCfgs['pagesize'])) {
  115. $this->pagesize = $this->tpl->tpCfgs['pagesize'];
  116. }
  117. $this->totalPage = ceil($this->totalResult / $this->pagesize);
  118. if ($this->totalResult == 0) {
  119. $countQuery = preg_replace("#SELECT[ \r\n\t](.*)[ \r\n\t]FROM#is", 'SELECT COUNT(*) AS dd FROM', $this->sourceSql);
  120. $countQuery = preg_replace("#ORDER[ \r\n\t]{1,}BY(.*)#is", '', $countQuery);
  121. $row = $this->dsql->GetOne($countQuery);
  122. if (!is_array($row)) $row['dd'] = 0;
  123. $this->totalResult = isset($row['dd']) ? $row['dd'] : 0;
  124. $this->sourceSql .= " LIMIT 0,".$this->pagesize;
  125. } else {
  126. $this->sourceSql .= " LIMIT ".(($this->pageNO - 1) * $this->pagesize).",".$this->pagesize;
  127. }
  128. }
  129. //设置网址的Get参数键值
  130. function SetParameter($key, $value)
  131. {
  132. $this->getValues[$key] = $value;
  133. }
  134. //设置/获取文档相关的各种变量
  135. function SetVar($k, $v)
  136. {
  137. global $_vars;
  138. if (!isset($_vars[$k])) {
  139. $_vars[$k] = $v;
  140. }
  141. }
  142. function GetVar($k)
  143. {
  144. global $_vars;
  145. return isset($_vars[$k]) ? $_vars[$k] : '';
  146. }
  147. function XSSClean($val)
  148. {
  149. if (is_array($val)) {
  150. foreach ($val as $key => $v) {
  151. $val[$key] = $this->XSSClean($v);
  152. }
  153. return $val;
  154. }
  155. return $this->RemoveXss($val);
  156. }
  157. function RemoveXss($val)
  158. {
  159. global $cfg_soft_lang;
  160. if ($cfg_soft_lang == 'gb2312') $val = gb2utf8($val);
  161. $val = preg_replace('/([\x00-\x08|\x0b-\x0c|\x0e-\x19])/', '', $val);
  162. $search = 'abcdefghijklmnopqrstuvwxyz';
  163. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  164. $search .= '1234567890!@#$%^&*()';
  165. $search .= '~`";:?+/={}[]-_|\'\\';
  166. for ($i = 0; $i < strlen($search); $i++) {
  167. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); //with a ;
  168. $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); //with a ;
  169. }
  170. $val = str_replace("`", "‘", $val);
  171. $val = str_replace("'", "‘", $val);
  172. $val = str_replace("\"", "“", $val);
  173. $val = str_replace(",", ",", $val);
  174. $val = str_replace("(", "(", $val);
  175. $val = str_replace(")", ")", $val);
  176. $val = str_replace("flink", "fl*&k", $val);
  177. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  178. $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
  179. $ra = array_merge($ra1, $ra2);
  180. $found = true;
  181. while ($found == true) {
  182. $val_before = $val;
  183. for ($i = 0; $i < sizeof($ra); $i++) {
  184. $pattern = '/';
  185. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  186. if ($j > 0) {
  187. $pattern .= '(';
  188. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  189. $pattern .= '|';
  190. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  191. $pattern .= ')*';
  192. }
  193. $pattern .= $ra[$i][$j];
  194. }
  195. $pattern .= '/i';
  196. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);
  197. $val = preg_replace($pattern, $replacement, $val);
  198. if ($val_before == $val) {
  199. $found = false;
  200. }
  201. }
  202. }
  203. $val = str_replace("fl*&k","flink", $val);
  204. if ($cfg_soft_lang == 'gb2312') $val = utf82gb($val);
  205. return $val;
  206. }
  207. //获取当前页数据列表
  208. function GetArcList($atts, $refObj = '', $fields = array())
  209. {
  210. $rsArray = array();
  211. $t1 = Exectime();
  212. if (!$this->isQuery) $this->dsql->Execute('dlist', $this->sourceSql);
  213. $i = 0;
  214. while ($arr = $this->dsql->GetArray('dlist')) {
  215. $i++;
  216. $rsArray[$i] = $this->XSSClean($arr);
  217. if ($i >= $this->pagesize) {
  218. break;
  219. }
  220. }
  221. $this->dsql->FreeResult('dlist');
  222. $this->queryTime = (Exectime() - $t1);
  223. return $rsArray;
  224. }
  225. //获取分页导航列表
  226. function GetPageList($atts, $refObj = '', $fields = array())
  227. {
  228. global $lang_pre_page, $lang_next_page, $lang_index_page, $lang_end_page, $lang_record_number, $lang_page, $lang_total;
  229. $prepage = $nextpage = $geturl = $hidenform = '';
  230. $purl = $this->GetCurUrl();
  231. $prepagenum = $this->pageNO - 1;
  232. $nextpagenum = $this->pageNO + 1;
  233. if (!isset($atts['listsize']) || preg_match("#[^0-9]#", $atts['listsize'])) {
  234. $atts['listsize'] = 5;
  235. }
  236. if (!isset($atts['listitem'])) {
  237. $atts['listitem'] = "info,index,end,pre,next,pageno";
  238. }
  239. $totalpage = ceil($this->totalResult / $this->pagesize);
  240. //echo " {$totalpage}=={$this->totalResult}=={$this->pagesize}";
  241. //无结果或只有一页的情况
  242. if ($totalpage <= 1 && $this->totalResult > 0) {
  243. return "<ul class='pagination justify-content-center'><li class='page-item d-none d-sm-block disabled'><span class='page-link'>{$lang_total}1{$lang_page}".$this->totalResult.$lang_record_number."</span></li></ul>";
  244. }
  245. if ($this->totalResult == 0) {
  246. return "<ul class='pagination justify-content-center'><li class='page-item d-none d-sm-block disabled'><span class='page-link'>{$lang_total}0{$lang_page}".$this->totalResult.$lang_record_number."</span></li></ul>";
  247. }
  248. $infos = "<li class='page-item d-none d-sm-block disabled'><span class='page-link'>{$lang_total}{$totalpage}{$lang_page}/{$this->totalResult}{$lang_record_number}</span></li>";
  249. if ($this->totalResult != 0) {
  250. $this->getValues['totalresult'] = $this->totalResult;
  251. }
  252. if (count($this->getValues) > 0) {
  253. foreach ($this->getValues as $key => $value) {
  254. $value = urlencode($value);
  255. $geturl .= "$key=$value"."&";
  256. $hidenform .= "<input type='hidden' name='$key' value='$value' />\n";
  257. }
  258. }
  259. $purl .= "?".$geturl;
  260. //获得上一页和下一页的链接
  261. if ($this->pageNO != 1) {
  262. $prepage .= "<li class='page-item'><a class='page-link' href='".$purl."pageno=$prepagenum'>$lang_pre_page</a></li> \n";
  263. $indexpage = "<li class='page-item'><a class='page-link' href='".$purl."pageno=1'>$lang_index_page</a></li> \n";
  264. } else {
  265. $indexpage = "<li class='page-item d-none d-sm-block disabled'><span class='page-link'>"."$lang_index_page \n"."</span></li>";
  266. }
  267. if ($this->pageNO != $totalpage && $totalpage > 1) {
  268. $nextpage .= "<li class='page-item'><a class='page-link' href='".$purl."pageno=$nextpagenum'>$lang_next_page</a></li> \n";
  269. $endpage = "<li class='page-item'><a class='page-link' href='".$purl."pageno=$totalpage'>$lang_end_page</a></li> \n";
  270. } else {
  271. $endpage = " <li class='page-item d-none d-sm-block disabled'><span class='page-link'>$lang_end_page</span></li> \n";
  272. }
  273. //获得数字链接
  274. $listdd = "";
  275. $total_list = $atts['listsize'] * 2 + 1;
  276. if ($this->pageNO >= $total_list) {
  277. $j = $this->pageNO - $atts['listsize'];
  278. $total_list = $this->pageNO + $atts['listsize'];
  279. if ($total_list > $totalpage) {
  280. $total_list = $totalpage;
  281. }
  282. } else {
  283. $j = 1;
  284. if ($total_list > $totalpage) {
  285. $total_list = $totalpage;
  286. }
  287. }
  288. for ($j; $j <= $total_list; $j++) {
  289. $listdd .= $j == $this->pageNO ? "<li class='page-item'><span class='page-link'>$j</span></li>\r\n" : "<li class='page-item'><a class='page-link' href='".$purl."pageno=$j'>".$j."</a></li>\n";
  290. }
  291. $plist = "<ul class='pagination justify-content-center'>\n";
  292. //info,index,end,pre,next,pageno,form
  293. if (preg_match("#info#i", $atts['listitem'])) {
  294. $plist .= $infos;
  295. }
  296. if (preg_match("#index#i", $atts['listitem'])) {
  297. $plist .= $indexpage;
  298. }
  299. if (preg_match("#pre#i", $atts['listitem'])) {
  300. $plist .= $prepage;
  301. }
  302. if (preg_match("#pageno#i", $atts['listitem'])) {
  303. $plist .= $listdd;
  304. }
  305. if (preg_match("#next#i", $atts['listitem'])) {
  306. $plist .= $nextpage;
  307. }
  308. if (preg_match("#end#i", $atts['listitem'])) {
  309. $plist .= $endpage;
  310. }
  311. if (preg_match("#form#i", $atts['listitem'])) {
  312. $plist .= " <form name='pagelist' action='".$this->GetCurUrl()."' style='float:left' class='pagelistform'>$hidenform";
  313. if ($totalpage > $total_list) {
  314. $plist .= "<input type='text' name='pageno' style='padding:0;width:30px;height:18px' />\r\n";
  315. $plist .= "<input type='submit' name='plistgo' value='GO' style='padding:0;width:30px;height:22px' />\r\n";
  316. }
  317. $plist .= "</form>\n";
  318. }
  319. $plist .= "</ul>\n";
  320. return $plist;
  321. }
  322. //获得当前网址
  323. function GetCurUrl()
  324. {
  325. if (!empty($_SERVER["REQUEST_URI"])) {
  326. $nowurl = $_SERVER["REQUEST_URI"];
  327. $nowurls = explode("?", $nowurl);
  328. $nowurl = $nowurls[0];
  329. } else {
  330. $nowurl = $_SERVER["PHP_SELF"];
  331. }
  332. return $nowurl;
  333. }
  334. //关闭
  335. function Close()
  336. {
  337. }
  338. //显示数据
  339. function Display()
  340. {
  341. $this->PreLoad();
  342. //在PHP4中,对象引用必须放在display之前,放在其它位置中无效
  343. $this->tpl->SetObject($this);
  344. $this->tpl->Display();
  345. }
  346. //保存为HTML
  347. function SaveTo($filename)
  348. {
  349. $this->tpl->SaveTo($filename);
  350. }
  351. }