source: branches/1.7/install.php @ 27569

Last change on this file since 27569 was 2202, checked in by rub, 16 years ago

Replace old use of $lang by l10n function.

Merge BSF 2200:2201 into branch-1_7

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: install.php 2202 2008-01-30 22:16:01Z rub $
8// | last update   : $Date: 2008-01-30 22:16:01 +0000 (Wed, 30 Jan 2008) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 2202 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27//----------------------------------------------------------- include
28define('PHPWG_ROOT_PATH','./');
29
30// Guess an initial language ...
31function guess_lang()
32{
33  return 'en_UK.iso-8859-1';
34}
35
36//
37// Pick a language, any language ...
38//
39function language_select($default, $select_name = "language")
40{
41  $available_lang = get_languages();
42
43  $lang_select = '<select name="' . $select_name . '" onchange="document.location = \''.PHPWG_ROOT_PATH.'install.php?language=\'+this.options[this.selectedIndex].value;">';
44  foreach ($available_lang as $code => $displayname)
45  {
46    $selected = ( strtolower($default) == strtolower($code) ) ? ' selected="selected"' : '';
47    $lang_select .= '<option value="'.$code.'" ' . $selected . '>' . ucwords($displayname) . '</option>';
48  }
49  $lang_select .= '</select>';
50
51  return $lang_select;
52}
53
54/**
55 * loads an sql file and executes all queries
56 *
57 * Before executing a query, $replaced is... replaced by $replacing. This is
58 * useful when the SQL file contains generic words. Drop table queries are
59 * not executed.
60 *
61 * @param string filepath
62 * @param string replaced
63 * @param string replacing
64 * @return void
65 */
66function execute_sqlfile($filepath, $replaced, $replacing)
67{
68  $sql_lines = file($filepath);
69  $query = '';
70  foreach ($sql_lines as $sql_line)
71  {
72    $sql_line = trim($sql_line);
73    if (preg_match('/(^--|^$)/', $sql_line))
74    {
75      continue;
76    }
77    $query.= ' '.$sql_line;
78    // if we reached the end of query, we execute it and reinitialize the
79    // variable "query"
80    if (preg_match('/;$/', $sql_line))
81    {
82      $query = trim($query);
83      $query = str_replace($replaced, $replacing, $query);
84      // we don't execute "DROP TABLE" queries
85      if (!preg_match('/^DROP TABLE/i', $query))
86      {
87        mysql_query($query);
88      }
89      $query = '';
90    }
91  }
92}
93
94set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
95//
96// addslashes to vars if magic_quotes_gpc is off this is a security
97// precaution to prevent someone trying to break out of a SQL statement.
98//
99if( !get_magic_quotes_gpc() )
100{
101  if( is_array($_POST) )
102  {
103    while( list($k, $v) = each($_POST) )
104    {
105      if( is_array($_POST[$k]) )
106      {
107        while( list($k2, $v2) = each($_POST[$k]) )
108        {
109          $_POST[$k][$k2] = addslashes($v2);
110        }
111        @reset($_POST[$k]);
112      }
113      else
114      {
115        $_POST[$k] = addslashes($v);
116      }
117    }
118    @reset($_POST);
119  }
120
121  if( is_array($_GET) )
122  {
123    while( list($k, $v) = each($_GET) )
124    {
125      if( is_array($_GET[$k]) )
126      {
127        while( list($k2, $v2) = each($_GET[$k]) )
128        {
129          $_GET[$k][$k2] = addslashes($v2);
130        }
131        @reset($_GET[$k]);
132      }
133      else
134      {
135        $_GET[$k] = addslashes($v);
136      }
137    }
138    @reset($_GET);
139  }
140
141  if( is_array($_COOKIE) )
142  {
143    while( list($k, $v) = each($_COOKIE) )
144    {
145      if( is_array($_COOKIE[$k]) )
146      {
147        while( list($k2, $v2) = each($_COOKIE[$k]) )
148        {
149          $_COOKIE[$k][$k2] = addslashes($v2);
150        }
151        @reset($_COOKIE[$k]);
152      }
153      else
154      {
155        $_COOKIE[$k] = addslashes($v);
156      }
157    }
158    @reset($_COOKIE);
159  }
160}
161
162//----------------------------------------------------- variable initialization
163
164define('DEFAULT_PREFIX_TABLE', 'phpwebgallery_');
165
166// Obtain various vars
167$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
168$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
169$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
170$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
171
172if (isset($_POST['install']))
173{
174  $table_prefix = $_POST['prefix'];
175}
176else
177{
178  $table_prefix = DEFAULT_PREFIX_TABLE;
179}
180
181$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
182$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
183$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
184$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
185
186$infos = array();
187$errors = array();
188
189// Open config.php ... if it exists
190$config_file = PHPWG_ROOT_PATH.'include/mysql.inc.php';
191if (@file_exists($config_file))
192{
193  include($config_file);
194  // Is PhpWebGallery already installed ?
195  if (defined("PHPWG_INSTALLED"))
196  {
197    die('PhpWebGallery is already installed');
198  }
199}
200
201$prefixeTable = $table_prefix;
202include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
203@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
204include(PHPWG_ROOT_PATH . 'include/constants.php');
205include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
206include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
207include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
208include(PHPWG_ROOT_PATH . 'include/template.php');
209
210if ( isset( $_POST['language'] ))
211{
212  $language = strip_tags($_POST['language']);
213}
214elseif ( isset( $_GET['language'] ))
215{
216  $language = strip_tags($_GET['language']);
217}
218else 
219{
220  $language = guess_lang();
221}
222
223if (!file_exists(PHPWG_ROOT_PATH.'language/'.$language.'/install.lang.php'))
224{
225  $language = 'en_UK.iso-8859-1';
226}
227
228include( './language/'.$language.'/common.lang.php' );
229// Never: @include( './language/'.$language.'/local.lang.php' );
230include( './language/'.$language.'/admin.lang.php' );
231include( './language/'.$language.'/install.lang.php' );
232//----------------------------------------------------- template initialization
233$template=setup_style('yoga');
234$template->set_filenames( array('install'=>'install.tpl') );
235$step = 1;
236//---------------------------------------------------------------- form analyze
237if ( isset( $_POST['install'] ))
238{
239  if ( @mysql_connect( $_POST['dbhost'],
240                       $_POST['dbuser'],
241                       $_POST['dbpasswd'] ) )
242  {
243    if ( @mysql_select_db($_POST['dbname'] ) )
244    {
245      array_push( $infos, l10n('step1_confirmation') );
246    }
247    else
248    {
249      array_push( $errors, l10n('step1_err_db') );
250    }
251  }
252  else
253  {
254    array_push( $errors, l10n('step1_err_server') );
255  }
256 
257  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
258  if ( empty($webmaster))
259    array_push( $errors, l10n('step2_err_login1') );
260  else if ( preg_match( '/[\'"]/', $webmaster ) )
261    array_push( $errors, l10n('step2_err_login3') );
262  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
263    array_push( $errors, l10n('step2_err_pass') );
264  if ( empty($admin_mail))
265    array_push( $errors, l10n('reg_err_mail_address') );
266  else 
267  {
268    $error_mail_address = validate_mail_address($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$cfgBase = \''.$dbname.'\';
278$cfgUser = \''.$dbuser.'\';
279$cfgPassword = \''.$dbpasswd.'\';
280$cfgHote = \''.$dbhost.'\';
281
282$prefixeTable = \''.$table_prefix.'\';
283
284define(\'PHPWG_INSTALLED\', true);
285?'.'>';
286   
287    @umask(0111);
288    // writing the configuration file
289    if ( !($fp = @fopen( $config_file, 'w' )))
290    {
291      $html_content = htmlentities( $file_content, ENT_QUOTES );
292      $html_content = nl2br( $html_content );
293      $template->assign_block_vars(
294        'error_copy',
295        array(
296          'FILE_CONTENT' => $html_content,
297          )
298        );
299    }
300    @fputs($fp, $file_content, strlen($file_content));
301    @fclose($fp);
302
303    // Create empty local files to avoid log errors
304    create_empty_local_files();
305   
306    // tables creation, based on phpwebgallery_structure.sql
307    execute_sqlfile(
308      PHPWG_ROOT_PATH.'install/phpwebgallery_structure.sql',
309      DEFAULT_PREFIX_TABLE,
310      $table_prefix
311      );
312    // We fill the tables with basic informations
313    execute_sqlfile(
314      PHPWG_ROOT_PATH.'install/config.sql',
315      DEFAULT_PREFIX_TABLE,
316      $table_prefix
317      );
318
319    // fill $conf global array
320    load_conf_from_db();
321
322    $insert = array(
323      'id' => 1,
324      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
325      );
326    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
327   
328    // webmaster admin user
329    $inserts = array(
330      array(
331        'id'           => 1,
332        'username'     => $admin_name,
333        'password'     => md5($admin_pass1),
334        'mail_address' => $admin_mail,
335        ),
336      array(
337        'id'           => 2,
338        'username'     => 'guest',
339        ),
340      );
341    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
342
343    create_user_infos(array(1,2), array('language' => $language));
344
345    // Available upgrades must be ignored after a fresh installation. To
346    // make PWG avoid upgrading, we must tell it upgrades have already been
347    // made.
348    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
349    define('CURRENT_DATE', $dbnow);
350    $datas = array();
351    foreach (get_available_upgrade_ids() as $upgrade_id)
352    {
353      array_push(
354        $datas,
355        array(
356          'id'          => $upgrade_id,
357          'applied'     => CURRENT_DATE,
358          'description' => 'upgrade included in installation',
359          )
360        );
361    }
362    mass_inserts(
363      UPGRADE_TABLE,
364      array_keys($datas[0]),
365      $datas
366      );
367  }
368}
369
370$template->assign_vars(
371  array(
372    'RELEASE'=>PHPWG_VERSION,
373 
374    'L_BASE_TITLE'=>l10n('Initial_config'),
375    'L_LANG_TITLE'=>l10n('Default_lang'),
376    'L_DB_TITLE'=>l10n('step1_title'),
377    'L_DB_HOST'=>l10n('step1_host'),
378    'L_DB_HOST_INFO'=>l10n('step1_host_info'),
379    'L_DB_USER'=>l10n('step1_user'),
380    'L_DB_USER_INFO'=>l10n('step1_user_info'),
381    'L_DB_PASS'=>l10n('step1_pass'),
382    'L_DB_PASS_INFO'=>l10n('step1_pass_info'),
383    'L_DB_NAME'=>l10n('step1_database'),
384    'L_DB_NAME_INFO'=>l10n('step1_database_info'),
385    'L_DB_PREFIX'=>l10n('step1_prefix'),
386    'L_DB_PREFIX_INFO'=>l10n('step1_prefix_info'),
387    'L_ADMIN_TITLE'=>l10n('step2_title'),
388    'L_ADMIN'=>l10n('install_webmaster'),
389    'L_ADMIN_INFO'=>l10n('install_webmaster_info'),
390    'L_ADMIN_PASSWORD'=>l10n('step2_pwd'),
391    'L_ADMIN_PASSWORD_INFO'=>l10n('step2_pwd_info'),
392    'L_ADMIN_CONFIRM_PASSWORD'=>l10n('step2_pwd_conf'),
393    'L_ADMIN_CONFIRM_PASSWORD_INFO'=>l10n('step2_pwd_conf_info'),
394    'L_ADMIN_EMAIL'=>l10n('conf_mail_webmaster'),
395    'L_ADMIN_EMAIL_INFO'=>l10n('conf_mail_webmaster_info'),
396    'L_SUBMIT'=>l10n('Start_Install'),
397    'L_INSTALL_HELP'=>sprintf(l10n('install_help'), 'http://forum.'.PHPWG_DOMAIN.'/'),
398    'L_ERR_COPY'=>l10n('step1_err_copy'),
399    'L_END_TITLE'=>l10n('install_end_title'),
400    'L_END_MESSAGE'=>l10n('install_end_message'),
401   
402    'F_ACTION'=>'install.php',
403    'F_DB_HOST'=>$dbhost,
404    'F_DB_USER'=>$dbuser,
405    'F_DB_NAME'=>$dbname,
406    'F_DB_PREFIX' => (
407      $table_prefix != DEFAULT_PREFIX_TABLE
408      ? $table_prefix
409      : DEFAULT_PREFIX_TABLE
410      ),
411    'F_ADMIN'=>$admin_name,
412    'F_ADMIN_EMAIL'=>$admin_mail,
413    'F_LANG_SELECT'=>language_select($language),
414   
415    'T_CONTENT_ENCODING' => $lang_info['charset']
416    ));
417
418//------------------------------------------------------ errors & infos display
419if ( sizeof( $errors ) != 0 )
420{
421  $template->assign_block_vars('errors',array());
422  for ( $i = 0; $i < sizeof( $errors ); $i++ )
423  {
424    $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
425  }
426}
427
428if ( sizeof( $infos ) != 0 )
429{
430  $template->assign_block_vars('infos',array());
431  for ( $i = 0; $i < sizeof( $infos ); $i++ )
432  {
433    $template->assign_block_vars('infos.info',array('INFO'=>$infos[$i]));
434  }
435}
436
437if ($step ==1)
438{
439  $template->assign_block_vars('install',array());
440}
441else
442{
443  $template->assign_block_vars('install_end',array());
444}
445
446//----------------------------------------------------------- html code display
447$template->pparse('install');
448?>
Note: See TracBrowser for help on using the repository browser.