mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
387 lines
10 KiB
PHP
387 lines
10 KiB
PHP
<?php
|
|
|
|
/**
|
|
* CMDBSource
|
|
* database access wrapper
|
|
*
|
|
* @package iTopORM
|
|
* @author Romain Quetiez <romainquetiez@yahoo.fr>
|
|
* @author Denis Flaven <denisflave@free.fr>
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
* @link www.itop.com
|
|
* @since 1.0
|
|
* @version 1.1.1.1 $
|
|
*/
|
|
|
|
require_once('MyHelpers.class.inc.php');
|
|
|
|
class MySQLException extends CoreException
|
|
{
|
|
public function __construct($sIssue, $aContext)
|
|
{
|
|
$aContext['mysql_error'] = mysql_error();
|
|
parent::__construct($sIssue, $aContext);
|
|
}
|
|
}
|
|
|
|
|
|
class CMDBSource
|
|
{
|
|
protected static $m_sDBHost;
|
|
protected static $m_sDBUser;
|
|
protected static $m_sDBPwd;
|
|
protected static $m_sDBName;
|
|
protected static $m_resDBLink;
|
|
|
|
public static function Init($sServer, $sUser, $sPwd, $sSource = '')
|
|
{
|
|
self::$m_sDBHost = $sServer;
|
|
self::$m_sDBUser = $sUser;
|
|
self::$m_sDBPwd = $sPwd;
|
|
self::$m_sDBName = $sSource;
|
|
if (!self::$m_resDBLink = mysql_pconnect($sServer, $sUser, $sPwd))
|
|
{
|
|
throw new MySQLException('Could not connect to the DB server', array('host'=>$sServer, 'user'=>$sUser));
|
|
}
|
|
if (!empty($sSource))
|
|
{
|
|
if (!mysql_select_db($sSource, self::$m_resDBLink))
|
|
{
|
|
throw new MySQLException('Could not select DB', array('db_name'=>$sSource));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function IsDB($sSource)
|
|
{
|
|
$aDBs = self::QueryToCol('SHOW DATABASES', 'Database');
|
|
|
|
// Show Database does return the DB names in lower case
|
|
$sSourceRef = strtolower($sSource);
|
|
return (in_array($sSourceRef, $aDBs));
|
|
}
|
|
|
|
public static function SelectDB($sSource)
|
|
{
|
|
if (!mysql_select_db($sSource, self::$m_resDBLink))
|
|
{
|
|
throw new MySQLException('Could not select DB', array('db_name'=>$sSource));
|
|
}
|
|
self::$m_sDBName = $sSource;
|
|
}
|
|
|
|
public static function CreateDB($sSource)
|
|
{
|
|
self::Query("CREATE DATABASE `$sSource`");
|
|
self::SelectDB($sSource);
|
|
}
|
|
|
|
public static function DropDB($sDBToDrop = '')
|
|
{
|
|
if (empty($sDBToDrop))
|
|
{
|
|
$sDBToDrop = self::$m_sDBName;
|
|
}
|
|
self::Query("DROP DATABASE `$sDBToDrop`");
|
|
if ($sDBToDrop == self::$m_sDBName)
|
|
{
|
|
self::$m_sDBName = '';
|
|
}
|
|
}
|
|
|
|
public static function CreateTable($sQuery)
|
|
{
|
|
$res = self::Query($sQuery);
|
|
self::_TablesInfoCacheReset(); // reset the table info cache!
|
|
return $res;
|
|
}
|
|
|
|
public static function DropTable($sTable)
|
|
{
|
|
$res = self::Query("DROP TABLE `$sTable`");
|
|
self::_TablesInfoCacheReset(true); // reset the table info cache!
|
|
return $res;
|
|
}
|
|
|
|
public static function DBHost() {return self::$m_sDBHost;}
|
|
public static function DBUser() {return self::$m_sDBUser;}
|
|
public static function DBPwd() {return self::$m_sDBPwd;}
|
|
public static function DBName() {return self::$m_sDBName;}
|
|
|
|
public static function Quote($value, $bAlways = false, $cQuoteStyle = "'")
|
|
{
|
|
// Quote variable and protect against SQL injection attacks
|
|
// Code found in the PHP documentation: quote_smart($value)
|
|
|
|
// bAlways should be set to true when the purpose is to create a IN clause,
|
|
// otherwise and if there is a mix of strings and numbers, the clause
|
|
// would always be false
|
|
|
|
if (is_array($value))
|
|
{
|
|
$aRes = array();
|
|
foreach ($value as $key => $itemvalue)
|
|
{
|
|
$aRes[$key] = self::Quote($itemvalue, $bAlways, $cQuoteStyle);
|
|
}
|
|
return $aRes;
|
|
}
|
|
|
|
// Stripslashes
|
|
if (get_magic_quotes_gpc())
|
|
{
|
|
$value = stripslashes($value);
|
|
}
|
|
// Quote if not a number or a numeric string
|
|
if ($bAlways || !is_numeric($value))
|
|
{
|
|
$value = $cQuoteStyle . mysql_real_escape_string($value, self::$m_resDBLink) . $cQuoteStyle;
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
public static function Query($sSQLQuery)
|
|
{
|
|
// Add info into the query as a comment, for easier error tracking
|
|
//
|
|
//if ($user_contact) $aTraceInf['userID'] = $user_contact->get_key();
|
|
//$aTraceInf['file'] = __FILE__;
|
|
if ($_SERVER['REQUEST_URI']) $aTraceInf['requestURI'] = $_SERVER['REQUEST_URI'];
|
|
$i = 0;
|
|
foreach(debug_backtrace() as $aCallData)
|
|
{
|
|
$sClass = key_exists("class", $aCallData) ? $aCallData["class"]."::" : "";
|
|
//if ($aCallData['function'] !== 'mysql_simple_query' AND $sClass !== 'r2_set::')
|
|
//{
|
|
if ($i == 3) break;
|
|
$aTraceInf['function'.$i] = $sClass.$aCallData["function"]." on line ".$aCallData['line'];
|
|
$i++;
|
|
//}
|
|
}
|
|
// disabled until we need it really!
|
|
// $sSQLQuery = $sSQLQuery.MyHelpers::MakeSQLComment($aTraceInf);
|
|
|
|
$mu_t1 = MyHelpers::getmicrotime();
|
|
$result = mysql_query($sSQLQuery, self::$m_resDBLink);
|
|
if (!$result)
|
|
{
|
|
throw new MySQLException('Failed to issue SQL query', array('query' => $sSQLQuery));
|
|
}
|
|
$mu_t2 = MyHelpers::getmicrotime();
|
|
// #@# todo - query_trace($sSQLQuery, $mu_t2 - $mu_t1);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function GetInsertId()
|
|
{
|
|
return mysql_insert_id(self::$m_resDBLink);
|
|
}
|
|
public static function InsertInto($sSQLQuery)
|
|
{
|
|
if (self::Query($sSQLQuery))
|
|
{
|
|
return self::GetInsertId();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function QueryToArray($sSql)
|
|
{
|
|
$aData = array();
|
|
$result = mysql_query($sSql, self::$m_resDBLink);
|
|
if (!$result)
|
|
{
|
|
throw new MySQLException('Failed to issue SQL query', array('query' => $sSql));
|
|
}
|
|
while ($aRow = mysql_fetch_array($result, MYSQL_BOTH))
|
|
{
|
|
$aData[] = $aRow;
|
|
}
|
|
mysql_free_result($result);
|
|
return $aData;
|
|
}
|
|
|
|
public static function QueryToCol($sSql, $col)
|
|
{
|
|
$aColumn = array();
|
|
$aData = self::QueryToArray($sSql);
|
|
foreach($aData as $aRow)
|
|
{
|
|
@$aColumn[] = $aRow[$col];
|
|
}
|
|
return $aColumn;
|
|
}
|
|
|
|
public static function ExplainQuery($sSql)
|
|
{
|
|
$aData = array();
|
|
$result = mysql_query("EXPLAIN $sSql", self::$m_resDBLink);
|
|
if (!$result)
|
|
{
|
|
throw new MySQLException('Failed to issue SQL query', array('query' => $sSql));
|
|
}
|
|
|
|
$aNames = array();
|
|
for ($i = 0; $i < mysql_num_fields($result) ; $i++)
|
|
{
|
|
$meta = mysql_fetch_field($result, $i);
|
|
if (!$meta)
|
|
{
|
|
throw new MySQLException('mysql_fetch_field: No information available', array('query'=>$sSql, 'i'=>$i));
|
|
}
|
|
else
|
|
{
|
|
$aNames[] = $meta->name;
|
|
}
|
|
}
|
|
|
|
$aData[] = $aNames;
|
|
while ($aRow = mysql_fetch_array($result, MYSQL_ASSOC))
|
|
{
|
|
$aData[] = $aRow;
|
|
}
|
|
mysql_free_result($result);
|
|
return $aData;
|
|
}
|
|
|
|
public static function TestQuery($sSql)
|
|
{
|
|
$result = mysql_query("EXPLAIN $sSql", self::$m_resDBLink);
|
|
if (!$result)
|
|
{
|
|
return mysql_error();
|
|
}
|
|
|
|
mysql_free_result($result);
|
|
return '';
|
|
}
|
|
|
|
public static function NbRows($result)
|
|
{
|
|
return mysql_num_rows($result);
|
|
}
|
|
|
|
public static function FetchArray($result)
|
|
{
|
|
return mysql_fetch_array($result, MYSQL_ASSOC);
|
|
}
|
|
|
|
public static function Seek($result, $iRow)
|
|
{
|
|
return mysql_data_seek($result, $iRow);
|
|
}
|
|
|
|
public static function FreeResult($result)
|
|
{
|
|
return mysql_free_result($result);
|
|
}
|
|
|
|
public static function IsTable($sTable)
|
|
{
|
|
$aTableInfo = self::GetTableInfo($sTable);
|
|
return (!empty($aTableInfo));
|
|
}
|
|
|
|
public static function IsKey($sTable, $iKey)
|
|
{
|
|
$aTableInfo = self::GetTableInfo($sTable);
|
|
if (empty($aTableInfo)) return false;
|
|
if (!array_key_exists($iKey, $aTableInfo["Fields"])) return false;
|
|
$aFieldData = $aTableInfo["Fields"][$iKey];
|
|
if (!array_key_exists("Key", $aFieldData)) return false;
|
|
return ($aFieldData["Key"] == "PRI");
|
|
}
|
|
|
|
public static function IsAutoIncrement($sTable, $sField)
|
|
{
|
|
$aTableInfo = self::GetTableInfo($sTable);
|
|
if (empty($aTableInfo)) return false;
|
|
if (!array_key_exists($sField, $aTableInfo["Fields"])) return false;
|
|
$aFieldData = $aTableInfo["Fields"][$sField];
|
|
if (!array_key_exists("Extra", $aFieldData)) return false;
|
|
//MyHelpers::debug_breakpoint($aFieldData);
|
|
return (strstr($aFieldData["Extra"], "auto_increment"));
|
|
}
|
|
|
|
public static function IsField($sTable, $sField)
|
|
{
|
|
$aTableInfo = self::GetTableInfo($sTable);
|
|
if (empty($aTableInfo)) return false;
|
|
if (!array_key_exists($sField, $aTableInfo["Fields"])) return false;
|
|
return true;
|
|
}
|
|
|
|
public static function IsNullAllowed($sTable, $sField)
|
|
{
|
|
$aTableInfo = self::GetTableInfo($sTable);
|
|
if (empty($aTableInfo)) return false;
|
|
if (!array_key_exists($sField, $aTableInfo["Fields"])) return false;
|
|
$aFieldData = $aTableInfo["Fields"][$sField];
|
|
return (strtolower($aFieldData["Null"]) == "yes");
|
|
}
|
|
|
|
// Returns an array of (fieldname => array of field info)
|
|
public static function GetTableFieldsList($sTable)
|
|
{
|
|
assert(!empty($sTable));
|
|
|
|
$aTableInfo = self::GetTableInfo($sTable);
|
|
if (empty($aTableInfo)) return array(); // #@# or an error ?
|
|
|
|
return array_keys($aTableInfo["Fields"]);
|
|
}
|
|
|
|
// Cache the information about existing tables, and their fields
|
|
private static $m_aTablesInfo = array();
|
|
private static function _TablesInfoCacheReset()
|
|
{
|
|
self::$m_aTablesInfo = array();
|
|
}
|
|
private static function _TablesInfoCacheInit()
|
|
{
|
|
if (!empty(self::$m_aTablesInfo)) return;
|
|
|
|
$aTables = self::QueryToArray("SHOW TABLES FROM `".self::$m_sDBName."`", 0);
|
|
foreach ($aTables as $aTableData)
|
|
{
|
|
$sTableName = $aTableData[0];
|
|
$aFields = self::QueryToArray("SHOW COLUMNS FROM `$sTableName`");
|
|
// Note: without backticks, you get an error with some table names (e.g. "group")
|
|
foreach ($aFields as $aFieldData)
|
|
{
|
|
$sFieldName = $aFieldData["Field"];
|
|
self::$m_aTablesInfo[$sTableName]["Fields"][$sFieldName] =
|
|
array
|
|
(
|
|
"Name"=>$aFieldData["Field"],
|
|
"Type"=>$aFieldData["Type"],
|
|
"Null"=>$aFieldData["Null"],
|
|
"Key"=>$aFieldData["Key"],
|
|
"Default"=>$aFieldData["Default"],
|
|
"Extra"=>$aFieldData["Extra"]
|
|
);
|
|
}
|
|
}
|
|
}
|
|
public static function EnumTables()
|
|
{
|
|
self::_TablesInfoCacheInit();
|
|
return array_keys(self::$m_aTablesInfo);
|
|
}
|
|
public static function GetTableInfo($sTable)
|
|
{
|
|
self::_TablesInfoCacheInit();
|
|
|
|
// table names are transformed into lower case
|
|
// is that 100% specific to innodb and myISAM?
|
|
$sTableLC = strtolower($sTable);
|
|
|
|
if (array_key_exists($sTableLC, self::$m_aTablesInfo)) return self::$m_aTablesInfo[$sTableLC];
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
?>
|