<?php
/*
+----------------------------------------------+
|             用户信息查询函数                 |
+----------------------------------------------+
*@$shibie 要查询的字段
*@$word	 查询条件字段
*@$ser    条件参数
*@$mix    修正参数 非空昵称不返回VIP图片
*@返回 string信息
*/
function user($shibie, $word, $ser, $mix) {
    $sql = "SELECT {$word},{$shibie} FROM user WHERE {$word}='{$ser}' ";
    $logininfo = mysql_query($sql);
    if ($logininfo && mysql_num_rows($logininfo) > 0) {
        list($a, $b) = mysql_fetch_row($logininfo);
    }
    if ($shibie == 'head' && $b == NULL) {
        $b = 'http://' . $_SERVER['HTTP_HOST'] . '/style/nohead.jpg';
    }
    if ($shibie == 'nc') {
        $sql = "SELECT {$word},vip FROM user WHERE {$word}='{$ser}' ";
        $vip = mysql_query($sql);
        if ($vip && mysql_num_rows($vip) > 0) {
            list($a1, $b1) = mysql_fetch_row($vip);
            if ($b1 == '1' && $mix == NULL) {
                $VIP_PIC = '<img src="http://' . HTTP_HOST . '/style/vip.gif" alt="vip" style="height:15px;width:20px;">';
            } else {
                $VIP_PIC = NULL;
            }
            return $VIP_PIC . ' ' . $b;
        }
    }
    return $b;
}
function user2($shibie, $xid, $wapid=null) {
    if ($wapid == NULL) {
        $wapid = WAPID;
    }
    $logininfo = mysql_query("SELECT {$shibie},xid,site_u FROM user WHERE xid='{$xid}' AND site_u='{$wapid}'");
    if ($logininfo && mysql_num_rows($logininfo) > 0) {
        list($a, $b, $c) = mysql_fetch_row($logininfo);
    }
    if ($shibie == 'head' && $b == NULL) {
        $b = 'http://' . HTTP_HOST . '/style/nohead.jpg';
    }
    if ($shibie == 'nc') {
        $sql = "SELECT vip,xid,site_u FROM user WHERE user WHERE xid='{$xid}' AND site_u='{$wapid}'";
        $vip = mysql_query($sql);
        if ($vip && mysql_num_rows($vip) > 0) {
            list($a1, $b1, $c1) = mysql_fetch_row($vip);
            if ($a1 == '1') {
                $VIP_PIC = '<img src="http://' . HTTP_HOST . '/style/vip.gif" alt="vip" style="height:15px;width:20px;">';
            } else {
                $VIP_PIC = NULL;
            }
            return $VIP_PIC . ' ' . $b;
        }
    }
    mysql_free_result($logininfo);
    return $a;
}
/*
+----------------------------------------------+
|             系统配置查询函数                 |
+----------------------------------------------+
*/
function sys_config($str) {
    $sql = "SELECT $str FROM sys_config WHERE type='sys'";
    $result = mysql_query($sql);
    if ($result && mysql_num_rows($result) > 0) {
        list($back) = mysql_fetch_row($result);
    }
    return $back;
}
/*金币查询函数*/
function cre($type, $value, $uid) {
    if ($type == '+') {
        $value = crevalue + $value;
    } elseif ($type == '-') {
        $value = crevalue - $value;
    }
    $sql = "UPDATE user SET crevalue='{$value}' WHERE id ='{$uid}'";
    mysql_query($sql);
    return $value;
}
/*
+----------------------------------------------+
|             栏目信息查询函数                 |
+----------------------------------------------+
*/
function class_name($cid, $str, $wapid) {
    $sql = "SELECT cid,{$str} FROM class WHERE siteid='{$wapid}' AND cid='{$cid}'";
    $result = mysql_query($sql);
    list($cid, $strs) = mysql_fetch_row($result);
    if ($str == 'cn') {
        if ($cid == 0 || $cid == NULL || $cid == '') {
            $sql = "SELECT id,stitle FROM user WHERE id='{$wapid}'";
            $result = mysql_query($sql);
            list($wapid, $stitle) = mysql_fetch_row($result);
            $strs = $stitle;
            mysql_free_result($result);
        } else {
            $strs = $strs;
        }
    } else {
        $strs = $strs;
    }
    return $strs;
}
//获取栏目信息2
function lm_info($cid) {
    $wapid = WAPID;
    $result = mysql_query("SELECT * FROM class WHERE siteid='{$wapid}' AND cid='{$cid}'");
    if ($result && mysql_num_rows($result) > 0) {
        return mysql_fetch_array($result);
    }
    return FALSE;
}
function class_get_info($cid, $str) {
    $wapid = WAPID;
    $result = mysql_query("SELECT cid,{$str} FROM class_info WHERE siteid='{$wapid}' AND cid='{$cid}'");
    list($cid, $str) = mysql_fetch_row($result);
    return $str;
}
/*
+----------------------------------------
* 函数获取栏目配置信息
+----------------------------------------
@$cid 栏目id
@返回栏目配置数组
*/
function get_class_cfg($cid) {
    $wapid = WAPID;
    $result = mysql_query("SELECT * FROM class_info WHERE siteid='{$wapid}' AND cid='{$cid}'");
    return mysql_fetch_array($result);
}
/*
+----------------------------------------------+
|             终端设备查询函数                 |
+----------------------------------------------+
*返回TRUE 为移动终端
*返回FALSE 为PC终端
*/
function isMobile() {
    if (isset($_SERVER["HTTP_X_WAP_PROFILE"])) {
        return true;
    }
    if (isset($_SERVER["HTTP_VIA"])) {
        if (strpos($_SERVER["HTTP_VIA"], "wap")) {
            return true;
        }
    }
    if (preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])) {
        //检查USER_AGENT
        return true;
    }
    return FALSE;
}
/*
+------------------------------------------------
* 函数判断网站是否为VIP网站
+------------------------------------------------
@ $siteid 网站ID
*/
function site_vip($siteid) {
    $result = mysql_query("SELECT id,vip FROM sys_config WHERE id='{$siteid}'");
    if ($result && mysql_num_rows($result) > 0) {
        list($id, $back) = mysql_fetch_row($result);
    }
    return $back;
}
/*
+----------------------------------------------+
|             论坛信息查询函数                 |
+----------------------------------------------+
*/
function is_bbsadmin($id) {
    $sql = 'SELECT * FROM `bbs_adminer` WHERE bbser_siteid=\'' . WAPID . '\' AND bbser_classid=\'' . $_GET['cid'] . '\' AND bbs_userid=\'' . $id . '\'';
    $res = mysql_query($sql);
    if ($res && mysql_num_rows($res) > 0) {
        return TRUE;
    } else {
        return FALSE;
    }
}
/*获取会员禁言状态*/
function is_bbshei($id) {
    $sql = 'SELECT * FROM `bbs_adminer` WHERE bbser_siteid=\'' . WAPID . '\' AND bbser_classid=\'' . $_GET['cid'] . '\' AND bbs_userid=\'' . $id . '\'';
    $res = mysql_query($sql);
    if ($res && mysql_num_rows($res) > 0) {
        return TRUE;
    } else {
        return FALSE;
    }
}
/*获取会员勋章函数*/
function myxz($id) {
    $res = mysql_query('SELECT * FROM `bbs_xunzhang_log` WHERE bbser_siteid=\'' . WAPID . '\' AND bbser_userid=\'' . $id . '\'');
    if ($res && mysql_num_rows($res) > 0) {
        while (list($bbser_id, $bbser_userid, $bbser_siteid, $bbser_xzid, $bbser_why, $bbser_time) = mysql_fetch_row($res)) {
            $res2 = mysql_query('SELECT * FROM `bbs_xunzhang` WHERE bbser_siteid=\'' . WAPID . '\' AND bbser_id=\'' . $bbser_xzid . '\'');
            if ($res2 && mysql_num_rows($res2) > 0) {
                list($bbser_id2, $bbser_text2, $bbser_siteid2, $bbser_url2, $bbser_buy2, $bbser_time2) = mysql_fetch_row($res2);
                $list.= '<img src="http://' . HTTP_HOST . $bbser_url2 . '" alt="' . $bbser_text2 . '">';
            }
        }
        return $list;
    }
}
/*
+---------------------------------------------------------
* 跳转函数
+---------------------------------------------------------
* @ string $str 输入要跳转的地址
*/
function tourl($str) {
    header("location: " . $str);
    //结束脚本防止程序继续执行
    exit();
}
/*
+----------------------------------------------+
|             聊天信息查询函数                 |
+----------------------------------------------+
*/
//个人信息
function NoReadMess($MyID, $type) {
    $sql = "SELECT * FROM ask_list WHERE type ='{$type}' AND  read='0' AND sendinfo LINK '%:{$MyID}' ";
    $lists = mysql_query($sql);
    if ($lists && mysql_num_rows($lists) > 0) {
        return mysql_num_rows($lists);
    } else {
        return 0;
    }
}
//统计来自某个人的未读消息
function NoReadMessWho($MyID, $Who, $type) {
    $sql = "SELECT * FROM ask_list WHERE type ='{$type}' AND read='0' AND sendinfo='{$Who}:{$MyID}'";
    $lists = mysql_query($sql);
    if ($lists && mysql_num_rows($lists) > 0) {
        return mysql_num_rows($lists);
    } else {
        return 0;
    }
}
//统计未读短信函数
function NoReadMessNums() {
    $WAPID = WAPID;
    $sql = 'SELECT * FROM `message` WHERE `wapid`=\'' . $WAPID . '\' AND `toid`=\'' . MYID . '\' AND `read`=\'0\';';
    $mess = mysql_query($sql);
    if ($mess && mysql_num_rows($mess) > 0) {
        return mysql_num_rows($mess);
    } else {
        return 0;
    }
}
function SendMessage($title, $content, $user_xid, $idtype, $sendid) {
    if ($sendid == NULL) {
        $sid = MYID;
    } else {
        $sid = $sendid;
    }
    if ($idtype == 'xid') {
        $id = user2('id', $user_xid, WAPID);
    } else {
        $id = $user_xid;
    }
    $mess = mysql_query('INSERT INTO `message` (`messageid`, `sendid`, `toid`, `title`, `content`, `read`, `time`, `wapid`) VALUES (NULL, \'' . $sid . '\', \'' . $id . '\', \'' . $title . '\', \'' . $content . '\', \'0\', \'' . time() . '\', \'' . WAPID . '\');');
    if ($mess && mysql_affected_rows() > 0) {
        return 1;
    } else {
        return 0;
    }
}
function getMessCfg($info) {
    $wapid = WAPID;
    $sql = "SELECT {$info} FROM `message_cfg` WHERE siteid='{$wapid}'";
    $qcon = mysql_query($sql);
    /*数据验证*/
    if ($qcon && mysql_num_rows($qcon) > 0) {
        list($back) = mysql_fetch_row($qcon);
        return $back;
    } else {
        return FALSE;
    }
}
/*
+----------------------------------------------+
|             静态页面缓存函数                 |
+----------------------------------------------+
*/
function ReadHtml() {
    if (!is_dir(root_path . '/temp/html')) {
        mkdir(root_path . '/temp/html');
    }
    if (!is_dir(root_path . '/temp/html/' . WAPID)) {
        mkdir(root_path . '/temp/html/' . WAPID);
    }
    $filename = root_path . '/temp/html/' . WAPID . '/' . MD5($_SERVER["PHP_SELF"] . '_read_' . $_GET['cid'] . '_' . $_GET['id'] . '_' . SID) . '.html';
    //检测是否存在缓存文件 并且检测缓存文件是否过期
    if (is_file($filename) && time() - filemtime($filename) < ReadHtmlTime) {
        //存在 停止程序输出缓存文件
        die(file_get_contents($filename));
    } else {
        //不存在  继续程序  并且生成缓存文件
        if ($_GET['isdata'] == 'yes') {
        } else {
            $content = file_get_contents('http://' . HTTP_HOST . $_SERVER["PHP_SELF"] . '?' . $_SERVER['QUERY_STRING'] . '&sid=' . SID . '&isdata=yes');
            file_put_contents($filename, $content);
        }
    }
}
//验证字符串格式
function YZ($str) {
    $output = '';
    $a = preg_match('/[' . chr(0xa1) . '-' . chr(0xff) . ']/', $str);
    $b = preg_match('/[0-9]/', $str);
    $c = preg_match('/[a-zA-Z]/', $str);
    if ($a && $b && $c) $output = 1; //'汉字数字英文的混合字符串';
    elseif ($a && $b && !$c) $output = 2; //'汉字数字的混合字符串';
    elseif ($a && !$b && $c) $output = 3; //'汉字英文的混合字符串';
    elseif (!$a && $b && $c) $output = 4; //'数字英文的混合字符串';
    elseif ($a && !$b && !$c) $output = 5; //'纯汉字';
    elseif (!$a && $b && !$c) $output = 6; //'纯数字';
    elseif (!$a && !$b && $c) $output = 7; //'纯英文';
    return $output;
}
function slist($page, $limit) {
    if ($page < 1) {
        $page = 1;
    }
    return ($page - 1) * $limit;
}
function htmlzm($str, $mix) {
    if ($mix == NULL) {
        return htmlspecialchars($str, ENT_QUOTES);
    }
    return htmlspecialchars_decode($str, ENT_QUOTES);
}
function ddtpl($tpl_name) {
    $mysql = new D('dd_tpl');
    $find = $mysql->where(array(
        'tplsm' => $tpl_name,
        'tplsiteid' => WAPID
    ))->find();
    if ($find == FALSE) {
        $_POST['tplcontent'] = htmlzm(file_get_contents(root_path . '/DdTpl/sysTpl/dd_' . $tpl_name . '_tpl.ddt'));
        $_POST['tpltitle'] = '叮咚模板';
        $_POST['tpluserid'] = MYID;
        $_POST['tplsiteid'] = WAPID;
        $_POST['tpltime'] = time();
        $_POST['tplsm'] = $tpl_name;
        if ($mysql->insert()) {
            return $_POST['tplcontent'];
        }
    }
    return $find['tplcontent'];
}
function url_come($urlid) {
    $wapid = WAPID;
    $sql = "SELECT url,come,siteid FROM sitelink WHERE urlid='{$urlid}' AND siteid='{$wapid}'";
    $view = mysql_query($sql);
    if ($view && mysql_num_rows($view) > 0) {
        list($url, $come, $siteid) = mysql_fetch_row($view);
    }
    return $come;
}
/*
+------------------------------------+
|         配置文件获取               |
+------------------------------------+
*获取全部配置参数: cfg();
*获取一个参数:cfg(key)|支持数据 cfg(array('key1'=>'key2'))
*/
function cfg($key=null) {
    include root_path . '/config.php';
    if ($key == null) {
        return $cfg;
    }
    if (!is_array($key)) {
        if (isset($cfg[$key])) {
            return $cfg[$key];
        }
    } else {
        foreach ($key as $k => $value) {
            return $cfg[$k][$value];
        }
    }
}
/*
+------------------------------------+
|         Cookie 设置 与 获取        |
+------------------------------------+
*Cookie 获取 cookie(name);
*Cookie 设置 cookie(name,value);
*/
function cookie($name, $value = null) {
    if ($value == null) {
        $cookie = $_COOKIE[$name];
        if (isset($cookie)) {
            return $cookie;
        }
        return FALSE;
    } else {
        $cfg = cfg();
        $time = !empty($cfg['cookie']) ? time() + $cfg['cookie'] : 0;
        SetCookie($name, $value, $time, '/');
    }
}
/**
 +----------------------------------------------------------
 * 代码加亮
 +----------------------------------------------------------
 * @param String  $str 要高亮显示的字符串 或者 文件名
 * @param Boolean $show 是否输出
 +----------------------------------------------------------
 * @return String
 +----------------------------------------------------------
 */
