<?php

//判断登陆权限
function confirm_power() {
  global $web, $session;
  $session = @explode('|', $_COOKIE['usercookie']);
  if (is_array($session)) {
    $n = count($session);
    if ($n >= 7) {
      if (isset($session[6]) && !empty($session[6])/* && in_array($session[6], (array)$web['login_key'])*/) {
        return 0.5;
      }
      if ($session[4] != 'm') {
        if (!preg_match('/^(\(\d+\-\d+\))+$/', $session[4])) {
          return 1; //会员
        } else {
          if ((string)$_GET['area_id'] != '' && @strstr((string)$session[4], '('.get_arearoot($_GET['area_id']).')') != '') {
            return 3; //当前版主
          } else {
            return 2; //版主
          }
        }
      } else {
        if ($session[0] == $web['manager']) {
          return 5; //基础管理员
        } else {
          return 4; //管理员
        }
      }
    } elseif($n >= 5) {
	  if (strstr($session[5], $web['root'])) {
        $get_162100_session = get_162100_session();
	    if (!$get_162100_session) {
          $session[6] = '162100';
          //$session[7] = $session[5];
	      $session[7] = preg_replace('/login_key(\/.*)?$/i', '', $session[5]);
          //$session[5] = '';
          return 0.5;
		} else {
          return $get_162100_session;
		}
	  }
      return 0; //过客
	} else {
      return 0; //过客
    }
  }
  return 0;
}

function get_162100_session() {
  global $web, $sql, $session, $db;

  if (!function_exists('read_file')) {
    @ require ($GLOBALS['WEATHER_DATA'].'inc/function/read_file.php');
  }
  $member_confirm = read_file($session[5].'/get_user_for162100.php?username='.urlencode($session[0]).'&userkey='.urlencode($session[1].'|'.$session[2]).'');
  if ((string)$member_confirm != 'yes') {
    return 0;
  }

  $cookie_time = time() + floatval($web['time_pos']) * 3600 + 31536000;
  if (!isset($sql['db_err'])) {
    db_conn();
  }
  if ($sql['db_err'] == '') {
    $result = @mysql_query('SELECT username,session_key,point,power FROM '.$sql['pref'].''.$sql['data']['承载成员档案的表名'].' WHERE login_key_162100="'.$session[0].'" LIMIT 1', $db);
    if ($row = @mysql_fetch_assoc($result)) {
      setcookie('usercookie', '', time() - 366 * 60 * 60, '/');
      setcookie('usercookie', ''.$row['username'].'|'.$row['session_key'].'|'.$row['point'].'|'.$row['power'].'|'.$cookie_time.'||'.$web['path'], $cookie_time, '/');
	  $session[0] = $row['username'];
	  list($session[1], $session[2]) = @explode('|', $row['session_key']);
	  $session[3] = $row['point'];
	  $session[4] = $row['power'];
	  $session[5] = $cookie_time;
	  //$session[7] = $web['sitehttp'];
	  $session[7] = $web['path'];
	  
	  if ($row['power'] != 'm') {
        if (!preg_match('/^(\(\d+\-\d+\))+$/', $row['power'])) {
          return 1; //会员
        } else {
          if ((string)$_GET['area_id'] != '' && @strstr((string)$row['power'], '('.get_arearoot($_GET['area_id']).')') != '') {
            return 3; //当前版主
          } else {
            return 2; //版主
          }
        }
      } else {
        if ($session[0] == $web['manager']) {
          return 5; //基础管理员
        } else {
          return 4; //管理员
        }
      }
    }
    @mysql_free_result($result);
  } else {
    //echo $sql['db_err'];
  }
  //@mysql_close();
  return 0;
}


