/
README
101 lines (81 loc) · 2.74 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
btoさんの作ったsfMobileIPPluginをCakePHPに移植してみました。
<http://blog.bz2.jp/archives/2009/04/ipsymfony.html>
<http://openpear.org/package/sfMobileIPPlugin>
spyc<http://code.google.com/p/spyc/> と PEAR Net_IPv4<http://pear.php.net/package/Net_IPv4> を使ってます。
以下の様に配置すると動くようになってます。
APP/vendors/spyc/spyc.php
/Net/IPv4.php
デフォルトでは CACHE/mobile_ip_component/range.php にキャッシュデータが保存されます。
yamlファイルが更新されるとキャッシュが再生成されます。
// 使用例-1
<?php
class HogeController extends AppController
{
var $uses = null ;
var $components = array('mobileip.MobileIp') ;
function index()
{
pr($this->MobileIp->carrier()) ;
}
}
// 使用例-2 以下の様にすると多少カスタマイズできます。
<?php
class HogeController extends AppController
{
var $uses = null ;
var $components = array(
'mobileip.MobileIp' => array(
'file' => 'YAMLファイルのパス',
)
) ;
function index()
{
pr($this->MobileIp->carrier()) ;
}
}
// IPアドレスをDBで管理
// 下記の様なモデルを書けばDBでIPアドレスを管理できますね。 下記のモデルを使うにはこんな感じで。
// var $components = array(
// 'mobileip.MobileIp' => array(
// 'ModelName' => 'Ipaddress'
// )
// ) ;
//
<?php
App::import('Model', 'mobileip.MobileIp') ;
class Ipaddress extends MobileIp
{
var $name = 'Ipaddress';
var $useTable = 'ipaddresses' ;
function afterSave($created)
{
$last_update_cache = $this->getCacheDir() . DS . 'lastupdated';
@touch($last_update_cache) ;
}
function find($conditions = null, $fields = array(), $order = null, $recursive = null)
{
if ( $conditions != 'range' )
return parent::find($conditions, $fields, $order, $recursive) ;
$cacheDir = $this->getCacheDir();
$folder = new Folder($cacheDir);
$folder->create($cacheDir, 0777);
$last_update_cache = $this->getCacheDir() . DS . 'lastupdated';
$cacheFile = $this->getCacheFile();
if (file_exists($cacheFile) &&
($this->_getLastModified($last_update_cache) <= filemtime($cacheFile))) {
return include($cacheFile);
}
$ipaddresses = $this->find('all') ;
if ( !is_array($ipaddresses) )
return false ;
$mobile_ips = array() ;
foreach( $ipaddresses as $ip ) {
$mobile_ips[$ip['Ipaddress']['type']][] = long2ip($ip['Ipaddress']['ip']) . "/{$ip['Ipaddress']['mask']}" ;
}
$data = $this->_get_ranges($mobile_ips) ;
$file = new File($cacheFile, true);
$file->write($data);
$file->close();
return include($cacheFile);
}
}