3 * Database connection library
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2009 Cracker Tracker Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 // Function to aquire a database link
26 function aquireCrackerTrackerDatabaseLink () {
28 if (!isCrackerTrackerDatabaseLinkUp()) {
29 // Then connect to the database
30 $GLOBALS['ctracker_link'] = mysql_connect($GLOBALS['ctracker_host'], $GLOBALS['ctracker_user'], $GLOBALS['ctracker_password']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
32 // Select the database
33 if (!mysql_select_db($GLOBALS['ctracker_dbname'], $GLOBALS['ctracker_link'])) {
35 crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
40 // Checks if the link is up
41 function isCrackerTrackerDatabaseLinkUp () {
42 return ((isset($GLOBALS['ctracker_link'])) && (is_resource($GLOBALS['ctracker_link'])));
45 // Database error detected
46 function crackerTrackerDatabaseError ($F, $L) {
48 if (isCrackerTrackerDebug()) {
50 print 'Function : ' . $F . '<br />';
51 print 'Line : ' . $L . '<br />';
52 print 'MySQL error : ' . mysql_error() . '<br />';
53 print 'Last SQL : '. $GLOBALS['ctracker_last_sql'] . '<br />';
56 // Currently only die here
60 // Closes a maybe open database link
61 function crackerTrackerCloseDatabaseLink () {
63 if (isCrackerTrackerDatabaseLinkUp()) {
65 if (!mysql_close($GLOBALS['ctracker_link'])) {
66 // Remove the link from global array
67 unset($GLOBALS['ctracker_link']);
70 crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
74 // Remove the link from global array
75 unset($GLOBALS['ctracker_link']);
78 // Inserts given array, if IP/check_worm combination was not found
79 function crackerTrackerInsertArray ($rowData) {
81 if (!isCrackerTrackerEntryFound($rowData)) {
82 // Insert first attempt stamp
83 $rowData['first_attempt'] = 'NOW()';
84 $rowData['count'] = '1';
87 $SQL = 'INSERT INTO `ctracker_data` (`' . implode('`,`', array_keys($rowData)) . '`) VALUES(' . implode_secure($rowData) . ')';
90 runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
92 // Only update the entry
93 updateCrackerTrackerEntry($rowData);
97 // Updates a given entry by just counting it up
98 function updateCrackerTrackerEntry ($rowData) {
99 // Construct the SELECT query
100 $SQL = 'UPDATE `ctracker_data` SET `count`=`count`+1 WHERE `remote_addr`="' . crackerTrackerEscapeString($rowData['remote_addr']) . '" AND `check_worm` = "' . crackerTrackerEscapeString($rowData['check_worm']) . '" LIMIT 1';
102 // Run the SQL and check if we have one line
103 runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
106 // Checks if an entry with IP/check_worm/domain combination is there
107 function isCrackerTrackerEntryFound ($rowData) {
108 // Construct the SELECT query
109 $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';
111 // Run the SQL and check if we have one line
112 return (mysql_num_rows(runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__)) == 1);
115 // Escapes the string
116 function crackerTrackerEscapeString ($string) {
118 if (!isCrackerTrackerDatabaseLinkUp()) {
119 // Then we cant use mysql_real_escape_string!
120 $string = addslashes($string);
121 } elseif (function_exists('mysql_real_escape_string')) {
122 // Use mysql_real_escape_string()
123 $string = mysql_real_escape_string($string, $GLOBALS['ctracker_link']);
124 } elseif (function_exists('mysql_escape_string')) {
125 // Use deprecated function
126 $string = mysql_escape_string($string, $GLOBALS['ctracker_link']);
128 // Use fall-back (bad!)
129 $string = addslashes($string);
132 // Return the secured string
136 // Runs an SQL query and checks for errors
137 function runCrackerTrackerSql ($SQL, $F, $L) {
139 if (!isCrackerTrackerDatabaseLinkUp()) {
145 $GLOBALS['ctracker_last_sql'] = $SQL;
148 $GLOBALS['ctracker_last_result'] = mysql_query($SQL, $GLOBALS['ctracker_link']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
151 return $GLOBALS['ctracker_last_result'];