source: trunk/install.php @ 2231

Last change on this file since 2231 was 2201, checked in by rub, 16 years ago

Replace old use of $lang by l10n function.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.6 KB
RevLine 
[218]1<?php
[354]2// +-----------------------------------------------------------------------+
[593]3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
[1726]5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
[354]6// +-----------------------------------------------------------------------+
[1855]7// | file          : $Id: install.php 2201 2008-01-30 22:07:07Z rub $
[354]8// | last update   : $Date: 2008-01-30 22:07:07 +0000 (Wed, 30 Jan 2008) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2201 $
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// +-----------------------------------------------------------------------+
[218]26
[367]27//----------------------------------------------------------- include
28define('PHPWG_ROOT_PATH','./');
[345]29
[1855]30//
31// Pick a language, any language ...
32//
33function language_select($default, $select_name = "language")
34{
[2127]35  $available_lang = get_languages('utf-8');
[1855]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
[529]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)
[382]61{
[529]62  $sql_lines = file($filepath);
[382]63  $query = '';
[529]64  foreach ($sql_lines as $sql_line)
65  {
66    $sql_line = trim($sql_line);
67    if (preg_match('/(^--|^$)/', $sql_line))
68    {
69      continue;
70    }
[382]71    $query.= ' '.$sql_line;
72    // if we reached the end of query, we execute it and reinitialize the
73    // variable "query"
[529]74    if (preg_match('/;$/', $sql_line))
[382]75    {
[529]76      $query = trim($query);
77      $query = str_replace($replaced, $replacing, $query);
[382]78      // we don't execute "DROP TABLE" queries
[529]79      if (!preg_match('/^DROP TABLE/i', $query))
80      {
[2127]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        }
[529]89        mysql_query($query);
90      }
[382]91      $query = '';
92    }
93  }
94}
95
[367]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() )
[218]102{
[367]103  if( is_array($_POST) )
[218]104  {
[367]105    while( list($k, $v) = each($_POST) )
[218]106    {
[367]107      if( is_array($_POST[$k]) )
[218]108      {
[367]109        while( list($k2, $v2) = each($_POST[$k]) )
110        {
111          $_POST[$k][$k2] = addslashes($v2);
112        }
113        @reset($_POST[$k]);
[218]114      }
115      else
116      {
[367]117        $_POST[$k] = addslashes($v);
[218]118      }
119    }
[367]120    @reset($_POST);
121  }
122
[1855]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
[367]143  if( is_array($_COOKIE) )
144  {
145    while( list($k, $v) = each($_COOKIE) )
[218]146    {
[367]147      if( is_array($_COOKIE[$k]) )
[218]148      {
[367]149        while( list($k2, $v2) = each($_COOKIE[$k]) )
150        {
151          $_COOKIE[$k][$k2] = addslashes($v2);
152        }
153        @reset($_COOKIE[$k]);
[218]154      }
155      else
156      {
[367]157        $_COOKIE[$k] = addslashes($v);
[218]158      }
159    }
[367]160    @reset($_COOKIE);
[218]161  }
[367]162}
[218]163
[367]164//----------------------------------------------------- variable initialization
[1147]165
166define('DEFAULT_PREFIX_TABLE', 'phpwebgallery_');
167
[367]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
[1147]174if (isset($_POST['install']))
175{
176  $table_prefix = $_POST['prefix'];
177}
178else
179{
180  $table_prefix = DEFAULT_PREFIX_TABLE;
181}
[367]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{
[529]195  include($config_file);
196  // Is PhpWebGallery already installed ?
197  if (defined("PHPWG_INSTALLED"))
198  {
199    die('PhpWebGallery is already installed');
200  }
[367]201}
202
[682]203$prefixeTable = $table_prefix;
[819]204include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
[1079]205@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
[529]206include(PHPWG_ROOT_PATH . 'include/constants.php');
207include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
[1221]208include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
[2102]209include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
[529]210include(PHPWG_ROOT_PATH . 'include/template.php');
211
[2127]212if ( isset( $_REQUEST['language'] ))
[405]213{
[2127]214  $language = strip_tags($_REQUEST['language']);
[367]215}
[2127]216else
[1855]217{
[2127]218  $language = 'en_UK';
[1855]219}
[367]220
[2127]221load_language( 'common.lang', '', $language, false, 'utf-8' );
222load_language( 'admin.lang', '', $language, false, 'utf-8' );
223load_language( 'install.lang', '', $language, false, 'utf-8' );
[529]224
[367]225//----------------------------------------------------- template initialization
[2127]226$template=new Template(PHPWG_ROOT_PATH.'template/yoga');
[367]227$template->set_filenames( array('install'=>'install.tpl') );
228$step = 1;
[529]229//---------------------------------------------------------------- form analyze
[367]230if ( isset( $_POST['install'] ))
231{
[382]232  if ( @mysql_connect( $_POST['dbhost'],
233                       $_POST['dbuser'],
234                       $_POST['dbpasswd'] ) )
235  {
236    if ( @mysql_select_db($_POST['dbname'] ) )
[367]237    {
[2201]238      array_push( $infos, l10n('step1_confirmation') );
[218]239    }
[367]240    else
241    {
[2201]242      array_push( $errors, l10n('step1_err_db') );
[367]243    }
[2127]244    if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
245    {
246      $pwg_charset='utf-8';
247      $pwg_db_charset='utf8';
248      $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
249    }
250    else
251    {
252      $pwg_charset='iso-8859-1';
253      $pwg_db_charset='latin1';
254      $install_charset_collate = '';
255      if ( !array_key_exists($language, get_languages($pwg_charset) ) )
256      {
257        $language='en_UK';
258      }
259    }
[382]260  }
261  else
262  {
[2201]263    array_push( $errors, l10n('step1_err_server') );
[382]264  }
[2127]265
[382]266  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
267  if ( empty($webmaster))
[2201]268    array_push( $errors, l10n('step2_err_login1') );
[382]269  else if ( preg_match( '/[\'"]/', $webmaster ) )
[2201]270    array_push( $errors, l10n('step2_err_login3') );
[382]271  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
[2201]272    array_push( $errors, l10n('step2_err_pass') );
[382]273  if ( empty($admin_mail))
[2201]274    array_push( $errors, l10n('reg_err_mail_address') );
[2127]275  else
[382]276  {
[2124]277    $error_mail_address = validate_mail_address(null, $admin_mail);
[382]278    if (!empty($error_mail_address))
279      array_push( $errors, $error_mail_address );
280  }
[2127]281
[382]282  if ( count( $errors ) == 0 )
283  {
284    $step = 2;
[1147]285    $file_content = '<?php
286$cfgBase = \''.$dbname.'\';
287$cfgUser = \''.$dbuser.'\';
288$cfgPassword = \''.$dbpasswd.'\';
289$cfgHote = \''.$dbhost.'\';
290
291$prefixeTable = \''.$table_prefix.'\';
292
293define(\'PHPWG_INSTALLED\', true);
[2127]294define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
295define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
296define(\'DB_COLLATE\', \'\');
297
[1147]298?'.'>';
[2127]299
[382]300    @umask(0111);
301    // writing the configuration file
302    if ( !($fp = @fopen( $config_file, 'w' )))
[367]303    {
[382]304      $html_content = htmlentities( $file_content, ENT_QUOTES );
305      $html_content = nl2br( $html_content );
[1284]306      $template->assign_block_vars(
307        'error_copy',
308        array(
309          'FILE_CONTENT' => $html_content,
310          )
311        );
[382]312    }
313    @fputs($fp, $file_content, strlen($file_content));
314    @fclose($fp);
[2127]315
[2189]316    // Create empty local files to avoid log errors
317    create_empty_local_files();
318
[382]319    // tables creation, based on phpwebgallery_structure.sql
[1147]320    execute_sqlfile(
321      PHPWG_ROOT_PATH.'install/phpwebgallery_structure.sql',
322      DEFAULT_PREFIX_TABLE,
323      $table_prefix
324      );
[382]325    // We fill the tables with basic informations
[1147]326    execute_sqlfile(
327      PHPWG_ROOT_PATH.'install/config.sql',
328      DEFAULT_PREFIX_TABLE,
329      $table_prefix
330      );
[218]331
[1284]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));
[2127]340
[382]341    // webmaster admin user
[1284]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);
[801]355
[1930]356    create_user_infos(array(1,2), array('language' => $language));
[808]357
[1027]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.
[1221]361    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
362    define('CURRENT_DATE', $dbnow);
363    $datas = array();
[1027]364    foreach (get_available_upgrade_ids() as $upgrade_id)
365    {
[1221]366      array_push(
367        $datas,
368        array(
369          'id'          => $upgrade_id,
370          'applied'     => CURRENT_DATE,
371          'description' => 'upgrade included in installation',
372          )
373        );
[1027]374    }
[1221]375    mass_inserts(
376      UPGRADE_TABLE,
377      array_keys($datas[0]),
378      $datas
379      );
[382]380  }
[367]381}
[218]382
[529]383$template->assign_vars(
384  array(
385    'RELEASE'=>PHPWG_VERSION,
[2127]386
[2201]387    'L_BASE_TITLE'=>l10n('Initial_config'),
388    'L_LANG_TITLE'=>l10n('Default_lang'),
389    'L_DB_TITLE'=>l10n('step1_title'),
390    'L_DB_HOST'=>l10n('step1_host'),
391    'L_DB_HOST_INFO'=>l10n('step1_host_info'),
392    'L_DB_USER'=>l10n('step1_user'),
393    'L_DB_USER_INFO'=>l10n('step1_user_info'),
394    'L_DB_PASS'=>l10n('step1_pass'),
395    'L_DB_PASS_INFO'=>l10n('step1_pass_info'),
396    'L_DB_NAME'=>l10n('step1_database'),
397    'L_DB_NAME_INFO'=>l10n('step1_database_info'),
398    'L_DB_PREFIX'=>l10n('step1_prefix'),
399    'L_DB_PREFIX_INFO'=>l10n('step1_prefix_info'),
400    'L_ADMIN_TITLE'=>l10n('step2_title'),
401    'L_ADMIN'=>l10n('install_webmaster'),
402    'L_ADMIN_INFO'=>l10n('install_webmaster_info'),
403    'L_ADMIN_PASSWORD'=>l10n('step2_pwd'),
404    'L_ADMIN_PASSWORD_INFO'=>l10n('step2_pwd_info'),
405    'L_ADMIN_CONFIRM_PASSWORD'=>l10n('step2_pwd_conf'),
406    'L_ADMIN_CONFIRM_PASSWORD_INFO'=>l10n('step2_pwd_conf_info'),
407    'L_ADMIN_EMAIL'=>l10n('conf_mail_webmaster'),
408    'L_ADMIN_EMAIL_INFO'=>l10n('conf_mail_webmaster_info'),
409    'L_SUBMIT'=>l10n('Start_Install'),
410    'L_INSTALL_HELP'=>sprintf(l10n('install_help'), 'http://forum.'.PHPWG_DOMAIN.'/'),
411    'L_ERR_COPY'=>l10n('step1_err_copy'),
412    'L_END_TITLE'=>l10n('install_end_title'),
413    'L_END_MESSAGE'=>l10n('install_end_message'),
[2127]414
[801]415    'F_ACTION'=>'install.php',
[529]416    'F_DB_HOST'=>$dbhost,
417    'F_DB_USER'=>$dbuser,
418    'F_DB_NAME'=>$dbname,
[1147]419    'F_DB_PREFIX' => (
420      $table_prefix != DEFAULT_PREFIX_TABLE
421      ? $table_prefix
422      : DEFAULT_PREFIX_TABLE
423      ),
[529]424    'F_ADMIN'=>$admin_name,
425    'F_ADMIN_EMAIL'=>$admin_mail,
426    'F_LANG_SELECT'=>language_select($language),
[2127]427
428    'T_CONTENT_ENCODING' => 'utf-8'
[529]429    ));
430
431//------------------------------------------------------ errors & infos display
[367]432if ( sizeof( $errors ) != 0 )
433{
434  $template->assign_block_vars('errors',array());
435  for ( $i = 0; $i < sizeof( $errors ); $i++ )
[218]436  {
[367]437    $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
[218]438  }
[367]439}
[218]440
[367]441if ( sizeof( $infos ) != 0 )
442{
443  $template->assign_block_vars('infos',array());
444  for ( $i = 0; $i < sizeof( $infos ); $i++ )
[218]445  {
[367]446    $template->assign_block_vars('infos.info',array('INFO'=>$infos[$i]));
[218]447  }
[367]448}
[218]449
[367]450if ($step ==1)
451{
452  $template->assign_block_vars('install',array());
[218]453}
454else
455{
[367]456  $template->assign_block_vars('install_end',array());
[218]457}
[367]458
[218]459//----------------------------------------------------------- html code display
[367]460$template->pparse('install');
[362]461?>
Note: See TracBrowser for help on using the repository browser.