hoolux /system/expressionengine/modules/ip_to_nation/models/ip_to_nation_data.php

Language PHP Lines 190
MD5 Hash d8135fad4c7e52fb5049ec05fc1e0e25 Estimated Cost $2,738 (why?)
Repository https://bitbucket.org/tdevonshire/hoolux.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ip_to_nation_data extends CI_Model {

	private $table = 'ip2nation';
	private $c_table = 'ip2nation_countries';

	// ----------------------------------------------------------------------

	/**
	 * Get a country by ip address
	 */
	function find($ip)
	{
		$BIN = $this->to_binary($ip);

		$query = $this->db
			->select('country')
			->where("ip_range_low <= '".$BIN."'", '', FALSE)
			->where("ip_range_high >= '".$BIN."'", '', FALSE)
			->order_by('ip_range_low', 'desc')
			->limit(1, 0)
			->get($this->table);

		if ( ! $query->num_rows())
		{
			return FALSE;
		}

		return $query->row('country');
	}

	// ----------------------------------------------------------------------

	/**
	 * Replace IP data with that of all the files in `$dir`
	 */
	function load_dir($dir)
	{
		$dir = rtrim($dir, '/');
		return $this->load(
			glob($dir.'/*.csv')
		);
	}

	// ----------------------------------------------------------------------

	/**
	 * Replace IP data with that in `$files`
	 */
	function load($files)
	{
		// get old banned info
		$banned = $this->db
			->select('code')
			->get_where($this->c_table, array('banned' => 'y'))
			->result_array();

		// all we care about is if it's set
		$banned = array_map('array_pop', $banned);
		$banned = array_flip($banned);

		// remove all data
		$this->db->truncate($this->table);
		$this->db->truncate($this->c_table);


		// populate the db with file data
		$files = (array) $files;
		$countries = array();

		foreach ($files as $file)
		{
			$add_countries = $this->_read_and_insert($file);
			$countries = array_merge($countries, $add_countries);
		}

		// sort the new ones for a pretty db
		sort($countries);
		$countries = array_unique($countries);
		$countries = array_map('strtolower', $countries);

		// merge with the banned and insert
		foreach ($countries as &$c)
		{
			$c = array(
				'code' => $c,
				'banned' => isset($banned[$c]) ? 'y' : 'n'
			);
		}

		$this->db->insert_batch($this->c_table, $countries);
	}

	// ----------------------------------------------------------------------

	/**
	 * Ban a countries by their country code
	 */
	function ban($countries)
	{
		// Set all countries to unbanned
		$this->db->update($this->c_table, array('banned' => 'n'));

		// And then reban those we know about
		if (count($countries))
		{
			$this->db
				->where_in('code', $countries)
				->update($this->c_table, array('banned' => 'y'));
		}
	}

	// ----------------------------------------------------------------------

	/**
	 * Convert an IP address to its IPv6 packed format
	 */
	function to_binary($addr)
	{
		// all IPv4 go to IPv6 mapped
		if (strpos($addr, ':') === FALSE && strpos($addr, '.') !== FALSE)
		{
			$addr = '::'.$addr;
		}
		return inet_pton($addr);
	}

	// ----------------------------------------------------------------------

	/**
	 * Read the ip file and update the db
	 *
	 * Returns a list of countries in the file
	 */
	private function _read_and_insert($file)
	{
		if (($fh = fopen($file, 'r')) === FALSE)
		{
			return FALSE;
		}

		$batch = 0;
		$insert = array();
		$countries = array();

		while (($data = fgetcsv($fh, 300, ',')) !== FALSE)
		{
			if (count($data) < 5)
			{
				continue;
			}

			// $low, $high, $dec_low, $dec_high, $c_code, $c_name
			$insert[$batch++] = array(
				'ip_range_low'	=> $this->to_binary($data[0]),
				'ip_range_high'	=> $this->to_binary($data[1]),
				'country'		=> strtolower($data[4])
			);

			$countries[] = $data[4];

			if ($batch >= 1000)
			{
				$countries = array_unique($countries);
				$this->db->insert_batch('exp_ip2nation', $insert);

				$batch = 0;
				$insert = array();
			}
		}

		// deal with leftovers
		if (count($insert))
		{
			$this->db->insert_batch('exp_ip2nation', $insert);
		}

		$countries = array_unique($countries);
		sort($countries);

		fclose($fh);

		return $countries;
	}
}

/* End of file Iptonation_math.php */
/* Location: system/expressionengine/modules/ip_to_nation/libraries/Iptonation_math.php
 */
Back to Top