source: trunk/install.php @ 2127

Last change on this file since 2127 was 2127, checked in by rvelices, 17 years ago
  • PWG_CHARSET, DB_CHARSET and DB_COLLATE... utf-8 ready
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: install.php 2127 2007-10-09 01:43:29Z rvelices $
8// | last update   : $Date: 2007-10-09 01:43:29 +0000 (Tue, 09 Oct 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2127 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27//----------------------------------------------------------- include
28define('PHPWG_ROOT_PATH','./');
29
30//
31// Pick a language, any language ...
32//
33function language_select($default, $select_name = "language")
34{
35  $available_lang = get_languages('utf-8');
36
37  $lang_select = '<select name="' . $select_name . '" onchange="document.location = \''.PHPWG_ROOT_PATH.'install.php?language=\'+this.options[this.selectedIndex].value;">';
38  foreach ($available_lang as $code => $displayname)
39  {
40    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
41    $lang_select .= '<option value="'.$code.'" ' . $selected . '>' . ucwords($displayname) . '</option>';
42  }
43  $lang_select .= '</select>';
44
45  return $lang_select;
46}
47
48/**
49 * loads an sql file and executes all queries
50 *
51 * Before executing a query, $replaced is... replaced by $replacing. This is
52 * useful when the SQL file contains generic words. Drop table queries are
53 * not executed.
54 *
55 * @param string filepath
56 * @param string replaced
57 * @param string replacing
58 * @return void
59 */
60function execute_sqlfile($filepath, $replaced, $replacing)
61{
62  $sql_lines = file($filepath);
63  $query = '';
64  foreach ($sql_lines as $sql_line)
65  {
66    $sql_line = trim($sql_line);
67    if (preg_match('/(^--|^$)/', $sql_line))
68    {
69      continue;
70    }
71    $query.= ' '.$sql_line;
72    // if we reached the end of query, we execute it and reinitialize the
73    // variable "query"
74    if (preg_match('/;$/', $sql_line))
75    {
76      $query = trim($query);
77      $query = str_replace($replaced, $replacing, $query);
78      // we don't execute "DROP TABLE" queries
79      if (!preg_match('/^DROP TABLE/i', $query))
80      {
81        global $install_charset_collate;
82        if ( !empty($install_charset_collate) )
83        {
84          if ( preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches) )
85          {
86            $query = $matches[1].' '.$install_charset_collate.';';
87          }
88        }
89        mysql_query($query);
90      }
91      $query = '';
92    }
93  }
94}
95
96set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
97//
98// addslashes to vars if magic_quotes_gpc is off this is a security
99// precaution to prevent someone trying to break out of a SQL statement.
100//
101if( !get_magic_quotes_gpc() )
102{
103  if( is_array($_POST) )
104  {
105    while( list($k, $v) = each($_POST) )
106    {
107      if( is_array($_POST[$k]) )
108      {
109        while( list($k2, $v2) = each($_POST[$k]) )
110        {
111          $_POST[$k][$k2] = addslashes($v2);
112        }
113        @reset($_POST[$k]);
114      }
115      else
116      {
117        $_POST[$k] = addslashes($v);
118      }
119    }
120    @reset($_POST);
121  }
122
123  if( is_array($_GET) )
124  {
125    while( list($k, $v) = each($_GET) )
126    {
127      if( is_array($_GET[$k]) )
128      {
129        while( list($k2, $v2) = each($_GET[$k]) )
130        {
131          $_GET[$k][$k2] = addslashes($v2);
132        }
133        @reset($_GET[$k]);
134      }
135      else
136      {
137        $_GET[$k] = addslashes($v);
138      }
139    }
140    @reset($_GET);
141  }
142
143  if( is_array($_COOKIE) )
144  {
145    while( list($k, $v) = each($_COOKIE) )
146    {
147      if( is_array($_COOKIE[$k]) )
148      {
149        while( list($k2, $v2) = each($_COOKIE[$k]) )
150        {
151          $_COOKIE[$k][$k2] = addslashes($v2);
152        }
153        @reset($_COOKIE[$k]);
154      }
155      else
156      {
157        $_COOKIE[$k] = addslashes($v);
158      }
159    }
160    @reset($_COOKIE);
161  }
162}
163
164//----------------------------------------------------- variable initialization
165
166define('DEFAULT_PREFIX_TABLE', 'phpwebgallery_');
167
168// Obtain various vars
169$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
170$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
171$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
172$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
173
174if (isset($_POST['install']))
175{
176  $table_prefix = $_POST['prefix'];
177}
178else
179{
180  $table_prefix = DEFAULT_PREFIX_TABLE;
181}
182
183$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
184$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
185$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
186$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
187
188$infos = array();
189$errors = array();
190
191// Open config.php ... if it exists
192$config_file = PHPWG_ROOT_PATH.'include/mysql.inc.php';
193if (@file_exists($config_file))
194{
195  include($config_file);
196  // Is PhpWebGallery already installed ?
197  if (defined("PHPWG_INSTALLED"))
198  {
199    die('PhpWebGallery is already installed');
200  }
201}
202
203$prefixeTable = $table_prefix;
204include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
205@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
206include(PHPWG_ROOT_PATH . 'include/constants.php');
207include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
208include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
209include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
210include(PHPWG_ROOT_PATH . 'include/template.php');
211
212// Create empty local files to avoid log errors
213create_empty_local_files();
214
215if ( isset( $_REQUEST['language'] ))
216{
217  $language = strip_tags($_REQUEST['language']);
218}
219else
220{
221  $language = 'en_UK';
222}
223
224load_language( 'common.lang', '', $language, false, 'utf-8' );
225load_language( 'admin.lang', '', $language, false, 'utf-8' );
226load_language( 'install.lang', '', $language, false, 'utf-8' );
227
228//----------------------------------------------------- template initialization
229$template=new Template(PHPWG_ROOT_PATH.'template/yoga');
230$template->set_filenames( array('install'=>'install.tpl') );
231$step = 1;
232//---------------------------------------------------------------- form analyze
233if ( isset( $_POST['install'] ))
234{
235  if ( @mysql_connect( $_POST['dbhost'],
236                       $_POST['dbuser'],
237                       $_POST['dbpasswd'] ) )
238  {
239    if ( @mysql_select_db($_POST['dbname'] ) )
240    {
241      array_push( $infos, $lang['step1_confirmation'] );
242    }
243    else
244    {
245      array_push( $errors, $lang['step1_err_db'] );
246    }
247    if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
248    {
249      $pwg_charset='utf-8';
250      $pwg_db_charset='utf8';
251      $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
252    }
253    else
254    {
255      $pwg_charset='iso-8859-1';
256      $pwg_db_charset='latin1';
257      $install_charset_collate = '';
258      if ( !array_key_exists($language, get_languages($pwg_charset) ) )
259      {
260        $language='en_UK';
261      }
262    }
263  }
264  else
265  {
266    array_push( $errors, $lang['step1_err_server'] );
267  }
268
269  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
270  if ( empty($webmaster))
271    array_push( $errors, $lang['step2_err_login1'] );
272  else if ( preg_match( '/[\'"]/', $webmaster ) )
273    array_push( $errors, $lang['step2_err_login3'] );
274  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
275    array_push( $errors, $lang['step2_err_pass'] );
276  if ( empty($admin_mail))
277    array_push( $errors, $lang['reg_err_mail_address'] );
278  else
279  {
280    $error_mail_address = validate_mail_address(null, $admin_mail);
281    if (!empty($error_mail_address))
282      array_push( $errors, $error_mail_address );
283  }
284
285  if ( count( $errors ) == 0 )
286  {
287    $step = 2;
288    $file_content = '<?php
289$cfgBase = \''.$dbname.'\';
290$cfgUser = \''.$dbuser.'\';
291$cfgPassword = \''.$dbpasswd.'\';
292$cfgHote = \''.$dbhost.'\';
293
294$prefixeTable = \''.$table_prefix.'\';
295
296define(\'PHPWG_INSTALLED\', true);
297define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
298define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
299define(\'DB_COLLATE\', \'\');
300
301?'.'>';
302
303    @umask(0111);
304    // writing the configuration file
305    if ( !($fp = @fopen( $config_file, 'w' )))
306    {
307      $html_content = htmlentities( $file_content, ENT_QUOTES );
308      $html_content = nl2br( $html_content );
309      $template->assign_block_vars(
310        'error_copy',
311        array(
312          'FILE_CONTENT' => $html_content,
313          )
314        );
315    }
316    @fputs($fp, $file_content, strlen($file_content));
317    @fclose($fp);
318
319    // tables creation, based on phpwebgallery_structure.sql
320    execute_sqlfile(
321      PHPWG_ROOT_PATH.'install/phpwebgallery_structure.sql',
322      DEFAULT_PREFIX_TABLE,
323      $table_prefix
324      );
325    // We fill the tables with basic informations
326    execute_sqlfile(
327      PHPWG_ROOT_PATH.'install/config.sql',
328      DEFAULT_PREFIX_TABLE,
329      $table_prefix
330      );
331
332    // fill $conf global array
333    load_conf_from_db();
334
335    $insert = array(
336      'id' => 1,
337      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
338      );
339    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
340
341    // webmaster admin user
342    $inserts = array(
343      array(
344        'id'           => 1,
345        'username'     => $admin_name,
346        'password'     => md5($admin_pass1),
347        'mail_address' => $admin_mail,
348        ),
349      array(
350        'id'           => 2,
351        'username'     => 'guest',
352        ),
353      );
354    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
355
356    create_user_infos(array(1,2), array('language' => $language));
357
358    // Available upgrades must be ignored after a fresh installation. To
359    // make PWG avoid upgrading, we must tell it upgrades have already been
360    // made.
361    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
362    define('CURRENT_DATE', $dbnow);
363    $datas = array();
364    foreach (get_available_upgrade_ids() as $upgrade_id)
365    {
366      array_push(
367        $datas,
368        array(
369          'id'          => $upgrade_id,
370          'applied'     => CURRENT_DATE,
371          'description' => 'upgrade included in installation',
372          )
373        );
374    }
375    mass_inserts(
376      UPGRADE_TABLE,
377      array_keys($datas[0]),
378      $datas
379      );
380  }
381}
382
383$template->assign_vars(
384  array(
385    'RELEASE'=>PHPWG_VERSION,
386
387    'L_BASE_TITLE'=>$lang['Initial_config'],
388    'L_LANG_TITLE'=>$lang['Default_lang'],
389    'L_DB_TITLE'=>$lang['step1_title'],
390    'L_DB_HOST'=>$lang['step1_host'],
391    'L_DB_HOST_INFO'=>$lang['step1_host_info'],
392    'L_DB_USER'=>$lang['step1_user'],
393    'L_DB_USER_INFO'=>$lang['step1_user_info'],
394    'L_DB_PASS'=>$lang['step1_pass'],
395    'L_DB_PASS_INFO'=>$lang['step1_pass_info'],
396    'L_DB_NAME'=>$lang['step1_database'],
397    'L_DB_NAME_INFO'=>$lang['step1_database_info'],
398    'L_DB_PREFIX'=>$lang['step1_prefix'],
399    'L_DB_PREFIX_INFO'=>$lang['step1_prefix_info'],
400    'L_ADMIN_TITLE'=>$lang['step2_title'],
401    'L_ADMIN'=>$lang['install_webmaster'],
402    'L_ADMIN_INFO'=>$lang['install_webmaster_info'],
403    'L_ADMIN_PASSWORD'=>$lang['step2_pwd'],
404    'L_ADMIN_PASSWORD_INFO'=>$lang['step2_pwd_info'],
405    'L_ADMIN_CONFIRM_PASSWORD'=>$lang['step2_pwd_conf'],
406    'L_ADMIN_CONFIRM_PASSWORD_INFO'=>$lang['step2_pwd_conf_info'],
407    'L_ADMIN_EMAIL'=>$lang['conf_mail_webmaster'],
408    'L_ADMIN_EMAIL_INFO'=>$lang['conf_mail_webmaster_info'],
409    'L_SUBMIT'=>$lang['Start_Install'],
410    'L_INSTALL_HELP'=>sprintf($lang['install_help'], 'http://forum.'.PHPWG_DOMAIN.'/'),
411    'L_ERR_COPY'=>$lang['step1_err_copy'],
412    'L_END_TITLE'=>$lang['install_end_title'],
413    'L_END_MESSAGE'=>$lang['install_end_message'],
414
415    'F_ACTION'=>'install.php',
416    'F_DB_HOST'=>$dbhost,
417    'F_DB_USER'=>$dbuser,
418    'F_DB_NAME'=>$dbname,
419    'F_DB_PREFIX' => (
420      $table_prefix != DEFAULT_PREFIX_TABLE
421      ? $table_prefix
422      : DEFAULT_PREFIX_TABLE
423      ),
424    'F_ADMIN'=>$admin_name,
425    'F_ADMIN_EMAIL'=>$admin_mail,
426    'F_LANG_SELECT'=>language_select($language),
427
428    'T_CONTENT_ENCODING' => 'utf-8'
429    ));
430
431//------------------------------------------------------ errors & infos display
432if ( sizeof( $errors ) != 0 )
433{
434  $template->assign_block_vars('errors',array());
435  for ( $i = 0; $i < sizeof( $errors ); $i++ )
436  {
437    $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
438  }
439}
440
441if ( sizeof( $infos ) != 0 )
442{
443  $template->assign_block_vars('infos',array());
444  for ( $i = 0; $i < sizeof( $infos ); $i++ )
445  {
446    $template->assign_block_vars('infos.info',array('INFO'=>$infos[$i]));
447  }
448}
449
450if ($step ==1)
451{
452  $template->assign_block_vars('install',array());
453}
454else
455{
456  $template->assign_block_vars('install_end',array());
457}
458
459//----------------------------------------------------------- html code display
460$template->pparse('install');
461?>
Note: See TracBrowser for help on using the repository browser.