Fixes for member registration (IP lock) and rewrites for remote IP address
[mailer.git] / inc / wrapper-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 04/04/2009 *
4  * ===================                          Last change: 04/04/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : wrapper-functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Wrapper functions                                *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Wrapper-Funktionen                               *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Read a given file
46 function readFromFile ($FQFN) {
47         // Sanity-check if file is there (should be there, but just to make it sure)
48         if (!isFileReadable($FQFN)) {
49                 // This should not happen
50                 debug_report_bug(__FUNCTION__.': File ' . basename($FQFN) . ' is not readable!');
51         } // END - if
52
53         // Is it cached?
54         if (!isset($GLOBALS['file_content'][$FQFN])) {
55                 // Load the file
56                 if (function_exists('file_get_contents')) {
57                         // Use new function
58                         $GLOBALS['file_content'][$FQFN] = file_get_contents($FQFN);
59                 } else {
60                         // Fall-back to implode-file chain
61                         $GLOBALS['file_content'][$FQFN] = implode('', file($FQFN));
62                 }
63         } // END - if
64
65         // Return the content
66         return $GLOBALS['file_content'][$FQFN];
67 }
68
69 // Writes content to a file
70 function writeToFile ($FQFN, $content, $aquireLock = false) {
71         // Is the file writeable?
72         if ((isFileReadable($FQFN)) && (!is_writeable($FQFN)) && (!changeMode($FQFN, 0644))) {
73                 // Not writeable!
74                 logDebugMessage(__FUNCTION__, __LINE__, sprintf("File %s not writeable.", basename($FQFN)));
75
76                 // Failed! :(
77                 return false;
78         } // END - if
79
80         // By default all is failed...
81         $return = false;
82
83         // Is the function there?
84         if (function_exists('file_put_contents')) {
85                 // With lock?
86                 if ($aquireLock === true) {
87                         // Write it directly with lock
88                         $return = file_put_contents($FQFN, $content, LOCK_EX);
89                 } else {
90                         // Write it directly
91                         $return = file_put_contents($FQFN, $content);
92                 }
93         } else {
94                 // Write it with fopen
95                 $fp = fopen($FQFN, 'w') or app_die(__FUNCTION__, __LINE__, "Cannot write file ".basename($FQFN).'!');
96
97                 // Aquire lock
98                 if ($aquireLock === true) flock($fp, LOCK_EX);
99
100                 // Write content
101                 fwrite($fp, $content);
102
103                 // Close stream
104                 fclose($fp);
105         }
106
107         // Mark it as readable
108         $GLOBALS['file_readable'][$FQFN] = true;
109
110         // Remember content in cache
111         $GLOBALS['file_content'][$FQFN] = $content;
112
113         // Return status
114         return changeMode($FQFN, 0644);
115 }
116
117 // Clears the output buffer. This function does *NOT* backup sent content.
118 function clearOutputBuffer () {
119         // Trigger an error on failure
120         if (!ob_end_clean()) {
121                 // Failed!
122                 debug_report_bug(__FUNCTION__.': Failed to clean output buffer.');
123         } // END - if
124 }
125
126 // Encode strings
127 // @TODO Implement $compress
128 function encodeString ($str, $compress = true) {
129         $str = urlencode(base64_encode(compileUriCode($str)));
130         return $str;
131 }
132
133 // Decode strings encoded with encodeString()
134 // @TODO Implement $decompress
135 function decodeString ($str, $decompress = true) {
136         $str = compileUriCode(base64_decode(urldecode(compileUriCode($str))));
137         return $str;
138 }
139
140 // Decode entities in a nicer way
141 function decodeEntities ($str, $quote = ENT_NOQUOTES) {
142         // Decode the entities to UTF-8 now
143         $decodedString = html_entity_decode($str, $quote, 'UTF-8');
144
145         // Return decoded string
146         return $decodedString;
147 }
148
149 // Merges an array together but only if both are arrays
150 function merge_array ($array1, $array2) {
151         // Are both an array?
152         if ((!is_array($array1)) && (!is_array($array2))) {
153                 // Both are not arrays
154                 debug_report_bug(__FUNCTION__ . ': No arrays provided!');
155         } elseif (!is_array($array1)) {
156                 // Left one is not an array
157                 debug_report_bug(sprintf("[%s:%s] array1 is not an array. array != %s", __FUNCTION__, __LINE__, gettype($array1)));
158         } elseif (!is_array($array2)) {
159                 // Right one is not an array
160                 debug_report_bug(sprintf("[%s:%s] array2 is not an array. array != %s", __FUNCTION__, __LINE__, gettype($array2)));
161         }
162
163         // Merge all together
164         return array_merge($array1, $array2);
165 }
166
167 // Check if given FQFN is a readable file
168 function isFileReadable ($FQFN) {
169         // Do we have cache?
170         if (!isset($GLOBALS['file_readable'][$FQFN])) {
171                 // Check all...
172                 $GLOBALS['file_readable'][$FQFN] = ((file_exists($FQFN)) && (is_file($FQFN)) && (is_readable($FQFN)));
173
174                 // Debug message
175                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . basename($FQFN) . ' - CHECK! (' . intval($GLOBALS['file_readable'][$FQFN]) . ')');
176         } else {
177                 // Cache used
178                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . basename($FQFN) . ' - CACHE! (' . intval($GLOBALS['file_readable'][$FQFN]) . ')');
179         }
180
181         // Return result
182         return $GLOBALS['file_readable'][$FQFN];
183 }
184
185 // Checks wether the given FQFN is a directory and not ., .. or .svn
186 function isDirectory ($FQFN) {
187         // Do we have cache?
188         if (!isset($GLOBALS['is_directory'][$FQFN])) {
189                 // Generate baseName
190                 $baseName = basename($FQFN);
191
192                 // Check it
193                 $GLOBALS['is_directory'][$FQFN] = ((is_dir($FQFN)) && ($baseName != '.') && ($baseName != '..') && ($baseName != '.svn'));
194         } // END - if
195
196         // Return the result
197         return $GLOBALS['is_directory'][$FQFN];
198 }
199
200 // "Getter" for remote IP number
201 function detectRemoteAddr () {
202         // Get remote ip from environment
203         $remoteAddr = determineRealRemoteAddress();
204
205         // Is removeip installed?
206         if (isExtensionActive('removeip')) {
207                 // Then anonymize it
208                 $remoteAddr = getAnonymousRemoteAddress($remoteAddr);
209         } // END - if
210
211         // Return it
212         return $remoteAddr;
213 }
214
215 // "Getter" for remote hostname
216 function detectRemoteHostname () {
217         // Get remote ip from environment
218         $remoteHost = getenv('REMOTE_HOST');
219
220         // Is removeip installed?
221         if (isExtensionActive('removeip')) {
222                 // Then anonymize it
223                 $remoteHost = getAnonymousRemoteHost($remoteHost);
224         } // END - if
225
226         // Return it
227         return $remoteHost;
228 }
229
230 // "Getter" for user agent
231 function detectUserAgent ($alwaysReal = false) {
232         // Get remote ip from environment
233         $userAgent = getenv('HTTP_USER_AGENT');
234
235         // Is removeip installed?
236         if ((isExtensionActive('removeip')) && ($alwaysReal === false)) {
237                 // Then anonymize it
238                 $userAgent = getAnonymousUserAgent($userAgent);
239         } // END - if
240
241         // Return it
242         return $userAgent;
243 }
244
245 // "Getter" for referer
246 function detectReferer () {
247         // Get remote ip from environment
248         $referer = getenv('HTTP_REFERER');
249
250         // Is removeip installed?
251         if (isExtensionActive('removeip')) {
252                 // Then anonymize it
253                 $referer = getAnonymousReferer($referer);
254         } // END - if
255
256         // Return it
257         return $referer;
258 }
259
260 // "Getter" for request URI
261 function detectRequestUri () {
262         // Return it
263         return (getenv('REQUEST_URI'));
264 }
265
266 // "Getter" for query string
267 function detectQueryString () {
268         return str_replace('&', '&amp;', (getenv('QUERY_STRING')));
269 }
270
271 // "Getter" for SERVER_NAME
272 function detectServerName () {
273         // Return it
274         return (getenv('SERVER_NAME'));
275 }
276
277 // Check wether we are installing
278 function isInstalling () {
279         // Determine wether we are installing
280         if (!isset($GLOBALS['mxchange_installing'])) {
281                 // Check URL (css.php/js.php need this)
282                 $GLOBALS['mxchange_installing'] = isGetRequestParameterSet('installing');
283         } // END - if
284
285         // Return result
286         return $GLOBALS['mxchange_installing'];
287 }
288
289 // Check wether this script is installed
290 function isInstalled () {
291         // Do we have cache?
292         if (!isset($GLOBALS['is_installed'])) {
293                 // Determine wether this script is installed
294                 $GLOBALS['is_installed'] = (
295                 (
296                         // First is config
297                         (
298                                 (
299                                         isConfigEntrySet('MXCHANGE_INSTALLED')
300                                 ) && (
301                                         getConfig('MXCHANGE_INSTALLED') == 'Y'
302                                 )
303                         )
304                 ) || (
305                         // New config file found and loaded
306                         isIncludeReadable(getConfig('CACHE_PATH') . 'config-local.php')
307                 ) || (
308                         (
309                                 // New config file found, but not yet read
310                                 isIncludeReadable(getConfig('CACHE_PATH') . 'config-local.php')
311                         ) && (
312                                 (
313                                         // Only new config file is found
314                                         !isIncludeReadable('inc/config.php')
315                                 ) || (
316                                         // Is installation mode
317                                         !isInstalling()
318                                 )
319                         )
320                 ));
321         } // END - if
322
323         // Then use the cache
324         return $GLOBALS['is_installed'];
325 }
326
327 // Check wether an admin is registered
328 function isAdminRegistered () {
329         return ((isConfigEntrySet('ADMIN_REGISTERED')) && (getConfig('ADMIN_REGISTERED') == 'Y'));
330 }
331
332 // Checks wether the reset mode is active
333 function isResetModeEnabled () {
334         // Now simply check it
335         return ((isset($GLOBALS['reset_enabled'])) && ($GLOBALS['reset_enabled'] === true));
336 }
337
338 // Checks wether the debug mode is enabled
339 function isDebugModeEnabled () {
340         // Simply check it
341         return ((isConfigEntrySet('DEBUG_MODE')) && (getConfig('DEBUG_MODE') == 'Y'));
342 }
343
344 // Checks wether SQL debugging is enabled
345 function isSqlDebuggingEnabled () {
346         return ((isConfigEntrySet('DEBUG_SQL')) && (getConfig('DEBUG_SQL') == 'Y'));
347 }
348
349 // Checks wether we shall debug regular expressions
350 function isDebugRegExpressionEnabled () {
351         // Simply check it
352         return ((isConfigEntrySet('DEBUG_REGEX')) && (getConfig('DEBUG_REGEX') == 'Y'));
353 }
354
355 // Checks wether the cache instance is valid
356 function isCacheInstanceValid () {
357         return ((isset($GLOBALS['cache_instance'])) && (is_object($GLOBALS['cache_instance'])));
358 }
359
360 // Copies a file from source to destination and verifies if that goes fine.
361 // This function should wrap the copy() command and make a nicer debug backtrace
362 // even if there is no xdebug extension installed.
363 function copyFileVerified ($source, $dest, $chmod = '') {
364         // Failed is the default
365         $status = false;
366
367         // Is the source file there?
368         if (!isFileReadable($source)) {
369                 // Then abort here
370                 debug_report_bug('Cannot read from source file ' . basename($source) . '.');
371         } // END - if
372
373         // Is the target directory there?
374         if (!isDirectory(dirname($dest))) {
375                 // Then abort here
376                 debug_report_bug('Cannot find directory ' . str_replace(getConfig('PATH'), '', dirname($dest)) . '.');
377         } // END - if
378
379         // Now try to copy it
380         if (!copy($source, $dest)) {
381                 // Something went wrong
382                 debug_report_bug('copy() has failed to copy the file.');
383         } else {
384                 // Reset cache
385                 $GLOBALS['file_readable'][$dest] = true;
386         }
387
388         // If there are chmod rights set, apply them
389         if (!empty($chmod)) {
390                 // Try to apply them
391                 $status = changeMode($dest, $chmod);
392         } else {
393                 // All fine
394                 $status = true;
395         }
396
397         // All fine
398         return $status;
399 }
400
401 // Wrapper function for header()
402 // Send a header but checks before if we can do so
403 function sendHeader ($header) {
404         // Send the header
405         //* DEBUG: */ logDebugMessage(__FUNCTION__ . ': header=' . $header);
406         $GLOBALS['header'][] = trim($header);
407 }
408
409 // Flushes all headers
410 function flushHeaders () {
411         // Is the header already sent?
412         if (headers_sent()) {
413                 // Then abort here
414                 debug_report_bug('Headers already sent!');
415         } // END - if
416
417         // Flush all headers if found
418         if ((isset($GLOBALS['header'])) && (is_array($GLOBALS['header']))) {
419                 foreach ($GLOBALS['header'] as $header) {
420                         header($header);
421                 } // END - foreach
422         } // END - if
423
424         // Mark them as flushed
425         $GLOBALS['header'] = array();
426 }
427
428 // Wrapper function for chmod()
429 // @TODO Do some more sanity check here
430 function changeMode ($FQFN, $mode) {
431         // Is the file/directory there?
432         if ((!isFileReadable($FQFN)) && (!isDirectory($FQFN))) {
433                 // Neither, so abort here
434                 debug_report_bug('Cannot chmod() on ' . basename($FQFN) . '.');
435         } // END - if
436
437         // Try to set them
438         chmod($FQFN, $mode);
439 }
440
441 // Wrapper for unlink()
442 function removeFile ($FQFN) {
443         // Is the file there?
444         if (isFileReadable($FQFN)) {
445                 // Reset cache first
446                 $GLOBALS['file_readable'][$FQFN] = false;
447
448                 // Yes, so remove it
449                 return unlink($FQFN);
450         } // END - if
451
452         // All fine if no file was removed. If we change this to 'false' or rewrite
453         // above if() block it would be to restrictive.
454         return true;
455 }
456
457 // Wrapper for $_POST['sel']
458 function countPostSelection ($element = 'sel') {
459         // Is it set?
460         if (isPostRequestParameterSet($element)) {
461                 // Return counted elements
462                 return countSelection(postRequestParameter($element));
463         } else {
464                 // Return zero if not found
465                 return 0;
466         }
467 }
468
469 // Checks wether the config-local.php is loaded
470 function isConfigLocalLoaded () {
471         return ((isset($GLOBALS['config_local_loaded'])) && ($GLOBALS['config_local_loaded'] === true));
472 }
473
474 // Checks wether a nickname or userid was entered and caches the result
475 function isNicknameUsed ($userid) {
476         // Default is false
477         $isUsed = false;
478
479         // Is the cache there
480         if (isset($GLOBALS['is_nickname_used'][$userid])) {
481                 // Then use it
482                 $isUsed = $GLOBALS['is_nickname_used'][$userid];
483         } else {
484                 // Determine it
485                 $isUsed = (('' . round($userid) . '') != $userid);
486
487                 // And write it to the cache
488                 $GLOBALS['is_nickname_used'][$userid] = $isUsed;
489         }
490
491         // Return the result
492         return $isUsed;
493 }
494
495 // Getter for 'what' value
496 function getWhat () {
497         // Default is null
498         $what = null;
499
500         // Is the value set?
501         if (isWhatSet(true)) {
502                 // Then use it
503                 $what = $GLOBALS['what'];
504         } // END - if
505
506         // Return it
507         return $what;
508 }
509
510 // Setter for 'what' value
511 function setWhat ($newWhat) {
512         $GLOBALS['what'] = SQL_ESCAPE($newWhat);
513 }
514
515 // Setter for 'what' from configuration
516 function setWhatFromConfig ($configEntry) {
517         // Get 'what' from config
518         $what = getConfig($configEntry);
519
520         // Set it
521         setWhat($what);
522 }
523
524 // Checks wether what is set and optionally aborts on miss
525 function isWhatSet ($strict =  false) {
526         // Check for it
527         $isset = isset($GLOBALS['what']);
528
529         // Should we abort here?
530         if (($strict === true) && ($isset === false)) {
531                 // Output backtrace
532                 debug_report_bug('what is empty.');
533         } // END - if
534
535         // Return it
536         return $isset;
537 }
538
539 // Getter for 'action' value
540 function getAction () {
541         // Default is null
542         $action = null;
543
544         // Is the value set?
545         if (isActionSet(true)) {
546                 // Then use it
547                 $action = $GLOBALS['action'];
548         } // END - if
549
550         // Return it
551         return $action;
552 }
553
554 // Setter for 'action' value
555 function setAction ($newAction) {
556         $GLOBALS['action'] = SQL_ESCAPE($newAction);
557 }
558
559 // Checks wether action is set and optionally aborts on miss
560 function isActionSet ($strict =  false) {
561         // Check for it
562         $isset = ((isset($GLOBALS['action'])) && (!empty($GLOBALS['action'])));
563
564         // Should we abort here?
565         if (($strict === true) && ($isset === false)) {
566                 // Output backtrace
567                 debug_report_bug('action is empty.');
568         } // END - if
569
570         // Return it
571         return $isset;
572 }
573
574 // Getter for 'module' value
575 function getModule ($strict = true) {
576         // Default is null
577         $module = null;
578
579         // Is the value set?
580         if (isModuleSet($strict)) {
581                 // Then use it
582                 $module = $GLOBALS['module'];
583         } // END - if
584
585         // Return it
586         return $module;
587 }
588
589 // Setter for 'module' value
590 function setModule ($newModule) {
591         // Secure it and make all modules lower-case
592         $GLOBALS['module'] = SQL_ESCAPE(strtolower($newModule));
593 }
594
595 // Checks wether module is set and optionally aborts on miss
596 function isModuleSet ($strict =  false) {
597         // Check for it
598         $isset = (!empty($GLOBALS['module']));
599
600         // Should we abort here?
601         if (($strict === true) && ($isset === false)) {
602                 // Output backtrace
603                 debug_report_bug('module is empty.');
604         } // END - if
605
606         // Return it
607         return (($isset === true) && ($GLOBALS['module'] != 'unknown')) ;
608 }
609
610 // Getter for 'output_mode' value
611 function getOutputMode () {
612         // Default is null
613         $output_mode = null;
614
615         // Is the value set?
616         if (isOutputModeSet(true)) {
617                 // Then use it
618                 $output_mode = $GLOBALS['output_mode'];
619         } // END - if
620
621         // Return it
622         return $output_mode;
623 }
624
625 // Setter for 'output_mode' value
626 function setOutputMode ($newOutputMode) {
627         $GLOBALS['output_mode'] = (int) $newOutputMode;
628 }
629
630 // Checks wether output_mode is set and optionally aborts on miss
631 function isOutputModeSet ($strict =  false) {
632         // Check for it
633         $isset = (isset($GLOBALS['output_mode']));
634
635         // Should we abort here?
636         if (($strict === true) && ($isset === false)) {
637                 // Output backtrace
638                 debug_report_bug('output_mode is empty.');
639         } // END - if
640
641         // Return it
642         return $isset;
643 }
644
645 // Enables block-mode
646 function enableBlockMode ($enabled = true) {
647         $GLOBALS['block_mode'] = $enabled;
648 }
649
650 // Checks wether block-mode is enabled
651 function isBlockModeEnabled () {
652         // Abort if not set
653         if (!isset($GLOBALS['block_mode'])) {
654                 // Needs to be fixed
655                 debug_report_bug(__FUNCTION__ . ': block_mode is not set.');
656         } // END - if
657
658         // Return it
659         return $GLOBALS['block_mode'];
660 }
661
662 // Wrapper function for addPointsThroughReferalSystem()
663 function addPointsDirectly ($subject, $userid, $points) {
664         // Reset level here
665         unset($GLOBALS['ref_level']);
666
667         // Call more complicated method (due to more parameters)
668         return addPointsThroughReferalSystem($subject, $userid, $points, false, 0, false, 'direct');
669 }
670
671 // Wrapper function to redirect from member-only modules to index
672 function redirectToIndexMemberOnlyModule () {
673         // Do the redirect here
674         redirectToUrl('modules.php?module=index&code=' . getCode('MODULE_MEM_ONLY') . '&mod=' . getModule());
675 }
676
677 // Wrapper function to redirect to current URL
678 function redirectToRequestUri () {
679         redirectToUrl(basename(detectRequestUri()));
680 }
681
682 // Wrapper function to redirect to de-refered URL
683 function redirectToDereferedUrl ($URL) {
684         // Redirect to to
685         redirectToUrl(generateDerefererUrl($URL));
686 }
687
688 // Wrapper function for checking if extension is installed and newer or same version
689 function isExtensionInstalledAndNewer ($ext_name, $version) {
690         // Return it
691         //* DEBUG: */ print __FUNCTION__.':'.$ext_name.'=&gt;'.$version.'<br />';
692         return ((isExtensionInstalled($ext_name)) && (getExtensionVersion($ext_name) >= $version));
693 }
694
695 // Wrapper function for checking if extension is installed and older than given version
696 function isExtensionInstalledAndOlder ($ext_name, $version) {
697         // Return it
698         //* DEBUG: */ print __FUNCTION__.':'.$ext_name.'&lt;'.$version.'<br />';
699         return ((isExtensionInstalled($ext_name)) && (isExtensionOlder($ext_name, $version)));
700 }
701
702 // Set username
703 function setUsername ($userName) {
704         $GLOBALS['username'] = (string) $userName;
705 }
706
707 // Get username
708 function getUsername () {
709         // default is guest
710         $username = getMessage('USERNAME_GUEST');
711
712         // User name set?
713         if (isset($GLOBALS['username'])) {
714                 // Use the set name
715                 $username = $GLOBALS['username'];
716         } // END - if
717
718         // Return it
719         return $username;
720 }
721
722 // Wrapper function for installation phase
723 function isInstallationPhase () {
724         // Do we have cache?
725         if (!isset($GLOBALS['installation_phase'])) {
726                 // Determine it
727                 $GLOBALS['installation_phase'] = ((!isInstalled()) || (isInstalling()));
728         } // END - if
729
730         // Return result
731         return $GLOBALS['installation_phase'];
732 }
733
734 // Checks wether the extension demo is actuve and the admin login is demo (password needs to be demo, too!)
735 function isDemoModeActive () {
736         return ((isExtensionActive('demo')) && (getSession('admin_login') == 'demo'));
737 }
738
739 // Getter for PHP caching value
740 function getPhpCaching () {
741         return $GLOBALS['php_caching'];
742 }
743
744 // Checks wether the admin hash is set
745 function isAdminHashSet ($admin) {
746         /**
747          * @TODO Do we really need this check? If yes, try to fix this:
748          * 1.:functions.php:2504, debug_get_mailable_backtrace(0)
749          * 2.:wrapper-functions.php:744, debug_report_bug(1)
750          * 3.:mysql-manager.php:728, isAdminHashSet(1)
751          * 4.:filters.php:384, isAdmin(0)
752          * 5.:debug_get_mailable_backtrace:2457, FILTER_DETERMINE_USERNAME(1)
753          * 6.:filter-functions.php:280, call_user_func_array(2)
754          * 7.:load_cache.php:74, runFilterChain(1)
755          * 8.:inc-functions.php:131, include(1)
756          * 9.:inc-functions.php:145, loadInclude(1)
757          * 10.:mysql-connect.php:104, loadIncludeOnce(1)
758          * 11.:inc-functions.php:131, include(1)
759          * 12.:inc-functions.php:145, loadInclude(1)
760          * 13.:config-global.php:106, loadIncludeOnce(1)
761          * 14.:js.php:57, require(1)
762          */
763         if (!isset($GLOBALS['cache_array']['admin'])) {
764                 debug_report_bug('Cache not set.');
765         } // END - if
766
767         // Check for admin hash
768         return isset($GLOBALS['cache_array']['admin']['password'][$admin]);
769 }
770
771 // Setter for admin hash
772 function setAdminHash ($admin, $hash) {
773         $GLOBALS['cache_array']['admin']['password'][$admin] = $hash;
774 }
775
776 // Init user data array
777 function initUserData () {
778         // User id should not be zero
779         if (getCurrentUserId() < 1) debug_report_bug(__FUNCTION__.': User id is zero.');
780
781         // Init the user
782         $GLOBALS['user_data'][getCurrentUserId()] = array();
783 }
784
785 // Getter for user data
786 function getUserData ($column) {
787         // User id should not be zero
788         if (getCurrentUserId() < 1) debug_report_bug(__FUNCTION__.': User id is zero.');
789
790         // Return the value
791         return $GLOBALS['user_data'][getCurrentUserId()][$column];
792 }
793
794 // Geter for whole user data array
795 function getUserDataArray () {
796         // Get user id
797         $uid = getCurrentUserId();
798
799         // User id should not be zero
800         if ($uid < 1) debug_report_bug(__FUNCTION__.': User id is zero.');
801
802         // Get the whole array if found
803         if (isset($GLOBALS['user_data'][$uid])) {
804                 // Found, so return it
805                 return $GLOBALS['user_data'][$uid];
806         } else {
807                 // Return empty array
808                 return array();
809         }
810 }
811
812 // Checks if the user data is valid, this may indicate that the user has logged
813 // in, but you should use isMember() if you want to find that out.
814 function isUserDataValid () {
815         // User id should not be zero so abort here
816         if (!isCurrentUserIdSet()) return false;
817
818         // Is the array there and filled?
819         return ((isset($GLOBALS['user_data'][getCurrentUserId()])) && (count($GLOBALS['user_data'][getCurrentUserId()]) > 1));
820 }
821
822 // Setter for current userid
823 function setCurrentUserId ($userid) {
824         $GLOBALS['current_userid'] = bigintval($userid);
825 }
826
827 // Getter for current userid
828 function getCurrentUserId () {
829         // Userid must be set before it can be used
830         if (!isCurrentUserIdSet()) {
831                 // Not set
832                 debug_report_bug('User id is not set.');
833         } // END - if
834
835         // Return the userid
836         return $GLOBALS['current_userid'];
837 }
838
839 // Checks if current userid is set
840 function isCurrentUserIdSet () {
841         return isset($GLOBALS['current_userid']);
842 }
843
844 // Checks wether we are debugging template cache
845 function isDebuggingTemplateCache () {
846         return (getConfig('DEBUG_TEMPLATE_CACHE') == 'Y');
847 }
848
849 // Wrapper for fetchUserData() and getUserData() calls
850 function getFetchedUserData ($keyColumn, $userId, $valueColumn) {
851         // Default is 'guest'
852         $data = getMessage('USERNAME_GUEST');
853
854         // Can we fetch the user data?
855         if (($userId > 0) && (fetchUserData($userId, $keyColumn))) {
856                 // Now get the data back
857                 $data = getUserData($valueColumn);
858         } // END - if
859
860         // Return it
861         return $data;
862 }
863
864 // Wrapper for strpos() to ease porting from deprecated ereg() function
865 function isInString ($needle, $haystack) {
866         return (strpos($haystack, $needle) !== false);
867 }
868
869 // Wrapper for strpos() to ease porting from deprecated eregi() function
870 // This function is case-insensitive
871 function isInStringIgnoreCase ($needle, $haystack) {
872         return (isInString(strtolower($haystack), strtolower($needle)));
873 }
874
875 // [EOF]
876 ?>