56e281e356472b3a7eaa763d7312e2c036b792f5
[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 we shall debug regular expressions
345 function isDebugRegExpressionEnabled () {
346         // Simply check it
347         return ((isConfigEntrySet('DEBUG_REGEX')) && (getConfig('DEBUG_REGEX') == 'Y'));
348 }
349
350 // Checks wether the cache instance is valid
351 function isCacheInstanceValid () {
352         return ((isset($GLOBALS['cache_instance'])) && (is_object($GLOBALS['cache_instance'])));
353 }
354
355 // Copies a file from source to destination and verifies if that goes fine.
356 // This function should wrap the copy() command and make a nicer debug backtrace
357 // even if there is no xdebug extension installed.
358 function copyFileVerified ($source, $dest, $chmod = '') {
359         // Failed is the default
360         $status = false;
361
362         // Is the source file there?
363         if (!isFileReadable($source)) {
364                 // Then abort here
365                 debug_report_bug('Cannot read from source file ' . basename($source) . '.');
366         } // END - if
367
368         // Is the target directory there?
369         if (!isDirectory(dirname($dest))) {
370                 // Then abort here
371                 debug_report_bug('Cannot find directory ' . str_replace(getConfig('PATH'), '', dirname($dest)) . '.');
372         } // END - if
373
374         // Now try to copy it
375         if (!copy($source, $dest)) {
376                 // Something went wrong
377                 debug_report_bug('copy() has failed to copy the file.');
378         } else {
379                 // Reset cache
380                 $GLOBALS['file_readable'][$dest] = true;
381         }
382
383         // If there are chmod rights set, apply them
384         if (!empty($chmod)) {
385                 // Try to apply them
386                 $status = changeMode($dest, $chmod);
387         } else {
388                 // All fine
389                 $status = true;
390         }
391
392         // All fine
393         return $status;
394 }
395
396 // Wrapper function for header()
397 // Send a header but checks before if we can do so
398 function sendHeader ($header) {
399         // Send the header
400         $GLOBALS['header'][] = trim($header);
401 }
402
403 // Flushes all headers
404 function flushHeaders () {
405         // Is the header already sent?
406         if (headers_sent()) {
407                 // Then abort here
408                 debug_report_bug('Headers already sent!');
409         } // END - if
410
411         // Flush all headers if found
412         if ((isset($GLOBALS['header'])) && (is_array($GLOBALS['header']))) {
413                 foreach ($GLOBALS['header'] as $header) {
414                         header($header);
415                 } // END - foreach
416         } // END - if
417
418         // Mark them as flushed
419         $GLOBALS['header'] = array();
420 }
421
422 // Wrapper function for chmod()
423 // @TODO Do some more sanity check here
424 function changeMode ($FQFN, $mode) {
425         // Is the file/directory there?
426         if ((!isFileReadable($FQFN)) && (!isDirectory($FQFN))) {
427                 // Neither, so abort here
428                 debug_report_bug('Cannot chmod() on ' . basename($FQFN) . '.');
429         } // END - if
430
431         // Try to set them
432         chmod($FQFN, $mode);
433 }
434
435 // Wrapper for unlink()
436 function removeFile ($FQFN) {
437         // Is the file there?
438         if (isFileReadable($FQFN)) {
439                 // Reset cache first
440                 $GLOBALS['file_readable'][$FQFN] = false;
441
442                 // Yes, so remove it
443                 return unlink($FQFN);
444         } // END - if
445
446         // All fine if no file was removed. If we change this to 'false' or rewrite
447         // above if() block it would be to restrictive.
448         return true;
449 }
450
451 // Wrapper for $_POST['sel']
452 function countPostSelection ($element = 'sel') {
453         // Is it set?
454         if (isPostRequestParameterSet($element)) {
455                 // Return counted elements
456                 return countSelection(postRequestParameter($element));
457         } else {
458                 // Return zero if not found
459                 return 0;
460         }
461 }
462
463 // Checks wether the config-local.php is loaded
464 function isConfigLocalLoaded () {
465         return ((isset($GLOBALS['config_local_loaded'])) && ($GLOBALS['config_local_loaded'] === true));
466 }
467
468 // Checks wether a nickname or userid was entered and caches the result
469 function isNicknameUsed ($userid) {
470         // Default is false
471         $isUsed = false;
472
473         // Is the cache there
474         if (isset($GLOBALS['is_nickname_used'][$userid])) {
475                 // Then use it
476                 $isUsed = $GLOBALS['is_nickname_used'][$userid];
477         } else {
478                 // Determine it
479                 $isUsed = (('' . round($userid) . '') != $userid);
480
481                 // And write it to the cache
482                 $GLOBALS['is_nickname_used'][$userid] = $isUsed;
483         }
484
485         // Return the result
486         return $isUsed;
487 }
488
489 // Getter for 'what' value
490 function getWhat () {
491         // Default is null
492         $what = null;
493
494         // Is the value set?
495         if (isWhatSet(true)) {
496                 // Then use it
497                 $what = $GLOBALS['what'];
498         } // END - if
499
500         // Return it
501         return $what;
502 }
503
504 // Setter for 'what' value
505 function setWhat ($newWhat) {
506         $GLOBALS['what'] = SQL_ESCAPE($newWhat);
507 }
508
509 // Setter for 'what' from configuration
510 function setWhatFromConfig ($configEntry) {
511         // Get 'what' from config
512         $what = getConfig($configEntry);
513
514         // Set it
515         setWhat($what);
516 }
517
518 // Checks wether what is set and optionally aborts on miss
519 function isWhatSet ($strict =  false) {
520         // Check for it
521         $isset = isset($GLOBALS['what']);
522
523         // Should we abort here?
524         if (($strict === true) && ($isset === false)) {
525                 // Output backtrace
526                 debug_report_bug('what is empty.');
527         } // END - if
528
529         // Return it
530         return $isset;
531 }
532
533 // Getter for 'action' value
534 function getAction () {
535         // Default is null
536         $action = null;
537
538         // Is the value set?
539         if (isActionSet(true)) {
540                 // Then use it
541                 $action = $GLOBALS['action'];
542         } // END - if
543
544         // Return it
545         return $action;
546 }
547
548 // Setter for 'action' value
549 function setAction ($newAction) {
550         $GLOBALS['action'] = SQL_ESCAPE($newAction);
551 }
552
553 // Checks wether action is set and optionally aborts on miss
554 function isActionSet ($strict =  false) {
555         // Check for it
556         $isset = ((isset($GLOBALS['action'])) && (!empty($GLOBALS['action'])));
557
558         // Should we abort here?
559         if (($strict === true) && ($isset === false)) {
560                 // Output backtrace
561                 debug_report_bug('action is empty.');
562         } // END - if
563
564         // Return it
565         return $isset;
566 }
567
568 // Getter for 'module' value
569 function getModule ($strict = true) {
570         // Default is null
571         $module = null;
572
573         // Is the value set?
574         if (isModuleSet($strict)) {
575                 // Then use it
576                 $module = $GLOBALS['module'];
577         } // END - if
578
579         // Return it
580         return $module;
581 }
582
583 // Setter for 'module' value
584 function setModule ($newModule) {
585         // Secure it and make all modules lower-case
586         $GLOBALS['module'] = SQL_ESCAPE(strtolower($newModule));
587 }
588
589 // Checks wether module is set and optionally aborts on miss
590 function isModuleSet ($strict =  false) {
591         // Check for it
592         $isset = (!empty($GLOBALS['module']));
593
594         // Should we abort here?
595         if (($strict === true) && ($isset === false)) {
596                 // Output backtrace
597                 debug_report_bug('module is empty.');
598         } // END - if
599
600         // Return it
601         return (($isset === true) && ($GLOBALS['module'] != 'unknown')) ;
602 }
603
604 // Getter for 'output_mode' value
605 function getOutputMode () {
606         // Default is null
607         $output_mode = null;
608
609         // Is the value set?
610         if (isOutputModeSet(true)) {
611                 // Then use it
612                 $output_mode = $GLOBALS['output_mode'];
613         } // END - if
614
615         // Return it
616         return $output_mode;
617 }
618
619 // Setter for 'output_mode' value
620 function setOutputMode ($newOutputMode) {
621         $GLOBALS['output_mode'] = (int) $newOutputMode;
622 }
623
624 // Checks wether output_mode is set and optionally aborts on miss
625 function isOutputModeSet ($strict =  false) {
626         // Check for it
627         $isset = (isset($GLOBALS['output_mode']));
628
629         // Should we abort here?
630         if (($strict === true) && ($isset === false)) {
631                 // Output backtrace
632                 debug_report_bug('output_mode is empty.');
633         } // END - if
634
635         // Return it
636         return $isset;
637 }
638
639 // Enables block-mode
640 function enableBlockMode ($enabled = true) {
641         $GLOBALS['block_mode'] = $enabled;
642 }
643
644 // Checks wether block-mode is enabled
645 function isBlockModeEnabled () {
646         // Abort if not set
647         if (!isset($GLOBALS['block_mode'])) {
648                 // Needs to be fixed
649                 debug_report_bug(__FUNCTION__ . ': block_mode is not set.');
650         } // END - if
651
652         // Return it
653         return $GLOBALS['block_mode'];
654 }
655
656 // Wrapper function for addPointsThroughReferalSystem()
657 function addPointsDirectly ($subject, $userid, $points) {
658         // Reset level here
659         unset($GLOBALS['ref_level']);
660
661         // Call more complicated method (due to more parameters)
662         return addPointsThroughReferalSystem($subject, $userid, $points, false, 0, false, 'direct');
663 }
664
665 // Wrapper function to redirect from member-only modules to index
666 function redirectToIndexMemberOnlyModule () {
667         // Do the redirect here
668         redirectToUrl('modules.php?module=index&amp;code=' . getCode('MODULE_MEM_ONLY') . '&amp;mod=' . getModule());
669 }
670
671 // Wrapper function for checking if extension is installed and newer or same version
672 function isExtensionInstalledAndNewer ($ext_name, $version) {
673         // Return it
674         //* DEBUG: */ print __FUNCTION__.':'.$ext_name.'=&gt;'.$version.'<br />';
675         return ((isExtensionInstalled($ext_name)) && (getExtensionVersion($ext_name) >= $version));
676 }
677
678 // Wrapper function for checking if extension is installed and older than given version
679 function isExtensionInstalledAndOlder ($ext_name, $version) {
680         // Return it
681         //* DEBUG: */ print __FUNCTION__.':'.$ext_name.'&lt;'.$version.'<br />';
682         return ((isExtensionInstalled($ext_name)) && (isExtensionOlder($ext_name, $version)));
683 }
684
685 // Set username
686 function setUsername ($userName) {
687         $GLOBALS['username'] = (string) $userName;
688 }
689
690 // Get username
691 function getUsername () {
692         // default is guest
693         $username = getMessage('USERNAME_GUEST');
694
695         // User name set?
696         if (isset($GLOBALS['username'])) {
697                 // Use the set name
698                 $username = $GLOBALS['username'];
699         } // END - if
700
701         // Return it
702         return $username;
703 }
704
705 // Wrapper function for installation phase
706 function isInstallationPhase () {
707         // Do we have cache?
708         if (!isset($GLOBALS['installation_phase'])) {
709                 // Determine it
710                 $GLOBALS['installation_phase'] = ((!isInstalled()) || (isInstalling()));
711         } // END - if
712
713         // Return result
714         return $GLOBALS['installation_phase'];
715 }
716
717 // Checks wether the extension demo is actuve and the admin login is demo (password needs to be demo, too!)
718 function isDemoModeActive () {
719         return ((isExtensionActive('demo')) && (getSession('admin_login') == 'demo'));
720 }
721
722 // Wrapper function to redirect to de-refered URL
723 function redirectToDereferedUrl ($URL) {
724         // De-refer the URL
725         $URL = generateDerefererUrl($URL);
726
727         // Redirect to to
728         redirectToUrl($URL);
729 }
730
731 // Getter for PHP caching value
732 function getPhpCaching () {
733         return $GLOBALS['php_caching'];
734 }
735
736 // Checks wether the admin hash is set
737 function isAdminHashSet ($admin) {
738         if (!isset($GLOBALS['cache_array']['admin'])) debug_report_bug('Cache not set.');
739         return isset($GLOBALS['cache_array']['admin']['password'][$admin]);
740 }
741
742 // Setter for admin hash
743 function setAdminHash ($admin, $hash) {
744         $GLOBALS['cache_array']['admin']['password'][$admin] = $hash;
745 }
746
747 // Init user data array
748 function initUserData () {
749         // User id should not be zero
750         if (getCurrentUserId() < 1) debug_report_bug(__FUNCTION__.': User id is zero.');
751
752         // Init the user
753         $GLOBALS['user_data'][getCurrentUserId()] = array();
754 }
755
756 // Getter for user data
757 function getUserData ($column) {
758         // User id should not be zero
759         if (getCurrentUserId() < 1) debug_report_bug(__FUNCTION__.': User id is zero.');
760
761         // Return the value
762         return $GLOBALS['user_data'][getCurrentUserId()][$column];
763 }
764
765 // Geter for whole user data array
766 function getUserDataArray () {
767         // User id should not be zero
768         if (getCurrentUserId() < 1) debug_report_bug(__FUNCTION__.': User id is zero.');
769
770         // Get the whole array
771         return $GLOBALS['user_data'][getCurrentUserId()];
772 }
773
774 // Checks if the user data is valid, this may indicate that the user has logged
775 // in, but you should use isMember() if you want to find that out.
776 function isUserDataValid () {
777         // User id should not be zero so abort here
778         if (!isCurrentUserIdSet()) return false;
779
780         // Is the array there and filled?
781         return ((isset($GLOBALS['user_data'][getCurrentUserId()])) && (count($GLOBALS['user_data'][getCurrentUserId()]) > 1));
782 }
783
784 // Setter for current userid
785 function setCurrentUserId ($userid) {
786         $GLOBALS['current_userid'] = bigintval($userid);
787 }
788
789 // Getter for current userid
790 function getCurrentUserId () {
791         // Userid must be set before it can be used
792         if (!isCurrentUserIdSet()) {
793                 // Not set
794                 debug_report_bug('User id is not set.');
795         } // END - if
796
797         // Return the userid
798         return $GLOBALS['current_userid'];
799 }
800
801 // Checks if current userid is set
802 function isCurrentUserIdSet () {
803         return isset($GLOBALS['current_userid']);
804 }
805
806 // Checks wether we are debugging template cache
807 function isDebuggingTemplateCache () {
808         return (getConfig('DEBUG_TEMPLATE_CACHE') == 'Y');
809 }
810
811 // Wrapper for fetchUserData() and getUserData() calls
812 function getFetchedUserData ($keyColumn, $userId, $valueColumn) {
813         // Default is 'guest'
814         $data = getMessage('USERNAME_GUEST');
815
816         // Can we fetch the user data?
817         if (($userId > 0) && (fetchUserData($userId, $keyColumn))) {
818                 // Now get the data back
819                 $data = getUserData($valueColumn);
820         } // END - if
821
822         // Return it
823         return $data;
824 }
825
826 // [EOF]
827 ?>