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

362 lines
15KB

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