]> git.mxchange.org Git - mailer.git/blobdiff - inc/functions.php
Even more constants/language strings rewritten, merge_array() now logs debug messages
[mailer.git] / inc / functions.php
index e545aa0d6e731f2e2007ac7927149bf1059aad45..6f668fc188a1775352b19ec101e1b93074eb1597 100644 (file)
@@ -40,7 +40,7 @@ if (!defined('__SECURITY')) {
 // Check if our config file is writeable or not
 function IS_INC_WRITEABLE($inc) {
        // Generate FQFN
-       $fqfn = sprintf("%sinc/%s.php", PATH, $inc);
+       $fqfn = sprintf("%sinc/%s.php", constant('PATH'), $inc);
 
        // Abort by simple test
        if ((FILE_READABLE($fqfn)) && (!is_writeable($fqfn))) {
@@ -67,7 +67,7 @@ function OUTPUT_HTML ($HTML, $NEW_LINE = true) {
        // Do we have HTML-Code here?
        if (!empty($HTML)) {
                // Yes, so we handle it as you have configured
-               switch (OUTPUT_MODE)
+               switch (constant('OUTPUT_MODE'))
                {
                case "render":
                        // That's why you don't need any \n at the end of your HTML code... :-)
@@ -97,7 +97,7 @@ function OUTPUT_HTML ($HTML, $NEW_LINE = true) {
 
                default:
                        // Huh, something goes wrong or maybe you have edited config.php ???
-                       DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Invalid renderer %s detected.", OUTPUT_MODE));
+                       DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Invalid renderer %s detected.", constant('OUTPUT_MODE')));
                        mxchange_die("<strong>{!FATAL_ERROR!}:</strong> {!LANG_NO_RENDER_DIRECT!}");
                        break;
                }
@@ -152,7 +152,7 @@ function OUTPUT_HTML ($HTML, $NEW_LINE = true) {
 
                // Output code here, DO NOT REMOVE! ;-)
                OUTPUT_RAW($OUTPUT);
-       } elseif ((OUTPUT_MODE == "render") && (!empty($OUTPUT))) {
+       } elseif ((constant('OUTPUT_MODE') == "render") && (!empty($OUTPUT))) {
                // Rewrite links when rewrite extension is active
                if ((EXT_IS_ACTIVE("rewrite")) && ($CSS != "1") && ($CSS != "-1")) {
                        $OUTPUT = REWRITE_LINKS($OUTPUT);
@@ -265,7 +265,7 @@ function LOAD_TEMPLATE ($template, $return=false, $content=array()) {
        $date_time = MAKE_DATETIME(time(), "1");
 
        // Base directory
-       $BASE = sprintf("%stemplates/%s/html/", PATH, GET_LANGUAGE());
+       $BASE = sprintf("%stemplates/%s/html/", constant('PATH'), GET_LANGUAGE());
        $MODE = "";
 
        // Check for admin/guest/member templates
@@ -459,7 +459,7 @@ Message : ".$MSG."
                SEND_RAW_EMAIL($TO, $SUBJECT, $MSG, $FROM);
        } elseif ($HTML == "N") {
                // Problem found!
-               SEND_RAW_EMAIL(WEBMASTER, "[PROBLEM:]".$SUBJECT, $MSG, $FROM);
+               SEND_RAW_EMAIL(constant('WEBMASTER'), "[PROBLEM:]".$SUBJECT, $MSG, $FROM);
        }
 }
 
@@ -476,25 +476,25 @@ function SEND_RAW_EMAIL ($to, $subject, $msg, $from) {
        // Shall we use PHPMailer class or legacy mode?
        if (CHECK_PHPMAILER_USAGE()) {
                // Use PHPMailer class with SMTP enabled
-               require_once(PATH."inc/phpmailer/class.phpmailer.php");
-               require_once(PATH."inc/phpmailer/class.smtp.php");
+               LOAD_INC_ONCE("inc/phpmailer/class.phpmailer.php");
+               LOAD_INC_ONCE("inc/phpmailer/class.smtp.php");
 
                // get new instance
                $mail = new PHPMailer();
-               $mail->PluginDir  = sprintf("%sinc/phpmailer/", PATH);
+               $mail->PluginDir  = sprintf("%sinc/phpmailer/", constant('PATH'));
 
                $mail->IsSMTP();
                $mail->SMTPAuth   = true;
-               $mail->Host       = SMTP_HOSTNAME;
+               $mail->Host       = constant('SMTP_HOSTNAME');
                $mail->Port       = 25;
-               $mail->Username   = SMTP_USER;
-               $mail->Password   = SMTP_PASSWORD;
+               $mail->Username   = constant('SMTP_USER');
+               $mail->Password   = constant('SMTP_PASSWORD');
                if (empty($from)) {
                        $mail->From = constant('WEBMASTER');
                } else {
                        $mail->From = $from;
                }
-               $mail->FromName   = MAIN_TITLE;
+               $mail->FromName   = constant('MAIN_TITLE');
                $mail->Subject    = $subject;
                if ((EXT_IS_ACTIVE("html_mail")) && (strip_tags($msg) != $msg)) {
                        $mail->Body       = $msg;
@@ -505,9 +505,9 @@ function SEND_RAW_EMAIL ($to, $subject, $msg, $from) {
                        $mail->Body       = html_entity_decode($msg);
                }
                $mail->AddAddress($to, "");
-               $mail->AddReplyTo(WEBMASTER,MAIN_TITLE);
-               $mail->AddCustomHeader("Errors-To:".WEBMASTER);
-               $mail->AddCustomHeader("X-Loop:".WEBMASTER);
+               $mail->AddReplyTo(constant('WEBMASTER'), constant('MAIN_TITLE'));
+               $mail->AddCustomHeader("Errors-To:".constant('WEBMASTER'));
+               $mail->AddCustomHeader("X-Loop:".constant('WEBMASTER'));
                $mail->Send();
        } else {
                // Use legacy mail() command
@@ -701,7 +701,7 @@ function GET_LANGUAGE() {
        global $cacheArray;
 
        // Set default return value to default language from config
-       $ret = DEFAULT_LANG;
+       $ret = constant('DEFAULT_LANG');
 
        // Init variable
        $lang = "";
@@ -724,7 +724,7 @@ function GET_LANGUAGE() {
                $ret = get_session('mx_lang');
 
                // Fixes a warning before the session has the mx_lang constant
-               if (empty($ret)) $ret = DEFAULT_LANG;
+               if (empty($ret)) $ret = constant('DEFAULT_LANG');
        }
 
        // Cache entry
@@ -751,15 +751,12 @@ function LOAD_EMAIL_TEMPLATE($template, $content=array(), $UID="0") {
        // Default "nickname" if extension is not installed
        $nick = "---";
 
-       // Keept for backward-compatiblity (please replace these variables against our new {!CONST!} syntax!)
-       // No longer used: $MAIN_TITLE = MAIN_TITLE; $URL = constant('URL'); $WEBMASTER = constant('WEBMASTER');
-
        // Prepare IP number and User Agent
        $REMOTE_ADDR     = GET_REMOTE_ADDR();
        $HTTP_USER_AGENT = GET_USER_AGENT();
 
        // Default admin
-       $ADMIN = MAIN_TITLE;
+       $ADMIN = constant('MAIN_TITLE');
 
        // Is the admin logged in?
        if (IS_ADMIN()) {
@@ -823,7 +820,7 @@ function LOAD_EMAIL_TEMPLATE($template, $content=array(), $UID="0") {
        $DATA['email'] = $email;
 
        // Base directory
-       $BASE = sprintf("%stemplates/%s/emails/", PATH, GET_LANGUAGE());
+       $BASE = sprintf("%stemplates/%s/emails/", constant('PATH'), GET_LANGUAGE());
 
        // Check for admin/guest/member templates
        if (strpos($template, "admin_") > -1) {
@@ -950,9 +947,9 @@ function LOAD_URL($URL, $addUrlData=true) {
                header ("Location: ".str_replace("&amp;", "&", $URL));
        } else {
                // Output error message
-               require(PATH."inc/header.php");
+               LOAD_INC("inc/header.php");
                LOAD_TEMPLATE("redirect_url", false, str_replace("&amp;", "&", $URL));
-               require(PATH."inc/footer.php");
+               LOAD_INC("inc/footer.php");
        }
        exit();
 }
@@ -1319,7 +1316,7 @@ function GENERATE_IMAGE($img_code, $header=true) {
        }
 
        // Load image
-       $img = sprintf("%s/theme/%s/images/code_bg.%s", PATH, GET_CURR_THEME(), getConfig('img_type'));
+       $img = sprintf("%s/theme/%s/images/code_bg.%s", constant('PATH'), GET_CURR_THEME(), getConfig('img_type'));
        if (FILE_READABLE($img)) {
                // Switch image type
                switch (getConfig('img_type'))
@@ -1955,7 +1952,7 @@ function VALIDATE_URL ($URL, $compile=true) {
        return (((substr($URL, 0, 7) == "http://") || (substr($URL, 0, 8) == "https://")) && (strlen($URL) >= 12));
 }
 //
-function MEMBER_ACTION_LINKS($uid, $status="") {
+function MEMBER_ACTION_LINKS ($uid, $status = "") {
        // Define all main targets
        $TARGETS = array("del_user", "edit_user", "lock_user", "add_points", "sub_points");
 
@@ -2240,13 +2237,13 @@ function FIX_DELETED_COOKIES ($cookies) {
 // Output error messages in a fasioned way and die...
 function mxchange_die ($msg) {
        // Load header
-       require_once(PATH."inc/header.php");
+       LOAD_INC_ONCE("inc/header.php");
 
        // Load the message template
        LOAD_TEMPLATE("admin_settings_saved", false, $msg);
 
        // Load footer
-       require(PATH."inc/footer.php");
+       LOAD_INC("inc/footer.php");
 
        // Exit explicitly
        exit;
@@ -2400,10 +2397,15 @@ function merge_array ($array1, $array2) {
                return array_merge($array1, $array2);
        } elseif (is_array($array1)) {
                // Return left array
+               DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("array2 is not an array. array != %s", gettype($array2)));
                return $array1;
+       } elseif (is_array($array2)) {
+               // Return right array
+               DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("array1 is not an array. array != %s", gettype($array1)));
+               return $array2;
        }
 
-       // Something wired happened here...
+       // Both are not arrays
        print(__FUNCTION__.":<pre>");
        debug_print_backtrace();
        die("</pre>");
@@ -2639,13 +2641,13 @@ function REBUILD_CACHE ($cache, $inc="") {
                // Include file given?
                if (!empty($inc)) {
                        // Construct FQFN
-                       $fqfn = sprintf("%sinc/loader/load_cache-%s.php", PATH, $inc);
+                       $INC = sprintf("inc/loader/load_cache-%s.php", $inc);
 
                        // Is the include there?
-                       if (FILE_READABLE($fqfn)) {
+                       if (FILE_READABLE($INC)) {
                                // And rebuild it from scratch
                                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): inc={$inc} - LOADED!<br />\n";
-                               require($fqfn);
+                               LOAD_INC($INC);
                        } else {
                                // Include not found!
                                DEBUG_LOG(__FUNCTION__, __LINE__, "Include {$inc} not found. cache={$cache}");
@@ -2915,13 +2917,13 @@ function GET_CURR_THEME() {
                } // END - if
        } elseif ((!isBooleanConstantAndTrue('mxchange_installed')) && ((isBooleanConstantAndTrue('mxchange_installing')) || ($CSS == true)) && ((!empty($_GET['theme'])) || (!empty($_POST['theme'])))) {
                // Prepare FQFN for checking
-               $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_GET['theme']));
+               $theme = sprintf("%stheme/%s/theme.php", constant('PATH'), SQL_ESCAPE($_GET['theme']));
 
                // Installation mode active
                if ((!empty($_GET['theme'])) && (FILE_READABLE($theme))) {
                        // Set cookie from URL data
                        set_session('mxchange_theme', SQL_ESCAPE($_GET['theme']));
-               } elseif (FILE_READABLE(sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($_POST['theme'])))) {
+               } elseif (FILE_READABLE(sprintf("%stheme/%s/theme.php", constant('PATH'), SQL_ESCAPE($_POST['theme'])))) {
                        // Set cookie from posted data
                        set_session('mxchange_theme', SQL_ESCAPE($_POST['theme']));
                }
@@ -2934,7 +2936,7 @@ function GET_CURR_THEME() {
        }
 
        // Add (maybe) found theme.php file to inclusion list
-       $theme = sprintf("%stheme/%s/theme.php", PATH, SQL_ESCAPE($ret));
+       $theme = sprintf("%stheme/%s/theme.php", constant('PATH'), SQL_ESCAPE($ret));
 
        // Try to load the requested include file
        if (FILE_READABLE($theme)) $INC_POOL[] = $theme;
@@ -3069,7 +3071,7 @@ function getActualVersion ($type = 0) {
        $new = false;
 
        // FQFN of revision file
-       $FQFN = sprintf("%sinc/cache/.revision", PATH);
+       $FQFN = sprintf("%sinc/cache/.revision", constant('PATH'));
 
        // Check for revision file
        if (!FILE_READABLE($FQFN)) {
@@ -3105,6 +3107,48 @@ function getActualVersion ($type = 0) {
                return trim($akt_vers[$type]);
        }
 }
+
+// Loads an include file and logs any missing files for debug purposes
+function LOAD_INC ($INC) {
+       // Get constant path
+       $PATH = constant('PATH');
+
+       // Use the include file name directly
+       // @TODO Try to find all locations where an FQFN is given to these two
+       // @TODO functions and avoid it.
+       $FQFN = $INC;
+
+       // Check if PATH is in $INC
+       if (substr($INC, 0, $PATH) != $PATH) {
+               // Add it. This is why we need a trailing slash in config.php
+               $FQFN = $PATH . $INC;
+       } // END - if
+
+       // Is the include file there?
+       if (!FILE_READABLE($FQFN)) {
+               // Not there so log it
+               DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Include file %s not found.", basename($INC)));
+               return false;
+       } // END - if
+
+       // Try to load it
+       require($FQFN);
+}
+
+// Loads an include file once
+function LOAD_INC_ONCE ($INC) {
+       global $cacheArray;
+
+       // Is it not loaded?
+       if (!isset($cacheArray['load_once'][$INC])) {
+               // Then try to load it
+               LOAD_INC($INC);
+
+               // And mark it as loaded
+               $cacheArray['load_once'][$INC] = true;
+       } // END - if
+}
+
 //////////////////////////////////////////////////
 // AUTOMATICALLY RE-GENERATED MISSING FUNCTIONS //
 //////////////////////////////////////////////////