* @version 3.0.0
* @copyright Copyright (c) 2009 Cracker Tracker Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see .
*/
// Function to aquire a database link
function aquireCrackerTrackerDatabaseLink () {
// Is the link up?
if (!isCrackerTrackerDatabaseLinkUp()) {
// Then connect to the database
$GLOBALS['ctracker_link'] = mysql_connect($GLOBALS['ctracker_host'], $GLOBALS['ctracker_user'], $GLOBALS['ctracker_password']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
// Select the database
if (!mysql_select_db($GLOBALS['ctracker_dbname'], $GLOBALS['ctracker_link'])) {
// Attempt has failed
crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
} // END - if
} // END - if
}
// Checks if the link is up
function isCrackerTrackerDatabaseLinkUp () {
return ((isset($GLOBALS['ctracker_link'])) && (is_resource($GLOBALS['ctracker_link'])));
}
// Database error detected
function crackerTrackerDatabaseError ($F, $L) {
// Should we debug?
if (isCrackerTrackerDebug()) {
// Output error
print 'Function : ' . $F . '
';
print 'Line : ' . $L . '
';
print 'MySQL error : ' . mysql_error() . '
';
print 'Last SQL : '. $GLOBALS['ctracker_last_sql'] . '
';
} // END - if
// Currently only die here
crackerTrackerDie();
}
// Closes a maybe open database link
function crackerTrackerCloseDatabaseLink () {
// Is the link up?
if (isCrackerTrackerDatabaseLinkUp()) {
// Did it work?
if (!mysql_close($GLOBALS['ctracker_link'])) {
// Remove the link from global array
unset($GLOBALS['ctracker_link']);
// Attempt has failed
crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
} // END - if
} // END - if
// Remove the link from global array
unset($GLOBALS['ctracker_link']);
}
// Inserts given array, if IP/check_worm combination was not found
function crackerTrackerInsertArray ($rowData) {
// Is it found?
if (!isCrackerTrackerEntryFound($rowData)) {
// Insert first attempt stamp
$rowData['first_attempt'] = 'NOW()';
$rowData['count'] = '1';
// Prepare SQL
$SQL = 'INSERT INTO `ctracker_data` (`' . implode('`,`', array_keys($rowData)) . '`) VALUES(' . implode_secure($rowData) . ')';
// Run it
runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
} else {
// Only update the entry
updateCrackerTrackerEntry($rowData);
}
}
// Updates a given entry by just counting it up
function updateCrackerTrackerEntry ($rowData) {
// Construct the SELECT query
$SQL = 'UPDATE `ctracker_data` SET `count`=`count`+1 WHERE `remote_addr`="' . crackerTrackerEscapeString($rowData['remote_addr']) . '" AND `check_worm` = "' . crackerTrackerEscapeString($rowData['check_worm']) . '" LIMIT 1';
// Run the SQL and check if we have one line
runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
}
// Checks if an entry with IP/check_worm/domain combination is there
function isCrackerTrackerEntryFound ($rowData) {
// Construct the SELECT query
$SQL = 'SELECT `id` FROM `ctracker_data` WHERE `remote_addr`="' . crackerTrackerEscapeString($rowData['remote_addr']) . '" AND `check_worm` = "' . crackerTrackerEscapeString($rowData['check_worm']) . '" AND `server_name`="' . crackerTrackerEscapeString($rowData['server_name']) . '" LIMIT 1';
// Run the SQL and check if we have one line
return (mysql_num_rows(runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__)) == 1);
}
// Escapes the string
function crackerTrackerEscapeString ($string) {
// Is the link up?
if (!isCrackerTrackerDatabaseLinkUp()) {
// Then we cant use mysql_real_escape_string!
$string = addslashes($string);
} elseif (function_exists('mysql_real_escape_string')) {
// Use mysql_real_escape_string()
$string = mysql_real_escape_string($string, $GLOBALS['ctracker_link']);
} elseif (function_exists('mysql_escape_string')) {
// Use deprecated function
$string = mysql_escape_string($string, $GLOBALS['ctracker_link']);
} else {
// Use fall-back (bad!)
$string = addslashes($string);
}
// Return the secured string
return $string;
} // END - if
// Runs an SQL query and checks for errors
function runCrackerTrackerSql ($SQL, $F, $L) {
// Is the link up?
if (!isCrackerTrackerDatabaseLinkUp()) {
// Abort here
crackerTrackerDie();
} // END - if
// Remember last SQL
$GLOBALS['ctracker_last_sql'] = $SQL;
// Run the query
$GLOBALS['ctracker_last_result'] = mysql_query($SQL, $GLOBALS['ctracker_link']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
// And return it
return $GLOBALS['ctracker_last_result'];
}
// [EOF]
?>