OSDN Git Service

MERGE: リビジョン1672から1680にかけて行われた修正のうち、反映されてないものを追加。
[nucleus-jp/nucleus-next.git] / install / index.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2007 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  *
12  * This script will install the Nucleus tables in your SQL-database,
13  * and initialize the data in those tables.
14  *
15  * Below is a friendly way of letting users on non-php systems know that Nucleus won't run there.
16  * ?><div style="font-size: xx-large;"> Your web server is not properly configured to run PHP scripts and will not be able to install Nucleus. </div> <div style="display: none;"><?php
17  */
18
19
20 /**
21  * @license http://nucleuscms.org/license.txt GNU General Public License
22  * @copyright Copyright (C) 2002-2007 The Nucleus Group
23  * @version $Id$
24  */
25
26 // don't give warnings for uninitialized vars
27 error_reporting(E_ERROR | E_WARNING | E_PARSE);
28
29
30 $minimum_php_version = '5.0.6';
31 $minimum_mysql_version = '3.23';
32
33 // begin if: server's PHP version is below the minimum; halt installation
34 if ( version_compare(PHP_VERSION, $minimum_php_version, '<') )
35 {
36         exit('<div style="font-size: xx-large;"> Nucleus requires at least PHP version '. $minimum_php_version .'</div>');
37 } // end if
38
39 // make sure there's no unnecessary escaping: # set_magic_quotes_runtime(0);
40 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
41 {
42     ini_set('magic_quotes_runtime', '0');
43 } // end if
44
45
46 /**
47  * This part of the ./install/index.php code allows for customization of the install process.
48  * When distributing plugins or skins together with a Nucleus installation, the
49  * configuration below will instruct to install them
50  *
51  * -- Start Of Configurable Part --
52  **/
53
54 /**
55  * array with names of plugins to install. Plugin files must be present in the nucleus/plugin/ directory
56  *
57  * example:
58  *              array('NP_TrackBack', 'NP_MemberGoodies')
59  **/
60 $aConfPlugsToInstall = array('NP_SkinFiles', 'NP_SecurityEnforcer', 'NP_Text');
61
62 /**
63  * array with skins to install. skins must be present under the skins/ directory with
64  * a subdirectory having the same name that contains a skinbackup.xml file
65  *
66  * example:
67  *              array('base', 'rsd')
68  **/
69 $aConfSkinsToImport = array('atom', 'rss2.0', 'rsd', 'default');
70
71 /**
72  * -- End Of Configurable Part --
73  **/
74
75
76 // Check if some important files
77 doCheckFiles();
78
79 // if there are some plugins or skins to import, do not include vars in globalfunctions.php again... so set a flag
80 if ( (count($aConfPlugsToInstall) > 0) || (count($aConfSkinsToImport) > 0) )
81 {
82         global $CONF;
83         $CONF['installscript'] = 1;
84 } // end if
85
86 // we will use postVar, getVar, ... methods instead of $_GET, $_POST ...
87 include_once('../nucleus/libs/vars4.1.0.php');
88
89 // include core classes that are needed for internationalization
90 include_once('../nucleus/libs/i18n.php');
91
92 if ( !i18n::init('UTF-8', './locales') )
93 {
94         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>');
95 } // end if
96
97 // check if mysql support is installed; this check may not make sense, as is, in a version past 3.5x
98 if ( !function_exists('mysql_query') && !function_exists('mysqli_query') )
99 {
100         exit('<div style="font-size: xx-large;"> Your PHP version does not have support for MySQL :( </div>');
101 } // end if
102
103 // include core classes that are needed for login & plugin handling
104 include_once('../nucleus/libs/mysql.php');
105
106 // added for 3.5 sql_* wrapper
107 global $MYSQL_HANDLER;
108
109 //set the handler if different from mysql (or mysqli) # $MYSQL_HANDLER = array('pdo','mysql');
110 if ( !isset($MYSQL_HANDLER) )
111 {
112         $MYSQL_HANDLER = array('mysql', '');
113 } // end if
114
115 include_once('../nucleus/libs/sql/' . $MYSQL_HANDLER[0] . '.php');
116 // end new for 3.5 sql_* wrapper
117
118         
119 doAction();
120
121 exit;
122
123 /**
124  * header tag of the installation screens
125  **/
126 function showHeader()
127 {
128         /* send HTTP header */
129         header('Content-Type: text/html; charset=' . i18n::get_current_charset());
130
131 ?>
132 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
133 <html xmlns="http://www.w3.org/1999/xhtml">
134         <head>
135                 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo i18n::get_current_charset(); ?>" />
136                 <title><?php echo _TITLE; ?></title>
137                 <link rel="stylesheet" type="text/css" href="./styles/inst.css" />
138                 <style type="text/css">
139                 <!--
140                 <?php echo _BODYFONTSTYLE; ?>
141                 -->
142                 </style>
143         </head>
144         <body>
145 <?php
146 }
147
148 /**
149  * footer tag of the installation screens
150  **/
151 function showFooter()
152 {
153 ?>
154                 <div id="footer">
155                         &copy; 2001-2012 The Nucleus Groupe . Running Nucleus CMS v4.00
156                 </div>
157         </body>
158 </html>
159 <?php
160 }
161
162
163 /**
164  * installer action
165  */
166 function doAction()
167 {
168         global $errors;
169         $errors = array();
170         
171         session_start();
172
173         if ( postVar('locale') )
174         {
175                 $_SESSION['locale'] = postVar('locale');
176         }
177         
178         /* TODO: if something input related to locale, set it, else set default */
179         i18n::set_current_locale($_SESSION['locale'] ? $_SESSION['locale'] : 'en_Latn_US');
180         $translation_file = './locales/' . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php';
181         if ( !file_exists($translation_file) )
182         {
183                 $translation_file = './locales/en_Latn_US.UTF-8.php';
184         }
185         include($translation_file);
186
187         
188
189         showHeader();
190
191         if ( !postVar('action') && !requestVar('mode') )
192         {
193                 // Initialize the session
194                 $_SESSION = array();
195                 $_SESSION['mode'] = 'simple';
196                 $_SESSION['mysql_host'] = _hsc(@ini_get('mysql.default_host'));
197                 getURLAndPath();
198                 showSelectLocaleForm();
199         }
200         else
201         {
202                 // mode change
203                 if ( requestVar('mode') )
204                 {
205                         $_SESSION['mode'] = requestVar('mode');
206                 }
207
208                 $action = postVar('action');
209                 $isValid = doValidate();
210                 if ( $action == 'install' && $isValid && doInstall() ) // install process
211                 {
212                         showInstallCompleteForm();
213                         // Clearing the session
214                         $_SESSION = array();
215                 }
216                 elseif ( $_SESSION['mode'] == 'simple' ) // simple install mode
217                 {
218                         if ( !$action || ( $action == 'next' && count($errors['db_setting']) > 0 ) )
219                         {
220                                 if ( !$action )
221                                 {
222                                         $errors = array();
223                                 }
224                                 showDBSettingForm();
225                         }
226                         else
227                         {
228                                 if ( $action == 'next' )
229                                 {
230                                         $errors = array();
231                                 }
232                                 showBlogSettingForm();
233                         }
234                 }
235                 else // advanced install mode
236                 {
237                         if ( !$action )
238                         {
239                                 $errors = array();
240                         }
241                         showDetailSettingForm();
242                 }
243         }
244         
245         showFooter();
246 }
247
248 /**
249  * Display the form for language select
250  */
251 function showSelectLocaleForm()
252 {
253         // Get the browser language that can be displayed
254         // TODO: not used
255         $languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
256         foreach ($languages as $language)
257         {
258                 if (strpos($language, ';') === FALSE)
259                 {
260                         break;
261                 }
262                 else
263                 {
264                         $language = preg_replace('#([^;]+).*#', '$1', $language);
265                         break;
266                 }
267         }
268
269 ?>
270                 <div id="header">
271                         <div id="navigation">
272                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
273                         </div>
274                 </div>
275                 <div id="container">
276                         <p style="font-size:152%;font-weight:bold;">
277                                 Select your locale:
278                         </p>
279                         <form method="post" action="./index.php">
280                         
281                                 <div class="prt">
282                                         <select name="locale">
283                                                 <option value="en_Latn_US">English - United States</option>
284                                                 <option value="ja_Jpan_JP">Japanese - Japan</option>
285                                         </select>
286                                         <p class="sbt">
287                                                 <button type="submit" name="mode" value="simple" class="sbt_arw">START</button>
288                                         </p>
289                                 </div>
290                         </form>
291                 </div>
292 <?php
293 }
294
295 /**
296  * Display the form to set up a database
297  */
298 function showDBSettingForm()
299 {
300         global $errors;
301         getMySqlVersion(); // error is stored in $errors['mysql'].
302 ?>
303                 <div id="header">
304                         <div id="navigation">
305                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
306                                 <ul>
307                                         <li><?php echo _STEP1; ?></li>
308                                         <li class="gry">&nbsp; &gt; &nbsp;<?php echo _STEP2; ?></li>
309                                         <li class="gry">&nbsp; &gt; &nbsp;<?php echo _STEP3; ?></li>
310                                         <li class="rightbox">
311                                                 <a href="./?mode=details"><?php echo _MODE2; ?></a>
312                                         </li>
313                                 </ul>
314                         </div>
315                 </div>
316                 <div id="container">
317                         <p class="msg">
318                                 <?php echo _SIMPLE_NAVI1;
319                                 if ( !canConfigFileWritable() ) echo '<span class="err">', $errors['config'], '</span>';
320                                 if ( $errors['mysql'] ) echo '<span class="err">', $errors['mysql'], '</span>';
321                                 if ( is_array($errors['install']) )
322                                 {
323                                         foreach ( $errors['install'] as $error )
324                                         {
325                                                 echo '<span class="err">', $error, "</span>\n";
326                                         }
327                                 }
328                                 ?> 
329                         </p>
330                         <form method="post" action="./index.php">
331                                 <div class="prt">
332                                         <h2><?php echo _DB_HEADER; ?></h2>
333                                         <p class="msg">
334                                                 <?php
335                                                 if ( is_array($errors['db_setting']) )
336                                                 {
337                                                         foreach ( $errors['db_setting'] as $error )
338                                                         {
339                                                                 echo '<span class="err">', $error, "</span>\n";
340                                                         }
341                                                 }
342                                                 ?>
343                                         </p>
344                                         <table>
345                                                 <tr>
346                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
347                                                                 <td><input type="text" name="mysql_host" value="<?php echo $_SESSION['mysql_host']; ?>" /></td>
348                                                 </tr>
349                                                 <tr>
350                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
351                                                                 <td><input type="text" name="mysql_user" value="<?php echo $_SESSION['mysql_user']; ?>" /></td>
352                                                 </tr>
353                                                 <tr>
354                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
355                                                                 <td><input type="text" name="mysql_password" value="<?php echo $_SESSION['mysql_password']; ?>" /></td>
356                                                 </tr>
357                                                 <tr>
358                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
359                                                                 <td><input type="text" name="mysql_database" value="<?php echo $_SESSION['mysql_database']; ?>" /></td>
360                                                 </tr>
361                                         </table>
362                                         <p class="sbt">
363                                                 <button type="submit" name="mode" value="detail" class="sbt_sqr"><?php echo _MODE2; ?></button>
364                                                 <button type="submit" name="action" value="next" class="sbt_arw"><?php echo _NEXT; ?></button>
365                                         </p>
366                                         <p class="msg">
367                                                 <?php echo _DB_TEXT1; ?>
368                                         </p>
369                                 </div>
370                         </form>
371                 </div>
372 <?php
373 }
374
375 /**
376  * Displays a form to the blog settings
377  */
378 function showBlogSettingForm()
379 {
380         global $errors;
381 ?>
382                 <div id="header">
383                         <div id="navigation">
384                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
385                                 <ul>
386                                         <li><?php echo _STEP1; ?></li>
387                                         <li>&nbsp; &gt; &nbsp;<?php echo _STEP2; ?></li>
388                                         <li class="gry">&nbsp; &gt; &nbsp;<?php echo _STEP3; ?></li>
389                                         <li class="rightbox">
390                                                 <a href="./?mode=details"><?php echo _MODE2; ?></a>
391                                         </li>
392                                 </ul>
393                         </div>
394                 </div>
395                 <div id="container">
396                         <p class="msg">
397                                 <?php echo _SIMPLE_NAVI2; ?>
398                         </p>
399                         <form method="post" action="./index.php">
400                                 <div class="prt">
401                                         <h2><?php echo _BLOG_HEADER; ?></h2>
402                                         <p class="msg">
403                                                 <?php
404                                                 if ( is_array($errors['blog_setting']) )
405                                                 {
406                                                         foreach ( $errors['blog_setting'] as $error )
407                                                         {
408                                                                 echo '<span class="err">', $error, "</span>\n";
409                                                         }
410                                                 }
411                                                 ?>
412                                         </p>
413                                         <table>
414                                                 <tr>
415                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
416                                                                 <td><input type="text" name="blog_name" value="<?php echo $_SESSION['blog_name'] ? $_SESSION['blog_name'] : 'My Nucleus CMS'; ?>" /></td>
417                                                 </tr>
418                                                 <tr>
419                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
420                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $_SESSION['blog_shortname'] ? $_SESSION['blog_shortname'] : 'mynucleuscms'; ?>" /></td>
421                                                 </tr>
422                                         </table>
423                                 </div>
424
425                                 <div class="prt">
426                                         <h2><?php echo _ADMIN_HEADER; ?></h2>
427                                         <p class="msg">
428                                                 <?php
429                                                 if ( is_array($errors['user_setting']) )
430                                                 {
431                                                         foreach ( $errors['user_setting'] as $error )
432                                                         {
433                                                                 echo '<span class="err">', $error, "</span>\n";
434                                                         }
435                                                 }
436                                                 ?>
437                                         </p>
438                                         <table>
439                                                 <tr>
440                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
441                                                                 <td><input type="text" name="user_realname" value="<?php echo $_SESSION['user_name']; ?>" /></td>
442                                                 </tr>
443                                                 <tr>
444                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
445                                                                 <td><input type="text" name="user_name" value="<?php echo $_SESSION['user_realname']; ?>" /></td>
446                                                 </tr>
447                                                 <tr>
448                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
449                                                                 <td><input type="password" name="user_password" /></td>
450                                                 </tr>
451                                                 <tr>
452                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
453                                                                 <td><input type="password" name="user_password2" /></td>
454                                                 </tr>
455                                                 <tr>
456                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
457                                                                 <td><input type="text" name="user_email" value="<?php echo $_SESSION['user_email']; ?>" /></td>
458                                                 </tr>
459                                         </table>
460                                         <p class="sbt">
461                                                 <button type="submit" name="action" value="install" class="sbt_arw"><?php echo _INSTALL; ?></button>
462                                         </p>
463                                 </div>
464                         </form>
465                 </div>
466 <?php
467 }
468
469 /**
470  * Displays a form to the detail settings
471  */
472 function showDetailSettingForm()
473 {
474         global $errors;
475 ?>
476                 <div id="header">
477                         <div id="navigation">
478                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
479                                 <ul>
480                                         <li class="rightbox">
481                                                 <a href="./?mode=simple"><?php echo _MODE1; ?></a>
482                                         </li>
483                                 </ul>
484                         </div>
485                 </div>
486                 <div id="container_detailed">
487                         <p class="msg">
488                                 <?php echo _DETAIL_NAVI1; ?>
489                         </p>
490                         <ul class="msg">
491                                 <li>PHP: <?php echo phpversion(); ?></li>
492                                 <li>MySQL: <?php echo getMySqlVersion();
493                                                         if (array_key_exists('mysql', $errors)) echo '<span class="err">', $errors['mysql'], '</span>'; ?></li>
494                         </ul>
495                         <?php
496                         if ( is_array($errors['install']) )
497                         {
498                                 echo '<p class="msg">';
499                                 foreach ( $errors['install'] as $error )
500                                 {
501                                         echo '<span class="err">', $error, "</span>\n";
502                                 }
503                                 echo '</p>';
504                         }
505                         ?>
506                         
507                         <form method="post" action="">
508                         
509                                 <div class="prt">
510                                         <h2><?php echo _DETAIL_HEADER1; ?></h2>
511                                         <p class="msg">
512                                                 <?php echo _DETAIL_TEXT1;
513                                                 if ( is_array($errors['db_setting']) )
514                                                 {
515                                                         foreach ( $errors['db_setting'] as $error )
516                                                         {
517                                                                 echo '<span class="err">', $error, "</span>\n";
518                                                         }
519                                                 }
520                                                 ?>
521                                         </p>
522                                         <table>
523                                                 <tr>
524                                                         <th><span class="nam"><?php echo _DB_FIELD1; ?></span><span class="sub"><?php echo _DB_FIELD1_DESC; ?></span></th>
525                                                                 <td><input type="text" name="mysql_host" value="<?php echo $_SESSION['mysql_host']; ?>" /></td>
526                                                 </tr>
527                                                 <tr>
528                                                         <th><span class="nam"><?php echo _DB_FIELD2; ?></span><span class="sub"><?php echo _DB_FIELD2_DESC; ?></span></th>
529                                                                 <td><input type="text" name="mysql_user" value="<?php echo $_SESSION['mysql_user']; ?>" /></td>
530                                                 </tr>
531                                                 <tr>
532                                                         <th><span class="nam"><?php echo _DB_FIELD3; ?></span><span class="sub"><?php echo _DB_FIELD3_DESC; ?></span></th>
533                                                                 <td><input type="text" name="mysql_password" value="<?php echo $_SESSION['mysql_password']; ?>" /></td>
534                                                 </tr>
535                                                 <tr>
536                                                         <th><span class="nam"><?php echo _DB_FIELD4; ?></span><span class="sub"><?php echo _DB_FIELD4_DESC; ?></span></th>
537                                                                 <td><input type="text" name="mysql_database" value="<?php echo $_SESSION['mysql_database']; ?>" /></td>
538                                                 </tr>
539                                                 <tr>
540                                                         <th><span class="nam"><?php echo _DB_FIELD5; ?></span><span class="sub"><?php echo _DB_FIELD5_DESC; ?></span></th>
541                                                                 <td><input type="text" name="mysql_tablePrefix" value="<?php echo $_SESSION['mysql_tablePrefix']; ?>" /></td>
542                                                 </tr>
543                                         </table>
544                                         
545                                         <h2><?php echo _DETAIL_HEADER2; ?></h2>
546                                         <p class="msg">
547                                                 <?php echo _DETAIL_TEXT2;
548                                                 if ( is_array($errors['path_setting']) )
549                                                 {
550                                                         foreach ( $errors['path_setting'] as $error )
551                                                         {
552                                                                 echo '<span class="err">', $error, "</span>\n";
553                                                         }
554                                                 }
555                                                 ?>
556                                         </p>
557                                         <table>
558                                                 <tr>
559                                                         <th><span class="nam"><?php echo _PATH_FIELD1; ?></span></th>
560                                                                 <td><input type="text" name="IndexURL" value="<?php echo $_SESSION['IndexURL']; ?>" /></td>
561                                                 </tr>
562                                                 <tr>
563                                                         <th><span class="nam"><?php echo _PATH_FIELD2; ?></span></th>
564                                                                 <td><input type="text" name="AdminURL" value="<?php echo $_SESSION['AdminURL']; ?>" /></td>
565                                                 </tr>
566                                                 <tr>
567                                                         <th><span class="nam"><?php echo _PATH_FIELD3; ?></span></th>
568                                                                 <td><input type="text" name="AdminPath" value="<?php echo $_SESSION['AdminPath']; ?>" /></td>
569                                                 </tr>
570                                                 <tr>
571                                                         <th><span class="nam"><?php echo _PATH_FIELD4; ?></span></th>
572                                                                 <td><input type="text" name="MediaURL" value="<?php echo $_SESSION['MediaURL']; ?>" /></td>
573                                                 </tr>
574                                                 <tr>
575                                                         <th><span class="nam"><?php echo _PATH_FIELD5; ?></span></th>
576                                                                 <td><input type="text" name="MediaPath" value="<?php echo $_SESSION['MediaPath']; ?>" /></td>
577                                                 </tr>
578                                                 <tr>
579                                                         <th><span class="nam"><?php echo _PATH_FIELD6; ?></span></th>
580                                                                 <td><input type="text" name="SkinsURL" value="<?php echo $_SESSION['SkinsURL']; ?>" /></td>
581                                                 </tr>
582                                                 <tr>
583                                                         <th><span class="nam"><?php echo _PATH_FIELD7; ?></span></th>
584                                                                 <td><input type="text" name="SkinsPath" value="<?php echo $_SESSION['SkinsPath']; ?>" /></td>
585                                                 </tr>
586                                                 <tr>
587                                                         <th><span class="nam"><?php echo _PATH_FIELD8; ?></span></th>
588                                                                 <td><input type="text" name="PluginURL" value="<?php echo $_SESSION['PluginURL']; ?>" /></td>
589                                                 </tr>
590                                                 <tr>
591                                                         <th><span class="nam"><?php echo _PATH_FIELD9; ?></span></th>
592                                                                 <td><input type="text" name="ActionURL" value="<?php echo $_SESSION['ActionURL']; ?>" /></td>
593                                                 </tr>
594                                         </table>
595                                         <p class="msg">
596                                                 <?php echo _DETAIL_TEXT3; ?>
597                                         </p>
598                                         
599                                         <h2><?php echo _DETAIL_HEADER3; ?></h2>
600                                         <p class="msg">
601                                                 <?php echo _DETAIL_TEXT4;
602                                                 if ( is_array($errors['user_setting']) )
603                                                 {
604                                                         foreach ( $errors['user_setting'] as $error )
605                                                         {
606                                                                 echo '<span class="err">', $error, "</span>\n";
607                                                         }
608                                                 }
609                                                 ?>
610                                         </p>
611                                         <table>
612                                                 <tr>
613                                                         <th><span class="nam"><?php echo _ADMIN_FIELD1; ?></span></th>
614                                                                 <td><input type="text" name="user_realname" value="<?php echo $_SESSION['user_realname']; ?>" /></td>
615                                                 </tr>
616                                                 <tr>
617                                                         <th><span class="nam"><?php echo _ADMIN_FIELD2; ?></span><span class="sub"><?php echo _ADMIN_FIELD2_DESC; ?></span></th>
618                                                                 <td><input type="text" name="user_name" value="<?php echo $_SESSION['user_name']; ?>" /></td>
619                                                 </tr>
620                                                 <tr>
621                                                         <th><span class="nam"><?php echo _ADMIN_FIELD3; ?></span><span class="sub"><?php echo _ADMIN_FIELD3_DESC; ?></span></th>
622                                                                 <td><input type="password" name="user_password" /></td>
623                                                 </tr>
624                                                 <tr>
625                                                         <th><span class="nam"><?php echo _ADMIN_FIELD4; ?></span><span class="sub"><?php echo _ADMIN_FIELD4_DESC; ?></span></th>
626                                                                 <td><input type="password" name="user_password2" /></td>
627                                                 </tr>
628                                                 <tr>
629                                                         <th><span class="nam"><?php echo _ADMIN_FIELD5; ?></span></th>
630                                                                 <td><input type="text" name="user_email" value="<?php echo $_SESSION['user_email']; ?>" /></td>
631                                                 </tr>
632                                         </table>
633                                         
634                                         <h2><?php echo _DETAIL_HEADER4; ?></h2>
635                                         <p class="msg">
636                                                 <?php echo _DETAIL_TEXT5;
637                                                 if ( is_array($errors['blog_setting']) )
638                                                 {
639                                                         foreach ( $errors['blog_setting'] as $error )
640                                                         {
641                                                                 echo '<span class="err">', $error, "</span>\n";
642                                                         }
643                                                 }
644                                                 ?>
645                                         </p>
646                                         <table>
647                                                 <tr>
648                                                         <th><span class="nam"><?php echo _BLOG_FIELD1; ?></span></th>
649                                                                 <td><input type="text" name="blog_name" value="<?php echo $_SESSION['blog_name']; ?>" /></td>
650                                                 </tr>
651                                                 <tr>
652                                                         <th><span class="nam"><?php echo _BLOG_FIELD2; ?></span><span class="sub"><?php echo _BLOG_FIELD2_DESC; ?></span></th>
653                                                                 <td><input type="text" name="blog_shortname" value="<?php echo $_SESSION['blog_shortname']; ?>" /></td>
654                                                 </tr>
655                                         </table>
656                                         
657                                         <p class="msg">
658                                                 <?php echo _DETAIL_TEXT6; ?>
659                                         </p>
660                                         
661                                         <p class="sbt">
662                                                 <button type="submit" name="action" value="install" class="sbt_arw"><?php echo _INSTALL; ?></button>
663                                         </p>
664                                 </div>
665                                 
666                         </form>
667                         
668                 </div>
669 <?php
670 }
671
672 /**
673  * Displays a screen to signal the completion of the installation
674  */
675 function showInstallCompleteForm()
676 {
677         global $errors;
678 ?>
679                 <div id="header">
680                         <div id="navigation">
681                                 <h1><img src="./styles/nucleus_rogo.png" alt="NucleusCMS" /></h1>
682                                 <ul>
683                                         <li><?php echo _STEP1; ?></li>
684                                         <li>&nbsp; &gt; &nbsp;<?php echo _STEP2; ?></li>
685                                         <li>&nbsp; &gt; &nbsp;<?php echo _STEP3; ?></li>
686                                 </ul>
687                         </div>
688                 </div>
689                 <div id="container">
690                         <p class="msg">
691                                 <?php echo _INST_TEXT;
692                                 if ( $errors['config_data'] )
693                                 {
694                                         echo '<span class="err">', _INST_TEXT4, '</span>';
695                                         ?><pre><code>&lt;?php
696 // mySQL connection information
697 $MYSQL_HOST = '<b><?php echo $_SESSION['mysql_host']; ?></b>';
698 $MYSQL_USER = '<b><?php echo $_SESSION['mysql_user']; ?></b>';
699 $MYSQL_PASSWORD = '<i><b><?php echo $_SESSION['mysql_password']; ?></b></i>';
700 $MYSQL_DATABASE = '<b><?php echo $_SESSION['mysql_database']; ?></b>';
701 $MYSQL_PREFIX = '<b><?php echo $_SESSION['mysql_tablePrefix']; ?></b>';
702
703 // new in 3.50. first element is db handler, the second is the db driver used by the handler
704 // default is $MYSQL_HANDLER = array('mysql','mysql');
705 //$MYSQL_HANDLER = array('mysql','mysql');
706 //$MYSQL_HANDLER = array('pdo','mysql');
707 $MYSQL_HANDLER = array('mysql','');
708
709 // main nucleus directory
710 $DIR_NUCLEUS = '<b><?php echo $_SESSION['AdminPath']; ?></b>';
711
712 // path to media dir
713 $DIR_MEDIA = '<b><?php echo $_SESSION['MediaPath']; ?></b>';
714
715 // extra skin files for imported skins
716 $DIR_SKINS = '<b><?php echo $_SESSION['SkinsPath']; ?></b>';
717
718 // these dirs are normally sub dirs of the nucleus dir, but
719 // you can redefine them if you wish
720 $DIR_PLUGINS = $DIR_NUCLEUS . 'plugins/';
721 $DIR_LOCALES = $DIR_NUCLEUS . 'locales/';
722 $DIR_LIBS = $DIR_NUCLEUS . 'libs/';
723
724 // include libs
725 include($DIR_LIBS.'globalfunctions.php');
726 ?&gt;</code></pre>
727                                         <?php
728                                 }
729                                 else
730                                 {
731                                         echo '<span class="err">', _INST_TEXT5, '</span>';
732                                 }
733                                 ?>
734                         </p>
735                         <form method="post" action="./index.php">
736                                 <div class="prt">
737                                         <h2><?php echo _INST_HEADER1; ?></h2>
738                                         <p class="msg">
739                                                 <?php echo sprintf(_INST_TEXT1, $_SESSION['blog_name']); ?>
740                                         </p>
741                                         <p class="sbt">
742                                                 <button type="button" name="toBlog" onclick="location.href='<?php echo $_SESSION['IndexURL']; ?>';" class="sbt_arw"><?php echo _INST_BUTTON1; ?></button>
743                                         </p>
744                                 </div>
745
746                                 <div class="prt">
747                                         <h2><?php echo _INST_HEADER2; ?></h2>
748                                         <p class="msg">
749                                                 <?php echo _INST_TEXT2; ?>
750                                         </p>
751                                         <p class="sbt">
752                                                 <button type="button" name="toMng" onclick="location.href='<?php echo $_SESSION['AdminURL']; ?>';" class="sbt_arw"><?php echo _INST_BUTTON2; ?></button>
753                                         </p>
754                                 </div>
755
756                                 <div class="prt">
757                                         <h2><?php echo _INST_HEADER3; ?></h2>
758                                         <p class="msg">
759                                                 <?php echo _INST_TEXT3; ?>
760                                         </p>
761                                         <p class="sbt">
762                                                 <button type="button" name="toAddBlog" onclick="location.href='<?php echo $_SESSION['AdminURL']; ?>index.php?action=createnewlog';" class="sbt_arw"><?php echo _INST_BUTTON3; ?></button>
763                                         </p>
764                                 </div>
765                         </form>
766                 </div>
767 <?php
768 }
769
770 /**
771  * 
772  */
773 function doValidate()
774 {
775         global $errors;
776         $errors = array();
777
778         // 0. put all POST-vars into sessions
779         if (postVar('mysql_host'))        $_SESSION['mysql_host'] = postVar('mysql_host');
780         if (postVar('mysql_user'))        $_SESSION['mysql_user'] = postVar('mysql_user');
781         if (postVar('mysql_password'))    $_SESSION['mysql_password'] = postVar('mysql_password');
782         if (postVar('mysql_database'))    $_SESSION['mysql_database'] = postVar('mysql_database');
783         if (postVar('mysql_tablePrefix')) $_SESSION['mysql_tablePrefix'] = postVar('mysql_tablePrefix');
784         
785         if (postVar('blog_name'))         $_SESSION['blog_name'] = postVar('blog_name');
786         if (postVar('blog_shortname'))    $_SESSION['blog_shortname'] = postVar('blog_shortname');
787         
788         if (postVar('user_name'))
789         {
790                 $_SESSION['user_name'] = postVar('user_name');
791                 $_SESSION['user_password'] = postVar('user_password');
792                 $_SESSION['user_password2'] = postVar('user_password2');
793         }
794         if (postVar('user_realname'))     $_SESSION['user_realname'] = postVar('user_realname');
795         if (postVar('user_email'))        $_SESSION['user_email'] = postVar('user_email');
796         
797         if (postVar('IndexURL'))          $_SESSION['IndexURL'] = postVar('IndexURL');
798         if (postVar('AdminURL'))          $_SESSION['AdminURL'] = postVar('AdminURL');
799         if (postVar('AdminPath'))         $_SESSION['AdminPath'] = postVar('AdminPath');
800         if (postVar('MediaURL'))          $_SESSION['MediaURL'] = postVar('MediaURL');
801         if (postVar('SkinsURL'))          $_SESSION['SkinsURL'] = postVar('SkinsURL');
802         if (postVar('PluginURL'))         $_SESSION['PluginURL'] = postVar('PluginURL');
803         if (postVar('ActionURL'))         $_SESSION['ActionURL'] = postVar('ActionURL');
804         if (postVar('MediaPath'))         $_SESSION['MediaPath'] = postVar('MediaPath');
805         if (postVar('SkinsPath'))         $_SESSION['SkinsPath'] = postVar('SkinsPath');
806         
807         // TODO: I do not know why this is necessary
808         $_SESSION['IndexURL'] = replace_double_backslash($_SESSION['IndexURL']);
809         $_SESSION['AdminURL'] = replace_double_backslash($_SESSION['AdminURL']);
810         $_SESSION['AdminPath'] = replace_double_backslash($_SESSION['AdminPath']);
811         $_SESSION['MediaURL'] = replace_double_backslash($_SESSION['MediaURL']);
812         $_SESSION['SkinsURL'] = replace_double_backslash($_SESSION['SkinsURL']);
813         $_SESSION['PluginURL'] = replace_double_backslash($_SESSION['PluginURL']);
814         $_SESSION['ActionURL'] = replace_double_backslash($_SESSION['ActionURL']);
815         $_SESSION['MediaPath'] = replace_double_backslash($_SESSION['MediaPath']);
816         $_SESSION['SkinsPath'] = replace_double_backslash($_SESSION['SkinsPath']);
817
818
819         // 1. check the database connection
820         $errors['db_setting'] = array();
821         if ( !$_SESSION['mysql_host'] )
822         {
823                 $errors['db_setting'][] = sprintf(_ERROR1, _DB_FIELD1);
824         }
825
826         if ( !$_SESSION['mysql_user'] )
827         {
828                 $errors['db_setting'][] = sprintf(_ERROR1, _DB_FIELD2);
829         }
830
831         if ( !$_SESSION['mysql_database'] )
832         {
833                 $errors['db_setting'][] = sprintf(_ERROR1, _DB_FIELD4);
834         }
835
836         if ( $_SESSION['mysql_tablePrefix'] && (!preg_match('/^[a-zA-Z0-9_]+$/i', $_SESSION['mysql_tablePrefix']) ) )
837         {
838                 $errors['db_setting'][] = sprintf(_ERROR2, _DB_FIELD5);
839         }
840
841         if ( count($errors['db_setting']) == 0 )
842         {
843                 // try to log in to mySQL
844                 global $MYSQL_CONN;
845
846                 // this will need to be changed if we ever allow
847                 $MYSQL_CONN = @sql_connect_args($_SESSION['mysql_host'], $_SESSION['mysql_user'], $_SESSION['mysql_password']);
848
849                 if ( $MYSQL_CONN == FALSE )
850                 {
851                         $errors['db_setting'][] = _ERROR3;
852                 }
853                 else
854                 {
855                         sql_close($MYSQL_CONN);
856                 }
857         }
858
859         if (count($errors['db_setting']) > 0)
860         {
861                 return false;
862         }
863         
864         // 2. check the blog settings
865         $errors['blog_setting'] = array();
866         $errors['user_setting'] = array();
867         if ( !$_SESSION['blog_name'] )
868         {
869                 $errors['blog_setting'][] = sprintf(_ERROR1, _BLOG_FIELD1);
870         }
871
872         if ( !$_SESSION['blog_shortname'] )
873         {
874                 $errors['blog_setting'][] = sprintf(_ERROR1, _BLOG_FIELD2);
875         }
876         elseif ( !_isValidShortName($_SESSION['blog_shortname']) )
877         {
878                 $errors['blog_setting'][] = _ERROR4;
879         }
880
881         if ( !$_SESSION['user_name'] )
882         {
883                 $errors['user_setting'][] = sprintf(_ERROR1, _ADMIN_FIELD2);
884         }
885         elseif ( !_isValidDisplayName($_SESSION['user_name']) )
886         {
887                 $errors['user_setting'][] = _ERROR5;
888         }
889
890         if ( !$_SESSION['user_password'] || !$_SESSION['user_password2'] )
891         {
892                 $errors['user_setting'][] = sprintf(_ERROR1, _ADMIN_FIELD3);
893         }
894
895         if ( $_SESSION['user_password'] != $_SESSION['user_password2'] )
896         {
897                 $errors['user_setting'][] = _ERROR6;
898         }
899
900         if ( !$_SESSION['user_email'] )
901         {
902                 $errors['user_setting'][] = sprintf(_ERROR1, _ADMIN_FIELD5);
903         }
904         elseif ( !_isValidMailAddress($_SESSION['user_email']) )
905         {
906                 $errors['user_setting'][] = _ERROR7;
907         }
908         
909         if (count($errors['blog_setting']) > 0 || count($errors['user_setting']) > 0)
910         {
911                 return false;
912         }
913
914
915         // 3. check the path/url
916         $errors['path_setting'] = array();
917         // TODO: add action.php check
918         if ( !ends_with_slash($_SESSION['IndexURL'])
919                 || !ends_with_slash($_SESSION['AdminURL'])
920                 || !ends_with_slash($_SESSION['MediaURL'])
921                 || !ends_with_slash($_SESSION['SkinsURL'])
922                 || !ends_with_slash($_SESSION['PluginURL']) )
923         {
924                 $errors['path_setting'][] = _ERROR8;
925         }
926
927         if ( !ends_with_slash($_SESSION['AdminPath']) )
928         {
929                 $errors['path_setting'][] = sprintf(_ERROR9, _PATH_FIELD3);
930         }
931
932         if ( !ends_with_slash($_SESSION['MediaPath']) )
933         {
934                 $errors['path_setting'][] = sprintf(_ERROR9, _PATH_FIELD5);
935         }
936
937         if ( !ends_with_slash($_SESSION['SkinsPath']) )
938         {
939                 $errors['path_setting'][] = sprintf(_ERROR9, _PATH_FIELD7);
940         }
941
942         if ( !is_dir($_SESSION['AdminPath']) )
943         {
944                 $errors['path_setting'][] = _ERROR10;
945         }
946
947         if (count($errors['path_setting']) > 0)
948         {
949                 return false;
950         }
951         
952         return true;
953 }
954
955 /**
956  * The installation process itself
957  */
958 function doInstall()
959 {
960         global $errors;
961         global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_PREFIX, $MYSQL_CONN;
962
963         $errors = array();
964         $errors['install'] = array();
965         
966         // 1. put all SESSION-vars into vars
967         $MYSQL_HOST = $_SESSION['mysql_host'];
968         $MYSQL_USER = $_SESSION['mysql_user'];
969         $MYSQL_PASSWORD = $_SESSION['mysql_password'];
970         $MYSQL_DATABASE = $_SESSION['mysql_database'];
971         $MYSQL_PREFIX = $_SESSION['mysql_tablePrefix'];
972
973         $blog_name = $_SESSION['blog_name'];
974         $blog_shortname = $_SESSION['blog_shortname'];
975
976         $user_name = $_SESSION['user_name'];
977         $user_realname = $_SESSION['user_realname'];
978         $user_password = $_SESSION['user_password'];
979         $user_email = $_SESSION['user_email'];
980
981         $config_indexurl = $_SESSION['IndexURL'];
982         $config_adminurl = $_SESSION['AdminURL'];
983         $config_mediaurl = $_SESSION['MediaURL'];
984         $config_skinsurl = $_SESSION['SkinsURL'];
985         $config_pluginurl = $_SESSION['PluginURL'];
986         $config_actionurl = $_SESSION['ActionURL'];
987         $config_adminpath = $_SESSION['AdminPath'];
988         $config_mediapath = $_SESSION['MediaPath'];
989         $config_skinspath = $_SESSION['SkinsPath'];
990         
991         $config_adminemail = $user_email;
992         $config_sitename = $blog_name;
993         
994         // 2.open mySQL connection
995
996         // this will need to be changed if we ever allow
997         $MYSQL_CONN = @sql_connect_args($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD);
998
999         if ( $MYSQL_CONN == FALSE )
1000         {
1001                 $errors['install'][] = _ERROR3;
1002                 return false;
1003         }
1004         
1005         // 3. try to create database
1006         if ( !sql_query('CREATE DATABASE IF NOT EXISTS ' . $MYSQL_DATABASE) )
1007         {
1008                 $errors['install'][] = _ERROR11 . ': ' . sql_error();
1009         }
1010
1011         // 4. try to select database
1012         if ( !sql_select_db($MYSQL_DATABASE) )
1013         {
1014                 $errors['install'][] = _ERROR12;
1015         }
1016         sql_set_charset('utf8');
1017
1018         // 4a. drop table that have already been created
1019         $result = sql_query('SHOW TABLES');
1020         while ($row = mysql_fetch_array($result, MYSQL_NUM))
1021         {
1022                 if ( !sql_query('DROP TABLE ' . $row[0]) )
1023                 {
1024                         $errors['install'][] = _ERROR13 . ': ' . sql_error();
1025                 }
1026         }
1027
1028         if ( count($errors['install']) > 0 )
1029         {
1030                 return false;
1031         }
1032
1033         // 5. execute queries
1034         $filename = 'install.sql';
1035         $fd = fopen($filename, 'r');
1036         $queries = fread($fd, filesize($filename) );
1037         fclose($fd);
1038
1039         $queries = preg_split('#(;\n|;\r)#', $queries);
1040
1041         $aTableNames = array(
1042                 'nucleus_actionlog',
1043                 'nucleus_ban',
1044                 'nucleus_blog',
1045                 'nucleus_category',
1046                 'nucleus_comment',
1047                 'nucleus_config',
1048                 'nucleus_item',
1049                 'nucleus_karma',
1050                 'nucleus_member',
1051                 'nucleus_plugin',
1052                 'nucleus_skin',
1053                 'nucleus_template',
1054                 'nucleus_team',
1055                 'nucleus_activation',
1056                 'nucleus_tickets'
1057         );
1058
1059         // these are unneeded (one of the replacements above takes care of them)
1060         //                      'nucleus_plugin_event',
1061         //                      'nucleus_plugin_option',
1062         //                      'nucleus_plugin_option_desc',
1063         //                      'nucleus_skin_desc',
1064         //                      'nucleus_template_desc',
1065
1066         $aTableNamesPrefixed = array(
1067                 $MYSQL_PREFIX . 'nucleus_actionlog',
1068                 $MYSQL_PREFIX . 'nucleus_ban',
1069                 $MYSQL_PREFIX . 'nucleus_blog',
1070                 $MYSQL_PREFIX . 'nucleus_category',
1071                 $MYSQL_PREFIX . 'nucleus_comment',
1072                 $MYSQL_PREFIX . 'nucleus_config',
1073                 $MYSQL_PREFIX . 'nucleus_item',
1074                 $MYSQL_PREFIX . 'nucleus_karma',
1075                 $MYSQL_PREFIX . 'nucleus_member',
1076                 $MYSQL_PREFIX . 'nucleus_plugin',
1077                 $MYSQL_PREFIX . 'nucleus_skin',
1078                 $MYSQL_PREFIX . 'nucleus_template',
1079                 $MYSQL_PREFIX . 'nucleus_team',
1080                 $MYSQL_PREFIX . 'nucleus_activation',
1081                 $MYSQL_PREFIX . 'nucleus_tickets'
1082         );
1083
1084         // these are unneeded (one of the replacements above takes care of them)
1085         //                      $MYSQL_PREFIX . 'nucleus_plugin_event',
1086         //                      $MYSQL_PREFIX . 'nucleus_plugin_option',
1087         //                      $MYSQL_PREFIX . 'nucleus_plugin_option_desc',
1088         //                      $MYSQL_PREFIX . 'nucleus_skin_desc',
1089         //                      $MYSQL_PREFIX . 'nucleus_template_desc',
1090
1091         $count = count($queries);
1092
1093         for ( $idx = 0; $idx < $count; $idx++ )
1094         {
1095                 $query = trim($queries[$idx]);
1096                 // echo "QUERY = <small>" . _hsc($query) . "</small><p>";
1097
1098                 if ( $query )
1099                 {
1100
1101                         if ( $MYSQL_PREFIX )
1102                         {
1103                                 $query = str_replace($aTableNames, $aTableNamesPrefixed, $query);
1104                         } // end if
1105
1106                         if ( !sql_query($query) )
1107                         {
1108                                 $errors['install'][] = _ERROR13 . ' (<small>' . _hsc($query) . '</small>): ' . sql_error();
1109                         }
1110                 
1111                 } // end if
1112
1113         } // end loop
1114
1115         // 5a. make first post
1116         $newpost = "INSERT INTO ". tableName('nucleus_item')
1117                 . " VALUES (1, '" . _1ST_POST_TITLE . "', '" . _1ST_POST . "', '" . _1ST_POST2 . "', 1, 1, '2005-08-15 11:04:26', 0, 0, 0, 1, 0, 1)";
1118         if ( !sql_query($newpost) )
1119         {
1120                 $errors['install'][] = _ERROR13 . ' (<small>' . _hsc($newpost) . '</small>): ' . sql_error();
1121         }
1122
1123         // 6. update global settings
1124         updateConfig('IndexURL', $config_indexurl);
1125         updateConfig('AdminURL', $config_adminurl);
1126         updateConfig('MediaURL', $config_mediaurl);
1127         updateConfig('SkinsURL', $config_skinsurl);
1128         updateConfig('PluginURL', $config_pluginurl);
1129         updateConfig('ActionURL', $config_actionurl);
1130         updateConfig('AdminEmail', $config_adminemail);
1131         updateConfig('SiteName', $config_sitename);
1132         updateConfig('Locale', i18n::get_current_locale());
1133
1134         // 7. update GOD member
1135         $query = 'UPDATE ' . tableName('nucleus_member')
1136                         . " SET mname = '" . sql_real_escape_string($user_name) . "',"
1137                         . " mrealname = '" . sql_real_escape_string($user_realname) . "',"
1138                         . " mpassword = '" . md5(sql_real_escape_string($user_password) ) . "',"
1139                         . " murl      = '" . sql_real_escape_string($config_indexurl) . "',"
1140                         . " memail    = '" . sql_real_escape_string($user_email) . "',"
1141                         . " madmin    = 1, mcanlogin = 1"
1142                         . " WHERE mnumber = 1";
1143
1144         if ( !sql_query($query) )
1145         {
1146                 $errors['install'][] = _ERROR14 . ': ' . sql_error();
1147         }
1148
1149         // 8. update weblog settings
1150         $query = 'UPDATE ' . tableName('nucleus_blog')
1151                         . " SET bname  = '" . sql_real_escape_string($blog_name) . "',"
1152                         . " bshortname = '" . sql_real_escape_string($blog_shortname) . "',"
1153                         . " burl       = '" . sql_real_escape_string($config_indexurl) . "'"
1154                         . " WHERE bnumber = 1";
1155
1156         if ( !sql_query($query) )
1157         {
1158                 $errors['install'][] = _ERROR15 . ': ' . sql_error();
1159         }
1160
1161         // 8-2. update category settings
1162         $query = 'UPDATE ' . tableName('nucleus_category')
1163                    . " SET cname  = '" . _GENERALCAT_NAME . "',"
1164                    . " cdesc      = '" . _GENERALCAT_DESC . "'"
1165                    . " WHERE catid = 1";
1166
1167         if ( !sql_query($query) )
1168         {
1169                 $errors['install'][] = _ERROR15 . ': ' . sql_error();
1170         }
1171
1172         // 9. update item date
1173         $query = 'UPDATE ' . tableName('nucleus_item')
1174                         . " SET itime = '" . date('Y-m-d H:i:s', time() ) ."'"
1175                         . " WHERE inumber = 1";
1176
1177         if ( !sql_query($query) )
1178         {
1179                 $errors['install'][] = _ERROR16 . ': ' . sql_error();
1180         }
1181
1182         sql_close();
1183         
1184         global $aConfPlugsToInstall, $aConfSkinsToImport;
1185         $aSkinErrors = array();
1186         $aPlugErrors = array();
1187
1188         if ( (count($aConfPlugsToInstall) > 0) || (count($aConfSkinsToImport) > 0) )
1189         {
1190                 // 10. set global variables
1191                 global $DIR_NUCLEUS, $DIR_MEDIA, $DIR_SKINS, $DIR_PLUGINS, $DIR_LOCALES, $DIR_LIBS;
1192                 $DIR_NUCLEUS = $config_adminpath;
1193                 $DIR_MEDIA = $config_mediapath;
1194                 $DIR_SKINS = $config_skinspath;
1195                 $DIR_PLUGINS = $DIR_NUCLEUS . 'plugins/';
1196                 $DIR_LOCALES = $DIR_NUCLEUS . 'locales/';
1197                 $DIR_LIBS = $DIR_NUCLEUS . 'libs/';
1198
1199                 // Are utilized in the installCustomPlugs and installCustomSkins functions.
1200                 include_once($DIR_LIBS . 'globalfunctions.php');
1201                 global $manager;
1202                 if ( !isset($manager) )
1203                 {
1204                         $manager = new MANAGER;
1205                 }
1206
1207                 // 11. install custom skins
1208                 $aSkinErrors = installCustomSkins();
1209                 if ( count($aSkinErrors) > 0 )
1210                 {
1211                         array_push($errors['install'], $aSkinErrors);
1212                 }
1213                 $defskinQue  = 'SELECT sdnumber FROM ' . tableName('nucleus_skin_desc') . " WHERE sdname='default'";
1214                 $res = sql_query($defskinQue);
1215                 $obj = sql_fetch_assoc($res);
1216                 $defSkinID   = $obj['sdnumber'];
1217                 $updateQuery = 'UPDATE ' . tableName('nucleus_blog') . ' SET bdefskin=' . intval($defSkinID) . ' WHERE bnumber=1';
1218                 sql_query($updateQuery);
1219                 $updateQuery = 'UPDATE ' . tableName('nucleus_config') . ' SET value=' . intval($defSkinID). " WHERE name='BaseSkin'";
1220                 sql_query($updateQuery);
1221
1222                 // 12. install custom plugins
1223                 $aPlugErrors = installCustomPlugs($manager);
1224                 if ( count($aPlugErrors) > 0 )
1225                 {
1226                         array_push($errors['install'], $aPlugErrors);
1227                 }
1228         }
1229
1230         // 13. Write config file ourselves (if possible)
1231         $config_data = '<' . '?php' . "\n\n";
1232         $config_data .= "// mySQL connection information\n";
1233         $config_data .= "\$MYSQL_HOST = '" . $MYSQL_HOST . "';\n";
1234         $config_data .= "\$MYSQL_USER = '" . $MYSQL_USER . "';\n";
1235         $config_data .= "\$MYSQL_PASSWORD = '" . $MYSQL_PASSWORD . "';\n";
1236         $config_data .= "\$MYSQL_DATABASE = '" . $MYSQL_DATABASE . "';\n";
1237         $config_data .= "\$MYSQL_PREFIX = '" . $MYSQL_PREFIX . "';\n";
1238         $config_data .= "// new in 3.50. first element is db handler, the second is the db driver used by the handler\n";
1239         $config_data .= "// default is \$MYSQL_HANDLER = array('mysql','mysql');\n";
1240         $config_data .= "//\$MYSQL_HANDLER = array('mysql','mysql');\n";
1241         $config_data .= "//\$MYSQL_HANDLER = array('pdo','mysql');\n";
1242         $config_data .= "\$MYSQL_HANDLER = array('".$MYSQL_HANDLER[0]."','".$MYSQL_HANDLER[1]."');\n";
1243         $config_data .= "\n";
1244         $config_data .= "// main nucleus directory\n";
1245         $config_data .= "\$DIR_NUCLEUS = '" . $config_adminpath . "';\n";
1246         $config_data .= "\n";
1247         $config_data .= "// path to media dir\n";
1248         $config_data .= "\$DIR_MEDIA = '" . $config_mediapath . "';\n";
1249         $config_data .= "\n";
1250         $config_data .= "// extra skin files for imported skins\n";
1251         $config_data .= "\$DIR_SKINS = '" . $config_skinspath . "';\n";
1252         $config_data .= "\n";
1253         $config_data .= "// these dirs are normally sub dirs of the nucleus dir, but \n";
1254         $config_data .= "// you can redefine them if you wish\n";
1255         $config_data .= "\$DIR_PLUGINS = \$DIR_NUCLEUS . 'plugins/';\n";
1256         $config_data .= "\$DIR_LOCALES = \$DIR_NUCLEUS . 'locales/';\n";
1257         $config_data .= "\$DIR_LIBS = \$DIR_NUCLEUS . 'libs/';\n";
1258         $config_data .= "\n";
1259         $config_data .= "// include libs\n";
1260         $config_data .= "include(\$DIR_LIBS.'globalfunctions.php');\n";
1261         $config_data .= "?" . ">";
1262
1263         $errors['config_data'] = $config_data;
1264         
1265         if ( @!file_exists('../config.php')
1266                 || (@file_exists('../config.php') && is_writable('../config.php')) )
1267         {
1268                 if ( $fp = @fopen('../config.php', 'w') )
1269                 {
1270                         $result = @fwrite($fp, $config_data, i18n::strlen($config_data) );
1271                         fclose($fp);
1272
1273                         if ( $result )
1274                         {
1275                                 unset($errors['config_data']);
1276                                 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1277                                 {
1278                                         @chmod('../config.php', 0444);
1279                                 }
1280                         } // end if
1281                 }
1282         } // end if
1283
1284         if ( count($errors['install']) > 0 )
1285         {
1286                 return false;
1287         }
1288         return true;
1289 } // end function doInstall()
1290
1291 /**
1292  * Verifying that you can write to the configuration file
1293  */
1294 function canConfigFileWritable()
1295 {
1296         global $errors;
1297         
1298         // tell people how they can have their config file filled out automatically
1299         if ( @file_exists('../config.php') && @!is_writable('../config.php') )
1300         {
1301                 // try to change the permissions.
1302                 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' )
1303                 {
1304                     @chmod('../config.php', 0666);
1305                 }
1306
1307                 if ( @!is_writable('../config.php') )
1308                 {
1309                         $errors['config'] = _ERROR17;
1310                         return false;
1311                 }
1312         } // end if
1313         
1314         return true;
1315 }
1316
1317 /**
1318  * To obtain the version of MySQL
1319  *
1320  * @return string
1321  */
1322 function getMySqlVersion()
1323 {
1324         global $minimum_mysql_version, $errors;
1325         // Turn on output buffer
1326         // Needed to repress the output of the sql function that are
1327         // not part of php (in this case the @ operator doesn't work)
1328         ob_start();
1329
1330         // note: this piece of code is taken from phpMyAdmin
1331         $conn = sql_connect_args('localhost', '', '');
1332         $result = @sql_query('SELECT VERSION() AS version', $conn);
1333
1334         if ( $result != FALSE && sql_num_rows($result) > 0 )
1335         {
1336                 $row = sql_fetch_array($result);
1337                 $match = i18n::explode('.', $row['version']);
1338         }
1339         else
1340         {
1341                 $result = @sql_query('SHOW VARIABLES LIKE \'version\'', $conn);
1342
1343                 if ( $result != FALSE && @sql_num_rows($result) > 0 )
1344                 {
1345                         $row = sql_fetch_row($result);
1346                         $match = i18n::explode('.', $row[1]);
1347                 }
1348                 else
1349                 {
1350                         //$output = shell_exec('mysql -V');
1351                         $output = ( function_exists('shell_exec') ) ? @shell_exec('mysql -V') : '0.0.0';
1352                         preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
1353                         $match = i18n::explode('.', $version[0]);
1354
1355                         if ( $match[0] == '' )
1356                         {
1357                                 $match = array('0', '0', '0');
1358                         } // end if
1359
1360                 } // end if
1361
1362         } // end if
1363
1364         @sql_disconnect($conn);
1365
1366         //End and clean output buffer
1367         ob_end_clean();
1368
1369         $mysqlVersion = implode($match, '.');
1370
1371         if ( $mysqlVersion < $minimum_mysql_version )
1372         {
1373                 $errors['mysql'] = sprintf(_ERROR18 , $minimum_mysql_version);
1374         }
1375
1376         if ( $mysqlVersion == '0.0.0' )
1377         {
1378                 return _ERROR19;
1379         }
1380         else
1381         {
1382                 return $mysqlVersion;
1383         }
1384 }
1385
1386
1387 /**
1388  * To obtain information of the directory and path where you want to install the Nucleus.
1389  */
1390 function getURLAndPath()
1391 {
1392         // no need to this all! dirname(__FILE__) is all we need -- moraes
1393         /*
1394         // discover full path
1395         $fullPath = serverVar('PATH_TRANSLATED');
1396
1397         if ($fullPath == '') {
1398                 $fullPath = serverVar('SCRIPT_FILENAME');
1399         }
1400
1401         $base_path = str_replace('./index.php', '', $fullPath);
1402         $base_path = replace_double_backslash($base_path);
1403         $base_path = replace_double_backslash($base_path);
1404
1405         // add slash at end if necessary
1406         if (!ends_with_slash($base_path) ) {
1407                 $base_path .= '/';
1408         }
1409         */
1410
1411         /**
1412          * In the code below, there used to be if conditions within the form's HTML that conditionally echoed the URLs.
1413          *
1414          * For example:
1415          *              if ($base_path) . . .
1416          * Or:
1417          *              if ($url) . . .
1418          *
1419          * I removed this and simplified below, because PHP's type casting will make these always evaluate to TRUE.
1420          * At least currently, $base_path will always be non-empty, since the trailing slash is appended to it.
1421          * Similarly, $index_url will always be non-empty, since the 'http://' is prepended to it.
1422          * Non-empty, non-zero strings evaluated in if conditions are always cast to boolean TRUE.
1423          * The if conditions were accomplishing nothing (currently) and we should avoid using such comparisons, anyway.
1424          * If we need to check for a blank/empty string, use empty().
1425          *
1426          * I was initially replacing those if conditions with ternary operators for empty(), but then I realized
1427          * they will never be empty.
1428          *
1429          * In addition, I decided to remove the PHP logic from within the form and set up separate variables (they were
1430          * all just $url before), so the form just echos the values as needed.
1431          * - gregorlove 7/13/2011 5:56 PM
1432          */
1433         
1434         /* base path */
1435         $this_dir = implode('/', i18n::explode(DIRECTORY_SEPARATOR, dirname(__FILE__)));
1436         $base_path = realpath($this_dir . '/..');
1437         $pwd = preg_replace("#$base_path/#", '', $this_dir);
1438         
1439         # Index URL
1440         $index_url = 'http://' . serverVar('HTTP_HOST') . serverVar('PHP_SELF');
1441         $index_url = preg_replace("#$pwd/index.php#", '', $index_url);
1442         $index_url = replace_double_backslash($index_url);
1443
1444         // add slash at end if necessary
1445         if ( !ends_with_slash($base_path) )
1446         {
1447                 $base_path .= '/';
1448         }
1449         if ( !ends_with_slash($index_url) )
1450         {
1451                 $index_url .= '/';
1452         }
1453
1454         # Admin URL and path
1455         $admin_url = $index_url . 'nucleus/';
1456         $admin_path = $base_path . 'nucleus/';
1457
1458         # Media URL and path
1459         $media_url = $index_url . 'media/';
1460         $media_path = $base_path . 'media/';
1461
1462         # Skins URL and path
1463         $skins_url = $index_url . 'skins/';
1464         $skins_path = $base_path . 'skins/';
1465
1466         # Plugins URL
1467         $plugins_url = $admin_url . 'plugins/';
1468
1469         # Action URL
1470         $action_url = $index_url . 'action.php';
1471
1472         $_SESSION['IndexURL'] = $index_url;
1473         $_SESSION['AdminURL'] = $admin_url;
1474         $_SESSION['AdminPath'] = $admin_path;
1475         $_SESSION['MediaURL'] = $media_url;
1476         $_SESSION['MediaPath'] = $media_path;
1477         $_SESSION['SkinsURL'] = $skins_url;
1478         $_SESSION['SkinsPath'] = $skins_path;
1479         $_SESSION['PluginURL'] = $plugins_url;
1480         $_SESSION['ActionURL'] = $action_url;
1481         
1482 }
1483
1484 /**
1485  * Add a table prefix if it is used
1486  *
1487  * @param string $input table name with prefix
1488  * @return string
1489  */
1490 function tableName($input)
1491 {
1492         if ( $_SESSION['mysql_tablePrefix'] )
1493         {
1494                 return $_SESSION['mysql_tablePrefix'] . $input;
1495         }
1496         else
1497         {
1498                 return $input;
1499         } // end if
1500
1501 }
1502
1503
1504
1505 /**
1506  * Install custom plugins
1507  *
1508  * @param object $manager MANAGER class instance
1509  */
1510 function installCustomPlugs($manager)
1511 {
1512         global $aConfPlugsToInstall, $DIR_LIBS;
1513
1514         $aErrors = array();
1515
1516         if ( count($aConfPlugsToInstall) == 0 )
1517         {
1518                 return $aErrors;
1519         }
1520
1521         $res = sql_query('SELECT * FROM ' . tableName('nucleus_plugin') );
1522         $numCurrent = sql_num_rows($res);
1523
1524         foreach ( $aConfPlugsToInstall as $plugName )
1525         {
1526                 // do this before calling getPlugin (in case the plugin id is used there)
1527                 $query = 'INSERT INTO ' . tableName('nucleus_plugin') . ' (porder, pfile) VALUES (' . (++$numCurrent) . ", '" . sql_real_escape_string($plugName) . "')";
1528                 sql_query($query);
1529
1530                 // get and install the plugin
1531                 $manager->clearCachedInfo('installedPlugins');
1532                 $plugin =& $manager->getPlugin($plugName);
1533                 $plugin->setID($numCurrent);
1534
1535                 if ( !$plugin )
1536                 {
1537                         sql_query('DELETE FROM ' . tableName('nucleus_plugin') . " WHERE pfile = '" . sql_real_escape_string($plugName) . "'");
1538                         $numCurrent--;
1539                         array_push($aErrors, sprintf(_ERROR20 ,$plugName));
1540                         continue;
1541                 } // end if
1542
1543                 $plugin->install();
1544         } // end loop
1545
1546         // SYNC PLUGIN EVENT LIST
1547         sql_query('DELETE FROM ' . tableName('nucleus_plugin_event') );
1548
1549         // loop over all installed plugins
1550         $res = sql_query('SELECT pid, pfile FROM ' . tableName('nucleus_plugin') );
1551
1552         while ( $o = sql_fetch_object($res) )
1553         {
1554                 $pid = $o->pid;
1555                 $plug =& $manager->getPlugin($o->pfile);
1556
1557                 if ( $plug )
1558                 {
1559                         $eventList = $plug->getEventList();
1560
1561                         foreach ( $eventList as $eventName )
1562                         {
1563                                 sql_query('INSERT INTO ' . tableName('nucleus_plugin_event') . ' (pid, event) VALUES (' . $pid . ", '" . $eventName . "')");
1564                         } // end loop
1565
1566                 } // end if
1567
1568         } // end loop
1569
1570         return $aErrors;
1571 } // end function installCustomPlugs()
1572
1573
1574 /**
1575  * Install custom skins
1576  * Prepares the installation of custom skins
1577  */
1578 function installCustomSkins()
1579 {
1580         global $aConfSkinsToImport, $DIR_LIBS, $DIR_SKINS;
1581
1582         $aErrors = array();
1583
1584         if ( count($aConfSkinsToImport) == 0 )
1585         {
1586                 return $aErrors;
1587         }
1588
1589         // load skinie class
1590         include_once($DIR_LIBS . 'skinie.php');
1591
1592         $importer = new SKINIMPORT();
1593
1594         foreach ( $aConfSkinsToImport as $skinName )
1595         {
1596                 $importer->reset();
1597                 $skinFile = $DIR_SKINS . $skinName . '/skinbackup.xml';
1598
1599                 if ( !@file_exists($skinFile) )
1600                 {
1601                         array_push($aErrors, sprintf(_ERROR21, $skinFile));
1602                         continue;
1603                 } // end if
1604
1605                 $error = $importer->readFile($skinFile);
1606
1607                 if ( $error )
1608                 {
1609                         array_push($aErrors, sprintf(_ERROR22, $skinName) . ' : ' . $error);
1610                         continue;
1611                 } // end if
1612
1613                 $error = $importer->writeToDatabase(1);
1614
1615                 if ( $error )
1616                 {
1617                         array_push($aErrors, sprintf(_ERROR22, $skinName) . ' : ' . $error);
1618                         continue;
1619                 } // end if
1620
1621         } // end loop
1622
1623         return $aErrors;
1624 } // end function installCustomSkins()
1625
1626
1627 /**
1628  * Check if some important files of the Nucleus CMS installation are available
1629  * Give an error if one or more files are not accessible
1630  */
1631 function doCheckFiles()
1632 {
1633         $missingfiles = array();
1634         $files = array(
1635                 './install.sql',
1636                 '../index.php',
1637                 '../action.php',
1638                 '../nucleus/index.php',
1639                 '../nucleus/media.php',
1640                 '../nucleus/libs/globalfunctions.php',
1641                 '../nucleus/libs/ADMIN.php',
1642                 '../nucleus/libs/BLOG.php',
1643                 '../nucleus/libs/COMMENT.php',
1644                 '../nucleus/libs/COMMENTS.php',
1645                 '../nucleus/libs/ITEM.php',
1646                 '../nucleus/libs/MEMBER.php',
1647                 '../nucleus/libs/SKIN.php',
1648                 '../nucleus/libs/TEMPLATE.php',
1649                 '../nucleus/libs/MEDIA.php',
1650                 '../nucleus/libs/ACTIONLOG.php',
1651                 '../nucleus/libs/vars4.1.0.php',
1652                 '../nucleus/libs/i18n.php',
1653                 '../nucleus/libs/mysql.php',
1654                 '../nucleus/libs/sql/mysql.php'
1655         );
1656         
1657         $count = count($files);
1658
1659         for ( $i = 0; $i < $count; $i++ )
1660         {
1661
1662                 if ( !is_readable($files[$i]) )
1663                 {
1664                         array_push( $missingfiles, 'File <b>' . $files[$i] . '</b> is missing or not readable.<br />');
1665                 } // end if
1666
1667         } // end loop
1668
1669         if ( count($missingfiles) > 0 )
1670         {
1671                 exit(implode( "\n", $missingfiles));
1672         } // end if
1673
1674 } // end function doCheckFiles()
1675
1676
1677 /**
1678  * Updates the configuration in the database
1679  *
1680  * @param string $name name of the config var
1681  * @param string $value new value of the config var
1682  * @return int
1683  */
1684 function updateConfig($name, $value)
1685 {
1686         global $errors;
1687         $name = addslashes($name);
1688         $value = trim(addslashes($value) );
1689
1690         $query = 'UPDATE ' . tableName('nucleus_config')
1691                         . " SET `value` = '$value'"
1692                         . " WHERE `name` = '$name'";
1693
1694         if ( !sql_query($query) )
1695         {
1696                 $errors['install'][] = _ERROR13 . ': ' . sql_error();
1697         }
1698         return sql_insert_id();
1699 }
1700
1701
1702 /**
1703  * Replaces double backslashs
1704  *
1705  * @param string $input string that could have double backslashs
1706  * @return string
1707  */
1708 function replace_double_backslash($input)
1709 {
1710         return str_replace('\\', '/', $input);
1711 }
1712
1713
1714 /**
1715  * Checks if a string ends with a slash
1716  *
1717  * @param string $input
1718  * @return string
1719  */
1720 function ends_with_slash($input)
1721 {
1722         return ( i18n::strrpos($input, '/') == i18n::strlen($input) - 1);
1723 }
1724
1725
1726 /**
1727  * Checks if email address is valid
1728  *
1729  * @param string $address address which should be tested
1730  * @return bool
1731  */
1732 function _isValidMailAddress($address)
1733 {
1734
1735         if ( preg_match("/^[a-zA-Z0-9\._-]+@[A-Za-z0-9\._-]+\.[A-Za-z]{2,6}$/", $address) )
1736         {
1737                 return TRUE;
1738         }
1739         else
1740         {
1741                 return FALSE;
1742         } // end if
1743
1744 }
1745
1746
1747 /*
1748  * Check if short blog names and nicknames are allowed
1749  * Returns true if the given string is a valid shortname
1750  * logic: only letters and numbers are allowed, no spaces allowed
1751  *
1752  * FIX: function eregi is deprecated since PHP 5.3.0
1753  *
1754  * @param string $name name which should be tested
1755  * @return bool
1756  */
1757 function _isValidShortName($name)
1758 {
1759
1760         if ( preg_match("/^[a-z0-9]+$/i", $name) )
1761         {
1762                 return TRUE;
1763         }
1764         else
1765         {
1766                 return FALSE;
1767         } // end if
1768
1769 }
1770
1771
1772 /*
1773  * Check if a display name is allowed
1774  * Returns true if the given string is a valid display name
1775  *
1776  * FIX: function eregi is deprecated since PHP 5.3.0
1777  *
1778  * @param string $name name which should be tested
1779  * @return bool
1780  */
1781 function _isValidDisplayName($name)
1782 {
1783
1784         if ( preg_match("/^[a-z0-9]+[a-z0-9 ]*[a-z0-9]+$/i", $name) )
1785         {
1786                 return TRUE;
1787         }
1788         else
1789         {
1790                 return FALSE;
1791         } // end if
1792
1793 }
1794
1795 /**
1796  * _hsc
1797  * htmlspecialchars wrapper
1798  * 
1799  * NOTE: htmlspecialchars_decode() is ASCII-to-ACII conversion
1800  *  and its target string consists of several letters.
1801  *   There are no problems.
1802  * 
1803  * @static
1804  * @access public
1805  * @param       string  $string target string
1806  * @param       string  $quotation      quotation mode. please refer to the argument of PHP built-in htmlspecialchars
1807  * @return      string  escaped string
1808  * 
1809  */
1810 function _hsc($string, $quotation=ENT_QUOTES)
1811 {
1812         /*
1813          * we can use 'double_encode' flag instead of this when dropping supports for PHP 5.2.2 or lower
1814          */
1815         $string = htmlspecialchars_decode($string, $quotation);
1816         return (string) htmlspecialchars($string, $quotation, i18n::get_current_charset());
1817 }
1818
1819 /* for the non-php systems that decide to show the contents:
1820 ?></div>
1821 <?php
1822 */