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

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