2004-12-12 03:51:51 +00:00
|
|
|
<?php
|
2005-01-14 05:28:58 +00:00
|
|
|
$hAppdbLink = null;
|
|
|
|
|
$hBugzillaLink = null;
|
|
|
|
|
|
2006-11-25 17:24:44 +00:00
|
|
|
define("MYSQL_DEADLOCK_ERRNO", 1213);
|
2006-07-19 19:20:16 +00:00
|
|
|
|
2007-08-03 23:27:25 +00:00
|
|
|
function query_appdb($sQuery, $sComment="")
|
2004-03-15 16:22:00 +00:00
|
|
|
{
|
2005-01-12 02:43:52 +00:00
|
|
|
global $hAppdbLink;
|
2004-03-15 16:22:00 +00:00
|
|
|
|
2005-01-14 05:28:58 +00:00
|
|
|
if(!is_resource($hAppdbLink))
|
2004-12-12 03:51:51 +00:00
|
|
|
{
|
2005-01-14 16:05:14 +00:00
|
|
|
// The last argument makes sure we are really opening a new connection
|
2007-08-03 23:27:25 +00:00
|
|
|
$hAppdbLink = mysql_connect(APPS_DBHOST, APPS_DBUSER, APPS_DBPASS, true);
|
2007-09-18 20:51:28 -04:00
|
|
|
if(!$hAppdbLink)
|
|
|
|
|
die("Database error, please try again soon.");
|
2005-01-14 16:05:14 +00:00
|
|
|
mysql_select_db(APPS_DB, $hAppdbLink);
|
2004-12-12 03:51:51 +00:00
|
|
|
}
|
2006-07-19 19:20:16 +00:00
|
|
|
|
|
|
|
|
$iRetries = 2;
|
|
|
|
|
|
|
|
|
|
/* we need to retry queries that hit transaction deadlocks */
|
|
|
|
|
/* as a deadlock isn't really a failure */
|
|
|
|
|
while($iRetries)
|
|
|
|
|
{
|
|
|
|
|
$hResult = mysql_query($sQuery, $hAppdbLink);
|
|
|
|
|
if(!$hResult)
|
|
|
|
|
{
|
|
|
|
|
/* if this error isn't a deadlock OR if it is a deadlock and we've */
|
|
|
|
|
/* run out of retries, report the error */
|
|
|
|
|
$iErrno = mysql_errno();
|
|
|
|
|
if(($iErrno != MYSQL_DEADLOCK_ERRNO) || (($iErrno == MYSQL_DEADLOCK_ERRNO) && ($iRetries <= 0)))
|
|
|
|
|
{
|
|
|
|
|
query_error($sQuery, $sComment);
|
|
|
|
|
return $hResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$iRetries--;
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
return $hResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
2004-03-15 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-24 04:20:32 +00:00
|
|
|
/*
|
|
|
|
|
* Wildcard Rules
|
|
|
|
|
* SCALAR (?) => 'original string quoted'
|
|
|
|
|
* OPAQUE (&) => 'string from file quoted'
|
|
|
|
|
* MISC (~) => original string (left 'as-is')
|
|
|
|
|
*
|
|
|
|
|
* NOTE: These rules convienently match those for Pear DB
|
|
|
|
|
*
|
|
|
|
|
* MySQL Prepare Function
|
|
|
|
|
* By: Kage (Alex)
|
|
|
|
|
* KageKonjou@GMail.com
|
|
|
|
|
* http://us3.php.net/manual/en/function.mysql-query.php#53400
|
|
|
|
|
*
|
|
|
|
|
* Modified by CMM 20060622
|
|
|
|
|
*
|
|
|
|
|
* Values are mysql_real_escape_string()'d to prevent against injection attacks
|
|
|
|
|
* See http://php.net/mysql_real_escape_string for more information about why this is the case
|
|
|
|
|
*
|
2006-07-04 06:19:06 +00:00
|
|
|
* Usage:
|
|
|
|
|
* $hResult = query_parameters("Select * from mytable where userid = '?'",
|
|
|
|
|
* $iUserId);
|
|
|
|
|
*
|
|
|
|
|
* Note:
|
|
|
|
|
* Ensure that all variables are passed as parameters to query_parameters()
|
|
|
|
|
* to ensure that sql injection attacks are prevented against
|
|
|
|
|
*
|
2006-06-24 04:20:32 +00:00
|
|
|
*/
|
|
|
|
|
function query_parameters()
|
|
|
|
|
{
|
|
|
|
|
global $hAppdbLink;
|
|
|
|
|
|
|
|
|
|
if(!is_resource($hAppdbLink))
|
|
|
|
|
{
|
|
|
|
|
// The last argument makes sure we are really opening a new connection
|
2007-08-03 23:27:25 +00:00
|
|
|
$hAppdbLink = mysql_connect(APPS_DBHOST, APPS_DBUSER, APPS_DBPASS, true);
|
2006-06-24 04:20:32 +00:00
|
|
|
mysql_select_db(APPS_DB, $hAppdbLink);
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-11 19:25:16 +00:00
|
|
|
$aData = func_get_args();
|
|
|
|
|
$sQuery = $aData[0];
|
|
|
|
|
$aTokens = split("[&?~]", $sQuery); /* NOTE: no need to escape characters inside of [] in regex */
|
|
|
|
|
$sPreparedquery = $aTokens[0];
|
|
|
|
|
$iCount = strlen($aTokens[0]);
|
2006-06-24 04:20:32 +00:00
|
|
|
|
2006-06-27 16:39:40 +00:00
|
|
|
/* do we have the correct number of tokens to the number of parameters provided? */
|
2006-07-11 19:25:16 +00:00
|
|
|
if(count($aTokens) != count($aData))
|
2006-06-27 16:39:40 +00:00
|
|
|
return NULL; /* count mismatch, return NULL */
|
|
|
|
|
|
2006-07-11 19:25:16 +00:00
|
|
|
for ($i=1; $i < count($aTokens); $i++)
|
2006-06-24 04:20:32 +00:00
|
|
|
{
|
2006-07-11 19:25:16 +00:00
|
|
|
$char = substr($sQuery, $iCount, 1);
|
|
|
|
|
$iCount += (strlen($aTokens[$i])+1);
|
2006-06-24 04:20:32 +00:00
|
|
|
if ($char == "&")
|
|
|
|
|
{
|
2006-07-11 19:25:16 +00:00
|
|
|
$fp = @fopen($aData[$i], 'r');
|
2006-06-24 04:20:32 +00:00
|
|
|
$pdata = "";
|
|
|
|
|
if ($fp)
|
|
|
|
|
{
|
2006-07-11 19:25:16 +00:00
|
|
|
while (($sBuf = fread($fp, 4096)) != false)
|
2006-06-24 04:20:32 +00:00
|
|
|
{
|
2006-07-11 19:25:16 +00:00
|
|
|
$pdata .= $sBuf;
|
2006-06-24 04:20:32 +00:00
|
|
|
}
|
|
|
|
|
fclose($fp);
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
{
|
2006-07-11 19:25:16 +00:00
|
|
|
$pdata = &$aData[$i];
|
2006-06-24 04:20:32 +00:00
|
|
|
}
|
2006-07-11 19:25:16 +00:00
|
|
|
$sPreparedquery .= ($char != "~" ? mysql_real_escape_string($pdata) : $pdata);
|
|
|
|
|
$sPreparedquery .= $aTokens[$i];
|
2006-06-24 04:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-11 19:25:16 +00:00
|
|
|
return query_appdb($sPreparedquery);
|
2006-06-24 04:20:32 +00:00
|
|
|
}
|
2004-03-15 16:22:00 +00:00
|
|
|
|
2005-01-12 02:43:52 +00:00
|
|
|
function query_bugzilladb($sQuery,$sComment="")
|
|
|
|
|
{
|
|
|
|
|
global $hBugzillaLink;
|
|
|
|
|
|
2005-01-14 05:28:58 +00:00
|
|
|
if(!is_resource($hBugzillaLink))
|
2004-12-12 03:51:51 +00:00
|
|
|
{
|
2005-01-14 16:05:14 +00:00
|
|
|
// The last argument makes sure we are really opening a new connection
|
2007-09-18 20:51:28 -04:00
|
|
|
$hBugzillaLink = mysql_connect(BUGZILLA_DBHOST, BUGZILLA_DBUSER, BUGZILLA_DBPASS, true);
|
2005-08-01 20:53:44 +00:00
|
|
|
if(!$hBugzillaLink) return;
|
2005-01-14 16:05:14 +00:00
|
|
|
mysql_select_db(BUGZILLA_DB, $hBugzillaLink);
|
2004-12-12 03:51:51 +00:00
|
|
|
}
|
2005-01-14 16:05:14 +00:00
|
|
|
|
2005-01-12 02:43:52 +00:00
|
|
|
$hResult = mysql_query($sQuery, $hBugzillaLink);
|
2005-01-12 17:29:04 +00:00
|
|
|
if(!$hResult) query_error($sQuery, $sComment);
|
2004-12-29 03:36:57 +00:00
|
|
|
return $hResult;
|
2004-03-15 16:22:00 +00:00
|
|
|
}
|
2004-12-29 18:42:34 +00:00
|
|
|
|
2005-01-12 02:43:52 +00:00
|
|
|
|
2005-01-12 17:29:04 +00:00
|
|
|
function query_error($sQuery, $sComment="")
|
2005-01-12 02:43:52 +00:00
|
|
|
{
|
2006-09-26 02:10:57 +00:00
|
|
|
static $bInQueryError = false;
|
|
|
|
|
|
|
|
|
|
// if we are already reporting an error we can't report it again
|
|
|
|
|
// as that indicates that error reporting itself produced an error
|
|
|
|
|
if($bInQueryError)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// record that we are inside of this function, we don't want to recurse
|
|
|
|
|
$bInQueryError = true;
|
|
|
|
|
|
2006-07-19 17:34:59 +00:00
|
|
|
error_log::log_error(ERROR_SQL, "Query: '".$sQuery."' ".
|
|
|
|
|
"mysql_errno(): '".mysql_errno()."' ".
|
|
|
|
|
"mysql_error(): '".mysql_error()."' ".
|
|
|
|
|
"comment: '".$sComment."'");
|
2006-07-19 17:18:16 +00:00
|
|
|
|
2006-07-19 16:37:54 +00:00
|
|
|
$sStatusMessage = "<p><b>An internal error has occurred and has been logged and reported to appdb admins</b></p>";
|
|
|
|
|
addmsg($sStatusMessage);
|
2006-09-26 02:10:57 +00:00
|
|
|
|
|
|
|
|
$bInQueryError = false; // clear variable upon exit
|
2005-01-12 02:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
2007-08-03 23:27:25 +00:00
|
|
|
function query_fetch_row($hResult)
|
|
|
|
|
{
|
|
|
|
|
return mysql_fetch_row($hResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_fetch_object($hResult)
|
|
|
|
|
{
|
|
|
|
|
return mysql_fetch_object($hResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_appdb_insert_id()
|
|
|
|
|
{
|
|
|
|
|
global $hAppdbLink;
|
|
|
|
|
return mysql_insert_id($hAppdbLink);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_bugzilla_insert_id()
|
|
|
|
|
{
|
|
|
|
|
global $hBugzillaLink;
|
|
|
|
|
return mysql_insert_id($hBugzillaLink);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_num_rows($hResult)
|
|
|
|
|
{
|
|
|
|
|
return mysql_num_rows($hResult);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_escape_string($sString)
|
|
|
|
|
{
|
|
|
|
|
return mysql_real_escape_string($sString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_field_type($hResult, $iFieldOffset)
|
|
|
|
|
{
|
|
|
|
|
return mysql_field_type($hResult, $iFieldOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_field_name($hResult, $iFieldOffset)
|
|
|
|
|
{
|
|
|
|
|
return mysql_field_name($hResult, $iFieldOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_field_len($hResult, $ifieldOffset)
|
|
|
|
|
{
|
|
|
|
|
return mysql_field_len($hResult, $iFieldOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_field_flags($hResult, $iFieldOffset)
|
|
|
|
|
{
|
|
|
|
|
return mysql_field_flags($hResult, $iFieldOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function query_fetch_field($hResult, $iFieldOffset)
|
|
|
|
|
{
|
|
|
|
|
return mysql_fetch_field($hResult, $iFieldOffset);
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-15 16:22:00 +00:00
|
|
|
?>
|