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

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