みずの日記

ボカロとかニコ動とか忘備録とか

ドメイン失効

ドメイン更新を忘れていて、失効させてしまいました。
なので、MobileIPもファイルをDLすることができません。

どうせPHPなんだから、ここにソースを書いておきます。

./system/application/libraries/Mobileip.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Code Igniter
 *
 * An open source application development framework for PHP 4.3.2 or newer
 *
 * @package    CodeIgniter
 * @author     Yoshiyuki Kadotani
 * @copyright  Copyright (c) 2009 Yoshiyuki Kadotani <kado@miscast.org>
 * @license    http://www.codeignitor.com/user_guide/license.html
 * @since      Version 1.0
 * 
 */

/**
 * IPアドレスが日本の携帯IPリストに一致するか検索する
 */
class Mobileip
{
	
	private $CI;
	private $_cachedir;
	private $_carrier_url;
	private $_carrier_file;
	private $_carrier_name;
	private $_reload;
	
	private $_remote_ip;
	private $_remote_carrier = "other";
	private $_is_mobile = FALSE;
	
	
	function __construct()
	{
		
		//初期化
		$this->CI =& get_instance();
		$this->CI->load->helper('file');
		$this->CI->load->config('mobileip_config');
		$this->_remote_ip = $_SERVER['REMOTE_ADDR'];
		
		//Config値読み込み
		$this->_cachedir = $this->CI->config->item('mobileip_cache_dir');
		$this->_carrier_url = $this->CI->config->item('mobileip_carrier_url');
		$this->_reload = $this->CI->config->item('mobileip_reload_time');
		$this->_carrier_name = $this->CI->config->item('mobileip_carrier_name');
		
		//IPキャッシュ生成
		$this->_set_iplist();
		
		//IP判定
		$this->_judge_ip();
		
	}
	
	// public functions
	
	function isMobile()
	{
		return $this->_is_mobile;
	}
	
	function get_remote_carrier()
	{
		return $this->_remote_carrier;
	}
	
	function set_remote_ip($ip)
	{
		$this->_remote_ip = $ip;
		$this->_judge_ip();
	}
	
	// local functions
	
	function _judge_ip()
	{
		foreach($this->_carrier_file as $carrier => $file){
			$fp = fopen($file, 'r');
			while($line = fgets($fp, 1024)){
				$carrier_ip = explode('/', $line);
				
				$ipc = $this->_get_ip_bit($carrier_ip[0]);
				$mask = $this->_get_mask_bit($carrier_ip[1]);
				$ipr = $this->_get_ip_bit($this->_remote_ip);
				
				if(($ipc & $mask) == ($ipr & $mask)){
					$this->_is_mobile = TRUE;
					$this->_remote_carrier = $this->_carrier_name[$carrier];
					return TRUE;
				}
				
			}
			fclose($fp);
		}
		return FALSE;
	}
	
	function _set_iplist()
	{
		//IPキャッシュがあるか
		foreach($this->_carrier_url as $carrier => $url){
			
			$this->_carrier_file[$carrier] = $file =  $this->_cachedir .'ip_'. $carrier .'.txt';
			
			if(!file_exists($file) or (filemtime($file) < time()-$this->_reload)){
				//なければ作る
				$iptext = $this->_get_iplist_from_url($url);
				write_file($file, $iptext);
			}
		}
	}
	
	function _get_iplist_from_url($_url)
	{
		$html = file_get_contents($_url);
		if(strpos($_url, 'au')){
			
			$pattern = "/(\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).+\n.+(\/\d{2})/";
			if(preg_match_all($pattern, $html, $matches)){
				$iptext = "";
				for($i=0; $i<=count($matches[0])-1; $i++){
					$iptext .= $matches[1][$i] . $matches[2][$i] ."\n";
				}
				return $iptext;
			}
		} else {
				
			$pattern = "/\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{2}/";
			if(preg_match_all($pattern, $html, $matches)){
				$iptext = "";
				for($i=0; $i<=count($matches[0])-1; $i++){
					$iptext .= $matches[0][$i] ."\n";
				}
				return $iptext;
			}
			
		}
	}
	
	function _get_mask_bit($_bit)
	{
		$mask = 0;
		for($i=1; $i<=$_bit; $i++){
			$mask++;
			$mask = $mask << 1;
		}
		$mask = $mask << 32-$_bit;
		return $mask;
	}
	
	function _get_ip_bit($ip)
	{
		$ips = explode('.', $ip);
		$ipb = ($ips[0] << 24) | ($ips[1] << 16) | ($ips[2] << 8) | ($ips[3]);
		return $ipb;
	}

}
./system/application/config/mobileip_config.php
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// ------------------------------------------------------------------------
// mobile ip
// ------------------------------------------------------------------------

// cache directory
$config['mobileip_cache_dir'] = BASEPATH . 'cache/';

// carrier URL
$config['mobileip_carrier_url']['au'] = 'http://www.au.kddi.com/ezfactory/tec/spec/ezsava_ip.html';
$config['mobileip_carrier_url']['docomo'] = 'http://www.nttdocomo.co.jp/service/imode/make/content/ip/';
$config['mobileip_carrier_url']['softbank'] = 'http://creation.mb.softbank.jp/web/web_ip.html';
$config['mobileip_carrier_url']['willcom'] = 'http://www.willcom-inc.com/ja/service/contents_service/create/center_info/';
$config['mobileip_carrier_url']['em'] = 'http://developer.emnet.ne.jp/ipaddress.html';

// carrier name
$config['mobileip_carrier_name']['au'] = 'au';
$config['mobileip_carrier_name']['docomo'] = 'docomo';
$config['mobileip_carrier_name']['softbank'] = 'softbank';
$config['mobileip_carrier_name']['willcom'] = 'willcom';
$config['mobileip_carrier_name']['em'] = 'emobile';

// ip list reload time
$config['mobileip_reload_time'] = 60*60*24;

?>