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 /* reporting all errors for support */
23 error_reporting(E_ALL);
25 $minimum_php_version = '5.0.6';
26 $minimum_mysql_version = '3.23';
28 $page_footer_copyright = '© 2001-2012 The Nucleus Groupe . Running Nucleus CMS v4.00';
30 // begin if: server's PHP version is below the minimum; halt installation
31 if ( version_compare(PHP_VERSION, $minimum_php_version, '<') )
33 exit('<div style="font-size: xx-large;"> Nucleus requires at least PHP version '. $minimum_php_version .'</div>');
36 // make sure there's no unnecessary escaping: # set_magic_quotes_runtime(0);
37 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
39 ini_set('magic_quotes_runtime', '0');
42 /* default installed plugins and skins */
43 $aConfPlugsToInstall = array('NP_SecurityEnforcer', 'NP_SkinFiles', 'NP_Text');
44 $aConfSkinsToImport = array('atom', 'rss2.0', 'rsd', 'default');
46 // Check if some important files
49 /* i18n class is needed for internationalization */
50 include_once('../nucleus/libs/i18n.php');
51 if ( !i18n::init('UTF-8', './locales') )
53 exit('<div style="font-size: xx-large;"> Failed to initialize iconv or mbstring extension. Would you please contact the administrator of your PHP server? </div>');
56 // check if mysql support is installed; this check may not make sense, as is, in a version past 3.5x
57 if ( !function_exists('mysql_query') && !function_exists('mysqli_query') )
59 exit('<div style="font-size: xx-large;"> Your PHP version does not have support for MySQL :( </div>');
62 // include core classes that are needed for login & plugin handling
63 include_once('../nucleus/libs/mysql.php');
65 // added for 3.5 sql_* wrapper
66 global $MYSQL_HANDLER;
68 if ( !isset($MYSQL_HANDLER) )
70 $MYSQL_HANDLER = array('mysql', '');
72 include_once('../nucleus/libs/sql/' . $MYSQL_HANDLER[0] . '.php');
75 if ( count($_GET) == 0 && count($_POST) == 0 )
77 unset($_SESSION['param_manager']);
80 // restore the $param from the session
81 if ( array_key_exists('param_manager', $_SESSION) )
83 $param = $_SESSION['param_manager'];
87 $param = new PARAM_MANAGER();
90 // include translation file
91 if ( array_key_exists('locale', $_POST) ) $param->set_locale();
92 i18n::set_current_locale($param->locale);
93 $translation_file = './locales/' . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php';
94 if ( !file_exists($translation_file) )
96 $translation_file = './locales/en_Latn_US.UTF-8.php';
98 include($translation_file);
102 // $param is saved to the session
105 $_SESSION['param_manager'] = $param;
109 unset($_SESSION['param_manager']);
121 if ( array_key_exists('action', $_POST) )
131 if ( array_key_exists('mode', $_REQUEST) )
133 if ( $_REQUEST['mode'] == 'detail' )
135 $param->set_state('detail');
137 elseif ( $_REQUEST['mode'] == 'simple' )
139 $param->set_state('mysql');
143 // input parameter check
146 switch ( $param->state )
149 $param->set_locale();
150 $param->set_state('mysql');
154 if ( count($param->check_mysql_parameters()) == 0 )
156 $param->set_state('weblog');
161 if ( count($param->check_user_parameters()) == 0
162 && count($param->check_weblog_parameters()) == 0 )
164 $param->set_state('install');
169 if ( $param->check_all_parameters() )
171 $param->set_state('install');
180 switch ( $param->state )
183 show_select_locale_form();
186 show_database_setting_form($isPostback);
189 show_blog_setting_form($isPostback);
192 show_detail_setting_form($isPostback);
195 show_install_complete_form();
202 * header tag of the installation screens
204 function show_header()
208 /* HTTP 1.1 application for no caching */
209 header("Cache-Control: no-cache, must-revalidate");
210 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
211 header('Content-Type: text/html; charset=' . i18n::get_current_charset());
214 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
215 <html xmlns="http://www.w3.org/1999/xhtml">
217 <title><?php echo _TITLE; ?></title>
218 <link rel="stylesheet" type="text/css" href="./styles/inst.css" />
219 <style type="text/css">
221 <?php echo _BODYFONTSTYLE; ?>
227 <div id="navigation">
228 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
231 if ( in_array($param->state, array('mysql', 'weblog', 'install')) )
233 echo '<li>', _STEP1, '</li><li';
234 if ( $param->state == 'mysql' )
238 echo '> > ', _STEP2, '</li><li';
239 if ( in_array($param->state, array('mysql', 'weblog')) )
243 echo '> > ', _STEP3, "</li>\n";
245 if ( in_array($param->state, array('mysql', 'weblog', 'detail')) )
247 echo '<li class="rightbox">';
248 if ( in_array($param->state, array('mysql', 'weblog')) )
250 echo '<a href="./?mode=detail">', _MODE2, '</a>';
254 echo '<a href="./?mode=simple">', _MODE1, '</a>';
266 * footer tag of the installation screens
268 function show_footer()
270 global $page_footer_copyright;
273 <?php echo $page_footer_copyright; ?>
281 * Display the form for language select
283 function show_select_locale_form()
285 // Get the browser language that can be displayed
286 // TODO: default locale select simple implementation
287 $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
288 foreach ($languages as $language)
290 $language = preg_replace('#([\w]+).*#', '$1', $language);
295 array('en_Latn_US', 'English - United States'),
296 array('ja_Jpan_JP', 'Japanese - Japan')
300 <p style="font-size:152%;font-weight:bold;">
303 <form method="post" action="./index.php">
306 <select name="locale">
308 foreach ( $locales as $locale )
310 echo "<option value=\"$locale[0]\"";
311 if ( i18n::strpos($locale[0], $language) === 0 )
315 echo ">$locale[1]</option>\n";
320 <button type="submit" name="action" value="locale" class="sbt_arw">START</button>
329 * Display the form to set up a database
330 * @param bool $isPostback
332 function show_database_setting_form($isPostback)
334 global $param, $minimum_mysql_version;
336 $config_writable = canConfigFileWritable();
337 $mysql_version = getMySqlVersion();
343 if ( $config_writable != '' )
345 echo '<span class="err">', $config_writable, '</span>';
347 if ( $mysql_version == '0.0.0' )
349 echo '<span class="err">', _ERROR21, '</span>';
351 elseif ( $mysql_version < $minimum_mysql_version )
353 echo '<span class="err">', sprintf(_ERROR20 , $minimum_mysql_version), '</span>';
357 <form method="post" action="./index.php">
359 <h2><?php echo _DB_HEADER; ?></h2>
364 $errors = $param->check_mysql_parameters();
365 if ( is_array($errors) )
367 foreach ( $errors as $error )
369 echo '<span class="err">', $error, "</span>\n";
377 <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
378 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
381 <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
382 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
385 <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
386 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
389 <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
390 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
394 <button type="submit" name="mode" value="detail" class="sbt_sqr"><?php echo _MODE2; ?></button>
395 <button type="submit" name="action" value="mysql" class="sbt_arw"><?php echo _NEXT; ?></button>
398 <?php echo _DB_TEXT1; ?>
407 * Displays a form to the blog settings
408 * @param bool $isPostback
410 function show_blog_setting_form($isPostback)
417 <?php echo _SIMPLE_NAVI2; ?>
419 <form method="post" action="./index.php">
421 <h2><?php echo _BLOG_HEADER; ?></h2>
426 $errors = $param->check_weblog_parameters();
427 if ( is_array($errors) )
429 foreach ( $errors as $error )
431 echo '<span class="err">', $error, "</span>\n";
439 <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
440 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
443 <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
444 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
450 <h2><?php echo _ADMIN_HEADER; ?></h2>
455 $errors = $param->check_user_parameters();
456 if ( is_array($errors) )
458 foreach ( $errors as $error )
460 echo '<span class="err">', $error, "</span>\n";
468 <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
469 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
472 <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
473 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
476 <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
477 <td><input type="password" name="user_password" /></td>
480 <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
481 <td><input type="password" name="user_password2" /></td>
484 <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
485 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
489 <button type="submit" name="action" value="weblog" class="sbt_arw"><?php echo _INSTALL; ?></button>
498 * Displays a form to the detail settings
499 * @param bool $isPostback
501 function show_detail_setting_form($isPostback)
503 global $param, $minimum_mysql_version;
505 $mysql_version = getMySqlVersion();
507 <div id="container_detailed">
509 <?php echo _DETAIL_NAVI1; ?>
511 if ( $isPostback && !$param->check_all_parameters() )
513 echo '<span class="err">', _ERROR26, "</span>\n";
518 <li>PHP: <?php echo phpversion(); ?></li>
521 echo ($mysql_version == '0.0.0') ? _ERROR21 : $mysql_version;
522 if ( $mysql_version < $minimum_mysql_version )
524 echo '<span class="err">', sprintf(_ERROR20 , $minimum_mysql_version), '</span>';
528 <form method="post" action="">
531 <h2><?php echo _DETAIL_HEADER1; ?></h2>
536 $errors = $param->check_mysql_parameters();
537 if ( is_array($errors) )
539 foreach ( $errors as $error )
541 echo '<span class="err">', $error, "</span>\n";
549 <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
550 <td><input type="text" name="mysql_host" value="<?php echo $param->mysql_host; ?>" /></td>
553 <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
554 <td><input type="text" name="mysql_user" value="<?php echo $param->mysql_user; ?>" /></td>
557 <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
558 <td><input type="text" name="mysql_password" value="<?php echo $param->mysql_password; ?>" /></td>
561 <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
562 <td><input type="text" name="mysql_database" value="<?php echo $param->mysql_database; ?>" /></td>
565 <th><span class="nam"><?php echo _DB_FIELD5; ?></span><span class="sub"><?php echo _DB_FIELD5_DESC; ?></span></th>
566 <td><input type="text" name="mysql_tablePrefix" value="<?php echo $param->mysql_tablePrefix; ?>" /></td>
570 <h2><?php echo _DETAIL_HEADER2; ?></h2>
575 $errors = $param->check_uri_parameters();
576 if ( is_array($errors) )
578 foreach ( $errors as $error )
580 echo '<span class="err">', $error, "</span>\n";
583 $errors = $param->check_path_parameters();
584 if ( is_array($errors) )
586 foreach ( $errors as $error )
588 echo '<span class="err">', $error, "</span>\n";
596 <th><span class="nam"><?php echo _PATH_FIELD1; ?></span></th>
597 <td><input type="text" name="IndexURL" value="<?php echo $param->IndexURL; ?>" /></td>
600 <th><span class="nam"><?php echo _PATH_FIELD2; ?></span></th>
601 <td><input type="text" name="AdminURL" value="<?php echo $param->AdminURL; ?>" /></td>
604 <th><span class="nam"><?php echo _PATH_FIELD3; ?></span></th>
605 <td><input type="text" name="AdminPath" value="<?php echo $param->AdminPath; ?>" /></td>
608 <th><span class="nam"><?php echo _PATH_FIELD4; ?></span></th>
609 <td><input type="text" name="MediaURL" value="<?php echo $param->MediaURL; ?>" /></td>
612 <th><span class="nam"><?php echo _PATH_FIELD5; ?></span></th>
613 <td><input type="text" name="MediaPath" value="<?php echo $param->MediaPath; ?>" /></td>
616 <th><span class="nam"><?php echo _PATH_FIELD6; ?></span></th>
617 <td><input type="text" name="SkinsURL" value="<?php echo $param->SkinsURL; ?>" /></td>
620 <th><span class="nam"><?php echo _PATH_FIELD7; ?></span></th>
621 <td><input type="text" name="SkinsPath" value="<?php echo $param->SkinsPath; ?>" /></td>
624 <th><span class="nam"><?php echo _PATH_FIELD8; ?></span></th>
625 <td><input type="text" name="PluginURL" value="<?php echo $param->PluginURL; ?>" /></td>
628 <th><span class="nam"><?php echo _PATH_FIELD9; ?></span></th>
629 <td><input type="text" name="ActionURL" value="<?php echo $param->ActionURL; ?>" /></td>
633 <?php echo _DETAIL_TEXT3; ?>
636 <h2><?php echo _DETAIL_HEADER3; ?></h2>
642 $errors = $param->check_user_parameters();
643 if ( is_array($errors) )
645 foreach ( $errors as $error )
647 echo '<span class="err">', $error, "</span>\n";
655 <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
656 <td><input type="text" name="user_realname" value="<?php echo $param->user_realname; ?>" /></td>
659 <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
660 <td><input type="text" name="user_name" value="<?php echo $param->user_name; ?>" /></td>
663 <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
664 <td><input type="password" name="user_password" /></td>
667 <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
668 <td><input type="password" name="user_password2" /></td>
671 <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
672 <td><input type="text" name="user_email" value="<?php echo $param->user_email; ?>" /></td>
676 <h2><?php echo _DETAIL_HEADER4; ?></h2>
682 $errors = $param->check_weblog_parameters();
683 if ( is_array($errors) )
685 foreach ( $errors as $error )
687 echo '<span class="err">', $error, "</span>\n";
695 <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
696 <td><input type="text" name="blog_name" value="<?php echo $param->blog_name; ?>" /></td>
699 <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
700 <td><input type="text" name="blog_shortname" value="<?php echo $param->blog_shortname; ?>" /></td>
705 <?php echo _DETAIL_TEXT6; ?>
709 <button type="submit" name="action" value="detail" class="sbt_arw"><?php echo _INSTALL; ?></button>
718 * Displays a screen to signal the completion of the installation
720 function show_install_complete_form()
722 global $MYSQL_HANDLER, $param;
723 $errors = do_install();
728 if ( is_array($errors) && count($errors) > 0 )
731 foreach ( $errors as $error )
733 echo '<span class="err">', $error, "</span>\n";
739 if ( array_key_exists('config_data', $_SESSION) )
741 echo '<span class="err">', _INST_TEXT4, '</span>';
743 <pre><code><?php echo htmlentities($_SESSION['config_data'], null, i18n::get_current_charset() ) ?></code></pre>
748 echo '<span class="err">', _INST_TEXT5, '</span>';
752 <form method="post" action="./index.php">
754 <h2><?php echo _INST_HEADER1; ?></h2>
756 <?php echo sprintf(_INST_TEXT1, $param->blog_name); ?>
759 <button type="button" name="toBlog" onclick="location.href='<?php echo $param->IndexURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON1; ?></button>
764 <h2><?php echo _INST_HEADER2; ?></h2>
766 <?php echo _INST_TEXT2; ?>
769 <button type="button" name="toMng" onclick="location.href='<?php echo $param->AdminURL; ?>';" class="sbt_arw"><?php echo _INST_BUTTON2; ?></button>
774 <h2><?php echo _INST_HEADER3; ?></h2>
776 <?php echo _INST_TEXT3; ?>
779 <button type="button" name="toAddBlog" onclick="location.href='<?php echo $param->AdminURL; ?>index.php?action=createnewlog';" class="sbt_arw"><?php echo _INST_BUTTON3; ?></button>
792 * The installation process itself
793 * @return array error messages
795 function do_install()
798 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_PREFIX, $MYSQL_CONN;
799 global $DIR_NUCLEUS, $DIR_MEDIA, $DIR_SKINS, $DIR_PLUGINS, $DIR_LANG, $DIR_LIBS;
803 * 1. put all param-vars into vars
805 $MYSQL_HOST = $param->mysql_host;
806 $MYSQL_USER = $param->mysql_user;
807 $MYSQL_PASSWORD = $param->mysql_password;
808 $MYSQL_DATABASE = $param->mysql_database;
809 $MYSQL_PREFIX = $param->mysql_tablePrefix;
811 $DIR_NUCLEUS = $param->AdminPath;
812 $DIR_MEDIA = $param->MediaPath;
813 $DIR_SKINS = $param->SkinsPath;
814 $DIR_PLUGINS = $DIR_NUCLEUS . 'plugins/';
815 $DIR_LOCALES = $DIR_NUCLEUS . 'locales/';
816 $DIR_LIBS = $DIR_NUCLEUS . 'libs/';
819 * 2.open mySQL connection
821 $MYSQL_CONN = @sql_connect_args($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD);
822 if ( $MYSQL_CONN == false )
829 * 3. try to create database if needed
831 if ( !sql_query('CREATE DATABASE IF NOT EXISTS ' . $MYSQL_DATABASE) )
833 $errors[] = _ERROR12 . ': ' . sql_error();
837 * 4. try to select database
839 if ( !sql_select_db($MYSQL_DATABASE) )
841 $errors[] = _ERROR13;
843 sql_set_charset('utf8');
845 if ( count($errors) > 0 )
853 $table_names = array(
867 'nucleus_activation',
871 $prefixed_table_names = array();
872 foreach ( $table_names as $table_name )
874 $prefixed_table_names[] = $MYSQL_PREFIX . $table_name;
877 // table exists check
878 $result = sql_query('SHOW TABLES');
879 while ($row = mysql_fetch_array($result, MYSQL_NUM))
881 if ( in_array($row[0], $prefixed_table_names) )
883 $errors[] = _ERROR14;
887 if ( count($errors) > 0 )
892 $filename = 'install.sql';
893 $fd = fopen($filename, 'r');
894 $queries = fread($fd, filesize($filename) );
897 $queries = preg_split('#(;\n|;\r)#', $queries);
899 foreach ( $queries as $query )
901 if ( preg_match('/\w+/', $query) )
905 $query = str_replace($table_names, $prefixed_table_names, $query);
908 if ( !sql_query($query) )
910 $errors[] = _ERROR15 . ' (<small>' . $query . '</small>): ' . sql_error();
916 * 6. put needed records
918 /* push first post */
919 $query = "INSERT INTO %s VALUES (1, '%s', '%s', '%s', 1, 1, '2005-08-15 11:04:26', 0, 0, 0, 1, 0, 1)";
920 $query = sprintf($query, tableName('nucleus_item'), _1ST_POST_TITLE, _1ST_POST, _1ST_POST2);
921 if ( !sql_query($query) )
923 $errors[] = _ERROR15 . ' (<small>' . $newpost . '</small>): ' . sql_error();
926 /* update datetime for this first post */
927 $query = "UPDATE %s SET itime = '%s' WHERE inumber = 1";
928 $query = sprintf($query, tableName('nucleus_item'), date('Y-m-d H:i:s',time()));
929 if ( !sql_query($query) )
931 $errors[] = _ERROR18 . ': ' . sql_error();
934 /* push configurations */
935 array_merge($errors, updateConfig('IndexURL', $param->IndexURL));
936 array_merge($errors, updateConfig('AdminURL', $param->AdminURL));
937 array_merge($errors, updateConfig('MediaURL', $param->MediaURL));
938 array_merge($errors, updateConfig('SkinsURL', $param->SkinsURL));
939 array_merge($errors, updateConfig('PluginURL', $param->PluginURL));
940 array_merge($errors, updateConfig('ActionURL', $param->ActionURL));
941 array_merge($errors, updateConfig('AdminEmail', $param->user_email));
942 array_merge($errors, updateConfig('SiteName', $param->blog_name));
943 array_merge($errors, updateConfig('Locale', i18n::get_current_locale()));
945 /* escape strings for SQL */
946 $user_name = sql_real_escape_string($param->user_name);
947 $user_realname = sql_real_escape_string($param->user_realname);
948 $user_password = sql_real_escape_string(md5($param->user_password));
949 $user_email = sql_real_escape_string($param->user_email);
950 $blog_name = sql_real_escape_string($param->blog_name);
951 $blog_shortname = sql_real_escape_string($param->blog_shortname);
952 $config_indexurl = sql_real_escape_string($param->IndexURL);
954 /* push super admin */
955 $query = "UPDATE %s SET mname = '%s', mrealname = '%s', mpassword = '%s', memail = '%s', murl = '%s', madmin = 1, mcanlogin = 1 WHERE mnumber = 1";
956 $query = sprintf($query, tableName('nucleus_member'), $user_name, $user_realname, $user_password, $user_email, $config_indexurl);
957 if ( !sql_query($query) )
959 $errors[] = _ERROR16 . ': ' . sql_error();
962 /* push new weblog */
963 $query = "UPDATE %s SET bname = '%s', bshortname = '%s', burl = '%s' WHERE bnumber = 1";
964 $query = sprintf($query, tableName('nucleus_blog'), $blog_name, $blog_shortname, $config_indexurl);
965 if ( !sql_query($query) )
967 $errors[] = _ERROR17 . ': ' . sql_error();
970 /* push default category */
971 $query = "UPDATE %s SET cname = '%s', cdesc = '%s' WHERE catid = 1";
972 $query = sprintf($query, tableName('nucleus_category'), _GENERALCAT_NAME, _GENERALCAT_DESC);
973 if ( !sql_query($query) )
975 $errors[] = _ERROR17 . ': ' . sql_error();
981 * 7. install default plugins and skins
983 global $aConfPlugsToInstall, $aConfSkinsToImport;
984 $aSkinErrors = array();
985 $aPlugErrors = array();
987 if ( (count($aConfPlugsToInstall) > 0) || (count($aConfSkinsToImport) > 0) )
989 include_once($DIR_LIBS . 'globalfunctions.php');
991 if ( !isset($manager) )
993 $manager = new MANAGER;
996 $aSkinErrors = installCustomSkins();
997 if ( count($aSkinErrors) > 0 )
999 array_merge($errors, $aSkinErrors);
1002 $query = "SELECT sdnumber FROM %s WHERE sdname='default'";
1003 $query = sprintf($query, tableName('nucleus_skin_desc'));
1004 $res = sql_query($query);
1005 $obj = sql_fetch_assoc($res);
1006 $defSkinID = (integer) $obj['sdnumber'];
1008 $query = "UPDATE %s SET bdefskin=%d WHERE bnumber=1";
1009 $query = sprintf($query, tableName('nucleus_blog'), $defSkinID);
1011 $query = "UPDATE %s SET value=%d WHERE name='BaseSkin'";
1012 $query = sprintf($query, tableName('nucleus_config'), $defSkinID);
1015 $aPlugErrors = installCustomPlugs($manager);
1016 if ( count($aPlugErrors) > 0 )
1018 array_merge($errors, $aPlugErrors);
1023 * 8. Write config file ourselves (if possible)
1025 $config_data = '<' . '?php' . "\n\n";
1026 $config_data .= "// mySQL connection information\n";
1027 $config_data .= "\$MYSQL_HOST = '" . $MYSQL_HOST . "';\n";
1028 $config_data .= "\$MYSQL_USER = '" . $MYSQL_USER . "';\n";
1029 $config_data .= "\$MYSQL_PASSWORD = '" . $MYSQL_PASSWORD . "';\n";
1030 $config_data .= "\$MYSQL_DATABASE = '" . $MYSQL_DATABASE . "';\n";
1031 $config_data .= "\$MYSQL_PREFIX = '" . $MYSQL_PREFIX . "';\n";
1032 $config_data .= "// new in 3.50. first element is db handler, the second is the db driver used by the handler\n";
1033 $config_data .= "// default is \$MYSQL_HANDLER = array('mysql','mysql');\n";
1034 $config_data .= "//\$MYSQL_HANDLER = array('mysql','mysql');\n";
1035 $config_data .= "//\$MYSQL_HANDLER = array('pdo','mysql');\n";
1036 $config_data .= "\$MYSQL_HANDLER = array('".$MYSQL_HANDLER[0]."','".$MYSQL_HANDLER[1]."');\n";
1037 $config_data .= "\n";
1038 $config_data .= "// main nucleus directory\n";
1039 $config_data .= "\$DIR_NUCLEUS = '" . $DIR_NUCLEUS . "';\n";
1040 $config_data .= "\n";
1041 $config_data .= "// path to media dir\n";
1042 $config_data .= "\$DIR_MEDIA = '" . $DIR_MEDIA . "';\n";
1043 $config_data .= "\n";
1044 $config_data .= "// extra skin files for imported skins\n";
1045 $config_data .= "\$DIR_SKINS = '" . $DIR_SKINS . "';\n";
1046 $config_data .= "\n";
1047 $config_data .= "// these dirs are normally sub dirs of the nucleus dir, but \n";
1048 $config_data .= "// you can redefine them if you wish\n";
1049 $config_data .= "\$DIR_PLUGINS = \$DIR_NUCLEUS . 'plugins/';\n";
1050 $config_data .= "\$DIR_LOCALES = \$DIR_NUCLEUS . 'locales/';\n";
1051 $config_data .= "\$DIR_LIBS = \$DIR_NUCLEUS . 'libs/';\n";
1052 $config_data .= "\n";
1053 $config_data .= "// include libs\n";
1054 $config_data .= "include(\$DIR_LIBS.'globalfunctions.php');\n";
1055 $config_data .= "?" . ">";
1058 if ( @!file_exists('../config.php') || is_writable('../config.php') )
1060 if ( $fp = @fopen('../config.php', 'w') )
1062 $result = @fwrite($fp, $config_data, i18n::strlen($config_data) );
1069 // try to change the read-only permission.
1070 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1072 @chmod('../config.php', 0444);
1077 $_SESSION['config_data'] = $config_data;
1084 * Confirm that you can write to the configuration file
1085 * @return string error message
1087 function canConfigFileWritable()
1089 if ( @file_exists('../config.php') && @!is_writable('../config.php') )
1091 // try to change the read-write permission.
1092 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1094 @chmod('../config.php', 0666);
1097 if ( @!is_writable('../config.php') )
1106 * To obtain the version of MySQL
1109 function getMySqlVersion()
1111 global $minimum_mysql_version, $errors;
1112 // Turn on output buffer
1113 // Needed to repress the output of the sql function that are
1114 // not part of php (in this case the @ operator doesn't work)
1117 // note: this piece of code is taken from phpMyAdmin
1118 $conn = sql_connect_args('localhost', '', '');
1119 $result = @sql_query('SELECT VERSION() AS version', $conn);
1121 if ( $result != FALSE && sql_num_rows($result) > 0 )
1123 $row = sql_fetch_array($result);
1124 $match = i18n::explode('.', $row['version']);
1128 $result = @sql_query('SHOW VARIABLES LIKE \'version\'', $conn);
1130 if ( $result != FALSE && @sql_num_rows($result) > 0 )
1132 $row = sql_fetch_row($result);
1133 $match = i18n::explode('.', $row[1]);
1137 //$output = shell_exec('mysql -V');
1138 $output = ( function_exists('shell_exec') ) ? @shell_exec('mysql -V') : '0.0.0';
1139 preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
1140 $match = i18n::explode('.', $version[0]);
1142 if ( $match[0] == '' )
1144 $match = array('0', '0', '0');
1149 @sql_disconnect($conn);
1151 //End and clean output buffer
1154 return implode($match, '.');
1158 * Add a table prefix if it is used
1160 * @param string $input table name with prefix
1163 function tableName($input)
1165 global $MYSQL_PREFIX;
1166 if ( $MYSQL_PREFIX )
1168 return $MYSQL_PREFIX . $input;
1177 * Install custom plugins
1179 * @param object $manager MANAGER class instance
1181 function installCustomPlugs($manager)
1183 global $aConfPlugsToInstall, $DIR_LIBS;
1186 if ( count($aConfPlugsToInstall) == 0 )
1191 $res = sql_query('SELECT * FROM ' . tableName('nucleus_plugin') );
1192 $numCurrent = sql_num_rows($res);
1194 foreach ( $aConfPlugsToInstall as $plugName )
1196 $query = 'INSERT INTO ' . tableName('nucleus_plugin') . ' (porder, pfile) VALUES (' . (++$numCurrent) . ", '" . sql_real_escape_string($plugName) . "')";
1199 $manager->clearCachedInfo('installedPlugins');
1200 $plugin =& $manager->getPlugin($plugName);
1201 $plugin->setID($numCurrent);
1205 sql_query('DELETE FROM ' . tableName('nucleus_plugin') . " WHERE pfile = '" . sql_real_escape_string($plugName) . "'");
1207 array_push($aErrors, sprintf(_ERROR22 ,$plugName));
1213 sql_query('DELETE FROM ' . tableName('nucleus_plugin_event') );
1214 $res = sql_query('SELECT pid, pfile FROM ' . tableName('nucleus_plugin') );
1216 while ( $o = sql_fetch_object($res) )
1219 $plug =& $manager->getPlugin($o->pfile);
1223 $eventList = $plug->getEventList();
1224 foreach ( $eventList as $eventName )
1226 sql_query('INSERT INTO ' . tableName('nucleus_plugin_event') . ' (pid, event) VALUES (' . $pid . ", '" . $eventName . "')");
1234 * Install custom skins
1235 * Prepares the installation of custom skins
1237 function installCustomSkins()
1239 global $aConfSkinsToImport, $DIR_LIBS, $DIR_SKINS;
1242 if ( count($aConfSkinsToImport) == 0 )
1247 include_once($DIR_LIBS . 'skinie.php');
1248 $importer = new SKINIMPORT();
1250 foreach ( $aConfSkinsToImport as $skinName )
1253 $skinFile = $DIR_SKINS . $skinName . '/skinbackup.xml';
1255 if ( !@file_exists($skinFile) )
1257 array_push($aErrors, sprintf(_ERROR23, $skinFile));
1261 $error = $importer->readFile($skinFile);
1265 array_push($aErrors, sprintf(_ERROR24, $skinName) . ' : ' . $error);
1269 $error = $importer->writeToDatabase(1);
1273 array_push($aErrors, sprintf(_ERROR25, $skinName) . ' : ' . $error);
1282 * Check if some important files of the Nucleus CMS installation are available
1283 * Give an error if one or more files are not accessible
1285 function do_check_files()
1287 $missingfiles = array();
1292 '../nucleus/index.php',
1293 '../nucleus/media.php',
1294 '../nucleus/libs/ACTION.php',
1295 '../nucleus/libs/ACTIONLOG.php',
1296 '../nucleus/libs/ACTIONS.php',
1297 '../nucleus/libs/ADMIN.php',
1298 '../nucleus/libs/BaseActions.php',
1299 '../nucleus/libs/BLOG.php',
1300 '../nucleus/libs/BODYACTIONS.php',
1301 '../nucleus/libs/COMMENT.php',
1302 '../nucleus/libs/COMMENTACTIONS.php',
1303 '../nucleus/libs/COMMENTS.php',
1304 '../nucleus/libs/ENCAPSULATE.php',
1305 '../nucleus/libs/ENTITY.php',
1306 '../nucleus/libs/globalfunctions.php',
1307 '../nucleus/libs/i18n.php',
1308 '../nucleus/libs/ITEM.php',
1309 '../nucleus/libs/ITEMACTIONS.php',
1310 '../nucleus/libs/LINK.php',
1311 '../nucleus/libs/MANAGER.php',
1312 '../nucleus/libs/MEDIA.php',
1313 '../nucleus/libs/MEMBER.php',
1314 '../nucleus/libs/mysql.php',
1315 '../nucleus/libs/NOTIFICATION.php',
1316 '../nucleus/libs/PAGEFACTORY.php',
1317 '../nucleus/libs/PARSER.php',
1318 '../nucleus/libs/PLUGIN.php',
1319 '../nucleus/libs/PLUGINADMIN.php',
1320 '../nucleus/libs/SEARCH.php',
1321 '../nucleus/libs/showlist.php',
1322 '../nucleus/libs/SKIN.php',
1323 '../nucleus/libs/TEMPLATE.php',
1324 '../nucleus/libs/vars4.1.0.php',
1325 '../nucleus/libs/xmlrpc.inc.php',
1326 '../nucleus/libs/xmlrpcs.inc.php',
1327 '../nucleus/libs/sql/mysql.php'
1330 $count = count($files);
1331 for ( $i = 0; $i < $count; $i++ )
1333 if ( !is_readable($files[$i]) )
1335 array_push( $missingfiles, 'File <b>' . $files[$i] . '</b> is missing or not readable.<br />');
1339 if ( count($missingfiles) > 0 )
1341 exit(implode( "\n", $missingfiles));
1347 * Updates the configuration in the database
1349 * @param string $name name of the config var
1350 * @param string $value new value of the config var
1353 function updateConfig($name, $value)
1356 $name = sql_real_escape_string($name);
1357 $value = trim(sql_real_escape_string($value) );
1359 $query = "UPDATE %s SET value = '%s' WHERE name = '%s'";
1360 $query = sprintf($query, tableName('nucleus_config'), $value, $name);
1362 if ( !sql_query($query) )
1364 $errors[] = _ERROR15 . ': ' . sql_error();
1372 /* process parameter */
1376 /* mysql connection parameters */
1379 public $mysql_password;
1380 public $mysql_database;
1381 public $mysql_tablePrefix;
1383 /* weblog configuration parameters */
1385 public $blog_shortname;
1387 /* member configuration parameters */
1389 public $user_realname;
1390 public $user_password;
1391 private $user_password2;
1394 /* URI parameters */
1403 /* path parameters */
1412 public function __construct()
1417 public function init()
1419 // set default values
1420 $this->state = 'locale';
1421 $this->install_mode = 'simple';
1422 $this->locale = 'en_Latn_US';
1423 $this->mysql_host = @ini_get('mysql.default_host');
1424 $this->blog_name = 'My Nucleus CMS';
1425 $this->blog_shortname = 'mynucleuscms';
1428 $this->root_path = realpath(dirname(__FILE__) . '/..');
1429 if ( substr($this->root_path, -1, 1) !== '/' )
1431 $this->root_path .= '/';
1433 $base_path_pcre = preg_quote($this->root_path, '#');
1435 /* current directry name */
1436 $directory_name = preg_replace("#$base_path_pcre#", '', dirname(__FILE__));
1437 $directory_name_pcre = preg_quote($directory_name, '#');
1440 $root_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
1441 $this->root_url = preg_replace("#$directory_name_pcre(.*)$#", '', $root_url);
1443 $this->AdminPath = $this->root_path . 'nucleus' . DIRECTORY_SEPARATOR;
1444 $this->MediaPath = $this->root_path . 'media' . DIRECTORY_SEPARATOR;
1445 $this->SkinsPath = $this->root_path . 'skins' . DIRECTORY_SEPARATOR;
1447 $this->IndexURL = $this->root_url;
1448 $this->AdminURL = $this->root_url . 'nucleus/';
1449 $this->MediaURL = $this->root_url . 'media/';
1450 $this->SkinsURL = $this->root_url . 'skins/';
1451 $this->PluginURL = $this->root_url . 'nucleus/plugins/';
1452 $this->ActionURL = $this->root_url . 'action.php';
1455 private function read_parameter($parameter)
1457 foreach ( $parameter as $element )
1459 if ( array_key_exists($element, $_POST) )
1461 $this->$element = $_POST[$element];
1466 public function set_state($state)
1468 $states = array('locale', 'mysql', 'weblog', 'detail', 'install');
1469 if ( in_array($state, $states) )
1471 $this->state = $state;
1475 public function set_locale()
1477 $this->read_parameter(array('locale'));
1479 if ( !in_array($this->locale, i18n::get_available_locale_list()) )
1481 $this->locale = 'en_Latn_US';
1485 public function check_mysql_parameters()
1487 $parameters = array('mysql_host', 'mysql_user', 'mysql_password', 'mysql_database', 'mysql_tablePrefix');
1488 $this->read_parameter($parameters);
1491 if ( $this->mysql_host == '' )
1493 $errors[] = sprintf(_ERROR1, _DB_FIELD1);
1496 if ( $this->mysql_user == '' )
1498 $errors[] = sprintf(_ERROR1, _DB_FIELD2);
1501 if ( $this->mysql_password == '' )
1503 $errors[] = sprintf(_ERROR1, _DB_FIELD3);
1506 if ( $this->mysql_database == '' )
1508 $errors[] = sprintf(_ERROR1, _DB_FIELD4);
1511 if ( $this->mysql_database != ''
1512 && !preg_match('/^\w+$/', $this->mysql_database) )
1514 $errors[] = sprintf(_ERROR2, _DB_FIELD4);
1517 if ( $this->mysql_tablePrefix != ''
1518 && !preg_match('/^\w+$/', $this->mysql_tablePrefix) )
1520 $errors[] = sprintf(_ERROR2, _DB_FIELD5);
1523 if ( count($errors) == 0 )
1525 $mysql_conn = @sql_connect_args($this->mysql_host, $this->mysql_user, $this->mysql_password);
1526 if ( $mysql_conn == false )
1528 $errors[] = _ERROR3;
1532 @sql_close($mysql_conn);
1539 public function check_user_parameters()
1541 $parameters = array('user_name', 'user_realname', 'user_password', 'user_password2', 'user_email');
1542 $this->read_parameter($parameters);
1545 if ( $this->user_name == '' )
1547 $errors[] = sprintf(_ERROR1, _ADMIN_FIELD2);
1549 elseif ( !preg_match("/^[[:alnum:]]+[ [:alnum:]]*[[:alnum:]]+$/i", $this->user_name) )
1551 $errors[] = _ERROR5;
1554 if ( $this->user_realname == '' )
1556 $errors[] = sprintf(_ERROR1, _ADMIN_FIELD1);
1559 if ( $this->user_password == '' || $this->user_password2 == '' )
1561 $errors[] = sprintf(_ERROR1, _ADMIN_FIELD3);
1562 $this->user_password = '';
1564 elseif ( $this->user_password != $this->user_password2 )
1566 $errors[] = _ERROR6;
1567 $this->user_password = '';
1570 if ( !preg_match("/^[\w\.-]+@[\w\.-]+\.[[:alpha:]]{2,6}$/", $this->user_email) )
1572 $errors[] = _ERROR7;
1578 public function check_weblog_parameters()
1580 $parameters = array('blog_name', 'blog_shortname');
1581 $this->read_parameter($parameters);
1584 if ( $this->blog_name == '' )
1586 $errors[] = sprintf(_ERROR1, _BLOG_FIELD1);
1589 if ( $this->blog_shortname == '' )
1591 $errors[] = sprintf(_ERROR1, _BLOG_FIELD2);
1594 if ( !preg_match("/^[a-z0-9]+$/", $this->blog_shortname) )
1596 $errors[] = _ERROR4;
1602 public function check_uri_parameters()
1604 $parameters = array('IndexURL', 'AdminURL', 'MediaURL', 'SkinsURL', 'PluginURL', 'ActionURL');
1605 $this->read_parameter($parameters);
1608 if ( substr($this->IndexURL, -1, 1) !== '/' )
1610 $errors[] = sprintf(_ERROR8, _PATH_FIELD1);
1613 if ( substr($this->AdminURL, -1, 1) !== '/' )
1615 $errors[] = sprintf(_ERROR8, _PATH_FIELD2);
1618 if ( substr($this->MediaURL, -1, 1) !== '/' )
1620 $errors[] = sprintf(_ERROR8, _PATH_FIELD4);
1623 if ( substr($this->SkinsURL, -1, 1) !== '/' )
1625 $errors[] = sprintf(_ERROR8, _PATH_FIELD6);
1628 if ( substr($this->PluginURL, -1, 1) !== '/' )
1630 $errors[] = sprintf(_ERROR8, _PATH_FIELD8);
1633 if ( strrchr($this->ActionURL, '/') != '/action.php' )
1635 $errors[] = sprintf(_ERROR9, _PATH_FIELD9);
1641 public function check_path_parameters()
1643 $parameters = array('AdminPath', 'MediaPath', 'SkinsPath');
1644 $this->read_parameter($parameters);
1646 $separators = array('/', DIRECTORY_SEPARATOR);
1648 if ( !in_array(substr($this->AdminPath, -1, 1), $separators) || !file_exists($this->AdminPath) )
1650 $errors[] = sprintf(_ERROR10, _PATH_FIELD3);
1653 if ( !in_array(substr($this->MediaPath, -1, 1), $separators) || !file_exists($this->MediaPath) )
1655 $errors[] = sprintf(_ERROR10, _PATH_FIELD5);
1658 if ( !in_array(substr($this->SkinsPath, -1, 1), $separators) || !file_exists($this->SkinsPath) )
1660 $errors[] = sprintf(_ERROR10, _PATH_FIELD7);
1667 * check all parameters
1670 public function check_all_parameters()
1672 $this->set_locale();
1675 $isValid &= (count($this->check_mysql_parameters()) == 0);
1676 $isValid &= (count($this->check_user_parameters()) == 0);
1677 $isValid &= (count($this->check_weblog_parameters()) == 0);
1678 $isValid &= (count($this->check_uri_parameters()) == 0);
1679 $isValid &= (count($this->check_path_parameters()) == 0);