source: trunk/install.php @ 6049

Last change on this file since 6049 was 5983, checked in by plg, 14 years ago

remove useless check of mysql.inc.php

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