source: branches/2.3/install.php @ 13083

Last change on this file since 13083 was 12103, checked in by plg, 13 years ago

feature 2431: only keep MySQL on installation form

  • Property svn:eol-style set to LF
File size: 14.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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
27@set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
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//
32if( !@get_magic_quotes_gpc() )
33{
34  if( is_array($_POST) )
35  {
36    while( list($k, $v) = each($_POST) )
37    {
38      if( is_array($_POST[$k]) )
39      {
40        while( list($k2, $v2) = each($_POST[$k]) )
41        {
42          $_POST[$k][$k2] = addslashes($v2);
43        }
44        @reset($_POST[$k]);
45      }
46      else
47      {
48        $_POST[$k] = addslashes($v);
49      }
50    }
51    @reset($_POST);
52  }
53
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
74  if( is_array($_COOKIE) )
75  {
76    while( list($k, $v) = each($_COOKIE) )
77    {
78      if( is_array($_COOKIE[$k]) )
79      {
80        while( list($k2, $v2) = each($_COOKIE[$k]) )
81        {
82          $_COOKIE[$k][$k2] = addslashes($v2);
83        }
84        @reset($_COOKIE[$k]);
85      }
86      else
87      {
88        $_COOKIE[$k] = addslashes($v);
89      }
90    }
91    @reset($_COOKIE);
92  }
93}
94
95//----------------------------------------------------- variable initialization
96
97define('DEFAULT_PREFIX_TABLE', 'piwigo_');
98
99if (isset($_POST['install']))
100{
101  $prefixeTable = $_POST['prefix'];
102}
103else
104{
105  $prefixeTable = DEFAULT_PREFIX_TABLE;
106}
107
108if (is_file(PHPWG_ROOT_PATH .'local/config/multisite.inc.php'))
109{
110  include(PHPWG_ROOT_PATH .'local/config/multisite.inc.php');
111  define('PWG_LOCAL_DIR', $conf['local_dir_site']);
112}
113else
114{
115  define('PWG_LOCAL_DIR', 'local/');
116}
117
118include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
119@include(PHPWG_ROOT_PATH. 'local/config/config.inc.php');
120if (isset($conf['local_dir_site']))
121{
122  @include(PHPWG_ROOT_PATH.PWG_LOCAL_DIR. 'config/config.inc.php');
123}
124
125// download database config file if exists
126if (!empty($_GET['dl']) && file_exists($conf['local_data_dir'].'/pwg_'.$_GET['dl']))
127{
128  $filename = $conf['local_data_dir'].'/pwg_'.$_GET['dl'];
129  header('Cache-Control: no-cache, must-revalidate');
130  header('Pragma: no-cache');
131  header('Content-Disposition: attachment; filename="database.inc.php"');
132  header('Content-Transfer-Encoding: binary');
133  header('Content-Length: '.filesize($filename));
134  echo file_get_contents($filename);
135  unlink($filename);
136  exit();
137} 
138
139// Obtain various vars
140$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
141$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
142$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
143$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
144$dblayer = 'mysql';
145
146$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
147$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
148$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
149$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
150
151$is_newsletter_subscribe = true;
152if (isset($_POST['install']))
153{
154  $is_newsletter_subscribe = isset($_POST['newsletter_subscribe']);
155}
156
157$infos = array();
158$errors = array();
159
160$config_file = PHPWG_ROOT_PATH.PWG_LOCAL_DIR .'config/database.inc.php';
161if (@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/constants.php');
172include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
173include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
174
175include(PHPWG_ROOT_PATH . 'admin/include/languages.class.php');
176$languages = new languages('utf-8');
177
178if (isset($_GET['language']))
179{
180  $language = strip_tags($_GET['language']);
181}
182else
183{
184  $language = 'en_UK';
185  // Try to get browser language
186  foreach ($languages->fs_languages as $language_code => $fs_language)
187  {
188    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
189    {
190      $language = $language_code;
191      break;
192    }
193  }
194}
195
196if ('fr_FR' == $language) {
197  define('PHPWG_DOMAIN', 'fr.piwigo.org');
198}
199else if ('it_IT' == $language) {
200  define('PHPWG_DOMAIN', 'it.piwigo.org');
201}
202else if ('de_DE' == $language) {
203  define('PHPWG_DOMAIN', 'de.piwigo.org');
204}
205else if ('es_ES' == $language) {
206  define('PHPWG_DOMAIN', 'es.piwigo.org');
207}
208else if ('pl_PL' == $language) {
209  define('PHPWG_DOMAIN', 'pl.piwigo.org');
210}
211else if ('zh_CN' == $language) {
212  define('PHPWG_DOMAIN', 'cn.piwigo.org');
213}
214else if ('hu_HU' == $language) {
215  define('PHPWG_DOMAIN', 'hu.piwigo.org');
216}
217else if ('ru_RU' == $language) {
218  define('PHPWG_DOMAIN', 'ru.piwigo.org');
219}
220else if ('nl_NL' == $language) {
221  define('PHPWG_DOMAIN', 'nl.piwigo.org');
222}
223else {
224  define('PHPWG_DOMAIN', 'piwigo.org');
225}
226define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
227
228load_language('common.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
229load_language('admin.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
230load_language('install.lang', '', array('language' => $language, 'target_charset'=>'utf-8'));
231
232header('Content-Type: text/html; charset=UTF-8');
233//------------------------------------------------- check php version
234if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<'))
235{
236  include(PHPWG_ROOT_PATH.'install/php5_apache_configuration.php');
237}
238
239//----------------------------------------------------- template initialization
240include( PHPWG_ROOT_PATH .'include/template.class.php');
241$template = new Template(PHPWG_ROOT_PATH.'admin/themes', 'clear');
242$template->set_filenames( array('install' => 'install.tpl') );
243if (!isset($step))
244{
245  $step = 1;
246}
247//---------------------------------------------------------------- form analyze
248include(PHPWG_ROOT_PATH .'include/dblayer/functions_'.$dblayer.'.inc.php');
249include(PHPWG_ROOT_PATH . 'admin/include/functions_install.inc.php');
250include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
251
252if ( isset( $_POST['install'] ))
253{
254  install_db_connect($infos, $errors);
255  pwg_db_check_charset();
256
257  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
258  if ( empty($webmaster))
259    array_push( $errors, l10n('enter a login for webmaster') );
260  else if ( preg_match( '/[\'"]/', $webmaster ) )
261    array_push( $errors, l10n('webmaster login can\'t contain characters \' or "') );
262  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
263    array_push( $errors, l10n('please enter your password again') );
264  if ( empty($admin_mail))
265    array_push( $errors, l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)') );
266  else
267  {
268    $error_mail_address = validate_mail_address(null, $admin_mail);
269    if (!empty($error_mail_address))
270      array_push( $errors, $error_mail_address );
271  }
272
273  if ( count( $errors ) == 0 )
274  {
275    $step = 2;
276    $file_content = '<?php
277$conf[\'dblayer\'] = \''.$dblayer.'\';
278$conf[\'db_base\'] = \''.$dbname.'\';
279$conf[\'db_user\'] = \''.$dbuser.'\';
280$conf[\'db_password\'] = \''.$dbpasswd.'\';
281$conf[\'db_host\'] = \''.$dbhost.'\';
282
283$prefixeTable = \''.$prefixeTable.'\';
284
285define(\'PHPWG_INSTALLED\', true);
286define(\'PWG_CHARSET\', \'utf-8\');
287define(\'DB_CHARSET\', \'utf8\');
288define(\'DB_COLLATE\', \'\');
289
290?'.'>';
291
292    @umask(0111);
293    // writing the configuration file
294    if ( !($fp = @fopen( $config_file, 'w' )))
295    {
296      $tmp_filename = md5(uniqid(time()));
297      $fh = @fopen( $conf['local_data_dir'] . '/pwg_' . $tmp_filename, 'w' );
298      @fputs($fh, $file_content, strlen($file_content));
299      @fclose($fh);
300
301      $template->assign(
302        array(
303          'config_creation_failed' => true,
304          'config_url' => 'install.php?dl='.$tmp_filename,
305          'config_file_content' => $file_content,
306          )
307        );
308    }
309    @fputs($fp, $file_content, strlen($file_content));
310    @fclose($fp);
311
312    // tables creation, based on piwigo_structure.sql
313    execute_sqlfile(
314      PHPWG_ROOT_PATH.'install/piwigo_structure-'.$dblayer.'.sql',
315      DEFAULT_PREFIX_TABLE,
316      $prefixeTable,
317      $dblayer
318      );
319    // We fill the tables with basic informations
320    execute_sqlfile(
321      PHPWG_ROOT_PATH.'install/config.sql',
322      DEFAULT_PREFIX_TABLE,
323      $prefixeTable,
324      $dblayer
325      );
326
327    $query = '
328INSERT INTO '.$prefixeTable.'config (param,value,comment)
329   VALUES (\'secret_key\',md5('.pwg_db_cast_to_text(DB_RANDOM_FUNCTION.'()').'),
330   \'a secret key specific to the gallery for internal use\');';
331    pwg_query($query);
332
333    conf_update_param('piwigo_db_version', get_branch_from_version(PHPWG_VERSION));
334    conf_update_param('gallery_title', l10n('Just another Piwigo gallery'));
335    conf_update_param('page_banner', '<h1>%gallery_title%</h1>'."\n\n<p>".l10n('Welcome to my photo gallery').'</p>');
336
337    // fill languages table
338    foreach ($languages->fs_languages as $language_code => $fs_language)
339    {
340      $languages->perform_action('activate', $language_code);
341    }
342
343    // fill $conf global array
344    load_conf_from_db();
345
346    // PWG_CHARSET is required for building the fs_themes array in the
347    // themes class
348    if (!defined('PWG_CHARSET'))
349    {
350      define('PWG_CHARSET', 'utf-8');
351    }
352    activate_core_themes();
353
354    $insert = array(
355      'id' => 1,
356      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
357      );
358    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
359
360    // webmaster admin user
361    $inserts = array(
362      array(
363        'id'           => 1,
364        'username'     => $admin_name,
365        'password'     => md5($admin_pass1),
366        'mail_address' => $admin_mail,
367        ),
368      array(
369        'id'           => 2,
370        'username'     => 'guest',
371        ),
372      );
373    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
374
375    create_user_infos(array(1,2), array('language' => $language));
376
377    // Available upgrades must be ignored after a fresh installation. To
378    // make PWG avoid upgrading, we must tell it upgrades have already been
379    // made.
380    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
381    define('CURRENT_DATE', $dbnow);
382    $datas = array();
383    foreach (get_available_upgrade_ids() as $upgrade_id)
384    {
385      array_push(
386        $datas,
387        array(
388          'id'          => $upgrade_id,
389          'applied'     => CURRENT_DATE,
390          'description' => 'upgrade included in installation',
391          )
392        );
393    }
394    mass_inserts(
395      UPGRADE_TABLE,
396      array_keys($datas[0]),
397      $datas
398      );
399
400    if ($is_newsletter_subscribe)
401    {
402      fetchRemote(
403        get_newsletter_subscribe_base_url($language).$admin_mail,
404        $result,
405        array(),
406        array('origin' => 'installation')
407        );
408    }
409  }
410}
411
412//------------------------------------------------------ start template output
413foreach ($languages->fs_languages as $language_code => $fs_language)
414{
415  if ($language == $language_code)
416  {
417    $template->assign('language_selection', $language_code);
418  }
419  $languages_options[$language_code] = $fs_language['name'];
420}
421$template->assign('language_options', $languages_options);
422
423$template->assign(
424  array(
425    'T_CONTENT_ENCODING' => 'utf-8',
426    'RELEASE' => PHPWG_VERSION,
427    'F_ACTION' => 'install.php?language=' . $language,
428    'F_DB_HOST' => $dbhost,
429    'F_DB_USER' => $dbuser,
430    'F_DB_NAME' => $dbname,
431    'F_DB_PREFIX' => $prefixeTable,
432    'F_ADMIN' => $admin_name,
433    'F_ADMIN_EMAIL' => $admin_mail,
434    'EMAIL' => '<span class="adminEmail">'.$admin_mail.'</span>',
435    'F_NEWSLETTER_SUBSCRIBE' => $is_newsletter_subscribe,
436    'L_INSTALL_HELP' => sprintf(l10n('Need help ? Ask your question on <a href="%s">Piwigo message board</a>.'), PHPWG_URL.'/forum'),
437    ));
438
439//------------------------------------------------------ errors & infos display
440if ($step == 1)
441{
442  $template->assign('install', true);
443}
444else
445{
446  array_push(
447    $infos,
448    l10n('Congratulations, Piwigo installation is completed')
449    );
450
451  if (isset($error_copy))
452  {
453    array_push($errors, $error_copy);
454  }
455  else
456  {
457    session_set_save_handler('pwg_session_open',
458      'pwg_session_close',
459      'pwg_session_read',
460      'pwg_session_write',
461      'pwg_session_destroy',
462      'pwg_session_gc'
463    );
464    if ( function_exists('ini_set') )
465    {
466      ini_set('session.use_cookies', $conf['session_use_cookies']);
467      ini_set('session.use_only_cookies', $conf['session_use_only_cookies']);
468      ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid']));
469      ini_set('session.cookie_httponly', 1);
470    }
471    session_name($conf['session_name']);
472    session_set_cookie_params(0, cookie_path());
473    $user = build_user(1, true);
474    log_user($user['id'], false);
475  }
476}
477if (count($errors) != 0)
478{
479  $template->assign('errors', $errors);
480}
481
482if (count($infos) != 0 )
483{
484  $template->assign('infos', $infos);
485}
486
487//----------------------------------------------------------- html code display
488$template->pparse('install');
489?>
Note: See TracBrowser for help on using the repository browser.