} elseif (isCrackerTrackerTableCreated('ctracker_config')) {
// Load the config
crackerTrackerLoadConfig();
- } // END - if
+ }
} else {
// Init fake config
crackerTrackerInitFakeConfig();
print 'No MySQLi available.<br />';
}
print 'Last SQL : '. $GLOBALS['ctracker_last_sql'] . '<br />';
- } // 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 (!mysqli_close($GLOBALS['ctracker_link'])) {
- // Attempt has failed
- crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
- } // END - if
- } // END - if
+ // The link should be up here
+ if (!isCrackerTrackerDatabaseLinkUp()) {
+ // Throw exception
+ throw new BadFunctionCallException('Link is not up.');
+ }
+
+ // Did it work?
+ if (!mysqli_close($GLOBALS['ctracker_link'])) {
+ // Attempt has failed
+ crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
+ }
}
// Inserts given array, if IP/check_get combination was not found
function crackerTrackerInsertArray ($table, array $rowData) {
- // Is there a link up?
- if (!isCrackerTrackerDatabaseLinkUp()) {
- // Abort silently here
- return FALSE;
- } // END - if
-
// Is it found?
if (!isCrackerTrackerEntryFound($rowData)) {
- // Prepare SQL
- $sqlString = 'INSERT INTO `' . $table . '` (`' . implode('`,`', array_keys($rowData)) . '`) VALUES(' . implode_secure($rowData) . ')';
-
// Reset insert id
$GLOBALS['ctracker_last_insert_id'] = FALSE;
// Run it
- runCrackerTrackerSql($sqlString, __FUNCTION__, __LINE__);
+ runCrackerTrackerSql(sprintf("INSERT INTO `%s` (`%s`) VALUES(%s)",
+ $table,
+ implode('`,`', array_keys($rowData)),
+ implode_secure($rowData)
+ ), __FUNCTION__, __LINE__);
// Remember the last insert id
$GLOBALS['ctracker_last_insert_id'] = mysqli_insert_id($GLOBALS['ctracker_link']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
// Updates a given entry by just counting it up
function updateCrackerTrackerEntry (array $rowData, $countColumn = 'count') {
- // Construct the SELECT query
- $sqlString = 'UPDATE `ctracker_data` SET `' . $countColumn . '`=`' . $countColumn . '`+1 WHERE (`remote_addr`="' . crackerTrackerEscapeString($rowData['remote_addr']) . '" AND `proxy_addr`="' . crackerTrackerEscapeString($rowData['proxy_addr']) . '") LIMIT 1';
+ // The link should be up here
+ if (!isCrackerTrackerDatabaseLinkUp()) {
+ // Throw exception
+ throw new BadFunctionCallException('Link is not up.');
+ }
// Run the SQL and check if we have one line
- runCrackerTrackerSql($sqlString, __FUNCTION__, __LINE__);
+ runCrackerTrackerSql(sprintf("UPDATE `ctracker_data` SET `%s`=`%s`+1 WHERE (`remote_addr`='%s' AND `proxy_addr`=' . ') LIMIT 1",
+ $countColumn,
+ $countColumn,
+ crackerTrackerEscapeString($rowData['remote_addr']),
+ crackerTrackerEscapeString($rowData['proxy_addr'])
+ ), __FUNCTION__, __LINE__);
}
// Checks if an entry with IP/check_get/domain combination is there
function isCrackerTrackerEntryFound (array $rowData) {
- // Construct the SELECT query
- $sqlString = 'SELECT `id` FROM `ctracker_data` WHERE (`remote_addr`="' . crackerTrackerEscapeString($rowData['remote_addr']) . '" OR `proxy_addr`="' . crackerTrackerEscapeString($rowData['proxy_addr']) . '") AND `check_get` = "' . crackerTrackerEscapeString($rowData['check_get']) . '" AND `server_name`="' . crackerTrackerEscapeString($rowData['server_name']) . '" LIMIT 1';
+ // The link should be up here
+ if (!isCrackerTrackerDatabaseLinkUp()) {
+ // Throw exception
+ throw new BadFunctionCallException('Link is not up.');
+ }
// Run the SQL and check if we have one line
- return ((isCrackerTrackerDatabaseLinkUp()) && (mysqli_num_rows(runCrackerTrackerSql($sqlString, __FUNCTION__, __LINE__)) == 1));
+ $result = runCrackerTrackerSql(sprintf("SELECT `id` FROM `ctracker_data` WHERE (`remote_addr`='%s' OR `proxy_addr`='%s') AND `check_get` = '%s' AND `server_name`='%s' LIMIT 1'",
+ crackerTrackerEscapeString($rowData['remote_addr']),
+ crackerTrackerEscapeString($rowData['proxy_addr']),
+ crackerTrackerEscapeString($rowData['check_get']),
+ crackerTrackerEscapeString($rowData['server_name'])
+ ), __FUNCTION__, __LINE__);
+
+ // Check count of rows
+ return (mysqli_num_rows($result) == 1);
}
// Escapes the string
// Return the secured string
return $string;
-} // END - if
+}
// Runs an SQL query and checks for errors
function runCrackerTrackerSql ($sqlString, $function, $line) {
if (!isCrackerTrackerDatabaseLinkUp()) {
// Abort here
crackerTrackerDie();
- } // END - if
+ }
// Remember last SQL
$GLOBALS['ctracker_last_sql'] = $sqlString;
// Okay, found. So abort
$found = TRUE;
break;
- } // END - if
- } // END - if
+ }
+ }
// Free result
freeCrackerTrackerResult($result);
foreach ($columns as $column=>$type) {
// Add this entry
$sqlString .= '`' . $column . '` ' . $type . ', ';
- } // END - foreach
+ }
// Add table name as primary key
$sqlString .= 'PRIMARY KEY (`' . $table . '`), ';
foreach ($keys as $key=>$type) {
// Add this entry
$sqlString .= '' . $type . ' (`' . $key . '`), ';
- } // END - foreach
+ }
// Finish SQL
$sqlString = substr($sqlString, 0, -2) . ') TYPE=InnoDB';
if (!isCrackerTrackerDatabaseLinkUp()) {
// Abort here silently
return;
- } // END - if
+ }
// Is the main config table there?
if (!isCrackerTrackerTableCreated('ctracker_config')) {
// Init that table
crackerTrackerInitTable('ctracker_config');
- } // END - if
+ }
// Init update array here
crackerTrackerInitUpdates();
// And count it up in the config array
$GLOBALS['ctracker_config']['ctracker_db_version']++;
- } // END - if
+ }
}
// Load the configuration
// die() on production systems
die();
}
- } // END - if
+ }
// Return it
return $GLOBALS['ctracker_config'][$entry];
if (!isCrackerTrackerDatabaseLinkUp()) {
// Skip this step silently, all is not suspicious
return FALSE;
- } // END - if
+ }
// Check if an entry is there
$result = runCrackerTrackerSql("SELECT COUNT(`id`) AS `cnt` FROM `ctracker_data` USE INDEX (`remote_proxy_last`) WHERE `remote_addr`='" . determineCrackerTrackerRealRemoteAddress() . "' OR `proxy_addr`='" . getenv('REMOTE_ADDR') . "' LIMIT 1", __FUNCTION__, __LINE__);
// Cache the entry
$GLOBALS['ctracker_last_suspicious_entry'] = mysqli_fetch_array($result);
- } // END - if
+ }
// Free result
freeCrackerTrackerResult($result);
if ($found === TRUE) {
// Cache the ticket data
$GLOBALS['ctracker_last_ticket'] = mysqli_fetch_array($result);
- } // END - if
+ }
// Free result
freeCrackerTrackerResult($result);