source: trunk/install.php @ 1930

Last change on this file since 1930 was 1930, checked in by rub, 17 years ago

Issue 578
User guest must be real user

Step 2: Installation finished, guest must be used on list and group, corrections

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 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 1930 2007-03-28 22:30:04Z rub $
8// | last update   : $Date: 2007-03-28 22:30:04 +0000 (Wed, 28 Mar 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1930 $
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 . 'include/template.php');
208
209if ( isset( $_POST['language'] ))
210{
211  $language = strip_tags($_POST['language']);
212}
213elseif ( isset( $_GET['language'] ))
214{
215  $language = strip_tags($_GET['language']);
216}
217else 
218{
219  $language = guess_lang();
220}
221
222if (!file_exists(PHPWG_ROOT_PATH.'language/'.$language.'/install.lang.php'))
223{
224  $language = 'en_UK.iso-8859-1';
225}
226
227include( './language/'.$language.'/common.lang.php' );
228// Never: @include( './language/'.$language.'/local.lang.php' );
229include( './language/'.$language.'/admin.lang.php' );
230include( './language/'.$language.'/install.lang.php' );
231//----------------------------------------------------- template initialization
232$template=setup_style('yoga');
233$template->set_filenames( array('install'=>'install.tpl') );
234$step = 1;
235//---------------------------------------------------------------- form analyze
236if ( isset( $_POST['install'] ))
237{
238  if ( @mysql_connect( $_POST['dbhost'],
239                       $_POST['dbuser'],
240                       $_POST['dbpasswd'] ) )
241  {
242    if ( @mysql_select_db($_POST['dbname'] ) )
243    {
244      array_push( $infos, $lang['step1_confirmation'] );
245    }
246    else
247    {
248      array_push( $errors, $lang['step1_err_db'] );
249    }
250  }
251  else
252  {
253    array_push( $errors, $lang['step1_err_server'] );
254  }
255 
256  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
257  if ( empty($webmaster))
258    array_push( $errors, $lang['step2_err_login1'] );
259  else if ( preg_match( '/[\'"]/', $webmaster ) )
260    array_push( $errors, $lang['step2_err_login3'] );
261  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
262    array_push( $errors, $lang['step2_err_pass'] );
263  if ( empty($admin_mail))
264    array_push( $errors, $lang['reg_err_mail_address'] );
265  else 
266  {
267    $error_mail_address = validate_mail_address($admin_mail);
268    if (!empty($error_mail_address))
269      array_push( $errors, $error_mail_address );
270  }
271 
272  if ( count( $errors ) == 0 )
273  {
274    $step = 2;
275    $file_content = '<?php
276$cfgBase = \''.$dbname.'\';
277$cfgUser = \''.$dbuser.'\';
278$cfgPassword = \''.$dbpasswd.'\';
279$cfgHote = \''.$dbhost.'\';
280
281$prefixeTable = \''.$table_prefix.'\';
282
283define(\'PHPWG_INSTALLED\', true);
284?'.'>';
285   
286    @umask(0111);
287    // writing the configuration file
288    if ( !($fp = @fopen( $config_file, 'w' )))
289    {
290      $html_content = htmlentities( $file_content, ENT_QUOTES );
291      $html_content = nl2br( $html_content );
292      $template->assign_block_vars(
293        'error_copy',
294        array(
295          'FILE_CONTENT' => $html_content,
296          )
297        );
298    }
299    @fputs($fp, $file_content, strlen($file_content));
300    @fclose($fp);
301   
302    // tables creation, based on phpwebgallery_structure.sql
303    execute_sqlfile(
304      PHPWG_ROOT_PATH.'install/phpwebgallery_structure.sql',
305      DEFAULT_PREFIX_TABLE,
306      $table_prefix
307      );
308    // We fill the tables with basic informations
309    execute_sqlfile(
310      PHPWG_ROOT_PATH.'install/config.sql',
311      DEFAULT_PREFIX_TABLE,
312      $table_prefix
313      );
314
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));
323   
324    // webmaster admin user
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);
338
339    create_user_infos(array(1,2), array('language' => $language));
340
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.
344    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
345    define('CURRENT_DATE', $dbnow);
346    $datas = array();
347    foreach (get_available_upgrade_ids() as $upgrade_id)
348    {
349      array_push(
350        $datas,
351        array(
352          'id'          => $upgrade_id,
353          'applied'     => CURRENT_DATE,
354          'description' => 'upgrade included in installation',
355          )
356        );
357    }
358    mass_inserts(
359      UPGRADE_TABLE,
360      array_keys($datas[0]),
361      $datas
362      );
363  }
364}
365
366$template->assign_vars(
367  array(
368    'RELEASE'=>PHPWG_VERSION,
369 
370    'L_BASE_TITLE'=>$lang['Initial_config'],
371    'L_LANG_TITLE'=>$lang['Default_lang'],
372    'L_DB_TITLE'=>$lang['step1_title'],
373    'L_DB_HOST'=>$lang['step1_host'],
374    'L_DB_HOST_INFO'=>$lang['step1_host_info'],
375    'L_DB_USER'=>$lang['step1_user'],
376    'L_DB_USER_INFO'=>$lang['step1_user_info'],
377    'L_DB_PASS'=>$lang['step1_pass'],
378    'L_DB_PASS_INFO'=>$lang['step1_pass_info'],
379    'L_DB_NAME'=>$lang['step1_database'],
380    'L_DB_NAME_INFO'=>$lang['step1_database_info'],
381    'L_DB_PREFIX'=>$lang['step1_prefix'],
382    'L_DB_PREFIX_INFO'=>$lang['step1_prefix_info'],
383    'L_ADMIN_TITLE'=>$lang['step2_title'],
384    'L_ADMIN'=>$lang['install_webmaster'],
385    'L_ADMIN_INFO'=>$lang['install_webmaster_info'],
386    'L_ADMIN_PASSWORD'=>$lang['step2_pwd'],
387    'L_ADMIN_PASSWORD_INFO'=>$lang['step2_pwd_info'],
388    'L_ADMIN_CONFIRM_PASSWORD'=>$lang['step2_pwd_conf'],
389    'L_ADMIN_CONFIRM_PASSWORD_INFO'=>$lang['step2_pwd_conf_info'],
390    'L_ADMIN_EMAIL'=>$lang['conf_mail_webmaster'],
391    'L_ADMIN_EMAIL_INFO'=>$lang['conf_mail_webmaster_info'],
392    'L_SUBMIT'=>$lang['Start_Install'],
393    'L_INSTALL_HELP'=>sprintf($lang['install_help'], 'http://forum.'.PHPWG_DOMAIN.'/'),
394    'L_ERR_COPY'=>$lang['step1_err_copy'],
395    'L_END_TITLE'=>$lang['install_end_title'],
396    'L_END_MESSAGE'=>$lang['install_end_message'],
397   
398    'F_ACTION'=>'install.php',
399    'F_DB_HOST'=>$dbhost,
400    'F_DB_USER'=>$dbuser,
401    'F_DB_NAME'=>$dbname,
402    'F_DB_PREFIX' => (
403      $table_prefix != DEFAULT_PREFIX_TABLE
404      ? $table_prefix
405      : DEFAULT_PREFIX_TABLE
406      ),
407    'F_ADMIN'=>$admin_name,
408    'F_ADMIN_EMAIL'=>$admin_mail,
409    'F_LANG_SELECT'=>language_select($language),
410   
411    'T_CONTENT_ENCODING' => $lang_info['charset']
412    ));
413
414//------------------------------------------------------ errors & infos display
415if ( sizeof( $errors ) != 0 )
416{
417  $template->assign_block_vars('errors',array());
418  for ( $i = 0; $i < sizeof( $errors ); $i++ )
419  {
420    $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
421  }
422}
423
424if ( sizeof( $infos ) != 0 )
425{
426  $template->assign_block_vars('infos',array());
427  for ( $i = 0; $i < sizeof( $infos ); $i++ )
428  {
429    $template->assign_block_vars('infos.info',array('INFO'=>$infos[$i]));
430  }
431}
432
433if ($step ==1)
434{
435  $template->assign_block_vars('install',array());
436}
437else
438{
439  $template->assign_block_vars('install_end',array());
440}
441
442//----------------------------------------------------------- html code display
443$template->pparse('install');
444?>
Note: See TracBrowser for help on using the repository browser.