<?php
defined('ROOT_PHPLEFT') or exit(require '../404.php');
function delfile($path){
	if (file_exists($moduleiddir)) {
     unlink($moduleiddir);
    }
	return true;
}
function loophtml($loop,$url,$txt,$is = '0'){
	foreach($loop as $id=>$m) {
	$html = file_get($m);
	MakeHtmlFile($url[$id], $html, $is);
	}
  if ($txt<>''){
  msg($txt,'','','','0');
  }
}
function loophtmlon($loop,$url){
	foreach($loop as $id=>$m) {
	$html = file_get($m);
	MakeHtmlFile($url[$id], $html, 1);
	}

}
function deletehtml($path)
{
	
	return @unlink($path);
}

function file_get($filename) {
	return @file_get_contents($filename);
}

function strip_sql($string) {
    $search = array("/ union /i","/ select /i","/ update /i","/ outfile /i","/ or /i");
    $replace = array(' union ',' select ',' update ',' outfile ',' or ');
    return is_array($string) ? array_map('strip_sql', $string) : preg_replace($search, $replace, $string);
}
function daddslashes($string, $force = 0) {
        if(!$GLOBALS['magic_quotes_gpc'] || $force) {
                if(is_array($string)) {
                        foreach($string as $key => $val) {
                                $string[$key] = daddslashes($val, $force);
                        }
                } else {
                        $string = addslashes($string);
                }
        }
        return $string;
}
function gourl($url , $time = '1'){
	$url='<meta http-equiv="refresh" content="'.$time.';URL='.$url.'">';
	return $url;
}
function gotourl($url){
	$url = header("location: ".$url);
	return $url;
}
function messages($url){
	$messages="<script language=javascript>location.href='".$url."';</script>";
	return $messages;
}
function message($name,$url){
	$messages="<script language=javascript>alert('$name');location.href='".$url."';</script>";
	return $messages;
}
function message_up($name,$url){
	$messages="<script language=javascript>alert('$name');history.go(-1);</script>";
	return $messages;
}

function convert($str, $from = 'utf-8', $to = 'gb2312') {
	if(!$str) return '';
	if(strtolower($from) == strtolower($to)) return $str;
	$from = str_replace('gbk', 'gb2312', strtolower($from));
	$to = str_replace('gbk', 'gb2312', strtolower($to));
	if($from == $to) return $str;
	$tmp = array();
	if(function_exists('iconv')) {
		if(is_array($str)) {
			foreach($str as $key => $val) {
				$tmp[$key] = iconv($from, $to."//IGNORE", $val);
			}
			return $tmp;
		} else {
			return iconv($from, $to."//IGNORE", $str);
		}
	} else if(function_exists('mb_convert_encoding')) {
		if(is_array($str)) {
			foreach($str as $key => $val) {
				$tmp[$key] = mb_convert_encoding($val, $to, $from);
			}
			return $tmp;
		} else {
			return mb_convert_encoding($str, $to, $from);
		}	
	} else {
		require_once PL_ROOT.'/include/convert.func.php';
		return dconvert($str, $to, $from);
	}
}
function pinyin($text, $exp = '') {
	if(!$text) return '';
	if(strtolower(DT_CHARSET) != 'gbk') $text = convert($text, DT_CHARSET, 'gbk');
	$data = array();
	$tmp = @file(PL_ROOT.'/file/table/gb-pinyin.table');
	if(!$tmp) return '';
	$tmps = count($tmp);
	for($i = 0; $i < $tmps; $i++) {
		$tmp1 = explode("	", $tmp[$i]);
		$data[$i]=array($tmp1[0], $tmp1[1]);
	}
	$r = array();
	$k = 0;
	$textlen = strlen($text);
	for($i = 0; $i < $textlen; $i++) {
		$p = ord(substr($text, $i, 1));		
		if($p > 160) {
			$q = ord(substr($text, ++$i, 1));
			$p = $p*256+$q-65536;
		}
        if($p > 0 && $p < 160) {
            $r[$k] = chr($p);
        } elseif($p< -20319 || $p > -10247) {
            $r[$k] = '';
        } else {
            for($j = $tmps-1; $j >= 0; $j--) {
                if($data[$j][1]<=$p) break;
            }
            $r[$k] = $data[$j][0];
        }
		$k++;
	}
	return implode($exp, $r);
}

function cache_read($file, $path = '', $iscachevar = 0)
{
	global $PL_CFG;
	$path = $path?$path:PL_ROOT.$PL_CFG['cache']."/";
	$cachefile = $path.$file;
	return @include $cachefile;
}

function cache_write($file, $array, $path = ''){
	global $PL_CFG;
	$path = $path?$path:PL_ROOT.$PL_CFG['cache']."/";
	if(!is_array($array)) return false;
	$array = "<?php\ndefined('ROOT_PHPLEFT') or exit('phpleft: Not page');\nreturn ".var_export($array, true).";\n?>";
	$cachefile = $path.$file;
	$strlen = file_put_contents($cachefile, $array);
	@chmod($cachefile, 0777);
	return $strlen;
}


function MakeHtmlFile($file_name, $content, $is){
 if ($is==1){
 if (!file_exists (dirname($file_name))){
  if (!@mkdir (dirname($file_name), 0777)) {
   die(); 
  }
 } 
 if(!$fp = fopen($file_name, "w")){ 
  return false;
 } 
 if(!fwrite($fp, $content)){ 
  fclose($fp);
  return false;
 } 
 fclose($fp);
 @chmod($file_name,0666);
 return true;
}
}

function ThisFile(){
$url=$_SERVER['PHP_SELF'];
$filename = end(explode('/',$url));
$filenames = str_replace('.php','',$filename);
return $filenames;
}
function delhtmls($html){
$htm = explode('.htm',$html);	
return $htm[0];	
}

function sysSubStr($string,$length,$suffix = '', $start = 0)
{
  global $CFG;
	if($start) {
		$tmp = dsubstr($string, $start);
		$string = substr($string, strlen($tmp));
	}
	$strlen = strlen($string);
	if($strlen <= $length) return $string;
	$string = str_replace(array('&', '&quot;', '&lt;', '&gt;'), array('&', '"', '<', '>'), $string);
	$str = '';
	if(strtolower($CFG['charset']) == 'utf-8') {
		$n = $tn = $noc = 0;
		while($n < $strlen)	{
			$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;
		$str = substr($string, 0, $n);
	} else {
		$suffixlen = strlen($suffix);
		$maxi = $length - $suffixlen - 1;
		for($i = 0; $i < $maxi; $i++) {
			$str .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
		}
	}
	$str = str_replace(array('&', '"', '<', '>'), array('&', '&quot;', '&lt;', '&gt;'), $str);
	return $str.$suffix;
}

function menurl($moduleid){
	global $PL_CFG;
	$menus = cache_read("moduleid_".$moduleid.PL_PHP,PL_ROOT.$PL_CFG['cache']."/");
	$menurl = PL_URL.$menus['path']."/";
	return $menurl;
}
?>