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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
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???
107 } elseif ($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(INSTALL_SMTP_HOSTNAME_EMPTY."<br />");
121 if ((empty($_POST['smtp_pass1'])) && (!empty($_POST['smtp_pass2']))) {
123 OUTPUT_HTML(INSTALL_SMTP_PASS1_EMPTY."<br />");
126 if ((!empty($_POST['smtp_pass1'])) && (empty($_POST['smtp_pass2']))) {
127 // Password repeat is empty
128 OUTPUT_HTML(INSTALL_SMTP_PASS2_EMPTY."<br />");
131 if ($_POST['smtp_pass1'] != $_POST['smtp_pass1']) {
132 // Passwords are not matching
133 OUTPUT_HTML(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 define('__PATH_LEN' , strlen(PATH));
153 define('__BURL_LEN' , strlen($burl));
154 define('__TITLE_LEN' , strlen(MAIN_TITLE));
155 define('__SLOGAN_LEN' , strlen(SLOGAN));
156 define('__WEBMASTER_LEN', (strlen(WEBMASTER) * 3));
159 LOAD_TEMPLATE("install_page1");
162 case "2": // MySQL data (alone!)
163 if (empty($mysql['dbase'])) $mysql['dbase'] = "your_database";
164 if (empty($mysql['login'])) $mysql['login'] = "your_login";
165 if (empty($mysql['host'])) $mysql['host'] = "localhost";
166 if (empty($mysql['prefix'])) $mysql['prefix'] = "mxchange_";
167 if ((sizeof($FATAL) > 0) || (!empty($FATAL[0]))) {
168 OUTPUT_HTML("<SPAN class=\"install_fatal\">");
169 foreach ($FATAL as $key => $err) {
170 OUTPUT_HTML(" <STRONG>·</STRONG> ".FATAL_NO.($key + 1).": ".$err."<br />");
172 OUTPUT_HTML("</SPAN><br />");
174 define('__MYSQL_HOST' , $mysql['host']);
175 define('__MYSQL_DBASE' , $mysql['dbase']);
176 define('__MYSQL_PREFIX', $mysql['prefix']);
177 define('__MYSQL_LOGIN' , $mysql['login']);
178 define('__SPATH_VALUE' , $_POST['spath']);
179 define('__BURL_VALUE' , $_POST['burl']);
180 define('__TITLE_VALUE' , $_POST['title']);
181 define('__SLOGAN_VALUE', $_POST['slogan']);
182 define('__EMAIL_VALUE' , $_POST['email']);
185 LOAD_TEMPLATE("install_page2");
190 define('__SPATH_VALUE' , $_POST['spath']);
191 define('__BURL_VALUE' , $_POST['burl']);
192 define('__TITLE_VALUE' , $_POST['title']);
193 define('__SLOGAN_VALUE' , $_POST['slogan']);
194 define('__EMAIL_VALUE' , $_POST['email']);
196 // Use default SMTP data
197 $smtpHost = SMTP_HOSTNAME;
198 $smtpUser = SMTP_USER;
199 $smtpPass1 = SMTP_PASSWORD;
200 $smtpPass2 = SMTP_PASSWORD;
202 // Overwrite it with the data from sent (failed) form
203 if (!empty($_POST['smtp_host'])) $smtpHost = $_POST['smtp_host'];
204 if (!empty($_POST['smtp_user'])) $smtpUser = $_POST['smtp_user'];
207 define('__MYSQL_HOST' , $mysql['host']);
208 define('__MYSQL_DBASE' , $mysql['dbase']);
209 define('__MYSQL_PREFIX' , $mysql['prefix']);
210 define('__MYSQL_LOGIN' , $mysql['login']);
211 define('__MYSQL_PASS1' , $mysql['pass1']);
212 define('__MYSQL_PASS2' , $mysql['pass2']);
214 // Set constants for SMTP data
215 define('__SMTP_HOST' , $smtpHost);
216 define('__SMTP_USER' , $smtpUser);
217 define('__SMTP_PASS1' , $smtpPass1);
218 define('__SMTP_PASS2' , $smtpPass2);
221 LOAD_TEMPLATE("install_page3");
224 case "5": // Misc settings
226 define('__SPATH_VALUE' , $_POST['spath']);
227 define('__BURL_VALUE' , $_POST['burl']);
228 define('__TITLE_VALUE' , $_POST['title']);
229 define('__SLOGAN_VALUE' , $_POST['slogan']);
230 define('__EMAIL_VALUE' , $_POST['email']);
233 define('__MYSQL_HOST' , $mysql['host']);
234 define('__MYSQL_DBASE' , $mysql['dbase']);
235 define('__MYSQL_PREFIX' , $mysql['prefix']);
236 define('__MYSQL_LOGIN' , $mysql['login']);
239 define('__SMTP_HOST' , $_POST['smtp_host']);
240 define('__SMTP_USER' , $_POST['smtp_user']);
241 define('__SMTP_PASS' , $_POST['smtp_pass1']);
242 OUTPUT_HTML("<FORM action=\"".__BURL_ACTION."/install.php?page=finalize\" method=\"POST\" target=\"_self\">
243 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
245 <TD colspan=\"2\" align=\"center\">
246 <STRONG class=\"install\">".HEADER_TEXT_PAGE5."</STRONG>
249 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\"> </TD></TR>
251 <TD colspan=\"2\"><SPAN class=\"install_intro\">
255 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\"> </TD></TR>
257 <TD class=\"install\" align=\"right\">".LANG_OUTPUT_MODE.": </TD>
259 <SELECT name=\"omode\" size=\"1\" class=\"install_select\">
260 <OPTION value=\"render\"");
261 if (OUTPUT_MODE == "render") OUTPUT_HTML(" selected=\"selected\"");
262 OUTPUT_HTML(">".MODE_RENDER."</OPTION>
263 <OPTION value=\"direct\"");
264 if (OUTPUT_MODE == "direct") OUTPUT_HTML(" selected=\"selected\"");
265 OUTPUT_HTML(">".MODE_DIRECT."</OPTION>
269 <TR><TD colspan=\"2\" height=\"10\" class=\"seperator\"> </TD></TR>
271 <TD class=\"install\" align=\"right\">".WARN_NO_PASSWORD.": </TD>
273 <SELECT name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
274 <OPTION value=\"true\"");
275 if (isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
276 OUTPUT_HTML(">".YES."</OPTION>
277 <OPTION value=\"false\"");
278 if (!isBooleanConstantAndTrue('warn_no_pass')) OUTPUT_HTML(" selected=\"selected\"");
279 OUTPUT_HTML(">".NO."</OPTION>
283 <TR><TD colspan=\"2\" height=\"10\" class=\"seperator\"> </TD></TR>
285 <TD class=\"install\" align=\"right\">".LANG_WRITE_FOOTER.": </TD>
287 <SELECT name=\"wfooter\" size=\"1\" class=\"install_select\">
288 <OPTION value=\"true\"");
289 if (isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
290 OUTPUT_HTML(">".YES."</OPTION>
291 <OPTION value=\"false\"");
292 if (!isBooleanConstantAndTrue('WRITE_FOOTER')) OUTPUT_HTML(" selected=\"selected\"");
293 OUTPUT_HTML(">".NO."</OPTION>
297 <TR><TD colspan=\"2\" height=\"10\" class=\"seperator\"> </TD></TR>
299 <TD class=\"install\" align=\"right\">".INSTALL_ENABLE_BACKLINK.": </TD>
301 <SELECT name=\"blink\" size=\"1\" class=\"install_select\">
302 <OPTION value=\"true\"");
303 if (ENABLE_BACKLINK) OUTPUT_HTML(" selected=\"selected\"");
304 OUTPUT_HTML(">".YES."</OPTION>
305 <OPTION value=\"false\"");
306 if (!ENABLE_BACKLINK) OUTPUT_HTML(" selected=\"selected\"");
307 OUTPUT_HTML(">".NO."</OPTION>
311 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\"> </TD></TR>
313 <TD colspan=\"2\" align=\"center\">");
314 foreach ($mysql as $key => $value) {
315 OUTPUT_HTML(" <INPUT type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">");
317 OUTPUT_HTML(" <INPUT type=\"hidden\" name=\"spath\" value=\"".__SPATH_VALUE."\">
318 <INPUT type=\"hidden\" name=\"burl\" value=\"".__BURL_VALUE."\">
319 <INPUT type=\"hidden\" name=\"title\" value=\"".__TITLE_VALUE."\">
320 <INPUT type=\"hidden\" name=\"slogan\" value=\"".__SLOGAN_VALUE."\">
321 <INPUT type=\"hidden\" name=\"email\" value=\"".__EMAIL_VALUE."\">
322 <INPUT type=\"hidden\" name=\"smtp_host\" value=\"".__SMTP_HOST."\">
323 <INPUT type=\"hidden\" name=\"smtp_user\" value=\"".__SMTP_USER."\">
324 <INPUT type=\"hidden\" name=\"smtp_pass\" value=\"".__SMTP_PASS."\">
325 <INPUT type=\"submit\" class=\"admin_submit submit\" name=\"finalize\" value=\"".NEXT_PAGE."\" class=\"install_submit\">
328 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\"> </TD></TR>
333 case "finalize": // Write captured data to files
334 if ((!empty($_POST['finalize'])) && (!isBooleanConstantAndTrue('mxchange_installed'))) {
335 // You have submitted data then we have to reset the fatal messages
336 $FATAL = array(); $SQLs = array();
338 // Connect to MySQL server
339 $link = SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
341 // Seems to work, also right database?
342 $db = SQL_SELECT_DB($mysql['dbase'], $link, __FILE__, __LINE__);
344 // Automatically run install.sql
345 if ((FILE_READABLE($_POST['spath']."install/tables.sql")) && (FILE_READABLE($_POST['spath']."install/menu-".GET_LANGUAGE().".sql"))) {
346 // Both exists so import them
347 foreach (array("tables.sql", "menu-".GET_LANGUAGE().".sql") as $dump) {
348 // Should be save here because file_exists() is there but we check it again. :)
349 $file = secureString($_POST['spath']) . "install/" . $dump;
350 if (FILE_READABLE($file)) {
352 $sql = implode("", file($file));
354 // Remove some unwanted chars
355 $sql = str_replace("\r", "", $sql);
356 $sql = str_replace("\n\n", "\n", $sql);
358 // And split it up against ;\n ...
359 $SQLs = array_merge($SQLs, explode(";\n", $sql));
363 // Are some SQLs found?
364 if (count($SQLs) == 0) {
366 ADD_FATAL(INSTALL_SQL_IMPORT_FAILED);
370 // Now run all queries through and try to keep out empty or comment queries
371 foreach ($SQLs as $sql) {
375 // Is this query not empty and not a comment?
376 if ((!empty($sql)) && (substr($sql, 0, 2) != "--") && (substr($sql, 0, 1) != "#")) {
378 SQL_QUERY($sql, __FILE__, __LINE__);
382 // Ok, all done. So we can write the config data to the php files
383 if ($_POST['spath'] != PATH) install_WriteData($_POST['spath']."inc/config.php", "SERVER-PATH", "define('PATH', \"", "\");", $_POST['spath'], 0);
384 if ($_POST['burl'] != URL) install_WriteData($_POST['spath']."inc/config.php", "HOST-URL", "define('URL', \"", "\");", $_POST['burl'], 0);
385 install_WriteData($_POST['spath']."inc/config.php", "MAIN_TITLE", "define('MAIN_TITLE', \"", "\");", $_POST['title'], 0);
386 install_WriteData($_POST['spath']."inc/config.php", "SLOGAN", "define('SLOGAN', \"", "\");", $_POST['slogan'], 0);
387 install_WriteData($_POST['spath']."inc/config.php", "WEBMASTER", "define('WEBMASTER', \"", "\");", $_POST['email'], 0);
388 install_WriteData($_POST['spath']."inc/config.php", "NULLPASS-WARNING", "define('warn_no_pass', ", ");", $_POST['warn_no_pass'], 0);
389 install_WriteData($_POST['spath']."inc/config.php", "WRITE-FOOTER", "define('WRITE_FOOTER', ", ");", $_POST['wfooter'], 0);
390 install_WriteData($_POST['spath']."inc/config.php", "BACKLINK", "define('ENABLE_BACKLINK', ", ");", $_POST['blink'], 0);
391 // install_WriteData($_POST['spath']."inc/config.php", "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", $_POST['omode'], 0);
392 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-HOST", " 'host' => \"", "\",", $mysql['host'], 0);
393 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-DBASE", " 'dbase' => \"", "\",", $mysql['dbase'], 0);
394 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-LOGIN", " 'login' => \"", "\",", $mysql['login'], 0);
395 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PASSWORD", " 'password' => \"", "\",", $mysql['pass1'], 0);
396 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PREFIX", "define('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
397 install_WriteData($_POST['spath']."inc/config.php", "SMTP-HOSTNAME", "define('SMTP_HOSTNAME', \"", "\");", $_POST['smtp_host'], 0);
398 install_WriteData($_POST['spath']."inc/config.php", "SMTP-USER", "define('SMTP_USER', \"", "\");", $_POST['smtp_user'], 0);
399 install_WriteData($_POST['spath']."inc/config.php", "SMTP-PASSWORD", "define('SMTP_PASSWORD', \"", "\");", $_POST['smtp_pass'], 0);
400 install_WriteData($_POST['spath']."inc/config.php", "INSTALLED", "define('mxchange_installed', ", ");", "true", 0);
403 SQL_CLOSE($link, __FILE__, __LINE__);
407 // Installation area not found!
408 ADD_FATAL(INSTALL_MISSING_DUMPS);
412 if ((sizeof($FATAL) > 0) || ($FATAL[0] != ''))
415 foreach ($FATAL as $value)
417 $OUT .= " <LI>".$value."</LI>\n";
419 define('__FATAL_ERROR_LI', $OUT);
421 foreach ($mysql as $key => $value)
423 $OUT .= " <INPUT type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
425 define('__MYSQL_DATA' , $OUT);
426 define('__SPATH_VALUE' , $_POST['spath']);
427 define('__BURL_VALUE' , $_POST['burl']);
428 define('__TITLE_VALUE' , $_POST['title']);
429 define('__SMTP_HOST' , $_POST['smtp_host']);
430 define('__SMTP_USER' , $_POST['smtp_user']);
431 define('__SMTP_PASS' , $_POST['smtp_pass']);
433 OUTPUT_HTML("<FORM action=\"".__BURL_ACTION."/install.php?page=2\" method=\"POST\">
434 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
436 <TD align=\"center\">
437 <STRONG class=\"install\">".HEADER_MYSQL_ERRORS."</STRONG>
440 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\"> </TD></TR>
442 <TD><SPAN class=\"install_intro\">
443 ".TEXT_MYSQL_ERRORS."
447 <TD align=\"center\"><STRONG>
452 <INPUT type=\"hidden\" name=\"spath\" value=\"".__SPATH_VALUE."\">
453 <INPUT type=\"hidden\" name=\"burl\" value=\"".__BURL_VALUE."\">
454 <INPUT type=\"hidden\" name=\"title\" value=\"".__TITLE_VALUE."\">
455 <INPUT type=\"submit\" class=\"admin_submit\" name=\"ok\" value=\"".RETURN_MYSQL_PAGE."\">
463 // Installation is done!
464 $URL = $burl."/install.php?page=finalize";
468 elseif (isBooleanConstantAndTrue('mxchange_installed'))
470 // Redirection after writing data... :-)
471 LOAD_TEMPLATE("install_finished");
475 // Something goes wrong during installation! :-(
476 ADD_FATAL(INSTALL_FINALIZER_FAILED);
477 include ("inc/fatal_errors.php");
482 OUTPUT_HTML(" <STRONG class=\"install_error\">".WRONG_PAGE."</STRONG>");
488 ADD_FATAL(ALREADY_INSTALLED);