source: trunk/install.php @ 5239

Last change on this file since 5239 was 5239, checked in by patdenice, 14 years ago

feature 1255: Move $confdb_sqlite_default and $confdbengine_select_default in install.php (as constants).
PDO::SQLite is now displayed as SQLite in install page.
Always display unique SQLite engine.

File size: 16.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
23
24//----------------------------------------------------------- include
25define('PHPWG_ROOT_PATH','./');
26
27include(PHPWG_ROOT_PATH . 'admin/include/functions_install.inc.php');
28
29@set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
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//
34if( !@get_magic_quotes_gpc() )
35{
36  if( is_array($_POST) )
37  {
38    while( list($k, $v) = each($_POST) )
39    {
40      if( is_array($_POST[$k]) )
41      {
42        while( list($k2, $v2) = each($_POST[$k]) )
43        {
44          $_POST[$k][$k2] = addslashes($v2);
45        }
46        @reset($_POST[$k]);
47      }
48      else
49      {
50        $_POST[$k] = addslashes($v);
51      }
52    }
53    @reset($_POST);
54  }
55
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
76  if( is_array($_COOKIE) )
77  {
78    while( list($k, $v) = each($_COOKIE) )
79    {
80      if( is_array($_COOKIE[$k]) )
81      {
82        while( list($k2, $v2) = each($_COOKIE[$k]) )
83        {
84          $_COOKIE[$k][$k2] = addslashes($v2);
85        }
86        @reset($_COOKIE[$k]);
87      }
88      else
89      {
90        $_COOKIE[$k] = addslashes($v);
91      }
92    }
93    @reset($_COOKIE);
94  }
95}
96
97//----------------------------------------------------- variable initialization
98
99define('DEFAULT_PREFIX_TABLE', 'piwigo_');
100
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
110if (isset($_POST['install']))
111{
112  $prefixeTable = $_POST['prefix'];
113}
114else
115{
116  $prefixeTable = DEFAULT_PREFIX_TABLE;
117}
118
119include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
120@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
121
122// Obtain various vars
123$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
124$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
125$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
126$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
127$dblayer = (!empty($_POST['dblayer'])) ? $_POST['dblayer'] : DEFAULT_DB_ENGINE;
128
129$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
130$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
131$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
132$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
133
134$infos = array();
135$errors = array();
136
137// database config file migration : mysql.inc.php et database.inc.php
138$old_config_file = PHPWG_ROOT_PATH . 'include/mysql.inc.php';
139$config_file = PHPWG_ROOT_PATH . 'local/config/database.inc.php';
140if (!file_exists($config_file) && file_exists($old_config_file))
141{
142  $step = 3;
143  include $old_config_file;
144  $file_content = '<?php
145$conf[\'dblayer\'] = \'mysql\';
146$conf[\'db_base\'] = \''.$cfgBase.'\';
147$conf[\'db_user\'] = \''.$cfgUser.'\';
148$conf[\'db_password\'] = \''.$cfgPassword.'\';
149$conf[\'db_host\'] = \''.$cfgHote.'\';
150
151$prefixeTable = \''.$prefixeTable.'\';
152
153define(\'PHPWG_INSTALLED\', true);
154define(\'PWG_CHARSET\', \''.PWG_CHARSET.'\');
155define(\'DB_CHARSET\', \''.DB_CHARSET.'\');
156define(\'DB_COLLATE\', \''.DB_COLLATE.'\');
157
158?'.'>';
159}
160// Open config.php ... if it exists
161elseif (@file_exists($config_file))
162{
163  include($config_file);
164  // Is Piwigo already installed ?
165  if (defined("PHPWG_INSTALLED"))
166  {
167    die('Piwigo is already installed');
168  }
169}
170
171include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$dblayer.'.inc.php');
172include(PHPWG_ROOT_PATH . 'include/constants.php');
173include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
174include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
175include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
176
177if (isset($_GET['language']))
178{
179  $language = strip_tags($_GET['language']);
180}
181else
182{
183  $language = 'en_UK';
184  // Try to get browser language
185  foreach (get_languages('utf-8') as $language_code => $language_name)
186  {
187    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
188    {
189      $language = $language_code;
190      break;
191    }
192  }
193}
194
195if ('fr_FR' == $language) {
196  define('PHPWG_DOMAIN', 'fr.piwigo.org');
197}
198else if ('it_IT' == $language) {
199  define('PHPWG_DOMAIN', 'it.piwigo.org');
200}
201else if ('de_DE' == $language) {
202  define('PHPWG_DOMAIN', 'de.piwigo.org');
203}
204else if ('es_ES' == $language) {
205  define('PHPWG_DOMAIN', 'es.piwigo.org');
206}
207else if ('pl_PL' == $language) {
208  define('PHPWG_DOMAIN', 'pl.piwigo.org');
209}
210else if ('zh_CN' == $language) {
211  define('PHPWG_DOMAIN', 'cn.piwigo.org');
212}
213else if ('hu_HU_CN' == $language) {
214  define('PHPWG_DOMAIN', 'hu.piwigo.org');
215}
216else if ('ru_RU_CN' == $language) {
217  define('PHPWG_DOMAIN', 'ru.piwigo.org');
218}
219else {
220  define('PHPWG_DOMAIN', 'piwigo.org');
221}
222define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
223
224if (empty($step) || ($step != 3))
225{
226  load_language('common.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
227  load_language('admin.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
228  load_language('install.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
229}
230header('Content-Type: text/html; charset=UTF-8');
231//------------------------------------------------- check php version
232if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
233{
234  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
235}
236
237//----------------------------------------------------- template initialization
238include( PHPWG_ROOT_PATH .'include/template.class.php');
239$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'roma');
240$template->set_filenames( array('install' => 'install.tpl') );
241if (!isset($step))
242{
243  $step = 1;
244}
245//---------------------------------------------------------------- form analyze
246if ( isset( $_POST['install'] ))
247{
248  try
249  {
250    $pwg_db_link = pwg_db_connect($_POST['dbhost'], $_POST['dbuser'], 
251                                  $_POST['dbpasswd'], $_POST['dbname']);
252 
253    array_push( $infos, l10n('Parameters are correct') );
254   
255    $required_version = constant('REQUIRED_'.strtoupper($dblayer).'_VERSION');
256    if ( version_compare(pwg_get_db_version(), $required_version, '>=') )
257    {
258      $pwg_charset = 'utf-8';
259      $pwg_db_charset = 'utf8';
260      if ($dblayer=='mysql')
261      {
262        $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
263      }
264      else 
265      {
266        $install_charset_collate = '';
267      }
268    }
269    else
270    {
271      $pwg_charset = 'iso-8859-1';
272      $pwg_db_charset = 'latin1';
273      $install_charset_collate = '';
274      if ( !array_key_exists($language, get_languages($pwg_charset) ) )
275      {
276        $language='en_UK';
277      }
278    }
279  }
280  catch (Exception $e)
281  {
282    array_push( $errors, l10n($e->getMessage()));
283  }
284  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
285  if ( empty($webmaster))
286    array_push( $errors, l10n('enter a login for webmaster') );
287  else if ( preg_match( '/[\'"]/', $webmaster ) )
288    array_push( $errors, l10n('webmaster login can\'t contain characters \' or "') );
289  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
290    array_push( $errors, l10n('please enter your password again') );
291  if ( empty($admin_mail))
292    array_push( $errors, l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)') );
293  else
294  {
295    $error_mail_address = validate_mail_address(null, $admin_mail);
296    if (!empty($error_mail_address))
297      array_push( $errors, $error_mail_address );
298  }
299
300  if ( count( $errors ) == 0 )
301  {
302    $step = 2;
303    $file_content = '<?php
304$conf[\'dblayer\'] = \''.$dblayer.'\';
305$conf[\'db_base\'] = \''.$dbname.'\';
306$conf[\'db_user\'] = \''.$dbuser.'\';
307$conf[\'db_password\'] = \''.$dbpasswd.'\';
308$conf[\'db_host\'] = \''.$dbhost.'\';
309
310$prefixeTable = \''.$prefixeTable.'\';
311
312define(\'PHPWG_INSTALLED\', true);
313define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
314define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
315define(\'DB_COLLATE\', \'\');
316
317?'.'>';
318
319    @umask(0111);
320    // writing the configuration file
321    if ( !($fp = @fopen( $config_file, 'w' )))
322    {
323      $html_content = htmlentities( $file_content, ENT_QUOTES );
324      $html_content = nl2br( $html_content );
325      $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)');
326      $error_copy .= '<br>--------------------------------------------------------------------<br>';
327      $error_copy .= '<span class="sql_content">' . $html_content . '</span>';
328      $error_copy .= '<br>--------------------------------------------------------------------<br>';
329    }
330    @fputs($fp, $file_content, strlen($file_content));
331    @fclose($fp);
332
333    // tables creation, based on piwigo_structure.sql
334    execute_sqlfile(
335      PHPWG_ROOT_PATH.'install/piwigo_structure-'.$dblayer.'.sql',
336      DEFAULT_PREFIX_TABLE,
337      $prefixeTable
338      );
339    // We fill the tables with basic informations
340    execute_sqlfile(
341      PHPWG_ROOT_PATH.'install/config.sql',
342      DEFAULT_PREFIX_TABLE,
343      $prefixeTable
344      );
345
346    $query = '
347INSERT INTO '.$prefixeTable.'config (param,value,comment)
348   VALUES (\'secret_key\',\'md5('.pwg_db_cast_to_text(DB_RANDOM_FUNCTION.'()').')\',
349   \'a secret key specific to the gallery for internal use\');';
350    pwg_query($query);
351
352    // fill $conf global array
353    load_conf_from_db();
354
355    $insert = array(
356      'id' => 1,
357      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
358      );
359    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
360
361    // webmaster admin user
362    $inserts = array(
363      array(
364        'id'           => 1,
365        'username'     => $admin_name,
366        'password'     => md5($admin_pass1),
367        'mail_address' => $admin_mail,
368        ),
369      array(
370        'id'           => 2,
371        'username'     => 'guest',
372        ),
373      );
374    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
375
376    create_user_infos(array(1,2), array('language' => $language));
377
378    // Available upgrades must be ignored after a fresh installation. To
379    // make PWG avoid upgrading, we must tell it upgrades have already been
380    // made.
381    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
382    define('CURRENT_DATE', $dbnow);
383    $datas = array();
384    foreach (get_available_upgrade_ids() as $upgrade_id)
385    {
386      array_push(
387        $datas,
388        array(
389          'id'          => $upgrade_id,
390          'applied'     => CURRENT_DATE,
391          'description' => 'upgrade included in installation',
392          )
393        );
394    }
395    mass_inserts(
396      UPGRADE_TABLE,
397      array_keys($datas[0]),
398      $datas
399      );
400  }
401}
402
403//------------------------------------------------------ start template output
404if ($step == 3)
405{
406  @umask(0111);
407  // writing the new configuration file
408  if ( !($fp = @fopen( $config_file, 'w' )))
409  {
410    $html_content = htmlentities( $file_content, ENT_QUOTES );
411    $html_content = nl2br( $html_content );
412    $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)');
413    $error_copy .= '<br>--------------------------------------------------------------------<br>';
414    $error_copy .= '<span class="sql_content">' . $html_content . '</span>';
415    $error_copy .= '<br>--------------------------------------------------------------------<br>';
416  } 
417  else 
418  {
419    @fputs($fp, $file_content, strlen($file_content));
420    @fclose($fp);
421
422    @unlink($old_config_file);
423    header("Location: index.php");
424    exit();
425  }
426
427  $template->assign(
428    array(
429      'T_CONTENT_ENCODING' => 'utf-8',
430      'migration' => true
431          ));
432}
433else
434{
435  $dbengines = available_engines();
436
437  foreach (get_languages('utf-8') as $language_code => $language_name)
438  {
439    if ($language == $language_code)
440    {
441      $template->assign('language_selection', $language_code);
442    }
443    $languages_options[$language_code] = $language_name;
444  }
445  $template->assign('language_options', $languages_options);
446
447  $template->assign(
448    array(
449      'T_CONTENT_ENCODING' => 'utf-8',
450      'RELEASE' => PHPWG_VERSION,
451      'F_ACTION' => 'install.php?language=' . $language,
452      'F_DB_ENGINES' => $dbengines,
453      'F_DB_LAYER' => $dblayer,
454      'F_DB_HOST' => $dbhost,
455      'F_DB_USER' => $dbuser,
456      'F_DB_NAME' => $dbname,
457      'F_DB_PREFIX' => $prefixeTable,
458      'F_ADMIN' => $admin_name,
459      'F_ADMIN_EMAIL' => $admin_mail,
460      'L_INSTALL_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
461      ));
462}
463
464//------------------------------------------------------ errors & infos display
465if ($step == 1)
466{
467  $template->assign('install', true);
468}
469elseif ($step == 3)
470{
471  if (isset($error_copy))
472  {
473    array_push($errors, $error_copy);
474  }
475}
476else
477{
478  array_push($infos, l10n('The configuration of Piwigo is finished, here is the next step<br><br>
479* go to the identification page and use the login/password given for webmaster<br>
480* this login will enable you to access to the administration panel and to the instructions in order to place pictures in your directories'));
481
482  if (isset($error_copy))
483  {
484    array_push($errors, $error_copy);
485  }
486  else
487  {
488    session_set_save_handler('pwg_session_open',
489      'pwg_session_close',
490      'pwg_session_read',
491      'pwg_session_write',
492      'pwg_session_destroy',
493      'pwg_session_gc'
494    );
495    if ( function_exists('ini_set') )
496    {
497      ini_set('session.use_cookies', $conf['session_use_cookies']);
498      ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
499      ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
500      ini_set('session.cookie_httponly', 1);
501    }
502    session_name($conf['session_name']);
503    session_set_cookie_params(0, cookie_path());
504    $user = build_user(1, true);
505    log_user($user['id'], false);
506  }
507
508  $template->assign(
509    'SUBSCRIBE_BASE_URL',
510    get_newsletter_subscribe_base_url($language)
511    );
512}
513if (count($errors) != 0)
514{
515  $template->assign('errors', $errors);
516}
517
518if (count($infos) != 0 )
519{
520  $template->assign('infos', $infos);
521}
522
523//----------------------------------------------------------- html code display
524$template->pparse('install');
525?>
Note: See TracBrowser for help on using the repository browser.