source: trunk/install.php @ 5357

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

Feature 1535: Add language manager.

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