fixing bug 4851

This commit is contained in:
Fernando Ontiveros
2011-02-24 21:28:42 +00:00
parent 6940301b89
commit 8159d61c1e

View File

@@ -1,16 +1,4 @@
<?php <?php
/*--------------------------------------------------
| net.class.php
| By Erik Amaru Ortiz
| CopyLeft (f) 2008
| Email: erik@colosa.com
+--------------------------------------------------
| Email bugs/suggestions to erik@colosa.com erik.260mb.com
+--------------------------------------------------
| This script has been created and released under
| the GNU GPL and is free to use and redistribute
| only if this copyright statement is not removed
+--------------------------------------------------*/
/** /**
* LastModification 30/05/2008 * LastModification 30/05/2008
*/ */
@@ -29,7 +17,7 @@ class NET
private $db_sourcename; private $db_sourcename;
private $db_port; private $db_port;
/*errors handle*/ /*errors handle*/
public $error; public $error;
public $errno; public $errno;
public $errstr; public $errstr;
@@ -41,20 +29,20 @@ class NET
*/ */
function __construct($pHost) function __construct($pHost)
{ {
$this->errno = 0; $this->errno = 0;
$this->errstr = ""; $this->errstr = "";
unset($this->db_user); unset($this->db_user);
unset($this->db_passwd); unset($this->db_passwd);
unset($this->db_sourcename); unset($this->db_sourcename);
#verifing valid param #verifing valid param
if ($pHost == "") { if ($pHost == "") {
$this->errno = 1000; $this->errno = 1000;
$this->errstr = "NET::You must specify a host"; $this->errstr = "NET::You must specify a host";
//$this->showMsg(); //$this->showMsg();
} }
$this->resolv($pHost); $this->resolv($pHost);
} }
/** /**
@@ -65,20 +53,22 @@ class NET
function resolv($pHost) function resolv($pHost)
{ {
if ($this->is_ipaddress($pHost)) { if ($this->is_ipaddress($pHost)) {
$this->ip = $pHost; $this->ip = $pHost;
if (!$this->hostname = @gethostbyaddr($pHost)) { if (!$this->hostname = @gethostbyaddr($pHost)) {
$this->errno = 2000; $this->errno = 2000;
$this->errstr = "NET::Host down"; $this->errstr = "NET::Host down";
$this->error = "Destination Host Unreachable"; $this->error = "Destination Host Unreachable";
} }
} else { }
else {
$ip = @gethostbyname($pHost); $ip = @gethostbyname($pHost);
$long = ip2long($ip); $long = ip2long($ip);
if ( $long == -1 || $long === FALSE) { if ( $long == -1 || $long === FALSE) {
$this->errno = 2000; $this->errno = 2000;
$this->errstr = "NET::Host down"; $this->errstr = "NET::Host down";
$this->error = "Destination Host Unreachable"; $this->error = "Destination Host Unreachable";
} else { }
else {
$this->ip = @gethostbyname($pHost); $this->ip = @gethostbyname($pHost);
$this->hostname = $pHost; $this->hostname = $pHost;
} }
@@ -96,15 +86,15 @@ class NET
$hostip = @gethostbyname($host); // resloves IP from Hostname returns hostname on failure $hostip = @gethostbyname($host); // resloves IP from Hostname returns hostname on failure
// attempt to connect // attempt to connect
if (@fsockopen($this->ip, $pPort, $this->errno, $this->errstr, TIMEOUT)) { if (@fsockopen($this->ip, $pPort, $this->errno, $this->errstr, TIMEOUT)) {
return true; return true;
@fclose($x); //close connection (i dont know if this is needed or not). @fclose($x); //close connection (i dont know if this is needed or not).
} else { }
$this->errno = 9999; else {
$this->errstr = "NET::Port Host Unreachable"; $this->errno = 9999;
$this->error = "Destination Port Unreachable"; $this->errstr = "NET::Port Host Unreachable";
return false; $this->error = "Destination Port Unreachable";
return false;
} }
} }
/** /**
@@ -119,15 +109,16 @@ class NET
$tmp = explode(".", $pHost); $tmp = explode(".", $pHost);
#if have a ip address format #if have a ip address format
if (count($tmp) == 4) { if (count($tmp) == 4) {
#if a correct ip address #if a correct ip address
for ($i = 0; $i < count($tmp); $i++) { for ($i = 0; $i < count($tmp); $i++) {
if (!is_int($tmp[$i])) { if (!is_int($tmp[$i])) {
$key = false; $key = false;
break; break;
}
} }
} else { }
$key = false; }
else {
$key = false;
} }
return $key; return $key;
} }
@@ -139,12 +130,12 @@ class NET
*/ */
function ping($pTTL = 3000) function ping($pTTL = 3000)
{ {
$cmd = "ping -w $pTTL $this->ip"; $cmd = "ping -w $pTTL $this->ip";
$output = exec($cmd, $a, $a1); $output = exec($cmd, $a, $a1);
$this->errstr = ""; $this->errstr = "";
for ($i = 0; $i < count($a); $i++) for ($i = 0; $i < count($a); $i++)
$this->errstr += $a[$i]; $this->errstr += $a[$i];
$this->errno = $a1; $this->errno = $a1;
} }
/** /**
@@ -155,8 +146,8 @@ class NET
*/ */
function loginDbServer($pUser, $pPasswd) function loginDbServer($pUser, $pPasswd)
{ {
$this->db_user = $pUser; $this->db_user = $pUser;
$this->db_passwd = $pPasswd; $this->db_passwd = $pPasswd;
} }
/** /**
@@ -167,8 +158,8 @@ class NET
*/ */
function setDataBase($pDb, $pPort='') function setDataBase($pDb, $pPort='')
{ {
$this->db_sourcename = $pDb; $this->db_sourcename = $pDb;
$this->db_port = $pPort; $this->db_port = $pPort;
} }
/** /**
@@ -178,96 +169,102 @@ class NET
*/ */
function tryConnectServer($pDbDriver) function tryConnectServer($pDbDriver)
{ {
if($this->errno != 0) { if($this->errno != 0) {
return 0; return 0;
} }
$stat = new Stat(); $stat = new Stat();
if(isset($this->db_user) && (isset($this->db_passwd) || ('' == $this->db_passwd)) && isset($this->db_sourcename)) if(isset($this->db_user) && (isset($this->db_passwd) || ('' == $this->db_passwd)) && isset($this->db_sourcename))
{
switch($pDbDriver)
{ {
switch($pDbDriver) case 'mysql':
{ if ($this->db_passwd == '') {
case 'mysql': $link = @mysql_connect($this->ip.(($this->db_port != '') && ($this->db_port != 0) ? ':'.$this->db_port : ''), $this->db_user);
if ($this->db_passwd == '') { }
$link = @mysql_connect($this->ip.(($this->db_port != '') && ($this->db_port != 0) ? ':'.$this->db_port : ''), $this->db_user); else {
} else { $link = @mysql_connect($this->ip.(($this->db_port != '') && ($this->db_port != 0) ? ':'.$this->db_port : ''), $this->db_user, $this->db_passwd);
$link = @mysql_connect($this->ip.(($this->db_port != '') && ($this->db_port != 0) ? ':'.$this->db_port : ''), $this->db_user, $this->db_passwd); }
} if ($link) {
if ($link) { if (@mysql_ping($link)) {
if (@mysql_ping($link)) { $stat->status = 'SUCCESS';
$stat->status = 'SUCCESS'; $this->errstr = "";
$this->errstr = ""; $this->errno = 0;
$this->errno = 0; }
} else { else {
$this->error = "Lost MySql Connection"; $this->error = "Lost MySql Connection";
$this->errstr = "NET::MYSQL->Lost Connection"; $this->errstr = "NET::MYSQL->Lost Connection";
$this->errno = 10010; $this->errno = 10010;
} }
} else { }
$this->error = "MySql connection refused!"; else {
$this->errstr = "NET::MYSQL->The connection was refused"; $this->error = "MySql connection refused!";
$this->errno = 10001; $this->errstr = "NET::MYSQL->The connection was refused";
} $this->errno = 10001;
break; }
break;
case 'pgsql':
$this->db_port = ($this->db_port == "") ? "5432" : $this->db_port; case 'pgsql':
$link = @pg_connect("host='$this->ip' port='$this->db_port' user='$this->db_user' password='$this->db_passwd' dbname='$this->db_sourcename'"); $this->db_port = ($this->db_port == "") ? "5432" : $this->db_port;
if ($link) { $link = @pg_connect("host='$this->ip' port='$this->db_port' user='$this->db_user' password='$this->db_passwd' dbname='$this->db_sourcename'");
$stat->status = 'SUCCESS'; if ($link) {
$this->errstr = ""; $stat->status = 'SUCCESS';
$this->errno = 0; $this->errstr = "";
} else { $this->errno = 0;
} else {
$this->error = "PostgreSql connection refused!"; $this->error = "PostgreSql connection refused!";
$this->errstr = "NET::POSTGRES->The connection was refused"; $this->errstr = "NET::POSTGRES->The connection was refused";
$this->errno = 20001; $this->errno = 20001;
}
break;
case 'mssql':
$str_port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":".$this->db_port;
$link = @mssql_connect($this->ip . $str_port, $this->db_user, $this->db_passwd);
if ($link) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
} else {
$this->error = "MS-SQL Server connection refused";
$this->errstr = "NET::MSSQL->The connection was refused";
$this->errno = 30001;
}
break;
case 'oracle':
$this->db_port = (($this->db_port == "") || ($this->db_port == 0)) ? "1521" : $this->db_port;
try{
$link = $conn = @oci_connect($this->db_user,$this->db_passwd, "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (HOST=$this->ip) (PORT=$this->db_port) )) (CONNECT_DATA=(SERVICE_NAME=$this->db_sourcename)))");
if ($link) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
} else {
$this->error = "Oracle connection refused";
$this->errstr = "NET::ORACLE->The connection was refused";
$this->errno = 30001;
}
} catch (Exception $e){
throw new Exception("[erik] Couldn't connect to Oracle Server! - ".$e->getMessage());
}
break;
case 'informix':
break;
case 'sqlite':
break;
} }
} else { break;
throw new Exception("CLASS::NET::ERROR: No connections param.");
case 'mssql':
$str_port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":".$this->db_port;
$link = @mssql_connect($this->ip . $str_port, $this->db_user, $this->db_passwd);
if ($link) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
}
else {
$this->error = "MS-SQL Server connection refused";
$this->errstr = "NET::MSSQL->The connection was refused";
$this->errno = 30001;
}
break;
case 'oracle':
$this->db_port = (($this->db_port == "") || ($this->db_port == 0)) ? "1521" : $this->db_port;
try{
$link = $conn = @oci_connect($this->db_user,$this->db_passwd, "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (HOST=$this->ip) (PORT=$this->db_port) )) (CONNECT_DATA=(SERVICE_NAME=$this->db_sourcename)))");
if ($link) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
}
else {
$this->error = "Oracle connection refused";
$this->errstr = "NET::ORACLE->The connection was refused";
$this->errno = 30001;
}
}
catch (Exception $e){
throw new Exception("[erik] Couldn't connect to Oracle Server! - ".$e->getMessage());
}
break;
case 'informix':
break;
case 'sqlite':
break;
} }
}
return $stat; else {
throw new Exception("CLASS::NET::ERROR: No connections param.");
}
return $stat;
} }
/** /**
@@ -277,118 +274,129 @@ class NET
*/ */
function tryOpenDataBase($pDbDriver) function tryOpenDataBase($pDbDriver)
{ {
if($this->errno != 0) { if($this->errno != 0) {
return 0; return 0;
} }
set_time_limit(0); set_time_limit(0);
$stat = new Stat(); $stat = new Stat();
if(isset($this->db_user) && (isset($this->db_passwd) || ('' == $this->db_passwd)) && isset($this->db_sourcename)) if(isset($this->db_user) && (isset($this->db_passwd) || ('' == $this->db_passwd)) && isset($this->db_sourcename))
{
switch($pDbDriver)
{ {
switch($pDbDriver) case 'mysql':
{ $link = @mysql_connect($this->ip.(($this->db_port != '') && ($this->db_port != 0) ? ':'.$this->db_port : ''), $this->db_user, $this->db_passwd);
case 'mysql': $db = @mysql_select_db($this->db_sourcename);
$link = @mysql_connect($this->ip.(($this->db_port != '') && ($this->db_port != 0) ? ':'.$this->db_port : ''), $this->db_user, $this->db_passwd); if ($link) {
$db = @mysql_select_db($this->db_sourcename); if ($db) {
if ($link) { $result = @mysql_query("show tables;");
if ($db) { if ($result) {
$result = @mysql_query("show tables;"); $stat->status = 'SUCCESS';
if ($result) { $this->errstr = "";
$stat->status = 'SUCCESS'; $this->errno = 0;
$this->errstr = ""; @mysql_free_result($result);
$this->errno = 0; }
@mysql_free_result($result); else {
} else { $this->error = "the user $this->db_user doesn't have privileges to run queries!";
$this->error = "the user $this->db_user doesn't have privileges to run queries!"; $this->errstr = "NET::MYSQL->Test query failed";
$this->errstr = "NET::MYSQL->Test query failed"; $this->errno = 10100;
$this->errno = 10100; }
} }
} else { else {
$this->error = "The $this->db_sourcename data base does'n exist!"; $this->error = "The $this->db_sourcename data base does'n exist!";
$this->errstr = "NET::MYSQL->Select data base failed"; $this->errstr = "NET::MYSQL->Select data base failed";
$this->errno = 10011; $this->errno = 10011;
} }
} else { }
$this->error = "MySql connection refused!"; else {
$this->errstr = "NET::MYSQL->The connection was refused"; $this->error = "MySql connection refused!";
$this->errno = 10001; $this->errstr = "NET::MYSQL->The connection was refused";
} $this->errno = 10001;
break;
case 'pgsql':
$this->db_port = (($this->db_port == "") || ($this->db_port == 0)) ? "5432" : $this->db_port;
$link = @pg_connect("host='$this->ip' port='$this->db_port' user='$this->db_user' password='$this->db_passwd' dbname='$this->db_sourcename'");
if ($link) {
if (@pg_ping($link)) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
} else {
$this->error = "PostgreSql Connection to $this->ip is unreachable!";
$this->errstr = "NET::POSTGRES->Lost Connection";
$this->errno = 20010;
}
} else {
$this->error = "PostgrSql connection refused";
$this->errstr = "NET::POSTGRES->The connection was refused";
$this->errno = 20001;
}
break;
case 'mssql':
$str_port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":".$this->db_port;
$link = @mssql_connect($this->ip . $str_port, $this->db_user, $this->db_passwd);
if ($link) {
$db = @mssql_select_db($this->db_sourcename, $link);
if ($db) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
} else {
$this->error = "The $this->db_sourcename data base does'n exist!";
$this->errstr = "NET::MSSQL->Select data base failed";
$this->errno = 30010;
}
} else {
$this->error = "MS-SQL Server connection refused!";
$this->errstr = "NET::MSSQL->The connection was refused";
$this->errno = 30001;
}
break;
case 'oracle':
$this->db_port = (($this->db_port == "") || ($this->db_port == 0)) ? "1521" : $this->db_port;
$link = @oci_connect($this->db_user,$this->db_passwd, "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (HOST=$this->ip) (PORT=$this->db_port) )) (CONNECT_DATA=(SERVICE_NAME=$this->db_sourcename)))");
if ($link) {
$stid = @oci_parse($link, 'select AUTHENTICATION_TYPE from v$session_connect_info');
$result = @oci_execute($stid, OCI_DEFAULT);
if($result){
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
@oci_close($link);
} else {
$this->error = "the user $this->db_user don't has privileges to run queries!";
$this->errstr = "NET::ORACLE->Couldn't execute any query on this server!";
$this->errno = 40010;
}
} else {
$this->error = "Oracle connection refused!";
$this->errstr = "NET::ORACLE->The connection was refused";
$this->errno = 40001;
}
break;
case 'informix':
break;
case 'sqlite':
break;
} }
} else { break;
throw new Exception("CLASS::NET::ERROR: No connections param.");
case 'pgsql':
$this->db_port = (($this->db_port == "") || ($this->db_port == 0)) ? "5432" : $this->db_port;
$link = @pg_connect("host='$this->ip' port='$this->db_port' user='$this->db_user' password='$this->db_passwd' dbname='$this->db_sourcename'");
if ($link) {
if (@pg_ping($link)) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
}
else {
$this->error = "PostgreSql Connection to $this->ip is unreachable!";
$this->errstr = "NET::POSTGRES->Lost Connection";
$this->errno = 20010;
}
}
else {
$this->error = "PostgrSql connection refused";
$this->errstr = "NET::POSTGRES->The connection was refused";
$this->errno = 20001;
}
break;
case 'mssql':
$str_port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":".$this->db_port;
$link = @mssql_connect($this->ip . $str_port, $this->db_user, $this->db_passwd);
if ($link) {
$db = @mssql_select_db($this->db_sourcename, $link);
if ($db) {
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
}
else {
$this->error = "The $this->db_sourcename data base does'n exist!";
$this->errstr = "NET::MSSQL->Select data base failed";
$this->errno = 30010;
}
}
else {
$this->error = "MS-SQL Server connection refused!";
$this->errstr = "NET::MSSQL->The connection was refused";
$this->errno = 30001;
}
break;
case 'oracle':
$this->db_port = (($this->db_port == "") || ($this->db_port == 0)) ? "1521" : $this->db_port;
$link = @oci_connect($this->db_user,$this->db_passwd, "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (HOST=$this->ip) (PORT=$this->db_port) )) (CONNECT_DATA=(SERVICE_NAME=$this->db_sourcename)))");
if ($link) {
$stid = @oci_parse($link, 'select AUTHENTICATION_TYPE from v$session_connect_info');
$result = @oci_execute($stid, OCI_DEFAULT);
if($result){
$stat->status = 'SUCCESS';
$this->errstr = "";
$this->errno = 0;
@oci_close($link);
}
else {
$this->error = "the user $this->db_user don't has privileges to run queries!";
$this->errstr = "NET::ORACLE->Couldn't execute any query on this server!";
$this->errno = 40010;
}
}
else {
$this->error = "Oracle connection refused!";
$this->errstr = "NET::ORACLE->The connection was refused";
$this->errno = 40001;
}
break;
case 'informix':
break;
case 'sqlite':
break;
} }
return $stat; }
else {
throw new Exception("CLASS::NET::ERROR: No connections param.");
}
return $stat;
} }
/** /**
@@ -402,61 +410,24 @@ class NET
$this->ip = getenv('HTTP_CLIENT_IP'); $this->ip = getenv('HTTP_CLIENT_IP');
if(isset($this->ip) && isset($this->db_user) && isset($this->db_passwd)) { if(isset($this->ip) && isset($this->db_user) && isset($this->db_passwd)) {
try{ try{
/* if(!isset($this->db_sourcename))
switch($driver) $this->db_sourcename = DB_NAME;
{ $value = 'none';
case 'mysql': $sDataBase = 'database_' . strtolower(DB_ADAPTER);
if($link = @mysql_connect($this->ip, $this->db_user, $this->db_passwd)){ if(G::LoadSystemExist($sDataBase)){
$v = @mysql_get_server_info(); G::LoadSystem($sDataBase);
} else { $oDataBase = new database();
throw new Exception(@mysql_error($link)); $value = $oDataBase->getServerVersion($driver, $this->ip, $this->db_port, $this->db_user, $this->db_passwd, $this->db_sourcename);
}
break;
case 'pgsql':
$this->db_port = (($this->db_port == "") || ($this->db_port == 0)) ? "5432" : $this->db_port;
$link = @pg_connect("host='$this->ip' port='$this->db_port' user='$this->db_user' password='$this->db_passwd' dbname='$this->db_sourcename'");
if($link){
$v = @pg_version($link);
} else {
throw new Exception(@pg_last_error($link));
}
break;
case 'mssql':
if(strlen(trim($this->ip))<=0)
$this->ip = DB_HOST;
if($link = @mssql_connect($this->ip, $this->db_user, $this->db_passwd)){
@mssql_select_db( DB_NAME, $link );
$oResult = @mssql_query("select substring(@@version, 21, 6) + ' (' + CAST(SERVERPROPERTY ('productlevel') as varchar(10)) + ') ' + CAST(SERVERPROPERTY('productversion') AS VARCHAR(15)) + ' ' + CAST(SERVERPROPERTY ('edition') AS VARCHAR(25)) as version; ", $link);
$aResult = @mssql_fetch_array($oResult);
@mssql_free_result($oResult);
$v = $aResult[0];
} else {
throw new Exception(@mssql_error($link));
}
break;
}
return (isset($v))?$v:'none';
*/
if(!isset($this->db_sourcename))
$this->db_sourcename = DB_NAME;
$value = 'none';
$sDataBase = 'database_' . strtolower(DB_ADAPTER);
if(G::LoadSystemExist($sDataBase)){
G::LoadSystem($sDataBase);
$oDataBase = new database();
$value = $oDataBase->getServerVersion($driver, $this->ip, $this->db_port, $this->db_user, $this->db_passwd, $this->db_sourcename);
}
return $value;
} catch (Exception $e){
throw new Exception($e->getMessage());
} }
return $value;
}
catch (Exception $e){
throw new Exception($e->getMessage());
}
} }
else{ else{
throw new Exception('NET::Error->No params for Data Base Server!'); throw new Exception('NET::Error->No params for Data Base Server!');
} }
} }
@@ -467,15 +438,15 @@ class NET
*/ */
function dbName($pAdapter) function dbName($pAdapter)
{ {
switch($pAdapter) switch($pAdapter)
{ {
case 'mysql': return 'MySql'; break; case 'mysql': return 'MySql'; break;
case 'pgsql': return 'PostgreSQL'; break; case 'pgsql': return 'PostgreSQL'; break;
case 'mssql': return 'Microsoft SQL Server'; break; case 'mssql': return 'Microsoft SQL Server'; break;
case 'oracle': return 'Oracle'; break; case 'oracle': return 'Oracle'; break;
case 'informix': return 'Informix'; break; case 'informix': return 'Informix'; break;
case 'sqlite': return 'SQLite'; break; case 'sqlite': return 'SQLite'; break;
} }
} }
/** /**
@@ -508,7 +479,7 @@ class NET
*/ */
function getErrno() function getErrno()
{ {
return $this->errno; return $this->errno;
} }
/** /**
@@ -518,7 +489,7 @@ class NET
*/ */
function getErrmsg() function getErrmsg()
{ {
return $this->errstr; return $this->errstr;
} }
} }
@@ -528,12 +499,10 @@ class NET
*/ */
class Stat class Stat
{ {
public $stutus; public $stutus;
function __construct() function __construct()
{ {
$this->status = false; $this->status = false;
} }
} }
?>