欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

谁能帮我把php代码翻译成java的代码。跪求急啊

程序员文章站 2024-01-23 22:38:28
...
谁能帮我把php代码翻译成java的代码。跪求急啊
<?php
class User extends CI_Model {
public function __construct() {
parent::__construct();
defined('RUN_TIME') || define('RUN_TIME', time());
$this->load->model('share');
}

function iflogin($redirect = true, $forward='') {
$this->share->cncn_session_start();
$uid = 0;

//检查是SESSION和COOKIE里的uch_auth是否一致,确保是同一个人
$still_login = false;
if(isset($_SESSION['uch_auth']) && isset($_COOKIE['uch_auth'])
 && ($_SESSION['uch_auth'] == $_COOKIE['uch_auth'])){
$still_login = true;
}
if (isset($_SESSION['uid']) && $_SESSION['uid'] && $still_login) {
$uid = $_SESSION['uid'];

// 没有相关缓存时,做退出的操作,is.cncn.net这里不再做单独的同步退出
if (empty($_COOKIE['uch_auth'])) {
$this->doexit(); 
}

// 每5分钟检测登录期间是否登录进程已被删除(比如其他进程修改了密码)
if ( $this->chk_auth_session($uid) < 0 ) {
$this->doexit(); 
}

// 每60秒更换一次session_id
if ( empty($_SESSION['last_sess_time']) ) {
$_SESSION['last_sess_time'] = time();
} elseif (time() - $_SESSION['last_sess_time'] > 60) {
session_regenerate_id(TRUE);
$_SESSION['last_sess_time'] = time();
}
} else {
if (isset($_COOKIE['uch_auth']) && $_COOKIE['uch_auth']) {
$uid = $this->chk_auth_cookie();
// cookie信息通过了验证时,从cookie恢复登录状态
//------------------------------------------------
if ($uid > 0) {
// 生成session、cookie
$this->set_login($uid);
// 添加登录进程检测相关的session值,以便于后续session进行检测
$_SESSION['last_uc_check_time'] = time();

//------------------------------------------------
} else {
$uid = 0;
}
}
}
if (!$uid && $redirect) {
// 如果是来自ajax的请求,则不做跳转,否则ajax接收到的就是登录页面的html代码
$ajax = $this->input->get_post('ajax');
if (empty($ajax)) {
if (empty($forward)) {
$forward = empty($_SERVER['REQUEST_URI']) ? $this->uri->uri_string : ' 
 $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];
}
header('Location: '. NET_URL. 'login?forward=' . urlencode($forward));
exit;
} else {
$this->share->cncn_exit('您尚未登录');
}
}
return $uid;
}


/*
* 验证保持登录的cookie是否正确
* @param string $check_type 验证类型(默认为cookie):
* cookie 通过cookie恢复登录时进行的验证
* session 恢复登录后已有session时进行的验证,
这时不验证登录权限的有效时间(因为有可能cookie已失效,但session还在的情况)
* @return mixed 验证登录时,返回该cookie对应的UID,失败时,返回FALSE
*/
function chk_auth_cookie($check_type='cookie') {


if ($check_type == 'cookie') {
if (empty($_COOKIE['uch_auth'])) {
return -1;
} 
$auth_code = $_COOKIE['uch_auth'];
$uid = 0;
} else {
if (empty($_SESSION['uch_auth'])) {
return -1;
} 
$auth_code = $_SESSION['uch_auth'];
$uid = $_SESSION['uid'];
}


$info = array(
'uid' => $uid,
'auth_code' => $auth_code,
'client_ip' => $_SERVER['REMOTE_ADDR'],
'client_info' => $this->get_client_info(),
'user_agent' => $this->get_client_info(FALSE),
'check_type' => $check_type,
);

static $checked;
if ($checked[$auth_code]) {
return $checked[$auth_code];
}

if ( CURRENT_ENV == 'production' ) {
$uc_key = '6e1a233d3c9d4677a617096d70e0c612';
} else {
$uc_key = 'adc0a1d842a7545633b06f451aa3a74c';
}


$info = array(
'sign' => $this->authcode(serialize($info), 'ENCODE', $uc_key),
'from' => 'finance',
);

// 向服务端发起验证请求
if ( CURRENT_ENV == 'production' ) {
$url = 'http://www.cncn.net/homepage/check_uc_session';
} else {
$url = 'http://192.168.1.158:876/homepage/check_uc_session';
}

$info = http_build_query($info);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $info);
$r = curl_exec($ch);

