<?
/*
* ===========================================*
* 项目: Ourplus网站流量统计分析系统*
* 版本: Bate1*
* 开发: 看海*
* ===========================================*
* Copyright (c) 2004 - 2005*
* 主页: http://stat.ourplus.com*
* 信箱: fantasysisky@hotmail.com*
* 版权信息: 本软体为共享软体(shareware)提供个人网站免费使用，
                作者看海 (stat.ourplus.com) 保留全部权力，受相关法律和国际
				公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，
				并请勿删除版权声明。*
* ===========================================*
*/
	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)
	{
		if( $Key == '' )
		{
			return '';
		}
		$Res = mysql_query( " select gb from count_unicode2gb where unicode = '$Key'" );
		$Ret = mysql_fetch_array( $Res, MYSQL_ASSOC );
		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 );
	}
?>