[ Index ]

MailPress 7.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Ip.class.php (source)

   1  <?php
   2  class MP_Ip
   3  {
   4      const cache_days     = 120;            // keep it 120 days
   5      const provider     = 'mailpress';
   6      const no_state    = 'ZZ';
   7  
   8  	public static function get_all( $ip )
   9      {
  10          return self::get_ip_info( $ip );
  11      }
  12  
  13  	public static function get_latlng( $ip )
  14      {
  15          $content = self::get_ip_info( $ip );
  16          if ( !$content ) return false;
  17          if ( !isset( $content['geo']['lat'] ) ) return false;
  18          return $content['geo'];
  19      }
  20  
  21  	public static function get_current_latlng()
  22      {
  23          $ip = filter_input( INPUT_SERVER, 'REMOTE_ADDR' );
  24          return self::get_latlng( $ip );
  25      }
  26  
  27  	public static function get_country( $ip )
  28      {
  29          $content = self::get_ip_info( $ip );
  30          if ( ( !$content ) || ( !isset( $content['country'] ) ) ||( 2 < strlen( $content['country'] ) ) ) return self::no_state;
  31          return $content['country'];
  32      }
  33  
  34  	public static function get_subcountry( $ip )
  35      {
  36          $content = self::get_ip_info( $ip );
  37          if ( ( !$content ) || ( !isset( $content['subcountry'] ) ) || empty( $content['subcountry'] ) ) return self::no_state;
  38          return $content['subcountry'];
  39      }
  40  
  41  	public static function get_USstate( $ip )
  42      {
  43          $response = wp_remote_get( "http://api.hostip.info/get_html.php?ip=$ip" );
  44          $x = ( is_wp_error( $response ) ) ? false : $response['body'];
  45          if ( !$x || empty( $x ) || ( 2 < strlen( $x ) ) ) return self::no_state;
  46  
  47          $USstates = array( 'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY' );
  48          $USstate = substr( $x, strlen( $x )-2, 2 );
  49          return ( in_array( substr( $USstate, 0, 2 ), $USstates ) ) ? substr( $USstate, 0, 2 ) : self::no_state;
  50      }
  51  
  52  	public static function get_ip_info( $ip )
  53      {
  54          new MP_Ip_providers();
  55          $providers = MP_Ip_providers::get_all();
  56          foreach( $providers as $provider => $pdata )
  57          {
  58              list( $file, $ip_url, $cache ) = self::get_context( $pdata, $ip );
  59              if ( $file == $cache ) break;
  60          }
  61  
  62          switch ( $file )
  63          {
  64              case $cache :
  65                  $content = @file_get_contents( $file );
  66                  $valid   = ( !empty( $content ) && $content );
  67              break;
  68              default :
  69                  unset( $providers[self::provider] );
  70                  do
  71                  {
  72                      $provider    = array_rand( $providers );
  73                      $pdata    = $providers[$provider];
  74                      list( $file, $ip_url, $cache ) = self::get_context( $pdata, $ip );
  75  
  76                      $response     = wp_remote_get( $ip_url );
  77                      $content     = ( is_wp_error( $response ) ) ? false : $response['body'];
  78                      $valid     = ( !empty( $content ) && $content );
  79  
  80                      if ( $valid )
  81                      {
  82                          switch( $pdata['type'] )
  83                          {
  84                              case 'xml' :
  85                                  if ( !simplexml_load_string( $content ) )     $valid = false;
  86                              break;
  87                              case 'array' :
  88                                  if ( !is_serialized( $content ) )         $valid = false;
  89                              break;
  90                          }
  91                          if ( $valid ) $valid = apply_filters( "MailPress_ip_content_{$provider}", $valid, $content );    
  92                      }
  93                      if ( !$valid ) unset( $providers[$provider] );
  94                      if ( empty( $providers ) ) break;
  95                  } while ( !$valid );
  96  
  97                  if ( $valid ) file_put_contents( $cache, $content );
  98              break;
  99          }
 100  
 101          if ( !$valid ) return false;
 102  
 103          switch( $provider )
 104          {
 105              case self::provider :
 106                  return ( is_serialized( $content ) ) ? unserialize( $content ) : false;
 107              break;
 108              default :
 109                  return apply_filters( "MailPress_ip_data_{$provider}", $content, $ip );
 110              break;
 111          }
 112      }
 113  
 114  	public static function get_context( $pdata, $ip )
 115      {
 116          $file  = $ip_url = vsprintf( $pdata['url'], apply_filters( "MailPress_ip_url_{$pdata['id']}" , array( $ip ) ) );
 117          $cache = MP_UPL_ABSPATH . 'ip/' . ( ( !isset( $pdata['md5'] ) ) ?  md5( $ip_url ) : $ip_url ) . '.spc';
 118  
 119          if ( is_file( $cache ) )
 120          {
 121              $cache_days = ( ( isset( $pdata['cache_days'] ) ) ? $pdata['cache_days'] : self::cache_days )*24*60*60;
 122              if ( filemtime( $cache ) >= ( time() - $cache_days ) )    $file = $cache;
 123              else                                    @unlink( $cache );
 124          }
 125          return array( $file, $ip_url, $cache );
 126      }
 127  
 128  	public static function get_address( $lat, $lng )
 129      {
 130          /* deprecated */
 131  
 132          return MP_Map::get_address( $lng, $lat );
 133      }
 134  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1