OSDN Git Service

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