<?php
/*
* ConPortal - Pomona College ITS scheduling appplication
* Copyright (C) 2005-2006 Pomona College
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
function db_connect(&$db)
{
$db = mysql_pconnect(DB_HOST, DB_USERNAME, DB_PASSWORD);
if (!$db) {
echo 'Connection to database server failed: ' . mysql_error() .
'<br />Please <a href="mailto:' . SUPERS . '">contact '.
'the Supers</a> immediately.';
exit;
}
if (!mysql_select_db(DB_DATABASE)) {
echo 'Connection to database failed: ' . mysql_error() .
'<br />Please <a href="mailto:' . SUPERS . '">contact '.
'the Supers</a> immediately.';
exit;
}
}
function start_transaction()
{
safeQuery("START TRANSACTION");
}
function rollback_transaction()
{
safeQuery("ROLLBACK");
}
function commit_transaction()
{
safeQuery("COMMIT");
}
/*
* "Safe" query - performs query with error checking;
* returns only if the operation was successful.
* You can pass additional arguments to safeQuery and it
* will use them sprintf-style.
*/
function safeQuery ($query)
{
$args = func_get_args();
unset($args[0]); // get rid of $query from the arglist
$query = vsprintf($query, $args);
$result = mysql_query($query);
if (!$result)
{
echo "Could not perform query:<br/>$query<br/>" . mysql_error();
echo "<pre>";
var_dump(debug_backtrace());
echo "</pre>";
mysql_query("rollback");
die();
}
return $result;
}
/*
From "Ryan" 07-Jul-2008 01:52
http://us2.php.net/manual/en/function.mysql-query.php
*/
function sql_sanitize($string){
if(get_magic_quotes_gpc())
{
$string = stripslashes($string);
}
$badWords = "(delete)|(update)|(union)|(insert)|(drop)|(http)|(--)";
$string = eregi_replace($badWords, "", $string);
if (phpversion() >= '4.3.0')
{
$string = mysql_real_escape_string($string);
}
else
{
$string = mysql_escape_string($string);
}
return $string;
}
?>