<?php
/**
 * @Cscms open source management system
 * @copyright 2009-2014 chshcms.com. All rights reserved.
 * @Author:Cheng Jie
 * @Dtime:2014-04-29
 */
 
//截取UTF-8编码下字符串的函数
if(!function_exists('sub_str')) {
     function sub_str($str, $length, $start=0, $suffix="...", $charset="gb2312")
     {
		 if(($length+2) >= strlen($str)){
            return $str;
         }
         if(function_exists("mb_substr")){
             return mb_substr($str, $start, $length, $charset).$suffix;
         }elseif(function_exists('iconv_substr')){
             return iconv_substr($str,$start,$length,$charset).$suffix;
         }
         $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
         $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
         $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
         $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
         preg_match_all($re[$charset], $str, $match);
         $slice = join("",array_slice($match[0], $start, $length));
         return $slice.$suffix;
     }
}

//写文件
if ( ! function_exists('write_file'))
{
	function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
	{
		$dir = dirname($path);
		if(!is_dir($dir)){
			mkdirss($dir);
		}
		if ( ! $fp = @fopen($path, $mode))
		{
			return FALSE;
		}
		flock($fp, LOCK_EX);
		fwrite($fp, $data);
		flock($fp, LOCK_UN);
		fclose($fp);

		return TRUE;
	}
}

//递归创建文件
if ( ! function_exists('mkdirss'))
{
	function mkdirss($dirs,$mode=0777) {
		if(!is_dir($dirs)){
			mkdirss(dirname($dirs), $mode);
			return @mkdir($dirs, $mode);
		}
		return true;
	}
}

//字符加密、解密
function sys_auth($string,$operation,$key=''){
	     $key=($key=='')?CS_Encryption_Key:$key;
         $key=md5($key);
         $key_length=strlen($key);
         $string=$operation=='D'?base64_decode(str_replace('-','/',str_replace('_','+',$string))):substr(md5($string.$key),0,8).$string;
         $string_length=strlen($string);
         $rndkey=$box=array();
         $result='';
         for($i=0;$i<=255;$i++){
                   $rndkey[$i]=ord($key[$i%$key_length]);
                   $box[$i]=$i;
         }
         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=='D'){
               if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){
                       return substr($result,8);
               }else{
                       return'';
			   }
		 }else{
               return str_replace('+','_',str_replace('=','',base64_encode($result)));
		 }
}