//////////////////////////////////////////////////////////////////////////////////////
function logSilentError($strTitle, $strDescription, $boolFatal, $strSourceFile, $intSourceLineNumber){
logError($strTitle, $strDescription, $boolFatal, $strSourceFile, $intSourceLineNumber, true, true);
}
//////////////////////////////////////////////////////////////////////////////////////
function logError($strTitle, $strDescription, $boolFatal, $strSourceFile, $intSourceLineNumber, $bAlwaysEmailTechSupport=false, $bSilent=false){
//this is the official error reporting mechanism
//$bAlwaysEmailTechSupport allows you to force the error to be emailed to tech support, regardless of which server it happens on
//$bSilent allows you to throw errors but not bother the user with them - useful for debugging and warnings
//global $php_errormsg, $SERVER_ADDR, $REMOTE_ADDR, $HTTP_HOST, $REQUEST_URI, $QUERY_STRING, $HTTP_REFERER, $REQUEST_METHOD, $HTTP_USER_AGENT;
global $php_errormsg;
global $db;
global $bOfficeIPAddress;
$strDestEmail = "error@config3.com";
//$strDestEmail = "gregwallis@gmail.com";
global $bIncludeRyanInErrorEmail;
if($bIncludeRyanInErrorEmail) $strDestEmail .= ",ryan@config3.com";
$bAlwaysEmailTechSupport = true;
$bShowTechnicalMessage = ($_SERVER['REMOTE_ADDR']=="67.193.179.124" or $bOfficeIPAddress or php_sapi_name()==='cli');
global $bOfficeIPAddress;
if($bOfficeIPAddress) $bShowTechnicalMessage = true;
if (!$bShowTechnicalMessage and !$bSilent){
$s = "\n";
$s.= "
\nERROR " . date("M d, Y h:i:s A") ."
\n";
$s.= "Sorry, we encountered an error while processing your request.
";
$s.= "The details of this error will be sent to the technical support department for analysis.
";
$s.= "Again, sorry for the inconvenience.
";
$s.= "
\n";
echo $s;
$s = "";
}
$s = "\n
\nERROR " . date("M d, Y h:i:s A") ."
\n";
if ($bSilent) $s.= "This is a SILENT error (the user has no knowlege of it)
\n";
$s.= "The details of this error will be sent to the technical support department for analysis.
\n";
//assemble the current URL
//note that appending the $QUERY_STRING is optional - in some cases the $REQUEST_URI already has the $QUERY_STRING baked into it (depends if we are truly submitting a form, or if's a URL with the form fields baked into it)
$strURL = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ($_SERVER['QUERY_STRING'] && (strpos($strURL, $_SERVER['QUERY_STRING'])===false)) $strURL .= $_SERVER['QUERY_STRING'];
$s.=
"$strSourceFile line $intSourceLineNumber
\n".
"REMOTE_ADDR: $_SERVER[REMOTE_ADDR]
\n" .
"$strTitle
\n".
"$strDescription
\n" .
"session[userName] = $_SESSION[userName]
\n" .
"URL: $strURL
\n" .
"Referrer: $_SERVER[HTTP_REFERER]
\n" .
"Request Method: $_SERVER[REQUEST_METHOD]
\n" .
"Client: $_SERVER[REMOTE_ADDR]
\n" .
"Server: $_SERVER[SERVER_ADDR]
\n" .
"User Agent: $_SERVER[HTTP_USER_AGENT]
\n" .
"PHP Status: $php_errormsg
\n";
if(function_exists("mysqli_error") and $db) $s .= "SQL Status: ". mysqli_error($db) . "
\n";
$s.= "\n";
$s.= "
\n";
$s.= "SESSION VARIABLES:
\n";
if(is_array($_SESSION)){
foreach($_SESSION as $key=>$val){
if(is_array($val)) $val2 = print_r($val,true); else $val2 = $val;
$s.=" $key=$val2
\n";
}
}
$s.= "
\n";
$s.= "CGI POST VARIABLES:
\n";
if(is_array($_POST)){
foreach($_POST as $key=>$val){
if(is_array($val)) $val2 = print_r($val,true); else $val2 = $val;
$s.=" $key=$val2
\n";
}
}
$s.= "
\n";
$s.= "CGI GET VARIABLES:
\n";
if(is_array($_GET)){
foreach($_GET as $key=>$val){
if(is_array($val)) $val2 = print_r($val,true); else $val2 = $val;
$s.=" $key=$val2
\n";
}
}
$s.= "
\n";
$s.= "COOKIES:
\n";
if(is_array($_COOKIE)){
foreach($_COOKIE as $key=>$val){
if(is_array($val)) $val2 = print_r($val,true); else $val2 = $val;
$s.=" $key=$val2
\n";
}
}
if(function_exists("debug_print_backtrace")){
ob_start();
debug_print_backtrace();
$trace = ob_get_contents();
ob_end_clean();
$s.= "
\n";
$s .= "STACK TRACE:
\n";
$s .= str_replace("\n", "
\n", $trace);
}
if ($bShowTechnicalMessage && !$bSilent) echo $s;
if ($bAlwaysEmailTechSupport) {
$s = str_replace("
","",$s); //strip out the html, so that it's email friendly
$s = str_replace("
","",$s);
$s = str_replace(" "," ",$s);
mail($strDestEmail,"$_SERVER[HTTP_HOST] Error", $s);
}
if ($boolFatal) exit();
}
//////////////////////////////////////////////////////////////////////////////////////
function strVarDump($var){
ob_start();
var_dump($var);
$s=ob_get_contents();
ob_end_clean();
return($s);
}
//////////////////////////////////////////////////////////////////////////////////////
function logger($strFilename, $strContent){
return; //disabled
static $strTime, $strDate;
if(!$strDate) $strDate = date("Ymd");
if(!$strTime) $strTime = date("His"); //date("Ymd_His");
$strDir = $_SERVER['DOCUMENT_ROOT'] . "/../logger/" . $strDate;
if(!is_dir($strDir)){
mkdir($strDir, 0777);
if(!is_dir($strDir)){
logSilentError("logger", "could not create directory $strDir", false, __FILE__, __LINE__);
return;
}
}
$strPathname = $strDir . "/" . $strDate . "_" . $strTime . "_" . $strFilename.".txt";
$fp = fopen($strPathname, "a");
if(!$fp){
logSilentError("logger", "could not open $strPathname for append", false, __FILE__, __LINE__);
return;
}
fwrite($fp, $strContent . "\n");
fclose($fp);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
//db credentials are defined in /inc_first.php
global $db;
$db = @mysqli_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
if(!$db){
if(function_exists("logError")) logError("Unable to connect to database", "", true, __FILE__, __LINE__);
else die("Unable to connect to database");
}
$b = @mysqli_select_db($db, MYSQL_DBNAME);
if(!$b){
if(function_exists("logError")) logError("Unable to select database", "", true, __FILE__, __LINE__);
else die("Unable to select database");
}
?>
function strNullable($v){
//return v if it is non-empty, otherwise return the string "NULL"
if ( ($v==NULL) || ($v=="") )
return("NULL");
else
return((string)$v);
}
function bIsNumeric($n){
//return true if $n contains a number
//can handle null, empty, strings, objects, etc
return( is_numeric($n) );
//if ($n=="0") return true;
//$notInt=!is_int($n); $notDbl=!is_double($n);
//if ( ($n==0) && $notInt && $notDbl ) return false; //if it's 0 and not an integer, then it must be not set or null
//if ( ($n=="") && $notInt && $notDbl ) return false; //it turns out that "" is equal to 0 (apparently). (hence the extra test)
//if ( is_object($n) ) return false;
//return true;
}
function strToDb($s){
//maintained for compatibility.
//do not use.
//use strToMySQL or strToMSSql
return strToMySQLi($s);
}
function strToMySQL($s){
return( "'" . mysql_real_escape_string($s) . "'" );
}
function strToMySQLi($s){
global $db;
if(!$db) die("missing \$db - aborting " . __FILE__ . " on line " . __LINE__);
return( "'" . mysqli_real_escape_string($db,$s) . "'" );
}
function strToMsSQL($s){
//return a quoted copy of s which has all it's single quotes doubled up
return
"'".
str_replace("'", "''", $s).
"'";
}
function strToDbNullable($s){
if ($s=="") return "NULL";
else return strToDb($s);
}
function strFromDb($s){
//if magic quotes are disabled, use this function to convert database output into usable strings
if ($s==null) return null;
else return stripSlashes($s);
}
function numToDbNullable($n){
return( numToDb($n, true) );
}
function numToDb($n, $bNullable=true){
//return $n or NULL, if $n is empty,null, unset or ""
if (bIsNumeric($n)) return( $n );
elseif ($bNullable) return( "NULL" );
else return( "0" );
}
function numFromDb($n){
return( number_format($n) );
}
function dateToDb($intDate){
//return $intDate formatted for the database
//return NULL if $intDate is null or ""
//$intDate is expected to be of the type returned by the mktime() function (int)
//PHP doesn't have "date" type, it uses integer ( which is seconds since Unix Epoch (January 1 1970 00:00:00 GMT) )
if (($intDate==null)||($intDate=="")) return "NULL";
return "'".date("Y-m-d", $intDate)."'"; //mysql uses the format "YYYY-MM-DD"
}
function dateFromDb($strDate){
//return a date, based on the database return value of $strDate
//PHP doesn't have "date" type, it uses integer ( which is seconds since Unix Epoch (January 1 1970 00:00:00 GMT) )
//we are using mssql here
return( dateFromMsSql($strDate) );
if ( ($strDate=="") || ($strDate==null) || ($strDate=="0000-00-00") ) return null;
$ar = explode("-", $strDate);
return mktime(0,0,0,$ar[1],$ar[2],$ar[0]); //mysql uses the format "YYYY-MM-DD"
}
function dateFromMsSql($strDate){
//return a date, based on the database return value of $strDate
//PHP doesn't have "date" type, it uses integer ( which is seconds since Unix Epoch (January 1 1970 00:00:00 GMT) )
if ( ($strDate=="") || ($strDate==null) || ($strDate=="0000-00-00") ) return null;
return(strToTime($strDate));
}
function datetimeToDb($intDate){
//return $intDate formatted for the database
//return NULL if $intDate is null or ""
//$intDate is expected to be of the type returned by the mktime() function (int)
//PHP doesn't have "date" type, it uses integer ( which is seconds since Unix Epoch (January 1 1970 00:00:00 GMT) )
if (($intDate==null)||($intDate=="")) return "NULL";
return "'".date("Y-m-d H:i:s", $intDate)."'"; //mysql uses the format 'YYYY-MM-DD HH:MM:SS'
}
function datetimeFromDb($strTimestamp){
//return a timestamp (integer), based on the database return value of $strTimestamp
//PHP doesn't have "date" type, it uses integer ( which is seconds since Unix Epoch (January 1 1970 00:00:00 GMT) )
if ( !$strTimestamp || ($strTimestamp=="0000-00-00 00:00:00")) return null;
return(strToTime($strTimestamp));
//$ar = split("[-: ]", $strTimestamp); //split() is depreciated
//return mktime($ar[3],$ar[4],$ar[5],$ar[1],$ar[2],$ar[0]); //mysql uses the format 'YYYY-MM-DD HH:MM:SS'
}
function strUpdateClauseForText($strFieldName, $strFieldValue){
//Apache's "magic quotes" (see php.ini) are enabled, then we don't need to worry about
//escape sequences! This is pretty nice, especially for reading data from sql.
//This is why we don't bother with calling strToDb()
return "$strFieldName=" . strToDb($strFieldValue);
}
function strUpdateClauseForNumber($strFieldName, $strFieldValue){
return( "$strFieldName=" . numToDb($strFieldValue) );
}
function strUpdateClauseForDatetime($strFieldName, $strFieldValue){
return( "$strFieldName=" . datetimeToDb($strFieldValue) );
}
function isBlank($s){
//return true if $s is null, empty, ""
if (empty($s)) return(true);
if ($s==null) return(true);
if ($s=="") return(true);
return(false);
}
FUNCTION MSSQL_ERROR($link=null){
if(!$link) $intResult = MSSQL_QUERY("select @@ERROR as ErrorCode");
else $intResult = MSSQL_QUERY("select @@ERROR as ErrorCode", $link);
$arRow = MSSQL_FETCH_ARRAY($intResult);
MSSQL_FREE_RESULT ($intResult);
RETURN $arRow[0];
}
function mssql_insert_id($link=null){
if(!$link) $intResult = MSSQL_QUERY("SELECT @@IDENTITY AS ID");
else $intResult = MSSQL_QUERY("SELECT @@IDENTITY AS ID", $link);
$arRow = MSSQL_FETCH_ARRAY($intResult);
MSSQL_FREE_RESULT ($intResult);
RETURN $arRow[0];
}
function bTableExists($dbLink, $strDBName, $strTableName){
//works in mysql only
//$sql ="SHOW TABLES WHERE `Tables_in_" . $strDBName . "` = " . strToDb($strTableName);
$sql ="SHOW TABLES IN $strDBName WHERE `Tables_in_" . $strDBName . "` = " . strToDb($strTableName);
print "$sql\n";
$result = mysqli_query($dbLink,$sql);
if(!$result){
if(function_exists("logError")){
logError("bTableExists(..., $strDBName, $strTableName)", "sql failed: $sql", true, __FILE__, __LINE__);
}else{
die(mysqli_error($dbLink) . "\n" . __FILE__ . " line " . __LINE__);
}
}
$row = mysqli_fetch_array($result);
mysqli_free_result($result);
return( $row[0] == $strTableName );
/*
$sql ="SHOW TABLES";
if($strDBName) $sql .= " IN $strDBName";
$result = mysqli_query($dbLink,$sql);
if(!$result){
if(function_exists("logError")){
logError("bTableExists(..., $strDBName, $strTableName)", "sql failed: $sql", true, __FILE__, __LINE__);
}else{
die(mysqli_error($dbLink) . "\n" . __FILE__ . " line " . __LINE__);
}
}
while($row = mysqli_fetch_array($result)){
if($row[0]==$strTableName) return(true);
}
mysqli_free_result($result);
return(false);
*/
}
function arGetRowsFromDB($strArrayFormat,$sql,$intResultType=MYSQLI_BOTH){
//$strArrayFormat can be "1D", "1D keyed by 1st col" "2D" "2D keyed by 1st col"
//the given sql query should be a select statement
//we run the sql and return the data in an array. The array format is defined by the $strArrayFormat parameter
//return the resulting array
//if $strArrayFormat=="first row" then just return the first row of the query result
//if $strArrayFormat=="1D" then the 1st column is loaded into the array values, using default array keys
//if $strArrayFormat=="1D keyed by 1st col" then the 1st column is loaded into the array keys and the 2nd is loaded into the array values
//if $strArrayFormat=="2D" then then each row is loaded into a 2D array, using default array keys for the outer array
//if $strArrayFormat=="2D keyed by 1st col" then each row is loaded into a 2D array, using the 1st column as the keys for the outer array
global $db;
$arReturn = array();
$result = mysqli_query($db,$sql);
if(!$result) logError("sql failure in arGetRowsFromDB()", $sql, true, __FILE__, __LINE__);
if($strArrayFormat=="first row"){
$arReturn = mysqli_fetch_array($result,$intResultType);
}elseif($strArrayFormat=="1D"){
while($row = mysqli_fetch_row($result)){
$arReturn[] = $row[0];
}
}elseif($strArrayFormat=="1D keyed by 1st col"){
while($row = mysqli_fetch_row($result)){
$arReturn[$row[0]] = $row[1];
}
}elseif($strArrayFormat=="2D"){
while($row = mysqli_fetch_array($result,$intResultType)){
$arReturn[] = $row;
}
}elseif($strArrayFormat=="2D keyed by 1st col"){
while($row = mysqli_fetch_array($result,$intResultType)){
$arReturn[$row[0]] = $row;
}
}else{
logError("arGetRowsFromDB()", "did not receive a valid array format paramter\n\$strArrayFormat=$strArrayFormat\n\$sql=$sql", true, __FILE__, __LINE__);
}
mysqli_free_result($result);
return($arReturn);
}
function cloneDatabase($strSourceDatabaseName, $strDestDatabaseName=null){
global $db;
if(!$strDestDatabaseName){
//get a list of databases
$sql ="SHOW databases";
if($_SESSION[debugMode]) print "$sql
\n";
$result = mysqli_query($db,$sql);
if(!$result) logError("cloneDatabase() failed", "sql failed: $sql", true, __FILE__, __LINE__);
while($row=mysqli_fetch_array($result)){
$arDatabases[] = $row[0];
}
mysqli_free_result($result);
while($intTries<100){
$strDestDatabaseName = $strSourceDatabaseName . "_" . date("Ymd");
if($intTries) $strDestDatabaseName .= "_" . $intTries;
$bSuccess = !in_array($strDestDatabaseName,$arDatabases);
if($bSuccess) break;
$intTries++;
}
if(!$bSuccess) logError("cloneDatabase() failed", "could not come up with an available name for the dest database", true, __FILE__, __LINE__);
$sql = "create database $strDestDatabaseName";
if($_SESSION[debugMode]) print "$sql
\n";
$bSuccess = mysqli_query($db,$sql);
if(!$bSuccess) logError("cloneDatabase() failed", "sql failed: $sql", true, __FILE__, __LINE__);
}
//get a list of tables
$sql ="SHOW TABLES IN $strSourceDatabaseName";
if($_SESSION[debugMode]) print "$sql
\n";
$result = mysqli_query($db,$sql);
if(!$result) logError("cloneDatabase() failed", "sql failed: $sql", true, __FILE__, __LINE__);
while($row=mysqli_fetch_array($result)){
$arTables[] = $row[0];
}
mysqli_free_result($result);
foreach($arTables as $strTable){
$sql = "CREATE TABLE $strDestDatabaseName.$strTable LIKE $strSourceDatabaseName.$strTable";
if($_SESSION[debugMode]) print "$sql
\n";
$bSuccess = mysqli_query($db,$sql);
if(!$bSuccess) logError("cloneDatabase() failed", "sql failed: $sql", true, __FILE__, __LINE__);
$sql = "INSERT INTO $strDestDatabaseName.$strTable SELECT * FROM $strSourceDatabaseName.$strTable";
if($_SESSION[debugMode]) print "$sql
\n";
$bSuccess = mysqli_query($db,$sql);
if(!$bSuccess) logError("cloneDatabase() failed", "sql failed: $sql", true, __FILE__, __LINE__);
}
}
?>