PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/phpip2country.class.php

http://php-ip-2-country.googlecode.com/
PHP | 294 lines | 169 code | 23 blank | 102 comment | 17 complexity | f8a317ea1380f2351d48b12bd1c41762 MD5 | raw file
  1. <?php
  2. /**
  3. * phpIp2Country class
  4. *
  5. * @author Mariusz G?rski
  6. * @copyright 2008 Mariusz G?rski
  7. * @name phpIp2Country
  8. * @version 1.0
  9. * @link http://code.google.com/p/php-ip-2-country/
  10. *
  11. * @todo (static?) function to get full countries list
  12. */
  13. define('IP_STR',1);
  14. define('IP_VALUE',2);
  15. define('IP_RANGE_NUMERICAL',3);
  16. define('IP_RANGE',4);
  17. define('IP_REGISTRY',5);
  18. define('IP_ASSIGNED_UNIXTIME',6);
  19. define('IP_COUNTRY_ISO',7);
  20. define('IP_COUNTRY_CODE',8);
  21. define('IP_COUNTRY_NAME',9);
  22. define('IP_INFO',10);
  23. class phpIp2Country {
  24. /**
  25. * @param string $ip
  26. * @param ip $method
  27. */
  28. function __construct($ip,$dbConfig=array()){
  29. if(!$this->chceckIpAddr($ip)){
  30. die('Bad IP address! Should be in xxx.xxx.xxx.xxx format!');
  31. }else{
  32. $this->ip = $ip;
  33. }
  34. if(!is_array($dbConfig)){
  35. die('Error! Database configuration not set! #1');
  36. }else{
  37. $this->dbConfig = $dbConfig;
  38. }
  39. $this->dbConnect();
  40. $this->ipArr = $this->getIpArr();
  41. $this->ipValue = $this->getIpValue();
  42. $this->ipInfoArr = $this->dbGetRow($this->getIpSelectSQL());
  43. if(!$this->ipInfoArr){
  44. die ('Error during reciving informations about IP address!');
  45. }else{
  46. $this->ipInfoArr['IP_STR'] = $this->ip;
  47. $this->ipInfoArr['IP_VALUE'] = $this->ipValue;
  48. $this->ipInfoArr['IP_FROM_STR'] = $this->getIpFromValue($this->ipInfoArr['IP_FROM']);
  49. $this->ipInfoArr['IP_TO_STR'] = $this->getIpFromValue($this->ipInfoArr['IP_TO']);
  50. }
  51. }
  52. function __destruct(){
  53. if($this->db)
  54. mysql_close($this->db);
  55. }
  56. /**
  57. * IP address
  58. *
  59. * @var string
  60. */
  61. public $ip = '';
  62. /**
  63. * Numerical representation of IP address
  64. * Example: (from Right to Left)
  65. * 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
  66. * is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
  67. * @var integer
  68. */
  69. private $ipValue = NULL;
  70. /**
  71. * database conection configuration
  72. * feel free to replece our db conection and use Your favorite database abstraction layer (ie. ADOdb)
  73. *
  74. * @var array
  75. */
  76. public $dbConfig = array();
  77. /**
  78. * database conection object
  79. * feel free to replece our db conection and use Your favorite database abstraction layer (ie. ADOdb)
  80. *
  81. * @var object
  82. */
  83. public $db = false;
  84. /**
  85. * IP address in form of array of integer values
  86. *
  87. * @var string
  88. */
  89. private $ipArr = array();
  90. /**
  91. * IP address information array
  92. *
  93. * @var string
  94. */
  95. private $ipInfoArr = false;
  96. /**
  97. * returns information about IP adrress
  98. *
  99. * @param integer $mode
  100. * @return mixed
  101. */
  102. public function getInfo($mode=IP_INFO){
  103. if(!in_array($mode,array( IP_STR , IP_VALUE, IP_RANGE_NUMERICAL, IP_RANGE, IP_REGISTRY, IP_ASSIGNED_UNIXTIME , IP_COUNTRY_ISO, IP_COUNTRY_CODE, IP_COUNTRY_NAME, IP_INFO, ))){
  104. die('Error! Bad getInfo() mode!');
  105. }else switch($mode){
  106. case IP_STR:
  107. return $this->ipInfoArr['IP_STR'];
  108. break;
  109. case IP_VALUE:
  110. return $this->ipInfoArr['IP_VALUE'];
  111. break;
  112. case IP_RANGE_NUMERICAL:
  113. return array(
  114. 'FROM' => $this->ipInfoArr['IP_FROM'],
  115. 'TO' => $this->ipInfoArr['IP_TO']
  116. );
  117. break;
  118. case IP_RANGE:
  119. return array(
  120. 'FROM' => $this->ipInfoArr['IP_FROM_STR'],
  121. 'TO' => $this->ipInfoArr['IP_TO_STR']
  122. );
  123. break;
  124. case IP_REGISTRY:
  125. return $this->ipInfoArr['REGISTRY'];
  126. break;
  127. case IP_ASSIGNED_UNIXTIME:
  128. return $this->ipInfoArr['ASSIGNED'];
  129. break;
  130. case IP_COUNTRY_ISO:
  131. return $this->ipInfoArr['CTRY'];
  132. break;
  133. case IP_COUNTRY_CODE:
  134. return $this->ipInfoArr['CNTRY'];
  135. break;
  136. case IP_COUNTRY_NAME:
  137. return $this->ipInfoArr['COUNTRY'];
  138. break;
  139. case IP_INFO:
  140. default:
  141. return $this->ipInfoArr;
  142. break;
  143. }
  144. }
  145. /**
  146. * validate IP address
  147. *
  148. * @param string $ip
  149. * @return boolean
  150. */
  151. private function chceckIpAddr($ip=''){
  152. return preg_match('/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/i',$ip);
  153. }
  154. /**
  155. * returns IP address in array of integer values
  156. *
  157. * @return array
  158. */
  159. private function getIpArr(){
  160. $vars = explode('.',$this->ip);
  161. return array(
  162. intval($vars[0]),
  163. intval($vars[1]),
  164. intval($vars[2]),
  165. intval($vars[3])
  166. );
  167. }
  168. /**
  169. * returns numerical representation of IP address.
  170. * Example: (from Right to Left)
  171. * 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
  172. * is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
  173. *
  174. * @return integer
  175. */
  176. private function getIpValue(){
  177. return $this->ipArr[3] + ( $this->ipArr[2] * 256 ) + ( $this->ipArr[1] * 256 * 256 ) + ( $this->ipArr[0] * 256 * 256 * 256 );
  178. }
  179. /**
  180. * returns IP numer from numerical representation.
  181. * Example: (from Right to Left)
  182. * 1.2.3.4 = 4 + (3 * 256) + (2 * 256 * 256) + (1 * 256 * 256 * 256)
  183. * is 4 + 768 + 13,1072 + 16,777,216 = 16,909,060
  184. *
  185. * @param integer $value
  186. * @param boolean $returnAsStr
  187. * @return mixed
  188. */
  189. private function getIpFromValue($value=0,$returnAsStr=true){
  190. $ip[0] = floor( intval($value) / (256*256*256) );
  191. $ip[1] = floor( ( intval($value) - $ip[0]*256*256*256 ) / (256*256) );
  192. $ip[2] = floor( ( intval($value) -$ip[0]*256*256*256 -$ip[1]*256*256 ) / 256 );
  193. $ip[3] = intval($value) - $ip[0]*256*256*256 - $ip[1]*256*256 - $ip[2]*256;
  194. if($returnAsStr){
  195. return $ip[0].'.'.$ip[1].'.'.$ip[2].'.'.$ip[3];
  196. }else{
  197. return $ip;
  198. }
  199. }
  200. /**
  201. * returns SQL used to get iformation from ip2country database
  202. *
  203. * @return string
  204. */
  205. private function getIpSelectSQL(){
  206. if(empty($this->dbConfig['tableName'])){
  207. $this->dbConfig['tableName'] = 'ip_to_country'; //setting default mysql port name
  208. echo "phpPp2Country table name not selected! traying default value: '".$this->dbConfig['tableName']."'";
  209. }
  210. return 'SELECT * FROM '.$this->dbConfig['tableName'].' WHERE IP_FROM <= '.$this->ipValue.' AND IP_TO >= '.$this->ipValue;
  211. }
  212. /**
  213. * connect to database
  214. * feel free to replece our function and use here Your favorite(s) database abstraction layer (ie. ADOdb)
  215. *
  216. * @return object - database conection resource
  217. */
  218. private function dbConnect(){
  219. if(is_array($this->dbConfig)){
  220. if(empty($this->dbConfig['host'])){
  221. $this->dbConfig['host'] = 'localhost'; //setting default mysql port name
  222. echo "Database connection host not selected! traying default value: '".$this->dbConfig['port']."'";
  223. }
  224. if(intval($this->dbConfig['port']==0)){
  225. $this->dbConfig['port'] = 3306; //setting default mysql port name
  226. echo "Database connection port not selected! traying default value: '".$this->dbConfig['port']."'";
  227. }
  228. if(empty($this->dbConfig['dbUserName'])){
  229. $this->dbConfig['dbUserName'] = 'ip_to_country'; //setting default mysql port name
  230. echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbUserName']."'";
  231. }
  232. if(empty($this->dbConfig['dbUserPassword'])){
  233. $this->dbConfig['dbUserPassword'] = 'xxx'; //setting default mysql port name
  234. echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbUserPassword']."'";
  235. }
  236. $this->db = mysql_connect($this->dbConfig['host'].':'.$this->dbConfig['port'], $this->dbConfig['dbUserName'], $this->dbConfig['dbUserPassword']);
  237. if (!$this->db) {
  238. die('Database connection error: ' . mysql_error());
  239. }else{
  240. if(empty($this->dbConfig['dbName'])){
  241. $this->dbConfig['dbName'] = 'ip_to_country'; //setting default mysql port name
  242. echo "Database connection host not selected! traying default value: '".$this->dbConfig['dbName']."'";
  243. }
  244. if( !mysql_select_db( $this->dbConfig['dbName'] , $this->db ) ){
  245. die("Error during selecting database '".$this->dbConfig['dbName']."' : ". mysql_error());
  246. }else{
  247. return true;
  248. }
  249. }
  250. }else{
  251. die('Error! Database configuration not set! #2');
  252. }
  253. }
  254. /**
  255. * executes given SQL querry and returns one row
  256. * feel free to replece our function and use here Your favorite(s) database abstraction layer (ie. ADOdb)
  257. *
  258. * @param string $sql
  259. * @return array
  260. */
  261. private function dbGetRow($sql){
  262. $result = mysql_query($sql);
  263. if($result){
  264. $row = mysql_fetch_assoc($result);
  265. if($row){
  266. return $row;
  267. }
  268. }
  269. die("Error during database querry:" . mysql_error());
  270. }
  271. }