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