<?php
/**
 * IP转城市
 * @作者 苏相锟 <admin@phpok.com>
 * @主页 https://www.phpok.com
 * @版本 5.x
 * @授权 GNU Lesser General Public License https://www.phpok.com/lgpl.html 
 * @时间 2022年2月19日
**/

/**
 * 安全限制，防止直接访问
**/
if(!defined("PHPOK_SET")){
	exit("<h1>Access Denied</h1>");
}

class ip2region_lib extends _init_lib
{
	private $obj;
	public function __construct()
	{
		include_once(EXTENSION.'ip2region/ip2region.class.php');
		$this->obj = new Ip2Region(EXTENSION.'ip2region/ip2region.db');
	}

	public function ip2city($ip)
	{
		$data   = $this->obj->btreeSearch($ip);
		if(!$data || !$data['region']){
			return false;
		}
		$tmp = explode("|",$data['region']);
		$data = array();
		$data['country'] = $tmp[0];
		if($data['country'] == '中国'){
			$data['province'] = $tmp[2];
			$data['city'] = $tmp[3];
			$data['net'] = $tmp[4];
		}
		return $data;
	}
}
