Skip to content

kaz29/mobileip

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
  }
}

About

Transplanting 'sfMobileIPPlugin' to CakePHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages