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 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 (getTotalFatalErrors() > 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 = constant('SMTP_HOSTNAME');
198 $smtpUser = constant('SMTP_USER');
199 $smtpPass1 = constant('SMTP_PASSWORD');
200 $smtpPass2 = constant('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 <div 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 (constant('OUTPUT_MODE') == "render") OUTPUT_HTML(" selected=\"selected\"");
262 OUTPUT_HTML(">{!MODE_RENDER!}</option>
263 <option value=\"direct\"");
264 if (constant('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 (isBooleanConstantAndTrue('ENABLE_BACKLINK')) OUTPUT_HTML(" selected=\"selected\"");
304 OUTPUT_HTML(">{!YES!}</option>
305 <option value=\"false\"");
306 if (!isBooleanConstantAndTrue('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
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 $SQLs = READ_FILE($file, true);
354 // And split it up against ;\n ...
355 $SQLs = merge_array($SQLs, explode(";\n", $sql));
359 // Are some SQLs found?
360 if (count($SQLs) == 0) {
362 addFatalMessage(getMessage('INSTALL_SQL_IMPORT_FAILED'));
366 // Now run all queries through and try to keep out empty or comment queries
367 foreach ($SQLs as $sql) {
371 // Is this query not empty and not a comment?
372 if ((!empty($sql)) && (substr($sql, 0, 2) != "--") && (substr($sql, 0, 1) != "#")) {
374 SQL_QUERY($sql, __FILE__, __LINE__);
378 // Ok, all done. So we can write the config data to the php files
379 if ($_POST['spath'] != constant('PATH')) install_WriteData($_POST['spath']."inc/config.php", "SERVER-PATH", "define('PATH', \"", "\");", $_POST['spath'], 0);
380 if ($_POST['burl'] != constant('URL')) install_WriteData($_POST['spath']."inc/config.php", "HOST-URL", "define('URL', \"", "\");", $_POST['burl'], 0);
381 install_WriteData($_POST['spath']."inc/config.php", "MAIN_TITLE", "define('MAIN_TITLE', \"", "\");", $_POST['title'], 0);
382 install_WriteData($_POST['spath']."inc/config.php", "SLOGAN", "define('SLOGAN', \"", "\");", $_POST['slogan'], 0);
383 install_WriteData($_POST['spath']."inc/config.php", "WEBMASTER", "define('WEBMASTER', \"", "\");", $_POST['email'], 0);
384 install_WriteData($_POST['spath']."inc/config.php", "NULLPASS-WARNING", "define('warn_no_pass', ", ");", $_POST['warn_no_pass'], 0);
385 install_WriteData($_POST['spath']."inc/config.php", "WRITE-FOOTER", "define('WRITE_FOOTER', ", ");", $_POST['wfooter'], 0);
386 install_WriteData($_POST['spath']."inc/config.php", "BACKLINK", "define('ENABLE_BACKLINK', ", ");", $_POST['blink'], 0);
387 // install_WriteData($_POST['spath']."inc/config.php", "OUTPUT-MODE", "define('OUTPUT_MODE', \"", "\");", $_POST['omode'], 0);
388 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-HOST", " 'host' => \"", "\",", $mysql['host'], 0);
389 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-DBASE", " 'dbase' => \"", "\",", $mysql['dbase'], 0);
390 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-LOGIN", " 'login' => \"", "\",", $mysql['login'], 0);
391 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PASSWORD", " 'password' => \"", "\",", $mysql['pass1'], 0);
392 install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PREFIX", "define('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
393 install_WriteData($_POST['spath']."inc/config.php", "SMTP-HOSTNAME", "define('SMTP_HOSTNAME', \"", "\");", $_POST['smtp_host'], 0);
394 install_WriteData($_POST['spath']."inc/config.php", "SMTP-USER", "define('SMTP_USER', \"", "\");", $_POST['smtp_user'], 0);
395 install_WriteData($_POST['spath']."inc/config.php", "SMTP-PASSWORD", "define('SMTP_PASSWORD', \"", "\");", $_POST['smtp_pass'], 0);
396 install_WriteData($_POST['spath']."inc/config.php", "INSTALLED", "define('mxchange_installed', ", ");", "true", 0);
398 // Installation area not found!
399 addFatalMessage(getMessage('INSTALL_MISSING_DUMPS'));
404 if (getTotalFatalErrors() > 0) {
406 foreach ($FATAL as $value) {
407 $OUT .= " <li>".$value."</li>\n";
409 define('__FATAL_ERROR_LI', $OUT);
411 foreach ($mysql as $key => $value) {
412 $OUT .= " <input type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
414 define('__MYSQL_DATA' , $OUT);
415 define('__SPATH_VALUE' , $_POST['spath']);
416 define('__BURL_VALUE' , $_POST['burl']);
417 define('__TITLE_VALUE' , $_POST['title']);
418 define('__SMTP_HOST' , $_POST['smtp_host']);
419 define('__SMTP_USER' , $_POST['smtp_user']);
420 define('__SMTP_PASS' , $_POST['smtp_pass']);
423 LOAD_TEMPLATE("install_fatal_errors");
425 // Installation is done!
426 LOAD_URL($burl."/install.php?page=finalize");
428 } elseif (isBooleanConstantAndTrue('mxchange_installed')) {
429 // Redirection after writing data... :-)
430 LOAD_TEMPLATE("install_finished");
432 // Something goes wrong during installation! :-(
433 addFatalMessage(getMessage('INSTALL_FINALIZER_FAILED'));
434 LOAD_INC("inc/fatal_errors.php");
439 DEBUG_LOG(__FILE__, __LINE__, sprintf("Wrong page %s detected!}, $_GET['page']));
440 OUTPUT_HTML(" <div class=\"install_error\">{!WRONG_PAGE!}</strong>");
444 addFatalMessage(ALREADY_INSTALLED);