<?php
class Load {
	static function controller($name) {
		//return @include_once ($config ['ROOT_PATH'] . '../protected/controller/' . $name . '.php');
	}
	static function logic($name) {
		
		return @include_once ( './protected/logic/' . $name . 'Logic.php');
	}
	static function lib($name) {
		//return @include_once ($config ['ROOT_PATH'] . 'protected/plugin/' . $name . '.han.php');
	}
	static function dao($name) {
		//return @include_once ($config ['ROOT_PATH'] . 'protected/model/' . $name . '.Dao.php');
	}
}

/**
 * 获取时间戳,day获取几天后的时间戳
 */
function get_date($day = 0) {
	if ($day != 0)
		return strtotime ( "+" . $day . " day" );
	return strtotime ( "now" );
}

/**
 * 获取几个小时后的时间戳
 * @param unknown_type $hours
 */
function get_hours($hours = 0) {
	if ($hours != 0)
		return strtotime ( "+" . $hours . " hours" );
	return strtotime ( "now" );
}

/**
 * 获取时间戳
 */
function get_time($time = 0) {
	return strtotime ( $time );
}

/**
 * 格式化时间为年月日时分秒
 * @param unknown_type $strtotime
 */
function format_date($strtotime = 0, $format = "Y-m-d H:i:s") {
	return date ( $format, $strtotime );
}

/**
 * 获取当天开始时间和结束时间
 */
function get_today() {
	
	$t = time ();
	
	$start = mktime ( 0, 0, 0, date ( "m", $t ), date ( "d", $t ), date ( "Y", $t ) );
	
	$end = mktime ( 23, 59, 59, date ( "m", $t ), date ( "d", $t ), date ( "Y", $t ) );
	
	return array ('start' => $start, 'end' => $end );
}

/**
 * 格式化问题状态
 * @param unknown_type $status
 */
function format_question_status($status = 1, $type = "index") {
	
	if ($type == "index") {
		if (1 == $status)
			return "待解决";
		elseif (2 == $status)
			return "<span class='queState colGreen'>已解决</span>";
		elseif (9 == $status)
			return "<span class='queState colRed'>已关闭</span>";
		elseif (6 == $status)
			return "推荐问题";
	} elseif ($type == "page") {
		if (1 == $status)
			return "<b class='extraIcon iconNotDone'></b>待解决";
		elseif (2 == $status)
			return "<b class='extraIcon iconDone'></b><span class='queState colGreen'>已解决</span>";
		elseif (9 == $status)
			return "<b class='extraIcon iconStop'></b><span class='queState colRed'>已关闭</span>";
		elseif (6 == $status)
			return "推荐问题";
	} elseif ($type == "vip") {
		if (1 == $status)
			return "未提问";
		elseif (2 == $status)
			return "已提问";
		elseif (3 == $status)
			return "满意";
		elseif (4 == $status)
			return "不满意";
	} else {
		if (1 == $status)
			return "<b class='extraIcon iconNotDone'></b>";
		elseif (2 == $status)
			return "<b class='extraIcon iconDone'></b>";
		elseif (9 == $status)
			return "<b class='extraIcon iconStop'></b>";
		elseif (6 == $status)
			return "推荐问题";
	}
}

/**
 * 获取客户端IP地址
 */
function client_ip() {
	if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
		$onlineip = getenv ( 'HTTP_CLIENT_IP' );
	} elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
		$onlineip = getenv ( 'HTTP_X_FORWARDED_FOR' );
	} elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
		$onlineip = getenv ( 'REMOTE_ADDR' );
	} elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
		$onlineip = $_SERVER ['REMOTE_ADDR'];
	}
	
	preg_match ( '/[\d\.]{7,15}/', $onlineip, $onlineipmatches );
	$onlineip = ($onlineipmatches [0] ? $onlineipmatches [0] : 'unknown');
	
	return $onlineip;
}

