мамам - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of мамам.php

  1. <?php
  2.  
  3. /******************************************************************************/
  4. /******************************************************************************/
  5.  
  6. class CHBSGeoLocation
  7. {
  8.     /**************************************************************************/
  9.  
  10.     function __construct()
  11.     {
  12.         $this->server=array
  13.         (
  14.             1                                                                   =>  array
  15.             (
  16.                 'name'                                                          =>  __('KeyCDN [keycdn.com]','chauffeur-booking-system'),
  17.                 'api_url_address'                                               =>  'https://tools.keycdn.com/geo.json?host={CLIENT_IP}'
  18.             ),
  19.             2                                                                   =>  array
  20.             (
  21.                 'name'                                                          =>  __('IP-API [ip-api.com]','chauffeur-booking-system'),
  22.                 'api_url_address'                                               =>  'http://ip-api.com/json/{CLIENT_IP}'
  23.             ),
  24.             3                                                                   =>  array
  25.             (
  26.                 'name'                                                          =>  __('ipstack [ipstack.com]','chauffeur-booking-system'),
  27.                 'api_url_address'                                               =>  'http://api.ipstack.com/{CLIENT_IP}?access_key={API_KEY}'
  28.             )           
  29.         );
  30.     }
  31.     
  32.     /**************************************************************************/
  33.     
  34.     function getIPAddress()
  35.     {
  36.         $address=null;
  37.         
  38.         $data=array('HTTP_CLIENT_IP','HTTP_X_FORWARDED_FOR','REMOTE_ADDR');
  39.         
  40.         foreach($data as $value)
  41.         {
  42.             if(array_key_exists($value,$_SERVER))
  43.             {
  44.                 $address=$_SERVER[$value];
  45.                 break;
  46.             }     
  47.         }
  48.         
  49.         return($address);
  50.     }
  51.     
  52.     /**************************************************************************/
  53.     
  54.     function getCountryCode()
  55.     {
  56.         $document=$this->getDocument();
  57.         if($document===false) return(null);
  58.         return($document['country_code']);
  59.     }
  60.     
  61.     /**************************************************************************/
  62.     
  63.     function getCoordinate()
  64.     {
  65.         $Validation=new CHBSValidation();
  66.         
  67.         if(($document=$this->getDocument())===false)
  68.             return(array('lat'=>0,'lng'=>0));
  69.         
  70.         $coordinate=array
  71.         (
  72.             'lat'                                                               =>  strval($document['latitude']),
  73.             'lng'                                                               =>  strval($document['longitude'])
  74.         );
  75.         
  76.         foreach($coordinate as $index=>$value)
  77.         {
  78.             if($Validation->isEmpty($value))
  79.                 $coordinate[$index]=0;
  80.         }
  81.         
  82.         return($coordinate);
  83.     }
  84.     
  85.     /**************************************************************************/
  86.     
  87.     function getDocument()
  88.     {
  89.         if(!array_key_exists(CHBSOption::getOption('geolocation_server_id'),$this->getServer())) return(false);
  90.         
  91.         if(!ini_get('allow_url_fopen')) return(false);
  92.         
  93.         $url=$this->server[CHBSOption::getOption('geolocation_server_id')]['api_url_address'];
  94.         
  95.         if(CHBSOption::getOption('geolocation_server_id')==3)
  96.             $url=preg_replace(array('/{CLIENT_IP}/','/{API_KEY}/'),array($this->getIPAddress(),CHBSOption::getOption('geolocation_server_id_3_api_key')),$url);
  97.         else $url=preg_replace(array('/{CLIENT_IP}/'),array($this->getIPAddress()),$url);
  98.         
  99.         $context=stream_context_create(array('http'=>array('timeout'=>3)));
  100.         
  101.         if(($document=file_get_contents($url,false,$context))===false) return(false);
  102.                 
  103.         if((is_null($document)) || ($document===false)) return(false);
  104.              
  105.         $data=array();
  106.         
  107.         $document=json_decode($document);    
  108.  
  109.         $LogManager=new CHBSLogManager();
  110.         $LogManager->add('geolocation',2,print_r($document,true));  
  111.         
  112.         switch(CHBSOption::getOption('geolocation_server_id'))
  113.         {
  114.             case 1:
  115.                 
  116.                 if($document->{'status'}!='success') return(false);
  117.                 
  118.                 $data['latitude']=strval($document->{'data'}->{'geo'}->{'latitude'});
  119.                 $data['longitude']=strval($document->{'data'}->{'geo'}->{'longitude'});
  120.                 $data['country_code']=strval($document->{'data'}->{'geo'}->{'country_code'});
  121.                 
  122.             break;
  123.                 
  124.             case 2:
  125.                 
  126.                 if(!property_exists($document,'countryCode')) return(false);
  127.                 
  128.                 $data['latitude']=strval($document->{'lat'});
  129.                 $data['longitude']=strval($document->{'lon'});
  130.                 $data['country_code']=strval($document->{'countryCode'});
  131.                 
  132.             break;
  133.                 
  134.             case 3:
  135.                 
  136.                 if(!property_exists($document,'country_code')) return(false);
  137.                 
  138.                 $data['latitude']=strval($document->{'latitude'});
  139.                 $data['longitude']=strval($document->{'longitude'});
  140.                 $data['country_code']=strval($document->{'country_code'});
  141.                  
  142.             break;
  143.         }
  144.  
  145.         return($data);
  146.     }
  147.     
  148.     /**************************************************************************/
  149.     
  150.     function getServer()
  151.     {
  152.         return($this->server);
  153.     }
  154.     
  155.     /**************************************************************************/
  156. }
  157.  
  158. /******************************************************************************/
  159. /******************************************************************************/