source: trunk/install.php @ 24611

Last change on this file since 24611 was 22570, checked in by ddtddt, 11 years ago

[trunk] - add new site br.piwigo.org

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