/**
 * random 获取字符串
 * @param int $length
 * @return string $hash
 */
function random($length = 6, $type = 0) {
	$hash = '';
	$chararr = array ('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', '0123456789', '23456789ABCDEFGHJKLMNPQRSTUVWXYZ' );
	$chars = $chararr [$type];
	$max = strlen ( $chars ) - 1;
	PHP_VERSION < '4.2.0' && mt_srand ( ( double ) microtime () * 1000000 );
	for($i = 0; $i < $length; $i ++) {
		$hash .= $chars [mt_rand ( 0, $max )];
	}
	return $hash;
}

/**
 * 获取用户等级
 */
function get_lv($credit = 0) {
	$lv = array ();
	if (0 <= $credit && $credit <= 80) {
		
		array_push ( $lv, 1 );
		
		array_push ( $lv, 80 );
		
		return $lv;
	} elseif (81 <= $credit && $credit <= 400) {
		
		array_push ( $lv, 2 );
		
		array_push ( $lv, 400 );
		
		return $lv;
	} elseif (401 <= $credit && $credit <= 800) {
		
		array_push ( $lv, 3 );
		
		array_push ( $lv, 800 );
		
		return $lv;
	} elseif (801 <= $credit && $credit <= 2000) {
		
		array_push ( $lv, 4 );
		
		array_push ( $lv, 2000 );
		
		return $lv;
	} elseif (2001 <= $credit && $credit <= 4000) {
		
		array_push ( $lv, 5 );
		
		array_push ( $lv, 4000 );
		
		return $lv;
	} elseif (4001 <= $credit && $credit <= 7000) {
		
		array_push ( $lv, 6 );
		
		array_push ( $lv, 7000 );
		
		return $lv;
	} elseif (7001 <= $credit && $credit <= 10000) {
		
		array_push ( $lv, 7 );
		
		array_push ( $lv, 10000 );
		
		return $lv;
	} elseif (10001 <= $credit && $credit <= 14000) {
		
		array_push ( $lv, 8 );
		
		array_push ( $lv, 14000 );
		
		return $lv;
	} elseif (14001 <= $credit && $credit <= 18000) {
		
		array_push ( $lv, 9 );
		
		array_push ( $lv, 18000 );
		
		return $lv;
	} elseif (18001 <= $credit && $credit <= 22000) {
		
		array_push ( $lv, 10 );
		
		array_push ( $lv, 22000 );
		
		return $lv;
	} elseif (22001 <= $credit && $credit <= 32000) {
		
		array_push ( $lv, 11 );
		
		array_push ( $lv, 32000 );
		
		return $lv;
	} elseif (32001 <= $credit && $credit <= 45000) {
		
		array_push ( $lv, 12 );
		
		array_push ( $lv, 45000 );
		
		return $lv;
	} elseif (45001 <= $credit && $credit <= 60000) {
		
		array_push ( $lv, 13 );
		
		array_push ( $lv, 60000 );
		
		return $lv;
	} elseif (60001 <= $credit && $credit <= 100000) {
		
		array_push ( $lv, 14 );
		
		array_push ( $lv, 100000 );
		
		return $lv;
	} elseif (100001 <= $credit && $credit <= 150000) {
		
		array_push ( $lv, 15 );
		
		array_push ( $lv, 150000 );
		
		return $lv;
	} elseif (150001 <= $credit && $credit <= 250000) {
		
		array_push ( $lv, 16 );
		
		array_push ( $lv, 250000 );
		
		return $lv;
	} elseif (250001 <= $credit && $credit <= 400000) {
		
		array_push ( $lv, 17 );
		
		array_push ( $lv, 400000 );
		
		return $lv;
	} elseif (400001 <= $credit && $credit <= 700000) {
		
		array_push ( $lv, 18 );
		
		array_push ( $lv, 8700000 );
		
		return $lv;
	} elseif (700001 <= $credit && $credit <= 1000000) {
		
		array_push ( $lv, 19 );
		
		array_push ( $lv, 1000000 );
		
		return $lv;
	} elseif (1000000 <= $credit && $credit <= 9999999) {
		
		array_push ( $lv, 20 );
		
		array_push ( $lv, 9999999 );
		
		return $lv;
	}
}

