1
wishcraft
IP Locality & Region of Origin on API's with Chronolabs & Profile with XOOPS 2.5.6

This is something I worked on last night at http://creator.labs.coop with XOOPS 2.5.6 & The Standard Profile Module with it.

IP Locality This in registration then with permissions on edit's in a seperate field can store their registration IP Details from https://lookups.labs.coop with the IP & Region of Origin on API's With their nearest location to that IP Address with the 'nearby' call on https://places.labs.coop with Chronolabs & Profile with XOOPS 2.5.6.

Code to Alter

/profile/admin/field.php

// Change lines 82 - 84 too.
'rank'         => _PROFILE_AM_RANK,
'userip'       => _PROFILE_AM_USERIP,
'userlocality' => _PROFILE_AM_LOCALITY);


/profile/class/field.php[/u]

// add lines 146 - 185
            
case "userlocality":
                if (empty(
$value)) {
                    
$approx json_decode($this->getURL("https://lookups.labs.coop/v1/country/".$this->getIP(true)."/json.api"), true);
                    
$places json_decode($this->getURL("https://places.labs.coop/v1/nearby/".$approx['location']['coordinates']['latitude']."/".$approx['location']['coordinates']['longitude']."/25/json.api"), true);
                    
$locations = array();
                    foreach(
$places['results']['places'] as $countrykey => $areas) {
                        foreach(
$places['results']['countries'] as $countrykeyb => $country) {
                            if (
$countrykey==$countrykeyb
                            {
                                
$countryname $country['Country'];
                            }
                        }
                        foreach(
$areas as $areakey => $area) {
                            
$locations[$areakey] = $area['RegionName'] . ' ('.$countryname.')';
                        }
                    }
                    
$selected '';
                } else {
                    
$array json_decode($valuetrue);
                    
$locations $array['options'];
                    
$selected $array['value'];
                }
                
$selectbox = new XoopsFormSelect(''$name.'[value]'$selected13false);
                
$selectbox->addOptionArray($locations);
                
$element = new XoopsFormElementTray($caption);
                
$element->addElement(new XoopsFormHidden($name.'[options]'json_encode($locations)));
                
$element->addElement($selectbox);
                break;
                    
            case 
"userip":
                
$element = new XoopsFormElementTray($caption);
                if (empty(
$value)) {
                    
$value $this->getURL("https://lookups.labs.coop/v1/country/".$this->getIP(true)."/json.api");
                }
                  
$element->addElement(new XoopsFormHidden($name$value));
                   
$element->addElement(new XoopsFormLabel(''$this->formatIPInfo($value)));
                   break;

// Add Lines 265 - 365
    /**
     *
     * Formats for display the IP Info from a JSON String in Profile Module
     *
     * @author Simon Roberts (Chronolabs) simon@labs.coop
     *
     * @param string $json IP String in JSON from https://lookups.labs.coop
     *
     * @return string
     */
    
function formatIPInfo($json='')
    {
        
$array json_decode($jsontrue);
        
$ret '';
        
$ret .= sprintf('IP Address: %s.
'
$array['ip']);
        
$ret .= sprintf('Country: %s.
'
$array['country']['name']);
        
$ret .= sprintf('Region/Province: %s.
'
$array['location']['region']);
        
$ret .= sprintf('Town/City/Suburb: %s(%s).
'
$array['location']['city'], $array['location']['postcode']);
        
$ret .= sprintf('
Latitude: %sLongitude: %s (GMT %s).
'
$array['location']['coordinates']['latitude'], $array['location']['coordinates']['longitude'], $array['location']['gmt']);
        return 
$ret;
    }
    
    
/**
     *
     * gets a URL and Data with cURL for the Profile Module
     * 
     * @author Simon Roberts (Chronolabs) simon@labs.coop
     *
     * @param string $url URI Path to retrieve
     *
     * @return string
     */
    
function getURL($url ''$removecookie true
    {
        
$cookies XOOPS_UPLOAD_PATH DIRECTORY_SEPARATOR 'cURL_'.md5($url).'.cookie';
        try {
            
            
// Intialises cURL
            
$cc curl_init($url);
            
            
// Sets cURL options
            
curl_setopt($ccCURLOPT_CONNECTTIMEOUT120);
            
curl_setopt($ccCURLOPT_TIMEOUT120);
            
curl_setopt($ccCURLOPT_COOKIEJAR$cookies);
            
curl_setopt($ccCURLOPT_RETURNTRANSFER1);
            
curl_setopt($ccCURLOPT_USERAGENTXOOPS_VERSION " :: date-creator.com - (".PHP_VERSION.")");
            
curl_setopt($ccCURLOPT_SSL_VERIFYPEERfalse);
            
            
// Executes and Closes cURL
            
$data curl_exec($cc);
            
curl_close($cc);
        }
        catch (
Exception $e) { trigger_error('Error with cURL: '.$e); return false; }
        if (
$removecookie==true)
            
unlink($cookies);
        return 
$data;
    }
    
    
/**
     *
     * gets the True IPv4/IPv6 address of the client using the Profile Module
     * 
     * @author Simon Roberts (Chronolabs) simon@labs.coop
     *
     * @param boolean $asString Whether to return an address or network long integer
     *
     * @return mixed
     */
    
function getIP($asString true){
            
// Gets the proxy ip sent by the user
        
$proxy_ip '';
        if (!empty(
$_SERVER['HTTP_X_FORWARDED_FOR'])) {
            
$proxy_ip $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else
        if (!empty(
$_SERVER['HTTP_X_FORWARDED'])) {
            
$proxy_ip $_SERVER['HTTP_X_FORWARDED'];
        } else
        if (! empty(
$_SERVER['HTTP_FORWARDED_FOR'])) {
            
$proxy_ip $_SERVER['HTTP_FORWARDED_FOR'];
        } else
        if (!empty(
$_SERVER['HTTP_FORWARDED'])) {
            
$proxy_ip $_SERVER['HTTP_FORWARDED'];
        } else
        if (!empty(
$_SERVER['HTTP_VIA'])) {
            
$proxy_ip $_SERVER['HTTP_VIA'];
        } else
        if (!empty(
$_SERVER['HTTP_X_COMING_FROM'])) {
            
$proxy_ip $_SERVER['HTTP_X_COMING_FROM'];
        } else
        if (!empty(
$_SERVER['HTTP_COMING_FROM'])) {
            
$proxy_ip $_SERVER['HTTP_COMING_FROM'];
        }
        if (!empty(
$proxy_ip) && $is_ip preg_match('/^([0-9]{1,3}.){3,3}[0-9]{1,3}/'$proxy_ip$regs) && count($regs) > 0)  {
            
$the_IP $regs[0];
        } else {
            
$the_IP $_SERVER['REMOTE_ADDR'];
        }
            
        
$the_IP = ($asString) ? $the_IP ip2long($the_IP);
        return 
$the_IP;
    }

// Add Lines 446 - 449
            
case "userlocality":
            case 
"userip":
                return 
json_decode($valuetrue);
                break;

// Add Lines 542 - 549
            
case "userlocality":
                
$value['options'] = json_decode($value['options'], true);
                return 
json_encode($value);
                break;
                
            case 
"userip":
                  return 
$value;
                  break;

// Add Lines 633 - 638
            
case "userlocality":
            case 
"userip":
                if (
$obj->getVar('field_valuetype') != XOBJ_DTYPE_INT) {
                    
$obj->setVar('field_valuetype'XOBJ_DTYPE_OTHER);
                }
                break;


/profile/include/forms.php
// Change Lines 79 - 81
            
'yesno' => _PROFILE_AM_YESNO,
            
'userip' => _PROFILE_AM_USERIP,
            
'userlocality' => _PROFILE_AM_LOCALITY);

// Add Lines 233 - 235
            
case "userip":
            case 
"userlocality":
                break;


/profile/language/english/admin.php
// Add Lines 81 & 82
define('_PROFILE_AM_USERIP''IP Address Details');
define('_PROFILE_AM_LOCALITY''User's Location (IP Based)');

2
wishcraft
Re: IP Locality & Region of Origin on API's with Chronolabs & Profile with XOOPS 2.5.6

You can see this hack running on the front basic registration option of this development site :: https://trust.labs.coop/register.php

Login

Who's Online

376 user(s) are online (212 user(s) are browsing Support Forums)


Members: 0


Guests: 376


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits