<?php
@$vercode=''; //此处取值请自行修改

date_default_timezone_set('Asia/Shanghai'); //消除时差


if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value) {
        $value = is_array($value) ?
            array_map('stripslashes_deep', $value) :
            stripslashes($value);
        return $value;
    }

    $_GET     = array_map('stripslashes_deep', $_GET);
    $_POST    = array_map('stripslashes_deep', $_POST);
    $_COOKIE  = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}

if(isset($_POST['vercode'])){
if ($_POST['vercode']!=$vercode){
 echo("[err]invalid vercode[/err]");
 exit();
}
}

function mk_dir( $target ) {

    $target = str_replace( '//', '/', $target );
    if ( file_exists( $target ) )
        return @is_dir( $target );

    if ( @mkdir( $target ) ) {
        $stat = @stat( dirname( $target ) );
        $dir_perms = $stat['mode'] & 0007777;  // Get the permission bits.
        @chmod( $target, $dir_perms );
        return true;
    } elseif ( is_dir( dirname( $target ) ) ) {
            return false;
    }

    // If the above failed, attempt to create the parent node, then try again.
    if ( ( $target != '/' ) && ( mk_dir( dirname( $target ) ) ) )
        return mk_dir( $target );

    return false;
}
$filename=trim($_POST['filename']);
if ($filename==''){
 echo('[err]文件名参数filename不能为空[/err]');
 exit;
}else{
    $filename= str_ireplace("yyyy",date('Y'),$filename);
    $filename= str_ireplace("yy",date('y'),$filename);
    $filename= str_ireplace("mm",date('m'),$filename);
    $filename= str_ireplace("dd",date('d'),$filename);
    $filename= str_ireplace("hh",date('h'),$filename);
    $filename= str_ireplace("ii",date('i'),$filename);
    $filename= str_ireplace("ss",date('s'),$filename);
    
    }

$filepath=trim($_POST['filepath']);
if ($filepath==''){
 echo('[err]文件保存路径参数filepath不能为空[/err]');
 exit;
}else{
    $filepath= str_ireplace("yyyy",date('Y'),$filepath);
    $filepath= str_ireplace("yy",date('y'),$filepath);
    $filepath= str_ireplace("mm",date('m'),$filepath);
    $filepath= str_ireplace("dd",date('d'),$filepath);
    $filepath= str_ireplace("hh",date('h'),$filepath);
    $filepath= str_ireplace("ii",date('i'),$filepath);
    $filepath= str_ireplace("ss",date('s'),$filepath);
    
    if(substr($filepath,-1,1)<>"/")
        $filepath=$filepath."/";

    if(!mk_dir($filepath)){
        echo('[err]创建目录失败[/err]');
        exit;
    }

}

$text=trim($_POST['text']);
if ($text==''){
 echo('[err]内容参数text不能为空[/err]');
 exit;
}

$file=fopen ($filepath.$filename,"w+"); 
if($file){
    if(fwrite($file,$text)) {
        echo('[ok]');    
    }else{
        echo('[err]生成文件失败[/err]');    
    }
    
}else{
 echo('[err]生成文件失败[/err]');
}
fclose($file);
?>