branch prepared
[mailer.git] / inc / install-inc.php
1 <?php
2 /************************************************************************
3  * MXChange v0.1.0                                    Start: 08/29/2003 *
4  * ===============                              Last change: 11/11/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : install-inc.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for installation procedure             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die Installationsroutine         *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 /************************************************************************
35  *      YOU MAY ALSO WANT TO REMOVE THIS FILE AFTER INSTALLTION!        *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
40 {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
42         require($INC);
43 }
44
45 //
46 function install_WriteData ($FILE, $COMMENT, $PREFIX, $SUFFIX, $DATA, $SNEAK=0)
47 {
48         $DONE = false;  $SNEAK++;
49         if (file_exists($FILE))
50         {
51                 $SEARCH = "CFG: ".$COMMENT;
52                 $TMP = $FILE.".tmp";
53                 $fp = fopen($FILE, 'r') or OUTPUT_HTML ("<STRONG>READ:</STRONG> ".$FILE."<BR>");
54                 if ($fp)
55                 {
56                         $fp_tmp = fopen($TMP, 'w') or OUTPUT_HTML ("<STRONG>WRITE:</STRONG> ".$TMP."<BR>");
57                         if ($fp_tmp)
58                         {
59                                 while (! feof($fp))
60                                 {
61                                         $line = fgets ($fp, 10240);
62                                         if (strpos($line, $SEARCH) > -1) $next = 0;
63                                         if ($next > -1)
64                                         {
65                                                 if ($next == $SNEAK)
66                                                 {
67                                                         $next = -1;
68                                                         $line = $PREFIX.$DATA.$SUFFIX."\n";
69                                                 }
70                                                  else
71                                                 {
72                                                         $next++;
73                                                 }
74                                         }
75                                         fputs($fp_tmp, $line);
76                                 }
77                                 fclose($fp_tmp);
78                                 // Finished writing tmp file
79                                 $DONE = true;
80                         }
81                         fclose($fp);
82                         if ($DONE)
83                         {
84                                 // Copy back tmp file and delete tmp :-)
85                                 @copy($TMP, $FILE);
86                                 @unlink($TMP);
87                         }
88                          else
89                         {
90                                 OUTPUT_HTML ("<STRONG>TMP:</STRONG> UNDONE!");
91                         }
92                 }
93         }
94          else
95         {
96                 OUTPUT_HTML ("<STRONG>404:</STRONG> ".$FILE."<BR>");
97         }
98 }
99
100 //
101 $mysql = "";
102 if (isset($_POST['mysql']) && is_array($_POST['mysql'])) $mysql = $_POST['mysql'];
103
104 if (URL != "http://your-server.com")
105 {
106         // Already installed???
107         $burl = URL;
108 }
109  elseif ($burl == "")
110 {
111         // Auto-detect URL
112         $burl = "http://".$SERVER_NAME.$_SERVER['PHP_SELF'];
113         $burl = substr($burl, 0, strpos($burl, "install.php"));
114 }
115 //
116 if ((!mxchange_installed) || (!admin_registered))
117 {
118         // Set URL for FORM actions
119         define('__BURL_ACTION', $burl);
120
121         // Output page for entered value
122         switch ($_GET['page'])
123         {
124         case "welcome": // Welcome to the installation!
125                 LOAD_TEMPLATE("install_welcome");
126                 break;
127
128         case "1": // Server path, base URL
129                 define('__PATH_LEN'     , strlen(PATH));
130                 define('__BURL_LEN'     , strlen($burl));
131                 define('__TITLE_LEN'    , strlen(MAIN_TITLE));
132                 define('__SLOGAN_LEN'   , strlen(SLOGAN));
133                 define('__WEBMASTER_LEN', (strlen(WEBMASTER) * 3));
134
135                 // Load template
136                 LOAD_TEMPLATE("install_page1");
137                 break;
138
139         case "2": // MySQL data (alone!)
140                 if (empty($mysql['dbase']))  $mysql['dbase']  = "your_database";
141                 if (empty($mysql['login']))  $mysql['login']  = "your_login";
142                 if (empty($mysql['host']))   $mysql['host']   = "localhost";
143                 if (empty($mysql['prefix'])) $mysql['prefix'] = "mxchange_";
144                 if ((sizeof($FATAL) > 0) || (!empty($FATAL[0])))
145                 {
146                         OUTPUT_HTML ("<SPAN class=\"install_fatal\">");
147                         foreach ($FATAL as $key=>$err)
148                         {
149                                 OUTPUT_HTML ("  <STRONG>&middot;</STRONG>&nbsp;".FATAL_NO.($key + 1).":&nbsp;".$err."<BR>\n");
150                         }
151                         OUTPUT_HTML ("</SPAN><BR>");
152                 }
153                 define('__MYSQL_HOST'  , $mysql['host']);
154                 define('__MYSQL_DBASE' , $mysql['dbase']);
155                 define('__MYSQL_PREFIX', $mysql['prefix']);
156                 define('__MYSQL_LOGIN' , $mysql['login']);
157                 define('__SPATH_VALUE' , $_POST['spath']);
158                 define('__BURL_VALUE'  , $_POST['burl']);
159                 define('__TITLE_VALUE' , $_POST['title']);
160                 define('__SLOGAN_VALUE', $_POST['slogan']);
161                 define('__EMAIL_VALUE' , $_POST['email']);
162
163                 // Load template
164                 LOAD_TEMPLATE("install_page2");
165                 break;
166
167         case "5": // Misc settings
168                 define('__SPATH_VALUE' , $_POST['spath']);
169                 define('__BURL_VALUE'  , $_POST['burl']);
170                 define('__TITLE_VALUE' , $_POST['title']);
171                 define('__SLOGAN_VALUE', $_POST['slogan']);
172                 define('__EMAIL_VALUE' , $_POST['email']);
173                 OUTPUT_HTML ("<FORM action=\"".__BURL_ACTION."/install.php?page=finalize\" method=\"POST\" target=\"_self\">
174 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
175 <TR>
176   <TD colspan=\"2\" align=\"center\">
177     <STRONG class=\"install\">".HEADER_TEXT_PAGE5."</STRONG>
178   </TD>
179 </TR>
180 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
181 <TR>
182   <TD colspan=\"2\"><SPAN class=\"install_intro\">
183     ".TEXT_PAGE_5."
184   </SPAN></TD>
185 </TR>
186 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
187 <TR>
188   <TD class=\"install\" align=\"right\">".LANG_OUTPUT_MODE.":&nbsp;&nbsp;</TD>
189   <TD>
190     <SELECT name=\"omode\" size=\"1\" class=\"install_select\">
191       <OPTION value=\"render\"");
192                 if (OUTPUT_MODE == "render") OUTPUT_HTML (" selected=\"selected\"");
193                 OUTPUT_HTML (">".MODE_RENDER."</OPTION>
194       <OPTION value=\"direct\"");
195                 if (OUTPUT_MODE == "direct") OUTPUT_HTML (" selected=\"selected\"");
196                 OUTPUT_HTML (">".MODE_DIRECT."</OPTION>
197     </SELECT>
198   </TD>
199 </TR>
200 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
201 <TR>
202   <TD class=\"install\" align=\"right\">".WARN_NO_PASSWORD.":&nbsp;&nbsp;</TD>
203   <TD>
204     <SELECT name=\"warn_no_pass\" size=\"1\" class=\"install_select\">
205       <OPTION value=\"true\"");
206                 if (warn_no_pass) OUTPUT_HTML (" selected=\"selected\"");
207                 OUTPUT_HTML (">".YES."</OPTION>
208       <OPTION value=\"false\"");
209                 if (!warn_no_pass) OUTPUT_HTML (" selected=\"selected\"");
210                 OUTPUT_HTML (">".NO."</OPTION>
211     </SELECT>
212   </TD>
213 </TR>
214 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
215 <TR>
216   <TD class=\"install\" align=\"right\">".LANG_WRITE_FOOTER.":&nbsp;&nbsp;</TD>
217   <TD>
218     <SELECT name=\"wfooter\" size=\"1\" class=\"install_select\">
219       <OPTION value=\"true\"");
220                 if (WRITE_FOOTER) OUTPUT_HTML (" selected=\"selected\"");
221                 OUTPUT_HTML (">".YES."</OPTION>
222       <OPTION value=\"false\"");
223                 if (!WRITE_FOOTER) OUTPUT_HTML (" selected=\"selected\"");
224                 OUTPUT_HTML (">".NO."</OPTION>
225     </SELECT>
226   </TD>
227 </TR>
228 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
229 <TR>
230   <TD class=\"install\" align=\"right\">".LANG_FRAMESET_ACTIVE.":&nbsp;&nbsp;</TD>
231   <TD>
232     <SELECT name=\"frameset\" size=\"1\" class=\"install_select\">
233       <OPTION value=\"true\"");
234                 if (frameset_active) OUTPUT_HTML (" selected=\"selected\"");
235                 OUTPUT_HTML (">".YES."</OPTION>
236       <OPTION value=\"false\"");
237                 if (!frameset_active) OUTPUT_HTML (" selected=\"selected\"");
238                 OUTPUT_HTML (">".NO."</OPTION>
239     </SELECT>
240   </TD>
241 </TR>
242 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
243 <TR>
244   <TD class=\"install\" align=\"right\">".INSTALL_ENABLE_BACKLINK.":&nbsp;&nbsp;</TD>
245   <TD>
246     <SELECT name=\"blink\" size=\"1\" class=\"install_select\">
247       <OPTION value=\"true\"");
248                 if (ENABLE_BACKLINK) OUTPUT_HTML (" selected=\"selected\"");
249                 OUTPUT_HTML (">".YES."</OPTION>
250       <OPTION value=\"false\"");
251                 if (!ENABLE_BACKLINK) OUTPUT_HTML (" selected=\"selected\"");
252                 OUTPUT_HTML (">".NO."</OPTION>
253     </SELECT>
254   </TD>
255 </TR>
256 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
257 <TR>
258   <TD colspan=\"2\" align=\"center\">");
259                 foreach ($mysql as $key=>$value)
260                 {
261                         OUTPUT_HTML ("    <INPUT type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">");
262                 }
263                 OUTPUT_HTML ("    <INPUT type=\"hidden\" name=\"spath\" value=\"".__SPATH_VALUE."\">
264     <INPUT type=\"hidden\" name=\"burl\" value=\"".__BURL_VALUE."\">
265     <INPUT type=\"hidden\" name=\"title\" value=\"".__TITLE_VALUE."\">
266     <INPUT type=\"hidden\" name=\"slogan\" value=\"".__SLOGAN_VALUE."\">
267     <INPUT type=\"hidden\" name=\"email\" value=\"".__EMAIL_VALUE."\">
268     <INPUT type=\"submit\" class=\"admin_submit submit\" name=\"finalize\" value=\"".NEXT_PAGE."\" class=\"install_submit\">
269   </TD>
270 </TR>
271 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
272 </TABLE>
273 </FORM>");
274                 break;
275
276         case "finalize": // Write captured data to files
277                 if (!empty($_POST['finalize']))
278                 {
279                         // You have submitted data then we have to reset the fatal messages
280                         $FATAL = array();
281
282                         // Connect to MySQL server
283                         $link = SQL_CONNECT($mysql['host'], $mysql['login'], $mysql['pass1'], __FILE__, __LINE__);
284                         if ($link)
285                         {
286                                 // Seems to work, also right database?
287                                 $db = SQL_SELECT_DB($mysql['dbase'], $link, __FILE__, __LINE__);
288                                 if ($db)
289                                 {
290                                         // Automatically run install.sql
291                                         if (file_exists($_POST['spath']."install/tables.sql"))
292                                         {
293                                                 $ERRORS = $_POST['spath']."inc/sql.err";
294                                                 $cmd = array(
295                                                         // "Dump" tha' s*** up... ;-) > /dev/null
296                                                         "mysql ".$mysql['dbase']." --force --host=".$mysql['host']." --user=".$mysql['login']." --password=".$mysql['pass1']." < ".$_POST['spath']."install/tables.sql",
297                                                         "mysql ".$mysql['dbase']." --force --host=".$mysql['host']." --user=".$mysql['login']." --password=".$mysql['pass1']." < ".$_POST['spath']."install/menu-".GET_LANGUAGE().".sql"
298                                                 );
299
300                                                 foreach ($cmd as $c)
301                                                 {
302                                                         @system($c);
303                                                 }
304
305                                                 if ((file_exists($ERRORS)) && (filesize($ERRORS) > 0))
306                                                 {
307                                                         ADD_FATAL(MYSQLDUMP_ERROR);
308                                                 }
309                                                  elseif (!mxchange_installed)
310                                                 {
311                                                         // Remove error file if empty
312                                                         if (file_exists($ERRORS)) @unlink ($ERRORS);
313
314                                                         // Ok, all done. So we can write the config data to the php files
315                                                         if ($_POST['spath'] != PATH) install_WriteData($_POST['spath']."inc/config.php", "SERVER-PATH", "define ('PATH', \"", "\");", $_POST['spath'], 0);
316                                                         if ($_POST['burl']  != URL)  install_WriteData($_POST['spath']."inc/config.php", "HOST-URL", "define ('URL', \"", "\");", $_POST['burl'], 0);
317                                                         install_WriteData($_POST['spath']."inc/config.php", "MAIN_TITLE", "define ('MAIN_TITLE', \"", "\");", $_POST['title'], 0);
318                                                         install_WriteData($_POST['spath']."inc/config.php", "SLOGAN", "define ('SLOGAN', \"", "\");", $_POST['slogan'], 0);
319                                                         install_WriteData($_POST['spath']."inc/config.php", "WEBMASTER", "define ('WEBMASTER', \"", "\");", $_POST['email'], 0);
320                                                         install_WriteData($_POST['spath']."inc/config.php", "NULLPASS-WARNING", "define ('warn_no_pass', ", ");", $_POST['warn_no_pass'], 0);
321                                                         install_WriteData($_POST['spath']."inc/config.php", "WRITE-FOOTER", "define ('WRITE_FOOTER', ", ");", $_POST['wfooter'], 0);
322                                                         install_WriteData($_POST['spath']."inc/config.php", "BACKLINK", "define ('ENABLED_BACKLINK', ", ");", $_POST['blink'], 0);
323                                                         // install_WriteData($_POST['spath']."inc/config.php", "FRAMESET", "define ('frameset_active', ", ");", $_POST['frameset'], 0);
324                                                         // install_WriteData($_POST['spath']."inc/config.php", "OUTPUT-MODE", "define ('OUTPUT_MODE', \"", "\");", $_POST['omode'], 0);
325                                                         install_WriteData($_POST['spath']."inc/config.php", "MYSQL-HOST", "     'host'     => \"", "\",", $mysql['host'], 0);
326                                                         install_WriteData($_POST['spath']."inc/config.php", "MYSQL-DBASE", "    'dbase'    => \"", "\",", $mysql['dbase'], 0);
327                                                         install_WriteData($_POST['spath']."inc/config.php", "MYSQL-LOGIN", "    'login'    => \"", "\",", $mysql['login'], 0);
328                                                         install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PASSWORD", " 'password' => \"", "\",", $mysql['pass1'], 0);
329                                                         install_WriteData($_POST['spath']."inc/config.php", "MYSQL-PREFIX", "define ('_MYSQL_PREFIX', \"", "\");", $mysql['prefix'], 0);
330                                                         install_WriteData($_POST['spath']."inc/config.php", "INSTALLED", "define ('mxchange_installed', ", ");", "true", 0);
331
332                                                         // Close the link
333                                                         SQL_CLOSE($link, __FILE__, __LINE__);
334                                                 }
335                                                  else
336                                                 {
337                                                         ADD_FATAL(INSTALL_MAYBE_DONE);
338                                                 }
339                                         }
340                                          else
341                                         {
342                                                 // Installation area not found!
343                                                 ADD_FATAL(INSTALL_MISSING_DUMPS);
344                                         }
345                                 }
346                         }
347                         if ((sizeof($FATAL) > 0) || ($FATAL[0] != ""))
348                         {
349                                 $OUT = "";
350                                 foreach ($FATAL as $value)
351                                 {
352                                         $OUT .= "    <LI>".$value."</LI>\n";
353                                 }
354                                 define('__FATAL_ERROR_LI', $OUT);
355                                 $OUT = "";
356                                 foreach ($mysql as $key=>$value)
357                                 {
358                                         $OUT .= "    <INPUT type=\"hidden\" name=\"mysql[".$key."]\" value=\"".$value."\">\n";
359                                 }
360                                 define('__MYSQL_DATA' , $OUT);
361                                 define('__SPATH_VALUE', $_POST['spath']);
362                                 define('__BURL_VALUE' , $_POST['burl']);
363                                 define('__TITLE_VALUE', $_POST['title']);
364
365                                 OUTPUT_HTML ("<FORM action=\"".__BURL_ACTION."/install.php?page=2\" method=\"POST\">
366 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"install_content\">
367 <TR>
368   <TD align=\"center\">
369     <STRONG class=\"install\">".HEADER_MYSQL_ERRORS."</STRONG>
370   </TD>
371 </TR>
372 <TR><TD colspan=\"2\" height=\"21\" class=\"seperator\">&nbsp;</TD></TR>
373 <TR>
374   <TD><SPAN class=\"install_intro\">
375     ".TEXT_MYSQL_ERRORS."
376   </SPAN></TD>
377 </TR>
378 <TR>
379   <TD align=\"center\"><STRONG>
380     <OL>
381 ".__FATAL_ERROR_LI."
382     </OL>
383 ".__MYSQL_DATA."
384     <INPUT type=\"hidden\" name=\"spath\" value=\"".__SPATH_VALUE."\">
385     <INPUT type=\"hidden\" name=\"burl\" value=\"".__BURL_VALUE."\">
386     <INPUT type=\"hidden\" name=\"title\" value=\"".__TITLE_VALUE."\">
387     <INPUT type=\"submit\" class=\"admin_submit\" name=\"ok\" value=\"".RETURN_MYSQL_PAGE."\">
388   </STRONG></TD>
389 </TR>
390 </TABLE>
391 </FORM>");
392                         }
393                          else
394                         {
395                                 // Installation is done!
396                                 $URL = $burl."/install.php?page=finalize";
397                                 LOAD_URL($URL);
398                         }
399                 }
400                  elseif (mxchange_installed)
401                 {
402                         // Redirection after writing data... :-)
403                         OUTPUT_HTML ("<SPAN class=\"install_finished\">".INSTALL_FINISHED."</SPAN><BR>
404 <BR>
405 <SPAN class=\"install_security\">".INSTALL_FIN_SECU."</SPAN><BR>
406 <BR>
407 <FORM action=\"modules.php\" method=\"GET\">
408   <INPUT type=\"hidden\" name=\"module\" value=\"admin\">
409   <INPUT type=\"submit\" class=\"admin_submit submit\" value=\"".CONTINUE_ADMIN."\" class=\"install_submit\">
410 </FORM>");
411                 }
412                  else
413                 {
414                         // Something goes wrong during installation! :-(
415                         ADD_FATAL(INSTALL_FINALIZER_FAILED);
416                         include ("inc/fatal_errors.php");
417                 }
418                 break;
419
420         default:
421                 OUTPUT_HTML ("    <STRONG class=\"install_error\">".WRONG_PAGE."</STRONG>");
422                 break;
423         }
424 }
425  else
426 {
427         ADD_FATAL(ALREADY_INSTALLED);
428 }
429 //
430 ?>