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