WRITE_FILLE() rewritten to try to set CHMOD, thanks to Profi-Concept
[mailer.git] / inc / functions.php
index ef6cbde5323b40bf5ff838a7513d39983f08896d..a55dda49c181db6def6913bd2a896880de657fb4 100644 (file)
@@ -303,7 +303,7 @@ function LOAD_TEMPLATE ($template, $return=false, $content=array()) {
        ////////////////////////
        // Generate file name //
        ////////////////////////
-       $file = $BASE.$MODE.$template.".tpl";
+       $FQFN = $BASE.$MODE.$template.".tpl";
 
        if ((!empty($GLOBALS['what'])) && ((strpos($template, "_header") > 0) || (strpos($template, "_footer") > 0)) && (($MODE == "guest/") || ($MODE == "member/") || ($MODE == "admin/"))) {
                // Select what depended header/footer template file for admin/guest/member area
@@ -315,22 +315,22 @@ function LOAD_TEMPLATE ($template, $return=false, $content=array()) {
                );
 
                // Probe for it...
-               if (FILE_READABLE($file2)) $file = $file2;
+               if (FILE_READABLE($file2)) $FQFN = $file2;
 
                // Remove variable from memory
                unset($file2);
        }
 
        // Does the special template exists?
-       if (!FILE_READABLE($file)) {
+       if (!FILE_READABLE($FQFN)) {
                // Reset to default template
-               $file = $BASE.$template.".tpl";
+               $FQFN = $BASE.$template.".tpl";
        } // END - if
 
        // Now does the final template exists?
-       if (FILE_READABLE($file)) {
+       if (FILE_READABLE($FQFN)) {
                // The local file does exists so we load it. :)
-               $tmpl_file = READ_FILE($file);
+               $tmpl_file = READ_FILE($FQFN);
 
                // Replace ' to our own chars to preventing them being quoted
                while (strpos($tmpl_file, "'") !== false) { $tmpl_file = str_replace("'", '{QUOT}', $tmpl_file); }
@@ -351,7 +351,7 @@ function LOAD_TEMPLATE ($template, $return=false, $content=array()) {
        } elseif ((IS_ADMIN()) || ((isBooleanConstantAndTrue('mxchange_installing')) && (!isBooleanConstantAndTrue('mxchange_installed')))) {
                // Only admins shall see this warning or when installation mode is active
                $ret = "<br /><span class=\"guest_failed\">".TEMPLATE_404."</span><br />
-(".basename($file).")<br />
+(".basename($FQFN).")<br />
 <br />
 ".TEMPLATE_CONTENT."
 <pre>".print_r($content, true)."</pre>
@@ -830,36 +830,36 @@ function LOAD_EMAIL_TEMPLATE($template, $content=array(), $UID="0") {
        // Check for admin/guest/member templates
        if (strpos($template, "admin_") > -1) {
                // Admin template found
-               $file = $BASE."admin/".$template.".tpl";
+               $FQFN = $BASE."admin/".$template.".tpl";
        } elseif (strpos($template, "guest_") > -1) {
                // Guest template found
-               $file = $BASE."guest/".$template.".tpl";
+               $FQFN = $BASE."guest/".$template.".tpl";
        } elseif (strpos($template, "member_") > -1) {
                // Member template found
-               $file = $BASE."member/".$template.".tpl";
+               $FQFN = $BASE."member/".$template.".tpl";
        } else {
                // Test for extension
                $test = substr($template, 0, strpos($template, "_"));
                if (EXT_IS_ACTIVE($test)) {
                        // Set extra path to extension's name
-                       $file = $BASE.$test."/".$template.".tpl";
+                       $FQFN = $BASE.$test."/".$template.".tpl";
                } else {
                        // No special filename
-                       $file = $BASE.$template.".tpl";
+                       $FQFN = $BASE.$template.".tpl";
                }
        }
 
        // Does the special template exists?
-       if (!FILE_READABLE($file)) {
+       if (!FILE_READABLE($FQFN)) {
                // Reset to default template
-               $file = $BASE.$template.".tpl";
+               $FQFN = $BASE.$template.".tpl";
        } // END - if
 
        // Now does the final template exists?
        $newContent = "";
-       if (FILE_READABLE($file)) {
+       if (FILE_READABLE($FQFN)) {
                // The local file does exists so we load it. :)
-               $tmpl_file = READ_FILE($file);
+               $tmpl_file = READ_FILE($FQFN);
                $tmpl_file = SQL_ESCAPE($tmpl_file);
 
                // Run code
@@ -1245,10 +1245,10 @@ function GEN_RANDOM_CODE ($length, $code, $uid, $DATA="") {
 
        // Build key string
        $keys   = constant('SITE_KEY').":".constant('DATE_KEY');
-       if (getConfig('secret_key') != null)  $keys .= ":".getConfig('secret_key');
-       if (getConfig('file_hash') != null)   $keys .= ":".getConfig('file_hash');
+       if (isConfigEntrySet('secret_key'))  $keys .= ":".getConfig('secret_key');
+       if (isConfigEntrySet('file_hash'))   $keys .= ":".getConfig('file_hash');
        $keys .= ":".date("d-m-Y (l-F-T)", bigintval(getConfig('patch_ctime')));
-       if (getConfig('master_salt') != null) $keys .= ":".getConfig('master_salt');
+       if (isConfigEntrySet('master_salt')) $keys .= ":".getConfig('master_salt');
 
        // Build string from misc data
        $data   = $code.":".$uid.":".$DATA;
@@ -1263,7 +1263,7 @@ function GEN_RANDOM_CODE ($length, $code, $uid, $DATA="") {
        // Calculate number for generating the code
        $a = $code + constant('_ADD') - 1;
 
-       if (getConfig('master_hash') != null) {
+       if (isConfigEntrySet('master_hash')) {
                // Generate hash with master salt from modula of number with the prime number and other data
                $saltedHash = generateHash(($a % constant('_PRIME')).":".$server.":".$keys.":".$data.":".date("d-m-Y (l-F-T)", time()).":".$a, getConfig('master_salt'));
 
@@ -2418,12 +2418,12 @@ function merge_array ($array1, $array2) {
 }
 
 // Debug message logger
-function DEBUG_LOG ($file, $line, $message, $force=true) {
+function DEBUG_LOG ($funcFile, $line, $message, $force=true) {
        // Is debug mode enabled?
        if ((isBooleanConstantAndTrue('DEBUG_MODE')) || ($force === true)) {
                // Log this message away
                $fp = fopen(constant('PATH')."inc/cache/debug.log", 'a') or mxchange_die("Cannot write logfile debug.log!");
-               fwrite($fp, date("d.m.Y|H:i:s", time())."|".basename($file)."|".$line."|".strip_tags($message)."\n");
+               fwrite($fp, date("d.m.Y|H:i:s", time())."|".basename($funcFile)."|".$line."|".strip_tags($message)."\n");
                fclose($fp);
        } // END - if
 }
@@ -2439,11 +2439,11 @@ function GET_DIR_AS_ARRAY ($baseDir, $prefix) {
        while ($baseFile = readdir($dirPointer)) {
                // Load file only if extension is active
                // Make full path
-               $file = $baseDir.$baseFile;
+               $FQFN = $baseDir.$baseFile;
 
                // Is this a valid reset file?
                //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):baseDir={$baseDir},prefix={$prefix},baseFile={$baseFile}<br />\n";
-               if ((FILE_READABLE($file)) && (substr($baseFile, 0, strlen($prefix)) == $prefix) && (substr($baseFile, -4, 4) == ".php")) {
+               if ((FILE_READABLE($FQFN)) && (substr($baseFile, 0, strlen($prefix)) == $prefix) && (substr($baseFile, -4, 4) == ".php")) {
                        // Remove both for extension name
                        $extName = substr($baseFile, strlen($prefix), -4);
 
@@ -2453,10 +2453,10 @@ function GET_DIR_AS_ARRAY ($baseDir, $prefix) {
                        // Is the extension valid and active?
                        if (($extId > 0) && (EXT_IS_ACTIVE($extName))) {
                                // Then add this file
-                               $INCs[] = $file;
+                               $INCs[] = $FQFN;
                        } elseif ($extId == 0) {
                                // Add non-extension files as well
-                               $INCs[] = $file;
+                               $INCs[] = $FQFN;
                        }
                } // END - if
        } // END - while
@@ -3050,10 +3050,22 @@ function READ_FILE ($FQFN, $sqlPrepare = false) {
 
 // Writes content to a file
 function WRITE_FILE ($FQFN, $content) {
+       // Is the file writeable?
+       if (((FILE_READABLE($FQFN)) && (!is_writeable($FQFN))) && (!chmod($FQFN, 0644)) {
+               // Not writeable!
+               DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("File %s not writeable.", basename($FQFN)));
+
+               // Failed! :(
+               return false;
+       } // END - if
+
+       // By default all is failed...
+       $return = false;
+
        // Is the function there?
        if (function_exists('file_put_contents')) {
                // Write it directly
-               file_put_contents($FQFN, $content);
+               $return = file_put_contents($FQFN, $content);
        } else {
                // Write it with fopen
                $fp = fopen($FQFN, 'w') or mxchange_die("Cannot write file ".basename($FQFN)."!");
@@ -3061,8 +3073,11 @@ function WRITE_FILE ($FQFN, $content) {
                fclose($fp);
 
                // Set CHMOD rights
-               chmod($FQFN, 0644);
+               $return = chmod($FQFN, 0644);
        }
+
+       // Return status
+       return $return;
 }
 
 // Generates an error code from given account status
@@ -3435,6 +3450,78 @@ function decodeEntities ($str) {
        return $decodedString;
 }
 
+// Wtites data to a config.php-style file
+// @TODO Rewrite this function to use READ_FILE() and WRITE_FILE()
+function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $DATA, $seek=0) {
+       // Initialize some variables
+       $done = false;
+       $seek++;
+       $next  = -1;
+       $found = false;
+
+       // Is the file there and read-/write-able?
+       if ((FILE_READABLE($FQFN)) && (is_writeable($FQFN))) {
+               $search = "CFG: ".$comment;
+               $tmp = $FQFN.".tmp";
+
+               // Open the source file
+               $fp = @fopen($FQFN, 'r') or OUTPUT_HTML("<strong>READ:</strong> ".$FQFN."<br />");
+
+               // Is the resource valid?
+               if (is_resource($fp)) {
+                       // Open temporary file
+                       $fp_tmp = @fopen($tmp, 'w') or OUTPUT_HTML("<strong>WRITE:</strong> ".$tmp."<br />");
+
+                       // Is the resource again valid?
+                       if (is_resource($fp_tmp)) {
+                               while (!feof($fp)) {
+                                       // Read from source file
+                                       $line = fgets ($fp, 1024);
+
+                                       if (strpos($line, $search) > -1) { $next = 0; $found = true; }
+
+                                       if ($next > -1) {
+                                               if ($next === $seek) {
+                                                       $next = -1;
+                                                       $line = $prefix . $DATA . $suffix."\n";
+                                               } else {
+                                                       $next++;
+                                               }
+                                       }
+
+                                       // Write to temp file
+                                       fputs($fp_tmp, $line);
+                               }
+
+                               // Close temp file
+                               fclose($fp_tmp);
+
+                               // Finished writing tmp file
+                               $done = true;
+                       }
+
+                       // Close source file
+                       fclose($fp);
+
+                       if (($done) && ($found)) {
+                               // Copy back tmp file and delete tmp :-)
+                               @copy($tmp, $FQFN);
+                               @unlink($tmp);
+                               define('_FATAL', false);
+                       } elseif (!$found) {
+                               OUTPUT_HTML("<strong>CHANGE:</strong> 404!");
+                               define('_FATAL', true);
+                       } else {
+                               OUTPUT_HTML("<strong>TMP:</strong> UNDONE!");
+                               define('_FATAL', true);
+                       }
+               }
+       } else {
+               // File not found, not readable or writeable
+               OUTPUT_HTML("<strong>404:</strong> ".$FQFN."<br />");
+       }
+}
+
 //////////////////////////////////////////////////
 // AUTOMATICALLY RE-GENERATED MISSING FUNCTIONS //
 //////////////////////////////////////////////////