Silent=SQL_Silent; if (defined('SQL_Debug')) $this->DebugOut=SQL_Debug; if (defined('SQL_DebugOut')) $this->DebugOut=SQL_DebugOut; if (defined('SQL_DebugMail')) $this->DebugMail=SQL_DebugMail; if (defined('SQL_Immortal')) $this->Immortal=SQL_Immortal; /* пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ read-write пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ */ if (!empty($dsn_rw)) { $this->Connect_rw=$this->setDSN($this->Connect_rw,$dsn_rw); $SetConnect=TRUE; if (! is_resource($SQL_Connect_rw) ) $SQL_Connect_rw=$this->Connect_rw; } else { if (is_resource($SQL_Connect_rw)) { $this->Connect_rw=$SQL_Connect_rw; } else { if (! is_resource($this->Connect_rw)) { if (!defined('SQL_Connect_rw')) $this->getDSN(); $this->setDSN($this->Connect_rw,SQL_Connect_rw); $this->setHandshakeQuery($this->Connect_rw); $SetConnect=TRUE; } $SQL_Connect_rw=$this->Connect_rw; } } /* пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ read-only пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ */ if (!empty($dsn_ro)) { $this->Connect_ro=$this->setDSN($this->Connect_ro,$dsn_ro); $SetConnect=TRUE; if (! is_resource($SQL_Connect_ro) ) $SQL_Connect_ro=$this->Connect_ro; } else { if (is_resource($SQL_Connect_ro)) { $this->Connect_ro=$SQL_Connect_ro; } else { if ( is_resource($SQL_Connect_rw) and !defined('SQL_Connect_ro') ) return $SetConnect; if (! is_resource($this->Connect_ro)) { if (!defined('SQL_Connect_ro')) $this->getDSN(); $this->setDSN($this->Connect_ro,SQL_Connect_ro); $this->setHandshakeQuery($this->Connect_ro); $SetConnect=TRUE; } $SQL_Connect_ro=$this->Connect_ro; } } return $SetConnect; } function setHandshakeQuery($connect) { if (defined('HandshakeQuery')) { if ($this->query(HandshakeQuery,__file__,__line__,$connect)===FALSE) $this->error_report(SQL_TypeError_SelectDB); } $i=1; while (defined('HandshakeQuery'.$i)) { if ($this->query(constant('HandshakeQuery'.$i++),__file__,__line__,$connect)===FALSE) $this->error_report(SQL_TypeError_SelectDB); } } function setDSN(&$conn,$dsn) { $arrDSN=$this->parseDSN($dsn); if (count($arrDSN)) { $conn=@mysql_connect($arrDSN['host'].':'.$arrDSN['port'],$arrDSN['user'],$arrDSN['pass']); if ( ! is_resource($conn) ) $this->error_report(SQL_TypeError_Connect, sprintf("\nUnable connect to server.\nError message: %s\nDSN: %s\nParseDSN={\n%s}", $php_errormsg,$dsn, var_export($arrDSN,TRUE))); if ( ! empty($arrDSN['db']) ) { if ( ! @mysql_select_db($arrDSN['db'],$conn) ) $this->error_report(SQL_TypeError_SelectDB, sprintf("\nUnable select DB.\nError message: %s\nDSN: %s\nParseDSN={\n%s}\n", $php_errormsg,$dsn, var_export($arrDSN,TRUE))); else $this->DBName=$arrDSN['db']; } } return $conn; } function error_report($error,$error_txt='') { $this->ErrorFlag=TRUE; $error_con="". "
Our server is busy, please try again later.
Thank you.
"; $error_query="". "
Internal error!
Try again later, please. This function temporarily does not work.
Thank you.
"; switch ($error) { case SQL_TypeError_Connect: if (!empty($this->ErrorConnect)) { $rep=$this->ErrorConnect; } elseif (defined('SQL_MsgError_Connect')) { $rep=SQL_MsgError_Connect; } else { $rep=$error_con; } break; case SQL_TypeError_SelectDB: if (!empty($this->ErrorDB)) { $rep=$this->ErrorDB; } elseif (defined('SQL_MsgError_DB')) { $rep=SQL_MsgError_DB; } else { $rep=$error_con; } break; case SQL_TypeError_Query: if (!empty($this->ErrorQuery)) { $rep=$this->ErrorQuery; } elseif (defined('SQL_MsgError_Query')) { $rep=SQL_MsgError_Query; } else { $rep=$error_query; } break; default: echo $error_query; break; } $err_msg=sprintf("SQL-Error.\n" . "This report about SQL-error of type '%s'. " . "This error has has been caused by site '%s' in script '%s'.\n" . "%sRequest URI: '%s'\nRemote IP: '%s'\n%s", $error, $_SERVER['SERVER_NAME'], $_SERVER['SCRIPT_NAME'], ( isset($_SERVER['HTTP_REFERER']) ? "Referer URL: '${_SERVER['HTTP_REFERER']}'\n" : ''), $_SERVER['REQUEST_URI'], $_SERVER['REMOTE_ADDR'], $error_txt ); if (! $this->Silent) echo $rep; if ($this->DebugOut) echo nl2br($err_msg); if ($this->DebugMail) { $Err=new ProcError; $Err->ErrorMailSubj='Error report'; $Err->Error(440,4,'sql.class',$err_msg); } if (! $this->Immortal) exit; } function getDSN() { if (! $this->readDSN()) { if ($this->readDSN_old()) $this->error(SQL_TypeError_Config); } } function readDSN() { if ( defined('SQL_CONNECT_FILE') ) $cf=SQL_CONNECT_FILE; else $cf='.connect.php'; include $cf; return defined('SQL_Connect_rw'); } function readDSN_old() { $cf='var.hp'; foreach(preg_split(PATH_SEPARATOR, ini_get('include_path')) as $path) { if (file_exists($path.'/'.$cf)) { include $cf; break; } } if (!empty($PASS_Connect)) $PASS_Connect=':'.$PASS_Connect; if (!empty($PORT_Connect)) $PORT_Connect=':'.$PORT_Connect; if (!empty($HOST_Connect)) $HOST_Connect='@'.$HOST_Connect; $dsn=sprintf('mysql://%s%s%s%s//%s',$USER_Connect,$PASS_Connect,$HOST_Connect,$PORT_Connect,$DB_Connect); if (!empty($USER_Connect)) define('SQL_Connect_rw',$dsn); else return FALSE; } function parseDSN($dsn) { $parsed=array(); if (preg_match('|mysql://([^:@/]+)(:[^@/]+)?(.+)?|',$dsn,$arr)) { $parsed['user']=$arr[1]; $parsed['pass']=substr($arr[2],1); $rest=$arr[3]; } if (preg_match('|(@[\w.-]+)?(:.+[^/])?//([^?]+)?|',$rest,$arr)) { $parsed['host']=substr($arr[1],1); if (!empty($arr[2])) $parsed['port']=substr($arr[2],1); if (!empty($arr[3])) $parsed['db']=$arr[3]; } if (count($parsed)) { if (empty($parsed['host'])) $parsed['host']='localhost'; if (empty($parsed['port'])) $parsed['port']='/tmp/mysql.sock'; return $parsed; } else { return FALSE; } } function check_ro_connect($connect=0) { if ( is_resource($connect) ) $conn=$connect; else $conn = $this->Connect_ro; $res = @mysql_query('show slave status',$conn); if ( $res === FALSE ) return FALSE; $row = @mysql_fetch_array($res, MYSQL_ASSOC); if ( $row === FALSE ) return FALSE; @mysql_free_result($res); if ( isset($row['Seconds_Behind_Master']) and $row['Seconds_Behind_Master'] == 0 ) return TRUE; else return FALSE; } function query($query,$file,$line,$connect=0) { if (preg_match('/^\s*lock/i',$query)) $this->setLockTable(); if (is_resource($connect)) { $conn=$connect; } elseif (is_resource($this->Connect_ro) and ! $this->getLockTable()) { if ( preg_match('/^\s*(select|show|explain|describe)/i',$query) and $this->check_ro_connect() ) { $conn=$this->Connect_ro; } else { $conn=$this->Connect_rw; } } else { $conn=$this->Connect_rw; } $this->res=mysql_query($query,$conn); if (preg_match('/^\s*unlock/i',$query)) $this->clsLockTable(); if ($this->res===FALSE) { $this->error_report(SQL_TypeError_Query,sprintf( "\nConnect={\n\tHost: %s\n\tServer: %s\n\tClient: %s\n}\nSQL={\n\t%s\n}\nSQL-Error={\n\t%s\n}\nSee %s in string %s to more.\n", mysql_get_host_info($conn),mysql_get_server_info($conn), mysql_get_client_info(),$query,mysql_error($conn),$file,$line)); $this->rows=0; return FALSE; } if (gettype($this->res)=='resource') { $this->rows=mysql_num_rows($this->res); } else { $this->rows=mysql_affected_rows($conn); $this->last_id=mysql_insert_id($conn); } return $this->rows; } function drop() { if (is_resource($this->res)) { mysql_free_result($this->res); unset($this->res); } unset($this->rows); unset($this->row); } function __destruct() { $this->drop(); } // function __construct($dsn_rw='', $dsn_ro='') { // return $this->CoreSQL($dsn_rw, $dsn_ro); // } /* LockTable operations (PHP4) */ function controlLockTable($data) { static $LockTable=FALSE; if ($data==0) $LockTable=FALSE; elseif ($data==1) $LockTable=TRUE; elseif ($data==2) ; // get value $this->LockTable=$LockTable; return $LockTable; } function setLockTable() { return $this->controlLockTable(1); } function clsLockTable() { return $this->controlLockTable(0); } function getLockTable() { return $this->controlLockTable(2); } /************************/ function getConnect($connect=0) { $ret = ''; if ( is_resource($connect) ) { $conn=$connect; } else { $conn=$this->Connect_rw; $ret .= "Connect_rw: \n"; } $ret .= sprintf( "Host: %s\nServer: %s\nClient: %s\n", mysql_get_host_info($conn), mysql_get_server_info($conn), mysql_get_client_info() ); if ( $connect == 0 and is_resource($this->Connect_ro) ) { $ret .= sprintf( "Connect_ro: \nHost: %s\nServer: %s\nClient: %s\n", mysql_get_host_info($this->Connect_ro), mysql_get_server_info($this->Connect_ro), mysql_get_client_info() ); } return $ret; } function set_fetch($fm) { if ($fm==MYSQL_ASSOC) $this->fetch_mode=$fm; elseif ($fm==MYSQL_NUM) $this->fetch_mode=$fm; elseif ($fm==MYSQL_BOTH) $this->fetch_mode=$fm; else $this->fetch_mode=MYSQL_BOTH; } function fetch($point=-1,$fm='') { if ($fm=='') $fm=$this->fetch_mode; if ($point>=0) { mysql_data_seek($this->res,$point); } if ($this->rows>0) { $this->row=mysql_fetch_array($this->res, $fm); return $this->row; } else return FALSE; } } ?>