<?php
class pager {
	var $url;
	var $countall;
	var $page;
	var $thestr;
	var $backstr;
	var $nextstr;
	var $pageno;
	var $is_base; 

	function Pager($countall, $countlist) {
		global $_SET, $channeldir, $channelselect, $ciddir, $_MODULE;
		$filename = substr(strrchr($_SERVER['PHP_SELF'], '/'), 1);
		if ($filename == $_MODULE[$channelselect]['user_list_php']) $this -> is_base = 1;
		if ($countlist < 1) $countlist = 20;
		@$this -> pageno = sprintf("%d", $_GET["pageno"]);
		if ($this -> pageno == 0) {
			$this -> pageno = 1;
		} 
		if (!isset($this -> pageno)) {
			$this -> pageno = 1;
		} 
		$this -> countall = $countall;
		if ($this -> countall % $countlist != 0) {
			$this -> page = sprintf("%d", $this -> countall / $countlist) + 1;
		} else {
			$this -> page = $this -> countall / $countlist;
		} 
		$this -> url = self :: getUrl();
		if ($this -> page <= 6) {
			for ($i = 1;$i < $this -> page + 1;$i++) {
				$this -> thestr = $this -> thestr . self :: makepageno($i, $this -> pageno);
			} 
		} else {
			if ($this -> pageno <= 3) {
				for ($i = 1;$i < 6;$i++) {
					$this -> thestr = $this -> thestr . self :: makepageno($i, $this -> pageno);
				} 
			} else {
				if (2 + $this -> pageno <= $this -> page) {
					for ($i = $this -> pageno-2;$i < $this -> pageno + 3;$i++) {
						$this -> thestr = $this -> thestr . self :: makepageno($i, $this -> pageno);
					} 
				} else {
					for ($i = $this -> pageno-2;$i < $this -> page + 1;$i++) {
						$this -> thestr = $this -> thestr . self :: makepageno($i, $this -> pageno);
					} 
				} 
			} 
		} 

		$this -> backstr = self :: gotoback($this -> pageno);
		$this -> nextstr = self :: gotonext($this -> pageno, $this -> page);
		$pp = $this -> backstr . $this -> thestr . $this -> nextstr . "<b>{$this->pageno}/{$this->page}</b><b>共{$countall}</b>";
		echo "<div class='pager'>" . $pp . "</div>";
	} 

	function makepageno($i, $pageno) {
		if ($i == $pageno) {
			return " <b><i class=a></i><i class=b></i><i class=c></i><i class=d></i>" . $i . "</b>";
		} else {
			return " <a href=" . self :: replacepageno($this -> url, 5, $i) . ">" . $i . "</a>";
		} 
	} 

	function gotoback($pageno) {
		if ($pageno-1 > 0) {
			return $this -> gotoback = " <a class='first' href=" . self :: replacepageno($this -> url, 3, 0) . ">首页</a><a class='pre' href=" . self :: replacepageno($this -> url, 2, 0) . ">上页</a>";
		} else {
			return $this -> gotoback = "";
		} 
	} 

	function gotonext($pageno, $page) {
		if ($pageno < $page) {
			return "<a class='next' href=" . self :: replacepageno($this -> url, 1, 0) . ">下页</a><a class='last' href=" . self :: replacepageno($this -> url, 4, 0) . ">尾页</a>";
		} else {
			return "";
		} 
	} 

	function replacepageno($url, $flag, $i) {
		global $ciddir, $channeldir, $html_size, $_SET, $is_html;
		if ($flag == 1) {
			$temp_pageno = $this -> pageno;
			return str_replace("pageno=" . $temp_pageno, "pageno=" . ($this -> pageno + 1), $url);
		} else if ($flag == 2) {
			$temp_pageno = $this -> pageno;
			return str_replace("pageno=" . $temp_pageno, "pageno=" . ($this -> pageno-1), $url);
		} else if ($flag == 3) {
			$temp_pageno = $this -> pageno;
			return str_replace("pageno=" . $temp_pageno, "pageno=1", $url);
		} else if ($flag == 4) {
			$temp_pageno = $this -> pageno;
			return str_replace("pageno=" . $temp_pageno, "pageno=" . $this -> page, $url);
		} else if ($flag == 5) {
			$temp_pageno = $this -> pageno;
			return str_replace("pageno=" . $temp_pageno, "pageno=" . $i, $url);
		} else if ($flag == 6) {
			$temp_pageno = $this -> pageno;
			return str_replace("pageno=" . $temp_pageno, "", $url);
		} else {
			return $url;
		} 
	} 
	// 获得当前URL的方法
	function getUrl() {
		$url = "http://" . $_SERVER["HTTP_HOST"];
		if (isset($_SERVER["REQUEST_URI"])) {
			$url .= $_SERVER["REQUEST_URI"];
		} else {
			$url .= $_SERVER["HP_SELF"];
			if (!empty($_SERVER["QUERY_STRING"])) {
				$url .= "?" . $_SERVER["QUERY_STRING"];
			} 
		} 
		// 在当前的URL里加入pageno=x字样
		if (!ereg("(pageno=|pageno=|pageno=|pageno=)", $url)) {
			if (!strpos($url, "?")) {
				$url = $url . "?pageno=1";
			} else {
				$url = $url . "&pageno=1";
			} 
		} 
		return $url;
	} 
} 
?>