[ Index ]

MailPress 544

[ 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_country($ip)
  22      {
  23          $content = self::get_ip_info($ip);
  24          if ( (!$content) || (!isset($content['country'])) ||(2 < strlen($content['country'])) ) return self::no_state;
  25          return $content['country'];
  26      }
  27  
  28  	public static function get_subcountry($ip)
  29      {
  30          $content = self::get_ip_info($ip);
  31          if ( (!$content) || (!isset($content['subcountry'])) || empty($content['subcountry']) ) return self::no_state;
  32          return $content['subcountry'];
  33      }
  34  
  35  	public static function get_USstate($ip)
  36      {
  37          $response = wp_remote_get("http://api.hostip.info/get_html.php?ip=$ip");
  38          $x = (is_wp_error($response)) ? false : $response['body'];
  39          if (!$x || empty($x) || (2 < strlen($x))) return self::no_state;
  40  
  41          $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');
  42          $USstate = substr($x, strlen($x)-2, 2);
  43          return (in_array(substr($USstate, 0, 2), $USstates)) ? substr($USstate, 0, 2) : self::no_state;
  44      }
  45  
  46  	public static function get_ip_info($ip)
  47      {
  48          new MP_Ip_providers();
  49          $providers = MP_Ip_providers::get_all();
  50          foreach($providers as $provider => $pdata)
  51          {
  52              list($file, $ip_url, $cache) = self::get_context($pdata, $ip);
  53              if ($file == $cache) break;
  54          }
  55  
  56          switch ($file)
  57          {
  58              case $cache :
  59                  $content = @file_get_contents($file);
  60                  $valid   = (!empty($content) && $content);
  61              break;
  62              default :
  63                  unset($providers[self::provider]);
  64                  do
  65                  {
  66                      $provider    = array_rand($providers);
  67                      $pdata    = $providers[$provider];
  68                      list($file, $ip_url, $cache) = self::get_context($pdata, $ip);
  69  
  70                      $response     = wp_remote_get($ip_url);
  71                      $content     = (is_wp_error($response)) ? false : $response['body'];
  72                      $valid     = (!empty($content) && $content);
  73  
  74                      if ($valid)
  75                      {
  76                          switch($pdata['type'])
  77                          {
  78                              case 'xml' :
  79                                  if (!simplexml_load_string($content))     $valid = false;
  80                              break;
  81                              case 'array' :
  82                                  if (!is_serialized($content))         $valid = false;
  83                              break;
  84                          }
  85                          if ($valid) $valid = apply_filters('MailPress_ip_content_' . $provider, $valid, $content);    
  86                      }
  87                      if (!$valid) unset($providers[$provider]);
  88                      if (empty($providers)) break;
  89                  } while (!$valid);
  90  
  91                  if ($valid) file_put_contents($cache, $content);
  92              break;
  93          }
  94  
  95          if (!$valid) return false;
  96  
  97          switch($provider)
  98          {
  99              case self::provider :
 100                  return (is_serialized($content)) ? unserialize($content) : false;
 101              break;
 102              default :
 103                  return apply_filters('MailPress_ip_data_' . $provider, $content, $ip);
 104              break;
 105          }
 106      }
 107  
 108  	public static function get_context($pdata, $ip)
 109      {
 110          $file  = $ip_url = vsprintf( $pdata['url'], apply_filters('MailPress_ip_url_' . $pdata['id'] , array($ip)) );
 111          $cache = MP_ABSPATH . 'tmp/' . ((!isset($pdata['md5'])) ?  md5($ip_url) : $ip_url) . '.spc';
 112  
 113          if (is_file($cache))
 114          {
 115              $cache_days = ( (isset($pdata['cache_days'])) ? $pdata['cache_days'] : self::cache_days )*24*60*60;
 116              if (filemtime($cache) >= (time() - $cache_days))    $file = $cache;
 117              else                                    @unlink($cache);
 118          }
 119          return array($file, $ip_url, $cache);
 120      }
 121  
 122  	public static function get_address($lat, $lng)
 123      {
 124          $url = "http://maps.googleapis.com/maps/api/geocode/xml?latlng=$lat,$lng&sensor=false";
 125  
 126          $xml = file_get_contents($url);
 127          if ( !$xml || empty($xml) ) return '';
 128  
 129          $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
 130  
 131          if (!$xml) return '';
 132          if ('OK' != $xml->status) return '';
 133          return (isset($xml->result[0]->formatted_address)) ? $xml->result[0]->formatted_address : '';
 134      }
 135  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1