source: trunk/install.php @ 4410

Last change on this file since 4410 was 4410, checked in by nikrou, 14 years ago

Feature 1255 :

  • add postgres database engine
  • change installation process to allow postgres or mysql database
  • Property svn:eol-style set to LF
File size: 13.0 KB
RevLine 
[218]1<?php
[354]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3049]5// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[218]23
[367]24//----------------------------------------------------------- include
25define('PHPWG_ROOT_PATH','./');
[345]26
[4410]27include(PHPWG_ROOT_PATH . 'admin/include/functions_install.inc.php');
[382]28
[3747]29@set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
[367]30//
31// addslashes to vars if magic_quotes_gpc is off this is a security
32// precaution to prevent someone trying to break out of a SQL statement.
33//
[4006]34if( !@get_magic_quotes_gpc() )
[218]35{
[367]36  if( is_array($_POST) )
[218]37  {
[367]38    while( list($k, $v) = each($_POST) )
[218]39    {
[367]40      if( is_array($_POST[$k]) )
[218]41      {
[367]42        while( list($k2, $v2) = each($_POST[$k]) )
43        {
44          $_POST[$k][$k2] = addslashes($v2);
45        }
46        @reset($_POST[$k]);
[218]47      }
48      else
49      {
[367]50        $_POST[$k] = addslashes($v);
[218]51      }
52    }
[367]53    @reset($_POST);
54  }
55
[1855]56  if( is_array($_GET) )
57  {
58    while( list($k, $v) = each($_GET) )
59    {
60      if( is_array($_GET[$k]) )
61      {
62        while( list($k2, $v2) = each($_GET[$k]) )
63        {
64          $_GET[$k][$k2] = addslashes($v2);
65        }
66        @reset($_GET[$k]);
67      }
68      else
69      {
70        $_GET[$k] = addslashes($v);
71      }
72    }
73    @reset($_GET);
74  }
75
[367]76  if( is_array($_COOKIE) )
77  {
78    while( list($k, $v) = each($_COOKIE) )
[218]79    {
[367]80      if( is_array($_COOKIE[$k]) )
[218]81      {
[367]82        while( list($k2, $v2) = each($_COOKIE[$k]) )
83        {
84          $_COOKIE[$k][$k2] = addslashes($v2);
85        }
86        @reset($_COOKIE[$k]);
[218]87      }
88      else
89      {
[367]90        $_COOKIE[$k] = addslashes($v);
[218]91      }
92    }
[367]93    @reset($_COOKIE);
[218]94  }
[367]95}
[218]96
[367]97//----------------------------------------------------- variable initialization
[1147]98
[2339]99define('DEFAULT_PREFIX_TABLE', 'piwigo_');
[1147]100
[367]101// Obtain various vars
102$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
103$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
104$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
105$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
[4410]106$dblayer = (!empty($_POST['dblayer'])) ? $_POST['dblayer'] : 'mysql';
[367]107
[1147]108if (isset($_POST['install']))
109{
110  $table_prefix = $_POST['prefix'];
111}
112else
113{
114  $table_prefix = DEFAULT_PREFIX_TABLE;
115}
[367]116
117$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
118$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
119$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
120$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
121
122$infos = array();
123$errors = array();
124
125// Open config.php ... if it exists
[4280]126$config_file = PHPWG_ROOT_PATH.'include/config_database.inc.php';
[367]127if (@file_exists($config_file))
128{
[529]129  include($config_file);
[2339]130  // Is Piwigo already installed ?
[529]131  if (defined("PHPWG_INSTALLED"))
132  {
[2339]133    die('Piwigo is already installed');
[529]134  }
[367]135}
136
[682]137$prefixeTable = $table_prefix;
[819]138include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
[1079]139@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
[4410]140include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$dblayer.'.inc.php');
[529]141include(PHPWG_ROOT_PATH . 'include/constants.php');
142include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
[1221]143include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
[2102]144include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
[529]145
[2248]146if (isset($_GET['language']))
[405]147{
[2248]148  $language = strip_tags($_GET['language']);
[367]149}
[2127]150else
[1855]151{
[2127]152  $language = 'en_UK';
[2248]153  // Try to get browser language
154  foreach (get_languages('utf-8') as $language_code => $language_name)
155  {
156    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
157    {
158      $language = $language_code;
159      break;
160    }
161  }
[1855]162}
[367]163
[3197]164if ('fr_FR' == $language) {
165  define('PHPWG_DOMAIN', 'fr.piwigo.org');
166}
[4047]167else if ('de_DE' == $language) {
168  define('PHPWG_DOMAIN', 'de.piwigo.org');
169}
170else if ('es_ES' == $language) {
171  define('PHPWG_DOMAIN', 'es.piwigo.org');
172}
[3197]173else {
174  define('PHPWG_DOMAIN', 'piwigo.org');
175}
176define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
177
[2479]178load_language( 'common.lang', '', array('language'=>$language, 'target_charset'=>'utf-8') );
179load_language( 'admin.lang', '', array('language'=>$language, 'target_charset'=>'utf-8') );
180load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8') );
[529]181
[3203]182//------------------------------------------------- check php version
183if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
184{
185  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
186}
187
[367]188//----------------------------------------------------- template initialization
[3203]189include( PHPWG_ROOT_PATH .'include/template.class.php');
[4410]190$template = new Template(PHPWG_ROOT_PATH.'admin/template/goto', 'roma');
[367]191$template->set_filenames( array('install'=>'install.tpl') );
192$step = 1;
[529]193//---------------------------------------------------------------- form analyze
[367]194if ( isset( $_POST['install'] ))
195{
[4410]196  if (($pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'], 
197                                     $_POST['dbpasswd'], $_POST['dbname']))!==false) 
[382]198  {
[4410]199
200    array_push( $infos, l10n('step1_confirmation') );
201
202    $required_version = constant('REQUIRED_'.strtoupper($conf['dblayer']).'_VERSION');
203    if ( version_compare(pwg_get_db_version(), $required_version, '>=') )
[367]204    {
[4410]205      $pwg_charset = 'utf-8';
206      $pwg_db_charset = 'utf8';
207      if ($dblayer=='mysql')
208      {
209        $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
210      }
211      else 
212      {
213        $install_charset_collate = '';
214      }
[218]215    }
[367]216    else
217    {
[4410]218      $pwg_charset = 'iso-8859-1';
219      $pwg_db_charset = 'latin1';
[2127]220      $install_charset_collate = '';
221      if ( !array_key_exists($language, get_languages($pwg_charset) ) )
222      {
223        $language='en_UK';
224      }
225    }
[382]226  }
227  else
228  {
[2201]229    array_push( $errors, l10n('step1_err_server') );
[382]230  }
[2127]231
[382]232  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
233  if ( empty($webmaster))
[2201]234    array_push( $errors, l10n('step2_err_login1') );
[382]235  else if ( preg_match( '/[\'"]/', $webmaster ) )
[2201]236    array_push( $errors, l10n('step2_err_login3') );
[382]237  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
[2201]238    array_push( $errors, l10n('step2_err_pass') );
[382]239  if ( empty($admin_mail))
[2201]240    array_push( $errors, l10n('reg_err_mail_address') );
[2127]241  else
[382]242  {
[2124]243    $error_mail_address = validate_mail_address(null, $admin_mail);
[382]244    if (!empty($error_mail_address))
245      array_push( $errors, $error_mail_address );
246  }
[2127]247
[382]248  if ( count( $errors ) == 0 )
249  {
250    $step = 2;
[1147]251    $file_content = '<?php
[4410]252$conf[\'dblayer\'] = \''.$dblayer.'\';
[4280]253$conf[\'db_base\'] = \''.$dbname.'\';
254$conf[\'db_user\'] = \''.$dbuser.'\';
255$conf[\'db_password\'] = \''.$dbpasswd.'\';
256$conf[\'db_host\'] = \''.$dbhost.'\';
[1147]257
258$prefixeTable = \''.$table_prefix.'\';
259
260define(\'PHPWG_INSTALLED\', true);
[2127]261define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
262define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
263define(\'DB_COLLATE\', \'\');
264
[1147]265?'.'>';
[2127]266
[382]267    @umask(0111);
268    // writing the configuration file
269    if ( !($fp = @fopen( $config_file, 'w' )))
[367]270    {
[382]271      $html_content = htmlentities( $file_content, ENT_QUOTES );
272      $html_content = nl2br( $html_content );
[2747]273      $error_copy = l10n('step1_err_copy');
[3185]274      $error_copy .= '<br>--------------------------------------------------------------------<br>';
[2747]275      $error_copy .= '<span class="sql_content">' . $html_content . '</span>';
[3185]276      $error_copy .= '<br>--------------------------------------------------------------------<br>';
[382]277    }
278    @fputs($fp, $file_content, strlen($file_content));
279    @fclose($fp);
[2127]280
[2189]281    // Create empty local files to avoid log errors
282    create_empty_local_files();
283
[2339]284    // tables creation, based on piwigo_structure.sql
[1147]285    execute_sqlfile(
[4410]286      PHPWG_ROOT_PATH.'install/piwigo_structure-'.$dblayer.'.sql',
[1147]287      DEFAULT_PREFIX_TABLE,
288      $table_prefix
289      );
[382]290    // We fill the tables with basic informations
[1147]291    execute_sqlfile(
292      PHPWG_ROOT_PATH.'install/config.sql',
293      DEFAULT_PREFIX_TABLE,
294      $table_prefix
295      );
[218]296
[4410]297    $query = '
298INSERT INTO piwigo_config (param,value,comment)
299   VALUES (\'secret_key\',\'md5('.pwg_db_cast_to_text(DB_RANDOM_FUNCTION.'()').')\',
300   \'a secret key specific to the gallery for internal use\');';
301    pwg_query($query);
302
[1284]303    // fill $conf global array
304    load_conf_from_db();
305
306    $insert = array(
307      'id' => 1,
308      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
309      );
310    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
[2127]311
[382]312    // webmaster admin user
[1284]313    $inserts = array(
314      array(
315        'id'           => 1,
316        'username'     => $admin_name,
317        'password'     => md5($admin_pass1),
318        'mail_address' => $admin_mail,
319        ),
320      array(
321        'id'           => 2,
322        'username'     => 'guest',
323        ),
324      );
325    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
[801]326
[1930]327    create_user_infos(array(1,2), array('language' => $language));
[808]328
[1027]329    // Available upgrades must be ignored after a fresh installation. To
330    // make PWG avoid upgrading, we must tell it upgrades have already been
331    // made.
[4325]332    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[1221]333    define('CURRENT_DATE', $dbnow);
334    $datas = array();
[1027]335    foreach (get_available_upgrade_ids() as $upgrade_id)
336    {
[1221]337      array_push(
338        $datas,
339        array(
340          'id'          => $upgrade_id,
341          'applied'     => CURRENT_DATE,
342          'description' => 'upgrade included in installation',
343          )
344        );
[1027]345    }
[1221]346    mass_inserts(
347      UPGRADE_TABLE,
348      array_keys($datas[0]),
349      $datas
350      );
[382]351  }
[367]352}
[218]353
[2248]354//------------------------------------------------------ start template output
[4410]355$dbengines = available_engines();
356
[2248]357foreach (get_languages('utf-8') as $language_code => $language_name)
358{
359  if ($language == $language_code)
360  {
361    $template->assign('language_selection', $language_code);
362  }
363  $languages_options[$language_code] = $language_name;
364}
365$template->assign('language_options', $languages_options);
366
367$template->assign(
[529]368  array(
[2248]369    'T_CONTENT_ENCODING' => 'utf-8',
[4410]370    'RELEASE' => PHPWG_VERSION,
[2248]371    'F_ACTION' => 'install.php?language=' . $language,
[4410]372    'F_DB_ENGINES' => $dbengines,
373    'F_DB_LAYER' => $dblayer,
374    'F_DB_HOST' => $dbhost,
375    'F_DB_USER' => $dbuser,
376    'F_DB_NAME' => $dbname,
[2248]377    'F_DB_PREFIX' => $table_prefix,
[4410]378    'F_ADMIN' => $admin_name,
379    'F_ADMIN_EMAIL' => $admin_mail,
380    'L_INSTALL_HELP' => sprintf(l10n('install_help'), PHPWG_URL.'/forum'),
[529]381    ));
382
383//------------------------------------------------------ errors & infos display
[2747]384if ($step == 1)
385{
386  $template->assign('install', true);
387}
388else
389{
390  array_push($infos, l10n('install_end_message'));
391
392  if (isset($error_copy))
393  {
394    array_push($errors, $error_copy);
395  }
[3715]396  else
397  {
398    session_set_save_handler('pwg_session_open',
399      'pwg_session_close',
400      'pwg_session_read',
401      'pwg_session_write',
402      'pwg_session_destroy',
403      'pwg_session_gc'
404    );
405    if ( function_exists('ini_set') )
406    {
407      ini_set('session.use_cookies', $conf['session_use_cookies']);
408      ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
409      ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
410      ini_set('session.cookie_httponly', 1);
411    }
412    session_name($conf['session_name']);
413    session_set_cookie_params(0, cookie_path());
414    $user = build_user(1, true);
415    log_user($user['id'], false);
416  }
[3382]417
418  $template->assign(
419    'SUBSCRIBE_BASE_URL',
420    get_newsletter_subscribe_base_url($language)
421    );
[2747]422}
[2248]423if (count($errors) != 0)
[367]424{
[2248]425  $template->assign('errors', $errors);
[367]426}
[218]427
[2248]428if (count($infos) != 0 )
[367]429{
[2248]430  $template->assign('infos', $infos);
[367]431}
[218]432
433//----------------------------------------------------------- html code display
[367]434$template->pparse('install');
[3203]435?>
Note: See TracBrowser for help on using the repository browser.