function get_session_key() {
  global $web, $sql, $session, $db;
  if (!isset($GLOBALS['session_key']) || !$GLOBALS['session_key']) {
    $GLOBALS['session_key'] = false;
    if (!isset($sql['db_err'])) {
      db_conn();
    }
    if ($sql['db_err'] == '') {
	  $result = @mysql_query('SELECT session_key FROM '.$sql['pref'].''.$sql['data']['承载成员档案的表名'].' WHERE username="'.$session[0].'" LIMIT 1', $db);
      if ($row = @mysql_fetch_assoc($result)) {
        $GLOBALS['session_key'] = $row['session_key'];
      }
      @mysql_free_result($result);
    } else {
      //echo $sql['db_err'];
    }
    //@mysql_close();
  }
  return $GLOBALS['session_key'];
}

//身份图像
function user_class($power, $point) {
  global $web, $session;
  if ($power == 5) {
    $image = '<img src="inc/images/manager_base.gif" title="基础管理员" onclick="goWebmasterCentral();">';
  } elseif ($power == 4) {
    $image = '<img src="inc/images/manager.gif" title="管理员" onclick="goWebmasterCentral();">';
  } elseif ($power == 3) {
    $image = '<img src="inc/images/admin.gif" title="当前版主" onclick="goWebmasterCentral();">';
  } elseif ($power == 2) {
    $image = '<img src="inc/images/admin_big.gif" title="版主" onclick="goWebmasterCentral();">';
  } elseif ($power == 1){
    if ($point <= abs($web['class_iron'])) {
      $www = $point < 0 ? 0 : ceil($point / (abs($web['class_iron']) / 10) * 3);
      $image = '<img src="inc/images/iron.gif" width="'.($www + 3).'" height="16" style="max-width:100px;" title="等级分'.$point.'-铁级用户">';
    } elseif ($point > abs($web['class_iron']) && $point <= abs($web['class_silver'])) {
      $image = '<img src="inc/images/silver.gif" width="'.(ceil($point / (abs($web['class_silver']) / 10) * 3) + 3).'" height="16" style="max-width:100px;" title="等级分'.$point.'-银级用户">';
    } elseif ($point > abs($web['class_slive']) && $point <= abs($web['class_gold'])) {
      $image = '<img src="inc/images/gold.gif" width="'.(ceil($point / (abs($web['class_gold']) / 10) * 3) + 3).'" height="16" style="max-width:100px;" title="等级分'.$point.'-金级用户">';
    } else {
      $image = '';
      $n = ceil($point / abs($web['class_gold'])) - 1;
      for ($i = 0; $i < $n; $i++) {
        $image .= '<img src="inc/images/diamond.gif">';
        if ($i >= 5) {
          $image .= '…';
          break;
        }
      }
      $image = '<span title="等级分'.$point.'-'.$n.'钻级用户">'.$image.'</span>';
    }
  } elseif ($power == 0.5) {
    $image = '<img src="inc/images/'.$session[6].'.png" title="来自'.$session[6].'登录">';
  } else {
    $image = '<img src="inc/images/i.gif" />';
  }
  return $image;
}


//获取ip
function user_ip() {
  if (@ $_SERVER['HTTP_X_FORWARDED_FOR']) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } elseif (@ $_SERVER['HTTP_CLIENT_IP']) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
  } elseif (@ $_SERVER['REMOTE_ADDR']) {
    $ip = $_SERVER['REMOTE_ADDR'];
  } elseif (@ getenv('HTTP_X_FORWARDED_FOR')) {
    $ip = getenv('HTTP_X_FORWARDED_FOR');
  } elseif (@ getenv('HTTP_CLIENT_IP')) {
    $ip = getenv('HTTP_CLIENT_IP');
  } elseif (@ getenv('REMOTE_ADDR')) {
    $ip = getenv('REMOTE_ADDR');
  }else{
    $ip = '未知IP';
  }
  return $ip;
}

//隐藏ip
function hidden_ip($ip) {
  return preg_replace('/\.\d+$/', '.*', $ip);
}


//获取根目
function get_arearoot($area_id) {
  //if (!$arearoot = strstr($area_id, '_', true)) { //php5.3
    if (!$arearoot = substr($area_id, 0, strpos($area_id, '_'))) {
      $arearoot = $area_id;
    }
  //}
  return $arearoot;
}


?>