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

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