<?php
if(!defined("PLAYBOX")){
	exit;
}
date_default_timezone_set('Asia/Shanghai');//设置为默认时区为中国
error_reporting(0);

function message($msg){
	if(isset($_GET['pg']) && $_GET['pg']!=1){
		echo '<script type="text/javascript">alert("'.$msg.'");location.href="'.CJNAME.'_list.php";</script>';
	}else{
		echo '<script type="text/javascript">alert("'.$msg.'");history.go(-1);</script>';
	}
	return false;
}

function magic_quotes($p){
	$is_magic_quotes=function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc();
	if(!$is_magic_quotes){
		return addslashes($p);
	}else{
		return $p;
	}
}

function convert_encoding($str,$nfate='GBK',$ofate='UTF8'){
	if(CODING=='UTF8'){
		if(function_exists("mb_convert_encoding")){
			$str=mb_convert_encoding($str,$nfate,$ofate);
			}
		else{
			if($nfate=="GBK") $nfate="GBK//IGNORE";
			$str=iconv($ofate, $nfate , $str);
			}
	}
	return $str;
}

//生成缓存文件
function writetocache($script, $cachedata, $path) {//$script为文件名，$path为路径

	$dir = $path;
	if(!is_dir($dir)) {
		@mkdir($dir, 0777);
	}
	if($fp = @fopen($dir.$script.".php", 'wb')) {
		fwrite($fp, "<?php\n//OY! cache file, DO NOT modify me!\n\n$cachedata?>");
		fclose($fp);
	} else {
		exit('操作失败 .');
	}
}
function arrayeval($array, $level = 0) {
	if(!is_array($array)) {
		return "'".$array."'";
	}
	if(is_array($array) && function_exists('var_export')) {
		return var_export($array, true);
	}

	$space = '';
	for($i = 0; $i <= $level; $i++) {
		$space .= "\t";
	}
	$evaluate = "Array\n$space(\n";
	$comma = $space;
	if(is_array($array)) {
		foreach($array as $key => $val) {
			$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
			$val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
			if(is_array($val)) {
				$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
			} else {
				$evaluate .= "$comma$key => $val";
			}
			$comma = ",\n$space";
		}
	}
	$evaluate .= "\n$space)";
	return $evaluate;
}

function getcachevars($data, $type = 'VAR') {
	$evaluate = '';
	foreach($data as $key => $val) {
		if(!preg_match("/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/", $key)) {
			continue;
		}
		if(is_array($val)) {
			$evaluate .= "\$$key = ".arrayeval($val).";\n";
		} else {
			$val = addcslashes($val, '\'\\');
			$evaluate .= $type == 'VAR' ? "\$$key = '$val';\n" : "define('".strtoupper($key)."', '$val');\n";
		}
	}
	return $evaluate;
}
?>