OSDN Git Service

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