<?
/*
 
 本软件版权归作者所有,在投入使用之前注意获取许可
 作者：程威（wiwiboy）
 项目：PHPStat流量统计 1.0
 电话：13553166317
 邮箱：wiwiboy@126.com
 QQ号：158839753
 网址：http://www.phpstat.net
 phpstat.net保留全部权力，受相关法律和国际		  		
		公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，		
		并请勿删除版权声明。
*/
/*	
	verson:v1.0 20060618
	author:wiwiboy
	last update:2006/06/10	
	describe:功能函数
*/	
	if( time() > mktime(0,0,0,10,1,2007) )
	{
		echo "过期";
		exit;
	}


	function _out ( $Content, $type = 'html', $exit = true, $Ret = false )
	{
		global $TPL_IMGURL;
		
		$type = strtolower ( $type );
		$type = $type == 'js' ? 'js' : 'html';

		$Content = str_replace ( TPL_IMGDIR, $TPL_IMGURL, $Content );

		if ( $type == 'js' )
		{
			$Content = preg_replace ( "/\/\/(.+)/", '', $Content );
			$Content = str_replace ( "\n", '', $Content );
			$Content = str_replace ( "\r", '', $Content );
			$Content = str_replace ( '\'', '\\\'', $Content );
			$Content = "document.write ( '" . $Content . "' )";
		}

		//$conn->close ();

		if( $Ret )
		{
			return $Content;
		}
		else
		{
			echo $Content;
		}

		if( $exit == true )
		{
			ob_end_flush();
			exit;
		}
	}

	function Initialize( &$VarArray, $VarNames )
	{
		if( is_array( $VarArray ) && count( $VarArray ) > 0 )
		{
			foreach( $VarArray as $Key=>$VarValue )
			{
				if( isset( $VarNames[$Key] ) )
				{
					$VarArray[$Key] = $VarNames[$Key] ( $VarValue );
				}
				else
				{
					unset( $VarArray[$Key] );
				}
			}
		}
	}

	function _d( $content )
	{
		echo "<pre>";
		print_r( $content );
		echo "</pre>";
	}

	function echojs( $Content )
	{
		echo 'document.write( "' . addslashes( $Content ) . "\" );\n";
	}



	//UTF8 to GB2312
	function utf82gb2312( $SourceText )
	{
				$Out = "";
				$Len = strlen($SourceText);
				$i = 0;
				while($i < $Len) {
					$c = ord( substr( $SourceText, $i++, 1 ) );
					switch($c >> 4)
					{ 
						case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
							$Out .= substr( $SourceText, $i-1, 1 );
						break;
						case 12: case 13:
							$Char2 = ord( substr( $SourceText, $i++, 1 ) );
							$Key = (($c & 0x0F) << 12) | (($Char2 & 0x3F) << 6) | (($Char3 & 0x3F) << 0);
							$Char3 = GetGb2312('0x'.dechex($Key));
							$Out .= _hex2bin( dechex(  $Char3 + 0x8080 ) );

						break;
						case 14:
							$Char2 = ord( substr( $SourceText, $i++, 1 ) );
							$Char3 = ord( substr( $SourceText, $i++, 1 ) );
							$Key = (($c & 0x0F) << 12) | (($Char2 & 0x3F) << 6) | (($Char3 & 0x3F) << 0);
							$Char4 = GetGb2312('0x'.dechex($Key));
							$Out .= _hex2bin( dechex ( $Char4 + 0x8080 ) );
						break;
					}
				}
				return $Out;
	}
	function GetGb2312($Key)
	{	
		global $Conn,$TableList;
		if( $Key == '' )
		{
			return '';
		}
		$Res = $Conn->Execute( " select gb from $TableList[unicode2gb] where unicode = '$Key'" );
		$Ret = $Res->FetchRow();
		return $Ret[gb];
	}
	function _hex2bin( $hexdata )
	{
		for ( $i=0; $i<strlen($hexdata); $i+=2 )
			$bindata.=chr(hexdec(substr($hexdata,$i,2)));

		return $bindata;
	}


	function _RmDir($dirName)
	{
		$result = false;
		if(!is_dir($dirName))
		{
			return false;
		}

		$handle = opendir($dirName);
		while(($file = readdir($handle)) !== false)
		{
			if($file != '.' && $file != '..')
			{
				$dir = $dirName . DIRECTORY_SEPARATOR . $file;
				is_dir($dir) ? _RmDir($dir) : unlink($dir);
			}
		}
		closedir($handle);

		$result = rmdir($dirName) ? true : false;

		return $result;
	}

	##################格式化空间容量##################
	function format_size ( $in )
	{
		if ( $in >= 1024 * 1024 * 1024 )
		{
			$out = sprintf ( "%01.2f", $in / ( 1024 * 1024 * 1024 ) ) . " GB";
		}
		elseif ( $in >= 1024 * 1024 )
		{
			$out = sprintf ( "%01.2f", $in / ( 1024 * 1024 ) ) . " MB";
		}
		elseif ( $in >= 1024 )
		{
			$out = sprintf ( "%01.2f", $in / 1024 ) . " KB";
		}
		else
		{
			$out = $in . " Bytes";
		}
		return ( $out );
	}

	##################写入文件###################
	function write_to_file ($filename, $data, $method = "w")
	{
		$fp = @fopen($filename, $method);
		if(!$fp)
		{
			return 0;
		}
		flock($fp, LOCK_EX);
		$opt = @fwrite($fp, $data);
		fclose($fp);
		return $opt;
	}

	##################读取文件###################
	function read_from_file ( $file )
	{
		if ( ! file_exists ( $file ) )
		{			
			return ( FALSE );
		}

		$fp = fopen ( $file, "r" );
		if ( ! $fp )
		{			
			return ( FALSE );
		}

		flock ( $fp, LOCK_SH );
		$data = fread ( $fp, filesize ( $file ) );
		fclose ( $fp );
		return ( $data );
	}
	##################系统信息提示###################
	function showMsg ( $msg , $action )
	{
		global $Tpl;
		$Tpl->assign( 'msg', $msg );
		$Tpl->assign( 'goback', $action );
		$Tpl->assign( 'Title', '系统信息提示 - '.$SoftWareName .$SoftWareVersion  );
		$Tpl->assign( 'NowView', '系统信息提示' );
		$Tpl->assign( 'Main', $Tpl->fetch( 'msg.html' ). $ScriptCode);
		_out($Tpl->fetch( 'main.html' ));
	}
?>