mysql/cwhois.php
<?php
define("E_MCW_WRONG_CONNECT_PARAMS", 1);
define("E_MCW_CONNECT", 2);
define("E_MCW_SELECT_DATABASE", 3);
define("E_MCW_SELECT_TABLE", 4);
define("E_MCW_TABLE_WRONG_COL_COUNT", 5);
define("E_MCW_WRONG_ADDR_FORMAT", 11);
define("E_MCW_WRONG_ADDR", 21);
define("E_MCW_WRONG_ADDR_LONG", 22);
define("E_MCW_QUERY_ERROR", 31);
define("E_MCW_NO_RESULT", 101);
class CMyCWhois
{
var $dbHost = "";
var $dbLogin = "";
var $dbPasswd = "";
var $dbBase = "";
var $dbTable = "";
var $dbConn = NULL;
var $error = NULL;
var $errReports = NULL;
var $pow8;
var $pow16;
var $pow24;
function _setError($errCode, $method, $line, $file, $additionalInfo = "")
{
$this->error->clean();
$text = "Error #".$errCode." in ".get_class($this)."::".$method.", file ".$file."\n".$additionalInfo."\n";
$text.= print_r($this, true);
$this->error->setError($errCode, $this->errReports[$errCode], $text);
}
function getLastErrorCode()
{
return $this->error->getCode();
}
function getLastError()
{
return $this->error->getString();
}
function getLastErrorText()
{
return $this->error->getDebugText();
}
function CMyCWhois()
{
$this->error = & new CMyCWhoisError();
$errorReports = Array();
include(dirname(__FILE__)."/config.php");
$this->errReports = $errorReports;
$this->pow8 = pow(2,8);
$this->pow16 = $this->pow8 * $this->pow8;
$this->pow24 = $this->pow16 * $this->pow8;
}
function connect($host, $login, $passwd, $base, $table)
{
if (!empty($this->dbConn)) {
$this->disconnect();
}
$this->dbHost = $host;
$this->dbLogin = $login;
$this->dbPasswd = $passwd;
$this->dbBase = $base;
$this->dbTable = $table;
if (empty($this->dbHost) || empty($this->dbLogin) || empty($this->dbBase) || empty($this->dbTable) || !is_string($this->dbHost) || !is_string($this->dbLogin) || !is_string($this->dbPasswd) || !is_string($this->dbBase) || !is_string($this->dbTable)) {
$this->_setError(E_MCW_WRONG_CONNECT_PARAMS, __FUNCTION__, __LINE__, __FILE__);
return false;
}
if (!($this->dbConn = mysql_connect($this->dbHost, $this->dbLogin, $this->dbPasswd))) {
$this->_setError(E_MCW_CONNECT, __FUNCTION__, __LINE__, __FILE__, mysql_error());
return false;
}
if (!mysql_select_db($this->dbBase, $this->dbConn)) {
$this->_setError(E_MCW_SELECT_DATABASE, __FUNCTION__, __LINE__, __FILE__, mysql_error());
return false;
}
$query = 'SHOW COLUMNS FROM '.$this->dbTable;
$result = mysql_query($query, $this->dbConn);
if (!$result) {
$this->_setError(E_MCW_SELECT_TABLE, __FUNCTION__, __LINE__, __FILE__, mysql_error()."\n".$query);
return false;
}
if (6 != mysql_num_rows($result)) {
$this->_setError(E_MCW_TABLE_WRONG_COL_COUNT, __FUNCTION__, __LINE__, __FILE__);
return false;
}
return true;
}
function disconnect()
{
mysql_close($this->dbConn);
}
function getCountryByAddr($addr)
{
if (is_array($addr)) {
$resArr = array();
foreach ($addr as $val) {
$resArr[] = $this->getCountryByAddr($val);
}
return $resArr;
}
if (!is_string($addr)) {
$this->_setError(E_MCW_WRONG_ADDR_FORMAT, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$addr = trim($addr);
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $addr)) {
$host = getHostByName($addr);
if ($host == $addr) {
$this->_setError(E_MCW_WRONG_ADDR, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$addr = $host;
}
$addr = sprintf("%u", ip2long($addr));
if ($addr < 0) {
$this->_setError(E_MCW_WRONG_ADDR_LONG, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$query = "SELECT code, name FROM ".$this->dbTable." WHERE start_num<=".$addr." AND end_num>=".$addr;
$result = mysql_query($query, $this->dbConn);
if (!$result) {
$this->_setError(E_MCW_QUERY_ERROR, __FUNCTION__, __LINE__, __FILE__, mysql_error()."\n".$query);
return false;
}
if (!mysql_num_rows($result)) {
$this->_setError(E_MCW_NO_RESULT, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$row = mysql_fetch_row($result);
return array($row[0], $row[1]);
}
function getNameByAddr($addr)
{
if (is_array($addr)) {
$resArr = array();
foreach ($addr as $val) {
$resArr[] = $this->getCountryByAddr($val);
}
return $resArr;
}
if (!is_string($addr)) {
$this->_setError(E_MCW_WRONG_ADDR_FORMAT, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$addr = trim($addr);
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $addr)) {
$host = getHostByName($addr);
if ($host == $addr) {
$this->_setError(E_MCW_WRONG_ADDR, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$addr = $host;
}
$addr = sprintf("%u", ip2long($addr));
if ($addr < 0) {
$this->_setError(E_MCW_WRONG_ADDR_LONG, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$query = "SELECT name FROM ".$this->dbTable." WHERE start_num<=".$addr." AND end_num>=".$addr;
$result = mysql_query($query, $this->dbConn);
if (!$result) {
$this->_setError(E_MCW_QUERY_ERROR, __FUNCTION__, __LINE__, __FILE__, mysql_error()."\n".$query);
return false;
}
if (!mysql_num_rows($result)) {
$this->_setError(E_MCW_NO_RESULT, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$row = mysql_fetch_row($result);
return $row[0];
}
function getCodeByAddr($addr)
{
if (is_array($addr)) {
$resArr = array();
foreach ($addr as $val) {
$resArr[] = $this->getCountryByAddr($val);
}
return $resArr;
}
if (!is_string($addr)) {
$this->_setError(E_MCW_WRONG_ADDR_FORMAT, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$addr = trim($addr);
if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $addr)) {
$host = getHostByName($addr);
if ($host == $addr) {
$this->_setError(E_MCW_WRONG_ADDR, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$addr = $host;
}
$addr = sprintf("%u", ip2long($addr));
if ($addr < 0) {
$this->_setError(E_MCW_WRONG_ADDR_LONG, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$query = "SELECT code FROM ".$this->dbTable." WHERE start_num<=".$addr." AND end_num>=".$addr;
$result = mysql_query($query, $this->dbConn);
if (!$result) {
$this->_setError(E_MCW_QUERY_ERROR, __FUNCTION__, __LINE__, __FILE__, mysql_error()."\n".$query);
return false;
}
if (!mysql_num_rows($result)) {
$this->_setError(E_MCW_NO_RESULT, __FUNCTION__, __LINE__, __FILE__);
return false;
}
$row = mysql_fetch_row($result);
return $row[0];
}
}
class CMyCWhoisError
{
var $errCode = 0;
var $errStr = "";
var $errDebugText = "";
function setError($code, $str, $text)
{
$this->errCode = $code;
$this->errStr = $str;
$this->errDebugText = $text;
}
function clean()
{
$this->errCode = 0;
$this->errStr = "";
$this->errDebugText = "";
}
function getCode()
{
return $this->errCode;
}
function getString()
{
return $this->errStr;
}
function getDebugText()
{
return $this->errDebugText;
}
}
?>
mysql/config.php
<?php
$errorReports[E_MCW_WRONG_CONNECT_PARAMS] = "Wrong connection parameters";
$errorReports[E_MCW_CONNECT] = "Couldn't connect to database";
$errorReports[E_MCW_SELECT_DATABASE] = "Couldn't select database";
$errorReports[E_MCW_SELECT_TABLE] = "Couldn't select table";
$errorReports[E_MCW_TABLE_WRONG_COL_COUNT] = "Wrong database table column count";
$errorReports[E_MCW_WRONG_ADDR_FORMAT] = "Wrong address format";
$errorReports[E_MCW_WRONG_ADDR] = "Wrong address";
$errorReports[E_MCW_WRONG_ADDR_LONG] = "Can't convert ip to long";
$errorReports[E_MCW_QUERY_ERROR] = "Database error: wrong query";
$errorReports[E_MCW_NO_RESULT] = "No result for this ip";
?>