source: trunk/install.php @ 5078

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

Feature 1450 :

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