source: trunk/install.php @ 5338

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

Feature 1525 : propose to download config database file when permissions for writing it to local/config directory are missing.

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