]> git.mxchange.org Git - ctracker.git/blob - libs/lib_connect.php
Some nice improvements:
[ctracker.git] / libs / lib_connect.php
1 <?php
2 /**
3  * Database connection library
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             3.0.0
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
10  *
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.
15  *
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.
20  *
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/>.
23  */
24
25 // Function to aquire a database link
26 function aquireCrackerTrackerDatabaseLink () {
27         // Is the link up?
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__);
31
32                 // Select the database
33                 if (!mysql_select_db($GLOBALS['ctracker_dbname'], $GLOBALS['ctracker_link'])) {
34                         // Attempt has failed
35                         crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
36                 } // END - if
37         } // END - if
38 }
39
40 // Checks if the link is up
41 function isCrackerTrackerDatabaseLinkUp () {
42         return ((isset($GLOBALS['ctracker_link'])) && (is_resource($GLOBALS['ctracker_link'])));
43 }
44
45 // Database error detected
46 function crackerTrackerDatabaseError ($F, $L) {
47         // Should we debug?
48         if (isCrackerTrackerDebug()) {
49                 // Output error
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 />';
54         } // END - if
55
56         // Currently only die here
57         crackerTrackerDie();
58 }
59
60 // Closes a maybe open database link
61 function crackerTrackerCloseDatabaseLink () {
62         // Is the link up?
63         if (isCrackerTrackerDatabaseLinkUp()) {
64                 // Did it work?
65                 if (!mysql_close($GLOBALS['ctracker_link'])) {
66                         // Remove the link from global array
67                         unset($GLOBALS['ctracker_link']);
68
69                         // Attempt has failed
70                         crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
71                 } // END - if
72         } // END - if
73
74         // Remove the link from global array
75         unset($GLOBALS['ctracker_link']);
76 }
77
78 // Inserts given array, if IP/check_worm combination was not found
79 function crackerTrackerInsertArray ($rowData) {
80         // Is it found?
81         if (!isCrackerTrackerEntryFound($rowData)) {
82                 // Insert first attempt stamp
83                 $rowData['first_attempt'] = 'NOW()';
84                 $rowData['count'] = '1';
85
86                 // Prepare SQL
87                 $SQL = 'INSERT INTO `ctracker_data` (`' . implode('`,`', array_keys($rowData)) . '`) VALUES(' . implode_secure($rowData) . ')';
88
89                 // Run it
90                 runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
91         } else {
92                 // Only update the entry
93                 updateCrackerTrackerEntry($rowData);
94         }
95 }
96
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';
101
102         // Run the SQL and check if we have one line
103         runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
104 }
105
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';
110
111         // Run the SQL and check if we have one line
112         return (mysql_num_rows(runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__)) == 1);
113 }
114
115 // Escapes the string
116 function crackerTrackerEscapeString ($string) {
117         // Is the link up?
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']);
127         } else {
128                 // Use fall-back (bad!)
129                 $string = addslashes($string);
130         }
131
132         // Return the secured string
133         return $string;
134 } // END - if
135
136 // Runs an SQL query and checks for errors
137 function runCrackerTrackerSql ($SQL, $F, $L) {
138         // Is the link up?
139         if (!isCrackerTrackerDatabaseLinkUp()) {
140                 // Abort here
141                 crackerTrackerDie();
142         } // END - if
143
144         // Remember last SQL
145         $GLOBALS['ctracker_last_sql'] = $SQL;
146
147         // Run the query
148         $GLOBALS['ctracker_last_result'] = mysql_query($SQL, $GLOBALS['ctracker_link']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
149
150         // And return it
151         return $GLOBALS['ctracker_last_result'];
152 }
153
154 // [EOF]
155 ?>