OSDN Git Service

CHANGE: インストールスクリプトの修正
[nucleus-jp/nucleus-next.git] / install / index.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2012 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  *
12  * This script will install the Nucleus tables in your SQL-database,
13  * and initialize the data in those tables.
14  */
15
16 /**
17  * @license http://nucleuscms.org/license.txt GNU General Public License
18  * @copyright Copyright (C) 2002-2012 The Nucleus Group
19  * @version $Id$
20  */
21
22 /* reporting all errors for support */
23 error_reporting(E_ALL);
24
25 $minimum_php_version    = '5.0.6';
26 $minimum_mysql_version  = '3.23';
27
28 $page_footer_copyright  = '&copy; 2001-2012 The Nucleus Groupe . Running Nucleus CMS v4.00';
29
30 // begin if: server's PHP version is below the minimum; halt installation
31 if ( version_compare(PHP_VERSION, $minimum_php_version, '<') )
32 {
33         exit('<div style="font-size: xx-large;"> Nucleus requires at least PHP version '. $minimum_php_version .'</div>');
34 }
35
36 // make sure there's no unnecessary escaping: # set_magic_quotes_runtime(0);
37 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
38 {
39         ini_set('magic_quotes_runtime', '0');
40 }
41
42 /* default installed plugins and skins */
43 $aConfPlugsToInstall    = array('NP_SecurityEnforcer', 'NP_SkinFiles', 'NP_Text');
44 $aConfSkinsToImport             = array('atom', 'rss2.0', 'rsd', 'default');
45
46 // Check if some important files
47 do_check_files();
48
49 /* i18n class is needed for internationalization */
50 include_once('../nucleus/libs/i18n.php');
51 if ( !i18n::init('UTF-8', './locales') )
52 {
53         exit('<div style="font-size: xx-large;"> Failed to initialize iconv or mbstring extension. Would you please contact the administrator of your PHP server? </div>');
54 }
55
56 // check if mysql support is installed; this check may not make sense, as is, in a version past 3.5x
57 if ( !function_exists('mysql_query') && !function_exists('mysqli_query') )
58 {
59         exit('<div style="font-size: xx-large;"> Your PHP version does not have support for MySQL :( </div>');
60 }
61
62 // include core classes that are needed for login & plugin handling
63 include_once('../nucleus/libs/mysql.php');
64
65 // added for 3.5 sql_* wrapper
66 global $MYSQL_HANDLER;
67
68 if ( !isset($MYSQL_HANDLER) )
69 {
70         $MYSQL_HANDLER = array('mysql', '');
71 }
72 include_once('../nucleus/libs/sql/' . $MYSQL_HANDLER[0] . '.php');
73
74 session_start();
75 if ( count($_GET) == 0 && count($_POST) == 0 )
76 {
77         unset($_SESSION['param_manager']);
78 }
79
80 // restore the $param from the session
81 if ( array_key_exists('param_manager', $_SESSION) )
82 {
83         $param = $_SESSION['param_manager'];
84 }
85 else
86 {
87         $param = new PARAM_MANAGER();
88 }
89
90 // include translation file
91 if ( array_key_exists('locale', $_POST) ) $param->set_locale();
92 i18n::set_current_locale($param->locale);
93 $translation_file = './locales/' . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php';
94 if ( !file_exists($translation_file) )
95 {
96         $translation_file = './locales/en_Latn_US.UTF-8.php';
97 }
98 include($translation_file);
99
100 do_action();
101
102 // $param is saved to the session
103 if ( isset($param) )
104 {
105         $_SESSION['param_manager'] = $param;
106 }
107 else
108 {
109         unset($_SESSION['param_manager']);
110 }
111 exit;
112
113
114 /**
115  * installer action
116  */
117 function do_action()
118 {
119         global $param;
120
121         if ( array_key_exists('action', $_POST) )
122         {
123                 $isPostback = true;
124         }
125         else
126         {
127                 $isPostback = false;
128         }
129
130         // mode change
131         if ( array_key_exists('mode', $_REQUEST) )
132         {
133                 if ( $_REQUEST['mode'] == 'detail' )
134                 {
135                         $param->set_state('detail');
136                 }
137                 elseif ( $_REQUEST['mode'] == 'simple' )
138                 {
139                         $param->set_state('mysql');
140                 }
141         }
142
143         // input parameter check
144         if ( $isPostback )
145         {
146                 switch ( $param->state )
147                 {
148                         case 'locale':
149                                 $param->set_locale();
150                                 $param->set_state('mysql');
151                                 $isPostback = false;
152                                 break;
153                         case 'mysql':
154                                 if ( count($param->check_mysql_parameters()) == 0 )
155                                 {
156                                         $param->set_state('weblog');
157                                         $isPostback = false;
158                                 }
159                                 break;
160                         case 'weblog':
161                                 if ( count($param->check_user_parameters()) == 0
162                                         && count($param->check_weblog_parameters()) == 0 )
163                                 {
164                                         $param->set_state('install');
165                                         $isPostback = false;
166                                 }
167                                 break;
168                         case 'detail':
169                                 if ( $param->check_all_parameters() )
170                                 {
171                                         $param->set_state('install');
172                                         $isPostback = false;
173                                 }
174                                 break;
175                 }
176         }
177
178         // page render
179         show_header();
180         switch ( $param->state )
181         {
182                 case 'locale':
183                         show_select_locale_form();
184                         break;
185                 case 'mysql':
186                         show_database_setting_form($isPostback);
187                         break;
188                 case 'weblog':
189                         show_blog_setting_form($isPostback);
190                         break;
191                 case 'detail':
192                         show_detail_setting_form($isPostback);
193                         break;
194                 case 'install':
195                         show_install_complete_form();
196                         break;
197         }
198         show_footer();
199 }
200
201 /**
202  * header tag of the installation screens
203  **/
204 function show_header()
205 {
206         global $param;
207
208         /* HTTP 1.1 application for no caching */
209         header("Cache-Control: no-cache, must-revalidate");
210         header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
211         header('Content-Type: text/html; charset=' . i18n::get_current_charset());
212
213 ?>
214 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
215 <html xmlns="http://www.w3.org/1999/xhtml">
216         <head>
217                 <title><?php echo _TITLE; ?></title>
218                 <link rel="stylesheet" type="text/css" href="./styles/inst.css" />
219                 <style type="text/css">
220                 <!--
221                 <?php echo _BODYFONTSTYLE; ?>
222                 -->
223                 </style>
224         </head>
225         <body>
226                 <div id="header">
227                         <div id="navigation">
228                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
229                                 <ul>
230                                         <?php
231                                         if ( in_array($param->state, array('mysql', 'weblog', 'install')) )
232                                         {
233                                                 echo '<li>', _STEP1, '</li><li';
234                                                 if ( $param->state == 'mysql' )
235                                                 {
236                                                         echo ' class="gry"';
237                                                 }
238                                                 echo '>&nbsp; &gt; &nbsp;', _STEP2, '</li><li';
239                                                 if ( in_array($param->state, array('mysql', 'weblog')) )
240                                                 {
241                                                         echo ' class="gry"';
242                                                 }
243                                                 echo '>&nbsp; &gt; &nbsp;', _STEP3, "</li>\n";
244                                         }
245                                         if ( in_array($param->state, array('mysql', 'weblog', 'detail')) )
246                                         {
247                                                 echo '<li class="rightbox">';
248                                                 if ( in_array($param->state, array('mysql', 'weblog')) )
249                                                 {
250                                                         echo '<a href="./?mode=detail">', _MODE2, '</a>';
251                                                 }
252                                                 else
253                                                 {
254                                                         echo '<a href="./?mode=simple">', _MODE1, '</a>';
255                                                 }
256                                                 echo '</li>';
257                                         }
258                                         ?>
259                                 </ul>
260                         </div>
261                 </div>
262 <?php
263 }
264
265 /**
266  * footer tag of the installation screens
267  **/
268 function show_footer()
269 {
270         global $page_footer_copyright;
271 ?>
272                 <div id="footer">
273                         <?php echo $page_footer_copyright; ?>
274                 </div>
275         </body>
276 </html>
277 <?php
278 }
279
280 /**
281  * Display the form for language select
282  */
283 function show_select_locale_form()
284 {
285         // Get the browser language that can be displayed
286         // TODO: default locale select simple implementation
287         $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
288         foreach ($languages as $language)
289         {
290                 $language = preg_replace('#([\w]+).*#', '$1', $language);
291                 break;
292         }
293
294         $locales = array(
295                 array('en_Latn_US', 'English - United States'),
296                 array('ja_Jpan_JP', 'Japanese - Japan')
297         );
298 ?>
299                 <div id="container">
300                         <p style="font-size:152%;font-weight:bold;">
301                                 Select your locale:
302                         </p>
303                         <form method="post" action="./index.php">
304
305                                 <div class="prt">
306                                         <select name="locale">
307 <?php
308         foreach ( $locales as $locale )
309         {
310                 echo "<option value=\"$locale[0]\"";
311                 if ( i18n::strpos($locale[0], $language) === 0 )
312                 {
313                         echo ' selected';
314                 }
315                 echo ">$locale[1]</option>\n";
316         }
317 ?>
318                                         </select>
319                                         <p class="sbt">
320                                                 <button type="submit" name="action" value="locale" class="sbt_arw">START</button>
321                                         </p>
322                                 </div>
323                         </form>
324                 </div>
325 <?php
326 }
327
328 /**
329  * Display the form to set up a database
330  * @param bool $isPostback
331  */
332 function show_database_setting_form($isPostback)
333 {
334         global $param, $minimum_mysql_version;
335
336         $config_writable = canConfigFileWritable();
337         $mysql_version = getMySqlVersion();
338         ?>
339                 <div id="container">
340                         <p class="msg">
341 <?php
342         echo _SIMPLE_NAVI1;
343         if ( $config_writable != '' )
344         {
345                 echo '<span class="err">', $config_writable, '</span>';
346         }
347         if ( $mysql_version == '0.0.0' )
348         {
349                 echo '<span class="err">', _ERROR21, '</span>';
350         }
351         elseif ( $mysql_version < $minimum_mysql_version )
352         {
353                 echo '<span class="err">', sprintf(_ERROR20 , $minimum_mysql_version), '</span>';
354         }
355 ?>
356                         </p>
357                         <form method="post" action="./index.php">
358                                 <div class="prt">
359                                         <h2><?php echo _DB_HEADER; ?></h2>
360                                         <p class="msg">
361 <?php
362         if ( $isPostback )
363         {
364                 $errors = $param->check_mysql_parameters();
365                 if ( is_array($errors) )
366                 {
367                         foreach ( $errors as $error )
368                         {
369                                 echo '<span class="err">', $error, "</span>\n";
370                         }
371                 }
372         }
373 ?>
374                                         </p>
375                                         <table>
376                                                 <tr>
377                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
378                                                                 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
379                                                 </tr>
380                                                 <tr>
381                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
382                                                                 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
383                                                 </tr>
384                                                 <tr>
385                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
386                                                                 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
387                                                 </tr>
388                                                 <tr>
389                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
390                                                                 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
391                                                 </tr>
392                                         </table>
393                                         <p class="sbt">
394                                                 <button type="submit" name="mode" value="detail" class="sbt_sqr"><?php echo _MODE2; ?></button>
395                                                 <button type="submit" name="action" value="mysql" class="sbt_arw"><?php echo _NEXT; ?></button>
396                                         </p>
397                                         <p class="msg">
398                                                 <?php echo _DB_TEXT1; ?>
399                                         </p>
400                                 </div>
401                         </form>
402                 </div>
403 <?php
404 }
405
406 /**
407  * Displays a form to the blog settings
408  * @param bool $isPostback
409  */
410 function show_blog_setting_form($isPostback)
411 {
412         global $param;
413
414 ?>
415                 <div id="container">
416                         <p class="msg">
417                                 <?php echo _SIMPLE_NAVI2; ?>
418                         </p>
419                         <form method="post" action="./index.php">
420                                 <div class="prt">
421                                         <h2><?php echo _BLOG_HEADER; ?></h2>
422                                         <p class="msg">
423 <?php
424         if ( $isPostback )
425         {
426                 $errors = $param->check_weblog_parameters();
427                 if ( is_array($errors) )
428                 {
429                         foreach ( $errors as $error )
430                         {
431                                 echo '<span class="err">', $error, "</span>\n";
432                         }
433                 }
434         }
435 ?>
436                                         </p>
437                                         <table>
438                                                 <tr>
439                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
440                                                                 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
441                                                 </tr>
442                                                 <tr>
443                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
444                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
445                                                 </tr>
446                                         </table>
447                                 </div>
448
449                                 <div class="prt">
450                                         <h2><?php echo _ADMIN_HEADER; ?></h2>
451                                         <p class="msg">
452 <?php
453         if ( $isPostback )
454         {
455                 $errors = $param->check_user_parameters();
456                 if ( is_array($errors) )
457                 {
458                         foreach ( $errors as $error )
459                         {
460                                 echo '<span class="err">', $error, "</span>\n";
461                         }
462                 }
463         }
464 ?>
465                                         </p>
466                                         <table>
467                                                 <tr>
468                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
469                                                                 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
470                                                 </tr>
471                                                 <tr>
472                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
473                                                                 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
474                                                 </tr>
475                                                 <tr>
476                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
477                                                                 <td><input type="password" name="user_password" /></td>
478                                                 </tr>
479                                                 <tr>
480                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
481                                                                 <td><input type="password" name="user_password2" /></td>
482                                                 </tr>
483                                                 <tr>
484                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
485                                                                 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
486                                                 </tr>
487                                         </table>
488                                         <p class="sbt">
489                                                 <button type="submit" name="action" value="weblog" class="sbt_arw"><?php echo _INSTALL; ?></button>
490                                         </p>
491                                 </div>
492                         </form>
493                 </div>
494 <?php
495 }
496
497 /**
498  * Displays a form to the detail settings
499  * @param bool $isPostback
500  */
501 function show_detail_setting_form($isPostback)
502 {
503         global $param, $minimum_mysql_version;
504
505         $mysql_version = getMySqlVersion();
506 ?>
507                 <div id="container_detailed">
508                         <p class="msg">
509                                 <?php echo _DETAIL_NAVI1; ?>
510 <?php
511         if ( $isPostback && !$param->check_all_parameters() )
512         {
513                 echo '<span class="err">', _ERROR26, "</span>\n";
514         }
515 ?>
516                         </p>
517                         <ul class="msg">
518                                 <li>PHP: <?php echo phpversion(); ?></li>
519                                 <li>MySQL:
520 <?php
521         echo ($mysql_version == '0.0.0') ? _ERROR21 : $mysql_version;
522         if ( $mysql_version < $minimum_mysql_version )
523         {
524                 echo '<span class="err">', sprintf(_ERROR20 , $minimum_mysql_version), '</span>';
525         }
526 ?></li>
527                         </ul>
528                         <form method="post" action="">
529
530                                 <div class="prt">
531                                         <h2><?php echo _DETAIL_HEADER1; ?></h2>
532                                         <p class="msg">
533 <?php
534         if ( $isPostback )
535         {
536                 $errors = $param->check_mysql_parameters();
537                 if ( is_array($errors) )
538                 {
539                         foreach ( $errors as $error )
540                         {
541                                 echo '<span class="err">', $error, "</span>\n";
542                         }
543                 }
544         }
545 ?>
546                                         </p>
547                                         <table>
548                                                 <tr>
549                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
550                                                                 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
551                                                 </tr>
552                                                 <tr>
553                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
554                                                                 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
555                                                 </tr>
556                                                 <tr>
557                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
558                                                                 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
559                                                 </tr>
560                                                 <tr>
561                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
562                                                                 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
563                                                 </tr>
564                                                 <tr>
565                                                         <th><span class="nam"><?php echo _DB_FIELD5; ?></span><span class="sub"><?php echo _DB_FIELD5_DESC; ?></span></th>
566                                                                 <td><input type="text" name="mysql_tablePrefix" value="<?php echo $param->mysql_tablePrefix; ?>" /></td>
567                                                 </tr>
568                                         </table>
569
570                                         <h2><?php echo _DETAIL_HEADER2; ?></h2>
571                                         <p class="msg">
572 <?php
573         if ( $isPostback )
574         {
575                 $errors = $param->check_uri_parameters();
576                 if ( is_array($errors) )
577                 {
578                         foreach ( $errors as $error )
579                         {
580                                 echo '<span class="err">', $error, "</span>\n";
581                         }
582                 }
583                 $errors = $param->check_path_parameters();
584                 if ( is_array($errors) )
585                 {
586                         foreach ( $errors as $error )
587                         {
588                                 echo '<span class="err">', $error, "</span>\n";
589                         }
590                 }
591         }
592 ?>
593                                         </p>
594                                         <table>
595                                                 <tr>
596                                                         <th><span class="nam"><?php echo _PATH_FIELD1; ?></span></th>
597                                                                 <td><input type="text" name="IndexURL" value="<?php echo $param->IndexURL; ?>" /></td>
598                                                 </tr>
599                                                 <tr>
600                                                         <th><span class="nam"><?php echo _PATH_FIELD2; ?></span></th>
601                                                                 <td><input type="text" name="AdminURL" value="<?php echo $param->AdminURL; ?>" /></td>
602                                                 </tr>
603                                                 <tr>
604                                                         <th><span class="nam"><?php echo _PATH_FIELD3; ?></span></th>
605                                                                 <td><input type="text" name="AdminPath" value="<?php echo $param->AdminPath; ?>" /></td>
606                                                 </tr>
607                                                 <tr>
608                                                         <th><span class="nam"><?php echo _PATH_FIELD4; ?></span></th>
609                                                                 <td><input type="text" name="MediaURL" value="<?php echo $param->MediaURL; ?>" /></td>
610                                                 </tr>
611                                                 <tr>
612                                                         <th><span class="nam"><?php echo _PATH_FIELD5; ?></span></th>
613                                                                 <td><input type="text" name="MediaPath" value="<?php echo $param->MediaPath; ?>" /></td>
614                                                 </tr>
615                                                 <tr>
616                                                         <th><span class="nam"><?php echo _PATH_FIELD6; ?></span></th>
617                                                                 <td><input type="text" name="SkinsURL" value="<?php echo $param->SkinsURL; ?>" /></td>
618                                                 </tr>
619                                                 <tr>
620                                                         <th><span class="nam"><?php echo _PATH_FIELD7; ?></span></th>
621                                                                 <td><input type="text" name="SkinsPath" value="<?php echo $param->SkinsPath; ?>" /></td>
622                                                 </tr>
623                                                 <tr>
624                                                         <th><span class="nam"><?php echo _PATH_FIELD8; ?></span></th>
625                                                                 <td><input type="text" name="PluginURL" value="<?php echo $param->PluginURL; ?>" /></td>
626                                                 </tr>
627                                                 <tr>
628                                                         <th><span class="nam"><?php echo _PATH_FIELD9; ?></span></th>
629                                                                 <td><input type="text" name="ActionURL" value="<?php echo $param->ActionURL; ?>" /></td>
630                                                 </tr>
631                                         </table>
632                                         <p class="msg">
633                                                 <?php echo _DETAIL_TEXT3; ?>
634                                         </p>
635
636                                         <h2><?php echo _DETAIL_HEADER3; ?></h2>
637                                         <p class="msg">
638 <?php
639         echo _DETAIL_TEXT4;
640         if ( $isPostback )
641         {
642                 $errors = $param->check_user_parameters();
643                 if ( is_array($errors) )
644                 {
645                         foreach ( $errors as $error )
646                         {
647                                 echo '<span class="err">', $error, "</span>\n";
648                         }
649                 }
650         }
651 ?>
652                                         </p>
653                                         <table>
654                                                 <tr>
655                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
656                                                                 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
657                                                 </tr>
658                                                 <tr>
659                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
660                                                                 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
661                                                 </tr>
662                                                 <tr>
663                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
664                                                                 <td><input type="password" name="user_password" /></td>
665                                                 </tr>
666                                                 <tr>
667                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
668                                                                 <td><input type="password" name="user_password2" /></td>
669                                                 </tr>
670                                                 <tr>
671                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
672                                                                 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
673                                                 </tr>
674                                         </table>
675
676                                         <h2><?php echo _DETAIL_HEADER4; ?></h2>
677                                         <p class="msg">
678 <?php
679         echo _DETAIL_TEXT5;
680         if ( $isPostback )
681         {
682                 $errors = $param->check_weblog_parameters();
683                 if ( is_array($errors) )
684                 {
685                         foreach ( $errors as $error )
686                         {
687                                 echo '<span class="err">', $error, "</span>\n";
688                         }
689                 }
690         }
691 ?>
692                                         </p>
693                                         <table>
694                                                 <tr>
695                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
696                                                                 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
697                                                 </tr>
698                                                 <tr>
699                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
700                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
701                                                 </tr>
702                                         </table>
703
704                                         <p class="msg">
705                                                 <?php echo _DETAIL_TEXT6; ?>
706                                         </p>
707
708                                         <p class="sbt">
709                                                 <button type="submit" name="action" value="detail" class="sbt_arw"><?php echo _INSTALL; ?></button>
710                                         </p>
711                                 </div>
712                         </form>
713                 </div>
714 <?php
715 }
716
717 /**
718  * Displays a screen to signal the completion of the installation
719  */
720 function show_install_complete_form()
721 {
722         global $MYSQL_HANDLER, $param;
723         $errors = do_install();
724 ?>
725                 <div id="container">
726                         <p class="msg">
727 <?php
728         if ( is_array($errors) && count($errors) > 0 )
729         {
730                 echo _ERROR27;
731                 foreach ( $errors as $error )
732                 {
733                         echo '<span class="err">', $error, "</span>\n";
734                 }
735         }
736         else
737         {
738                 echo _INST_TEXT;
739                 if ( array_key_exists('config_data', $_SESSION) )
740                 {
741                         echo '<span class="err">', _INST_TEXT4, '</span>';
742 ?>
743 <pre><code><?php echo htmlentities($_SESSION['config_data'], null, i18n::get_current_charset() ) ?></code></pre>
744 <?php
745                 }
746                 else
747                 {
748                         echo '<span class="err">', _INST_TEXT5, '</span>';
749                 }
750 ?>
751                         </p>
752                         <form method="post" action="./index.php">
753                                 <div class="prt">
754                                         <h2><?php echo _INST_HEADER1; ?></h2>
755                                         <p class="msg">
756                                                 <?php echo sprintf(_INST_TEXT1, $param->blog_name); ?>
757                                         </p>
758                                         <p class="sbt">
759                                                 <button type="button" name="toBlog" onclick="location.href='<?php echo $param->IndexURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON1; ?></button>
760                                         </p>
761                                 </div>
762
763                                 <div class="prt">
764                                         <h2><?php echo _INST_HEADER2; ?></h2>
765                                         <p class="msg">
766                                                 <?php echo _INST_TEXT2; ?>
767                                         </p>
768                                         <p class="sbt">
769                                                 <button type="button" name="toMng" onclick="location.href='<?php echo $param->AdminURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON2; ?></button>
770                                         </p>
771                                 </div>
772
773                                 <div class="prt">
774                                         <h2><?php echo _INST_HEADER3; ?></h2>
775                                         <p class="msg">
776                                                 <?php echo _INST_TEXT3; ?>
777                                         </p>
778                                         <p class="sbt">
779                                                 <button type="button" name="toAddBlog" onclick="location.href='<?php echo $param->AdminURL; ?>index.php?action=createnewlog';" class="sbt_arw"><?php echo _INST_BUTTON3; ?></button>
780                                         </p>
781                                 </div>
782                         </form>
783 <?php
784         }
785 ?>
786                 </div>
787 <?php
788         unset($param);
789 }
790
791 /**
792  * The installation process itself
793  * @return array error messages
794  */
795 function do_install()
796 {
797         global $param;
798         global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_PREFIX, $MYSQL_CONN;
799         global $DIR_NUCLEUS, $DIR_MEDIA, $DIR_SKINS, $DIR_PLUGINS, $DIR_LANG, $DIR_LIBS;
800         $errors = array();
801
802         /*
803          * 1. put all param-vars into vars
804          */
805         $MYSQL_HOST = $param->mysql_host;
806         $MYSQL_USER = $param->mysql_user;
807         $MYSQL_PASSWORD = $param->mysql_password;
808         $MYSQL_DATABASE = $param->mysql_database;
809         $MYSQL_PREFIX = $param->mysql_tablePrefix;
810
811         $DIR_NUCLEUS = $param->AdminPath;
812         $DIR_MEDIA = $param->MediaPath;
813         $DIR_SKINS = $param->SkinsPath;
814         $DIR_PLUGINS = $DIR_NUCLEUS . 'plugins/';
815         $DIR_LOCALES = $DIR_NUCLEUS . 'locales/';
816         $DIR_LIBS = $DIR_NUCLEUS . 'libs/';
817
818         /*
819          * 2.open mySQL connection
820          */
821         $MYSQL_CONN = @sql_connect_args($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD);
822         if ( $MYSQL_CONN == false )
823         {
824                 $errors[] = _ERROR3;
825                 return $errors;
826         }
827
828         /*
829          * 3. try to create database if needed
830          */
831         if ( !sql_query('CREATE DATABASE IF NOT EXISTS ' . $MYSQL_DATABASE) )
832         {
833                 $errors[] = _ERROR12 . ': ' . sql_error();
834         }
835
836         /*
837          * 4. try to select database
838          */
839         if ( !sql_select_db($MYSQL_DATABASE) )
840         {
841                 $errors[] = _ERROR13;
842         }
843         sql_set_charset('utf8');
844
845         if ( count($errors) > 0 )
846         {
847                 return $errors;
848         }
849
850         /*
851          * 5. execute queries
852          */
853         $table_names = array(
854                 'nucleus_actionlog',
855                 'nucleus_ban',
856                 'nucleus_blog',
857                 'nucleus_category',
858                 'nucleus_comment',
859                 'nucleus_config',
860                 'nucleus_item',
861                 'nucleus_karma',
862                 'nucleus_member',
863                 'nucleus_plugin',
864                 'nucleus_skin',
865                 'nucleus_template',
866                 'nucleus_team',
867                 'nucleus_activation',
868                 'nucleus_tickets'
869         );
870
871         $prefixed_table_names = array();
872         foreach ( $table_names as $table_name )
873         {
874                 $prefixed_table_names[] = $MYSQL_PREFIX . $table_name;
875         }
876
877         // table exists check
878         $result = sql_query('SHOW TABLES');
879         while ($row = mysql_fetch_array($result, MYSQL_NUM))
880         {
881                 if ( in_array($row[0], $prefixed_table_names) )
882                 {
883                         $errors[] = _ERROR14;
884                         break;
885                 }
886         }
887         if ( count($errors) > 0 )
888         {
889                 return $errors;
890         }
891
892         $filename = 'install.sql';
893         $fd = fopen($filename, 'r');
894         $queries = fread($fd, filesize($filename) );
895         fclose($fd);
896
897         $queries = preg_split('#(;\n|;\r)#', $queries);
898
899         foreach ( $queries as $query )
900         {
901                 if ( preg_match('/\w+/', $query) )
902                 {
903                         if ( $MYSQL_PREFIX )
904                         {
905                                 $query = str_replace($table_names, $prefixed_table_names, $query);
906                         }
907
908                         if ( !sql_query($query) )
909                         {
910                                 $errors[] = _ERROR15 . ' (<small>' . $query . '</small>): ' . sql_error();
911                         }
912                 }
913         }
914
915         /*
916          * 6. put needed records
917          */
918         /* push first post */
919         $query = "INSERT INTO %s VALUES (1, '%s', '%s', '%s', 1, 1, '2005-08-15 11:04:26', 0, 0, 0, 1, 0, 1)";
920         $query = sprintf($query, tableName('nucleus_item'), _1ST_POST_TITLE, _1ST_POST, _1ST_POST2);
921         if ( !sql_query($query) )
922         {
923                 $errors[] = _ERROR15 . ' (<small>' . $newpost . '</small>): ' . sql_error();
924         }
925
926         /* update datetime for this first post */
927         $query = "UPDATE %s SET itime = '%s' WHERE inumber = 1";
928         $query = sprintf($query, tableName('nucleus_item'), date('Y-m-d H:i:s',time()));
929         if ( !sql_query($query) )
930         {
931                 $errors[] = _ERROR18 . ': ' . sql_error();
932         }
933
934         /* push configurations */
935         array_merge($errors, updateConfig('IndexURL', $param->IndexURL));
936         array_merge($errors, updateConfig('AdminURL', $param->AdminURL));
937         array_merge($errors, updateConfig('MediaURL', $param->MediaURL));
938         array_merge($errors, updateConfig('SkinsURL', $param->SkinsURL));
939         array_merge($errors, updateConfig('PluginURL', $param->PluginURL));
940         array_merge($errors, updateConfig('ActionURL', $param->ActionURL));
941         array_merge($errors, updateConfig('AdminEmail', $param->user_email));
942         array_merge($errors, updateConfig('SiteName', $param->blog_name));
943         array_merge($errors, updateConfig('Locale', i18n::get_current_locale()));
944
945         /* escape strings for SQL */
946         $user_name                      = sql_real_escape_string($param->user_name);
947         $user_realname          = sql_real_escape_string($param->user_realname);
948         $user_password          = sql_real_escape_string(md5($param->user_password));
949         $user_email                     = sql_real_escape_string($param->user_email);
950         $blog_name                      = sql_real_escape_string($param->blog_name);
951         $blog_shortname         = sql_real_escape_string($param->blog_shortname);
952         $config_indexurl        = sql_real_escape_string($param->IndexURL);
953
954         /* push super admin */
955         $query = "UPDATE %s SET mname = '%s', mrealname = '%s', mpassword = '%s', memail = '%s', murl = '%s', madmin = 1, mcanlogin = 1 WHERE mnumber = 1";
956         $query = sprintf($query, tableName('nucleus_member'), $user_name, $user_realname, $user_password, $user_email, $config_indexurl);
957         if ( !sql_query($query) )
958         {
959                 $errors[] = _ERROR16 . ': ' . sql_error();
960         }
961
962         /* push new weblog */
963         $query = "UPDATE %s SET bname = '%s', bshortname = '%s', burl = '%s' WHERE bnumber = 1";
964         $query = sprintf($query, tableName('nucleus_blog'), $blog_name, $blog_shortname, $config_indexurl);
965         if ( !sql_query($query) )
966         {
967                 $errors[] = _ERROR17 . ': ' . sql_error();
968         }
969
970         /* push default category */
971         $query = "UPDATE %s SET cname = '%s', cdesc = '%s' WHERE catid = 1";
972         $query = sprintf($query, tableName('nucleus_category'), _GENERALCAT_NAME, _GENERALCAT_DESC);
973         if ( !sql_query($query) )
974         {
975                 $errors[] = _ERROR17 . ': ' . sql_error();
976         }
977
978         sql_close();
979
980         /*
981          * 7. install default plugins and skins
982          */
983         global $aConfPlugsToInstall, $aConfSkinsToImport;
984         $aSkinErrors = array();
985         $aPlugErrors = array();
986
987         if ( (count($aConfPlugsToInstall) > 0) || (count($aConfSkinsToImport) > 0) )
988         {
989                 include_once($DIR_LIBS . 'globalfunctions.php');
990                 global $manager;
991                 if ( !isset($manager) )
992                 {
993                         $manager = new MANAGER;
994                 }
995
996                 $aSkinErrors = installCustomSkins();
997                 if ( count($aSkinErrors) > 0 )
998                 {
999                         array_merge($errors, $aSkinErrors);
1000                 }
1001
1002                 $query  = "SELECT sdnumber FROM %s WHERE sdname='default'";
1003                 $query = sprintf($query, tableName('nucleus_skin_desc'));
1004                 $res = sql_query($query);
1005                 $obj = sql_fetch_assoc($res);
1006                 $defSkinID = (integer) $obj['sdnumber'];
1007
1008                 $query = "UPDATE %s SET bdefskin=%d WHERE bnumber=1";
1009                 $query = sprintf($query, tableName('nucleus_blog'), $defSkinID);
1010                 sql_query($query);
1011                 $query = "UPDATE %s SET value=%d WHERE name='BaseSkin'";
1012                 $query = sprintf($query, tableName('nucleus_config'), $defSkinID);
1013                 sql_query($query);
1014
1015                 $aPlugErrors = installCustomPlugs($manager);
1016                 if ( count($aPlugErrors) > 0 )
1017                 {
1018                         array_merge($errors, $aPlugErrors);
1019                 }
1020         }
1021
1022         /*
1023          * 8. Write config file ourselves (if possible)
1024          */
1025         $config_data = '<' . '?php' . "\n\n";
1026         $config_data .= "// mySQL connection information\n";
1027         $config_data .= "\$MYSQL_HOST = '" . $MYSQL_HOST . "';\n";
1028         $config_data .= "\$MYSQL_USER = '" . $MYSQL_USER . "';\n";
1029         $config_data .= "\$MYSQL_PASSWORD = '" . $MYSQL_PASSWORD . "';\n";
1030         $config_data .= "\$MYSQL_DATABASE = '" . $MYSQL_DATABASE . "';\n";
1031         $config_data .= "\$MYSQL_PREFIX = '" . $MYSQL_PREFIX . "';\n";
1032         $config_data .= "// new in 3.50. first element is db handler, the second is the db driver used by the handler\n";
1033         $config_data .= "// default is \$MYSQL_HANDLER = array('mysql','mysql');\n";
1034         $config_data .= "//\$MYSQL_HANDLER = array('mysql','mysql');\n";
1035         $config_data .= "//\$MYSQL_HANDLER = array('pdo','mysql');\n";
1036         $config_data .= "\$MYSQL_HANDLER = array('".$MYSQL_HANDLER[0]."','".$MYSQL_HANDLER[1]."');\n";
1037         $config_data .= "\n";
1038         $config_data .= "// main nucleus directory\n";
1039         $config_data .= "\$DIR_NUCLEUS = '" . $DIR_NUCLEUS . "';\n";
1040         $config_data .= "\n";
1041         $config_data .= "// path to media dir\n";
1042         $config_data .= "\$DIR_MEDIA = '" . $DIR_MEDIA . "';\n";
1043         $config_data .= "\n";
1044         $config_data .= "// extra skin files for imported skins\n";
1045         $config_data .= "\$DIR_SKINS = '" . $DIR_SKINS . "';\n";
1046         $config_data .= "\n";
1047         $config_data .= "// these dirs are normally sub dirs of the nucleus dir, but \n";
1048         $config_data .= "// you can redefine them if you wish\n";
1049         $config_data .= "\$DIR_PLUGINS = \$DIR_NUCLEUS . 'plugins/';\n";
1050         $config_data .= "\$DIR_LOCALES = \$DIR_NUCLEUS . 'locales/';\n";
1051         $config_data .= "\$DIR_LIBS = \$DIR_NUCLEUS . 'libs/';\n";
1052         $config_data .= "\n";
1053         $config_data .= "// include libs\n";
1054         $config_data .= "include(\$DIR_LIBS.'globalfunctions.php');\n";
1055         $config_data .= "?" . ">";
1056
1057         $result = false;
1058         if ( @!file_exists('../config.php') || is_writable('../config.php') )
1059         {
1060                 if ( $fp = @fopen('../config.php', 'w') )
1061                 {
1062                         $result = @fwrite($fp, $config_data, i18n::strlen($config_data) );
1063                         fclose($fp);
1064                 }
1065         }
1066
1067         if ( $result )
1068         {
1069                 // try to change the read-only permission.
1070                 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1071                 {
1072                         @chmod('../config.php', 0444);
1073                 }
1074         }
1075         else
1076         {
1077                 $_SESSION['config_data'] = $config_data;
1078         }
1079
1080         return $errors;
1081 }
1082
1083 /**
1084  * Confirm that you can write to the configuration file
1085  * @return string error message
1086  */
1087 function canConfigFileWritable()
1088 {
1089         if ( @file_exists('../config.php') && @!is_writable('../config.php') )
1090         {
1091                 // try to change the read-write permission.
1092                 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1093                 {
1094                         @chmod('../config.php', 0666);
1095                 }
1096
1097                 if ( @!is_writable('../config.php') )
1098                 {
1099                         return _ERROR19;
1100                 }
1101         }
1102         return '';
1103 }
1104
1105 /**
1106  * To obtain the version of MySQL
1107  * @return string
1108  */
1109 function getMySqlVersion()
1110 {
1111         global $minimum_mysql_version, $errors;
1112         // Turn on output buffer
1113         // Needed to repress the output of the sql function that are
1114         // not part of php (in this case the @ operator doesn't work)
1115         ob_start();
1116
1117         // note: this piece of code is taken from phpMyAdmin
1118         $conn = sql_connect_args('localhost', '', '');
1119         $result = @sql_query('SELECT VERSION() AS version', $conn);
1120
1121         if ( $result != FALSE && sql_num_rows($result) > 0 )
1122         {
1123                 $row = sql_fetch_array($result);
1124                 $match = i18n::explode('.', $row['version']);
1125         }
1126         else
1127         {
1128                 $result = @sql_query('SHOW VARIABLES LIKE \'version\'', $conn);
1129
1130                 if ( $result != FALSE && @sql_num_rows($result) > 0 )
1131                 {
1132                         $row = sql_fetch_row($result);
1133                         $match = i18n::explode('.', $row[1]);
1134                 }
1135                 else
1136                 {
1137                         //$output = shell_exec('mysql -V');
1138                         $output = ( function_exists('shell_exec') ) ? @shell_exec('mysql -V') : '0.0.0';
1139                         preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
1140                         $match = i18n::explode('.', $version[0]);
1141
1142                         if ( $match[0] == '' )
1143                         {
1144                                 $match = array('0', '0', '0');
1145                         }
1146                 }
1147         }
1148
1149         @sql_disconnect($conn);
1150
1151         //End and clean output buffer
1152         ob_end_clean();
1153
1154         return implode($match, '.');
1155 }
1156
1157 /**
1158  * Add a table prefix if it is used
1159  *
1160  * @param string $input table name with prefix
1161  * @return string
1162  */
1163 function tableName($input)
1164 {
1165         global $MYSQL_PREFIX;
1166         if ( $MYSQL_PREFIX )
1167         {
1168                 return $MYSQL_PREFIX . $input;
1169         }
1170         else
1171         {
1172                 return $input;
1173         }
1174 }
1175
1176 /**
1177  * Install custom plugins
1178  *
1179  * @param object $manager MANAGER class instance
1180  */
1181 function installCustomPlugs($manager)
1182 {
1183         global $aConfPlugsToInstall, $DIR_LIBS;
1184
1185         $aErrors = array();
1186         if ( count($aConfPlugsToInstall) == 0 )
1187         {
1188                 return $aErrors;
1189         }
1190
1191         $res = sql_query('SELECT * FROM ' . tableName('nucleus_plugin') );
1192         $numCurrent = sql_num_rows($res);
1193
1194         foreach ( $aConfPlugsToInstall as $plugName )
1195         {
1196                 $query = 'INSERT INTO ' . tableName('nucleus_plugin') . ' (porder, pfile) VALUES (' . (++$numCurrent) . ", '" . sql_real_escape_string($plugName) . "')";
1197                 sql_query($query);
1198
1199                 $manager->clearCachedInfo('installedPlugins');
1200                 $plugin =& $manager->getPlugin($plugName);
1201                 $plugin->setID($numCurrent);
1202
1203                 if ( !$plugin )
1204                 {
1205                         sql_query('DELETE FROM ' . tableName('nucleus_plugin') . " WHERE pfile = '" . sql_real_escape_string($plugName) . "'");
1206                         $numCurrent--;
1207                         array_push($aErrors, sprintf(_ERROR22 ,$plugName));
1208                         continue;
1209                 }
1210                 $plugin->install();
1211         }
1212
1213         sql_query('DELETE FROM ' . tableName('nucleus_plugin_event') );
1214         $res = sql_query('SELECT pid, pfile FROM ' . tableName('nucleus_plugin') );
1215
1216         while ( $o = sql_fetch_object($res) )
1217         {
1218                 $pid = $o->pid;
1219                 $plug =& $manager->getPlugin($o->pfile);
1220
1221                 if ( $plug )
1222                 {
1223                         $eventList = $plug->getEventList();
1224                         foreach ( $eventList as $eventName )
1225                         {
1226                                 sql_query('INSERT INTO ' . tableName('nucleus_plugin_event') . ' (pid, event) VALUES (' . $pid . ", '" . $eventName . "')");
1227                         }
1228                 }
1229         }
1230         return $aErrors;
1231 }
1232
1233 /**
1234  * Install custom skins
1235  * Prepares the installation of custom skins
1236  */
1237 function installCustomSkins()
1238 {
1239         global $aConfSkinsToImport, $DIR_LIBS, $DIR_SKINS;
1240
1241         $aErrors = array();
1242         if ( count($aConfSkinsToImport) == 0 )
1243         {
1244                 return $aErrors;
1245         }
1246
1247         include_once($DIR_LIBS . 'skinie.php');
1248         $importer = new SKINIMPORT();
1249
1250         foreach ( $aConfSkinsToImport as $skinName )
1251         {
1252                 $importer->reset();
1253                 $skinFile = $DIR_SKINS . $skinName . '/skinbackup.xml';
1254
1255                 if ( !@file_exists($skinFile) )
1256                 {
1257                         array_push($aErrors, sprintf(_ERROR23, $skinFile));
1258                         continue;
1259                 }
1260
1261                 $error = $importer->readFile($skinFile);
1262
1263                 if ( $error )
1264                 {
1265                         array_push($aErrors, sprintf(_ERROR24, $skinName) . ' : ' . $error);
1266                         continue;
1267                 }
1268
1269                 $error = $importer->writeToDatabase(1);
1270
1271                 if ( $error )
1272                 {
1273                         array_push($aErrors, sprintf(_ERROR25, $skinName) . ' : ' . $error);
1274                         continue;
1275                 }
1276         }
1277         return $aErrors;
1278 }
1279
1280
1281 /**
1282  * Check if some important files of the Nucleus CMS installation are available
1283  * Give an error if one or more files are not accessible
1284  */
1285 function do_check_files()
1286 {
1287         $missingfiles = array();
1288         $files = array(
1289                 './install.sql',
1290                 '../index.php',
1291                 '../action.php',
1292                 '../nucleus/index.php',
1293                 '../nucleus/media.php',
1294                 '../nucleus/libs/ACTION.php',
1295                 '../nucleus/libs/ACTIONLOG.php',
1296                 '../nucleus/libs/ACTIONS.php',
1297                 '../nucleus/libs/ADMIN.php',
1298                 '../nucleus/libs/BaseActions.php',
1299                 '../nucleus/libs/BLOG.php',
1300                 '../nucleus/libs/BODYACTIONS.php',
1301                 '../nucleus/libs/COMMENT.php',
1302                 '../nucleus/libs/COMMENTACTIONS.php',
1303                 '../nucleus/libs/COMMENTS.php',
1304                 '../nucleus/libs/ENCAPSULATE.php',
1305                 '../nucleus/libs/ENTITY.php',
1306                 '../nucleus/libs/globalfunctions.php',
1307                 '../nucleus/libs/i18n.php',
1308                 '../nucleus/libs/ITEM.php',
1309                 '../nucleus/libs/ITEMACTIONS.php',
1310                 '../nucleus/libs/LINK.php',
1311                 '../nucleus/libs/MANAGER.php',
1312                 '../nucleus/libs/MEDIA.php',
1313                 '../nucleus/libs/MEMBER.php',
1314                 '../nucleus/libs/mysql.php',
1315                 '../nucleus/libs/NOTIFICATION.php',
1316                 '../nucleus/libs/PAGEFACTORY.php',
1317                 '../nucleus/libs/PARSER.php',
1318                 '../nucleus/libs/PLUGIN.php',
1319                 '../nucleus/libs/PLUGINADMIN.php',
1320                 '../nucleus/libs/SEARCH.php',
1321                 '../nucleus/libs/showlist.php',
1322                 '../nucleus/libs/SKIN.php',
1323                 '../nucleus/libs/TEMPLATE.php',
1324                 '../nucleus/libs/vars4.1.0.php',
1325                 '../nucleus/libs/xmlrpc.inc.php',
1326                 '../nucleus/libs/xmlrpcs.inc.php',
1327                 '../nucleus/libs/sql/mysql.php'
1328         );
1329
1330         $count = count($files);
1331         for ( $i = 0; $i < $count; $i++ )
1332         {
1333                 if ( !is_readable($files[$i]) )
1334                 {
1335                         array_push( $missingfiles, 'File <b>' . $files[$i] . '</b> is missing or not readable.<br />');
1336                 }
1337         }
1338
1339         if ( count($missingfiles) > 0 )
1340         {
1341                 exit(implode( "\n", $missingfiles));
1342         }
1343 }
1344
1345
1346 /**
1347  * Updates the configuration in the database
1348  *
1349  * @param string $name name of the config var
1350  * @param string $value new value of the config var
1351  * @return array
1352  */
1353 function updateConfig($name, $value)
1354 {
1355         $errors = array();
1356         $name = sql_real_escape_string($name);
1357         $value = trim(sql_real_escape_string($value) );
1358
1359         $query = "UPDATE %s SET value = '%s' WHERE name = '%s'";
1360         $query = sprintf($query, tableName('nucleus_config'), $value, $name);
1361
1362         if ( !sql_query($query) )
1363         {
1364                 $errors[] = _ERROR15 . ': ' . sql_error();
1365         }
1366         return $errors;
1367 }
1368
1369
1370 class PARAM_MANAGER
1371 {
1372         /* process parameter */
1373         public $state;
1374         public $locale;
1375
1376         /* mysql connection parameters */
1377         public $mysql_host;
1378         public $mysql_user;
1379         public $mysql_password;
1380         public $mysql_database;
1381         public $mysql_tablePrefix;
1382
1383         /* weblog configuration parameters */
1384         public $blog_name;
1385         public $blog_shortname;
1386
1387         /* member configuration parameters */
1388         public $user_name;
1389         public $user_realname;
1390         public $user_password;
1391         private $user_password2;
1392         public $user_email;
1393
1394         /* URI parameters  */
1395         private $root_url;
1396         public $IndexURL;
1397         public $AdminURL;
1398         public $MediaURL;
1399         public $SkinsURL;
1400         public $PluginURL;
1401         public $ActionURL;
1402
1403         /* path parameters */
1404         private $root_path;
1405         public $AdminPath;
1406         public $MediaPath;
1407         public $SkinsPath;
1408
1409         /**
1410          * constructor
1411          */
1412         public function __construct()
1413         {
1414                 $this->init();
1415         }
1416
1417         public function init()
1418         {
1419                 // set default values
1420                 $this->state = 'locale';
1421                 $this->install_mode = 'simple';
1422                 $this->locale = 'en_Latn_US';
1423                 $this->mysql_host = @ini_get('mysql.default_host');
1424                 $this->blog_name = 'My Nucleus CMS';
1425                 $this->blog_shortname = 'mynucleuscms';
1426
1427                 /* root path */
1428                 $this->root_path = realpath(dirname(__FILE__) . '/..');
1429                 if ( substr($this->root_path, -1, 1) !== '/' )
1430                 {
1431                         $this->root_path .= '/';
1432                 }
1433                 $base_path_pcre = preg_quote($this->root_path, '#');
1434
1435                 /* current directry name */
1436                 $directory_name = preg_replace("#$base_path_pcre#", '', dirname(__FILE__));
1437                 $directory_name_pcre = preg_quote($directory_name, '#');
1438
1439                 /* root uri */
1440                 $root_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
1441                 $this->root_url = preg_replace("#$directory_name_pcre(.*)$#", '', $root_url);
1442
1443                 $this->AdminPath = $this->root_path . 'nucleus' . DIRECTORY_SEPARATOR;
1444                 $this->MediaPath = $this->root_path . 'media' . DIRECTORY_SEPARATOR;
1445                 $this->SkinsPath = $this->root_path . 'skins' . DIRECTORY_SEPARATOR;
1446
1447                 $this->IndexURL  = $this->root_url;
1448                 $this->AdminURL  = $this->root_url . 'nucleus/';
1449                 $this->MediaURL  = $this->root_url . 'media/';
1450                 $this->SkinsURL  = $this->root_url . 'skins/';
1451                 $this->PluginURL = $this->root_url . 'nucleus/plugins/';
1452                 $this->ActionURL = $this->root_url . 'action.php';
1453         }
1454
1455         private function read_parameter($parameter)
1456         {
1457                 foreach ( $parameter as $element )
1458                 {
1459                         if ( array_key_exists($element, $_POST) )
1460                         {
1461                                 $this->$element = $_POST[$element];
1462                         }
1463                 }
1464         }
1465
1466         public function set_state($state)
1467         {
1468                 $states = array('locale', 'mysql', 'weblog', 'detail', 'install');
1469                 if ( in_array($state, $states) )
1470                 {
1471                         $this->state = $state;
1472                 }
1473         }
1474
1475         public function set_locale()
1476         {
1477                 $this->read_parameter(array('locale'));
1478
1479                 if ( !in_array($this->locale, i18n::get_available_locale_list()) )
1480                 {
1481                         $this->locale = 'en_Latn_US';
1482                 }
1483         }
1484
1485         public function check_mysql_parameters()
1486         {
1487                 $parameters = array('mysql_host', 'mysql_user', 'mysql_password', 'mysql_database', 'mysql_tablePrefix');
1488                 $this->read_parameter($parameters);
1489
1490                 $errors = array();
1491                 if ( $this->mysql_host == '' )
1492                 {
1493                         $errors[] = sprintf(_ERROR1, _DB_FIELD1);
1494                 }
1495
1496                 if ( $this->mysql_user == '' )
1497                 {
1498                         $errors[] = sprintf(_ERROR1, _DB_FIELD2);
1499                 }
1500
1501                 if ( $this->mysql_password == '' )
1502                 {
1503                         $errors[] = sprintf(_ERROR1, _DB_FIELD3);
1504                 }
1505
1506                 if ( $this->mysql_database == '' )
1507                 {
1508                         $errors[] = sprintf(_ERROR1, _DB_FIELD4);
1509                 }
1510
1511                 if ( $this->mysql_database != ''
1512                         && !preg_match('/^\w+$/', $this->mysql_database) )
1513                 {
1514                         $errors[] = sprintf(_ERROR2, _DB_FIELD4);
1515                 }
1516
1517                 if ( $this->mysql_tablePrefix != ''
1518                         && !preg_match('/^\w+$/', $this->mysql_tablePrefix) )
1519                 {
1520                         $errors[] = sprintf(_ERROR2, _DB_FIELD5);
1521                 }
1522                 
1523                 if ( count($errors) == 0 )
1524                 {
1525                         $mysql_conn = @sql_connect_args($this->mysql_host, $this->mysql_user, $this->mysql_password);
1526                         if ( $mysql_conn == false )
1527                         {
1528                                 $errors[] = _ERROR3;
1529                         }
1530                         else
1531                         {
1532                                 @sql_close($mysql_conn);
1533                         }
1534                 }
1535
1536                 return $errors;
1537         }
1538
1539         public function check_user_parameters()
1540         {
1541                 $parameters = array('user_name', 'user_realname', 'user_password', 'user_password2', 'user_email');
1542                 $this->read_parameter($parameters);
1543
1544                 $errors = array();
1545                 if ( $this->user_name == '' )
1546                 {
1547                         $errors[] = sprintf(_ERROR1, _ADMIN_FIELD2);
1548                 }
1549                 elseif ( !preg_match("/^[[:alnum:]]+[ [:alnum:]]*[[:alnum:]]+$/i", $this->user_name) )
1550                 {
1551                         $errors[] = _ERROR5;
1552                 }
1553
1554                 if ( $this->user_realname == '' )
1555                 {
1556                         $errors[] = sprintf(_ERROR1, _ADMIN_FIELD1);
1557                 }
1558
1559                 if ( $this->user_password == '' || $this->user_password2 == '' )
1560                 {
1561                         $errors[] = sprintf(_ERROR1, _ADMIN_FIELD3);
1562                         $this->user_password = '';
1563                 }
1564                 elseif ( $this->user_password != $this->user_password2 )
1565                 {
1566                         $errors[] = _ERROR6;
1567                         $this->user_password = '';
1568                 }
1569
1570                 if ( !preg_match("/^[\w\.-]+@[\w\.-]+\.[[:alpha:]]{2,6}$/", $this->user_email) )
1571                 {
1572                         $errors[] = _ERROR7;
1573                 }
1574
1575                 return $errors;
1576         }
1577
1578         public function check_weblog_parameters()
1579         {
1580                 $parameters = array('blog_name', 'blog_shortname');
1581                 $this->read_parameter($parameters);
1582
1583                 $errors = array();
1584                 if ( $this->blog_name == '' )
1585                 {
1586                         $errors[] = sprintf(_ERROR1, _BLOG_FIELD1);
1587                 }
1588
1589                 if ( $this->blog_shortname == '' )
1590                 {
1591                         $errors[] = sprintf(_ERROR1, _BLOG_FIELD2);
1592                 }
1593
1594                 if ( !preg_match("/^[a-z0-9]+$/", $this->blog_shortname) )
1595                 {
1596                         $errors[] = _ERROR4;
1597                 }
1598
1599                 return $errors;
1600         }
1601
1602         public function check_uri_parameters()
1603         {
1604                 $parameters = array('IndexURL', 'AdminURL', 'MediaURL', 'SkinsURL', 'PluginURL', 'ActionURL');
1605                 $this->read_parameter($parameters);
1606
1607                 $errors = array();
1608                 if ( substr($this->IndexURL, -1, 1) !== '/' )
1609                 {
1610                         $errors[] = sprintf(_ERROR8, _PATH_FIELD1);
1611                 }
1612
1613                 if ( substr($this->AdminURL, -1, 1) !== '/' )
1614                 {
1615                         $errors[] = sprintf(_ERROR8, _PATH_FIELD2);
1616                 }
1617
1618                 if ( substr($this->MediaURL, -1, 1) !== '/' )
1619                 {
1620                         $errors[] = sprintf(_ERROR8, _PATH_FIELD4);
1621                 }
1622
1623                 if ( substr($this->SkinsURL, -1, 1) !== '/' )
1624                 {
1625                         $errors[] = sprintf(_ERROR8, _PATH_FIELD6);
1626                 }
1627
1628                 if ( substr($this->PluginURL, -1, 1) !== '/' )
1629                 {
1630                         $errors[] = sprintf(_ERROR8, _PATH_FIELD8);
1631                 }
1632
1633                 if ( strrchr($this->ActionURL, '/') != '/action.php' )
1634                 {
1635                         $errors[] = sprintf(_ERROR9, _PATH_FIELD9);
1636                 }
1637
1638                 return $errors;
1639         }
1640
1641         public function check_path_parameters()
1642         {
1643                 $parameters = array('AdminPath', 'MediaPath', 'SkinsPath');
1644                 $this->read_parameter($parameters);
1645
1646                 $separators = array('/', DIRECTORY_SEPARATOR);
1647                 $errors = array();
1648                 if ( !in_array(substr($this->AdminPath, -1, 1), $separators) || !file_exists($this->AdminPath) )
1649                 {
1650                         $errors[] = sprintf(_ERROR10, _PATH_FIELD3);
1651                 }
1652
1653                 if ( !in_array(substr($this->MediaPath, -1, 1), $separators) || !file_exists($this->MediaPath) )
1654                 {
1655                         $errors[] = sprintf(_ERROR10, _PATH_FIELD5);
1656                 }
1657
1658                 if ( !in_array(substr($this->SkinsPath, -1, 1), $separators) || !file_exists($this->SkinsPath) )
1659                 {
1660                         $errors[] = sprintf(_ERROR10, _PATH_FIELD7);
1661                 }
1662
1663                 return $errors;
1664         }
1665
1666         /**
1667          * check all parameters
1668          * @return bool
1669          */
1670         public function check_all_parameters()
1671         {
1672                 $this->set_locale();
1673
1674                 $isValid = true;
1675                 $isValid &= (count($this->check_mysql_parameters()) == 0);
1676                 $isValid &= (count($this->check_user_parameters()) == 0);
1677                 $isValid &= (count($this->check_weblog_parameters()) == 0);
1678                 $isValid &= (count($this->check_uri_parameters()) == 0);
1679                 $isValid &= (count($this->check_path_parameters()) == 0);
1680
1681                 return $isValid;
1682         }
1683 }