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

443 lines
15KB

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