curl_close($ch);

$return = unserialize($this->authcode($r, 'DECODE', $uc_key));
 
        if ( empty($return['uid']) ) {
            return '-997';
        }
 
        $uid = $checked[$auth_code] = $return['uid'];
 
        // 验证失败
        if ($uid < 0 ) {
return $uid;
}

// 确定返回的验证结果是否已失效
if ( time() - $return['time'] > 300 ) {
return '-996';
}

return $uid;
}

/*
* 每5分钟检测登录期间是否登录进程已被删除(比如其他进程修改了密码)
* @param int $uid 当前登录用户,保存在session里的uid
*
* @return mixed 验证登录时,返回该cookie对应的UID,失败时,返回FALSE
*/
function chk_auth_session($uid) {
if ( isset($_SESSION['last_uc_check_time']) && time() - $_SESSION['last_uc_check_time'] > 300 ) {
$uid = $this->chk_auth_cookie('session');
            if ( $uid < 0 ) {                    
                // 登录凭证验证失败后,将当前的session删除
                if (session_id() != '') {   // check login被调用多次。。所以这里需要加个判断
                    session_destroy();
                }
            } else {
                $_SESSION['last_uc_check_time'] = time();
            }
        }
        if ( empty($uid) || $uid < 0 ) {
            $uid = 0;
        }
        return $uid;
    }
 
    function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
 
      $ckey_length = 4;
 
      $key = md5($key ? $key : CNCN_API_KEY);
      $keya = md5(substr($key, 0, 16));
      $keyb = md5(substr($key, 16, 16));
      $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 {
    return $keyc.str_replace('=', '', base64_encode($result));
  }
}

/*
* 获取用户的浏览器信息,进行md5加密后返回
*
* @return string
*/
function get_client_info($md5=TRUE) {
$info = array(
'agent' => empty($_SERVER['HTTP_USER_AGENT']) ? 'user_agent' : $_SERVER['HTTP_USER_AGENT'], 
'lang' => empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? 'gbk' : strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), 
);

$info = join('', $info);

return $md5 ? md5($info) : $info;
}


function doexit() {
$this->share->cncn_session_start();
        $syn = '';
 
        // 彻底清空session
        $_SESSION = array();
        if (ini_get("session.use_cookies")) {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], 
            $params["secure"], $params["httponly"]
            );
        }
        session_destroy();
 
        setcookie("uc_auth", "", time() - 3600, '/', config_item('cookiedomain'));
        setcookie("uch_auth", "", time() - 3600, '/', config_item('cookiedomain'));
        echo $syn . '<script>location.href="/";</script>';
exit;
}

/**
* 获取uid获取相应的字段信息
*
* @param mixed $uids 需要获取字段的uid,可为多个,超过一个时用数组传递
* @param $type 需要获取的字段类型
*
*/
public function get_value_byid($uids, $type, $table = 'member') {
if (!$is_array = is_array($uids)) {
$uids = array($uids);
}
$return = array();

$this->cncndb = $this->load->database('cncndb', TRUE);
$query = $this->cncndb->select('uid, ' . $type)->from($table)->where_in('uid', $uids)->get();
foreach ($query->result_array() as $k => $v) {
$return[$v['uid']] = $v[$type];
}

return $is_array ? $return : array_pop($return);
}


/*
* 对用户登录的session进行处理
* @param int $uid 用户编号
* @param string $nickname 用户昵称,如果未传递,则根据uid从member表获取
* @param int $expire cookie保存时间,单位为秒,默认为NULL,即浏览器关闭后就失效
* @return void
*/

function set_login($uid, $nickname = '', $expire = NULL) {
// 保存session
$this->share->cncn_session_start();
        $_SESSION = array();   // 设置登录session前,应该清空之前的session,避免用户会串在一起
        $_SESSION['uid'] = $uid;
        $_SESSION['nickname'] = $nickname;             // 未传递时需要从用户库获取
        $_SESSION['contact_name'] = $contact_name;     // 未传递时需要从用户库获取
        $_SESSION['user_type'] = $user_type;           // 未传递时需要从用户库获取
        $_SESSION['login_time']   = time();
    }
 
}


以上就是谁能帮我把php代码翻译成java的代码。跪求急啊的内容,更多相内容请关注PHP中文网(www.php.cn)!

相关标签: php,java