source: trunk/install.php @ 2322

Last change on this file since 2322 was 2299, checked in by plg, 17 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.8 KB
RevLine 
[218]1<?php
[354]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// +-----------------------------------------------------------------------+
[218]23
[367]24//----------------------------------------------------------- include
25define('PHPWG_ROOT_PATH','./');
[345]26
[529]27/**
28 * loads an sql file and executes all queries
29 *
30 * Before executing a query, $replaced is... replaced by $replacing. This is
31 * useful when the SQL file contains generic words. Drop table queries are
32 * not executed.
33 *
34 * @param string filepath
35 * @param string replaced
36 * @param string replacing
37 * @return void
38 */
39function execute_sqlfile($filepath, $replaced, $replacing)
[382]40{
[529]41  $sql_lines = file($filepath);
[382]42  $query = '';
[529]43  foreach ($sql_lines as $sql_line)
44  {
45    $sql_line = trim($sql_line);
46    if (preg_match('/(^--|^$)/', $sql_line))
47    {
48      continue;
49    }
[382]50    $query.= ' '.$sql_line;
51    // if we reached the end of query, we execute it and reinitialize the
52    // variable "query"
[529]53    if (preg_match('/;$/', $sql_line))
[382]54    {
[529]55      $query = trim($query);
56      $query = str_replace($replaced, $replacing, $query);
[382]57      // we don't execute "DROP TABLE" queries
[529]58      if (!preg_match('/^DROP TABLE/i', $query))
59      {
[2127]60        global $install_charset_collate;
61        if ( !empty($install_charset_collate) )
62        {
63          if ( preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches) )
64          {
65            $query = $matches[1].' '.$install_charset_collate.';';
66          }
67        }
[529]68        mysql_query($query);
69      }
[382]70      $query = '';
71    }
72  }
73}
74
[367]75set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
76//
77// addslashes to vars if magic_quotes_gpc is off this is a security
78// precaution to prevent someone trying to break out of a SQL statement.
79//
80if( !get_magic_quotes_gpc() )
[218]81{
[367]82  if( is_array($_POST) )
[218]83  {
[367]84    while( list($k, $v) = each($_POST) )
[218]85    {
[367]86      if( is_array($_POST[$k]) )
[218]87      {
[367]88        while( list($k2, $v2) = each($_POST[$k]) )
89        {
90          $_POST[$k][$k2] = addslashes($v2);
91        }
92        @reset($_POST[$k]);
[218]93      }
94      else
95      {
[367]96        $_POST[$k] = addslashes($v);
[218]97      }
98    }
[367]99    @reset($_POST);
100  }
101
[1855]102  if( is_array($_GET) )
103  {
104    while( list($k, $v) = each($_GET) )
105    {
106      if( is_array($_GET[$k]) )
107      {
108        while( list($k2, $v2) = each($_GET[$k]) )
109        {
110          $_GET[$k][$k2] = addslashes($v2);
111        }
112        @reset($_GET[$k]);
113      }
114      else
115      {
116        $_GET[$k] = addslashes($v);
117      }
118    }
119    @reset($_GET);
120  }
121
[367]122  if( is_array($_COOKIE) )
123  {
124    while( list($k, $v) = each($_COOKIE) )
[218]125    {
[367]126      if( is_array($_COOKIE[$k]) )
[218]127      {
[367]128        while( list($k2, $v2) = each($_COOKIE[$k]) )
129        {
130          $_COOKIE[$k][$k2] = addslashes($v2);
131        }
132        @reset($_COOKIE[$k]);
[218]133      }
134      else
135      {
[367]136        $_COOKIE[$k] = addslashes($v);
[218]137      }
138    }
[367]139    @reset($_COOKIE);
[218]140  }
[367]141}
[218]142
[367]143//----------------------------------------------------- variable initialization
[1147]144
145define('DEFAULT_PREFIX_TABLE', 'phpwebgallery_');
146
[367]147// Obtain various vars
148$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
149$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
150$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
151$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
152
[1147]153if (isset($_POST['install']))
154{
155  $table_prefix = $_POST['prefix'];
156}
157else
158{
159  $table_prefix = DEFAULT_PREFIX_TABLE;
160}
[367]161
162$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
163$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
164$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
165$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
166
167$infos = array();
168$errors = array();
169
170// Open config.php ... if it exists
171$config_file = PHPWG_ROOT_PATH.'include/mysql.inc.php';
172if (@file_exists($config_file))
173{
[529]174  include($config_file);
175  // Is PhpWebGallery already installed ?
176  if (defined("PHPWG_INSTALLED"))
177  {
178    die('PhpWebGallery is already installed');
179  }
[367]180}
181
[682]182$prefixeTable = $table_prefix;
[819]183include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
[1079]184@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
[529]185include(PHPWG_ROOT_PATH . 'include/constants.php');
186include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
[1221]187include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
[2102]188include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
[2290]189include(PHPWG_ROOT_PATH . 'include/template.class.php');
[529]190
[2248]191if (isset($_GET['language']))
[405]192{
[2248]193  $language = strip_tags($_GET['language']);
[367]194}
[2127]195else
[1855]196{
[2127]197  $language = 'en_UK';
[2248]198  // Try to get browser language
199  foreach (get_languages('utf-8') as $language_code => $language_name)
200  {
201    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
202    {
203      $language = $language_code;
204      break;
205    }
206  }
[1855]207}
[367]208
[2127]209load_language( 'common.lang', '', $language, false, 'utf-8' );
210load_language( 'admin.lang', '', $language, false, 'utf-8' );
211load_language( 'install.lang', '', $language, false, 'utf-8' );
[529]212
[367]213//----------------------------------------------------- template initialization
[2248]214$template=new Template(PHPWG_ROOT_PATH.'template/yoga', 'clear');
[367]215$template->set_filenames( array('install'=>'install.tpl') );
216$step = 1;
[529]217//---------------------------------------------------------------- form analyze
[367]218if ( isset( $_POST['install'] ))
219{
[382]220  if ( @mysql_connect( $_POST['dbhost'],
221                       $_POST['dbuser'],
222                       $_POST['dbpasswd'] ) )
223  {
224    if ( @mysql_select_db($_POST['dbname'] ) )
[367]225    {
[2201]226      array_push( $infos, l10n('step1_confirmation') );
[218]227    }
[367]228    else
229    {
[2201]230      array_push( $errors, l10n('step1_err_db') );
[367]231    }
[2127]232    if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
233    {
234      $pwg_charset='utf-8';
235      $pwg_db_charset='utf8';
236      $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
237    }
238    else
239    {
240      $pwg_charset='iso-8859-1';
241      $pwg_db_charset='latin1';
242      $install_charset_collate = '';
243      if ( !array_key_exists($language, get_languages($pwg_charset) ) )
244      {
245        $language='en_UK';
246      }
247    }
[382]248  }
249  else
250  {
[2201]251    array_push( $errors, l10n('step1_err_server') );
[382]252  }
[2127]253
[382]254  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
255  if ( empty($webmaster))
[2201]256    array_push( $errors, l10n('step2_err_login1') );
[382]257  else if ( preg_match( '/[\'"]/', $webmaster ) )
[2201]258    array_push( $errors, l10n('step2_err_login3') );
[382]259  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
[2201]260    array_push( $errors, l10n('step2_err_pass') );
[382]261  if ( empty($admin_mail))
[2201]262    array_push( $errors, l10n('reg_err_mail_address') );
[2127]263  else
[382]264  {
[2124]265    $error_mail_address = validate_mail_address(null, $admin_mail);
[382]266    if (!empty($error_mail_address))
267      array_push( $errors, $error_mail_address );
268  }
[2127]269
[382]270  if ( count( $errors ) == 0 )
271  {
272    $step = 2;
[1147]273    $file_content = '<?php
274$cfgBase = \''.$dbname.'\';
275$cfgUser = \''.$dbuser.'\';
276$cfgPassword = \''.$dbpasswd.'\';
277$cfgHote = \''.$dbhost.'\';
278
279$prefixeTable = \''.$table_prefix.'\';
280
281define(\'PHPWG_INSTALLED\', true);
[2127]282define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
283define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
284define(\'DB_COLLATE\', \'\');
285
[1147]286?'.'>';
[2127]287
[382]288    @umask(0111);
289    // writing the configuration file
290    if ( !($fp = @fopen( $config_file, 'w' )))
[367]291    {
[382]292      $html_content = htmlentities( $file_content, ENT_QUOTES );
293      $html_content = nl2br( $html_content );
[2248]294      $template->assign('error_copy', $html_content);
[382]295    }
296    @fputs($fp, $file_content, strlen($file_content));
297    @fclose($fp);
[2127]298
[2189]299    // Create empty local files to avoid log errors
300    create_empty_local_files();
301
[382]302    // tables creation, based on phpwebgallery_structure.sql
[1147]303    execute_sqlfile(
304      PHPWG_ROOT_PATH.'install/phpwebgallery_structure.sql',
305      DEFAULT_PREFIX_TABLE,
306      $table_prefix
307      );
[382]308    // We fill the tables with basic informations
[1147]309    execute_sqlfile(
310      PHPWG_ROOT_PATH.'install/config.sql',
311      DEFAULT_PREFIX_TABLE,
312      $table_prefix
313      );
[218]314
[1284]315    // fill $conf global array
316    load_conf_from_db();
317
318    $insert = array(
319      'id' => 1,
320      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
321      );
322    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
[2127]323
[382]324    // webmaster admin user
[1284]325    $inserts = array(
326      array(
327        'id'           => 1,
328        'username'     => $admin_name,
329        'password'     => md5($admin_pass1),
330        'mail_address' => $admin_mail,
331        ),
332      array(
333        'id'           => 2,
334        'username'     => 'guest',
335        ),
336      );
337    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
[801]338
[1930]339    create_user_infos(array(1,2), array('language' => $language));
[808]340
[1027]341    // Available upgrades must be ignored after a fresh installation. To
342    // make PWG avoid upgrading, we must tell it upgrades have already been
343    // made.
[1221]344    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
345    define('CURRENT_DATE', $dbnow);
346    $datas = array();
[1027]347    foreach (get_available_upgrade_ids() as $upgrade_id)
348    {
[1221]349      array_push(
350        $datas,
351        array(
352          'id'          => $upgrade_id,
353          'applied'     => CURRENT_DATE,
354          'description' => 'upgrade included in installation',
355          )
356        );
[1027]357    }
[1221]358    mass_inserts(
359      UPGRADE_TABLE,
360      array_keys($datas[0]),
361      $datas
362      );
[382]363  }
[367]364}
[218]365
[2248]366//------------------------------------------------------ start template output
367foreach (get_languages('utf-8') as $language_code => $language_name)
368{
369  if ($language == $language_code)
370  {
371    $template->assign('language_selection', $language_code);
372  }
373  $languages_options[$language_code] = $language_name;
374}
375$template->assign('language_options', $languages_options);
376
377$template->assign(
[529]378  array(
[2248]379    'T_CONTENT_ENCODING' => 'utf-8',
[529]380    'RELEASE'=>PHPWG_VERSION,
[2248]381    'F_ACTION' => 'install.php?language=' . $language,
[529]382    'F_DB_HOST'=>$dbhost,
383    'F_DB_USER'=>$dbuser,
384    'F_DB_NAME'=>$dbname,
[2248]385    'F_DB_PREFIX' => $table_prefix,
[529]386    'F_ADMIN'=>$admin_name,
387    'F_ADMIN_EMAIL'=>$admin_mail,
[2248]388    'L_INSTALL_HELP'=>sprintf(l10n('install_help'), 'http://forum.'.PHPWG_DOMAIN.'/'),
[529]389    ));
390
391//------------------------------------------------------ errors & infos display
[2248]392if (count($errors) != 0)
[367]393{
[2248]394  $template->assign('errors', $errors);
[367]395}
[218]396
[2248]397if (count($infos) != 0 )
[367]398{
[2248]399  $template->assign('infos', $infos);
[367]400}
[218]401
[2248]402if ($step == 1)
[367]403{
[2248]404  $template->assign('install', true);
[218]405}
[367]406
[218]407//----------------------------------------------------------- html code display
[367]408$template->pparse('install');
[362]409?>
Note: See TracBrowser for help on using the repository browser.