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