<commit_before><?php

/**
 * Edmunds
 *
 * The fast PHP framework for building web applications.
 *
 * @license   This file is subject to the terms and conditions defined in file 'license.md', which is part of this source code package.
 */

namespace Edmunds\Localization\Models;

use Edmunds\Bases\Models\BaseModel;
use Edmunds\Http\Request;
use Edmunds\Auth\Models\User;
use Edmunds\Registry\Admin\Channel;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Model\City;

/**
 * The helper for the browser
 *
 * @property User $user
 * @property string $ip
 * @property string $continent_code
 * @property string $continent_name
 * @property string $country_code
 * @property string $country_name
 * @property string $region_code
 * @property string $region_name
 * @property string $city_name
 * @property string $postal_code
 * @property float $latitude
 * @property float $longitude
 * @property string $timezone
 */
class Location extends BaseModel
{
	const	GEOIP_DIR	= 'app/geoip',
			GEOIP_CITY	= 'GeoLite2-City.mmdb';

	/**
	 * The table associated with the model.
	 * @var string
	 */
	protected $table = 'user_locations';

	/**
	 * The primary key for the model.
	 * @var string
	 */
	protected $primaryKey = 'user_id';

	/**
	 * The user
	 * @return BelongsTo
	 */
	public function user()
	{
		return $this->belongsTo(config('app.auth.models.user'));
	}

	/**
	 * Initialize
	 * @param string $ip
	 */
	public function initialize($ip)
	{
		if (self::isEnabled())
		{
			$cityDetails = $this->getDetailsCity($ip);

			if ($cityDetails)
			{
				$this->ip = $ip;

				$this->continent_code = $cityDetails->continent->code;
				$this->continent_name = $cityDetails->continent->name;

				$this->country_code = $cityDetails->country->isoCode;
				$this->country_name = $cityDetails->country->name;

				$this->region_code = $cityDetails->mostSpecificSubdivision->isoCode;
				$this->region_name = $cityDetails->mostSpecificSubdivision->name;

				$this->city_name = $cityDetails->city->name;
				$this->postal_code = $cityDetails->postal->code;

				$this->latitude = $cityDetails->location->latitude;
				$this->longitude = $cityDetails->location->longitude;
				$this->timezone = $cityDetails->location->timeZone;
			}
		}
	}

	/**
	 * Return the GeoIP of browser
	 * @return Reader
	 */
	private function getGeoIPCity()
	{
		return new Reader(storage_path(self::GEOIP_DIR . '/' . self::GEOIP_CITY));
	}

	/**
	 * Return the details of city db
	 * @param string $ip
	 * @return City
	 */
	private function getDetailsCity($ip)
	{
		try
		{
			$reader = $this->getGeoIPCity();
			return $reader->city($ip);
		}
		catch(AddressNotFoundException $e)
		{
			return false;
		}
		catch(\InvalidArgumentException $e)
		{
			return false;
		}<commit_msg>#116: Implemented Google App Engine Location headers.<commit_after><?php

/**
 * Edmunds
 *
 * The fast PHP framework for building web applications.
 *
 * @license   This file is subject to the terms and conditions defined in file 'license.md', which is part of this source code package.
 */

namespace Edmunds\Localization\Models;

use Edmunds\Bases\Models\BaseModel;
use Edmunds\Http\Request;
use Edmunds\Auth\Models\User;
use Edmunds\Registry\Admin\Channel;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Model\City;

/**
 * The helper for the browser
 *
 * @property User $user
 * @property string $ip
 * @property string $continent_code
 * @property string $continent_name
 * @property string $country_code
 * @property string $country_name
 * @property string $region_code
 * @property string $region_name
 * @property string $city_name
 * @property string $postal_code
 * @property float $latitude
 * @property float $longitude
 * @property string $timezone
 */
class Location extends BaseModel
{
	const	GEOIP_DIR	= 'app/geoip',
			GEOIP_CITY	= 'GeoLite2-City.mmdb';

	/**
	 * The table associated with the model.
	 * @var string
	 */
	protected $table = 'user_locations';

	/**
	 * The primary key for the model.
	 * @var string
	 */
	protected $primaryKey = 'user_id';

	/**
	 * The user
	 * @return BelongsTo
	 */
	public function user()
	{
		return $this->belongsTo(config('app.auth.models.user'));
	}

	/**
	 * Initialize
	 * @param string $ip
	 */
	public function initialize($ip)
	{
		if (self::isEnabled())
		{
			$cityDetails = $this->getDetailsCity($ip);

			if ($cityDetails)
			{
				$this->ip = $ip;

				$this->continent_code = $cityDetails->continent->code;
				$this->continent_name = $cityDetails->continent->name;

				$this->country_code = $cityDetails->country->isoCode;
				$this->country_name = $cityDetails->country->name;

				$this->region_code = $cityDetails->mostSpecificSubdivision->isoCode;
				$this->region_name = $cityDetails->mostSpecificSubdivision->name;

				$this->city_name = $cityDetails->city->name;
				$this->postal_code = $cityDetails->postal->code;

				$this->latitude = $cityDetails->location->latitude;
				$this->longitude = $cityDetails->location->longitude;
				$this->timezone = $cityDetails->location->timeZone;
			}
		}
	}

	/**
	 * Return the details of city db
	 * @param string $ip
	 * @return City
	 */
	protected function getDetailsCity($ip)
	{
		if (app()->isGae())
		{
			return $this->getGaeIPCity();
		}

		try
		{
			$reader = $this->getGeoIPCity();
			return $reader->city($ip);
		}
		catch(AddressNotFoundException $e)
		{
			return false;
		}
		catch(\InvalidArgumentException $e)
		{
			return false;
		}
	}

	/**
	 * Return the GeoIP of browser
	 * @return Reader
	 */
	protected function getGeoIPCity()
	{
		return new Reader(storage_path(self::GEOIP_DIR . '/' . self::GEOIP_CITY));
	}

	/**
	 * Get Ip city in Google App Engine
	 * @return Std_Class
	 */
	protected function getGaeIPCity()
	{
		$request = Request::getInstance();

		$country = $request->getHeader('X-AppEngine-Country');
		if ($country == 'ZZ')
		{
			return false;
		}

		$region = $request->getHeader('X-AppEngine-Region');
		$city = $request->getHeader('X-AppEngine-City');
		$cityLatLong = $request->getHeader('X-AppEngine-CityLatLong');
		if ($cityLatLong)
		{
			list($latitude, $longitude) = explode(',', $cityLatLong);
			$latitude = floatval($latitude);
			$longitude = floatval($longitude);
		}
		else
		{
			$latitude = $longitude = null;
		}

		return json_decode(json_encode([
			'continent' => [
				'code' => null,
				'name' => null,
			],
			'country' => [
				'isoCode' => $country,
				'name' => null,
			],
			'mostSpecificSubdivision' => [
				'isoCode' => $region,
				'name' => null,
			],
			'city' => [
				'name' => $city,
			],
			'postal' => [
				'code' => null,
			],
			'location' => [
				'latitude' => $latitude,
				'longitude' => $longitude,
				'timeZone' => null,
			],
		]));
{'fraction_non_alphanumeric': 0.14871039056742816, 'fraction_numerical': 0.0016212232866617538, 'mean_word_length': 3.5944482058226135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4882659', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2239, 'n_words': 501}