>  기사  >  php教程  >  谁能帮我把php代码翻译成java的代码。跪求急啊

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

PHP中文网
PHP中文网원래의
2016-05-26 08:18:571150검색

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

<?php
class User extends CI_Model {
public function __construct() {
parent::__construct();
defined(&#39;RUN_TIME&#39;) || define(&#39;RUN_TIME&#39;, time());
$this->load->model(&#39;share&#39;);
}

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

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

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

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

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

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


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


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

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

if ( CURRENT_ENV == &#39;production&#39; ) {
$uc_key = &#39;6e1a233d3c9d4677a617096d70e0c612&#39;;
} else {
$uc_key = &#39;adc0a1d842a7545633b06f451aa3a74c&#39;;
}


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

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

$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, &#39;DECODE&#39;, $uc_key));
 
        if ( empty($return[&#39;uid&#39;]) ) {
            return &#39;-997&#39;;
        }
 
        $uid = $checked[$auth_code] = $return[&#39;uid&#39;];
 
        // 验证失败
        if ($uid < 0 ) {
return $uid;
}

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

return $uid;
}

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

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

$info = join(&#39;&#39;, $info);

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


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

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

$this->cncndb = $this->load->database(&#39;cncndb&#39;, TRUE);
$query = $this->cncndb->select(&#39;uid, &#39; . $type)->from($table)->where_in(&#39;uid&#39;, $uids)->get();
foreach ($query->result_array() as $k => $v) {
$return[$v[&#39;uid&#39;]] = $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 = &#39;&#39;, $expire = NULL) {
// 保存session
$this->share->cncn_session_start();
        $_SESSION = array();   // 设置登录session前,应该清空之前的session,避免用户会串在一起
        $_SESSION[&#39;uid&#39;] = $uid;
        $_SESSION[&#39;nickname&#39;] = $nickname;             // 未传递时需要从用户库获取
        $_SESSION[&#39;contact_name&#39;] = $contact_name;     // 未传递时需要从用户库获取
        $_SESSION[&#39;user_type&#39;] = $user_type;           // 未传递时需要从用户库获取
        $_SESSION[&#39;login_time&#39;]   = time();
    }
 
}


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


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:Php Mysql PDO다음 기사:PHP daemon