source: trunk/install.php @ 2172

Last change on this file since 2172 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
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 2127 2007-10-09 01:43:29Z rvelices $
[354]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// +-----------------------------------------------------------------------+
[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
[2102]212// Create empty local files to avoid log errors
213create_empty_local_files();
214
[2127]215if ( isset( $_REQUEST['language'] ))
[405]216{
[2127]217  $language = strip_tags($_REQUEST['language']);
[367]218}
[2127]219else
[1855]220{
[2127]221  $language = 'en_UK';
[1855]222}
[367]223
[2127]224load_language( 'common.lang', '', $language, false, 'utf-8' );
225load_language( 'admin.lang', '', $language, false, 'utf-8' );
226load_language( 'install.lang', '', $language, false, 'utf-8' );
[529]227
[367]228//----------------------------------------------------- template initialization
[2127]229$template=new Template(PHPWG_ROOT_PATH.'template/yoga');
[367]230$template->set_filenames( array('install'=>'install.tpl') );
231$step = 1;
[529]232//---------------------------------------------------------------- form analyze
[367]233if ( isset( $_POST['install'] ))
234{
[382]235  if ( @mysql_connect( $_POST['dbhost'],
236                       $_POST['dbuser'],
237                       $_POST['dbpasswd'] ) )
238  {
239    if ( @mysql_select_db($_POST['dbname'] ) )
[367]240    {
[382]241      array_push( $infos, $lang['step1_confirmation'] );
[218]242    }
[367]243    else
244    {
[382]245      array_push( $errors, $lang['step1_err_db'] );
[367]246    }
[2127]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    }
[382]263  }
264  else
265  {
266    array_push( $errors, $lang['step1_err_server'] );
267  }
[2127]268
[382]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'] );
[2127]278  else
[382]279  {
[2124]280    $error_mail_address = validate_mail_address(null, $admin_mail);
[382]281    if (!empty($error_mail_address))
282      array_push( $errors, $error_mail_address );
283  }
[2127]284
[382]285  if ( count( $errors ) == 0 )
286  {
287    $step = 2;
[1147]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);
[2127]297define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
298define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
299define(\'DB_COLLATE\', \'\');
300
[1147]301?'.'>';
[2127]302
[382]303    @umask(0111);
304    // writing the configuration file
305    if ( !($fp = @fopen( $config_file, 'w' )))
[367]306    {
[382]307      $html_content = htmlentities( $file_content, ENT_QUOTES );
308      $html_content = nl2br( $html_content );
[1284]309      $template->assign_block_vars(
310        'error_copy',
311        array(
312          'FILE_CONTENT' => $html_content,
313          )
314        );
[382]315    }
316    @fputs($fp, $file_content, strlen($file_content));
317    @fclose($fp);
[2127]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
[529]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'],
[1726]410    'L_INSTALL_HELP'=>sprintf($lang['install_help'], 'http://forum.'.PHPWG_DOMAIN.'/'),
[529]411    'L_ERR_COPY'=>$lang['step1_err_copy'],
412    'L_END_TITLE'=>$lang['install_end_title'],
413    'L_END_MESSAGE'=>$lang['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.