<?php

/**
 * macms 公用函数库
 * ============================================================================
 * 版权所有 Walton Zhang。QQ:31323185
 * 网站地址: http://www.t-mac.org；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: zhangwentao $
 * $Id: function.inc.php 53 2011-12-05 11:10:53Z zhangwentao $
 */
if (!defined('ROOTINC'))
    exit('Access Deniedfuncton!');

function daddslashes($string, $force = 0)
{
    !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
    if (!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 redirect($msg, $url = 'javascript:history.go(-1);', $min='4')
{
    global $template;
    $time = 2;
    include (ROOTINC . '/redirect.html');
    exit();
}

function jump($msg, $url = 'javascript:history.go(-1);', $min='2')
{
    global $template;
    include template('jump');
}

function ShowMsg($msg, $code, $url = '', $win = 'self')
{
    @header('Content-Type: text/html; charset=utf8');
    if ($code == 1) {
        echo '<script language="javascript">alert("' . $msg . '");history.back();</script>';
    } elseif ($code == 2) {
        echo '<script language="javascript">alert("' . $msg . '");window.close();</script>';
    } elseif ($code == 3) {
        echo '<script language="javascript">alert("' . $msg . '");this.document.location="' . $url . '";</script>';
    }
    exit ();
}

//截取中文字符串
/*
  Utf-8、gb2312都支持的汉字截取函数
  cut_str(字符串, 截取长度, 开始长度, 编码);
  编码默认为 utf-8
  开始长度默认为 0
 */

function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
    if ($code == 'UTF-8') {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
        preg_match_all($pa, $string, $t_string);

        if (count($t_string[0]) - $start > $sublen)
            return join('', array_slice($t_string[0], $start, $sublen)) . "...";
        return join('', array_slice($t_string[0], $start, $sublen));
    }
    else {
        $start = $start * 2;
        $sublen = $sublen * 2;
        $strlen = strlen($string);
        $tmpstr = '';

        for ($i = 0; $i < $strlen; $i++) {
            if ($i >= $start && $i < ($start + $sublen)) {
                if (ord(substr($string, $i, 1)) > 129) {
                    $tmpstr.= substr($string, $i, 2);
                } else {
                    $tmpstr.= substr($string, $i, 1);
                }
            }
            if (ord(substr($string, $i, 1)) > 129)
                $i++;
        }
        if (strlen($tmpstr) < $strlen)
            $tmpstr.= "...";
        return $tmpstr;
    }
}

/**
 * 模板调用函数
 */
function template($filename)
{
    global $template;
    $file = $template->getfile($filename);
    return $file;
}

/**
 * 取get post参数
 * @param <type> $key
 * @return <type>
 */
function getParam($key)
{
    if (isset($_GET[$key])) {
        return $_GET[$key];
    } elseif (isset($_POST[$key])) {
        return $_POST[$key];
    } else {
        return '';
    }
}

/**
 * 创建xml
 * @param <type> $xml_root  //创建的xml本地路径
 * @param <type> $xml_url   //接口xml的地址
 */
function createXML($xml_url, $xml_root, $xml_name, $checktime = 2592000)
{
    $xml_root_all = $xml_root . $xml_name;  //xml接口的本地路径+文件全名
    if (checkXML($xml_root_all, $checktime)) {
        CheckFolder($xml_root);
        $xml_content = GetHttps($xml_url);
        file_put_contents($xml_root_all, $xml_content);
    }
}

/**
 * 检查xml接口是否已经生成 是否过期
 * @param <type> $xml_root_all
 * @param <type> $time
 * @return <type>
 */
function checkXML($xml_root_all, $time)
{
    if (is_file($xml_root_all)) {
        if (filesize($xml_root_all) == 0)
            return true;
        $creat_time = filemtime($xml_root_all);    //查看本地xml接口文件的修改时间
        $time_diff = time() - $creat_time;         //与现在时间的时差
        //如果时间差大于2592000 (1个月)就重新生成一下
        if ($time_diff > $time) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}

function GetHttps($url, $charset = "utf-8")
{
    if (extension_loaded('curl')) {
        $file_contents = curl_file_get_contents($url);
    } else {
        $file_contents = @file_get_contents($url);
    }
    if ($charset == "utf-8") {
        return $file_contents;
    } elseif ($charset == "gb2312") {
        $file_contents = iconv("gb2312", "UTF-8", $file_contents);
        return $file_contents;
    }
}

function curl_file_get_contents($url, $timeout=5)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}

//创建目录 递归创建多级目录
function CheckFolder($filedir)
{
    if (!file_exists($filedir)) {
        if (!CheckFolder(dirname($filedir))) {
            return false;
        }
        if (!mkdir($filedir, 0777)) {
            return false;
        }
    }
    return true;
}

?>