<?php
/**
* 检查收集到的IP信息
* 运行时间：1 6 * * *
* @Copyright(c) 2007-2009
* @Author: iOrange
* @Email: yzxh24@gmail.com
* @Version $id:file 2009-12-06 04:42:27
*/
/* vim: set ts=4 sw=4: */
require_once dirname(dirname(__FILE__)) . '/init.php';

$sql = "SELECT * FROM `".Adip::table()."` WHERE `status` = 0";
$query = $db->query($sql);
while ($row = $db->getArr($query))
{
	$ip = $row['ip'];
	$id = $row['id'];
	$gateway = $db->select()->from(Gateway::table())->where(array('ip'=>$ip,'commited'=>1))->find(true);
	if (!empty($gateway))
	{
		continue;
	}
	
	$url = 'http://ip.cn/getip.php?action=queryip&ip_url=' . $ip;
	$x = 0;
	do {
		$content = file_get_contents($url);
		$x++;
	} while (empty($content) && $x < 3);
	
	$content = iconv('gb2312', 'utf-8', $content);
	$msg = explode('来自：', $content);
	if (!empty($msg[1]) && (false !== stripos($msg[1], '移动') || false !== stripos($msg[1], '联通')))
	{
		$value = explode(' ', $msg[1]);
		$type = false !== stripos($value[1], '移动') ? '移动' : '联通';
		$db->insert('gateway')->value(array('ip'=>$ip,'commited'=>1,'type'=>$type, 'province'=> $value[0],'create_date'=>date('Y-m-d')))->q();

		$db->update(Adip::table())->value(array('status'=>1))->where(array('id'=>$id))->q();
	}
	else
	{
		$db->update(Adip::table())->value(array('status'=>2))->where(array('id'=>$id))->q();
	}
}

// 刷新缓存
updateMobileGateway();