function highlight_code($str, $show = false) {
    if (file_exists($str)) {
        $str = file_get_contents($str);
    }
    $str = stripslashes(trim($str));
    // The highlight string function encodes and highlights
    // brackets so we need them to start raw
    $str = str_replace(array(
        '&lt;',
        '&gt;'
    ) , array(
        '<',
        '>'
    ) , $str);
    // Replace any existing PHP tags to temporary markers so they don't accidentally
    // break the string out of PHP, and thus, thwart the highlighting.
    $str = str_replace(array(
        '&lt;?php',
        '?&gt;',
        '\\'
    ) , array(
        'phptagopen',
        'phptagclose',
        'backslashtmp'
    ) , $str);
    // The highlight_string function requires that the text be surrounded
    // by PHP tags.  Sincludee we don't know if A) the submitted text has PHP tags,
    // or B) whether the PHP tags enclose the entire string, we will add our
    // own PHP tags around the string along with some markers to make replacement easier later
    $str = '<?php //tempstart' . "\n" . $str . '//tempend ?>'; // <?
    // All the magic happens here, baby!
    $str = highlight_string($str, TRUE);
    // Prior to PHP 5, the highlight function used icky font tags
    // so we'll replace them with span tags.
    if (abs(phpversion()) < 5) {
        $str = str_replace(array(
            '<font ',
            '</font>'
        ) , array(
            '<span ',
            '</span>'
        ) , $str);
        $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
    }
    // Remove our artificially added PHP
    $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str);
    $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str);
    $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str);
    // Replace our markers back to PHP tags.
    $str = str_replace(array(
        'phptagopen',
        'phptagclose',
        'backslashtmp'
    ) , array(
        '&lt;?php',
        '?&gt;',
        '\\'
    ) , $str); //<?
    $line = explode("<br />", rtrim(ltrim($str, '<code>') , '</code>'));
    $result = '<div class="code"><ol>';
    foreach ($line as $key => $val) {
        $result.= '<li>' . $val . '</li>';
    }
    $result.= '</ol></div>';
    $result = str_replace("\n", "", $result);
    if ($show !== false) {
        echo ($result);
    } else {
        return $result;
    }
}
// 发送Http状态信息
function send_http($code) {
    static $_status = array(
        // Informational 1xx
        100 => 'Continue',
        101 => 'Switching Protocols',
        // Success 2xx
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        203 => 'Non-Authoritative Information',
        204 => 'No Content',
        205 => 'Reset Content',
        206 => 'Partial Content',
        // Redirection 3xx
        300 => 'Multiple Choices',
        301 => 'Moved Permanently',
        302 => 'Moved Temporarily ', // 1.1
        303 => 'See Other',
        304 => 'Not Modified',
        305 => 'Use Proxy',
        // 306 is deprecated but reserved
        307 => 'Temporary Redirect',
        // Client Error 4xx
        400 => 'Bad Request',
        401 => 'Unauthorized',
        402 => 'Payment Required',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method Not Allowed',
        406 => 'Not Acceptable',
        407 => 'Proxy Authentication Required',
        408 => 'Request Timeout',
        409 => 'Conflict',
        410 => 'Gone',
        411 => 'Length Required',
        412 => 'Precondition Failed',
        413 => 'Request Entity Too Large',
        414 => 'Request-URI Too Long',
        415 => 'Unsupported Media Type',
        416 => 'Requested Range Not Satisfiable',
        417 => 'Expectation Failed',
        // Server Error 5xx
        500 => 'Internal Server Error',
        501 => 'Not Implemented',
        502 => 'Bad Gateway',
        503 => 'Service Unavailable',
        504 => 'Gateway Timeout',
        505 => 'HTTP Version Not Supported',
        509 => 'Bandwidth Limit Exceeded'
    );
    if (array_key_exists($code, $_status)) {
        header('HTTP/1.1 ' . $code . ' ' . $_status[$code]);
    }
}
function userinfo($type, $value = null) {
    if ($type == 'get') {
        if ($_GET['sid'] != NULL) {
            $sid = $_GET['sid'];
        } else {
            $sid = $_SESSION[MD5($_SERVER['HTTP_HOST']) ];
        }
        $arr = array();
        $arri = array();
        if ($sid != NULL) {
            $arr = explode('-', $sid);
            $arri = array(
                'sid' => $arr[0],
                'ver' => $arr[1],
                'css' => $arr[2]
            );
            return $arri;
        }
        return null;
    } else {
        if (is_array($value)) {
            $value = implode('-', $value);
        }
        $_SESSION[MD5($_SERVER['HTTP_HOST']) ] = $value;
    }
}
function runbin($name) {
    if (is_array($name)) {
        foreach ($name as $vale) {
            eval(hex2bin(file_get_contents(root_path . '/include/bin/' . $name . '.dd.bin')));
        }
    } else {
        eval(hex2bin(file_get_contents(root_path . '/include/bin/' . $name . '.dd.bin')));
    }
}
/*
+-----------------------------------
* CSS函数 
+-----------------------------------
* @$id CSS标示ID
*/
function css($id) {
    if ($id == NULL || $id == 0) {
        $result = mysql_query("SELECT cssid,csscontent,mr FROM css WHERE mr='1' AND site='" . WAPID . "' LIMIT 1");
        list($cssid, $csscontent, $mr) = mysql_fetch_row($result);
        $select = $cssid;
    }
    $result = mysql_query("SELECT cssid,csscontent FROM css WHERE cssid = '{$select}' AND site='" . WAPID . "'");
    list($cssid, $csscontent) = mysql_fetch_row($result);
    $css = htmlspecialchars_decode($csscontent, ENT_QUOTES);
    return $css;
}
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
    // 动态密匙长度，相同的明文会生成不同密文就是依靠动态密匙
    $ckey_length = 4;
    // 密匙
    $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
    // 密匙a会参与加解密
    $keya = md5(substr($key, 0, 16));
    // 密匙b会用来做数据完整性验证
    $keyb = md5(substr($key, 16, 16));
    // 密匙c用于变化生成的密文
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()) , -$ckey_length)) : '';
    // 参与运算的密匙
    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb) , 0, 16) . $string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    // 产生密匙簿
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    // 用固定的算法，打乱密匙簿，增加随机性，好像很复杂，实际上对并不会增加密文的强度
    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 == 'DECODE') {
        // 验证数据有效性，请看未加密明文的格式
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb) , 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        // 把动态密匙保存在密文里，这也是为什么同样的明文，生产不同密文后能解密的原因
        // 因为加密后的密文可能是一些特殊字符，复制过程可能会丢失，所以用base64编码
        return $keyc . str_replace('=', '', base64_encode($result));
    }
}
function B($str,$type=null){
	if($type != null){
		return base64_decode($str);
	}
	return base_encode($str);
}