/** * Fetches the location from the hostip.info web service * * @param string $ip */ function get_location($location_map) { $city = ''; $state = ''; $country = ''; $country_code = ''; $latitude = ''; $longitude = ''; // check to see if ip is in map if (array_key_exists('ip_address', $location_map) && !empty($location_map['ip_address']) && empty($location_map['country'])) { // check to see if ip is valid and not a private address if (filter_var($location_map['ip_address'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) { // create crawler $crawler = new owa_http(); $crawler->read_timeout = owa_coreAPI::getSetting('base', 'ws_timeout'); // hit web service $crawler->fetch(sprintf($this->ws_url, $location_map['ip_address'])); owa_coreAPI::debug(sprintf("HostIp web service response code: %s", $crawler->crawler->response_code)); $location = $crawler->crawler->results; // replace delimiter $location = str_replace("\n", "|", $location); // convert string to array $loc_array = explode("|", $location); $result = array(); // convert array to multi dimensional array foreach ($loc_array as $k => $v) { if (!empty($v)) { list($name, $value) = explode(":", $v, 2); $result[$name] = $value; } } // parse the city line of response if (isset($result['City']) && !empty($result['City'])) { // lowercase $result['City'] = strtolower($result['City']); // explode into array $city_array = explode(',', $result['City']); // city name is always first $city = $city_array[0]; // if there is a second element then it's a state if (isset($city_array[1])) { $state = $city_array[1]; } } // parse country line of response if (isset($result['Country']) && !empty($result['Country'])) { //lowercase $result['Country'] = strtolower($result['Country']); // set country $country_parts = explode('(', trim($result['Country'])); $country = $country_parts[0]; // if there is a second element then it's a country code. if (isset($country_parts[1])) { $country_code = substr($country_code, 0, -1); } // debug owa_coreAPI::debug('Parse of Hostip country string: ' . $result['Country'] . ' c: ' . $country . ' cc: ' . $country_code); } // set latitude if (isset($result['Latitude']) && !empty($result['Latitude'])) { $latitude = $result['Latitude']; } // set longitude if (isset($result['Longitude']) && !empty($result['Longitude'])) { $longitude = $result['Longitude']; } } // fail safe checks for empty, unknown or private adddress labels // check to make sure values are not "private address" contain "unknown" or "xx" if (empty($city) || strpos($city, 'private') || strpos($city, 'unknown')) { $city = '(not set)'; } // check state if (empty($state) || strpos($state, 'private') || strpos($state, 'unknown')) { $state = '(not set)'; } // check country if (empty($country) || strpos($country, 'unknown') || strpos($country, 'private')) { $country = '(not set)'; } // check country code if (empty($country_code) || strpos($country_code, 'xx') || strpos($country_code, 'unknown') || strpos($country_code, 'private')) { $country_code = '(not set)'; } $location_map['city'] = strtolower(trim($city)); $location_map['state'] = strtolower(trim($state)); $location_map['country'] = strtolower(trim($country)); $location_map['country_code'] = strtoupper(trim($country_code)); $location_map['latitude'] = trim($latitude); $location_map['longitude'] = trim($longitude); // log headers if status is not a 200 if (isset($crawler->response_code) && !strpos($crawler->response_code, '200')) { owa_coreAPI::debug(sprintf("HostIp web service response headers: %s", print_r($crawler->crawler->headers, true))); } } return $location_map; }
/** * Notify Event Handler * * @param unknown_type $event * @access public */ function notify($event) { $medium = $event->get('medium'); // if there is no session referer then return if (!$event->get('session_referer')) { return OWA_EHS_EVENT_HANDLED; } // Make entity $r = owa_coreAPI::entityFactory('base.referer'); $r->load($event->get('referer_id')); if (!$r->wasPersisted()) { // Set id if ($event->get('referer_id')) { $r->set('id', $event->get('referer_id')); } else { $r->set('id', $r->generateId($event->get('session_referer'))); } // set referer url $r->set('url', $event->get('session_referer')); // Set site $url = owa_lib::parse_url($event->get('session_referer')); $r->set('site', $url['host']); if ($medium === 'organic-search') { $r->set('is_searchengine', true); } // set title. this will be updated later by the crawler. $r->set('page_title', '(not set)'); // Crawl and analyze refering page if (owa_coreAPI::getSetting('base', 'fetch_refering_page_info') && $medium != 'organic-search') { //owa_coreAPI::debug('hello from logReferer'); $crawler = new owa_http(); //$crawler->fetch($this->params['HTTP_REFERER']); $res = $crawler->getRequest($event->get('session_referer')); owa_coreAPI::debug('http request response: ' . print_r($res, true)); //Extract Title $title = trim($crawler->extract_title()); if ($title) { $r->set('page_title', owa_lib::utf8Encode($title)); } $se = $r->get('is_searchengine'); //Extract anchortext and page snippet but not if it's a search engine... if ($se != true) { $snippet = $crawler->extract_anchor_snippet($event->get('inbound_page_url')); if ($snippet) { if (function_exists('iconv')) { $snippet = iconv('UTF-8', 'UTF-8//TRANSLIT', $snippet); } $r->set('snippet', $snippet); } $anchortext = $crawler->anchor_info['anchor_text']; if ($anchortext) { $r->set('refering_anchortext', owa_lib::utf8Encode($anchortext)); } } } // Persist to database $ret = $r->create(); if ($ret) { return OWA_EHS_EVENT_HANDLED; } else { return OWA_EHS_EVENT_FAILED; } } else { owa_coreAPI::debug('Not Persisting. Referrer already exists.'); return OWA_EHS_EVENT_HANDLED; } }