]> git.mxchange.org Git - ctracker.git/blob - libs/lib_general.php
Don't continue if the cookie has been set + ticket has created. 'unknown' was found...
[ctracker.git] / libs / lib_general.php
1 <?php
2 /**
3  * General functions 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 if (!function_exists('implode_r')) {
26         // Implode recursive a multi-dimension array, taken from www.php.net
27         function implode_r ($glue, $array, $array_name = NULL) {
28                 $return = array();
29                 while(list($key,$value) = @each($array)) {
30                         if(is_array($value)) {
31                                 // Is an array again, so call recursive
32                                 $return[] = implode_r($glue, $value, (string) $key);
33                         } else {
34                                 if($array_name != NULL) {
35                                         $return[] = $array_name . '[' . (string) $key . ']=' . $value . "\n";
36                                 } else {
37                                         $return[] = $key . '=' . $value."\n";
38                                 }
39                         }
40                 } // END - while
41
42                 // Return resulting array
43                 return(implode($glue, $return));
44         } // END - function
45 } // END - if
46
47 if (!function_exists('implode_secure')) {
48         // Implode a simple array with a 'call-back' to our escaper function
49         function implode_secure ($array) {
50                 // Return string
51                 $return = '';
52
53                 // Implode all data
54                 foreach ($array as $entry) {
55                         // Don't escape some
56                         if (in_array($entry, array('NOW()'))) {
57                                 // Add it with non-string glue
58                                 $return .= $entry . ',';
59                         } elseif (empty($entry)) {
60                                 // Empty strings need no escaping
61                                 $return .= '"",';
62                         } else {
63                                 // Secure this string and add it
64                                 $return .= '"' . crackerTrackerEscapeString($entry) . '",';
65                         }
66                 } // END - foreach
67
68                 // Remove last char
69                 $return = substr($return, 0, -1);
70
71                 // Return this string
72                 return $return;
73         } // END - function
74 } // END - if
75
76 // Getter for ctracker_debug_enabled
77 function isCrackerTrackerDebug () {
78         // Is it set?
79         return ((isset($GLOBALS['ctracker_debug_enabled'])) && ($GLOBALS['ctracker_debug_enabled'] === TRUE));
80 }
81
82 // Determines the real remote address
83 function determineCrackerTrackerRealRemoteAddress () {
84         // Initial value
85         $address = '0.0.0.0';
86
87         // Is a proxy in use?
88         if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
89                 // Proxy was used
90                 $address = trim($_SERVER['HTTP_X_FORWARDED_FOR']);
91         } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
92                 // Yet, another proxy
93                 $address = trim($_SERVER['HTTP_CLIENT_IP']);
94         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
95                 // The regular address when no proxy was used
96                 $address = trim(getenv('REMOTE_ADDR'));
97         }
98
99         if ($address == 'unknown') {
100                 // Invalid IP somehow given
101                 $address = '0.0.0.0';
102         elseif (strstr($address, ',')) {
103                 // This strips out the real address from proxy output
104                 $addressArray = explode(',', $address);
105                 $address = $addressArray[0];
106         }
107
108         // Return the result
109         return $address;
110 }
111
112 // Determine if a proxy was used
113 function isCrackerTrackerProxyUsed () {
114         // Check if specific entries are set
115         $proxyUsed = ((isset($_SERVER['HTTP_X_FORWARDED_FOR'])) || (isset($_SERVER['HTTP_CLIENT_IP'])));
116
117         // Return result
118         return $proxyUsed;
119 }
120
121 // Detects the user-agent string
122 function crackerTrackerUserAgent () {
123         // Default is 'unknown'
124         $ua = 'unknown';
125
126         // Is the entry there?
127         if (isset($_SERVER['HTTP_USER_AGENT'])) {
128                 // Then use it securely
129                 $ua = crackerTrackerSecureString($_SERVER['HTTP_USER_AGENT']);
130         } // END - if
131
132         // Return it
133         return $ua;
134 }
135
136 // Detects the script name
137 function crackerTrackerScriptName () {
138         // Is it there?
139         if (!isset($_SERVER['SCRIPT_NAME'])) {
140                 // Return NULL
141                 return NULL;
142         } // END - if
143
144         // Should always be there!
145         return crackerTrackerSecureString($_SERVER['SCRIPT_NAME']);
146 }
147
148 // Detects the query string
149 function crackerTrackerQueryString () {
150         // Is it there?
151         if (!isset($_SERVER['QUERY_STRING'])) {
152                 // Return NULL
153                 return NULL;
154         } // END - if
155
156         // Should always be there!
157         return crackerTrackerEscapeString(urldecode($_SERVER['QUERY_STRING']));
158 }
159
160 // Detects the server's name
161 function crackerTrackerServerName () {
162         // Is it there?
163         if (!isset($_SERVER['SERVER_NAME'])) {
164                 // Return NULL
165                 return NULL;
166         } // END - if
167
168         // Should always be there!
169         return crackerTrackerSecureString($_SERVER['SERVER_NAME']);
170 }
171
172 // Detects the referer
173 function crackerTrackerReferer () {
174         // Default is a dash
175         $referer = '-';
176
177         // Is it there?
178         if (isset($_SERVER['HTTP_REFERER'])) {
179                 // Then use it securely
180                 $referer = crackerTrackerSecureString(urldecode($_SERVER['HTTP_REFERER']));
181         } // END - if
182
183         // Return it
184         return $referer;
185 }
186
187 // Detects the scripts path
188 function crackerTrackerScriptPath () {
189         // Should always be there!
190         $path = dirname(crackerTrackerScriptName()) . '/';
191
192         // Return detected path
193         return $path;
194 }
195
196 // Detects wether we have SSL
197 function crackerTrackerSecured () {
198         // Detect it
199         $ssl = ((isset($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off'));
200
201         // Return result
202         return $ssl;
203 }
204
205 // Secures a string by escaping it and passing it through strip_tags,htmlentities-chain
206 function crackerTrackerSecureString ($str) {
207         // First escape it
208         $str = crackerTrackerEscapeString($str);
209
210         // Then pass it through the functions
211         $str = htmlentities(strip_tags($str), ENT_QUOTES);
212
213         // Return it
214         return $str;
215 }
216
217 // Is the file there and readable?
218 function isCrackerTrackerFileFound ($FQFN) {
219         // Simply check it
220         return ((file_exists($FQFN)) && (is_readable($FQFN)));
221 }
222
223 // Loads a given "template" (this is more an include file)
224 function crackerTrackerLoadTemplate ($template) {
225         // Create the full-qualified filename (FQFN)
226         $FQFN = sprintf('%s/templates/%s.tpl.php',
227                 dirname(__FILE__),
228                 $template
229         );
230
231         // Is this template found?
232         if (isCrackerTrackerFileFound($FQFN)) {
233                 // Detect language
234                 crackerTrackerLanguage();
235
236                 // Load it
237                 require_once($FQFN);
238         } else {
239                 // Not found, so die here
240                 crackerTrackerDie();
241         }
242 }
243
244 // Loads a given "template" (this is more an include file)
245 function crackerTrackerLoadLocalizedTemplate ($template) {
246         // Create the full-qualified filename (FQFN)
247         $FQFN = sprintf('%s/templates/%s/%s.tpl.php',
248                 dirname(__FILE__),
249                 getCrackerTrackerLanguage(),
250                 $template
251         );
252
253         // Is this template found?
254         if (isCrackerTrackerFileFound($FQFN)) {
255                 // Load it
256                 require_once($FQFN);
257         } else {
258                 // Not found, so die here
259                 crackerTrackerDie();
260         }
261 }
262
263 // Detects the browser's language file and tries to load it, fall-back on english!
264 function crackerTrackerLanguage () {
265         // Default is English
266         $GLOBALS['ctracker_language'] = 'en';
267         $weight = 1;
268
269         // Is the language string there?
270         if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
271                 // Then detect it
272                 foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
273                         // Split it again for q=x.x value
274                         $langArray = explode(';', $lang);
275
276                         // If there is an entry, we have a weight
277                         if ((isset($langArray[1])) && ($langArray[1] > $weight)) {
278                                 // Use this language/weight instead
279                                 $GLOBALS['ctracker_language'] = $langArray[0];
280                                 $weight   = $langArray[1];
281                         } // END - if
282                 } // END - foreach
283         } // END - if
284
285         // Construct FQFN
286         $FQFN = sprintf('%s/language/%s.php',
287                 dirname(__FILE__),
288                 getCrackerTrackerLanguage()
289         );
290
291         // Is it not there, switch to "en"
292         if ((getCrackerTrackerLanguage() != 'en') && (!isCrackerTrackerFileFound($FQFN))) {
293                 // English is default!
294                 $GLOBALS['ctracker_language'] = 'en';
295
296                 // Construct FQFN again
297                 $FQFN = sprintf('%s/language/en.php', dirname(__FILE__));
298         } // END - if
299
300         // Load the language file
301         require($FQFN);
302 }
303
304 // Loads a given email template and passes through $content
305 function crackerTrackerLoadEmailTemplate ($template, array $content = array(), $language = NULL) {
306         // Init language
307         crackerTrackerLanguage();
308
309         // Generate the FQFN
310         $FQFN = sprintf('%s/mails/%s/%s.tpl',
311                 dirname(__FILE__),
312                 getCrackerTrackerLanguage($language),
313                 $template
314         );
315
316         // So is the file there?
317         if (isCrackerTrackerFileFound($FQFN)) {
318                 // Init result
319                 $result = 'No result from template ' . $template . '. Please report this at http://forum.ship-simu.org Thank you.';
320
321                 // Then load it
322                 eval('$result = "' . crackerTrackerCompileCode(file_get_contents($FQFN)) . '";');
323
324                 // Return the result
325                 return $result;
326         } else {
327                 // Not found
328                 crackerTrackerDie();
329         }
330 }
331
332 // Getter for message
333 function getCrackerTrackerLocalized ($message) {
334         // Default message
335         $output = '!' . $message . '!';
336
337         // Is the language string there?
338         if (isset($GLOBALS['ctracker_localized'][$message])) {
339                 // Use this instead
340                 $output = $GLOBALS['ctracker_localized'][$message];
341         } // END - if
342
343         // Return it
344         return $output;
345 }
346
347 // Tries to find a message and outputs it
348 function crackerTrackerOutputLocalized ($message) {
349         // Output it
350         print getCrackerTrackerLocalized($message);
351 }
352
353 // Compiles the given code
354 function crackerTrackerCompileCode ($code) {
355         // Find all $content[foo]
356         preg_match_all('/\$(content|GLOBALS)((\[([a-zA-Z0-9-_]+)\])*)/', $code, $matches);
357
358         // Replace " with {QUOTE}
359         $code = str_replace('"', '{QUOTE}', $code);
360
361         // Replace all
362         foreach ($matches[0] as $key=>$match) {
363                 // Replace it
364                 if (substr($match, 0, 8) == '$GLOBALS') {
365                         // $GLOBALS
366                         $code = str_replace($match, "\" . \$GLOBALS['" . $matches[4][$key] . "'] . \"", $code);
367                 } elseif (substr($match, 0, 8) == '$content') {
368                         // $content
369                         $code = str_replace($match, "\" . \$content['" . $matches[4][$key] . "'] . \"", $code);
370                 }
371         } // END - foreach
372
373         // Return it
374         return $code;
375 }
376
377 // "Getter" for language
378 function getCrackerTrackerLanguage ($lang = NULL) {
379         // Default is from browser
380         $language = $GLOBALS['ctracker_language'];
381
382         // Is $lang set?
383         if (!is_null($lang)) {
384                 // Then use this instead
385                 $language = $lang;
386         } // END - if
387
388         // Return it
389         return $language;
390 }
391
392 // "Getter" for ticket id
393 function getCrackerTrackerTicketId () {
394         // Default is zero
395         $id = 0;
396
397         // Is it set?
398         if (isset($GLOBALS['ctracker_last_ticket']['ctracker_ticket'])) {
399                 // Then use it
400                 $id = $GLOBALS['ctracker_last_ticket']['ctracker_ticket'];
401         } // END - if
402
403         // Return the number
404         return $id;
405 }
406
407 // Sends a cookie to the user that he would not see this security warning again
408 function sendCrackerTrackerCookie () {
409         // Set the cookie
410         // @TODO Why can't domain be set to value from crackerTrackerServerName() ?
411         setcookie('ctracker_ticket', getCrackerTrackerTicketId(), (time() + 60*60*24), '/', '', crackerTrackerSecured(), TRUE);
412         $_COOKIE['ctracker_ticket'] = getCrackerTrackerTicketId();
413 }
414
415 // Is the cookie set?
416 function ifCrackerTrackerCookieIsSet () {
417         // Is it set and valid?
418         return ((isset($_COOKIE['ctracker_ticket'])) && ($_COOKIE['ctracker_ticket'] > 0));
419 }
420
421 // Redirects to the same URL
422 function crackerTrackerRedirectSameUrl () {
423         // Construct the url
424         $url = '://' . crackerTrackerServerName() . crackerTrackerScriptName() . '?' . crackerTrackerQueryString();
425
426         // Do we have SSL?
427         if (crackerTrackerSecured()) {
428                 // HTTPS
429                 $url = 'https' . $url;
430         } else {
431                 // HTTP
432                 $url = 'http' . $url;
433         }
434
435         // And redirect
436         crackerTrackerSendRawRedirect($url);
437 }
438
439 /**
440  * Send a HTTP redirect to the browser. This function was taken from DokuWiki
441  * (GNU GPL 2; http://www.dokuwiki.org) and modified to fit into this script.
442  *
443  * Works arround Microsoft IIS cookie sending bug. Does exit the script.
444  *
445  * @link    http://support.microsoft.com/kb/q176113/
446  * @author  Andreas Gohr <andi@splitbrain.org>
447  * @access  private
448  */
449 function crackerTrackerSendRawRedirect ($url) {
450         // Better remove any data by ctracker
451         unsetCtrackerData();
452
453         // always close the session
454         session_write_close();
455
456         // Revert entity &amp;
457         $url = str_replace('&amp;', '&', $url);
458
459         // check if running on IIS < 6 with CGI-PHP
460         if ((isset($_SERVER['SERVER_SOFTWARE'])) && (isset($_SERVER['GATEWAY_INTERFACE'])) &&
461                 (strpos($_SERVER['GATEWAY_INTERFACE'],'CGI') !== FALSE) &&
462                 (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) &&
463                 ($matches[1] < 6)) {
464                 // Send the IIS header
465                 header('Refresh: 0;url=' . $url);
466         } else {
467                 // Send generic header
468                 header('Location: ' . $url);
469         }
470         exit();
471 }
472
473 // Removes all ctracker-related data from global space
474 function unsetCtrackerData () {
475         // Unset all ctracker data
476         foreach (array(
477                         'ctracker_host',
478                         'ctracker_dbname',
479                         'ctracker_user',
480                         'ctracker_password',
481                         'ctracker_debug_enabled',
482                         'ctracker_email',
483                         'ctracker_whitelist',
484                         'ctracker_get_blacklist',
485                         'ctracker_post_blacklist',
486                         'ctracker_header',
487                         'ctracker_post_track',
488                         'ctracker_checkworm',
489                         'ctracker_check_post',
490                         'ctracker_last_sql',
491                         'ctracker_last_result',
492                         'ctracker_config',
493                         'ctracker_updates',
494                         'ctracker_language',
495                         'ctracker_localized',
496                         'ctracker_link',
497                 ) as $key) {
498                         // Unset it
499                         unset($GLOBALS[$key]);
500         } // END - foreach
501 }
502
503 // [EOF]
504 ?>