3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2012 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
12 * This script will install the Nucleus tables in your SQL-database,
13 * and initialize the data in those tables.
17 * @license http://nucleuscms.org/license.txt GNU General Public License
18 * @copyright Copyright (C) 2002-2012 The Nucleus Group
22 /* global values initialize */
25 /* reporting all errors for support */
26 error_reporting(E_ALL);
28 $minimum_php_version = '5.0.6';
29 $minimum_mysql_version = '3.23';
31 $page_footer_copyright = '© 2001-2012 The Nucleus Groupe . Running Nucleus CMS v4.00';
33 // begin if: server's PHP version is below the minimum; halt installation
34 if ( version_compare(PHP_VERSION, $minimum_php_version, '<') )
36 exit('<div style="font-size: xx-large;"> Nucleus requires at least PHP version '. $minimum_php_version .'</div>');
39 // make sure there's no unnecessary escaping: # set_magic_quotes_runtime(0);
40 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
42 ini_set('magic_quotes_runtime', '0');
45 /* default installed plugins and skins */
46 $aConfPlugsToInstall = array('NP_SecurityEnforcer', 'NP_SkinFiles', 'NP_Text');
47 $aConfSkinsToImport = array('atom', 'rss2.0', 'rsd', 'default');
49 // Check if some important files
52 /* i18n class is needed for internationalization */
53 include_once('../nucleus/libs/i18n.php');
54 if ( !i18n::init('UTF-8', './locales') )
56 exit('<div style="font-size: xx-large;"> Failed to initialize iconv or mbstring extension. Would you please contact the administrator of your PHP server? </div>');
59 // check if mysql support is installed; this check may not make sense, as is, in a version past 3.5x
60 if ( !function_exists('mysql_query') && !function_exists('mysqli_query') )
62 exit('<div style="font-size: xx-large;"> Your PHP version does not have support for MySQL :( </div>');
65 // include core classes that are needed for login & plugin handling
66 include_once('../nucleus/libs/mysql.php');
68 // added for 3.5 sql_* wrapper
69 global $MYSQL_HANDLER;
71 if ( !isset($MYSQL_HANDLER) )
73 $MYSQL_HANDLER = array('mysql', '');
75 include_once('../nucleus/libs/sql/' . $MYSQL_HANDLER[0] . '.php');
78 if ( count($_GET) == 0 && count($_POST) == 0 )
80 unset($_SESSION['param_manager']);
83 // restore the $param from the session
84 if ( array_key_exists('param_manager', $_SESSION) )
86 $param = $_SESSION['param_manager'];
90 $param = new PARAM_MANAGER();
93 // include translation file
94 if ( array_key_exists('locale', $_POST) ) $param->set_locale();
95 i18n::set_current_locale($param->locale);
96 $translation_file = './locales/' . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php';
97 if ( !file_exists($translation_file) )
99 $translation_file = './locales/en_Latn_US.UTF-8.php';
101 include($translation_file);
105 // $param is saved to the session
108 $_SESSION['param_manager'] = $param;
112 unset($_SESSION['param_manager']);
124 if ( array_key_exists('action', $_POST) )
134 if ( array_key_exists('mode', $_REQUEST) )
136 if ( $_REQUEST['mode'] == 'detail' )
138 $param->set_state('detail');
140 elseif ( $_REQUEST['mode'] == 'simple' )
142 $param->set_state('mysql');
146 // input parameter check
149 switch ( $param->state )
152 $param->set_locale();
153 $param->set_state('mysql');
157 if ( count($param->check_mysql_parameters()) == 0 )
159 $param->set_state('weblog');
164 if ( count($param->check_user_parameters()) == 0
165 && count($param->check_weblog_parameters()) == 0 )
167 $param->set_state('install');
172 if ( $param->check_all_parameters() )
174 $param->set_state('install');
183 switch ( $param->state )
186 show_select_locale_form();
189 show_database_setting_form($isPostback);
192 show_blog_setting_form($isPostback);
195 show_detail_setting_form($isPostback);
198 show_install_complete_form();
205 * header tag of the installation screens
207 function show_header()
211 /* HTTP 1.1 application for no caching */
212 header("Cache-Control: no-cache, must-revalidate");
213 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
214 header('Content-Type: text/html; charset=' . i18n::get_current_charset());
217 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
218 <html xmlns="http://www.w3.org/1999/xhtml">
220 <title><?php echo _TITLE; ?></title>
221 <link rel="stylesheet" type="text/css" href="./styles/inst.css" />
222 <style type="text/css">
224 <?php echo _BODYFONTSTYLE; ?>
230 <div id="navigation">
231 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
234 if ( in_array($param->state, array('mysql', 'weblog', 'install')) )
236 echo '<li>', _STEP1, '</li><li';
237 if ( $param->state == 'mysql' )
241 echo '> > ', _STEP2, '</li><li';
242 if ( in_array($param->state, array('mysql', 'weblog')) )
246 echo '> > ', _STEP3, "</li>\n";
248 if ( in_array($param->state, array('mysql', 'weblog', 'detail')) )
250 echo '<li class="rightbox">';
251 if ( in_array($param->state, array('mysql', 'weblog')) )
253 echo '<a href="./?mode=detail">', _MODE2, '</a>';
257 echo '<a href="./?mode=simple">', _MODE1, '</a>';
269 * footer tag of the installation screens
271 function show_footer()
273 global $page_footer_copyright;
276 <?php echo $page_footer_copyright; ?>
284 * Display the form for language select
286 function show_select_locale_form()
288 // Get the browser language that can be displayed
289 // TODO: default locale select simple implementation
290 $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
291 foreach ($languages as $language)
293 $language = preg_replace('#([\w]+).*#', '$1', $language);
298 array('en_Latn_US', 'English - United States'),
299 array('ja_Jpan_JP', 'Japanese - Japan')
303 <p style="font-size:152%;font-weight:bold;">
306 <form method="post" action="./index.php">
309 <select name="locale">
311 foreach ( $locales as $locale )
313 echo "<option value=\"$locale[0]\"";
314 if ( i18n::strpos($locale[0], $language) === 0 )
318 echo ">$locale[1]</option>\n";
323 <button type="submit" name="action" value="locale" class="sbt_arw">START</button>
332 * Display the form to set up a database
333 * @param bool $isPostback
335 function show_database_setting_form($isPostback)
337 global $param, $minimum_mysql_version;
339 $config_writable = canConfigFileWritable();
340 $mysql_version = getMySqlVersion();
346 if ( $config_writable != '' )
348 echo '<span class="err">', $config_writable, '</span>';
350 if ( $mysql_version == '0.0.0' )
352 echo '<span class="err">', _ERROR21, '</span>';
354 elseif ( version_compare($mysql_version, $minimum_mysql_version, '<') )
356 echo '<span class="err">', sprintf(_ERROR20 , $minimum_mysql_version), '</span>';
360 <form method="post" action="./index.php">
362 <h2><?php echo _DB_HEADER; ?></h2>
367 $errors = $param->check_mysql_parameters();
368 if ( is_array($errors) )
370 foreach ( $errors as $error )
372 echo '<span class="err">', $error, "</span>\n";
380 <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
381 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
384 <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
385 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
388 <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
389 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
392 <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
393 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
397 <button type="submit" name="mode" value="detail" class="sbt_sqr"><?php echo _MODE2; ?></button>
398 <button type="submit" name="action" value="mysql" class="sbt_arw"><?php echo _NEXT; ?></button>
401 <?php echo _DB_TEXT1; ?>
410 * Displays a form to the blog settings
411 * @param bool $isPostback
413 function show_blog_setting_form($isPostback)
420 <?php echo _SIMPLE_NAVI2; ?>
422 <form method="post" action="./index.php">
424 <h2><?php echo _BLOG_HEADER; ?></h2>
429 $errors = $param->check_weblog_parameters();
430 if ( is_array($errors) )
432 foreach ( $errors as $error )
434 echo '<span class="err">', $error, "</span>\n";
442 <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
443 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
446 <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
447 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
453 <h2><?php echo _ADMIN_HEADER; ?></h2>
458 $errors = $param->check_user_parameters();
459 if ( is_array($errors) )
461 foreach ( $errors as $error )
463 echo '<span class="err">', $error, "</span>\n";
471 <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
472 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
475 <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
476 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
479 <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
480 <td><input type="password" name="user_password" /></td>
483 <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
484 <td><input type="password" name="user_password2" /></td>
487 <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
488 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
492 <button type="submit" name="action" value="weblog" class="sbt_arw"><?php echo _INSTALL; ?></button>
501 * Displays a form to the detail settings
502 * @param bool $isPostback
504 function show_detail_setting_form($isPostback)
506 global $param, $minimum_mysql_version;
508 $mysql_version = getMySqlVersion();
510 <div id="container_detailed">
512 <?php echo _DETAIL_NAVI1; ?>
514 if ( $isPostback && !$param->check_all_parameters() )
516 echo '<span class="err">', _ERROR26, "</span>\n";
521 <li>PHP: <?php echo phpversion(); ?></li>
524 echo ($mysql_version == '0.0.0') ? _ERROR21 : $mysql_version;
525 if ( version_compare($mysql_version, $minimum_mysql_version, '<') )
527 echo '<span class="err">', sprintf(_ERROR20 , $minimum_mysql_version), '</span>';
531 <form method="post" action="">
534 <h2><?php echo _DETAIL_HEADER1; ?></h2>
539 $errors = $param->check_mysql_parameters();
540 if ( is_array($errors) )
542 foreach ( $errors as $error )
544 echo '<span class="err">', $error, "</span>\n";
552 <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
553 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
556 <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
557 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
560 <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
561 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
564 <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
565 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
568 <th><span class="nam"><?php echo _DB_FIELD5; ?></span><span class="sub"><?php echo _DB_FIELD5_DESC; ?></span></th>
569 <td><input type="text" name="mysql_tablePrefix" value="<?php echo $param->mysql_tablePrefix; ?>" /></td>
573 <h2><?php echo _DETAIL_HEADER2; ?></h2>
578 $errors = $param->check_uri_parameters();
579 if ( is_array($errors) )
581 foreach ( $errors as $error )
583 echo '<span class="err">', $error, "</span>\n";
586 $errors = $param->check_path_parameters();
587 if ( is_array($errors) )
589 foreach ( $errors as $error )
591 echo '<span class="err">', $error, "</span>\n";
599 <th><span class="nam"><?php echo _PATH_FIELD1; ?></span></th>
600 <td><input type="text" name="IndexURL" value="<?php echo $param->IndexURL; ?>" /></td>
603 <th><span class="nam"><?php echo _PATH_FIELD2; ?></span></th>
604 <td><input type="text" name="AdminURL" value="<?php echo $param->AdminURL; ?>" /></td>
607 <th><span class="nam"><?php echo _PATH_FIELD3; ?></span></th>
608 <td><input type="text" name="AdminPath" value="<?php echo $param->AdminPath; ?>" /></td>
611 <th><span class="nam"><?php echo _PATH_FIELD4; ?></span></th>
612 <td><input type="text" name="MediaURL" value="<?php echo $param->MediaURL; ?>" /></td>
615 <th><span class="nam"><?php echo _PATH_FIELD5; ?></span></th>
616 <td><input type="text" name="MediaPath" value="<?php echo $param->MediaPath; ?>" /></td>
619 <th><span class="nam"><?php echo _PATH_FIELD6; ?></span></th>
620 <td><input type="text" name="SkinsURL" value="<?php echo $param->SkinsURL; ?>" /></td>
623 <th><span class="nam"><?php echo _PATH_FIELD7; ?></span></th>
624 <td><input type="text" name="SkinsPath" value="<?php echo $param->SkinsPath; ?>" /></td>
627 <th><span class="nam"><?php echo _PATH_FIELD8; ?></span></th>
628 <td><input type="text" name="PluginURL" value="<?php echo $param->PluginURL; ?>" /></td>
631 <th><span class="nam"><?php echo _PATH_FIELD9; ?></span></th>
632 <td><input type="text" name="ActionURL" value="<?php echo $param->ActionURL; ?>" /></td>
636 <?php echo _DETAIL_TEXT3; ?>
639 <h2><?php echo _DETAIL_HEADER3; ?></h2>
645 $errors = $param->check_user_parameters();
646 if ( is_array($errors) )
648 foreach ( $errors as $error )
650 echo '<span class="err">', $error, "</span>\n";
658 <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
659 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
662 <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
663 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
666 <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
667 <td><input type="password" name="user_password" /></td>
670 <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
671 <td><input type="password" name="user_password2" /></td>
674 <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
675 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
679 <h2><?php echo _DETAIL_HEADER4; ?></h2>
685 $errors = $param->check_weblog_parameters();
686 if ( is_array($errors) )
688 foreach ( $errors as $error )
690 echo '<span class="err">', $error, "</span>\n";
698 <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
699 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
702 <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
703 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
708 <?php echo _DETAIL_TEXT6; ?>
712 <button type="submit" name="action" value="detail" class="sbt_arw"><?php echo _INSTALL; ?></button>
721 * Displays a screen to signal the completion of the installation
723 function show_install_complete_form()
725 global $MYSQL_HANDLER, $param;
726 $errors = do_install();
731 if ( is_array($errors) && count($errors) > 0 )
734 foreach ( $errors as $error )
736 echo '<span class="err">', $error, "</span>\n";
742 if ( array_key_exists('config_data', $_SESSION) )
744 echo '<span class="err">', _INST_TEXT4, '</span>';
745 // FIXME: textareaにしてJavascriptでコピーできたらいい?
747 <pre><code><?php echo htmlentities($_SESSION['config_data'], null, i18n::get_current_charset() ) ?></code></pre>
752 echo '<span class="err">', _INST_TEXT5, '</span>';
756 <form method="post" action="./index.php">
758 <h2><?php echo _INST_HEADER1; ?></h2>
760 <?php echo sprintf(_INST_TEXT1, $param->blog_name); ?>
763 <button type="button" name="toBlog" onclick="location.href='<?php echo $param->IndexURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON1; ?></button>
768 <h2><?php echo _INST_HEADER2; ?></h2>
770 <?php echo _INST_TEXT2; ?>
773 <button type="button" name="toMng" onclick="location.href='<?php echo $param->AdminURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON2; ?></button>
778 <h2><?php echo _INST_HEADER3; ?></h2>
780 <?php echo _INST_TEXT3; ?>
783 <button type="button" name="toAddBlog" onclick="location.href='<?php echo $param->AdminURL; ?>index.php?action=createnewlog';" class="sbt_arw"><?php echo _INST_BUTTON3; ?></button>
796 * The installation process itself
797 * @return array error messages
799 function do_install()
802 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_PREFIX, $MYSQL_CONN;
803 global $DIR_NUCLEUS, $DIR_MEDIA, $DIR_SKINS, $DIR_PLUGINS, $DIR_LANG, $DIR_LIBS;
807 * 1. put all param-vars into vars
809 $MYSQL_HOST = $param->mysql_host;
810 $MYSQL_USER = $param->mysql_user;
811 $MYSQL_PASSWORD = $param->mysql_password;
812 $MYSQL_DATABASE = $param->mysql_database;
813 $MYSQL_PREFIX = $param->mysql_tablePrefix;
815 $DIR_NUCLEUS = $param->AdminPath;
816 $DIR_MEDIA = $param->MediaPath;
817 $DIR_SKINS = $param->SkinsPath;
818 $DIR_PLUGINS = $DIR_NUCLEUS . 'plugins/';
819 $DIR_LOCALES = $DIR_NUCLEUS . 'locales/';
820 $DIR_LIBS = $DIR_NUCLEUS . 'libs/';
823 * 2.open mySQL connection
825 $MYSQL_CONN = @sql_connect_args($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD);
826 if ( $MYSQL_CONN == false )
833 * 3. try to create database if needed
835 if ( !sql_query('CREATE DATABASE IF NOT EXISTS `' . $MYSQL_DATABASE . '`') )
837 $errors[] = _ERROR12 . ': ' . sql_error();
841 * 4. try to select database
843 if ( !sql_select_db($MYSQL_DATABASE) )
845 $errors[] = _ERROR13;
847 sql_set_charset('utf8');
849 if ( count($errors) > 0 )
857 $table_names = array(
871 'nucleus_activation',
875 $prefixed_table_names = array();
876 foreach ( $table_names as $table_name )
878 $prefixed_table_names[] = $MYSQL_PREFIX . $table_name;
881 // table exists check
882 $result = sql_query('SHOW TABLES');
883 while ($row = mysql_fetch_array($result, MYSQL_NUM))
885 if ( in_array($row[0], $prefixed_table_names) )
887 $errors[] = _ERROR14;
891 if ( count($errors) > 0 )
896 $filename = 'install.sql';
897 $fd = fopen($filename, 'r');
898 $queries = fread($fd, filesize($filename) );
901 $queries = preg_split('#(;\n|;\r)#', $queries);
903 foreach ( $queries as $query )
905 if ( preg_match('/\w+/', $query) )
909 $query = str_replace($table_names, $prefixed_table_names, $query);
912 if ( !sql_query($query) )
914 $errors[] = _ERROR15 . ' (<small>' . $query . '</small>): ' . sql_error();
920 * 6. put needed records
922 /* push first post */
923 $query = "INSERT INTO %s VALUES (1, '%s', '%s', '%s', 1, 1, '%s', 0, 0, 0, 1, 0, 1)";
924 $query = sprintf($query, tableName('nucleus_item'), _1ST_POST_TITLE, _1ST_POST, _1ST_POST2, i18n::formatted_datetime('mysql', time()));
925 if ( !sql_query($query) )
927 $errors[] = _ERROR15 . ' (<small>' . $newpost . '</small>): ' . sql_error();
930 /* push configurations */
931 array_merge($errors, updateConfig('IndexURL', $param->IndexURL));
932 array_merge($errors, updateConfig('AdminURL', $param->AdminURL));
933 array_merge($errors, updateConfig('MediaURL', $param->MediaURL));
934 array_merge($errors, updateConfig('SkinsURL', $param->SkinsURL));
935 array_merge($errors, updateConfig('PluginURL', $param->PluginURL));
936 array_merge($errors, updateConfig('ActionURL', $param->ActionURL));
937 array_merge($errors, updateConfig('AdminEmail', $param->user_email));
938 array_merge($errors, updateConfig('SiteName', $param->blog_name));
939 array_merge($errors, updateConfig('Locale', i18n::get_current_locale()));
941 /* escape strings for SQL */
942 $user_name = sql_real_escape_string($param->user_name);
943 $user_realname = sql_real_escape_string($param->user_realname);
944 $user_password = sql_real_escape_string(md5($param->user_password));
945 $user_email = sql_real_escape_string($param->user_email);
946 $blog_name = sql_real_escape_string($param->blog_name);
947 $blog_shortname = sql_real_escape_string($param->blog_shortname);
948 $config_indexurl = sql_real_escape_string($param->IndexURL);
950 /* push super admin */
951 $query = "UPDATE %s SET mname = '%s', mrealname = '%s', mpassword = '%s', memail = '%s', murl = '%s', madmin = 1, mcanlogin = 1 WHERE mnumber = 1";
952 $query = sprintf($query, tableName('nucleus_member'), $user_name, $user_realname, $user_password, $user_email, $config_indexurl);
953 if ( !sql_query($query) )
955 $errors[] = _ERROR16 . ': ' . sql_error();
958 /* push new weblog */
959 $query = "UPDATE %s SET bname = '%s', bshortname = '%s', burl = '%s' WHERE bnumber = 1";
960 $query = sprintf($query, tableName('nucleus_blog'), $blog_name, $blog_shortname, $config_indexurl);
961 if ( !sql_query($query) )
963 $errors[] = _ERROR17 . ': ' . sql_error();
966 /* push default category */
967 $query = "UPDATE %s SET cname = '%s', cdesc = '%s' WHERE catid = 1";
968 $query = sprintf($query, tableName('nucleus_category'), _GENERALCAT_NAME, _GENERALCAT_DESC);
969 if ( !sql_query($query) )
971 $errors[] = _ERROR17 . ': ' . sql_error();
977 * 7. install default plugins and skins
979 global $aConfPlugsToInstall, $aConfSkinsToImport;
980 $aSkinErrors = array();
981 $aPlugErrors = array();
983 if ( (count($aConfPlugsToInstall) > 0) || (count($aConfSkinsToImport) > 0) )
985 include_once($DIR_LIBS . 'globalfunctions.php');
987 if ( !isset($manager) )
989 $manager = new MANAGER;
992 $aSkinErrors = installCustomSkins();
993 if ( count($aSkinErrors) > 0 )
995 array_merge($errors, $aSkinErrors);
998 $query = "SELECT sdnumber FROM %s WHERE sdname='default'";
999 $query = sprintf($query, tableName('nucleus_skin_desc'));
1000 $res = sql_query($query);
1001 $obj = sql_fetch_assoc($res);
1002 $defSkinID = (integer) $obj['sdnumber'];
1004 $query = "UPDATE %s SET bdefskin=%d WHERE bnumber=1";
1005 $query = sprintf($query, tableName('nucleus_blog'), $defSkinID);
1007 $query = "UPDATE %s SET value=%d WHERE name='BaseSkin'";
1008 $query = sprintf($query, tableName('nucleus_config'), $defSkinID);
1011 $aPlugErrors = installCustomPlugs($manager);
1012 if ( count($aPlugErrors) > 0 )
1014 array_merge($errors, $aPlugErrors);
1019 * 8. Write config file ourselves (if possible)
1021 $config_data = '<' . '?php' . "\n";
1022 $config_data .= "// mySQL connection information\n";
1023 $config_data .= "\$MYSQL_HOST = '" . $MYSQL_HOST . "';\n";
1024 $config_data .= "\$MYSQL_USER = '" . $MYSQL_USER . "';\n";
1025 $config_data .= "\$MYSQL_PASSWORD = '" . $MYSQL_PASSWORD . "';\n";
1026 $config_data .= "\$MYSQL_DATABASE = '" . $MYSQL_DATABASE . "';\n";
1027 $config_data .= "\$MYSQL_PREFIX = '" . $MYSQL_PREFIX . "';\n";
1028 $config_data .= "// new in 3.50. first element is db handler, the second is the db driver used by the handler\n";
1029 $config_data .= "// default is \$MYSQL_HANDLER = array('mysql','mysql');\n";
1030 $config_data .= "//\$MYSQL_HANDLER = array('mysql','mysql');\n";
1031 $config_data .= "//\$MYSQL_HANDLER = array('pdo','mysql');\n";
1032 $config_data .= "\$MYSQL_HANDLER = array('".$MYSQL_HANDLER[0]."','".$MYSQL_HANDLER[1]."');\n";
1033 $config_data .= "\n";
1034 $config_data .= "// main nucleus directory\n";
1035 $config_data .= "\$DIR_NUCLEUS = '" . $DIR_NUCLEUS . "';\n";
1036 $config_data .= "\n";
1037 $config_data .= "// path to media dir\n";
1038 $config_data .= "\$DIR_MEDIA = '" . $DIR_MEDIA . "';\n";
1039 $config_data .= "\n";
1040 $config_data .= "// extra skin files for imported skins\n";
1041 $config_data .= "\$DIR_SKINS = '" . $DIR_SKINS . "';\n";
1042 $config_data .= "\n";
1043 $config_data .= "// these dirs are normally sub dirs of the nucleus dir, but \n";
1044 $config_data .= "// you can redefine them if you wish\n";
1045 $config_data .= "\$DIR_PLUGINS = \$DIR_NUCLEUS . 'plugins/';\n";
1046 $config_data .= "\$DIR_LOCALES = \$DIR_NUCLEUS . 'locales/';\n";
1047 $config_data .= "\$DIR_LIBS = \$DIR_NUCLEUS . 'libs/';\n";
1048 $config_data .= "\n";
1049 $config_data .= "// include libs\n";
1050 $config_data .= "include(\$DIR_LIBS.'globalfunctions.php');\n";
1051 $config_data .= "?" . ">";
1054 if ( @!file_exists('../config.php') || is_writable('../config.php') )
1056 if ( $fp = @fopen('../config.php', 'w') )
1058 $result = @fwrite($fp, $config_data, i18n::strlen($config_data) );
1065 // try to change the read-only permission.
1066 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1068 @chmod('../config.php', 0444);
1073 $_SESSION['config_data'] = $config_data;
1080 * Confirm that you can write to the configuration file
1081 * @return string error message
1083 function canConfigFileWritable()
1085 if ( @file_exists('../config.php') && @!is_writable('../config.php') )
1087 // try to change the read-write permission.
1088 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1090 @chmod('../config.php', 0666);
1093 if ( @!is_writable('../config.php') )
1102 * To obtain the version of MySQL
1105 function getMySqlVersion()
1107 global $minimum_mysql_version, $errors;
1108 // Turn on output buffer
1109 // Needed to repress the output of the sql function that are
1110 // not part of php (in this case the @ operator doesn't work)
1113 // note: this piece of code is taken from phpMyAdmin
1114 $conn = sql_connect_args('localhost', '', '');
1115 $result = @sql_query('SELECT VERSION() AS version', $conn);
1117 if ( $result != FALSE && sql_num_rows($result) > 0 )
1119 $row = sql_fetch_array($result);
1120 $match = i18n::explode('.', $row['version']);
1124 $result = @sql_query('SHOW VARIABLES LIKE \'version\'', $conn);
1126 if ( $result != FALSE && @sql_num_rows($result) > 0 )
1128 $row = sql_fetch_row($result);
1129 $match = i18n::explode('.', $row[1]);
1133 //$output = shell_exec('mysql -V');
1134 $output = ( function_exists('shell_exec') ) ? @shell_exec('mysql -V') : '0.0.0';
1135 preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
1136 $match = i18n::explode('.', $version[0]);
1138 if ( $match[0] == '' )
1140 $match = array('0', '0', '0');
1145 @sql_disconnect($conn);
1147 //End and clean output buffer
1150 return implode($match, '.');
1154 * Add a table prefix if it is used
1156 * @param string $input table name with prefix
1159 function tableName($input)
1161 global $MYSQL_PREFIX;
1162 if ( $MYSQL_PREFIX )
1164 return $MYSQL_PREFIX . $input;
1173 * Install custom plugins
1175 * @param object $manager MANAGER class instance
1177 function installCustomPlugs($manager)
1179 global $aConfPlugsToInstall, $DIR_LIBS;
1182 if ( count($aConfPlugsToInstall) == 0 )
1187 $res = sql_query('SELECT * FROM ' . tableName('nucleus_plugin') );
1188 $numCurrent = sql_num_rows($res);
1190 foreach ( $aConfPlugsToInstall as $plugName )
1192 $query = 'INSERT INTO ' . tableName('nucleus_plugin') . ' (porder, pfile) VALUES (' . (++$numCurrent) . ", '" . sql_real_escape_string($plugName) . "')";
1195 $manager->clearCachedInfo('installedPlugins');
1196 $plugin =& $manager->getPlugin($plugName);
1197 $plugin->setID($numCurrent);
1201 sql_query('DELETE FROM ' . tableName('nucleus_plugin') . " WHERE pfile = '" . sql_real_escape_string($plugName) . "'");
1203 array_push($aErrors, sprintf(_ERROR22 ,$plugName));
1209 sql_query('DELETE FROM ' . tableName('nucleus_plugin_event') );
1210 $res = sql_query('SELECT pid, pfile FROM ' . tableName('nucleus_plugin') );
1212 while ( $o = sql_fetch_object($res) )
1215 $plug =& $manager->getPlugin($o->pfile);
1219 $eventList = $plug->getEventList();
1220 foreach ( $eventList as $eventName )
1222 sql_query('INSERT INTO ' . tableName('nucleus_plugin_event') . ' (pid, event) VALUES (' . $pid . ", '" . $eventName . "')");
1230 * Install custom skins
1231 * Prepares the installation of custom skins
1233 function installCustomSkins()
1235 global $aConfSkinsToImport, $DIR_LIBS, $DIR_SKINS;
1238 if ( count($aConfSkinsToImport) == 0 )
1243 include_once($DIR_LIBS . 'skinie.php');
1244 $importer = new SKINIMPORT();
1246 foreach ( $aConfSkinsToImport as $skinName )
1249 $skinFile = $DIR_SKINS . $skinName . '/skinbackup.xml';
1251 if ( !@file_exists($skinFile) )
1253 array_push($aErrors, sprintf(_ERROR23, $skinFile));
1257 $error = $importer->readFile($skinFile);
1261 array_push($aErrors, sprintf(_ERROR24, $skinName) . ' : ' . $error);
1265 $error = $importer->writeToDatabase(1);
1269 array_push($aErrors, sprintf(_ERROR25, $skinName) . ' : ' . $error);
1278 * Check if some important files of the Nucleus CMS installation are available
1279 * Give an error if one or more files are not accessible
1281 function do_check_files()
1283 $missingfiles = array();
1288 '../nucleus/index.php',
1289 '../nucleus/media.php',
1290 '../nucleus/libs/ACTION.php',
1291 '../nucleus/libs/ACTIONLOG.php',
1292 '../nucleus/libs/ACTIONS.php',
1293 '../nucleus/libs/ADMIN.php',
1294 '../nucleus/libs/BaseActions.php',
1295 '../nucleus/libs/BLOG.php',
1296 '../nucleus/libs/BODYACTIONS.php',
1297 '../nucleus/libs/COMMENT.php',
1298 '../nucleus/libs/COMMENTACTIONS.php',
1299 '../nucleus/libs/COMMENTS.php',
1300 '../nucleus/libs/ENCAPSULATE.php',
1301 '../nucleus/libs/ENTITY.php',
1302 '../nucleus/libs/globalfunctions.php',
1303 '../nucleus/libs/i18n.php',
1304 '../nucleus/libs/ITEM.php',
1305 '../nucleus/libs/ITEMACTIONS.php',
1306 '../nucleus/libs/LINK.php',
1307 '../nucleus/libs/MANAGER.php',
1308 '../nucleus/libs/MEDIA.php',
1309 '../nucleus/libs/MEMBER.php',
1310 '../nucleus/libs/mysql.php',
1311 '../nucleus/libs/NOTIFICATION.php',
1312 '../nucleus/libs/PAGEFACTORY.php',
1313 '../nucleus/libs/PARSER.php',
1314 '../nucleus/libs/PLUGIN.php',
1315 '../nucleus/libs/PLUGINADMIN.php',
1316 '../nucleus/libs/SEARCH.php',
1317 '../nucleus/libs/showlist.php',
1318 '../nucleus/libs/SKIN.php',
1319 '../nucleus/libs/TEMPLATE.php',
1320 '../nucleus/libs/vars4.1.0.php',
1321 '../nucleus/libs/xmlrpc.inc.php',
1322 '../nucleus/libs/xmlrpcs.inc.php',
1323 '../nucleus/libs/sql/mysql.php'
1326 $count = count($files);
1327 for ( $i = 0; $i < $count; $i++ )
1329 if ( !is_readable($files[$i]) )
1331 array_push( $missingfiles, 'File <b>' . $files[$i] . '</b> is missing or not readable.<br />');
1335 if ( count($missingfiles) > 0 )
1337 exit(implode( "\n", $missingfiles));
1343 * Updates the configuration in the database
1345 * @param string $name name of the config var
1346 * @param string $value new value of the config var
1349 function updateConfig($name, $value)
1352 $name = sql_real_escape_string($name);
1353 $value = trim(sql_real_escape_string($value) );
1355 $query = "UPDATE %s SET value = '%s' WHERE name = '%s'";
1356 $query = sprintf($query, tableName('nucleus_config'), $value, $name);
1358 if ( !sql_query($query) )
1360 $errors[] = _ERROR15 . ': ' . sql_error();
1368 /* process parameter */
1372 /* mysql connection parameters */
1375 public $mysql_password;
1376 public $mysql_database;
1377 public $mysql_tablePrefix;
1379 /* weblog configuration parameters */
1381 public $blog_shortname;
1383 /* member configuration parameters */
1385 public $user_realname;
1386 public $user_password;
1387 private $user_password2;
1390 /* URI parameters */
1399 /* path parameters */
1408 public function __construct()
1413 public function init()
1415 // set default values
1416 $this->state = 'locale';
1417 $this->install_mode = 'simple';
1418 $this->locale = 'en_Latn_US';
1419 $this->mysql_host = @ini_get('mysql.default_host');
1420 $this->blog_name = 'My Nucleus CMS';
1421 $this->blog_shortname = 'mynucleuscms';
1424 $this->root_path = realpath(dirname(__FILE__) . '/..');
1425 if ( substr($this->root_path, -1, 1) !== '/' )
1427 $this->root_path .= '/';
1429 $base_path_pcre = preg_quote($this->root_path, '#');
1431 /* current directry name */
1432 $directory_name = preg_replace("#$base_path_pcre#", '', dirname(__FILE__));
1433 $directory_name_pcre = preg_quote($directory_name, '#');
1436 $root_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
1437 $this->root_url = preg_replace("#$directory_name_pcre(.*)$#", '', $root_url);
1439 $this->AdminPath = $this->root_path . 'nucleus' . DIRECTORY_SEPARATOR;
1440 $this->MediaPath = $this->root_path . 'media' . DIRECTORY_SEPARATOR;
1441 $this->SkinsPath = $this->root_path . 'skins' . DIRECTORY_SEPARATOR;
1443 $this->IndexURL = $this->root_url;
1444 $this->AdminURL = $this->root_url . 'nucleus/';
1445 $this->MediaURL = $this->root_url . 'media/';
1446 $this->SkinsURL = $this->root_url . 'skins/';
1447 $this->PluginURL = $this->root_url . 'nucleus/plugins/';
1448 $this->ActionURL = $this->root_url . 'action.php';
1451 private function read_parameter($parameter)
1453 foreach ( $parameter as $element )
1455 if ( array_key_exists($element, $_POST) )
1457 $this->$element = $_POST[$element];
1462 public function set_state($state)
1464 $states = array('locale', 'mysql', 'weblog', 'detail', 'install');
1465 if ( in_array($state, $states) )
1467 $this->state = $state;
1471 public function set_locale()
1473 $this->read_parameter(array('locale'));
1475 if ( !in_array($this->locale, i18n::get_available_locale_list()) )
1477 $this->locale = 'en_Latn_US';
1481 public function check_mysql_parameters()
1483 $parameters = array('mysql_host', 'mysql_user', 'mysql_password', 'mysql_database', 'mysql_tablePrefix');
1484 $this->read_parameter($parameters);
1487 if ( $this->mysql_host == '' )
1489 $errors[] = sprintf(_ERROR1, _DB_FIELD1);
1492 if ( $this->mysql_user == '' )
1494 $errors[] = sprintf(_ERROR1, _DB_FIELD2);
1497 if ( $this->mysql_user != ''
1498 && !preg_match('/^[[:alnum:]_-]+$/i', $this->mysql_user) )
1500 $errors[] = sprintf(_ERROR2, _DB_FIELD2);
1503 if ( $this->mysql_password == '' )
1505 $errors[] = sprintf(_ERROR1, _DB_FIELD3);
1508 if ( $this->mysql_database == '' )
1510 $errors[] = sprintf(_ERROR1, _DB_FIELD4);
1513 if ( $this->mysql_database != ''
1514 && !preg_match('/^[[:alnum:]_-]+$/i', $this->mysql_database) )
1516 $errors[] = sprintf(_ERROR2, _DB_FIELD4);
1519 if ( $this->mysql_tablePrefix != ''
1520 && !preg_match('/^[[:alnum:]_-]+$/i', $this->mysql_tablePrefix) )
1522 $errors[] = sprintf(_ERROR2, _DB_FIELD5);
1525 if ( count($errors) == 0 )
1527 $mysql_conn = @sql_connect_args($this->mysql_host, $this->mysql_user, $this->mysql_password);
1528 if ( $mysql_conn == false )
1530 $errors[] = _ERROR3;
1534 @sql_close($mysql_conn);
1541 public function check_user_parameters()
1543 $parameters = array('user_name', 'user_realname', 'user_password', 'user_password2', 'user_email');
1544 $this->read_parameter($parameters);
1547 if ( $this->user_name == '' )
1549 $errors[] = sprintf(_ERROR1, _ADMIN_FIELD2);
1551 elseif ( !preg_match("/^[[:alnum:]]+([ [:alnum:]]*[[:alnum:]]+)?$/i", $this->user_name) )
1553 $errors[] = _ERROR5;
1556 if ( $this->user_realname == '' )
1558 $errors[] = sprintf(_ERROR1, _ADMIN_FIELD1);
1561 if ( $this->user_password == '' || $this->user_password2 == '' )
1563 $errors[] = sprintf(_ERROR1, _ADMIN_FIELD3);
1564 $this->user_password = '';
1566 elseif ( $this->user_password != $this->user_password2 )
1568 $errors[] = _ERROR6;
1569 $this->user_password = '';
1572 if ( !preg_match("/^[\w\.-]+@[\w\.-]+\.[[:alpha:]]{2,6}$/", $this->user_email) )
1574 $errors[] = _ERROR7;
1580 public function check_weblog_parameters()
1582 $parameters = array('blog_name', 'blog_shortname');
1583 $this->read_parameter($parameters);
1586 if ( $this->blog_name == '' )
1588 $errors[] = sprintf(_ERROR1, _BLOG_FIELD1);
1591 if ( $this->blog_shortname == '' )
1593 $errors[] = sprintf(_ERROR1, _BLOG_FIELD2);
1596 if ( !preg_match("/^[a-z0-9]+$/", $this->blog_shortname) )
1598 $errors[] = _ERROR4;
1604 public function check_uri_parameters()
1606 $parameters = array('IndexURL', 'AdminURL', 'MediaURL', 'SkinsURL', 'PluginURL', 'ActionURL');
1607 $this->read_parameter($parameters);
1610 if ( substr($this->IndexURL, -1, 1) !== '/' )
1612 $errors[] = sprintf(_ERROR8, _PATH_FIELD1);
1615 if ( substr($this->AdminURL, -1, 1) !== '/' )
1617 $errors[] = sprintf(_ERROR8, _PATH_FIELD2);
1620 if ( substr($this->MediaURL, -1, 1) !== '/' )
1622 $errors[] = sprintf(_ERROR8, _PATH_FIELD4);
1625 if ( substr($this->SkinsURL, -1, 1) !== '/' )
1627 $errors[] = sprintf(_ERROR8, _PATH_FIELD6);
1630 if ( substr($this->PluginURL, -1, 1) !== '/' )
1632 $errors[] = sprintf(_ERROR8, _PATH_FIELD8);
1635 if ( strrchr($this->ActionURL, '/') != '/action.php' )
1637 $errors[] = sprintf(_ERROR9, _PATH_FIELD9);
1643 public function check_path_parameters()
1645 $parameters = array('AdminPath', 'MediaPath', 'SkinsPath');
1646 $this->read_parameter($parameters);
1648 $separators = array('/', DIRECTORY_SEPARATOR);
1650 if ( !in_array(substr($this->AdminPath, -1, 1), $separators) || !file_exists($this->AdminPath) )
1652 $errors[] = sprintf(_ERROR10, _PATH_FIELD3);
1655 if ( !in_array(substr($this->MediaPath, -1, 1), $separators) || !file_exists($this->MediaPath) )
1657 $errors[] = sprintf(_ERROR10, _PATH_FIELD5);
1660 if ( !in_array(substr($this->SkinsPath, -1, 1), $separators) || !file_exists($this->SkinsPath) )
1662 $errors[] = sprintf(_ERROR10, _PATH_FIELD7);
1669 * check all parameters
1672 public function check_all_parameters()
1674 $this->set_locale();
1677 $isValid &= (count($this->check_mysql_parameters()) == 0);
1678 $isValid &= (count($this->check_user_parameters()) == 0);
1679 $isValid &= (count($this->check_weblog_parameters()) == 0);
1680 $isValid &= (count($this->check_uri_parameters()) == 0);
1681 $isValid &= (count($this->check_path_parameters()) == 0);