]> git.mxchange.org Git - ctracker.git/blob - libs/lib_connect.php
c704fcf793072cb60098b4791b416fa627fe3873
[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 - 2011 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()) && (!empty($GLOBALS['ctracker_host'])) && (!empty($GLOBALS['ctracker_dbname'])) && (!empty($GLOBALS['ctracker_user']))) {
29                 // Then connect to the database
30                 $GLOBALS['ctracker_link'] = mysqli_connect($GLOBALS['ctracker_host'], $GLOBALS['ctracker_user'], $GLOBALS['ctracker_password'], $GLOBALS['ctracker_dbname']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
31
32                 //  Check on connection and config table
33                 if (!isCrackerTrackerDatabaseLinkUp()) {
34                         // Connect didn't work
35                         crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
36                 } elseif (isCrackerTrackerTableCreated('ctracker_config')) {
37                         // Load the config
38                         crackerTrackerLoadConfig();
39                 } // END - if
40         } else {
41                 // Init fake config
42                 crackerTrackerInitFakeConfig();
43         }
44 }
45
46 // Inits a fake configurtation
47 function crackerTrackerInitFakeConfig () {
48         // Set the array
49         $GLOBALS['ctracker_config'] = array(
50                 'ctracker_alert_user' => 'Y',
51         );
52 }
53
54 // Checks if the link is up
55 function isCrackerTrackerDatabaseLinkUp () {
56         // Is the instance at least set?
57         if (isset($GLOBALS['ctracker_link'])) {
58                 // Debug message
59                 //* DEBUG: */ error_log('isset='.intval(isset($GLOBALS['ctracker_link'])) . ',is_object=' . intval(is_object($GLOBALS['ctracker_link'])) . ',mysqli_connect_errno=' . mysqli_connect_errno());
60         } else {
61                 // Not set!
62                 //* DEBUG: */ error_log('ctracker_link not set.');
63         }
64
65         return ((isset($GLOBALS['ctracker_link'])) && (is_object($GLOBALS['ctracker_link'])) && (mysqli_connect_errno() == 0));
66 }
67
68 // Database error detected
69 function crackerTrackerDatabaseError ($F, $L) {
70         // Should we debug?
71         if (isCrackerTrackerDebug()) {
72                 // Output error
73                 print 'Function    : ' . $F . '<br />';
74                 print 'Line        : ' . $L . '<br />';
75                 if (isset($GLOBALS['ctracker_link'])) {
76                         print 'MySQL error : ' . mysqli_error($GLOBALS['ctracker_link']) . '<br />';
77                 } else {
78                         print 'No MySQLi available.<br />';
79                 }
80                 print 'Last SQL    : '. $GLOBALS['ctracker_last_sql'] . '<br />';
81         } // END - if
82
83         // Currently only die here
84         crackerTrackerDie();
85 }
86
87 // Closes a maybe open database link
88 function crackerTrackerCloseDatabaseLink () {
89         // Is the link up?
90         if (isCrackerTrackerDatabaseLinkUp()) {
91                 // Did it work?
92                 if (!mysqli_close($GLOBALS['ctracker_link'])) {
93                         // Attempt has failed
94                         crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
95                 } // END - if
96         } // END - if
97 }
98
99 // Inserts given array, if IP/check_worm combination was not found
100 function crackerTrackerInsertArray ($table, $rowData) {
101         // Is there a link up?
102         if (!isCrackerTrackerDatabaseLinkUp()) {
103                 // Abort silently here
104                 return FALSE;
105         } // END - if
106
107         // Is it found?
108         if (!isCrackerTrackerEntryFound($rowData)) {
109                 // Prepare SQL
110                 $SQL = 'INSERT INTO `' . $table . '` (`' . implode('`,`', array_keys($rowData)) . '`) VALUES(' . implode_secure($rowData) . ')';
111
112                 // Reset insert id
113                 $GLOBALS['ctracker_last_insert_id'] = FALSE;
114
115                 // Run it
116                 runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
117
118                 // Remember the last insert id
119                 $GLOBALS['ctracker_last_insert_id'] = mysqli_insert_id($GLOBALS['ctracker_link']) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
120         } else {
121                 // Only update the entry
122                 updateCrackerTrackerEntry($rowData);
123         }
124 }
125
126 // Updates a given entry by just counting it up
127 function updateCrackerTrackerEntry ($rowData) {
128         // Construct the SELECT query
129         $SQL = 'UPDATE `ctracker_data` SET `count`=`count`+1 WHERE (`remote_addr`="' . crackerTrackerEscapeString($rowData['remote_addr']) . '" OR `proxy_addr`="' . crackerTrackerEscapeString($rowData['proxy_addr']) . '") AND `check_worm` = "' . crackerTrackerEscapeString($rowData['check_worm']) . '" LIMIT 1';
130
131         // Run the SQL and check if we have one line
132         runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__);
133 }
134
135 // Checks if an entry with IP/check_worm/domain combination is there
136 function isCrackerTrackerEntryFound ($rowData) {
137         // Construct the SELECT query
138         $SQL = 'SELECT `id` FROM `ctracker_data` WHERE (`remote_addr`="' . crackerTrackerEscapeString($rowData['remote_addr']) . '" OR `proxy_addr`="' . crackerTrackerEscapeString($rowData['proxy_addr']) . '") AND `check_worm` = "' . crackerTrackerEscapeString($rowData['check_worm']) . '" AND `server_name`="' . crackerTrackerEscapeString($rowData['server_name']) . '" LIMIT 1';
139
140         // Run the SQL and check if we have one line
141         return ((isCrackerTrackerDatabaseLinkUp()) && (mysqli_num_rows(runCrackerTrackerSql($SQL, __FUNCTION__, __LINE__)) == 1));
142 }
143
144 // Escapes the string
145 function crackerTrackerEscapeString ($string) {
146         // Is the link up?
147         if (!isCrackerTrackerDatabaseLinkUp()) {
148                 // Then we cant use mysqli_real_escape_string!
149                 $string = addslashes($string);
150         } elseif (function_exists('mysqli_real_escape_string')) {
151                 // Use mysqli_real_escape_string()
152                 $string = mysqli_real_escape_string($GLOBALS['ctracker_link'], $string);
153         } else {
154                 // Use fall-back (bad!)
155                 $string = addslashes($string);
156         }
157
158         // Return the secured string
159         return $string;
160 } // END - if
161
162 // Runs an SQL query and checks for errors
163 function runCrackerTrackerSql ($SQL, $F, $L) {
164         // Is the link up?
165         if (!isCrackerTrackerDatabaseLinkUp()) {
166                 // Abort here
167                 crackerTrackerDie();
168         } // END - if
169
170         // Remember last SQL
171         $GLOBALS['ctracker_last_sql'] = $SQL;
172
173         // Run the query
174         $GLOBALS['ctracker_last_result'] = mysqli_query($GLOBALS['ctracker_link'], $SQL) or crackerTrackerDatabaseError(__FUNCTION__, __LINE__);
175
176         // And return it
177         return $GLOBALS['ctracker_last_result'];
178 }
179
180 // Checks wether a table was found
181 function isCrackerTrackerTableCreated ($table) {
182         // Default is not found
183         $found = FALSE;
184
185         // Run the query
186         $result = runCrackerTrackerSql('SHOW TABLES', __FUNCTION__, __LINE__);
187
188         // Is our table there?
189         while (list($tab) = mysqli_fetch_row($result)) {
190                 // Debug message
191                 //* NOISY-DEBUG: */ error_log('tab=' . $tab);
192
193                 // Is the table there?
194                 if ($tab == $table) {
195                         // Okay, found. So abort
196                         $found = TRUE;
197                         break;
198                 } // END - if
199         } // END - if
200
201         // Free result
202         freeCrackerTrackerResult($result);
203
204         // Return result
205         return $found;
206 }
207
208 // Creates the given table with columns
209 function crackerTrackerCreateTable ($table, array $columns, array $keys) {
210         // Begin the SQL
211         $SQL = 'CREATE TABLE IF NOT EXISTS `' . $table . '` (';
212
213         // Add table name as first column
214         $SQL .= '`' . $table . '` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, ';
215
216         // Add all columns
217         foreach ($columns as $column=>$type) {
218                 // Add this entry
219                 $SQL .= '`' . $column . '` ' . $type . ', ';
220         } // END - foreach
221
222         // Add table name as primary key
223         $SQL .= 'PRIMARY KEY (`' . $table . '`), ';
224
225         // Add keys
226         foreach ($keys as $key=>$type) {
227                 // Add this entry
228                 $SQL .= '' . $type . ' (`' . $key . '`), ';
229         } // END - foreach
230
231         // Finish SQL
232         $SQL = substr($SQL, 0, -2) . ') TYPE=InnoDB';
233
234         // And run it
235         runCrackerTrackerSql($SQL);
236 }
237
238 // Inits a table by inserting 
239 function crackerTrackerInitTable ($table) {
240         // Prepare SQL and run it
241         runCrackerTrackerSql('INSERT INTO `' . $table . '` (`' . $table . '`) VALUES (NULL)');
242 }
243
244 // Updates the database scheme automatically
245 function crackerTrackerUpdateDatabaseScheme () {
246         // Is a link there?
247         if (!isCrackerTrackerDatabaseLinkUp()) {
248                 // Abort here silently
249                 return;
250         } // END - if
251
252         // Is the main config table there?
253         if (!isCrackerTrackerTableCreated('ctracker_config')) {
254                 // Then do it for us
255                 crackerTrackerCreateTable('ctracker_config', array(
256                         'ctracker_db_version' => 'BIGINT ( 20 ) UNSIGNED NOT NULL DEFAULT 0',
257                         'ctracker_min_sleep'  => 'SMALLINT ( 5 ) UNSIGNED NOT NULL DEFAULT 10',
258                         'ctracker_max_sleep'  => 'SMALLINT ( 5 ) UNSIGNED NOT NULL DEFAULT 30',
259                         'ctracker_alert_user' => "ENUM('Y','N') NOT NULL DEFAULT 'Y'",
260                         'ctracker_language'   => "CHAR ( 2) NOT NULL DEFAULT 'en'"
261                 ), array());
262
263                 // Init that table
264                 crackerTrackerInitTable('ctracker_config');
265         } // END - if
266
267         // Init update array here
268         crackerTrackerInitUpdates();
269
270         // Run any SQL updates recursively
271         while (isset($GLOBALS['ctracker_updates'][getCrackerTrackerConfig('ctracker_db_version')])) {
272                 // Run that updates
273                 runCrackerTrackerUpdates(getCrackerTrackerConfig('ctracker_db_version'));
274
275                 // Update config
276                 runCrackerTrackerSql('UPDATE `ctracker_config` SET `ctracker_db_version`=`ctracker_db_version`+1 WHERE `ctracker_config`=1 LIMIT 1', __FUNCTION__, __LINE__);
277
278                 // And count it up in the config array
279                 $GLOBALS['ctracker_config']['ctracker_db_version']++;
280         } // END - if
281 }
282
283 // Load the configuration
284 function crackerTrackerLoadConfig () {
285         // Construct SQL command and run it
286         $result = runCrackerTrackerSql('SELECT * FROM `ctracker_config` WHERE `ctracker_config`=1 LIMIT 1', __FUNCTION__, __LINE__);
287
288         // And get it
289         $GLOBALS['ctracker_config'] = mysqli_fetch_array($result);
290
291         // Free result
292         freeCrackerTrackerResult($result);
293 }
294
295 // Getter for config
296 function getCrackerTrackerConfig ($entry) {
297         // Is the config entry there?
298         if (!isset($GLOBALS['ctracker_config'][$entry])) {
299                 // Then better die here, else we may have an endless loop
300                 if (isCrackerTrackerDebug()) {
301                         // Nicer message in debug mode
302                         die('Configuration entry ' . $entry . ' missing!');
303                 } else {
304                         // die() on production systems
305                         die();
306                 }
307         } // END - if
308
309         // Return it
310         return $GLOBALS['ctracker_config'][$entry];
311 }
312
313 // Did the current IP already generated blocked attempts?
314 function isCrackerTrackerIpSuspicious () {
315         // Skip this silently if we have not config
316         if (!isCrackerTrackerDatabaseLinkUp()) {
317                 // Skip this step silently, all is not suspicious
318                 return FALSE;
319         } // END - if
320
321         // Check if an entry is there
322         $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__);
323
324         // Get row count
325         list($rows) = mysqli_fetch_row($result);
326
327         // Is there one entry?
328         $found = ($rows > 0);
329
330         // And again?
331         if ($found === TRUE) {
332                 // Yes, one is found, then load it
333                 $result = runCrackerTrackerSql("SELECT SQL_SMALL_RESULT * FROM `ctracker_data` USE INDEX (`remote_proxy_last`) WHERE `remote_addr`='" . determineCrackerTrackerRealRemoteAddress() . "' OR `proxy_addr`='" . getenv('REMOTE_ADDR') . "' ORDER BY `last_attempt` DESC LIMIT 1", __FUNCTION__, __LINE__);
334
335                 // Cache the entry
336                 $GLOBALS['ctracker_last_suspicious_entry'] = mysqli_fetch_array($result);
337         } // END - if
338
339         // Free result
340         freeCrackerTrackerResult($result);
341
342         // Return the result
343         return $found;
344 }
345
346 // Does the current IP have a ticket?
347 function ifCrackerTrackerIpHasTicket () {
348         // We only give one ticket per IP!
349         $result = runCrackerTrackerSql("SELECT * FROM `ctracker_ticket` WHERE `ctracker_ticket_remote_addr`='" . determineCrackerTrackerRealRemoteAddress() . "' OR `ctracker_ticket_proxy_addr`='" . getenv('REMOTE_ADDR') . "' LIMIT 1", __FUNCTION__, __LINE__);
350
351         // Do we have a ticket?
352         $found = (mysqli_num_rows($result) == 1);
353
354         // And again?
355         if ($found === TRUE) {
356                 // Cache the ticket data
357                 $GLOBALS['ctracker_last_ticket'] = mysqli_fetch_array($result);
358         } // END - if
359
360         // Free result
361         freeCrackerTrackerResult($result);
362
363         // Return the result
364         return $found;
365 }
366
367 // Adds a ticket based on given (mostly $_POST) data
368 function addCrackerTrackerTicket (array $data) {
369         // Prepare the array
370         $GLOBALS['ctracker_last_ticket'] = array(
371                 'ctracker_ticket_remote_addr' => determineCrackerTrackerRealRemoteAddress(),
372                 'ctracker_ticket_proxy_addr'  => getenv('REMOTE_ADDR'),
373                 'ctracker_ticket_user_agent'  => crackerTrackerUserAgent(),
374                 'ctracker_ticket_name'        => crackerTrackerSecureString($data['name']),
375                 'ctracker_ticket_email'       => crackerTrackerSecureString($data['email']),
376                 'ctracker_ticket_comment'     => crackerTrackerSecureString($data['comment'])
377         );
378
379         // Insert it
380         crackerTrackerInsertArray('ctracker_ticket', $GLOBALS['ctracker_last_ticket']);
381
382         // Is there an entry?
383         if ((isset($GLOBALS['ctracker_last_insert_id'])) && ($GLOBALS['ctracker_last_insert_id'] > 0)) {
384                 // All fine, so prepare the link between ticket<->data
385                 $data = array(
386                         'ctracker_ticket_id' => $GLOBALS['ctracker_last_insert_id'],
387                         'ctracker_data_id'   => $GLOBALS['ctracker_last_suspicious_entry']['id']
388                 );
389
390                 // And insert it as well
391                 crackerTrackerInsertArray('ctracker_ticket_data', $data);
392
393                 // Add ticket id again
394                 $GLOBALS['ctracker_ticket'] = $data['ctracker_ticket_id'];
395
396                 // Merge all data for emails
397                 $GLOBALS['ctracker_last_ticket'] = array_merge($GLOBALS['ctracker_last_ticket'], $data);
398
399                 // Is this also there?
400                 if ((isset($GLOBALS['ctracker_last_insert_id'])) && ($GLOBALS['ctracker_last_insert_id'] > 0)) {
401                         // All fine, so display "thank you page"
402                         crackerTrackerLoadTemplate('add_ticket_thanks');
403                 } else {
404                         // Did not insert
405                         crackerTrackerDie();
406                 }
407         } else {
408                 // Did not insert
409                 crackerTrackerDie();
410         }
411 }
412
413 // Frees given result instance
414 function freeCrackerTrackerResult (mysqli_result $result) {
415         // Free result
416         $result->free();
417 }
418
419 // [EOF]
420 ?>