]> git.mxchange.org Git - ctracker.git/blobdiff - libs/lib_connect.php
Continued:
[ctracker.git] / libs / lib_connect.php
index 56f44b6d12c858b2d8605c3f412eb9c5a5c3c576..9f90c5cbff9efd93330a9e7652dbcd1edec25135 100644 (file)
@@ -36,7 +36,7 @@ function aquireCrackerTrackerDatabaseLink () {
                } elseif (isCrackerTrackerTableCreated('ctracker_config')) {
                        // Load the config
                        crackerTrackerLoadConfig();
-               } // END - if
+               }
        } else {
                // Init fake config
                crackerTrackerInitFakeConfig();
@@ -78,7 +78,7 @@ function crackerTrackerDatabaseError ($F, $L) {
                        print 'No MySQLi available.<br />';
                }
                print 'Last SQL    : '. $GLOBALS['ctracker_last_sql'] . '<br />';
-       } // END - if
+       }
 
        // Currently only die here
        crackerTrackerDie();
@@ -86,34 +86,32 @@ function crackerTrackerDatabaseError ($F, $L) {
 
 // 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__);
@@ -125,20 +123,39 @@ function crackerTrackerInsertArray ($table, array $rowData) {
 
 // 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
@@ -157,7 +174,7 @@ function crackerTrackerEscapeString ($string) {
 
        // Return the secured string
        return $string;
-} // END - if
+}
 
 // Runs an SQL query and checks for errors
 function runCrackerTrackerSql ($sqlString, $function, $line) {
@@ -165,7 +182,7 @@ function runCrackerTrackerSql ($sqlString, $function, $line) {
        if (!isCrackerTrackerDatabaseLinkUp()) {
                // Abort here
                crackerTrackerDie();
-       } // END - if
+       }
 
        // Remember last SQL
        $GLOBALS['ctracker_last_sql'] = $sqlString;
@@ -195,8 +212,8 @@ function isCrackerTrackerTableCreated ($table) {
                        // Okay, found. So abort
                        $found = TRUE;
                        break;
-               } // END - if
-       } // END - if
+               }
+       }
 
        // Free result
        freeCrackerTrackerResult($result);
@@ -217,7 +234,7 @@ function crackerTrackerCreateTable ($table, array $columns, array $keys) {
        foreach ($columns as $column=>$type) {
                // Add this entry
                $sqlString .= '`' . $column . '` ' . $type . ', ';
-       } // END - foreach
+       }
 
        // Add table name as primary key
        $sqlString .= 'PRIMARY KEY (`' . $table . '`), ';
@@ -226,7 +243,7 @@ function crackerTrackerCreateTable ($table, array $columns, array $keys) {
        foreach ($keys as $key=>$type) {
                // Add this entry
                $sqlString .= '' . $type . ' (`' . $key . '`), ';
-       } // END - foreach
+       }
 
        // Finish SQL
        $sqlString = substr($sqlString, 0, -2) . ') TYPE=InnoDB';
@@ -247,7 +264,7 @@ function crackerTrackerUpdateDatabaseScheme () {
        if (!isCrackerTrackerDatabaseLinkUp()) {
                // Abort here silently
                return;
-       } // END - if
+       }
 
        // Is the main config table there?
        if (!isCrackerTrackerTableCreated('ctracker_config')) {
@@ -262,7 +279,7 @@ function crackerTrackerUpdateDatabaseScheme () {
 
                // Init that table
                crackerTrackerInitTable('ctracker_config');
-       } // END - if
+       }
 
        // Init update array here
        crackerTrackerInitUpdates();
@@ -277,7 +294,7 @@ function crackerTrackerUpdateDatabaseScheme () {
 
                // And count it up in the config array
                $GLOBALS['ctracker_config']['ctracker_db_version']++;
-       } // END - if
+       }
 }
 
 // Load the configuration
@@ -304,7 +321,7 @@ function getCrackerTrackerConfig ($entry) {
                        // die() on production systems
                        die();
                }
-       } // END - if
+       }
 
        // Return it
        return $GLOBALS['ctracker_config'][$entry];
@@ -316,7 +333,7 @@ function isCrackerTrackerIpSuspicious () {
        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__);
@@ -334,7 +351,7 @@ function isCrackerTrackerIpSuspicious () {
 
                // Cache the entry
                $GLOBALS['ctracker_last_suspicious_entry'] = mysqli_fetch_array($result);
-       } // END - if
+       }
 
        // Free result
        freeCrackerTrackerResult($result);
@@ -355,7 +372,7 @@ function ifCrackerTrackerIpHasTicket () {
        if ($found === TRUE) {
                // Cache the ticket data
                $GLOBALS['ctracker_last_ticket'] = mysqli_fetch_array($result);
-       } // END - if
+       }
 
        // Free result
        freeCrackerTrackerResult($result);