国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

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