<?php
/**
* Mudder通行证反响接口--客户端 (安装在整合系统目录)
* @author moufer<moufer@163.com>
* @copyright wa666.cn
* @version $Id: mudder_passport_client.php 2 2008-1-31 22:25 $
*/

// 通行证接口验证 - 服务器URL
$mud_server_url = 'http://localhost/mudder_gbk/api/mudder_passport_sever.php';

// 登录后跳转页面
$login_forward = 'http://localhost/mudder_gbk/my.php';
// 注册后跳转页面
$reg_forward = 'http://localhost/mudder_gbk/my.php';
// 登出后跳转页面
$logout_forward = 'http://localhost/mudder_gbk/index.php';

// 通行证加密码
$mud_authkey = 'Ac6aAPJ1bH2i';
// Mudder系统Cookie前缀
$mud_cookiepre = 'jr4gO_';

// Cookie作用域
$cpath = '/';
// 全局域名，不需要http://www之类的地址符和主机名，例如：mudder.com.cn,本地localhost则留空
$domain = "";

function SyncMudder($username,$email,$action,$cookielife) {
    global $mud_authkey,$mud_cookiepre,$cpath,$domain,$mud_server_url;
    $keys = array('username','email','action');
    $param = '';
    foreach($keys as $val) {
        if(!empty($$val)) $param .= $val.'='.urlencode($$val).'&';
    }
    $signhash = substr(md5($username.$mud_authkey),0,24);
    $param .= 'signhash='.$signhash;
    $mud_server_url = $mud_server_url.'?senddata='.base64_encode($param);

    if(function_exists("file_get_contents")) {
        $recvdata = @file_get_contents($mud_server_url) or die("远程通信错误(001)");
    } else {
        $fp = @fopen($mud_server_url,'r');
        $recvdata = $fp ? @fread($fp,200) : '';
        $fp && @fclose($fp);
        $recvdata or die("远程通信错误(002)");
    }

    if(substr($recvdata,0,10) == 'mudder_ok_') {
        $uid = trim(ereg_replace("^mudder_ok_", "", $recvdata));
        if($uid != '') {
            LoginMudder($uid, $username, $cookielife);
            return 'ok';
        } else {
            return $recvdata;
        }
    } else {
        return $recvdata;
    }
}

function LoginMudder($uid, $username, $life) {
    global $mud_cookiepre, $mud_authkey, $cpath, $domain;
    $timestamp = time();
    $hash = substr(md5($mud_authkey.$uid.$username),8,8);
    setcookie($mud_cookiepre.'uid', $uid, $timestamp + $life, $cpath, $domain);
    setcookie($mud_cookiepre.'username', $username, $timestamp + $life, $cpath, $domain);
    setcookie($mud_cookiepre.'hash', $hash, $timestamp + $life, $cpath, $domain);
}

?>
