2 /************************************************************************
3 * MXChange v0.2.1 Start: 08/29/2003 *
4 * =============== Last change: 11/11/2004 *
6 * -------------------------------------------------------------------- *
7 * File : install-inc.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for installation procedure *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer die Installationsroutine *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 /************************************************************************
35 * YOU MAY ALSO WANT TO REMOVE THIS FILE AFTER INSTALLTION! *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
45 function install_WriteData ($file, $comment, $prefix, $suffix, $DATA, $sneak=0) {
46 // Initialize all and count up the "seak" value
51 if (FILE_READABLE($file)) {
52 $search = "CFG: ".$comment;
54 $fp = @fopen($file, 'r') or OUTPUT_HTML("<strong>READ:</strong> ".$file."<br />");
55 if (is_resource($fp)) {
56 $fp_tmp = @fopen($tmp, 'w') or OUTPUT_HTML("<strong>WRITE:</strong> ".$tmp."<br />");
57 if (is_resource($fp_tmp)) {
59 $line = fgets ($fp, 10240);
60 //* DEBUG: */ echo $search."/<font color=\"red\">".htmlentities($line)."</font><br />\n";
61 $found = strpos($line, $search);
62 //* DEBUG: */ echo "FOUND: <font color=\"brown\">";
63 //* DEBUG: */ var_dump($found);
64 //* DEBUG: */ echo "</font><br />\n";
65 if ($found !== false) $next = 0;
67 if ($next == $sneak) {
69 $line = $prefix.$DATA.$suffix."\n";
70 //* DEBUG: */ echo "NEW: <font color=\"blue\">".htmlentities($line)."</font><br />\n";
75 //* DEBUG: */ echo "WRITE: <font color=\"green\">".htmlentities($line)."</font><br />\n";
76 fputs($fp_tmp, $line);
80 // Finished writing tmp file
84 // Close source directory
87 if (($done) && (is_writeable($file))) {
88 // Copy back tmp file and delete tmp :-)
92 OUTPUT_HTML("<strong>TMP:</strong> UNDONE!");
96 OUTPUT_HTML("<strong>404:</strong> ".$file."<br />");
102 if (isset($_POST['mysql']) && is_array($_POST['mysql'])) $mysql = $_POST['mysql'];
104 if (URL != "http://your-server.com") {
105 // Already installed???
106 $burl = constant('URL');
107 } elseif (empty($burl)) {
109 $burl = "http://".$SERVER_NAME.$_SERVER['PHP_SELF'];
110 $burl = substr($burl, 0, strpos($burl, "install.php"));
113 // Check if both passwords from SMTP are matching
114 if ((isset($_GET['page']) && ($_GET['page'] == 5))) {
115 // Okay, we have to check it
116 if (!empty($_POST['smtp_user']) && (empty($_POST['smtp_host']))) {
118 OUTPUT_HTML(getMessage('INSTALL_SMTP_HOSTNAME_EMPTY')."<br />");
121 if ((empty($_POST['smtp_pass1'])) && (!empty($_POST['smtp_pass2']))) {
123 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS1_EMPTY')."<br />");
126 if ((!empty($_POST['smtp_pass1'])) && (empty($_POST['smtp_pass2']))) {
127 // Password repeat is empty
128 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS2_EMPTY')."<br />");
131 if ($_POST['smtp_pass1'] != $_POST['smtp_pass1']) {
132 // Passwords are not matching
133 OUTPUT_HTML(getMessage('INSTALL_SMTP_PASS_MISMATCH')."<br />");
138 // Is MXChange installed or no admin registered so far?
139 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (!isBooleanConstantAndTrue('admin_registered')))
141 // Set URL for FORM actions
142 define('__BURL_ACTION', $burl);
144 // Output page for entered value
145 switch ($_GET['page'])
147 case "welcome": // Welcome to the installation!
148 LOAD_TEMPLATE("install_welcome");
151 case "1": // Server path, base URL
152 // @Profi-Concept: Hab meine Meinung doch geaendert! :-)
154 LOAD_TEMPLATE("install_page1");
157 case "2": // MySQL data (alone!)
158 if (empty($mysql['dbase'])) $mysql['dbase'] = "your_database";
159 if (empty($mysql['login'])) $mysql['login'] = "your_login";
160 if (empty($mysql['host'])) $mysql['host'] = "localhost";
161 if (empty($mysql['prefix'])) $mysql['prefix'] = "mxchange_";
162 if (getTotalFatalErrors() > 0) {
163 OUTPUT_HTML("<span class=\"install_fatal\">");
164 foreach ($FATAL as $key => $err) {
165 OUTPUT_HTML(" <strong>·</strong> {--FATAL_NO--}".($key + 1).": ".$err."<br />");
167 OUTPUT_HTML("</span><br />");
169 define('__MYSQL_HOST' , $mysql['host']);
170 define('__MYSQL_DBASE' , $mysql['dbase']);
171 define('__MYSQL_PREFIX', $mysql['prefix']);
172 define('__MYSQL_LOGIN' , $mysql['login']);
173 define('__SPATH_VALUE' , $_POST['spath']);
174 define('__BURL_VALUE' , $_POST['burl']);
175 define('__TITLE_VALUE' , $_POST['title']);
176 define('__SLOGAN_VALUE', $_POST['slogan']);
177 define('__EMAIL_VALUE' , $_POST['email']);
180 LOAD_TEMPLATE("install_page2");
185 define('__SPATH_VALUE' , $_POST['spath']);
186 define('__BURL_VALUE' , $_POST['burl']);
187 define('__TITLE_VALUE' , $_POST['title']);
188 define('__SLOGAN_VALUE' , $_POST['slogan']);
189 define('__EMAIL_VALUE' , $_POST['email']);
191 // Use default SMTP data
192 $smtpHost = constant('SMTP_HOSTNAME');
193 $smtpUser = constant('SMTP_USER');
194 $smtpPass1 = constant('SMTP_PASSWORD');
195 $smtpPass2 = constant('SMTP_PASSWORD');
197 // Overwrite it with the data from sent (failed) form
198 if (!empty($_POST['smtp_host'])) $smtpHost = $_POST['smtp_host'];
199 if (!empty($_POST['smtp_user'])) $smtpUser = $_POST['smtp_user'];
202 define('__MYSQL_HOST' , $mysql['host']);
203 define('__MYSQL_DBASE' , $mysql['dbase']);
204 define('__MYSQL_PREFIX' , $mysql['prefix']);
205 define('__MYSQL_LOGIN' , $mysql['login']);
206 define('__MYSQL_PASS1' , $mysql['pass1']);
207 define('__MYSQL_PASS2' , $mysql['pass2']);
209 // Set constants for SMTP data
210 define('__SMTP_HOST' , $smtpHost);
211 define('__SMTP_USER' , $smtpUser);
212 define('__SMTP_PASS1' , $smtpPass1);
213 define('__SMTP_PASS2' , $smtpPass2);
216 LOAD_TEMPLATE("install_page3");
219 case "5": // Misc settings
221 define('__SPATH_VALUE' , $_POST['spath']);
222 define('__BURL_VALUE' , $_POST['burl']);
223 define('__TITLE_VALUE' , $_POST['title']);
224 define('__SLOGAN_VALUE' , $_POST['slogan']);
225 define('__EMAIL_VALUE' , $_POST['email']);
228 define('__MYSQL_HOST' , $mysql['host']);
229 define('__MYSQL_DBASE' , $mysql['dbase']);
230 define('__MYSQL_PREFIX' , $mysql['prefix']);
231 define('__MYSQL_LOGIN' , $mysql['login']);
234 define('__SMTP_HOST' , $_POST['smtp_host']);
235 define('__SMTP_USER' , $_POST['smtp_user']);
236 define('__SMTP_PASS' , $_POST['smtp_pass1']);
237 OUTPUT_HTML("<form action=\"{!__BURL_ACTION!}/install.php?page=finalize\" method=\"POST\" target=\"_self\">
238 <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
240 <td colspan=\"2\" align=\"center\">
241 <div class=\"install\">{--HEADER_TEXT_PAGE5--}</strong>
244 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\"> </td></tr>
246 <td colspan=\"2\"><span class=\"install_intro\">
250 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\"> </td></tr>
252 <td class=\"install\" align=\"right\">{--LANG_OUTPUT_MODE--}: </td>
254 <select name=\"omode\" size=\"1\" class=\"install_select\">
255 <option value=\"render\"");
256 if (constant('OUTPUT_MODE') == "render") OUTPUT_HTML(" selected=\"selected\"");
257 OUTPUT_HTML(">{--MODE_RENDER--}</option>
258 <option value=\"direct\"");
259 if (constant('OUTPUT_MODE') == "direct") OUTPUT_HTML(" selected=\"selected\"");
260 OUTPUT_HTML(">{--MODE_DIRECT--}</option>
264 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\"> </td></tr>
266 <td class=\"install\" align=\"right\">{--WARN_NO_PASSWORD--}: </td>
268 <select name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
269 <option value=\"true\"");
270 if (isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
271 OUTPUT_HTML(">{--YES--}</option>
272 <option value=\"false\"");
273 if (!isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
274 OUTPUT_HTML(">{--NO--}</option>
278 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\"> </td></tr>
280 <td class=\"install\" align=\"right\">{--LANG_WRITE_FOOTER--}: </td>
282 <select name=\"wfooter\" size=\"1\" class=\"install_select\">
283 <option value=\"true\"");
284 if (isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
285 OUTPUT_HTML(">{--YES--}</option>
286 <option value=\"false\"");
287 if (!isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
288 OUTPUT_HTML(">{--NO--}</option>
292 <tr><td colspan=\"2\" height=\"10\" class=\"seperator\"> </td></tr>
294 <td class=\"install\" align=\"right\">{--INSTALL_ENABLE_BACKLINK--}: </td>
296 <select name=\"blink\" size=\"1\" class=\"install_select\">
297 <option value=\"true\"");
298 if (isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
299 OUTPUT_HTML(">{--YES--}</option>
300 <option value=\"false\"");
301 if (!isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
302 OUTPUT_HTML(">{--NO--}</option>
306 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\"> </td></tr>
308 <td colspan=\"2\" align=\"center\">");
309 foreach ($mysql as $key => $value) {
310 OUTPUT_HTML(" <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">");
312 OUTPUT_HTML(" <input type=\"hidden\" name=\"spath\" value=\"{!__SPATH_VALUE!}\">
313 <input type=\"hidden\" name=\"burl\" value=\"{!__BURL_VALUE!}\" />
314 <input type=\"hidden\" name=\"title\" value=\"{!__TITLE_VALUE!}\" />
315 <input type=\"hidden\" name=\"slogan\" value=\"{!__SLOGAN_VALUE!}\" />
316 <input type=\"hidden\" name=\"email\" value=\"{!__EMAIL_VALUE!}\" />
317 <input type=\"hidden\" name=\"smtp_host\" value=\"{!__SMTP_HOST!}\" />
318 <input type=\"hidden\" name=\"smtp_user\" value=\"{!__SMTP_USER!}\" />
319 <input type=\"hidden\" name=\"smtp_pass\" value=\"{!__SMTP_PASS!}\" />
320 <input type=\"submit\" class=\"admin_submit submit\" name=\"finalize\" value=\"{--NEXT_PAGE--}\" class=\"install_submit\" />
323 <tr><td colspan=\"2\" height=\"21\" class=\"seperator\"> </td></tr>
328 case "finalize": // Write captured data to files
329 if ((!empty($_POST['finalize'])) && (!isBooleanConstantAndTrue('mxchange_installed'))) {
330 // You have submitted data then we have to reset the fatal messages
333 // Connect to MySQL server
334 $link = SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
336 // Seems to work, also right database?
337 $db = SQL_SELECT_DB($mysql['dbase'], $link, __FILE__, __LINE__);
339 // Automatically run install.sql
340 if ((FILE_READABLE($_POST['spath']."install/tables.sql")) && (FILE_READABLE($_POST['spath']."install/menu-".GET_LANGUAGE().".sql"))) {
341 // Both exists so import them
342 foreach (array("tables.sql", "menu-".GET_LANGUAGE().".sql") as $dump) {
343 // Should be save here because file_exists() is there but we check it again. :)
344 $file = secureString($_POST['spath']) . "install/" . $dump;
345 if (FILE_READABLE($file)) {
347 $SQLs = READ_FILE($file, true);
349 // And split it up against ;\n ...
350 $SQLs = merge_array($SQLs, explode(";\n", $sql));
354 // Are some SQLs found?
355 if (count($SQLs) == 0) {
357 addFatalMessage(getMessage('INSTALL_SQL_IMPORT_FAILED'));
361 // Now run all queries through and try to keep out empty or comment queries
362 foreach ($SQLs as $sql) {
366 // Is this query not empty and not a comment?
367 if ((!empty($sql)) && (substr($sql, 0, 2) != "--") && (substr($sql, 0, 1) != "#")) {
369 SQL_QUERY($sql, __FILE__, __LINE__);
373 // Ok, all done. So we can write the config data to the php files
374 if ($_POST['spath'] != constant('PATH')) install_WriteData($_POST['spath']."inc/config.php", "SERVER-PATH", "define('PATH', \"", "\");", $_POST['spath'], 0);
375 if ($_POST['burl'] != constant('URL')) install_WriteData($_POST['spath']."inc/config.php", "HOST-URL", "define('URL', \"", "\");", $_POST['burl'], 0);
376 install_WriteData($_POST['spath']."inc/config.php", "MAIN_TITLE", "define('MAIN_TITLE', \"", "\");", $_POST['title'], 0);
377 install_WriteData($_POST['spath']."inc/config.php", "SLOGAN", "define('SLOGAN', \"", "\");", $_POST['slogan'], 0);
378 install_WriteData($_POST['spath']."inc/config.php", "WEBMASTER", "define('WEBMASTER', \"", "\");", $_POST['email'], 0);
379 install_WriteData($_POST['spath']."inc/config.php", "NULLPASS-WARNING", "define('warn_no_pass', ", ");", $_POST['warn_no_pass'], 0);
380 install_WriteData($_POST['spath']."inc/config.php", "WRITE-FOOTER", "define('WRITE_FOOTER', ", ");", $_POST['wfooter'], 0);
381 install_WriteData($_POST['spath']."inc/config.php", "BACKLINK", "define('ENABLE_BACKLINK', ", ");", $_POST['blink'], 0);
382 // install_WriteData($_POST['spath']."inc/config.php", "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", $_POST['omode'], 0);
383 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-HOST", " 'host' => \"", "\",", $mysql['host'], 0);
384 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-DBASE", " 'dbase' => \"", "\",", $mysql['dbase'], 0);
385 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-LOGIN", " 'login' => \"", "\",", $mysql['login'], 0);
386 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PASSWORD", " 'password' => \"", "\",", $mysql['pass1'], 0);
387 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PREFIX", "define('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
388 install_WriteData($_POST['spath']."inc/config.php", "SMTP-HOSTNAME", "define('SMTP_HOSTNAME', \"", "\");", $_POST['smtp_host'], 0);
389 install_WriteData($_POST['spath']."inc/config.php", "SMTP-USER", "define('SMTP_USER', \"", "\");", $_POST['smtp_user'], 0);
390 install_WriteData($_POST['spath']."inc/config.php", "SMTP-PASSWORD", "define('SMTP_PASSWORD', \"", "\");", $_POST['smtp_pass'], 0);
391 install_WriteData($_POST['spath']."inc/config.php", "INSTALLED", "define('mxchange_installed', ", ");", "true", 0);
393 // Installation area not found!
394 addFatalMessage(getMessage('INSTALL_MISSING_DUMPS'));
399 if (getTotalFatalErrors() > 0) {
401 foreach ($FATAL as $value) {
402 $OUT .= " <li>".$value."</li>\n";
404 define('__FATAL_ERROR_LI', $OUT);
406 foreach ($mysql as $key => $value) {
407 $OUT .= " <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
409 define('__MYSQL_DATA' , $OUT);
410 define('__SPATH_VALUE' , $_POST['spath']);
411 define('__BURL_VALUE' , $_POST['burl']);
412 define('__TITLE_VALUE' , $_POST['title']);
413 define('__SMTP_HOST' , $_POST['smtp_host']);
414 define('__SMTP_USER' , $_POST['smtp_user']);
415 define('__SMTP_PASS' , $_POST['smtp_pass']);
418 LOAD_TEMPLATE("install_fatal_errors");
420 // Installation is done!
421 LOAD_URL($burl."/install.php?page=finalize");
423 } elseif (isBooleanConstantAndTrue('mxchange_installed')) {
424 // Redirection after writing data... :-)
425 LOAD_TEMPLATE("install_finished");
427 // Something goes wrong during installation! :-(
428 addFatalMessage(getMessage('INSTALL_FINALIZER_FAILED'));
429 LOAD_INC("inc/fatal_errors.php");
434 DEBUG_LOG(__FILE__, __LINE__, sprintf("Wrong page %s detected", $_GET['page']));
435 OUTPUT_HTML(" <div class=\"install_error\">{--WRONG_PAGE--}</strong>");
439 // Already installed!
440 addFatalMessage(getMessage('ALREADY_INSTALLED'));