/**
 * 多少天以前
 */
function day_ago($day = 7) {
	$now = strtotime ( "now" );
}

/**
 * 获取天数差
 * @param unknown_type $nowtime	现在时间
 * @param unknown_type $future	过去时间
 */
function count_days($nowtime, $futuretime) {
	$a_dt = getdate ( $nowtime );
	$b_dt = getdate ( $futuretime );
	
	$a_new = mktime ( 12, 0, 0, $a_dt ['mon'], $a_dt ['mday'], $a_dt ['year'] );
	$b_new = mktime ( 12, 0, 0, $b_dt ['mon'], $b_dt ['mday'], $b_dt ['year'] );
	
	return round ( abs ( $a_new - $b_new ) / 86400 );
}
/**
 * 获取最后关闭时间
 */
function get_last_close_day($sortime = 0, $period = 7) {
	
	$result = count_days ( strtotime ( "now" ), $sortime );
	
	$day = ($period - $result) <= 0 ? 0 : ($period - $result);
	
	return $day;
}

function my_date_format($timestamp, $format = "Y-m-d H:i:s") {
	$SystemConfig = ConfigHandler::get ();
	
	$timezone = $SystemConfig ['timezone'];
	
	return gmdate ( $format, ($timestamp + $timezone * 3600) );
}

/**
 * 获取价格订阅的时间
 * @param unknown_type $begin
 * @param unknown_type $duration
 */
function get_rss_time($begin, $duration = 1) {
	
	$t = strtotime ( "+" . $duration . " year" ,$begin);
	
	return array("stodate"=>date ( "Y-m-d", $t ),"stotime"=>$t);
}

/**
 * 获取时长
 * @param unknown_type $duraction
 */
function get_rss_duraction() {
	
	$dur = array ('one' => 1, 'two' => 2, 'three' => 3 );
	
	return $dur;
}

//格式化时间函数
function my_date_format2($time = 0, $format = 'Y-m-d') {
	
	$time = isset ( $time ) ? $time : 0;
	
	$now = time ();
	
	$t = $now - $time;
	
	if ($t < 0) {
		$time = format_date ( $time, $format );
		return $time;
	}
	
	if ($t >= 3600) {
		$time = format_date ( $time, $format );
	} 

	elseif ($t < 3600 && $t >= 60) {
		$time = floor ( $t / 60 ) . "分钟前";
	} else {
		$time = "刚刚";
	}
	
	return $time;
}

/**
 * 计算剩下时间2天
 * @param unknown_type $time 剩下值
 */
function do_vip_answer_time($time = 0) {
	if ($time <= 0) {
		return "超过回答时间";
	}
	
	if ($time <= 172800 && $time >= 86400) {
		$time = $time - 86400;
		
		$t = round ( $time / 3600 );
		
		return "1天" . $t . "小时";
	}
	
	if ($time < 86400 && $time >= 3600) {
		
		$t = round ( $time / 3600 );
		
		return $t . "小时";
	}
	
	if ($time < 3600 && $time > 0) {
		$t = round ( $time / 60 );
		
		return $t . "分钟";
	}
}

/**
 * 获取文件后缀名
 * @param unknown_type $filename
 */
function _GetFileEXT($filename) {
	
	$pics = explode ( '.', $filename );
	
	$num = count ( $pics );
	
	return $pics [$num - 1];
}

/**
 * 获取上一页
 * @param unknown_type $on_page
 */
function get_previous($on_page = 1) {
	return $on_page != 0 ? $on_page - 1 : $on_page;
}

