More queries now depends on UNIX_TIMESTAMP() SQL function, wrong index in autopurge...
[mailer.git] / inc / install-inc.php
index 0909dd89bf3fee2de708c419b5fce8427bbe8321..55eb350f4fd272dab2db9fa5976de5e90018b5eb 100644 (file)
  ************************************************************************/
 
 // Some security stuff...
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
-{
+if (!defined('__SECURITY')) {
        $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
        require($INC);
 }
 
 //
 function install_WriteData ($file, $comment, $prefix, $suffix, $DATA, $sneak=0) {
-       $done = false;  $sneak++;
-       if (file_exists($file)) {
+       // Initialize all and count up the "seak" value
+       $done = false;
+       $next = -1;
+       $sneak++;
+
+       if (FILE_READABLE($file)) {
                $search = "CFG: ".$comment;
                $tmp = $file.".tmp";
-               $fp = @fopen($file, 'r') or OUTPUT_HTML("<STRONG>READ:</STRONG> ".$file."<BR>");
+               $fp = @fopen($file, 'r') or OUTPUT_HTML("<STRONG>READ:</STRONG> ".$file."<br />");
                if (is_resource($fp)) {
-                       $fp_tmp = @fopen($tmp, 'w') or OUTPUT_HTML("<STRONG>WRITE:</STRONG> ".$tmp."<BR>");
+                       $fp_tmp = @fopen($tmp, 'w') or OUTPUT_HTML("<STRONG>WRITE:</STRONG> ".$tmp."<br />");
                        if (is_resource($fp_tmp)) {
                                while (!feof($fp)) {
                                        $line = fgets ($fp, 10240);
-                                       if (strpos($line, $search) > -1) $next = 0;
+                                       //* DEBUG: */ echo $search."/<font color=\"red\">".htmlentities($line)."</font><br />\n";
+                                       $found = strpos($line, $search);
+                                       //* DEBUG: */ echo "FOUND: <font color=\"brown\">";
+                                       //* DEBUG: */ var_dump($found);
+                                       //* DEBUG: */ echo "</font><br />\n";
+                                       if ($found !== false) $next = 0;
                                        if ($next > -1) {
                                                if ($next == $sneak) {
                                                        $next = -1;
                                                        $line = $prefix.$DATA.$suffix."\n";
+                                                       //* DEBUG: */ echo "NEW: <font color=\"blue\">".htmlentities($line)."</font><br />\n";
                                                } else {
                                                        $next++;
                                                }
                                        }
+                                       //* DEBUG: */ echo "WRITE: <font color=\"green\">".htmlentities($line)."</font><br />\n";
                                        fputs($fp_tmp, $line);
                                }
                                fclose($fp_tmp);
+
                                // Finished writing tmp file
                                $done = true;
                        }
@@ -82,7 +93,7 @@ function install_WriteData ($file, $comment, $prefix, $suffix, $DATA, $sneak=0)
                        }
                }
        } else {
-               OUTPUT_HTML("<STRONG>404:</STRONG> ".$file."<BR>");
+               OUTPUT_HTML("<STRONG>404:</STRONG> ".$file."<br />");
        }
 }
 
@@ -125,7 +136,7 @@ if ((isset($_GET['page']) && ($_GET['page'] == 5))) {
 }
 
 // Is MXChange installed or no admin registered so far?
-if ((!mxchange_installed) || (!admin_registered))
+if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndTrue('admin_registered')))
 {
        // Set URL for FORM actions
        define('__BURL_ACTION', $burl);
@@ -155,10 +166,10 @@ if ((!mxchange_installed) || (!admin_registered))
                if (empty($mysql['prefix'])) $mysql['prefix'] = "mxchange_";
                if ((sizeof($FATAL) > 0) || (!empty($FATAL[0]))) {
                        OUTPUT_HTML("<SPAN class=\"install_fatal\">");
-                       foreach ($FATAL as $key=>$err) {
-                               OUTPUT_HTML("  <STRONG>&middot;</STRONG>&nbsp;".FATAL_NO.($key + 1).":&nbsp;".$err."<BR>\n");
+                       foreach ($FATAL as $key => $err) {
+                               OUTPUT_HTML("  <STRONG>&middot;</STRONG>&nbsp;".FATAL_NO.($key + 1).":&nbsp;".$err."<br />");
                        }
-                       OUTPUT_HTML("</SPAN><BR>");
+                       OUTPUT_HTML("</SPAN><br />");
                }
                define('__MYSQL_HOST'  , $mysql['host']);
                define('__MYSQL_DBASE' , $mysql['dbase']);
@@ -261,10 +272,10 @@ if ((!mxchange_installed) || (!admin_registered))
   <TD>
     <SELECT name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
       <OPTION value=\"true\"");
-               if (warn_no_pass) OUTPUT_HTML(" selected=\"selected\"");
+               if (isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
                OUTPUT_HTML(">".YES."</OPTION>
       <OPTION value=\"false\"");
-               if (!warn_no_pass) OUTPUT_HTML(" selected=\"selected\"");
+               if (!isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
                OUTPUT_HTML(">".NO."</OPTION>
     </SELECT>
   </TD>
@@ -275,24 +286,10 @@ if ((!mxchange_installed) || (!admin_registered))
   <TD>
     <SELECT name=\"wfooter\" size=\"1\" class=\"install_select\">
       <OPTION value=\"true\"");
-               if (WRITE_FOOTER) OUTPUT_HTML(" selected=\"selected\"");
+               if (isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
                OUTPUT_HTML(">".YES."</OPTION>
       <OPTION value=\"false\"");
-               if (!WRITE_FOOTER) OUTPUT_HTML(" selected=\"selected\"");
-               OUTPUT_HTML(">".NO."</OPTION>
-    </SELECT>
-  </TD>
-</TR>
-<TR><TD colspan=\"2\" height=\"10\" class=\"seperator\">&nbsp;</TD></TR>
-<TR>
-  <TD class=\"install\" align=\"right\">".LANG_FRAMESET_ACTIVE.":&nbsp;&nbsp;</TD>
-  <TD>
-    <SELECT name=\"frameset\" size=\"1\" class=\"install_select\">
-      <OPTION value=\"true\"");
-               if (frameset_active) OUTPUT_HTML(" selected=\"selected\"");
-               OUTPUT_HTML(">".YES."</OPTION>
-      <OPTION value=\"false\"");
-               if (!frameset_active) OUTPUT_HTML(" selected=\"selected\"");
+               if (!isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
                OUTPUT_HTML(">".NO."</OPTION>
     </SELECT>
   </TD>
@@ -314,7 +311,7 @@ if ((!mxchange_installed) || (!admin_registered))
 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
 <TR>
   <TD colspan=\"2\" align=\"center\">");
-               foreach ($mysql as $key=>$value) {
+               foreach ($mysql as $key => $value) {
                        OUTPUT_HTML("    <INPUT type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">");
                }
                OUTPUT_HTML("    <INPUT type=\"hidden\" name=\"spath\" value=\"".__SPATH_VALUE."\">
@@ -334,9 +331,9 @@ if ((!mxchange_installed) || (!admin_registered))
                break;
 
        case "finalize": // Write captured data to files
-               if (!empty($_POST['finalize'])) {
+               if ((!empty($_POST['finalize'])) && (!isBooleanConstantAndTrue('mxchange_installed'))) {
                        // You have submitted data then we have to reset the fatal messages
-                       $FATAL = array();
+                       $FATAL = array(); $SQLs = array();
 
                        // Connect to MySQL server
                        $link = SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
@@ -345,52 +342,65 @@ if ((!mxchange_installed) || (!admin_registered))
                                $db = SQL_SELECT_DB($mysql['dbase'], $link, __FILE__, __LINE__);
                                if ($db) {
                                        // Automatically run install.sql
-                                       if (file_exists($_POST['spath']."install/tables.sql")) {
-                                               $ERRORS = $_POST['spath']."inc/sql.err";
-                                               $cmd = array(
-                                                       // "Dump" tha' s*** up... ;-) > /dev/null
-                                                       "mysql ".$mysql['dbase']." --force --host=".$mysql['host']." --user=".$mysql['login']." --password=".$mysql['pass1']." < ".$_POST['spath']."install/tables.sql",
-                                                       "mysql ".$mysql['dbase']." --force --host=".$mysql['host']." --user=".$mysql['login']." --password=".$mysql['pass1']." < ".$_POST['spath']."install/menu-".GET_LANGUAGE().".sql"
-                                               );
-
-                                               foreach ($cmd as $c) {
-                                                       @system($c);
+                                       if ((FILE_READABLE($_POST['spath']."install/tables.sql")) && (FILE_READABLE($_POST['spath']."install/menu-".GET_LANGUAGE().".sql"))) {
+                                               // Both exists so import them
+                                               foreach (array("tables.sql", "menu-".GET_LANGUAGE().".sql") as $dump) {
+                                                       // Should be save here because file_exists() is there but we check it again. :)
+                                                       $file = secureString($_POST['spath']) . "install/" . $dump;
+                                                       if (FILE_READABLE($file)) {
+                                                               // Load the file
+                                                               $sql = implode("", file($file));
+
+                                                               // Remove some unwanted chars
+                                                               $sql = str_replace("\r", "", $sql);
+                                                               $sql = str_replace("\n\n", "\n", $sql);
+
+                                                               // And split it up against ;\n ...
+                                                               $SQLs = array_merge($SQLs, explode(";\n", $sql));
+                                                       }
                                                }
 
-                                               if ((file_exists($ERRORS)) && (filesize($ERRORS) > 0)) {
-                                                       ADD_FATAL(MYSQLDUMP_ERROR);
-                                               } elseif (!mxchange_installed) {
-                                                       // Remove error file if empty
-                                                       if (file_exists($ERRORS)) @unlink ($ERRORS);
-
-                                                       // Ok, all done. So we can write the config data to the php files
-                                                       if ($_POST['spath'] != PATH) install_WriteData($_POST['spath']."inc/config.php", "SERVER-PATH", "define ('PATH', \"", "\");", $_POST['spath'], 0);
-                                                       if ($_POST['burl']  != URL)  install_WriteData($_POST['spath']."inc/config.php", "HOST-URL", "define ('URL', \"", "\");", $_POST['burl'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "MAIN_TITLE", "define ('MAIN_TITLE', \"", "\");", $_POST['title'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "SLOGAN", "define ('SLOGAN', \"", "\");", $_POST['slogan'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "WEBMASTER", "define ('WEBMASTER', \"", "\");", $_POST['email'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "NULLPASS-WARNING", "define ('warn_no_pass', ", ");", $_POST['warn_no_pass'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "WRITE-FOOTER", "define ('WRITE_FOOTER', ", ");", $_POST['wfooter'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "BACKLINK", "define ('ENABLED_BACKLINK', ", ");", $_POST['blink'], 0);
-                                                       // install_WriteData($_POST['spath']."inc/config.php", "FRAMESET", "define ('frameset_active', ", ");", $_POST['frameset'], 0);
-                                                       // install_WriteData($_POST['spath']."inc/config.php", "OUTPUT-MODE", "define ('OUTPUT_MODE', \"", "\");", $_POST['omode'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "MYSQL-HOST", "     'host'     => \"", "\",", $mysql['host'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "MYSQL-DBASE", "    'dbase'    => \"", "\",", $mysql['dbase'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "MYSQL-LOGIN", "    'login'    => \"", "\",", $mysql['login'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PASSWORD", " 'password' => \"", "\",", $mysql['pass1'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PREFIX", "define ('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "SMTP-HOSTNAME", "define ('SMTP_HOSTNAME', \"", "\");", $_POST['smtp_host'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "SMTP-USER", "define ('SMTP_USER', \"", "\");", $_POST['smtp_user'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "SMTP-PASSWORD", "define ('SMTP_PASSWORD', \"", "\");", $_POST['smtp_pass'], 0);
-                                                       install_WriteData($_POST['spath']."inc/config.php", "INSTALLED", "define ('mxchange_installed', ", ");", "true", 0);
-
-                                                       // Close the link
-                                                       SQL_CLOSE($link, __FILE__, __LINE__);
+                                               // Are some SQLs found?
+                                               if (count($SQLs) == 0) {
+                                                       // Abort here
+                                                       ADD_FATAL(INSTALL_SQL_IMPORT_FAILED);
+                                                       return;
                                                }
-                                                else
-                                               {
-                                                       ADD_FATAL(INSTALL_MAYBE_DONE);
+
+                                               // Now run all queries through and try to keep out empty or comment queries
+                                               foreach ($SQLs as $sql) {
+                                                       // Trim spaces away
+                                                       $sql = trim($sql);
+
+                                                       // Is this query not empty and not a comment?
+                                                       if ((!empty($sql)) && (substr($sql, 0, 2) != "--") && (substr($sql, 0, 1) != "#")) {
+                                                               // Then run it!
+                                                               SQL_QUERY($sql, __FILE__, __LINE__);
+                                                       }
                                                }
+
+                                               // Ok, all done. So we can write the config data to the php files
+                                               if ($_POST['spath'] != PATH) install_WriteData($_POST['spath']."inc/config.php", "SERVER-PATH", "define('PATH', \"", "\");", $_POST['spath'], 0);
+                                               if ($_POST['burl']  != URL)  install_WriteData($_POST['spath']."inc/config.php", "HOST-URL", "define('URL', \"", "\");", $_POST['burl'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "MAIN_TITLE", "define('MAIN_TITLE', \"", "\");", $_POST['title'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "SLOGAN", "define('SLOGAN', \"", "\");", $_POST['slogan'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "WEBMASTER", "define('WEBMASTER', \"", "\");", $_POST['email'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "NULLPASS-WARNING", "define('warn_no_pass', ", ");", $_POST['warn_no_pass'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "WRITE-FOOTER", "define('WRITE_FOOTER', ", ");", $_POST['wfooter'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "BACKLINK", "define('ENABLE_BACKLINK', ", ");", $_POST['blink'], 0);
+                                               // install_WriteData($_POST['spath']."inc/config.php", "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", $_POST['omode'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "MYSQL-HOST", "     'host'     => \"", "\",", $mysql['host'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "MYSQL-DBASE", "    'dbase'    => \"", "\",", $mysql['dbase'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "MYSQL-LOGIN", "    'login'    => \"", "\",", $mysql['login'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PASSWORD", " 'password' => \"", "\",", $mysql['pass1'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PREFIX", "define('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "SMTP-HOSTNAME", "define('SMTP_HOSTNAME', \"", "\");", $_POST['smtp_host'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "SMTP-USER", "define('SMTP_USER', \"", "\");", $_POST['smtp_user'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "SMTP-PASSWORD", "define('SMTP_PASSWORD', \"", "\");", $_POST['smtp_pass'], 0);
+                                               install_WriteData($_POST['spath']."inc/config.php", "INSTALLED", "define('mxchange_installed', ", ");", "true", 0);
+
+                                               // Close the link
+                                               SQL_CLOSE($link, __FILE__, __LINE__);
                                        }
                                         else
                                        {
@@ -399,7 +409,7 @@ if ((!mxchange_installed) || (!admin_registered))
                                        }
                                }
                        }
-                       if ((sizeof($FATAL) > 0) || ($FATAL[0] != ""))
+                       if ((sizeof($FATAL) > 0) || ($FATAL[0] != ''))
                        {
                                $OUT = "";
                                foreach ($FATAL as $value)
@@ -408,7 +418,7 @@ if ((!mxchange_installed) || (!admin_registered))
                                }
                                define('__FATAL_ERROR_LI', $OUT);
                                $OUT = "";
-                               foreach ($mysql as $key=>$value)
+                               foreach ($mysql as $key => $value)
                                {
                                        $OUT .= "    <INPUT type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
                                }
@@ -418,8 +428,7 @@ if ((!mxchange_installed) || (!admin_registered))
                                define('__TITLE_VALUE'  , $_POST['title']);
                                define('__SMTP_HOST'    , $_POST['smtp_host']);
                                define('__SMTP_USER'    , $_POST['smtp_user']);
-                               define('__SMTP_PASS1'   , $_POST['smtp_pass1']);
-                               define('__SMTP_PASS2'   , $_POST['smtp_pass2']);
+                               define('__SMTP_PASS'    , $_POST['smtp_pass']);
 
                                OUTPUT_HTML("<FORM action=\"".__BURL_ACTION."/install.php?page=2\" method=\"POST\">
 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
@@ -456,7 +465,7 @@ if ((!mxchange_installed) || (!admin_registered))
                                LOAD_URL($URL);
                        }
                }
-                elseif (mxchange_installed)
+                elseif (isBooleanConstantAndTrue('mxchange_installed'))
                {
                        // Redirection after writing data... :-)
                        LOAD_TEMPLATE("install_finished");