OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / install / index.php
1 <<<<<<< HEAD
2 <?php\r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2012 The Nucleus Group\r
6  *\r
7  * This program is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU General Public License\r
9  * as published by the Free Software Foundation; either version 2\r
10  * of the License, or (at your option) any later version.\r
11  * (see nucleus/documentation/index.html#license for more info)\r
12  *\r
13  * This script will install the Nucleus tables in your SQL-database,\r
14  * and initialize the data in those tables.\r
15  */\r
16 \r
17 /**\r
18  * @license http://nucleuscms.org/license.txt GNU General Public License\r
19  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
20  * @version $Id: index.php 1745 2012-04-12 23:45:47Z sakamocchi $\r
21  */\r
22 \r
23 /* global values initialize */\r
24 $CONF = array();\r
25 \r
26 /* reporting all errors for support */\r
27 error_reporting(E_ALL);\r
28 \r
29 $minimum_php_version = '5.0.6';\r
30 $minimum_mysql_version = '3.23';\r
31 \r
32 $page_footer_copyright = '&copy; 2001-2012 The Nucleus Groupe . Running Nucleus CMS v4.00';\r
33 \r
34 // begin if: server's PHP version is below the minimum; halt installation\r
35 if ( version_compare(PHP_VERSION, $minimum_php_version, '<') )\r
36 {\r
37         exit('<div style="font-size: xx-large;"> Nucleus requires at least PHP version ' . $minimum_php_version . '</div>');\r
38 }\r
39 \r
40 // make sure there's no unnecessary escaping: # set_magic_quotes_runtime(0);\r
41 if ( version_compare(PHP_VERSION, '5.3.0', '<') )\r
42 {\r
43         ini_set('magic_quotes_runtime', '0');\r
44 }\r
45 \r
46 /* default installed plugins and skins */\r
47 $aConfPlugsToInstall = array('NP_SecurityEnforcer', 'NP_SkinFiles');\r
48 $aConfSkinsToImport = array('atom', 'rss2.0', 'rsd', 'default');\r
49 \r
50 // Check if some important files\r
51 do_check_files();\r
52 \r
53 /* i18n class is needed for internationalization */\r
54 include_once('../nucleus/libs/i18n.php');\r
55 if ( !i18n::init('UTF-8', '../nucleus/locales') )\r
56 {\r
57         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>');\r
58 }\r
59 \r
60 // include core classes that are needed for login & plugin handling\r
61 \r
62 // added for 3.5 sql_* wrapper\r
63 global $MYSQL_HANDLER;\r
64 \r
65 if ( !isset($MYSQL_HANDLER) )\r
66 {\r
67         $MYSQL_HANDLER = array('mysql', '');\r
68         \r
69         // check if mysql support is installed; this check may not make sense, as is, in a version past 3.5x\r
70         if ( !function_exists('mysql_query') && !function_exists('mysqli_query') )\r
71         {\r
72                 exit('<div style="font-size: xx-large;"> Your PHP version does not have support for MySQL :( </div>');\r
73         }\r
74 }\r
75 include_once('../nucleus/libs/sql/sql.php');\r
76 \r
77 session_start();\r
78 if ( count($_GET) == 0 && count($_POST) == 0 )\r
79 {\r
80         unset($_SESSION['param_manager']);\r
81 }\r
82 \r
83 // restore the $param from the session\r
84 if ( array_key_exists('param_manager', $_SESSION) )\r
85 {\r
86         $param = $_SESSION['param_manager'];\r
87 }\r
88 else\r
89 {\r
90         $param = new ParamManager();\r
91 }\r
92 \r
93 // include translation file\r
94 if ( array_key_exists('locale', $_POST) ) $param->set_locale();\r
95 i18n::set_current_locale($param->locale);\r
96 $translation_file = './locales/' . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php';\r
97 if ( !file_exists($translation_file) )\r
98 {\r
99         $translation_file = './locales/en_Latn_US.UTF-8.php';\r
100 }\r
101 include($translation_file);\r
102 \r
103 do_action();\r
104 \r
105 // $param is saved to the session\r
106 if ( isset($param) )\r
107 {\r
108         $_SESSION['param_manager'] = $param;\r
109 }\r
110 else\r
111 {\r
112         unset($_SESSION['param_manager']);\r
113 }\r
114 exit;\r
115 \r
116 /**\r
117  * installer action\r
118  */\r
119 function do_action()\r
120 {\r
121         global $param;\r
122 \r
123         if ( array_key_exists('action', $_POST) )\r
124         {\r
125                 $isPostback = true;\r
126         }\r
127         else\r
128         {\r
129                 $isPostback = false;\r
130         }\r
131 \r
132         // mode change\r
133         if ( array_key_exists('mode', $_REQUEST) )\r
134         {\r
135                 if ( $_REQUEST['mode'] == 'detail' )\r
136                 {\r
137                         $param->set_state('detail');\r
138                 }\r
139                 elseif ( $_REQUEST['mode'] == 'simple' )\r
140                 {\r
141                         $param->set_state('mysql');\r
142                 }\r
143         }\r
144 \r
145         // input parameter check\r
146         if ( $isPostback )\r
147         {\r
148                 switch ( $param->state )\r
149                 {\r
150                         case 'locale':\r
151                                 $param->set_locale();\r
152                                 $param->set_state('mysql');\r
153                                 $isPostback = false;\r
154                                 break;\r
155                         case 'mysql':\r
156                                 if ( count($param->check_mysql_parameters()) == 0 )\r
157                                 {\r
158                                         $param->set_state('weblog');\r
159                                         $isPostback = false;\r
160                                 }\r
161                                 break;\r
162                         case 'weblog':\r
163                                 if ( count($param->check_user_parameters()) == 0\r
164                                         && count($param->check_weblog_parameters()) == 0 )\r
165                                 {\r
166                                         $param->set_state('install');\r
167                                         $isPostback = false;\r
168                                 }\r
169                                 break;\r
170                         case 'detail':\r
171                                 if ( $param->check_all_parameters() )\r
172                                 {\r
173                                         $param->set_state('install');\r
174                                         $isPostback = false;\r
175                                 }\r
176                                 break;\r
177                 }\r
178         }\r
179 \r
180         // page render\r
181         show_header();\r
182         switch ( $param->state )\r
183         {\r
184                 case 'locale':\r
185                         show_select_locale_form();\r
186                         break;\r
187                 case 'mysql':\r
188                         show_database_setting_form($isPostback);\r
189                         break;\r
190                 case 'weblog':\r
191                         show_blog_setting_form($isPostback);\r
192                         break;\r
193                 case 'detail':\r
194                         show_detail_setting_form($isPostback);\r
195                         break;\r
196                 case 'install':\r
197                         show_install_complete_form();\r
198                         break;\r
199         }\r
200         show_footer();\r
201 }\r
202 \r
203 /**\r
204  * header tag of the installation screens\r
205  **/\r
206 function show_header()\r
207 {\r
208         global $param;\r
209 \r
210         /* HTTP 1.1 application for no caching */\r
211         header("Cache-Control: no-cache, must-revalidate");\r
212         header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");\r
213         header('Content-Type: text/html; charset=' . i18n::get_current_charset());\r
214 ?>\r
215 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\r
216 <html xmlns="http://www.w3.org/1999/xhtml">\r
217         <head>\r
218                 <title><?php echo _TITLE; ?></title>\r
219                 <link rel="stylesheet" type="text/css" href="./styles/inst.css" />\r
220                 <style type="text/css">\r
221                 <!--\r
222                 <?php echo _BODYFONTSTYLE; ?>\r
223                 -->\r
224                 </style>\r
225                 <script type="text/javascript">\r
226                         function SelectText( element ) {\r
227                                 window.setTimeout( function() { element.select() }, 0 );\r
228                         }\r
229                         var isSubmit = false;\r
230                         function OnceSubmit() {\r
231                                 if (!isSubmit) {\r
232                                         isSubmit = true;\r
233                                         window.setTimeout( function() { isSubmit = false; }, 10000 );\r
234                                         return true;\r
235                                 }\r
236                                 return false;\r
237                         }\r
238                 </script>\r
239         </head>\r
240         <body>\r
241                 <div id="header">\r
242                         <div id="navigation">\r
243                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>\r
244                                 <ul>\r
245 <?php\r
246         if ( in_array($param->state, array('mysql', 'weblog', 'install')) )\r
247         {\r
248                 echo '<li>', _STEP1, '</li><li';\r
249                 if ( $param->state == 'mysql' )\r
250                 {\r
251                         echo ' class="gry"';\r
252                 }\r
253                 echo '>&nbsp; &gt; &nbsp;', _STEP2, '</li><li';\r
254                 if ( in_array($param->state, array('mysql', 'weblog')) )\r
255                 {\r
256                         echo ' class="gry"';\r
257                 }\r
258                 echo '>&nbsp; &gt; &nbsp;', _STEP3, "</li>\n";\r
259         }\r
260         if ( in_array($param->state, array('mysql', 'weblog', 'detail')) )\r
261         {\r
262                 echo '<li class="rightbox">';\r
263                 if ( in_array($param->state, array('mysql', 'weblog')) )\r
264                 {\r
265                         echo '<a href="./?mode=detail">', _MODE2, '</a>';\r
266                 }\r
267                 else\r
268                 {\r
269                         echo '<a href="./?mode=simple">', _MODE1, '</a>';\r
270                 }\r
271                 echo '</li>';\r
272         }\r
273 ?>\r
274                                 </ul>\r
275                         </div>\r
276                 </div>\r
277 <?php\r
278 }\r
279 \r
280 /**\r
281  * footer tag of the installation screens\r
282  **/\r
283 function show_footer()\r
284 {\r
285         global $page_footer_copyright;\r
286 ?>\r
287                 <div id="footer">\r
288                         <?php echo $page_footer_copyright; ?>\r
289                 </div>\r
290         </body>\r
291 </html>\r
292 <?php\r
293 }\r
294 \r
295 /**\r
296  * Display the form for language select\r
297  */\r
298 function show_select_locale_form()\r
299 {\r
300         // get locale list\r
301         $localelist = i18n::get_available_locale_list();\r
302         $locales = array();\r
303         foreach ( $localelist as $locale ) {\r
304                 $checkfile = './locales/' . $locale . '.' . i18n::get_current_charset() . '.php';\r
305                 $locales[] = array( $locale, (!file_exists($checkfile) ? '*&nbsp;' : '') . $locale );\r
306         }\r
307 ?>\r
308                 <div id="container">\r
309                         <p style="font-size:152%;font-weight:bold;">\r
310                                 Select your locale:\r
311                         </p>\r
312                         <form method="post" action="./index.php">\r
313 \r
314                                 <div class="prt">\r
315                                         <select name="locale">\r
316 <?php\r
317         // Get the browser language that can be displayed\r
318         // TODO: default locale select simple implementation\r
319         $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);\r
320         $language = (is_array($languages) && count($languages) > 0) ? preg_replace('#^([\w]+).*$#', '$1', $languages[0]) : '';\r
321 \r
322         foreach ( $locales as $locale )\r
323         {\r
324                 echo "<option value=\"$locale[0]\"";\r
325                 if ( i18n::strpos($locale[0], $language) === 0 )\r
326                 {\r
327                         echo ' selected';\r
328                 }\r
329                 echo ">$locale[1]</option>\n";\r
330         }\r
331 ?>\r
332                                         </select>\r
333                                         <p>Nucleus is installed in the selected locale, the locale of the Asterisk prefixed will be displayed in English because there is no translation of the installer file.</p>\r
334                                         <p>We will wait for the translator by volunteers!</p>\r
335                                         </p>\r
336                                         <p class="sbt">\r
337                                                 <button type="submit" name="action" value="locale" class="sbt_arw">START</button>\r
338                                         </p>\r
339                                 </div>\r
340                         </form>\r
341                 </div>\r
342 <?php\r
343 }\r
344 \r
345 /**\r
346  * Display the form to set up a database\r
347  * @param bool $isPostback\r
348  */\r
349 function show_database_setting_form($isPostback)\r
350 {\r
351         global $param, $minimum_mysql_version;\r
352 \r
353         $config_writable = canConfigFileWritable();\r
354         $mysql_version = getMySqlVersion();\r
355 ?>\r
356                 <div id="container">\r
357                         <p class="msg">\r
358 <?php\r
359         echo _SIMPLE_NAVI1;\r
360         if ( $config_writable != '' )\r
361         {\r
362                 echo '<span class="err">', $config_writable, '</span>';\r
363         }\r
364         if ( $mysql_version == '0.0.0' )\r
365         {\r
366                 echo '<span class="err">', _DBVERSION_UNKOWN, '</span>';\r
367         }\r
368         elseif ( version_compare($mysql_version, $minimum_mysql_version, '<') )\r
369         {\r
370                 echo '<span class="err">', sprintf(_DBVERSION_TOOLOW, $minimum_mysql_version), '</span>';\r
371         }\r
372 ?>\r
373                         </p>\r
374                         <form method="post" action="./index.php">\r
375                                 <div class="prt">\r
376                                         <h2><?php echo _DB_HEADER; ?></h2>\r
377                                         <p class="msg">\r
378 <?php\r
379         if ( $isPostback )\r
380         {\r
381                 $errors = $param->check_mysql_parameters();\r
382                 if ( is_array($errors) )\r
383                 {\r
384                         foreach ( $errors as $error )\r
385                         {\r
386                                 echo '<span class="err">', $error, "</span>\n";\r
387                         }\r
388                 }\r
389         }\r
390 ?>\r
391                                         </p>\r
392                                         <table>\r
393                                                 <tr>\r
394                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>\r
395                                                                 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>\r
396                                                 </tr>\r
397                                                 <tr>\r
398                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>\r
399                                                                 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>\r
400                                                 </tr>\r
401                                                 <tr>\r
402                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span></th>\r
403                                                                 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>\r
404                                                 </tr>\r
405                                                 <tr>\r
406                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>\r
407                                                                 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>\r
408                                                 </tr>\r
409                                         </table>\r
410                                         <p class="sbt">\r
411                                                 <button type="submit" name="mode" value="detail" class="sbt_sqr"><?php echo _MODE2; ?></button>\r
412                                                 <button type="submit" name="action" value="mysql" class="sbt_arw"><?php echo _NEXT; ?></button>\r
413                                         </p>\r
414                                         <p class="msg">\r
415                                                 <?php echo _DB_TEXT1; ?>\r
416                                         </p>\r
417                                 </div>\r
418                         </form>\r
419                 </div>\r
420 <?php\r
421 }\r
422 \r
423 /**\r
424  * Displays a form to the blog settings\r
425  * @param bool $isPostback\r
426  */\r
427 function show_blog_setting_form($isPostback)\r
428 {\r
429         global $param;\r
430 ?>\r
431                 <div id="container">\r
432                         <p class="msg">\r
433                                 <?php echo _SIMPLE_NAVI2; ?>\r
434                         </p>\r
435                         <form method="post" action="./index.php">\r
436                                 <div class="prt">\r
437                                         <h2><?php echo _BLOG_HEADER; ?></h2>\r
438                                         <p class="msg">\r
439 <?php\r
440         if ( $isPostback )\r
441         {\r
442                 $errors = $param->check_weblog_parameters();\r
443                 if ( is_array($errors) )\r
444                 {\r
445                         foreach ( $errors as $error )\r
446                         {\r
447                                 echo '<span class="err">', $error, "</span>\n";\r
448                         }\r
449                 }\r
450         }\r
451 ?>\r
452                                         </p>\r
453                                         <table>\r
454                                                 <tr>\r
455                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>\r
456                                                                 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>\r
457                                                 </tr>\r
458                                                 <tr>\r
459                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>\r
460                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>\r
461                                                 </tr>\r
462                                         </table>\r
463                                 </div>\r
464 \r
465                                 <div class="prt">\r
466                                         <h2><?php echo _ADMIN_HEADER; ?></h2>\r
467                                         <p class="msg">\r
468 <?php\r
469         if ( $isPostback )\r
470         {\r
471                 $errors = $param->check_user_parameters();\r
472                 if ( is_array($errors) )\r
473                 {\r
474                         foreach ( $errors as $error )\r
475                         {\r
476                                 echo '<span class="err">', $error, "</span>\n";\r
477                         }\r
478                 }\r
479         }\r
480 ?>\r
481                                         </p>\r
482                                         <table>\r
483                                                 <tr>\r
484                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>\r
485                                                                 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>\r
486                                                 </tr>\r
487                                                 <tr>\r
488                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>\r
489                                                                 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>\r
490                                                 </tr>\r
491                                                 <tr>\r
492                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span></th>\r
493                                                                 <td><input type="password" name="user_password" /></td>\r
494                                                 </tr>\r
495                                                 <tr>\r
496                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span></th>\r
497                                                                 <td><input type="password" name="user_password2" /></td>\r
498                                                 </tr>\r
499                                                 <tr>\r
500                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>\r
501                                                                 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>\r
502                                                 </tr>\r
503                                         </table>\r
504                                         <p class="sbt">\r
505                                                 <button type="submit" name="action" value="weblog" class="sbt_arw" onclick="OnceSubmit()"><?php echo _INSTALL; ?></button>\r
506                                         </p>\r
507                                 </div>\r
508                         </form>\r
509                 </div>\r
510 <?php\r
511 }\r
512 \r
513 /**\r
514  * Displays a form to the detail settings\r
515  * @param bool $isPostback\r
516  */\r
517 function show_detail_setting_form($isPostback)\r
518 {\r
519         global $param, $minimum_mysql_version;\r
520 \r
521         $mysql_version = getMySqlVersion();\r
522 ?>\r
523                 <div id="container_detailed">\r
524                         <p class="msg">\r
525                                 <?php echo _DETAIL_NAVI1; ?>\r
526 <?php\r
527         if ( $isPostback && !$param->check_all_parameters() )\r
528         {\r
529                 echo '<span class="err">', _VALID_ERROR, "</span>\n";\r
530         }\r
531 ?>\r
532                         </p>\r
533                         <ul class="msg">\r
534                                 <li>PHP: <?php echo phpversion(); ?></li>\r
535                                 <li>MySQL:\r
536 <?php\r
537         echo ($mysql_version == '0.0.0') ? _DBVERSION_UNKOWN : $mysql_version;\r
538         if ( version_compare($mysql_version, $minimum_mysql_version, '<') )\r
539         {\r
540                 echo '<span class="err">', sprintf(_DBVERSION_TOOLOW, $minimum_mysql_version), '</span>';\r
541         }\r
542 ?></li>\r
543                         </ul>\r
544                         <form method="post" action="">\r
545 \r
546                                 <div class="prt">\r
547                                         <h2><?php echo _DETAIL_HEADER1; ?></h2>\r
548                                         <p class="msg">\r
549 <?php\r
550         if ( $isPostback )\r
551         {\r
552                 $errors = $param->check_mysql_parameters();\r
553                 if ( is_array($errors) )\r
554                 {\r
555                         foreach ( $errors as $error )\r
556                         {\r
557                                 echo '<span class="err">', $error, "</span>\n";\r
558                         }\r
559                 }\r
560         }\r
561 ?>\r
562                                         </p>\r
563                                         <table>\r
564                                                 <tr>\r
565                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>\r
566                                                                 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>\r
567                                                 </tr>\r
568                                                 <tr>\r
569                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>\r
570                                                                 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>\r
571                                                 </tr>\r
572                                                 <tr>\r
573                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span></th>\r
574                                                                 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>\r
575                                                 </tr>\r
576                                                 <tr>\r
577                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>\r
578                                                                 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>\r
579                                                 </tr>\r
580                                                 <tr>\r
581                                                         <th><span class="nam"><?php echo _DB_FIELD5; ?></span><span class="sub"><?php echo _DB_FIELD5_DESC; ?></span></th>\r
582                                                                 <td><input type="text" name="mysql_tablePrefix" value="<?php echo $param->mysql_tablePrefix; ?>" /></td>\r
583                                                 </tr>\r
584                                         </table>\r
585 \r
586                                         <h2><?php echo _DETAIL_HEADER2; ?></h2>\r
587                                         <p class="msg">\r
588 <?php\r
589         if ( $isPostback )\r
590         {\r
591                 $errors = $param->check_uri_parameters();\r
592                 if ( is_array($errors) )\r
593                 {\r
594                         foreach ( $errors as $error )\r
595                         {\r
596                                 echo '<span class="err">', $error, "</span>\n";\r
597                         }\r
598                 }\r
599                 $errors = $param->check_path_parameters();\r
600                 if ( is_array($errors) )\r
601                 {\r
602                         foreach ( $errors as $error )\r
603                         {\r
604                                 echo '<span class="err">', $error, "</span>\n";\r
605                         }\r
606                 }\r
607         }\r
608 ?>\r
609                                         </p>\r
610                                         <table>\r
611                                                 <tr>\r
612                                                         <th><span class="nam"><?php echo _PATH_FIELD1; ?></span></th>\r
613                                                                 <td><input type="text" name="IndexURL" value="<?php echo $param->IndexURL; ?>" /></td>\r
614                                                 </tr>\r
615                                                 <tr>\r
616                                                         <th><span class="nam"><?php echo _PATH_FIELD2; ?></span></th>\r
617                                                                 <td><input type="text" name="AdminURL" value="<?php echo $param->AdminURL; ?>" /></td>\r
618                                                 </tr>\r
619                                                 <tr>\r
620                                                         <th><span class="nam"><?php echo _PATH_FIELD3; ?></span></th>\r
621                                                                 <td><input type="text" name="AdminPath" value="<?php echo $param->AdminPath; ?>" /></td>\r
622                                                 </tr>\r
623                                                 <tr>\r
624                                                         <th><span class="nam"><?php echo _PATH_FIELD4; ?></span></th>\r
625                                                                 <td><input type="text" name="MediaURL" value="<?php echo $param->MediaURL; ?>" /></td>\r
626                                                 </tr>\r
627                                                 <tr>\r
628                                                         <th><span class="nam"><?php echo _PATH_FIELD5; ?></span></th>\r
629                                                                 <td><input type="text" name="MediaPath" value="<?php echo $param->MediaPath; ?>" /></td>\r
630                                                 </tr>\r
631                                                 <tr>\r
632                                                         <th><span class="nam"><?php echo _PATH_FIELD6; ?></span></th>\r
633                                                                 <td><input type="text" name="SkinsURL" value="<?php echo $param->SkinsURL; ?>" /></td>\r
634                                                 </tr>\r
635                                                 <tr>\r
636                                                         <th><span class="nam"><?php echo _PATH_FIELD7; ?></span></th>\r
637                                                                 <td><input type="text" name="SkinsPath" value="<?php echo $param->SkinsPath; ?>" /></td>\r
638                                                 </tr>\r
639                                                 <tr>\r
640                                                         <th><span class="nam"><?php echo _PATH_FIELD8; ?></span></th>\r
641                                                                 <td><input type="text" name="PluginURL" value="<?php echo $param->PluginURL; ?>" /></td>\r
642                                                 </tr>\r
643                                                 <tr>\r
644                                                         <th><span class="nam"><?php echo _PATH_FIELD9; ?></span></th>\r
645                                                                 <td><input type="text" name="ActionURL" value="<?php echo $param->ActionURL; ?>" /></td>\r
646                                                 </tr>\r
647                                         </table>\r
648                                         <p class="msg">\r
649                                                 <?php echo _DETAIL_TEXT3; ?>\r
650                                         </p>\r
651 \r
652                                         <h2><?php echo _DETAIL_HEADER3; ?></h2>\r
653                                         <p class="msg">\r
654 <?php\r
655         echo _DETAIL_TEXT4;\r
656         if ( $isPostback )\r
657         {\r
658                 $errors = $param->check_user_parameters();\r
659                 if ( is_array($errors) )\r
660                 {\r
661                         foreach ( $errors as $error )\r
662                         {\r
663                                 echo '<span class="err">', $error, "</span>\n";\r
664                         }\r
665                 }\r
666         }\r
667 ?>\r
668                                         </p>\r
669                                         <table>\r
670                                                 <tr>\r
671                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>\r
672                                                                 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>\r
673                                                 </tr>\r
674                                                 <tr>\r
675                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>\r
676                                                                 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>\r
677                                                 </tr>\r
678                                                 <tr>\r
679                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span></th>\r
680                                                                 <td><input type="password" name="user_password" /></td>\r
681                                                 </tr>\r
682                                                 <tr>\r
683                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span></th>\r
684                                                                 <td><input type="password" name="user_password2" /></td>\r
685                                                 </tr>\r
686                                                 <tr>\r
687                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>\r
688                                                                 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>\r
689                                                 </tr>\r
690                                         </table>\r
691 \r
692                                         <h2><?php echo _DETAIL_HEADER4; ?></h2>\r
693                                         <p class="msg">\r
694 <?php\r
695         echo _DETAIL_TEXT5;\r
696         if ( $isPostback )\r
697         {\r
698                 $errors = $param->check_weblog_parameters();\r
699                 if ( is_array($errors) )\r
700                 {\r
701                         foreach ( $errors as $error )\r
702                         {\r
703                                 echo '<span class="err">', $error, "</span>\n";\r
704                         }\r
705                 }\r
706         }\r
707 ?>\r
708                                         </p>\r
709                                         <table>\r
710                                                 <tr>\r
711                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>\r
712                                                                 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>\r
713                                                 </tr>\r
714                                                 <tr>\r
715                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>\r
716                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>\r
717                                                 </tr>\r
718                                         </table>\r
719 \r
720                                         <p class="msg">\r
721                                                 <?php echo _DETAIL_TEXT6; ?>\r
722                                         </p>\r
723 \r
724                                         <p class="sbt">\r
725                                                 <button type="submit" name="action" value="detail" class="sbt_arw" onclick="OnceSubmit()"><?php echo _INSTALL; ?></button>\r
726                                         </p>\r
727                                 </div>\r
728                         </form>\r
729                 </div>\r
730 <?php\r
731 }\r
732 \r
733 /**\r
734  * Displays a screen to signal the completion of the installation\r
735  */\r
736 function show_install_complete_form()\r
737 {\r
738         global $MYSQL_HANDLER, $param;\r
739         $errors = do_install();\r
740 ?>\r
741                 <div id="container">\r
742                         <p class="msg">\r
743 <?php\r
744         if ( is_array($errors) && count($errors) > 0 )\r
745         {\r
746                 echo _INST_ERROR;\r
747                 foreach ( $errors as $error )\r
748                 {\r
749                         echo '<span class="err">', $error, "</span>\n";\r
750                 }\r
751         }\r
752         else\r
753         {\r
754                 echo _INST_TEXT;\r
755                 if ( array_key_exists('config_data', $_SESSION) )\r
756                 {\r
757                         echo '<span class="err">', _INST_TEXT4, '</span>';\r
758 ?>\r
759 <textarea id="config_text" readonly="readonly" onfocus="SelectText(this);"><?php echo htmlentities($_SESSION['config_data'], null, i18n::get_current_charset()) ?></textarea>\r
760 <?php\r
761                 }\r
762                 else\r
763                 {\r
764                         echo '<span class="err">', _INST_TEXT5, '</span>';\r
765                 }\r
766 ?>\r
767                         </p>\r
768                         <form method="post" action="./index.php">\r
769                                 <div class="prt">\r
770                                         <h2><?php echo _INST_HEADER1; ?></h2>\r
771                                         <p class="msg">\r
772                                                 <?php echo sprintf(_INST_TEXT1, $param->blog_name); ?>\r
773                                         </p>\r
774                                         <p class="sbt">\r
775                                                 <button type="button" name="toBlog" onclick="location.href='<?php echo $param->IndexURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON1; ?></button>\r
776                                         </p>\r
777                                 </div>\r
778 \r
779                                 <div class="prt">\r
780                                         <h2><?php echo _INST_HEADER2; ?></h2>\r
781                                         <p class="msg">\r
782                                                 <?php echo _INST_TEXT2; ?>\r
783                                         </p>\r
784                                         <p class="sbt">\r
785                                                 <button type="button" name="toMng" onclick="location.href='<?php echo $param->AdminURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON2; ?></button>\r
786                                         </p>\r
787                                 </div>\r
788 \r
789                                 <div class="prt">\r
790                                         <h2><?php echo _INST_HEADER3; ?></h2>\r
791                                         <p class="msg">\r
792                                                 <?php echo _INST_TEXT3; ?>\r
793                                         </p>\r
794                                         <p class="sbt">\r
795                                                 <button type="button" name="toAddBlog" onclick="location.href='<?php echo $param->AdminURL; ?>index.php?action=createnewlog';" class="sbt_arw"><?php echo _INST_BUTTON3; ?></button>\r
796                                         </p>\r
797                                 </div>\r
798                         </form>\r
799 <?php\r
800         }\r
801 ?>\r
802                 </div>\r
803 <?php\r
804         unset($param);\r
805 }\r
806 \r
807 /**\r
808  * The installation process itself\r
809  * @return array error messages\r
810  */\r
811 function do_install()\r
812 {\r
813         global $param;\r
814         global $MYSQL_HANDLER, $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_PREFIX, $MYSQL_CONN;\r
815         global $DIR_NUCLEUS, $DIR_MEDIA, $DIR_SKINS, $DIR_PLUGINS, $DIR_LANG, $DIR_LIBS;\r
816         $errors = array();\r
817 \r
818         /*\r
819          * 1. put all param-vars into vars\r
820          */\r
821         $MYSQL_HOST             = $param->mysql_host;\r
822         $MYSQL_USER             = $param->mysql_user;\r
823         $MYSQL_PASSWORD = $param->mysql_password;\r
824         $MYSQL_DATABASE = $param->mysql_database;\r
825         $MYSQL_PREFIX   = $param->mysql_tablePrefix;\r
826 \r
827         $DIR_NUCLEUS    = $param->AdminPath;\r
828         $DIR_MEDIA              = $param->MediaPath;\r
829         $DIR_SKINS              = $param->SkinsPath;\r
830         $DIR_PLUGINS    = $DIR_NUCLEUS . 'plugins/';\r
831         $DIR_LOCALES    = $DIR_NUCLEUS . 'locales/';\r
832         $DIR_LIBS               = $DIR_NUCLEUS . 'libs/';\r
833 \r
834         /*\r
835          * 2.open mySQL connection\r
836          */\r
837         $MYSQL_CONN = @DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD);\r
838         if ( $MYSQL_CONN == FALSE )\r
839         {\r
840                 $errors[] = _DBCONNECT_ERROR;\r
841                 return $errors;\r
842         }\r
843 \r
844         /*\r
845          * 3. try to create database if needed\r
846          */\r
847         if ( DB::execute("CREATE DATABASE IF NOT EXISTS {$MYSQL_DATABASE}") === FALSE )\r
848         {\r
849                 $errinfo = DB::getError();\r
850                 $errors[] = _INST_ERROR1 . ': ' . $errinfo[2];\r
851         }\r
852 \r
853         /*\r
854          * 4. try to select database\r
855          */\r
856         $MYSQL_CONN = @DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE);\r
857         if ( !$MYSQL_CONN )\r
858         {\r
859                 $errors[] = _INST_ERROR2;\r
860         }\r
861 \r
862         if ( count($errors) > 0 )\r
863         {\r
864                 return $errors;\r
865         }\r
866 \r
867         /*\r
868          * 5. execute queries\r
869          */\r
870         $table_names = array(\r
871                 'nucleus_actionlog',\r
872                 'nucleus_ban',\r
873                 'nucleus_blog',\r
874                 'nucleus_category',\r
875                 'nucleus_comment',\r
876                 'nucleus_config',\r
877                 'nucleus_item',\r
878                 'nucleus_karma',\r
879                 'nucleus_member',\r
880                 'nucleus_plugin',\r
881                 'nucleus_skin',\r
882                 'nucleus_template',\r
883                 'nucleus_team',\r
884                 'nucleus_activation',\r
885                 'nucleus_tickets'\r
886         );\r
887 \r
888         $prefixed_table_names = array();\r
889         foreach ( $table_names as $table_name )\r
890         {\r
891                 $prefixed_table_names[] = $MYSQL_PREFIX . $table_name;\r
892         }\r
893 \r
894         // table exists check\r
895         $result = DB::getResult('SHOW TABLES');\r
896         foreach ( $result as $row )\r
897         {\r
898                 if ( in_array($row[0], $prefixed_table_names) )\r
899                 {\r
900                         $errors[] = _INST_ERROR3;\r
901                         break;\r
902                 }\r
903         }\r
904         if ( count($errors) > 0 )\r
905         {\r
906                 return $errors;\r
907         }\r
908 \r
909         $filename = 'install.sql';\r
910         $fd = fopen($filename, 'r');\r
911         $queries = fread($fd, filesize($filename));\r
912         fclose($fd);\r
913 \r
914         $queries = preg_split('#(;\n|;\r)#', $queries);\r
915 \r
916         foreach ( $queries as $query )\r
917         {\r
918                 if ( preg_match('/\w+/', $query) )\r
919                 {\r
920                         if ( $MYSQL_PREFIX )\r
921                         {\r
922                                 $query = str_replace($table_names, $prefixed_table_names, $query);\r
923                         }\r
924 \r
925                         if ( DB::execute($query) === FALSE )\r
926                         {\r
927                                 $errinfo = DB::getError();\r
928                                 $errors[] = _INST_ERROR4 . ' (<small>' . $query . '</small>): ' . $errinfo[2];\r
929                         }\r
930                 }\r
931         }\r
932 \r
933         /*\r
934          * 6. put needed records\r
935          */\r
936         /* push first post */\r
937         $query = "INSERT INTO %s VALUES (1, %s, %s, %s, 1, 1, %s, 0, 0, 0, 1, 0, 1)";\r
938         $query = sprintf($query,\r
939                 tableName('nucleus_item'),\r
940                 DB::quoteValue(_1ST_POST_TITLE),\r
941                 DB::quoteValue(_1ST_POST),\r
942                 DB::quoteValue(_1ST_POST2),\r
943                 DB::formatDateTime()\r
944         );\r
945         if ( DB::execute($query) === FALSE )\r
946         {\r
947                 $errinfo = DB::getError();\r
948                 $errors[] = _INST_ERROR4 . ' (<small>' . $query . '</small>): ' . $errinfo[2];\r
949         }\r
950 \r
951         /* push configurations */\r
952         array_merge($errors, updateConfig('IndexURL', $param->IndexURL));\r
953         array_merge($errors, updateConfig('AdminURL', $param->AdminURL));\r
954         array_merge($errors, updateConfig('MediaURL', $param->MediaURL));\r
955         array_merge($errors, updateConfig('SkinsURL', $param->SkinsURL));\r
956         array_merge($errors, updateConfig('PluginURL', $param->PluginURL));\r
957         array_merge($errors, updateConfig('ActionURL', $param->ActionURL));\r
958         array_merge($errors, updateConfig('AdminEmail', $param->user_email));\r
959         array_merge($errors, updateConfig('SiteName', $param->blog_name));\r
960         array_merge($errors, updateConfig('Locale', i18n::get_current_locale()));\r
961 \r
962         /* push super admin */\r
963         $query = "UPDATE %s SET mname = %s, mrealname = %s, mpassword = %s, memail = %s, murl = %s, madmin = 1, mcanlogin = 1 WHERE mnumber = 1";\r
964         $query = sprintf($query,\r
965                 tableName('nucleus_member'),\r
966                 DB::quoteValue($param->user_name),\r
967                 DB::quoteValue($param->user_realname),\r
968                 DB::quoteValue(md5($param->user_password)),\r
969                 DB::quoteValue($param->user_email),\r
970                 DB::quoteValue($param->IndexURL)\r
971         );\r
972         if ( DB::execute($query) === FALSE )\r
973         {\r
974                 $errinfo = DB::getError();\r
975                 $errors[] = _INST_ERROR5 . ': ' . $errinfo[2];\r
976         }\r
977 \r
978         /* push new weblog */\r
979         $query = "UPDATE %s SET bname = %s, bshortname = %s, burl = %s WHERE bnumber = 1";\r
980         $query = sprintf($query,\r
981                 tableName('nucleus_blog'),\r
982                 DB::quoteValue($param->blog_name),\r
983                 DB::quoteValue($param->blog_shortname),\r
984                 DB::quoteValue($param->IndexURL)\r
985         );\r
986         if ( DB::execute($query) === FALSE )\r
987         {\r
988                 $errinfo = DB::getError();\r
989                 $errors[] = _INST_ERROR6 . ': ' . $errinfo[2];\r
990         }\r
991 \r
992         /* push default category */\r
993         $query = "UPDATE %s SET cname = %s, cdesc = %s WHERE catid = 1";\r
994         $query = sprintf($query,\r
995                 tableName('nucleus_category'),\r
996                 DB::quoteValue(_GENERALCAT_NAME),\r
997                 DB::quoteValue(_GENERALCAT_DESC)\r
998         );\r
999         if ( DB::execute($query) === FALSE )\r
1000         {\r
1001                 $errinfo = DB::getError();\r
1002                 $errors[] = _INST_ERROR6 . ': ' . $errinfo[2];\r
1003         }\r
1004 \r
1005         /*\r
1006          * 7. install default plugins and skins\r
1007          */\r
1008         global $aConfPlugsToInstall, $aConfSkinsToImport;\r
1009         $aSkinErrors = array();\r
1010         $aPlugErrors = array();\r
1011 \r
1012         if ( (count($aConfPlugsToInstall) > 0) || (count($aConfSkinsToImport) > 0) )\r
1013         {\r
1014                 include_once($DIR_LIBS . 'globalfunctions.php');\r
1015                 global $manager;\r
1016                 if ( !isset($manager) )\r
1017                 {\r
1018                         $manager = new Manager();\r
1019                 }\r
1020 \r
1021                 include_once($DIR_LIBS . 'skinie.php');\r
1022                 \r
1023                 $aSkinErrors = installCustomSkins();\r
1024                 if ( count($aSkinErrors) > 0 )\r
1025                 {\r
1026                         array_merge($errors, $aSkinErrors);\r
1027                 }\r
1028 \r
1029                 $query          = "SELECT sdnumber FROM %s WHERE sdname='default'";\r
1030                 $query          = sprintf($query, tableName('nucleus_skin_desc'));\r
1031                 $defSkinID      = intval(DB::getValue($query));\r
1032 \r
1033                 $query = "UPDATE %s SET bdefskin=%d WHERE bnumber=1";\r
1034                 $query = sprintf($query, tableName('nucleus_blog'), $defSkinID);\r
1035                 DB::execute($query);\r
1036                 \r
1037                 $query = "UPDATE %s SET value=%d WHERE name='BaseSkin'";\r
1038                 $query = sprintf($query, tableName('nucleus_config'), $defSkinID);\r
1039                 DB::execute($query);\r
1040 \r
1041                 $aPlugErrors = installCustomPlugs();\r
1042                 if ( count($aPlugErrors) > 0 )\r
1043                 {\r
1044                         array_merge($errors, $aPlugErrors);\r
1045                 }\r
1046         }\r
1047 \r
1048         /*\r
1049          * 8. Write config file ourselves (if possible)\r
1050          */\r
1051         $config_data = '<' . '?php' . "\n";\r
1052         $config_data .= "// mySQL connection information\n";\r
1053         $config_data .= "\$MYSQL_HOST = '" . $MYSQL_HOST . "';\n";\r
1054         $config_data .= "\$MYSQL_USER = '" . $MYSQL_USER . "';\n";\r
1055         $config_data .= "\$MYSQL_PASSWORD = '" . $MYSQL_PASSWORD . "';\n";\r
1056         $config_data .= "\$MYSQL_DATABASE = '" . $MYSQL_DATABASE . "';\n";\r
1057         $config_data .= "\$MYSQL_PREFIX = '" . $MYSQL_PREFIX . "';\n";\r
1058         $config_data .= "// new in 3.50. first element is db handler, the second is the db driver used by the handler\n";\r
1059         $config_data .= "// default is \$MYSQL_HANDLER = array('mysql','mysql');\n";\r
1060         $config_data .= "//\$MYSQL_HANDLER = array('mysql','mysql');\n";\r
1061         $config_data .= "//\$MYSQL_HANDLER = array('pdo','mysql');\n";\r
1062         $config_data .= "\$MYSQL_HANDLER = array('" . $MYSQL_HANDLER[0] . "','" . $MYSQL_HANDLER[1] . "');\n";\r
1063         $config_data .= "\n";\r
1064         $config_data .= "// main nucleus directory\n";\r
1065         $config_data .= "\$DIR_NUCLEUS = '" . $DIR_NUCLEUS . "';\n";\r
1066         $config_data .= "\n";\r
1067         $config_data .= "// path to media dir\n";\r
1068         $config_data .= "\$DIR_MEDIA = '" . $DIR_MEDIA . "';\n";\r
1069         $config_data .= "\n";\r
1070         $config_data .= "// extra skin files for imported skins\n";\r
1071         $config_data .= "\$DIR_SKINS = '" . $DIR_SKINS . "';\n";\r
1072         $config_data .= "\n";\r
1073         $config_data .= "// these dirs are normally sub dirs of the nucleus dir, but \n";\r
1074         $config_data .= "// you can redefine them if you wish\n";\r
1075         $config_data .= "\$DIR_PLUGINS = \$DIR_NUCLEUS . 'plugins/';\n";\r
1076         $config_data .= "\$DIR_LOCALES = \$DIR_NUCLEUS . 'locales/';\n";\r
1077         $config_data .= "\$DIR_LIBS = \$DIR_NUCLEUS . 'libs/';\n";\r
1078         $config_data .= "\n";\r
1079         $config_data .= "// include libs\n";\r
1080         $config_data .= "include(\$DIR_LIBS.'globalfunctions.php');\n";\r
1081         $config_data .= "?" . ">";\r
1082 \r
1083         $result = false;\r
1084         if ( @!file_exists('../config.php') || is_writable('../config.php') )\r
1085         {\r
1086                 if ( $fp = @fopen('../config.php', 'w') )\r
1087                 {\r
1088                         $result = @fwrite($fp, $config_data, i18n::strlen($config_data));\r
1089                         fclose($fp);\r
1090                 }\r
1091         }\r
1092 \r
1093         if ( $result )\r
1094         {\r
1095                 // try to change the read-only permission.\r
1096                 if ( strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )\r
1097                 {\r
1098                         @chmod('../config.php', 0444);\r
1099                 }\r
1100         }\r
1101         else\r
1102         {\r
1103                 $_SESSION['config_data'] = $config_data;\r
1104         }\r
1105 \r
1106         return $errors;\r
1107 }\r
1108 \r
1109 /**\r
1110  * Confirm that you can write to the configuration file\r
1111  * @return string error message\r
1112  */\r
1113 function canConfigFileWritable()\r
1114 {\r
1115         if ( @file_exists('../config.php') && @!is_writable('../config.php') )\r
1116         {\r
1117                 // try to change the read-write permission.\r
1118                 if ( strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )\r
1119                 {\r
1120                         @chmod('../config.php', 0666);\r
1121                 }\r
1122 \r
1123                 if ( @!is_writable('../config.php') )\r
1124                 {\r
1125                         return _INST_ERROR8;\r
1126                 }\r
1127         }\r
1128         return '';\r
1129 }\r
1130 \r
1131 /**\r
1132  * To obtain the version of MySQL\r
1133  * @return string\r
1134  */\r
1135 function getMySqlVersion()\r
1136 {\r
1137         global $MYSQL_HANDLER, $minimum_mysql_version, $errors;\r
1138         // Turn on output buffer\r
1139         // Needed to repress the output of the sql function that are\r
1140         // not part of php (in this case the @ operator doesn't work)\r
1141         ob_start();\r
1142 \r
1143         // note: this piece of code is taken from phpMyAdmin\r
1144         $conn = @DB::setConnectionInfo($MYSQL_HANDLER[1], 'localhost', '', '');\r
1145 \r
1146         if ( $conn )\r
1147         {\r
1148                 $row = DB::getAttribute(PDO::ATTR_SERVER_VERSION);\r
1149                 $match = preg_split('#\.#', $row);\r
1150         }\r
1151         else\r
1152         {\r
1153                 $row = @DB::getRow('SHOW VARIABLES LIKE \'version\'');\r
1154 \r
1155                 if ( $row )\r
1156                 {\r
1157                         $match = preg_split('#\.#', $row[1]);\r
1158                 }\r
1159                 else\r
1160                 {\r
1161                         $output = (function_exists('shell_exec')) ? @shell_exec('mysql -V') : '0.0.0';\r
1162                         preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);\r
1163                         $match = preg_split('#\.#', $version[0]);\r
1164 \r
1165                         if ( $match[0] == '' )\r
1166                         {\r
1167                                 $match = array('0', '0', '0');\r
1168                         }\r
1169                 }\r
1170         }\r
1171 \r
1172         @DB::disConnect();\r
1173 \r
1174         //End and clean output buffer\r
1175         ob_end_clean();\r
1176 \r
1177         return implode($match, '.');\r
1178 }\r
1179 \r
1180 /**\r
1181  * Add a table prefix if it is used\r
1182  *\r
1183  * @param string $input table name with prefix\r
1184  * @return string\r
1185  */\r
1186 function tableName($input)\r
1187 {\r
1188         global $MYSQL_PREFIX;\r
1189         if ( $MYSQL_PREFIX )\r
1190         {\r
1191                 return $MYSQL_PREFIX . $input;\r
1192         }\r
1193         else\r
1194         {\r
1195                 return $input;\r
1196         }\r
1197 }\r
1198 \r
1199 /**\r
1200  * Install custom plugins\r
1201  */\r
1202 function installCustomPlugs()\r
1203 {\r
1204         global $aConfPlugsToInstall, $DIR_LIBS, $manager;\r
1205 \r
1206         $aErrors = array();\r
1207         if ( count($aConfPlugsToInstall) == 0 )\r
1208         {\r
1209                 return $aErrors;\r
1210         }\r
1211 \r
1212         $query = sprintf('SELECT * FROM %s', tableName('nucleus_plugin'));\r
1213         $res = DB::getResult($query);\r
1214         $numCurrent = $res->rowCount();\r
1215 \r
1216         foreach ( $aConfPlugsToInstall as $plugName )\r
1217         {\r
1218                 $query = sprintf('INSERT INTO %s (porder, pfile) VALUES (%d, %s)',\r
1219                         tableName('nucleus_plugin'),\r
1220                         (++$numCurrent),\r
1221                         DB::quoteValue($plugName));\r
1222                 DB::execute($query);\r
1223 \r
1224                 $manager->clearCachedInfo('installedPlugins');\r
1225                 $plugin =& $manager->getPlugin($plugName);\r
1226                 $plugin->setID($numCurrent);\r
1227 \r
1228                 if ( !$plugin )\r
1229                 {\r
1230                         $query = sprintf('DELETE FROM %s WHERE pfile = %s',\r
1231                                 tableName('nucleus_plugin'),\r
1232                                 DB::quoteValue($plugName));\r
1233                         DB::execute($query);\r
1234                         $numCurrent--;\r
1235                         array_push($aErrors, sprintf(_INST_ERROR9, $plugName));\r
1236                         continue;\r
1237                 }\r
1238                 $plugin->install();\r
1239         }\r
1240 \r
1241         $query = sprintf('DELETE FROM %s', tableName('nucleus_plugin_event'));\r
1242         DB::execute($query);\r
1243         $query = sprintf('SELECT pid, pfile FROM %s', tableName('nucleus_plugin'));\r
1244         $res = DB::getResult($query);\r
1245 \r
1246         foreach ( $res as $row )\r
1247         {\r
1248                 $plug =& $manager->getPlugin($row['pfile']);\r
1249 \r
1250                 if ( $plug )\r
1251                 {\r
1252                         $eventList = $plug->getEventList();\r
1253                         foreach ( $eventList as $eventName )\r
1254                         {\r
1255                                 $query = sprintf('INSERT INTO %s (pid, event) VALUES (%d, %s)',\r
1256                                         tableName('nucleus_plugin_event'),\r
1257                                         intval($row['pid']),\r
1258                                         DB::quoteValue($eventName));\r
1259                                 DB::execute($query);\r
1260                         }\r
1261                 }\r
1262         }\r
1263         return $aErrors;\r
1264 }\r
1265 \r
1266 /**\r
1267  * Install custom skins\r
1268  * Prepares the installation of custom skins\r
1269  */\r
1270 function installCustomSkins()\r
1271 {\r
1272         global $aConfSkinsToImport, $DIR_LIBS, $DIR_SKINS;\r
1273 \r
1274         $aErrors = array();\r
1275         if ( count($aConfSkinsToImport) == 0 )\r
1276         {\r
1277                 return $aErrors;\r
1278         }\r
1279 \r
1280         $importer = new SkinImport();\r
1281 \r
1282         foreach ( $aConfSkinsToImport as $skinName )\r
1283         {\r
1284                 $importer->reset();\r
1285                 $skinFile = $DIR_SKINS . $skinName . '/skinbackup.xml';\r
1286 \r
1287                 if ( !@file_exists($skinFile) )\r
1288                 {\r
1289                         array_push($aErrors, sprintf(_INST_ERROR10, $skinFile));\r
1290                         continue;\r
1291                 }\r
1292 \r
1293                 $error = $importer->readFile($skinFile);\r
1294 \r
1295                 if ( $error )\r
1296                 {\r
1297                         array_push($aErrors, sprintf(_INST_ERROR11, $skinName) . ' : ' . $error);\r
1298                         continue;\r
1299                 }\r
1300 \r
1301                 $error = $importer->writeToDatabase(1);\r
1302 \r
1303                 if ( $error )\r
1304                 {\r
1305                         array_push($aErrors, sprintf(_INST_ERROR12, $skinName) . ' : ' . $error);\r
1306                         continue;\r
1307                 }\r
1308         }\r
1309         return $aErrors;\r
1310 }\r
1311 \r
1312 /**\r
1313  * Check if some important files of the Nucleus CMS installation are available\r
1314  * Give an error if one or more files are not accessible\r
1315  */\r
1316 function do_check_files()\r
1317 {\r
1318         $missingfiles = array();\r
1319         $files = array(\r
1320                 './install.sql',\r
1321                 '../index.php',\r
1322                 '../action.php',\r
1323                 '../nucleus/index.php',\r
1324                 '../nucleus/media.php',\r
1325                 '../nucleus/libs/ACTION.php',\r
1326                 '../nucleus/libs/ACTIONLOG.php',\r
1327                 '../nucleus/libs/ACTIONS.php',\r
1328                 '../nucleus/libs/ADMIN.php',\r
1329                 '../nucleus/libs/BaseActions.php',\r
1330                 '../nucleus/libs/BLOG.php',\r
1331                 '../nucleus/libs/BODYACTIONS.php',\r
1332                 '../nucleus/libs/COMMENT.php',\r
1333                 '../nucleus/libs/COMMENTACTIONS.php',\r
1334                 '../nucleus/libs/COMMENTS.php',\r
1335                 '../nucleus/libs/ENCAPSULATE.php',\r
1336                 '../nucleus/libs/ENTITY.php',\r
1337                 '../nucleus/libs/globalfunctions.php',\r
1338                 '../nucleus/libs/i18n.php',\r
1339                 '../nucleus/libs/ITEM.php',\r
1340                 '../nucleus/libs/ITEMACTIONS.php',\r
1341                 '../nucleus/libs/LINK.php',\r
1342                 '../nucleus/libs/MANAGER.php',\r
1343                 '../nucleus/libs/MEDIA.php',\r
1344                 '../nucleus/libs/MEMBER.php',\r
1345                 '../nucleus/libs/NOTIFICATION.php',\r
1346                 '../nucleus/libs/PAGEFACTORY.php',\r
1347                 '../nucleus/libs/PARSER.php',\r
1348                 '../nucleus/libs/PLUGIN.php',\r
1349                 '../nucleus/libs/PLUGINADMIN.php',\r
1350                 '../nucleus/libs/SEARCH.php',\r
1351                 '../nucleus/libs/showlist.php',\r
1352                 '../nucleus/libs/SKIN.php',\r
1353                 '../nucleus/libs/TEMPLATE.php',\r
1354                 '../nucleus/libs/vars4.1.0.php',\r
1355                 '../nucleus/libs/xmlrpc.inc.php',\r
1356                 '../nucleus/libs/xmlrpcs.inc.php',\r
1357                 '../nucleus/libs/sql/DB.php',\r
1358                 '../nucleus/libs/sql/MYSQLPDO.php'\r
1359         );\r
1360 \r
1361         $count = count($files);\r
1362         for ( $i = 0; $i < $count; $i++ )\r
1363         {\r
1364                 if ( !is_readable($files[$i]) )\r
1365                 {\r
1366                         array_push($missingfiles, 'File <b>' . $files[$i] . '</b> is missing or not readable.<br />');\r
1367                 }\r
1368         }\r
1369 \r
1370         if ( count($missingfiles) > 0 )\r
1371         {\r
1372                 exit(implode("\n", $missingfiles));\r
1373         }\r
1374 }\r
1375 \r
1376 /**\r
1377  * Updates the configuration in the database\r
1378  *\r
1379  * @param string $name name of the config var\r
1380  * @param string $value new value of the config var\r
1381  * @return array\r
1382  */\r
1383 function updateConfig($name, $value)\r
1384 {\r
1385         $errors = array();\r
1386 \r
1387         $query = "UPDATE %s SET value = %s WHERE name = %s";\r
1388         $query = sprintf($query, tableName('nucleus_config'), DB::quoteValue(trim($value)), DB::quoteValue($name));\r
1389 \r
1390         if ( DB::execute($query) === FALSE )\r
1391         {\r
1392                 $errinfo = DB::getError();\r
1393                 $errors[] = _INST_ERROR4 . ': ' . $errinfo[2];\r
1394         }\r
1395         return $errors;\r
1396 }\r
1397 \r
1398 class ParamManager\r
1399 {\r
1400         /* process parameter */\r
1401         public $state;\r
1402         public $locale;\r
1403 \r
1404         /* mysql connection parameters */\r
1405         public $mysql_host;\r
1406         public $mysql_user;\r
1407         public $mysql_password;\r
1408         public $mysql_database;\r
1409         public $mysql_tablePrefix;\r
1410 \r
1411         /* weblog configuration parameters */\r
1412         public $blog_name;\r
1413         public $blog_shortname;\r
1414 \r
1415         /* member configuration parameters */\r
1416         public $user_name;\r
1417         public $user_realname;\r
1418         public $user_password;\r
1419         private $user_password2;\r
1420         public $user_email;\r
1421 \r
1422         /* URI parameters  */\r
1423         private $root_url;\r
1424         public $IndexURL;\r
1425         public $AdminURL;\r
1426         public $MediaURL;\r
1427         public $SkinsURL;\r
1428         public $PluginURL;\r
1429         public $ActionURL;\r
1430 \r
1431         /* path parameters */\r
1432         private $root_path;\r
1433         public $AdminPath;\r
1434         public $MediaPath;\r
1435         public $SkinsPath;\r
1436 \r
1437         /**\r
1438          * constructor\r
1439          */\r
1440         public function __construct()\r
1441         {\r
1442                 $this->init();\r
1443         }\r
1444 \r
1445         public function init()\r
1446         {\r
1447                 // set default values\r
1448                 $this->state = 'locale';\r
1449                 $this->install_mode = 'simple';\r
1450                 $this->locale = 'en_Latn_US';\r
1451                 $this->mysql_host = @ini_get('mysql.default_host');\r
1452                 $this->blog_name = 'My Nucleus CMS';\r
1453                 $this->blog_shortname = 'mynucleuscms';\r
1454 \r
1455                 /* root path */\r
1456                 $directory_separator = preg_quote(DIRECTORY_SEPARATOR, '|');\r
1457                 $this->root_path = implode('/', preg_split("|$directory_separator|", realpath(dirname(__FILE__) . '/..')));\r
1458                 if ( substr($this->root_path, -1, 1) !== '/' )\r
1459                 {\r
1460                         $this->root_path .= '/';\r
1461                 }\r
1462                 $base_path_pcre = preg_quote($this->root_path, '|');\r
1463 \r
1464                 /* current directry name */\r
1465                 $directory_name = preg_replace("#{$base_path_pcre}#", '', implode('/', preg_split("#{$directory_separator}#", realpath(dirname(__FILE__)))));\r
1466                 $directory_name_pcre = preg_quote($directory_name, '|');\r
1467 \r
1468                 /* root uri */\r
1469                 $root_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];\r
1470                 $this->root_url = preg_replace("|$directory_name_pcre(.*)$|", '', $root_url);\r
1471 \r
1472                 $this->AdminPath = $this->root_path . 'nucleus/';\r
1473                 $this->MediaPath = $this->root_path . 'media/';\r
1474                 $this->SkinsPath = $this->root_path . 'skins/';\r
1475 \r
1476                 $this->IndexURL  = $this->root_url;\r
1477                 $this->AdminURL  = $this->root_url . 'nucleus/';\r
1478                 $this->MediaURL  = $this->root_url . 'media/';\r
1479                 $this->SkinsURL  = $this->root_url . 'skins/';\r
1480                 $this->PluginURL = $this->root_url . 'nucleus/plugins/';\r
1481                 $this->ActionURL = $this->root_url . 'action.php';\r
1482         }\r
1483 \r
1484         private function read_parameter($parameter)\r
1485         {\r
1486                 foreach ( $parameter as $element )\r
1487                 {\r
1488                         if ( array_key_exists($element, $_POST) )\r
1489                         {\r
1490                                 $this->$element = $_POST[$element];\r
1491                         }\r
1492                 }\r
1493         }\r
1494 \r
1495         public function set_state($state)\r
1496         {\r
1497                 $states = array('locale', 'mysql', 'weblog', 'detail', 'install');\r
1498                 if ( in_array($state, $states) )\r
1499                 {\r
1500                         $this->state = $state;\r
1501                 }\r
1502         }\r
1503 \r
1504         public function set_locale()\r
1505         {\r
1506                 $this->read_parameter(array('locale'));\r
1507 \r
1508                 if ( !in_array($this->locale, i18n::get_available_locale_list()) )\r
1509                 {\r
1510                         $this->locale = 'en_Latn_US';\r
1511                 }\r
1512         }\r
1513 \r
1514         public function check_mysql_parameters()\r
1515         {\r
1516                 global $MYSQL_HANDLER;\r
1517                 \r
1518                 $parameters = array('mysql_host', 'mysql_user', 'mysql_password', 'mysql_database', 'mysql_tablePrefix');\r
1519                 $this->read_parameter($parameters);\r
1520 \r
1521                 $errors = array();\r
1522                 if ( $this->mysql_host == '' )\r
1523                 {\r
1524                         $errors[] = sprintf(_VALID_ERROR1, _DB_FIELD1);\r
1525                 }\r
1526 \r
1527                 if ( $this->mysql_user == '' )\r
1528                 {\r
1529                         $errors[] = sprintf(_VALID_ERROR1, _DB_FIELD2);\r
1530                 }\r
1531                 \r
1532                 if ( $this->mysql_user != ''\r
1533                         && !preg_match('/^[a-z0-9_\-]+$/i', $this->mysql_user) )\r
1534                 {\r
1535                         $errors[] = sprintf(_VALID_ERROR2, _DB_FIELD2);\r
1536                 }\r
1537                 \r
1538                 if ( $this->mysql_database == '' )\r
1539                 {\r
1540                         $errors[] = sprintf(_VALID_ERROR1, _DB_FIELD4);\r
1541                 }\r
1542 \r
1543                 if ( $this->mysql_database != ''\r
1544                         && !preg_match('/^[a-z0-9_\-]+$/i', $this->mysql_database) )\r
1545                 {\r
1546                         $errors[] = sprintf(_VALID_ERROR2, _DB_FIELD4);\r
1547                 }\r
1548 \r
1549                 if ( $this->mysql_tablePrefix != ''\r
1550                         && !preg_match('/^[a-z0-9_]+$/i', $this->mysql_tablePrefix) )\r
1551                 {\r
1552                         $errors[] = sprintf(_VALID_ERROR3, _DB_FIELD5);\r
1553                 }\r
1554                 \r
1555                 if ( count($errors) == 0 )\r
1556                 {\r
1557                         $mysql_conn = @DB::setConnectionInfo($MYSQL_HANDLER[1], $this->mysql_host, $this->mysql_user, $this->mysql_password);\r
1558                         if ( $mysql_conn == false )\r
1559                         {\r
1560                                 $errors[] = _DBCONNECT_ERROR;\r
1561                         }\r
1562                         else\r
1563                         {\r
1564                                 @DB::disConnect();\r
1565                         }\r
1566                 }\r
1567 \r
1568                 return $errors;\r
1569         }\r
1570 \r
1571         public function check_user_parameters()\r
1572         {\r
1573                 $parameters = array('user_name', 'user_realname', 'user_password', 'user_password2', 'user_email');\r
1574                 $this->read_parameter($parameters);\r
1575 \r
1576                 $errors = array();\r
1577                 if ( $this->user_realname == '' )\r
1578                 {\r
1579                         $errors[] = sprintf(_VALID_ERROR1, _ADMIN_FIELD1);\r
1580                 }\r
1581 \r
1582                 if ( $this->user_name == '' )\r
1583                 {\r
1584                         $errors[] = sprintf(_VALID_ERROR1, _ADMIN_FIELD2);\r
1585                 }\r
1586                 elseif ( !preg_match("/^[a-z0-9]+([ a-z0-9]*[a-z0-9]+)?$/i", $this->user_name) )\r
1587                 {\r
1588                         $errors[] = _VALID_ERROR5;\r
1589                 }\r
1590 \r
1591                 if ( $this->user_password == '' || $this->user_password2 == '' )\r
1592                 {\r
1593                         $errors[] = sprintf(_VALID_ERROR1, _ADMIN_FIELD3);\r
1594                         $this->user_password = '';\r
1595                 }\r
1596                 elseif ( $this->user_password != $this->user_password2 )\r
1597                 {\r
1598                         $errors[] = _VALID_ERROR6;\r
1599                         $this->user_password = '';\r
1600                 }\r
1601 \r
1602                 if ( !preg_match("/^[a-z0-9\._+\-]+@[a-z0-9\._\-]+\.[a-z]{2,6}$/i", $this->user_email) )\r
1603                 {\r
1604                         $errors[] = _VALID_ERROR7;\r
1605                 }\r
1606 \r
1607                 return $errors;\r
1608         }\r
1609 \r
1610         public function check_weblog_parameters()\r
1611         {\r
1612                 $parameters = array('blog_name', 'blog_shortname');\r
1613                 $this->read_parameter($parameters);\r
1614 \r
1615                 $errors = array();\r
1616                 if ( $this->blog_name == '' )\r
1617                 {\r
1618                         $errors[] = sprintf(_VALID_ERROR1, _BLOG_FIELD1);\r
1619                 }\r
1620 \r
1621                 if ( $this->blog_shortname == '' )\r
1622                 {\r
1623                         $errors[] = sprintf(_VALID_ERROR1, _BLOG_FIELD2);\r
1624                 }\r
1625 \r
1626                 if ( !preg_match("/^[a-z0-9]+$/i", $this->blog_shortname) )\r
1627                 {\r
1628                         $errors[] = _VALID_ERROR4;\r
1629                 }\r
1630 \r
1631                 return $errors;\r
1632         }\r
1633 \r
1634         public function check_uri_parameters()\r
1635         {\r
1636                 $parameters = array('IndexURL', 'AdminURL', 'MediaURL', 'SkinsURL', 'PluginURL', 'ActionURL');\r
1637                 $this->read_parameter($parameters);\r
1638 \r
1639                 $errors = array();\r
1640                 if ( substr($this->IndexURL, -1, 1) !== '/' )\r
1641                 {\r
1642                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD1);\r
1643                 }\r
1644 \r
1645                 if ( substr($this->AdminURL, -1, 1) !== '/' )\r
1646                 {\r
1647                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD2);\r
1648                 }\r
1649 \r
1650                 if ( substr($this->MediaURL, -1, 1) !== '/' )\r
1651                 {\r
1652                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD4);\r
1653                 }\r
1654 \r
1655                 if ( substr($this->SkinsURL, -1, 1) !== '/' )\r
1656                 {\r
1657                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD6);\r
1658                 }\r
1659 \r
1660                 if ( substr($this->PluginURL, -1, 1) !== '/' )\r
1661                 {\r
1662                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD8);\r
1663                 }\r
1664 \r
1665                 if ( strrchr($this->ActionURL, '/') != '/action.php' )\r
1666                 {\r
1667                         $errors[] = sprintf(_VALID_ERROR9, _PATH_FIELD9);\r
1668                 }\r
1669 \r
1670                 return $errors;\r
1671         }\r
1672 \r
1673         public function check_path_parameters()\r
1674         {\r
1675                 $parameters = array('AdminPath', 'MediaPath', 'SkinsPath');\r
1676                 $this->read_parameter($parameters);\r
1677 \r
1678                 $separators = array('/', DIRECTORY_SEPARATOR);\r
1679                 $errors = array();\r
1680                 if ( !in_array(substr($this->AdminPath, -1, 1), $separators) )\r
1681                 {\r
1682                         $errors[] = sprintf(_VALID_ERROR10, _PATH_FIELD3);\r
1683                 }\r
1684                 elseif ( !file_exists($this->AdminPath) )\r
1685                 {\r
1686                         $errors[] = sprintf(_VALID_ERROR11, _PATH_FIELD3);\r
1687                 }\r
1688 \r
1689                 if ( !in_array(substr($this->MediaPath, -1, 1), $separators) )\r
1690                 {\r
1691                         $errors[] = sprintf(_VALID_ERROR10, _PATH_FIELD5);\r
1692                 }\r
1693                 elseif ( !file_exists($this->MediaPath) )\r
1694                 {\r
1695                         $errors[] = sprintf(_VALID_ERROR11, _PATH_FIELD5);\r
1696                 }\r
1697 \r
1698                 if ( !in_array(substr($this->SkinsPath, -1, 1), $separators) )\r
1699                 {\r
1700                         $errors[] = sprintf(_VALID_ERROR10, _PATH_FIELD7);\r
1701                 }\r
1702                 elseif ( !file_exists($this->SkinsPath) )\r
1703                 {\r
1704                         $errors[] = sprintf(_VALID_ERROR11, _PATH_FIELD7);\r
1705                 }\r
1706 \r
1707                 return $errors;\r
1708         }\r
1709 \r
1710         /**\r
1711          * check all parameters\r
1712          * @return bool\r
1713          */\r
1714         public function check_all_parameters()\r
1715         {\r
1716                 $this->set_locale();\r
1717 \r
1718                 $isValid = true;\r
1719                 $isValid &= (count($this->check_mysql_parameters()) == 0);\r
1720                 $isValid &= (count($this->check_user_parameters()) == 0);\r
1721                 $isValid &= (count($this->check_weblog_parameters()) == 0);\r
1722                 $isValid &= (count($this->check_uri_parameters()) == 0);\r
1723                 $isValid &= (count($this->check_path_parameters()) == 0);\r
1724 \r
1725                 return $isValid;\r
1726         }\r
1727 }\r
1728 =======
1729 <?php
1730 /*
1731  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
1732  * Copyright (C) 2002-2012 The Nucleus Group
1733  *
1734  * This program is free software; you can redistribute it and/or
1735  * modify it under the terms of the GNU General Public License
1736  * as published by the Free Software Foundation; either version 2
1737  * of the License, or (at your option) any later version.
1738  * (see nucleus/documentation/index.html#license for more info)
1739  *
1740  * This script will install the Nucleus tables in your SQL-database,
1741  * and initialize the data in those tables.
1742  */
1743
1744 /**
1745  * @license http://nucleuscms.org/license.txt GNU General Public License
1746  * @copyright Copyright (C) 2002-2012 The Nucleus Group
1747  * @version $Id: index.php 1889 2012-06-17 08:46:45Z sakamocchi $
1748  */
1749
1750 /* global values initialize */
1751 $CONF = array();
1752
1753 /* reporting all errors for support */
1754 error_reporting(E_ALL);
1755
1756 $minimum_php_version = '5.0.6';
1757 $minimum_mysql_version = '3.23';
1758
1759 $page_footer_copyright = '&copy; 2001-2012 The Nucleus Groupe . Running Nucleus CMS v4.00';
1760
1761 // begin if: server's PHP version is below the minimum; halt installation
1762 if ( version_compare(PHP_VERSION, $minimum_php_version, '<') )
1763 {
1764         exit('<div style="font-size: xx-large;"> Nucleus requires at least PHP version ' . $minimum_php_version . '</div>');
1765 }
1766
1767 // make sure there's no unnecessary escaping: # set_magic_quotes_runtime(0);
1768 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
1769 {
1770         ini_set('magic_quotes_runtime', '0');
1771 }
1772
1773 /* default installed plugins and skins */
1774 $aConfPlugsToInstall = array('NP_SecurityEnforcer', 'NP_SkinFiles', 'NP_Medium');
1775 $aConfSkinsToImport = array('atom', 'rss2.0', 'rsd', 'default', 'admin/default', 'admin/bookmarklet');
1776
1777 // Check if some important files
1778 do_check_files();
1779
1780 /* i18n class is needed for internationalization */
1781 include_once('../nucleus/libs/i18n.php');
1782 if ( !i18n::init('UTF-8', '../nucleus/locales') )
1783 {
1784         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>');
1785 }
1786
1787 // include core classes that are needed for login & plugin handling
1788
1789 // added for 3.5 sql_* wrapper
1790 global $MYSQL_HANDLER;
1791
1792 if ( !isset($MYSQL_HANDLER) )
1793 {
1794         $MYSQL_HANDLER = array('mysql', '');
1795         
1796         // check if mysql support is installed; this check may not make sense, as is, in a version past 3.5x
1797         if ( !function_exists('mysql_query') && !function_exists('mysqli_query') )
1798         {
1799                 exit('<div style="font-size: xx-large;"> Your PHP version does not have support for MySQL :( </div>');
1800 }
1801 }
1802 include_once('../nucleus/libs/sql/sql.php');
1803
1804 session_start();
1805 if ( count($_GET) == 0 && count($_POST) == 0 )
1806 {
1807         unset($_SESSION['param_manager']);
1808 }
1809
1810 // restore the $param from the session
1811 if ( array_key_exists('param_manager', $_SESSION) )
1812 {
1813         $param = $_SESSION['param_manager'];
1814 }
1815 else
1816 {
1817         $param = new ParamManager();
1818 }
1819
1820 // include translation file
1821 $param->set_locale();
1822
1823 do_action();
1824
1825 // $param is saved to the session
1826 if ( isset($param) )
1827 {
1828         $_SESSION['param_manager'] = $param;
1829 }
1830 else
1831 {
1832         unset($_SESSION['param_manager']);
1833 }
1834 exit;
1835
1836 /**
1837  * installer action
1838  */
1839 function do_action()
1840 {
1841         global $param;
1842
1843         if ( array_key_exists('action', $_POST) )
1844         {
1845                 $isPostback = true;
1846         }
1847         else
1848         {
1849                 $isPostback = false;
1850         }
1851
1852         // mode change
1853         if ( array_key_exists('mode', $_REQUEST) )
1854         {
1855                 if ( $_REQUEST['mode'] == 'detail' )
1856                 {
1857                         $param->set_state('detail');
1858                 }
1859                 elseif ( $_REQUEST['mode'] == 'simple' )
1860                 {
1861                         $param->set_state('mysql');
1862                 }
1863         }
1864
1865         // input parameter check
1866         if ( $isPostback )
1867         {
1868                 switch ( $param->state )
1869                 {
1870                         case 'locale':
1871                                 $param->set_state('mysql');
1872                                 $isPostback = false;
1873                                 break;
1874                         case 'mysql':
1875                                 if ( count($param->check_mysql_parameters()) == 0 )
1876                                 {
1877                                         $param->set_state('weblog');
1878                                         $isPostback = false;
1879                                 }
1880                                 break;
1881                         case 'weblog':
1882                                 if ( count($param->check_user_parameters()) == 0
1883                                         && count($param->check_weblog_parameters()) == 0 )
1884                                 {
1885                                         $param->set_state('install');
1886                                         $isPostback = false;
1887                                 }
1888                                 break;
1889                         case 'detail':
1890                                 if ( $param->check_all_parameters() )
1891                                 {
1892                                         $param->set_state('install');
1893                                         $isPostback = false;
1894                                 }
1895                                 break;
1896                 }
1897         }
1898
1899         // page render
1900         show_header();
1901         switch ( $param->state )
1902         {
1903                 case 'locale':
1904                         show_select_locale_form();
1905                         break;
1906                 case 'mysql':
1907                         show_database_setting_form($isPostback);
1908                         break;
1909                 case 'weblog':
1910                         show_blog_setting_form($isPostback);
1911                         break;
1912                 case 'detail':
1913                         show_detail_setting_form($isPostback);
1914                         break;
1915                 case 'install':
1916                         show_install_complete_form();
1917                         break;
1918         }
1919         show_footer();
1920 }
1921
1922 /**
1923  * header tag of the installation screens
1924  **/
1925 function show_header()
1926 {
1927         global $param;
1928
1929         /* HTTP 1.1 application for no caching */
1930         header("Cache-Control: no-cache, must-revalidate");
1931         header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
1932         header('Content-Type: text/html; charset=' . i18n::get_current_charset());
1933 ?>
1934 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1935 <html xmlns="http://www.w3.org/1999/xhtml">
1936         <head>
1937                 <title><?php echo _TITLE; ?></title>
1938                 <link rel="stylesheet" type="text/css" href="./styles/inst.css" />
1939                 <style type="text/css">
1940                 <!--
1941                 <?php echo _BODYFONTSTYLE; ?>
1942                 -->
1943                 </style>
1944                 <script type="text/javascript">
1945                         function SelectText( element ) {
1946                                 window.setTimeout( function() { element.select() }, 0 );
1947                         }
1948                         var isSubmit = false;
1949                         function OnceSubmit() {
1950                                 if (!isSubmit) {
1951                                         isSubmit = true;
1952                                         window.setTimeout( function() { isSubmit = false; }, 10000 );
1953                                         return true;
1954                                 }
1955                                 return false;
1956                         }
1957                 </script>
1958         </head>
1959         <body>
1960                 <div id="header">
1961                         <div id="navigation">
1962                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
1963                                 <ul>
1964 <?php
1965         echo '<li>';
1966         $label = '_LOCALE_' . strtoupper($param->locale);
1967         if ( !defined($label) )
1968         {
1969                 echo $param->locale;
1970         }
1971         else
1972         {
1973                 echo constant($label);
1974         }
1975         echo "</li>\n";
1976         
1977         if ( in_array($param->state, array('mysql', 'weblog', 'install')) )
1978         {
1979                 echo '<li>&nbsp; &gt; &nbsp;' . _STEP1, '</li><li';
1980                 if ( $param->state == 'mysql' )
1981                 {
1982                         echo ' class="gry"';
1983                 }
1984                 echo '>&nbsp; &gt; &nbsp;', _STEP2, '</li><li';
1985                 if ( in_array($param->state, array('mysql', 'weblog')) )
1986                 {
1987                         echo ' class="gry"';
1988                 }
1989                 echo '>&nbsp; &gt; &nbsp;', _STEP3, "</li>\n";
1990         }
1991         if ( in_array($param->state, array('mysql', 'weblog', 'detail')) )
1992         {
1993                 echo '<li class="rightbox">';
1994                 if ( in_array($param->state, array('mysql', 'weblog')) )
1995                 {
1996                         echo '<a href="./?mode=detail">', _MODE2, '</a>';
1997                 }
1998                 else
1999                 {
2000                         echo '<a href="./?mode=simple">', _MODE1, '</a>';
2001                 }
2002                 echo '</li>';
2003         }
2004 ?>
2005                                 </ul>
2006                         </div>
2007                 </div>
2008 <?php
2009 }
2010
2011 /**
2012  * footer tag of the installation screens
2013  **/
2014 function show_footer()
2015 {
2016         global $page_footer_copyright;
2017 ?>
2018                 <div id="footer">
2019                         <?php echo $page_footer_copyright; ?>
2020                 </div>
2021         </body>
2022 </html>
2023 <?php
2024 }
2025
2026 /**
2027  * Display the form for language select
2028  */
2029 function show_select_locale_form()
2030 {
2031         global $param;
2032         
2033 ?>
2034                 <div id="container">
2035                         <p style="font-size:152%;font-weight:bold;">
2036                                 <?php echo _LOCALE_HEADER; ?>
2037                         </p>
2038                         <form method="post" action="./index.php">
2039
2040                                 <div class="prt">
2041                                         <select name="locale">
2042 <?php
2043         $locales = i18n::get_available_locale_list();
2044         foreach ( $locales as $locale )
2045         {
2046                 if ( $param->locale != $locale )
2047                 {
2048                         echo "<option value=\"{$locale}\">";
2049                 }
2050                 else
2051                 {
2052                         echo "<option value=\"{$locale}\" selected=\"selected\">";
2053                 }
2054                 
2055                 $checkfile = "./locales/{$locale}." . i18n::get_current_charset() . '.php';
2056                 if ( !file_exists($checkfile) )
2057                 {
2058                         echo '*&nbsp;';
2059                 }
2060
2061                 $label = '_LOCALE_' . strtoupper($locale);
2062                 if ( !defined($label) )
2063         {
2064                         echo $locale;
2065                 }
2066                 else
2067                 {
2068                         echo constant($label);
2069                 }
2070                 echo "</option>\n";
2071         }
2072 ?>
2073                                         </select>
2074                                         <p><?php echo _LOCALE_DESC1; ?></p>
2075                                         <p><?php echo _LOCALE_DESC2; ?></p>
2076                                         <p><?php echo _LOCALE_NEED_HELP;?></p>
2077                                         <p class="sbt">
2078                                                 <button type="submit" name="action" value="locale" class="sbt_arw">START</button>
2079                                         </p>
2080                                 </div>
2081                         </form>
2082                 </div>
2083 <?php
2084 }
2085
2086 /**
2087  * Display the form to set up a database
2088  * @param bool $isPostback
2089  */
2090 function show_database_setting_form($isPostback)
2091 {
2092         global $param, $minimum_mysql_version;
2093
2094         $config_writable = canConfigFileWritable();
2095         $mysql_version = getMySqlVersion();
2096 ?>
2097                 <div id="container">
2098                         <p class="msg">
2099 <?php
2100         echo _SIMPLE_NAVI1;
2101         if ( $config_writable != '' )
2102         {
2103                 echo '<span class="err">', $config_writable, '</span>';
2104         }
2105         if ( $mysql_version == '0.0.0' )
2106         {
2107                 echo '<span class="err">', _DBVERSION_UNKOWN, '</span>';
2108         }
2109         elseif ( version_compare($mysql_version, $minimum_mysql_version, '<') )
2110         {
2111                 echo '<span class="err">', sprintf(_DBVERSION_TOOLOW, $minimum_mysql_version), '</span>';
2112         }
2113 ?>
2114                         </p>
2115                         <form method="post" action="./index.php">
2116                                 <div class="prt">
2117                                         <h2><?php echo _DB_HEADER; ?></h2>
2118                                         <p class="msg">
2119 <?php
2120         if ( $isPostback )
2121         {
2122                 $errors = $param->check_mysql_parameters();
2123                 if ( is_array($errors) )
2124                 {
2125                         foreach ( $errors as $error )
2126                         {
2127                                 echo '<span class="err">', $error, "</span>\n";
2128                         }
2129                 }
2130         }
2131 ?>
2132                                         </p>
2133                                         <table>
2134                                                 <tr>
2135                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
2136                                                                 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
2137                                                 </tr>
2138                                                 <tr>
2139                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
2140                                                                 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
2141                                                 </tr>
2142                                                 <tr>
2143                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span></th>
2144                                                                 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
2145                                                 </tr>
2146                                                 <tr>
2147                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
2148                                                                 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
2149                                                 </tr>
2150                                         </table>
2151                                         <p class="sbt">
2152                                                 <button type="submit" name="mode" value="detail" class="sbt_sqr"><?php echo _MODE2; ?></button>
2153                                                 <button type="submit" name="action" value="mysql" class="sbt_arw"><?php echo _NEXT; ?></button>
2154                                         </p>
2155                                         <p class="msg">
2156                                                 <?php echo _DB_TEXT1; ?>
2157                                         </p>
2158                                 </div>
2159                         </form>
2160                 </div>
2161 <?php
2162 }
2163
2164 /**
2165  * Displays a form to the blog settings
2166  * @param bool $isPostback
2167  */
2168 function show_blog_setting_form($isPostback)
2169 {
2170         global $param;
2171 ?>
2172                 <div id="container">
2173                         <p class="msg">
2174                                 <?php echo _SIMPLE_NAVI2; ?>
2175                         </p>
2176                         <form method="post" action="./index.php">
2177                                 <div class="prt">
2178                                         <h2><?php echo _BLOG_HEADER; ?></h2>
2179                                         <p class="msg">
2180 <?php
2181         if ( $isPostback )
2182         {
2183                 $errors = $param->check_weblog_parameters();
2184                 if ( is_array($errors) )
2185                 {
2186                         foreach ( $errors as $error )
2187                         {
2188                                 echo '<span class="err">', $error, "</span>\n";
2189                         }
2190                 }
2191         }
2192 ?>
2193                                         </p>
2194                                         <table>
2195                                                 <tr>
2196                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
2197                                                                 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
2198                                                 </tr>
2199                                                 <tr>
2200                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
2201                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
2202                                                 </tr>
2203                                         </table>
2204                                 </div>
2205
2206                                 <div class="prt">
2207                                         <h2><?php echo _ADMIN_HEADER; ?></h2>
2208                                         <p class="msg">
2209 <?php
2210         if ( $isPostback )
2211         {
2212                 $errors = $param->check_user_parameters();
2213                 if ( is_array($errors) )
2214                 {
2215                         foreach ( $errors as $error )
2216                         {
2217                                 echo '<span class="err">', $error, "</span>\n";
2218                         }
2219                 }
2220         }
2221 ?>
2222                                         </p>
2223                                         <table>
2224                                                 <tr>
2225                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
2226                                                                 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
2227                                                 </tr>
2228                                                 <tr>
2229                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
2230                                                                 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
2231                                                 </tr>
2232                                                 <tr>
2233                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span></th>
2234                                                                 <td><input type="password" name="user_password" /></td>
2235                                                 </tr>
2236                                                 <tr>
2237                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span></th>
2238                                                                 <td><input type="password" name="user_password2" /></td>
2239                                                 </tr>
2240                                                 <tr>
2241                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
2242                                                                 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
2243                                                 </tr>
2244                                         </table>
2245                                         <p class="sbt">
2246                                                 <button type="submit" name="action" value="weblog" class="sbt_arw" onclick="OnceSubmit()"><?php echo _INSTALL; ?></button>
2247                                         </p>
2248                                 </div>
2249                         </form>
2250                 </div>
2251 <?php
2252 }
2253
2254 /**
2255  * Displays a form to the detail settings
2256  * @param bool $isPostback
2257  */
2258 function show_detail_setting_form($isPostback)
2259 {
2260         global $param, $minimum_mysql_version;
2261
2262         $mysql_version = getMySqlVersion();
2263 ?>
2264                 <div id="container_detailed">
2265                         <p class="msg">
2266                                 <?php echo _DETAIL_NAVI1; ?>
2267 <?php
2268         if ( $isPostback && !$param->check_all_parameters() )
2269         {
2270                 echo '<span class="err">', _VALID_ERROR, "</span>\n";
2271         }
2272 ?>
2273                         </p>
2274                         <ul class="msg">
2275                                 <li>PHP: <?php echo phpversion(); ?></li>
2276                                 <li>MySQL:
2277 <?php
2278         echo ($mysql_version == '0.0.0') ? _DBVERSION_UNKOWN : $mysql_version;
2279         if ( version_compare($mysql_version, $minimum_mysql_version, '<') )
2280         {
2281                 echo '<span class="err">', sprintf(_DBVERSION_TOOLOW, $minimum_mysql_version), '</span>';
2282         }
2283 ?></li>
2284                         </ul>
2285                         <form method="post" action="">
2286
2287                                 <div class="prt">
2288                                         <h2><?php echo _DETAIL_HEADER1; ?></h2>
2289                                         <p class="msg">
2290 <?php
2291         if ( $isPostback )
2292         {
2293                 $errors = $param->check_mysql_parameters();
2294                 if ( is_array($errors) )
2295                 {
2296                         foreach ( $errors as $error )
2297                         {
2298                                 echo '<span class="err">', $error, "</span>\n";
2299                         }
2300                 }
2301         }
2302 ?>
2303                                         </p>
2304                                         <table>
2305                                                 <tr>
2306                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
2307                                                                 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
2308                                                 </tr>
2309                                                 <tr>
2310                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
2311                                                                 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
2312                                                 </tr>
2313                                                 <tr>
2314                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span></th>
2315                                                                 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
2316                                                 </tr>
2317                                                 <tr>
2318                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
2319                                                                 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
2320                                                 </tr>
2321                                                 <tr>
2322                                                         <th><span class="nam"><?php echo _DB_FIELD5; ?></span><span class="sub"><?php echo _DB_FIELD5_DESC; ?></span></th>
2323                                                                 <td><input type="text" name="mysql_tablePrefix" value="<?php echo $param->mysql_tablePrefix; ?>" /></td>
2324                                                 </tr>
2325                                         </table>
2326
2327                                         <h2><?php echo _DETAIL_HEADER2; ?></h2>
2328                                         <p class="msg">
2329 <?php
2330         if ( $isPostback )
2331         {
2332                 $errors = $param->check_uri_parameters();
2333                 if ( is_array($errors) )
2334                 {
2335                         foreach ( $errors as $error )
2336                         {
2337                                 echo '<span class="err">', $error, "</span>\n";
2338                         }
2339                 }
2340                 $errors = $param->check_path_parameters();
2341                 if ( is_array($errors) )
2342                 {
2343                         foreach ( $errors as $error )
2344                         {
2345                                 echo '<span class="err">', $error, "</span>\n";
2346                         }
2347                 }
2348         }
2349 ?>
2350                                         </p>
2351                                         <table>
2352                                                 <tr>
2353                                                         <th><span class="nam"><?php echo _PATH_FIELD1; ?></span></th>
2354                                                                 <td><input type="text" name="IndexURL" value="<?php echo $param->IndexURL; ?>" /></td>
2355                                                 </tr>
2356                                                 <tr>
2357                                                         <th><span class="nam"><?php echo _PATH_FIELD2; ?></span></th>
2358                                                                 <td><input type="text" name="AdminURL" value="<?php echo $param->AdminURL; ?>" /></td>
2359                                                 </tr>
2360                                                 <tr>
2361                                                         <th><span class="nam"><?php echo _PATH_FIELD3; ?></span></th>
2362                                                                 <td><input type="text" name="AdminPath" value="<?php echo $param->AdminPath; ?>" /></td>
2363                                                 </tr>
2364                                                 <tr>
2365                                                         <th><span class="nam"><?php echo _PATH_FIELD4; ?></span></th>
2366                                                                 <td><input type="text" name="MediaURL" value="<?php echo $param->MediaURL; ?>" /></td>
2367                                                 </tr>
2368                                                 <tr>
2369                                                         <th><span class="nam"><?php echo _PATH_FIELD5; ?></span></th>
2370                                                                 <td><input type="text" name="MediaPath" value="<?php echo $param->MediaPath; ?>" /></td>
2371                                                 </tr>
2372                                                 <tr>
2373                                                         <th><span class="nam"><?php echo _PATH_FIELD6; ?></span></th>
2374                                                                 <td><input type="text" name="SkinsURL" value="<?php echo $param->SkinsURL; ?>" /></td>
2375                                                 </tr>
2376                                                 <tr>
2377                                                         <th><span class="nam"><?php echo _PATH_FIELD7; ?></span></th>
2378                                                                 <td><input type="text" name="SkinsPath" value="<?php echo $param->SkinsPath; ?>" /></td>
2379                                                 </tr>
2380                                                 <tr>
2381                                                         <th><span class="nam"><?php echo _PATH_FIELD8; ?></span></th>
2382                                                                 <td><input type="text" name="PluginURL" value="<?php echo $param->PluginURL; ?>" /></td>
2383                                                 </tr>
2384                                                 <tr>
2385                                                         <th><span class="nam"><?php echo _PATH_FIELD9; ?></span></th>
2386                                                                 <td><input type="text" name="ActionURL" value="<?php echo $param->ActionURL; ?>" /></td>
2387                                                 </tr>
2388                                         </table>
2389                                         <p class="msg">
2390                                                 <?php echo _DETAIL_TEXT3; ?>
2391                                         </p>
2392
2393                                         <h2><?php echo _DETAIL_HEADER3; ?></h2>
2394                                         <p class="msg">
2395 <?php
2396         echo _DETAIL_TEXT4;
2397         if ( $isPostback )
2398         {
2399                 $errors = $param->check_user_parameters();
2400                 if ( is_array($errors) )
2401                 {
2402                         foreach ( $errors as $error )
2403                         {
2404                                 echo '<span class="err">', $error, "</span>\n";
2405                         }
2406                 }
2407         }
2408 ?>
2409                                         </p>
2410                                         <table>
2411                                                 <tr>
2412                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
2413                                                                 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
2414                                                 </tr>
2415                                                 <tr>
2416                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
2417                                                                 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
2418                                                 </tr>
2419                                                 <tr>
2420                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span></th>
2421                                                                 <td><input type="password" name="user_password" /></td>
2422                                                 </tr>
2423                                                 <tr>
2424                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span></th>
2425                                                                 <td><input type="password" name="user_password2" /></td>
2426                                                 </tr>
2427                                                 <tr>
2428                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
2429                                                                 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
2430                                                 </tr>
2431                                         </table>
2432
2433                                         <h2><?php echo _DETAIL_HEADER4; ?></h2>
2434                                         <p class="msg">
2435 <?php
2436         echo _DETAIL_TEXT5;
2437         if ( $isPostback )
2438         {
2439                 $errors = $param->check_weblog_parameters();
2440                 if ( is_array($errors) )
2441                 {
2442                         foreach ( $errors as $error )
2443                         {
2444                                 echo '<span class="err">', $error, "</span>\n";
2445                         }
2446                 }
2447         }
2448 ?>
2449                                         </p>
2450                                         <table>
2451                                                 <tr>
2452                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
2453                                                                 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
2454                                                 </tr>
2455                                                 <tr>
2456                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
2457                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
2458                                                 </tr>
2459                                         </table>
2460
2461                                         <p class="msg">
2462                                                 <?php echo _DETAIL_TEXT6; ?>
2463                                         </p>
2464
2465                                         <p class="sbt">
2466                                                 <button type="submit" name="action" value="detail" class="sbt_arw" onclick="OnceSubmit()"><?php echo _INSTALL; ?></button>
2467                                         </p>
2468                                 </div>
2469                         </form>
2470                 </div>
2471 <?php
2472 }
2473
2474 /**
2475  * Displays a screen to signal the completion of the installation
2476  */
2477 function show_install_complete_form()
2478 {
2479         global $MYSQL_HANDLER, $param;
2480         $errors = do_install();
2481 ?>
2482                 <div id="container">
2483                         <p class="msg">
2484 <?php
2485         if ( is_array($errors) && count($errors) > 0 )
2486         {
2487                 echo _INST_ERROR;
2488                 foreach ( $errors as $error )
2489                 {
2490                         echo '<span class="err">', $error, "</span>\n";
2491                 }
2492         }
2493         else
2494         {
2495                 echo _INST_TEXT;
2496                 if ( array_key_exists('config_data', $_SESSION) )
2497                 {
2498                         echo '<span class="err">', _INST_TEXT4, '</span>';
2499 ?>
2500 <textarea id="config_text" readonly="readonly" onfocus="SelectText(this);"><?php echo htmlentities($_SESSION['config_data'], null, i18n::get_current_charset()) ?></textarea>
2501 <?php
2502                 }
2503                 else
2504                 {
2505                         echo '<span class="err">', _INST_TEXT5, '</span>';
2506                 }
2507 ?>
2508                         </p>
2509                         <form method="post" action="./index.php">
2510                                 <div class="prt">
2511                                         <h2><?php echo _INST_HEADER1; ?></h2>
2512                                         <p class="msg">
2513                                                 <?php echo sprintf(_INST_TEXT1, $param->blog_name); ?>
2514                                         </p>
2515                                         <p class="sbt">
2516                                                 <button type="button" name="toBlog" onclick="location.href='<?php echo $param->IndexURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON1; ?></button>
2517                                         </p>
2518                                 </div>
2519
2520                                 <div class="prt">
2521                                         <h2><?php echo _INST_HEADER2; ?></h2>
2522                                         <p class="msg">
2523                                                 <?php echo _INST_TEXT2; ?>
2524                                         </p>
2525                                         <p class="sbt">
2526                                                 <button type="button" name="toMng" onclick="location.href='<?php echo $param->AdminURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON2; ?></button>
2527                                         </p>
2528                                 </div>
2529
2530                                 <div class="prt">
2531                                         <h2><?php echo _INST_HEADER3; ?></h2>
2532                                         <p class="msg">
2533                                                 <?php echo _INST_TEXT3; ?>
2534                                         </p>
2535                                         <p class="sbt">
2536                                                 <button type="button" name="toAddBlog" onclick="location.href='<?php echo $param->AdminURL; ?>index.php?action=createnewlog';" class="sbt_arw"><?php echo _INST_BUTTON3; ?></button>
2537                                         </p>
2538                                 </div>
2539                         </form>
2540 <?php
2541         }
2542 ?>
2543                 </div>
2544 <?php
2545         unset($param);
2546 }
2547
2548 /**
2549  * The installation process itself
2550  * @return array error messages
2551  */
2552 function do_install()
2553 {
2554         global $param;
2555         global $MYSQL_HANDLER, $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_PREFIX, $MYSQL_CONN;
2556         global $DIR_NUCLEUS, $DIR_MEDIA, $DIR_SKINS, $DIR_PLUGINS, $DIR_LANG, $DIR_LIBS;
2557         $errors = array();
2558
2559         /*
2560          * 1. put all param-vars into vars
2561          */
2562         $MYSQL_HOST             = $param->mysql_host;
2563         $MYSQL_USER             = $param->mysql_user;
2564         $MYSQL_PASSWORD = $param->mysql_password;
2565         $MYSQL_DATABASE = $param->mysql_database;
2566         $MYSQL_PREFIX   = $param->mysql_tablePrefix;
2567
2568         $DIR_NUCLEUS    = $param->AdminPath;
2569         $DIR_MEDIA              = $param->MediaPath;
2570         $DIR_SKINS              = $param->SkinsPath;
2571         $DIR_PLUGINS    = $DIR_NUCLEUS . 'plugins/';
2572         $DIR_LOCALES    = $DIR_NUCLEUS . 'locales/';
2573         $DIR_LIBS               = $DIR_NUCLEUS . 'libs/';
2574
2575         /*
2576          * 2.open mySQL connection
2577          */
2578         $MYSQL_CONN = @DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD);
2579         if ( $MYSQL_CONN == FALSE )
2580         {
2581                 $errors[] = _DBCONNECT_ERROR;
2582                 return $errors;
2583         }
2584
2585         /*
2586          * 3. try to create database if needed
2587          */
2588         if ( DB::execute("CREATE DATABASE IF NOT EXISTS {$MYSQL_DATABASE}") === FALSE )
2589         {
2590                 $errinfo = DB::getError();
2591                 $errors[] = _INST_ERROR1 . ': ' . $errinfo[2];
2592         }
2593
2594         /*
2595          * 4. try to select database
2596          */
2597         $MYSQL_CONN = @DB::setConnectionInfo($MYSQL_HANDLER[1], $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE);
2598         if ( !$MYSQL_CONN )
2599         {
2600                 $errors[] = _INST_ERROR2;
2601         }
2602
2603         if ( count($errors) > 0 )
2604         {
2605                 return $errors;
2606         }
2607
2608         /*
2609          * 5. execute queries
2610          */
2611         $table_names = array(
2612                 'nucleus_actionlog',
2613                 'nucleus_ban',
2614                 'nucleus_blog',
2615                 'nucleus_category',
2616                 'nucleus_comment',
2617                 'nucleus_config',
2618                 'nucleus_item',
2619                 'nucleus_karma',
2620                 'nucleus_member',
2621                 'nucleus_plugin',
2622                 'nucleus_skin',
2623                 'nucleus_template',
2624                 'nucleus_team',
2625                 'nucleus_activation',
2626                 'nucleus_tickets'
2627         );
2628
2629         $prefixed_table_names = array();
2630         foreach ( $table_names as $table_name )
2631         {
2632                 $prefixed_table_names[] = $MYSQL_PREFIX . $table_name;
2633         }
2634
2635         // table exists check
2636         $result = DB::getResult('SHOW TABLES');
2637         foreach ( $result as $row )
2638         {
2639                 if ( in_array($row[0], $prefixed_table_names) )
2640                 {
2641                         $errors[] = _INST_ERROR3;
2642                         break;
2643                 }
2644         }
2645         if ( count($errors) > 0 )
2646         {
2647                 return $errors;
2648         }
2649
2650         $filename = 'install.sql';
2651         $fd = fopen($filename, 'r');
2652         $queries = fread($fd, filesize($filename));
2653         fclose($fd);
2654
2655         $queries = preg_split('#(;\n|;\r)#', $queries);
2656
2657         foreach ( $queries as $query )
2658         {
2659                 if ( preg_match('/\w+/', $query) )
2660                 {
2661                         if ( $MYSQL_PREFIX )
2662                         {
2663                                 $query = str_replace($table_names, $prefixed_table_names, $query);
2664                         }
2665
2666                         if ( DB::execute($query) === FALSE )
2667                         {
2668                                 $errinfo = DB::getError();
2669                                 $errors[] = _INST_ERROR4 . ' (<small>' . $query . '</small>): ' . $errinfo[2];
2670                         }
2671                 }
2672         }
2673
2674         /*
2675          * 6. put needed records
2676          */
2677         /* push first post */
2678         $query = "INSERT INTO %s VALUES (1, %s, %s, %s, 1, 1, %s, 0, 0, 0, 1, 0, 1)";
2679         $query = sprintf($query,
2680                 tableName('nucleus_item'),
2681                 DB::quoteValue(_1ST_POST_TITLE),
2682                 DB::quoteValue(_1ST_POST),
2683                 DB::quoteValue(_1ST_POST2),
2684                 DB::formatDateTime()
2685         );
2686         if ( DB::execute($query) === FALSE )
2687         {
2688                 $errinfo = DB::getError();
2689                 $errors[] = _INST_ERROR4 . ' (<small>' . $query . '</small>): ' . $errinfo[2];
2690         }
2691
2692         /* push configurations */
2693         array_merge($errors, updateConfig('IndexURL', $param->IndexURL));
2694         array_merge($errors, updateConfig('AdminURL', $param->AdminURL));
2695         array_merge($errors, updateConfig('MediaURL', $param->MediaURL));
2696         array_merge($errors, updateConfig('SkinsURL', $param->SkinsURL));
2697         array_merge($errors, updateConfig('PluginURL', $param->PluginURL));
2698         array_merge($errors, updateConfig('ActionURL', $param->ActionURL));
2699         array_merge($errors, updateConfig('AdminEmail', $param->user_email));
2700         array_merge($errors, updateConfig('SiteName', $param->blog_name));
2701         array_merge($errors, updateConfig('Locale', i18n::get_current_locale()));
2702
2703         /* push super admin */
2704         $query = "UPDATE %s SET mname = %s, mrealname = %s, mpassword = %s, memail = %s, murl = %s, madmin = 1, mcanlogin = 1 WHERE mnumber = 1";
2705         $query = sprintf($query,
2706                 tableName('nucleus_member'),
2707                 DB::quoteValue($param->user_name),
2708                 DB::quoteValue($param->user_realname),
2709                 DB::quoteValue(md5($param->user_password)),
2710                 DB::quoteValue($param->user_email),
2711                 DB::quoteValue($param->IndexURL)
2712         );
2713         if ( DB::execute($query) === FALSE )
2714         {
2715                 $errinfo = DB::getError();
2716                 $errors[] = _INST_ERROR5 . ': ' . $errinfo[2];
2717         }
2718
2719         /* push new weblog */
2720         $query = "UPDATE %s SET bname = %s, bshortname = %s, burl = %s WHERE bnumber = 1";
2721         $query = sprintf($query,
2722                 tableName('nucleus_blog'),
2723                 DB::quoteValue($param->blog_name),
2724                 DB::quoteValue($param->blog_shortname),
2725                 DB::quoteValue($param->IndexURL)
2726         );
2727         if ( DB::execute($query) === FALSE )
2728         {
2729                 $errinfo = DB::getError();
2730                 $errors[] = _INST_ERROR6 . ': ' . $errinfo[2];
2731         }
2732
2733         /* push default category */
2734         $query = "UPDATE %s SET cname = %s, cdesc = %s WHERE catid = 1";
2735         $query = sprintf($query,
2736                 tableName('nucleus_category'),
2737                 DB::quoteValue(_GENERALCAT_NAME),
2738                 DB::quoteValue(_GENERALCAT_DESC)
2739         );
2740         if ( DB::execute($query) === FALSE )
2741         {
2742                 $errinfo = DB::getError();
2743                 $errors[] = _INST_ERROR6 . ': ' . $errinfo[2];
2744         }
2745
2746         /*
2747          * 7. install default plugins and skins
2748          */
2749         global $aConfPlugsToInstall, $aConfSkinsToImport;
2750         $aSkinErrors = array();
2751         $aPlugErrors = array();
2752
2753         if ( (count($aConfPlugsToInstall) > 0) || (count($aConfSkinsToImport) > 0) )
2754         {
2755                 include_once($DIR_LIBS . 'globalfunctions.php');
2756                 global $manager;
2757                 if ( !isset($manager) )
2758                 {
2759                         $manager = new Manager();
2760                 }
2761
2762                 include_once($DIR_LIBS . 'skinie.php');
2763                 
2764                 $aSkinErrors = installCustomSkins();
2765                 if ( count($aSkinErrors) > 0 )
2766                 {
2767                         array_merge($errors, $aSkinErrors);
2768                 }
2769
2770                 $query  = "SELECT sdnumber FROM %s WHERE sdname='admin/default'";
2771                 $query  = sprintf($query, tableName('nucleus_skin_desc'));
2772                 $res    = intval(DB::getValue($query));
2773                 array_merge($errors, updateConfig('AdminSkin', $res));
2774                 
2775                 $query  = "SELECT sdnumber FROM %s WHERE sdname='admin/bookmarklet'";
2776                 $query  = sprintf($query, tableName('nucleus_skin_desc'));
2777                 $res    = intval(DB::getValue($query));
2778                 array_merge($errors, updateConfig('BookmarkletSkin', $res));
2779                 
2780                 $query          = "SELECT sdnumber FROM %s WHERE sdname='default'";
2781                 $query          = sprintf($query, tableName('nucleus_skin_desc'));
2782                 $defSkinID      = intval(DB::getValue($query));
2783
2784                 $query = "UPDATE %s SET bdefskin=%d WHERE bnumber=1";
2785                 $query = sprintf($query, tableName('nucleus_blog'), $defSkinID);
2786                 DB::execute($query);
2787                 $query = "UPDATE %s SET value=%d WHERE name='BaseSkin'";
2788                 $query = sprintf($query, tableName('nucleus_config'), $defSkinID);
2789                 DB::execute($query);
2790
2791                 $aPlugErrors = installCustomPlugs();
2792                 if ( count($aPlugErrors) > 0 )
2793                 {
2794                         array_merge($errors, $aPlugErrors);
2795                 }
2796         }
2797
2798         /*
2799          * 8. Write config file ourselves (if possible)
2800          */
2801         $config_data = '<' . '?php' . "\n";
2802         $config_data .= "// mySQL connection information\n";
2803         $config_data .= "\$MYSQL_HOST = '" . $MYSQL_HOST . "';\n";
2804         $config_data .= "\$MYSQL_USER = '" . $MYSQL_USER . "';\n";
2805         $config_data .= "\$MYSQL_PASSWORD = '" . $MYSQL_PASSWORD . "';\n";
2806         $config_data .= "\$MYSQL_DATABASE = '" . $MYSQL_DATABASE . "';\n";
2807         $config_data .= "\$MYSQL_PREFIX = '" . $MYSQL_PREFIX . "';\n";
2808         $config_data .= "// new in 3.50. first element is db handler, the second is the db driver used by the handler\n";
2809         $config_data .= "// default is \$MYSQL_HANDLER = array('mysql','mysql');\n";
2810         $config_data .= "//\$MYSQL_HANDLER = array('mysql','mysql');\n";
2811         $config_data .= "//\$MYSQL_HANDLER = array('pdo','mysql');\n";
2812         $config_data .= "\$MYSQL_HANDLER = array('" . $MYSQL_HANDLER[0] . "','" . $MYSQL_HANDLER[1] . "');\n";
2813         $config_data .= "\n";
2814         $config_data .= "// main nucleus directory\n";
2815         $config_data .= "\$DIR_NUCLEUS = '" . $DIR_NUCLEUS . "';\n";
2816         $config_data .= "\n";
2817         $config_data .= "// path to media dir\n";
2818         $config_data .= "\$DIR_MEDIA = '" . $DIR_MEDIA . "';\n";
2819         $config_data .= "\n";
2820         $config_data .= "// extra skin files for imported skins\n";
2821         $config_data .= "\$DIR_SKINS = '" . $DIR_SKINS . "';\n";
2822         $config_data .= "\n";
2823         $config_data .= "// these dirs are normally sub dirs of the nucleus dir, but \n";
2824         $config_data .= "// you can redefine them if you wish\n";
2825         $config_data .= "\$DIR_PLUGINS = \$DIR_NUCLEUS . 'plugins/';\n";
2826         $config_data .= "\$DIR_LOCALES = \$DIR_NUCLEUS . 'locales/';\n";
2827         $config_data .= "\$DIR_LIBS = \$DIR_NUCLEUS . 'libs/';\n";
2828         $config_data .= "\n";
2829         $config_data .= "// include libs\n";
2830         $config_data .= "include(\$DIR_LIBS.'globalfunctions.php');\n";
2831         $config_data .= "?" . ">";
2832
2833         $result = false;
2834         if ( @!file_exists('../config.php') || is_writable('../config.php') )
2835         {
2836                 if ( $fp = @fopen('../config.php', 'w') )
2837                 {
2838                         $result = @fwrite($fp, $config_data, i18n::strlen($config_data));
2839                         fclose($fp);
2840                 }
2841         }
2842
2843         if ( $result )
2844         {
2845                 // try to change the read-only permission.
2846                 if ( strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
2847                 {
2848                         @chmod('../config.php', 0444);
2849                 }
2850         }
2851         else
2852         {
2853                 $_SESSION['config_data'] = $config_data;
2854         }
2855
2856         return $errors;
2857 }
2858
2859 /**
2860  * Confirm that you can write to the configuration file
2861  * @return string error message
2862  */
2863 function canConfigFileWritable()
2864 {
2865         if ( @file_exists('../config.php') && @!is_writable('../config.php') )
2866         {
2867                 // try to change the read-write permission.
2868                 if ( strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
2869                 {
2870                         @chmod('../config.php', 0666);
2871                 }
2872
2873                 if ( @!is_writable('../config.php') )
2874                 {
2875                         return _INST_ERROR8;
2876                 }
2877         }
2878         return '';
2879 }
2880
2881 /**
2882  * To obtain the version of MySQL
2883  * @return string
2884  */
2885 function getMySqlVersion()
2886 {
2887         global $MYSQL_HANDLER, $minimum_mysql_version, $errors;
2888         // Turn on output buffer
2889         // Needed to repress the output of the sql function that are
2890         // not part of php (in this case the @ operator doesn't work)
2891         ob_start();
2892
2893         // note: this piece of code is taken from phpMyAdmin
2894         $conn = @DB::setConnectionInfo($MYSQL_HANDLER[1], 'localhost', '', '');
2895
2896         if ( $conn )
2897         {
2898                 $row = DB::getAttribute(PDO::ATTR_SERVER_VERSION);
2899                 $match = preg_split('#\.#', $row);
2900         }
2901         else
2902         {
2903                 $row = @DB::getRow('SHOW VARIABLES LIKE \'version\'');
2904
2905                 if ( $row )
2906                 {
2907                         $match = preg_split('#\.#', $row[1]);
2908                 }
2909                 else
2910                 {
2911                         $output = (function_exists('shell_exec')) ? @shell_exec('mysql -V') : '0.0.0';
2912                         preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
2913                         $match = preg_split('#\.#', $version[0]);
2914
2915                         if ( $match[0] == '' )
2916                         {
2917                                 $match = array('0', '0', '0');
2918                         }
2919                 }
2920         }
2921
2922         @DB::disConnect();
2923
2924         //End and clean output buffer
2925         ob_end_clean();
2926
2927         return implode($match, '.');
2928 }
2929
2930 /**
2931  * Add a table prefix if it is used
2932  *
2933  * @param string $input table name with prefix
2934  * @return string
2935  */
2936 function tableName($input)
2937 {
2938         global $MYSQL_PREFIX;
2939         if ( $MYSQL_PREFIX )
2940         {
2941                 return $MYSQL_PREFIX . $input;
2942         }
2943         else
2944         {
2945                 return $input;
2946         }
2947 }
2948
2949 /**
2950  * Install custom plugins
2951  */
2952 function installCustomPlugs()
2953 {
2954         global $aConfPlugsToInstall, $DIR_LIBS, $manager;
2955
2956         $aErrors = array();
2957         if ( count($aConfPlugsToInstall) == 0 )
2958         {
2959                 return $aErrors;
2960         }
2961
2962         $query = sprintf('SELECT * FROM %s', tableName('nucleus_plugin'));
2963         $res = DB::getResult($query);
2964         $numCurrent = $res->rowCount();
2965
2966         foreach ( $aConfPlugsToInstall as $plugName )
2967         {
2968                 $query = sprintf('INSERT INTO %s (porder, pfile) VALUES (%d, %s)',
2969                         tableName('nucleus_plugin'),
2970                         (++$numCurrent),
2971                         DB::quoteValue($plugName));
2972                 DB::execute($query);
2973
2974                 $manager->clearCachedInfo('installedPlugins');
2975                 $plugin =& $manager->getPlugin($plugName);
2976                 $plugin->setID($numCurrent);
2977
2978                 if ( !$plugin )
2979                 {
2980                         $query = sprintf('DELETE FROM %s WHERE pfile = %s',
2981                                 tableName('nucleus_plugin'),
2982                                 DB::quoteValue($plugName));
2983                         DB::execute($query);
2984                         $numCurrent--;
2985                         array_push($aErrors, sprintf(_INST_ERROR9, $plugName));
2986                         continue;
2987                 }
2988                 $plugin->install();
2989         }
2990
2991         $query = sprintf('DELETE FROM %s', tableName('nucleus_plugin_event'));
2992         DB::execute($query);
2993         $query = sprintf('SELECT pid, pfile FROM %s', tableName('nucleus_plugin'));
2994         $res = DB::getResult($query);
2995
2996         foreach ( $res as $row )
2997         {
2998                 $plug =& $manager->getPlugin($row['pfile']);
2999
3000                 if ( $plug )
3001                 {
3002                         $eventList = $plug->getEventList();
3003                         foreach ( $eventList as $eventName )
3004                         {
3005                                 $query = sprintf('INSERT INTO %s (pid, event) VALUES (%d, %s)',
3006                                         tableName('nucleus_plugin_event'),
3007                                         intval($row['pid']),
3008                                         DB::quoteValue($eventName));
3009                                 DB::execute($query);
3010                         }
3011                 }
3012         }
3013         return $aErrors;
3014 }
3015
3016 /**
3017  * Install custom skins
3018  * Prepares the installation of custom skins
3019  */
3020 function installCustomSkins()
3021 {
3022         global $aConfSkinsToImport, $DIR_LIBS, $DIR_SKINS;
3023
3024         $aErrors = array();
3025         if ( count($aConfSkinsToImport) == 0 )
3026         {
3027                 return $aErrors;
3028         }
3029
3030         $importer = new SkinImport();
3031
3032         foreach ( $aConfSkinsToImport as $skinName )
3033         {
3034                 $importer->reset();
3035                 $skinFile = $DIR_SKINS . $skinName . '/skinbackup.xml';
3036
3037                 if ( !@file_exists($skinFile) )
3038                 {
3039                         array_push($aErrors, sprintf(_INST_ERROR10, $skinFile));
3040                         continue;
3041                 }
3042
3043                 $error = $importer->readFile($skinFile);
3044
3045                 if ( $error )
3046                 {
3047                         array_push($aErrors, sprintf(_INST_ERROR11, $skinName) . ' : ' . $error);
3048                         continue;
3049                 }
3050
3051                 $error = $importer->writeToDatabase(1);
3052
3053                 if ( $error )
3054                 {
3055                         array_push($aErrors, sprintf(_INST_ERROR12, $skinName) . ' : ' . $error);
3056                         continue;
3057                 }
3058         }
3059         return $aErrors;
3060 }
3061
3062 /**
3063  * Check if some important files of the Nucleus CMS installation are available
3064  * Give an error if one or more files are not accessible
3065  */
3066 function do_check_files()
3067 {
3068         $missingfiles = array();
3069         $files = array(
3070                 './install.sql',
3071                 '../index.php',
3072                 '../action.php',
3073                 '../nucleus/index.php',
3074                 '../nucleus/libs/ACTION.php',
3075                 '../nucleus/libs/ACTIONLOG.php',
3076                 '../nucleus/libs/ACTIONS.php',
3077                 '../nucleus/libs/ADMIN.php',
3078                 '../nucleus/libs/BaseActions.php',
3079                 '../nucleus/libs/BLOG.php',
3080                 '../nucleus/libs/BODYACTIONS.php',
3081                 '../nucleus/libs/COMMENT.php',
3082                 '../nucleus/libs/COMMENTACTIONS.php',
3083                 '../nucleus/libs/COMMENTS.php',
3084                 '../nucleus/libs/ENTITY.php',
3085                 '../nucleus/libs/globalfunctions.php',
3086                 '../nucleus/libs/i18n.php',
3087                 '../nucleus/libs/ITEM.php',
3088                 '../nucleus/libs/ITEMACTIONS.php',
3089                 '../nucleus/libs/LINK.php',
3090                 '../nucleus/libs/MANAGER.php',
3091                 '../nucleus/libs/MEDIA.php',
3092                 '../nucleus/libs/MEMBER.php',
3093                 '../nucleus/libs/NOTIFICATION.php',
3094                 '../nucleus/libs/PARSER.php',
3095                 '../nucleus/libs/PLUGIN.php',
3096                 '../nucleus/libs/PLUGINADMIN.php',
3097                 '../nucleus/libs/SEARCH.php',
3098                 '../nucleus/libs/showlist.php',
3099                 '../nucleus/libs/SKIN.php',
3100                 '../nucleus/libs/TEMPLATE.php',
3101                 '../nucleus/libs/vars4.1.0.php',
3102                 '../nucleus/libs/xmlrpc.inc.php',
3103                 '../nucleus/libs/xmlrpcs.inc.php',
3104                 '../nucleus/libs/sql/DB.php',
3105                 '../nucleus/libs/sql/MYSQLPDO.php'
3106         );
3107
3108         $count = count($files);
3109         for ( $i = 0; $i < $count; $i++ )
3110         {
3111                 if ( !is_readable($files[$i]) )
3112                 {
3113                         array_push($missingfiles, 'File <b>' . $files[$i] . '</b> is missing or not readable.<br />');
3114                 }
3115         }
3116
3117         if ( count($missingfiles) > 0 )
3118         {
3119                 exit(implode("\n", $missingfiles));
3120         }
3121 }
3122
3123 /**
3124  * Updates the configuration in the database
3125  *
3126  * @param string $name name of the config var
3127  * @param string $value new value of the config var
3128  * @return array
3129  */
3130 function updateConfig($name, $value)
3131 {
3132         $errors = array();
3133
3134         $query = "UPDATE %s SET value = %s WHERE name = %s";
3135         $query = sprintf($query, tableName('nucleus_config'), DB::quoteValue(trim($value)), DB::quoteValue($name));
3136
3137         if ( DB::execute($query) === FALSE )
3138         {
3139                 $errinfo = DB::getError();
3140                 $errors[] = _INST_ERROR4 . ': ' . $errinfo[2];
3141         }
3142         return $errors;
3143 }
3144
3145 class ParamManager
3146 {
3147         /* process parameter */
3148         public $state;
3149         public $locale;
3150
3151         /* mysql connection parameters */
3152         public $mysql_host;
3153         public $mysql_user;
3154         public $mysql_password;
3155         public $mysql_database;
3156         public $mysql_tablePrefix;
3157
3158         /* weblog configuration parameters */
3159         public $blog_name;
3160         public $blog_shortname;
3161
3162         /* member configuration parameters */
3163         public $user_name;
3164         public $user_realname;
3165         public $user_password;
3166         private $user_password2;
3167         public $user_email;
3168
3169         /* URI parameters  */
3170         private $root_url;
3171         public $IndexURL;
3172         public $AdminURL;
3173         public $MediaURL;
3174         public $SkinsURL;
3175         public $PluginURL;
3176         public $ActionURL;
3177
3178         /* path parameters */
3179         private $root_path;
3180         public $AdminPath;
3181         public $MediaPath;
3182         public $SkinsPath;
3183
3184         /**
3185          * constructor
3186          */
3187         public function __construct()
3188         {
3189                 $this->init();
3190         }
3191
3192         public function init()
3193         {
3194                 // set default values
3195                 $this->state = 'locale';
3196                 $this->install_mode = 'simple';
3197                 $this->locale = '';
3198                 $this->mysql_host = @ini_get('mysql.default_host');
3199                 $this->blog_name = 'My Nucleus CMS';
3200                 $this->blog_shortname = 'mynucleuscms';
3201
3202                 /* root path */
3203                 $directory_separator = preg_quote(DIRECTORY_SEPARATOR, '|');
3204                 $this->root_path = implode('/', preg_split("|$directory_separator|", realpath(dirname(__FILE__) . '/..')));
3205                 if ( substr($this->root_path, -1, 1) !== '/' )
3206                 {
3207                         $this->root_path .= '/';
3208                 }
3209                 $base_path_pcre = preg_quote($this->root_path, '|');
3210
3211                 /* current directry name */
3212                 $directory_name = preg_replace("#{$base_path_pcre}#", '', implode('/', preg_split("#{$directory_separator}#", realpath(dirname(__FILE__)))));
3213                 $directory_name_pcre = preg_quote($directory_name, '|');
3214
3215                 /* root uri */
3216                 $root_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
3217                 $this->root_url = preg_replace("|$directory_name_pcre(.*)$|", '', $root_url);
3218
3219                 $this->AdminPath = $this->root_path . 'nucleus/';
3220                 $this->MediaPath = $this->root_path . 'media/';
3221                 $this->SkinsPath = $this->root_path . 'skins/';
3222
3223                 $this->IndexURL  = $this->root_url;
3224                 $this->AdminURL  = $this->root_url . 'nucleus/';
3225                 $this->MediaURL  = $this->root_url . 'media/';
3226                 $this->SkinsURL  = $this->root_url . 'skins/';
3227                 $this->PluginURL = $this->root_url . 'nucleus/plugins/';
3228                 $this->ActionURL = $this->root_url . 'action.php';
3229         }
3230
3231         private function read_parameter($parameter)
3232         {
3233                 foreach ( $parameter as $element )
3234                 {
3235                         if ( array_key_exists($element, $_POST) )
3236                         {
3237                                 $this->$element = $_POST[$element];
3238                         }
3239                 }
3240         }
3241
3242         public function set_state($state)
3243         {
3244                 $states = array('locale', 'mysql', 'weblog', 'detail', 'install');
3245                 if ( in_array($state, $states) )
3246                 {
3247                         $this->state = $state;
3248                 }
3249         }
3250
3251         public function set_locale()
3252         {
3253                 $this->read_parameter(array('locale'));
3254
3255                 if ( !$this->locale )
3256                 {
3257                         /**
3258                          * default locale select simple implementation
3259                          * 
3260                          * NOTE:
3261                          * RFC2616: Hypertext Transfer Protocol -- HTTP/1.1
3262                          * http://www.ietf.org/rfc/rfc2616.txt
3263                          * 
3264                          * 14.4 Accept-Language
3265                          * 
3266                          *    The Accept-Language request-header field is similar to Accept, but
3267                          *    restricts the set of natural languages that are preferred as a
3268                          *    response to the request. Language tags are defined in section 3.10.
3269                          * 
3270                          *        Accept-Language = "Accept-Language" ":"
3271                          *                          1#( language-range [ ";" "q" "=" qvalue ] )
3272                          *        language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
3273                          *        
3274                          *    Each language-range MAY be given an associated quality value which
3275                          *    represents an estimate of the user's preference for the languages
3276                          *    specified by that range. The quality value defaults to "q=1". For
3277                          *    example,
3278                          * 
3279                          *        Accept-Language: da, en-gb;q=0.8, en;q=0.7
3280                          * 
3281                          *    would mean: "I prefer Danish, but will accept British English and
3282                          *    other types of English." A language-range matches a language-tag if
3283                          *    it exactly equals the tag, or if it exactly equals a prefix of the
3284                          *    tag such that the first tag character following the prefix is "-".
3285                          *    The special range "*", if present in the Accept-Language field,
3286                          *    matches every tag not matched by any other range present in the
3287                          *    Accept-Language field.
3288                          * 
3289                          * TODO: this is appropriate implement or not
3290                          */
3291                         $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
3292                         
3293                         /* retrieve language token of language tag from first token */
3294                         $language = '';
3295                         if ( is_array($languages) && count($languages) > 0 )
3296                         {
3297                                 $language = preg_replace('#^([\w]+).*$#', '$1', $languages[0]);
3298                         }
3299                         
3300                         $locales = i18n::get_available_locale_list();
3301                         foreach ( $locales as $locale )
3302                         {
3303                                 if ( i18n::strpos($locale, $language) === 0 )
3304                                 {
3305                                         $this->locale = $locale;
3306                                         break;
3307                                 }
3308                         }
3309                 }
3310                 
3311                 /* include installer translation messages */
3312                 $translation_file = "./locales/{$this->locale}." . i18n::get_current_charset() . '.php';
3313                 if ( !file_exists($translation_file) )
3314                 {
3315                         $translation_file = './locales/en_Latn_US.UTF-8.php';
3316                 }
3317                 include($translation_file);
3318                 
3319                 /* include global translation messages */
3320                 $translation_file = "../nucleus/locales/{$this->locale}." . i18n::get_current_charset() . '.php';
3321                 if ( !file_exists($translation_file) )
3322                 {
3323                         $translation_file = './locales/en_Latn_US.UTF-8.php';
3324                 }
3325                 include($translation_file);
3326                 
3327                 i18n::set_current_locale($this->locale);
3328                 
3329                 return;
3330         }
3331
3332         public function check_mysql_parameters()
3333         {
3334                 global $MYSQL_HANDLER;
3335                 
3336                 $parameters = array('mysql_host', 'mysql_user', 'mysql_password', 'mysql_database', 'mysql_tablePrefix');
3337                 $this->read_parameter($parameters);
3338
3339                 $errors = array();
3340                 if ( $this->mysql_host == '' )
3341                 {
3342                         $errors[] = sprintf(_VALID_ERROR1, _DB_FIELD1);
3343                 }
3344
3345                 if ( $this->mysql_user == '' )
3346                 {
3347                         $errors[] = sprintf(_VALID_ERROR1, _DB_FIELD2);
3348                 }
3349                 
3350                 if ( $this->mysql_user != ''
3351                         && !preg_match('/^[a-z0-9_\-]+$/i', $this->mysql_user) )
3352                 {
3353                         $errors[] = sprintf(_VALID_ERROR2, _DB_FIELD2);
3354                 }
3355                 
3356                 if ( $this->mysql_database == '' )
3357                 {
3358                         $errors[] = sprintf(_VALID_ERROR1, _DB_FIELD4);
3359                 }
3360
3361                 if ( $this->mysql_database != ''
3362                         && !preg_match('/^[a-z0-9_\-]+$/i', $this->mysql_database) )
3363                 {
3364                         $errors[] = sprintf(_VALID_ERROR2, _DB_FIELD4);
3365                 }
3366
3367                 if ( $this->mysql_tablePrefix != ''
3368                         && !preg_match('/^[a-z0-9_]+$/i', $this->mysql_tablePrefix) )
3369                 {
3370                         $errors[] = sprintf(_VALID_ERROR3, _DB_FIELD5);
3371                 }
3372                 
3373                 if ( count($errors) == 0 )
3374                 {
3375                         $mysql_conn = @DB::setConnectionInfo($MYSQL_HANDLER[1], $this->mysql_host, $this->mysql_user, $this->mysql_password);
3376                         if ( $mysql_conn == false )
3377                         {
3378                                 $errors[] = _DBCONNECT_ERROR;
3379                         }
3380                         else
3381                         {
3382                                 @DB::disConnect();
3383                         }
3384                 }
3385
3386                 return $errors;
3387         }
3388
3389         public function check_user_parameters()
3390         {
3391                 $parameters = array('user_name', 'user_realname', 'user_password', 'user_password2', 'user_email');
3392                 $this->read_parameter($parameters);
3393
3394                 $errors = array();
3395                 if ( $this->user_realname == '' )
3396                 {
3397                         $errors[] = sprintf(_VALID_ERROR1, _ADMIN_FIELD1);
3398                 }
3399
3400                 if ( $this->user_name == '' )
3401                 {
3402                         $errors[] = sprintf(_VALID_ERROR1, _ADMIN_FIELD2);
3403                 }
3404                 elseif ( !preg_match("/^[a-z0-9]+([ a-z0-9]*[a-z0-9]+)?$/i", $this->user_name) )
3405                 {
3406                         $errors[] = _VALID_ERROR5;
3407                 }
3408
3409                 if ( $this->user_password == '' || $this->user_password2 == '' )
3410                 {
3411                         $errors[] = sprintf(_VALID_ERROR1, _ADMIN_FIELD3);
3412                         $this->user_password = '';
3413                 }
3414                 elseif ( $this->user_password != $this->user_password2 )
3415                 {
3416                         $errors[] = _VALID_ERROR6;
3417                         $this->user_password = '';
3418                 }
3419
3420                 if ( !preg_match("/^[a-z0-9\._+\-]+@[a-z0-9\._\-]+\.[a-z]{2,6}$/i", $this->user_email) )
3421                 {
3422                         $errors[] = _VALID_ERROR7;
3423                 }
3424
3425                 return $errors;
3426         }
3427
3428         public function check_weblog_parameters()
3429         {
3430                 $parameters = array('blog_name', 'blog_shortname');
3431                 $this->read_parameter($parameters);
3432
3433                 $errors = array();
3434                 if ( $this->blog_name == '' )
3435                 {
3436                         $errors[] = sprintf(_VALID_ERROR1, _BLOG_FIELD1);
3437                 }
3438
3439                 if ( $this->blog_shortname == '' )
3440                 {
3441                         $errors[] = sprintf(_VALID_ERROR1, _BLOG_FIELD2);
3442                 }
3443
3444                 if ( !preg_match("/^[a-z0-9]+$/i", $this->blog_shortname) )
3445                 {
3446                         $errors[] = _VALID_ERROR4;
3447                 }
3448
3449                 return $errors;
3450         }
3451
3452         public function check_uri_parameters()
3453         {
3454                 $parameters = array('IndexURL', 'AdminURL', 'MediaURL', 'SkinsURL', 'PluginURL', 'ActionURL');
3455                 $this->read_parameter($parameters);
3456
3457                 $errors = array();
3458                 if ( substr($this->IndexURL, -1, 1) !== '/' )
3459                 {
3460                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD1);
3461                 }
3462
3463                 if ( substr($this->AdminURL, -1, 1) !== '/' )
3464                 {
3465                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD2);
3466                 }
3467
3468                 if ( substr($this->MediaURL, -1, 1) !== '/' )
3469                 {
3470                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD4);
3471                 }
3472
3473                 if ( substr($this->SkinsURL, -1, 1) !== '/' )
3474                 {
3475                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD6);
3476                 }
3477
3478                 if ( substr($this->PluginURL, -1, 1) !== '/' )
3479                 {
3480                         $errors[] = sprintf(_VALID_ERROR8, _PATH_FIELD8);
3481                 }
3482
3483                 if ( strrchr($this->ActionURL, '/') != '/action.php' )
3484                 {
3485                         $errors[] = sprintf(_VALID_ERROR9, _PATH_FIELD9);
3486                 }
3487
3488                 return $errors;
3489         }
3490
3491         public function check_path_parameters()
3492         {
3493                 $parameters = array('AdminPath', 'MediaPath', 'SkinsPath');
3494                 $this->read_parameter($parameters);
3495
3496                 $separators = array('/', DIRECTORY_SEPARATOR);
3497                 $errors = array();
3498                 if ( !in_array(substr($this->AdminPath, -1, 1), $separators) )
3499                 {
3500                         $errors[] = sprintf(_VALID_ERROR10, _PATH_FIELD3);
3501                 }
3502                 elseif ( !file_exists($this->AdminPath) )
3503                 {
3504                         $errors[] = sprintf(_VALID_ERROR11, _PATH_FIELD3);
3505                 }
3506
3507                 if ( !in_array(substr($this->MediaPath, -1, 1), $separators) )
3508                 {
3509                         $errors[] = sprintf(_VALID_ERROR10, _PATH_FIELD5);
3510                 }
3511                 elseif ( !file_exists($this->MediaPath) )
3512                 {
3513                         $errors[] = sprintf(_VALID_ERROR11, _PATH_FIELD5);
3514                 }
3515
3516                 if ( !in_array(substr($this->SkinsPath, -1, 1), $separators) )
3517                 {
3518                         $errors[] = sprintf(_VALID_ERROR10, _PATH_FIELD7);
3519                 }
3520                 elseif ( !file_exists($this->SkinsPath) )
3521                 {
3522                         $errors[] = sprintf(_VALID_ERROR11, _PATH_FIELD7);
3523                 }
3524
3525                 return $errors;
3526         }
3527
3528         /**
3529          * check all parameters
3530          * @return bool
3531          */
3532         public function check_all_parameters()
3533         {
3534                 $this->set_locale();
3535
3536                 $isValid = true;
3537                 $isValid &= (count($this->check_mysql_parameters()) == 0);
3538                 $isValid &= (count($this->check_user_parameters()) == 0);
3539                 $isValid &= (count($this->check_weblog_parameters()) == 0);
3540                 $isValid &= (count($this->check_uri_parameters()) == 0);
3541                 $isValid &= (count($this->check_path_parameters()) == 0);
3542
3543                 return $isValid;
3544         }
3545 }
3546 >>>>>>> skinnable-master