/**
 * 加密或解密指定字符串
 * @param string $string 要加密或解密的字符串
 * @param string $operation 当取值为'DECODE'时表示解密，否则为加密
 * @param string $key 加解密的key
 * @param $expiry 超时值
 * */
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
	$ckey_length = 4;
	if (! $key) {
		$key = AUTH;
	}
	$key = md5 ( $key );
	$keya = md5 ( substr ( $key, 0, 16 ) );
	$keyb = md5 ( substr ( $key, 16, 16 ) );
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
	
	$cryptkey = $keya . md5 ( $keya . $keyc );
	$key_length = strlen ( $cryptkey );
	
	$string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
	$string_length = strlen ( $string );
	
	$result = '';
	$box = range ( 0, 255 );
	
	$rndkey = array ();
	for($i = 0; $i <= 255; $i ++) {
		$rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
	}
	
	for($j = $i = 0; $i < 256; $i ++) {
		$j = ($j + $box [$i] + $rndkey [$i]) % 256;
		$tmp = $box [$i];
		$box [$i] = $box [$j];
		$box [$j] = $tmp;
	}
	
	for($a = $j = $i = 0; $i < $string_length; $i ++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box [$a]) % 256;
		$tmp = $box [$a];
		$box [$a] = $box [$j];
		$box [$j] = $tmp;
		$result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
	}
	
	if ($operation == 'DECODE') {
		if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
			return substr ( $result, 26 );
		} else {
			return '';
		}
	} else {
		return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
	}
}

/**
 * 将一个字符串写入文件
 * @param string $filename 文件名
 * @param string $data     要写入文件的内容
 * @param mixed  $flag     文件的写入标识，如果为FILE_APPEND或'FILE_APPEND'表示追加，否则为新建
 * @return int 返回写入文件的字节数
 * */
function files_put_contents($filename, $data, $flag = false) {
	$mode = ($flag == FILE_APPEND || strtoupper ( $flag ) == 'FILE_APPEND') ? 'ab' : 'wb';
	$f = @fopen ( $filename, $mode );
	if ($f === false) {
		return 0;
	} else {
		if (is_array ( $data )) {
			$data = implode ( '', $data );
		}
		$bytes_written = @fwrite ( $f, $data );
		@fclose ( $f );
		return $bytes_written;
	}
}

function cut_str($string, $length, $dot = ' ...') {
	if (strlen ( $string ) <= $length) {
		return $string;
	}
	
	$strcut = '';
	$sys_config = ConfigHandler::get ();
	if (strtolower ( $sys_config ['charset'] ) == 'utf-8') {
		$n = $tn = $noc = 0;
		while ( $n < strlen ( $string ) ) {
			$t = ord ( $string [$n] );
			if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
				$tn = 1;
				$n ++;
				$noc ++;
			} elseif (194 <= $t && $t <= 223) {
				$tn = 2;
				$n += 2;
				$noc += 2;
			} elseif (224 <= $t && $t < 239) {
				$tn = 3;
				$n += 3;
				$noc += 2;
			} elseif (240 <= $t && $t <= 247) {
				$tn = 4;
				$n += 4;
				$noc += 2;
			} elseif (248 <= $t && $t <= 251) {
				$tn = 5;
				$n += 5;
				$noc += 2;
			} elseif ($t == 252 || $t == 253) {
				$tn = 6;
				$n += 6;
				$noc += 2;
			} else {
				$n ++;
			}
			
			if ($noc >= $length) {
				break;
			}
		}
		if ($noc > $length) {
			$n -= $tn;
		}
		
		$strcut = substr ( $string, 0, $n );
	} else {
		for($i = 0; $i < $length; $i ++) {
			$strcut .= ord ( $string [$i] ) > 127 ? $string [$i] . $string [++ $i] : $string [$i];
		}
	}
	
	return $strcut . $dot;
}
function cutstr($string, $length, $dot = '') {
	Return cut_str ( $string, $length, $dot );
}
;

?>