国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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