source: trunk/install.php @ 5742

Last change on this file since 5742 was 5571, checked in by plg, 14 years ago

Improve the install confirmation page: dedicated button to download the
database.inc.php file.

File size: 16.4 KB
RevLine 
[218]1<?php
[354]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 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
[3747]27@set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
[367]28//
29// addslashes to vars if magic_quotes_gpc is off this is a security
30// precaution to prevent someone trying to break out of a SQL statement.
31//
[4006]32if( !@get_magic_quotes_gpc() )
[218]33{
[367]34  if( is_array($_POST) )
[218]35  {
[367]36    while( list($k, $v) = each($_POST) )
[218]37    {
[367]38      if( is_array($_POST[$k]) )
[218]39      {
[367]40        while( list($k2, $v2) = each($_POST[$k]) )
41        {
42          $_POST[$k][$k2] = addslashes($v2);
43        }
44        @reset($_POST[$k]);
[218]45      }
46      else
47      {
[367]48        $_POST[$k] = addslashes($v);
[218]49      }
50    }
[367]51    @reset($_POST);
52  }
53
[1855]54  if( is_array($_GET) )
55  {
56    while( list($k, $v) = each($_GET) )
57    {
58      if( is_array($_GET[$k]) )
59      {
60        while( list($k2, $v2) = each($_GET[$k]) )
61        {
62          $_GET[$k][$k2] = addslashes($v2);
63        }
64        @reset($_GET[$k]);
65      }
66      else
67      {
68        $_GET[$k] = addslashes($v);
69      }
70    }
71    @reset($_GET);
72  }
73
[367]74  if( is_array($_COOKIE) )
75  {
76    while( list($k, $v) = each($_COOKIE) )
[218]77    {
[367]78      if( is_array($_COOKIE[$k]) )
[218]79      {
[367]80        while( list($k2, $v2) = each($_COOKIE[$k]) )
81        {
82          $_COOKIE[$k][$k2] = addslashes($v2);
83        }
84        @reset($_COOKIE[$k]);
[218]85      }
86      else
87      {
[367]88        $_COOKIE[$k] = addslashes($v);
[218]89      }
90    }
[367]91    @reset($_COOKIE);
[218]92  }
[367]93}
[218]94
[367]95//----------------------------------------------------- variable initialization
[1147]96
[2339]97define('DEFAULT_PREFIX_TABLE', 'piwigo_');
[1147]98
[5239]99// default database engine proposed if severals are available
100// choices : sqlite, mysql, pgsql, pdo-sqlite
101// see include/dblayer/dblayers.inc.php
102define('DEFAULT_DB_ENGINE', 'mysql');
103
104// database engine default choice between sqlite (native or via pdo)
105// if the twice are available.
106define('DEFAULT_DB_SQLITE', 'native'); 
107
[1147]108if (isset($_POST['install']))
109{
[5220]110  $prefixeTable = $_POST['prefix'];
[1147]111}
112else
113{
[5220]114  $prefixeTable = DEFAULT_PREFIX_TABLE;
[1147]115}
[367]116
[5220]117include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
118@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
119
[5266]120// download database config file if exists
121if (!empty($_GET['dl']) && file_exists($conf['local_data_dir'].'/pwg_'.$_GET['dl']))
122{
123  $filename = $conf['local_data_dir'].'/pwg_'.$_GET['dl'];
124  header('Cache-Control: no-cache, must-revalidate');
125  header('Pragma: no-cache');
126  header('Content-Disposition: attachment; filename="database.inc.php"');
127  header('Content-Transfer-Encoding: binary');
128  header('Content-Length: '.filesize($filename));
129  echo file_get_contents($filename);
130  unlink($filename);
131  exit();
132} 
133
[5220]134// Obtain various vars
135$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
136$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
137$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
138$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
[5239]139$dblayer = (!empty($_POST['dblayer'])) ? $_POST['dblayer'] : DEFAULT_DB_ENGINE;
[5220]140
[367]141$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
142$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
143$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
144$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
145
146$infos = array();
147$errors = array();
148
[5213]149// database config file migration : mysql.inc.php et database.inc.php
[4423]150$old_config_file = PHPWG_ROOT_PATH . 'include/mysql.inc.php';
[5213]151$config_file = PHPWG_ROOT_PATH . 'local/config/database.inc.php';
[4423]152if (!file_exists($config_file) && file_exists($old_config_file))
153{
154  $step = 3;
155  include $old_config_file;
156  $file_content = '<?php
157$conf[\'dblayer\'] = \'mysql\';
158$conf[\'db_base\'] = \''.$cfgBase.'\';
159$conf[\'db_user\'] = \''.$cfgUser.'\';
160$conf[\'db_password\'] = \''.$cfgPassword.'\';
161$conf[\'db_host\'] = \''.$cfgHote.'\';
162
163$prefixeTable = \''.$prefixeTable.'\';
164
165define(\'PHPWG_INSTALLED\', true);
166define(\'PWG_CHARSET\', \''.PWG_CHARSET.'\');
167define(\'DB_CHARSET\', \''.DB_CHARSET.'\');
168define(\'DB_COLLATE\', \''.DB_COLLATE.'\');
169
170?'.'>';
171}
[367]172// Open config.php ... if it exists
[4423]173elseif (@file_exists($config_file))
[367]174{
[529]175  include($config_file);
[2339]176  // Is Piwigo already installed ?
[529]177  if (defined("PHPWG_INSTALLED"))
178  {
[2339]179    die('Piwigo is already installed');
[529]180  }
[367]181}
182
[529]183include(PHPWG_ROOT_PATH . 'include/constants.php');
184include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
[1221]185include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
[529]186
[5357]187include(PHPWG_ROOT_PATH . 'admin/include/languages.class.php');
188$languages = new languages('utf-8');
189
[2248]190if (isset($_GET['language']))
[405]191{
[2248]192  $language = strip_tags($_GET['language']);
[367]193}
[2127]194else
[1855]195{
[2127]196  $language = 'en_UK';
[2248]197  // Try to get browser language
[5357]198  foreach ($languages->fs_languages as $language_code => $language_name)
[2248]199  {
200    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
201    {
202      $language = $language_code;
203      break;
204    }
205  }
[1855]206}
[367]207
[3197]208if ('fr_FR' == $language) {
209  define('PHPWG_DOMAIN', 'fr.piwigo.org');
210}
[5234]211else if ('it_IT' == $language) {
212  define('PHPWG_DOMAIN', 'it.piwigo.org');
213}
[4047]214else if ('de_DE' == $language) {
215  define('PHPWG_DOMAIN', 'de.piwigo.org');
216}
217else if ('es_ES' == $language) {
218  define('PHPWG_DOMAIN', 'es.piwigo.org');
219}
[4816]220else if ('pl_PL' == $language) {
221  define('PHPWG_DOMAIN', 'pl.piwigo.org');
222}
223else if ('zh_CN' == $language) {
224  define('PHPWG_DOMAIN', 'cn.piwigo.org');
225}
[5234]226else if ('hu_HU_CN' == $language) {
227  define('PHPWG_DOMAIN', 'hu.piwigo.org');
228}
229else if ('ru_RU_CN' == $language) {
230  define('PHPWG_DOMAIN', 'ru.piwigo.org');
231}
[3197]232else {
233  define('PHPWG_DOMAIN', 'piwigo.org');
234}
235define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
236
[4454]237if (empty($step) || ($step != 3))
[4423]238{
239  load_language('common.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
240  load_language('admin.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
241  load_language('install.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
242}
[4463]243header('Content-Type: text/html; charset=UTF-8');
[3203]244//------------------------------------------------- check php version
245if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
246{
247  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
248}
249
[367]250//----------------------------------------------------- template initialization
[3203]251include( PHPWG_ROOT_PATH .'include/template.class.php');
[5123]252$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'roma');
[4423]253$template->set_filenames( array('install' => 'install.tpl') );
254if (!isset($step))
255{
256  $step = 1;
257}
[529]258//---------------------------------------------------------------- form analyze
[5384]259include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$dblayer.'.inc.php');
260include(PHPWG_ROOT_PATH . 'admin/include/functions_install.inc.php');
[5387]261include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
[5384]262
[367]263if ( isset( $_POST['install'] ))
264{
[5387]265  if (install_db_connect($infos, $errors))
[382]266  {
[4454]267    $required_version = constant('REQUIRED_'.strtoupper($dblayer).'_VERSION');
[4410]268    if ( version_compare(pwg_get_db_version(), $required_version, '>=') )
[367]269    {
[4410]270      $pwg_charset = 'utf-8';
271      $pwg_db_charset = 'utf8';
272      if ($dblayer=='mysql')
273      {
[5341]274        $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
275        pwg_query('SET NAMES "'.$pwg_db_charset.'"');
[4410]276      }
277      else 
278      {
[5341]279        $install_charset_collate = '';
[4410]280      }
[218]281    }
[367]282    else
283    {
[4410]284      $pwg_charset = 'iso-8859-1';
285      $pwg_db_charset = 'latin1';
[2127]286      $install_charset_collate = '';
[5357]287      if ( !array_key_exists($language, $languages->get_fs_languages($pwg_charset) ) )
[2127]288      {
289        $language='en_UK';
290      }
291    }
[382]292  }
[5384]293
[382]294  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
295  if ( empty($webmaster))
[5021]296    array_push( $errors, l10n('enter a login for webmaster') );
[382]297  else if ( preg_match( '/[\'"]/', $webmaster ) )
[5207]298    array_push( $errors, l10n('webmaster login can\'t contain characters \' or "') );
[382]299  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
[5021]300    array_push( $errors, l10n('please enter your password again') );
[382]301  if ( empty($admin_mail))
[5021]302    array_push( $errors, l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)') );
[2127]303  else
[382]304  {
[2124]305    $error_mail_address = validate_mail_address(null, $admin_mail);
[382]306    if (!empty($error_mail_address))
307      array_push( $errors, $error_mail_address );
308  }
[2127]309
[382]310  if ( count( $errors ) == 0 )
311  {
312    $step = 2;
[1147]313    $file_content = '<?php
[4410]314$conf[\'dblayer\'] = \''.$dblayer.'\';
[4280]315$conf[\'db_base\'] = \''.$dbname.'\';
316$conf[\'db_user\'] = \''.$dbuser.'\';
317$conf[\'db_password\'] = \''.$dbpasswd.'\';
318$conf[\'db_host\'] = \''.$dbhost.'\';
[1147]319
[5220]320$prefixeTable = \''.$prefixeTable.'\';
[1147]321
322define(\'PHPWG_INSTALLED\', true);
[2127]323define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
324define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
325define(\'DB_COLLATE\', \'\');
326
[1147]327?'.'>';
[2127]328
[382]329    @umask(0111);
330    // writing the configuration file
331    if ( !($fp = @fopen( $config_file, 'w' )))
[367]332    {
[5266]333      $tmp_filename = md5(uniqid(time()));
334      $fh = @fopen( $conf['local_data_dir'] . '/pwg_' . $tmp_filename, 'w' );
335      @fputs($fh, $file_content, strlen($file_content));
336      @fclose($fh);
337
[5571]338      $template->assign(
339        array(
340          'config_creation_failed' => true,
341          'config_url' => 'install.php?dl='.$tmp_filename,
342          'config_file_content' => $file_content,
343          )
[5266]344        );
[382]345    }
346    @fputs($fp, $file_content, strlen($file_content));
347    @fclose($fp);
[2127]348
[2339]349    // tables creation, based on piwigo_structure.sql
[1147]350    execute_sqlfile(
[4410]351      PHPWG_ROOT_PATH.'install/piwigo_structure-'.$dblayer.'.sql',
[1147]352      DEFAULT_PREFIX_TABLE,
[5220]353      $prefixeTable
[1147]354      );
[382]355    // We fill the tables with basic informations
[1147]356    execute_sqlfile(
357      PHPWG_ROOT_PATH.'install/config.sql',
358      DEFAULT_PREFIX_TABLE,
[5220]359      $prefixeTable
[1147]360      );
[218]361
[4410]362    $query = '
[5220]363INSERT INTO '.$prefixeTable.'config (param,value,comment)
[4410]364   VALUES (\'secret_key\',\'md5('.pwg_db_cast_to_text(DB_RANDOM_FUNCTION.'()').')\',
365   \'a secret key specific to the gallery for internal use\');';
366    pwg_query($query);
367
[5341]368    // fill languages table
[5357]369    foreach ($languages->get_fs_languages($pwg_charset) as $language_code => $language_name)
[5341]370    {
[5357]371      $languages->perform_action('activate', $language_code);
[5341]372    }
373
[1284]374    // fill $conf global array
375    load_conf_from_db();
376
[5340]377    // PWG_CHARSET is required for building the fs_themes array in the
378    // themes class
[5452]379    if (!defined('PWG_CHARSET'))
380    {
381      define('PWG_CHARSET', $pwg_charset);
382    }
[5340]383    activate_all_themes();
384
[1284]385    $insert = array(
386      'id' => 1,
387      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
388      );
389    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
[2127]390
[382]391    // webmaster admin user
[1284]392    $inserts = array(
393      array(
394        'id'           => 1,
395        'username'     => $admin_name,
396        'password'     => md5($admin_pass1),
397        'mail_address' => $admin_mail,
398        ),
399      array(
400        'id'           => 2,
401        'username'     => 'guest',
402        ),
403      );
404    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
[801]405
[1930]406    create_user_infos(array(1,2), array('language' => $language));
[808]407
[1027]408    // Available upgrades must be ignored after a fresh installation. To
409    // make PWG avoid upgrading, we must tell it upgrades have already been
410    // made.
[4325]411    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[1221]412    define('CURRENT_DATE', $dbnow);
413    $datas = array();
[1027]414    foreach (get_available_upgrade_ids() as $upgrade_id)
415    {
[1221]416      array_push(
417        $datas,
418        array(
419          'id'          => $upgrade_id,
420          'applied'     => CURRENT_DATE,
421          'description' => 'upgrade included in installation',
422          )
423        );
[1027]424    }
[1221]425    mass_inserts(
426      UPGRADE_TABLE,
427      array_keys($datas[0]),
428      $datas
429      );
[382]430  }
[367]431}
[218]432
[2248]433//------------------------------------------------------ start template output
[4423]434if ($step == 3)
435{
436  @umask(0111);
437  // writing the new configuration file
438  if ( !($fp = @fopen( $config_file, 'w' )))
439  {
440    $html_content = htmlentities( $file_content, ENT_QUOTES );
441    $html_content = nl2br( $html_content );
[5571]442   
443    $error_copy = l10n('An alternate solution is to copy the text in the box above and paste it into the file "local/config/database.inc.php" (Warning : database.inc.php must only contain what is in the textarea, no line return or space character)');
444    $error_copy .= '<br><br>';
445    $error_copy .= '<textarea rows="15" cols="70">'.$html_content.'</textarea>';
[4423]446  } 
447  else 
448  {
449    @fputs($fp, $file_content, strlen($file_content));
450    @fclose($fp);
[4410]451
[4423]452    @unlink($old_config_file);
453    header("Location: index.php");
454    exit();
455  }
456
457  $template->assign(
458    array(
459      'T_CONTENT_ENCODING' => 'utf-8',
460      'migration' => true
[5571]461      )
462    );
[4423]463}
464else
[2248]465{
[4423]466  $dbengines = available_engines();
467
[5357]468  foreach ($languages->fs_languages as $language_code => $language_name)
[2248]469  {
[4423]470    if ($language == $language_code)
471    {
472      $template->assign('language_selection', $language_code);
473    }
474    $languages_options[$language_code] = $language_name;
[2248]475  }
[4423]476  $template->assign('language_options', $languages_options);
477
478  $template->assign(
479    array(
480      'T_CONTENT_ENCODING' => 'utf-8',
481      'RELEASE' => PHPWG_VERSION,
482      'F_ACTION' => 'install.php?language=' . $language,
483      'F_DB_ENGINES' => $dbengines,
484      'F_DB_LAYER' => $dblayer,
485      'F_DB_HOST' => $dbhost,
486      'F_DB_USER' => $dbuser,
487      'F_DB_NAME' => $dbname,
[5220]488      'F_DB_PREFIX' => $prefixeTable,
[4423]489      'F_ADMIN' => $admin_name,
490      'F_ADMIN_EMAIL' => $admin_mail,
[5021]491      'L_INSTALL_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
[4423]492      ));
[2248]493}
494
[529]495//------------------------------------------------------ errors & infos display
[2747]496if ($step == 1)
497{
498  $template->assign('install', true);
499}
[4423]500elseif ($step == 3)
501{
502  if (isset($error_copy))
503  {
504    array_push($errors, $error_copy);
505  }
506}
[2747]507else
508{
[5408]509  array_push(
510    $infos,
511    l10n('Congratulations, Piwigo installation is completed')
512    );
[2747]513
514  if (isset($error_copy))
515  {
516    array_push($errors, $error_copy);
517  }
[3715]518  else
519  {
520    session_set_save_handler('pwg_session_open',
521      'pwg_session_close',
522      'pwg_session_read',
523      'pwg_session_write',
524      'pwg_session_destroy',
525      'pwg_session_gc'
526    );
527    if ( function_exists('ini_set') )
528    {
529      ini_set('session.use_cookies', $conf['session_use_cookies']);
530      ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
531      ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
532      ini_set('session.cookie_httponly', 1);
533    }
534    session_name($conf['session_name']);
535    session_set_cookie_params(0, cookie_path());
536    $user = build_user(1, true);
537    log_user($user['id'], false);
538  }
[3382]539
540  $template->assign(
541    'SUBSCRIBE_BASE_URL',
542    get_newsletter_subscribe_base_url($language)
543    );
[2747]544}
[2248]545if (count($errors) != 0)
[367]546{
[2248]547  $template->assign('errors', $errors);
[367]548}
[218]549
[2248]550if (count($infos) != 0 )
[367]551{
[2248]552  $template->assign('infos', $infos);
[367]553}
[218]554
555//----------------------------------------------------------- html code display
[367]556$template->pparse('install');
[3203]557?>
Note: See TracBrowser for help on using the repository browser.