source: trunk/install.php @ 2102

Last change on this file since 2102 was 2102, checked in by patdenice, 17 years ago

0000421 : call a function to create empty local files during install and upgrade
Avoid log errors

  • 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 2102 2007-09-21 09:06:00Z patdenice $
8// | last update   : $Date: 2007-09-21 09:06:00 +0000 (Fri, 21 Sep 2007) $
9// | last modifier : $Author: patdenice $
10// | revision      : $Revision: 2102 $
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
210// Create empty local files to avoid log errors
211create_empty_local_files();
212
213if ( isset( $_POST['language'] ))
214{
215  $language = strip_tags($_POST['language']);
216}
217elseif ( isset( $_GET['language'] ))
218{
219  $language = strip_tags($_GET['language']);
220}
221else 
222{
223  $language = guess_lang();
224}
225
226if (!file_exists(PHPWG_ROOT_PATH.'language/'.$language.'/install.lang.php'))
227{
228  $language = 'en_UK.iso-8859-1';
229}
230
231include( './language/'.$language.'/common.lang.php' );
232// Never: @include( './language/'.$language.'/local.lang.php' );
233include( './language/'.$language.'/admin.lang.php' );
234include( './language/'.$language.'/install.lang.php' );
235//----------------------------------------------------- template initialization
236$template=setup_style('yoga');
237$template->set_filenames( array('install'=>'install.tpl') );
238$step = 1;
239//---------------------------------------------------------------- form analyze
240if ( isset( $_POST['install'] ))
241{
242  if ( @mysql_connect( $_POST['dbhost'],
243                       $_POST['dbuser'],
244                       $_POST['dbpasswd'] ) )
245  {
246    if ( @mysql_select_db($_POST['dbname'] ) )
247    {
248      array_push( $infos, $lang['step1_confirmation'] );
249    }
250    else
251    {
252      array_push( $errors, $lang['step1_err_db'] );
253    }
254  }
255  else
256  {
257    array_push( $errors, $lang['step1_err_server'] );
258  }
259 
260  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
261  if ( empty($webmaster))
262    array_push( $errors, $lang['step2_err_login1'] );
263  else if ( preg_match( '/[\'"]/', $webmaster ) )
264    array_push( $errors, $lang['step2_err_login3'] );
265  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
266    array_push( $errors, $lang['step2_err_pass'] );
267  if ( empty($admin_mail))
268    array_push( $errors, $lang['reg_err_mail_address'] );
269  else 
270  {
271    $error_mail_address = validate_mail_address($admin_mail);
272    if (!empty($error_mail_address))
273      array_push( $errors, $error_mail_address );
274  }
275 
276  if ( count( $errors ) == 0 )
277  {
278    $step = 2;
279    $file_content = '<?php
280$cfgBase = \''.$dbname.'\';
281$cfgUser = \''.$dbuser.'\';
282$cfgPassword = \''.$dbpasswd.'\';
283$cfgHote = \''.$dbhost.'\';
284
285$prefixeTable = \''.$table_prefix.'\';
286
287define(\'PHPWG_INSTALLED\', true);
288?'.'>';
289   
290    @umask(0111);
291    // writing the configuration file
292    if ( !($fp = @fopen( $config_file, 'w' )))
293    {
294      $html_content = htmlentities( $file_content, ENT_QUOTES );
295      $html_content = nl2br( $html_content );
296      $template->assign_block_vars(
297        'error_copy',
298        array(
299          'FILE_CONTENT' => $html_content,
300          )
301        );
302    }
303    @fputs($fp, $file_content, strlen($file_content));
304    @fclose($fp);
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'=>$lang['Initial_config'],
375    'L_LANG_TITLE'=>$lang['Default_lang'],
376    'L_DB_TITLE'=>$lang['step1_title'],
377    'L_DB_HOST'=>$lang['step1_host'],
378    'L_DB_HOST_INFO'=>$lang['step1_host_info'],
379    'L_DB_USER'=>$lang['step1_user'],
380    'L_DB_USER_INFO'=>$lang['step1_user_info'],
381    'L_DB_PASS'=>$lang['step1_pass'],
382    'L_DB_PASS_INFO'=>$lang['step1_pass_info'],
383    'L_DB_NAME'=>$lang['step1_database'],
384    'L_DB_NAME_INFO'=>$lang['step1_database_info'],
385    'L_DB_PREFIX'=>$lang['step1_prefix'],
386    'L_DB_PREFIX_INFO'=>$lang['step1_prefix_info'],
387    'L_ADMIN_TITLE'=>$lang['step2_title'],
388    'L_ADMIN'=>$lang['install_webmaster'],
389    'L_ADMIN_INFO'=>$lang['install_webmaster_info'],
390    'L_ADMIN_PASSWORD'=>$lang['step2_pwd'],
391    'L_ADMIN_PASSWORD_INFO'=>$lang['step2_pwd_info'],
392    'L_ADMIN_CONFIRM_PASSWORD'=>$lang['step2_pwd_conf'],
393    'L_ADMIN_CONFIRM_PASSWORD_INFO'=>$lang['step2_pwd_conf_info'],
394    'L_ADMIN_EMAIL'=>$lang['conf_mail_webmaster'],
395    'L_ADMIN_EMAIL_INFO'=>$lang['conf_mail_webmaster_info'],
396    'L_SUBMIT'=>$lang['Start_Install'],
397    'L_INSTALL_HELP'=>sprintf($lang['install_help'], 'http://forum.'.PHPWG_DOMAIN.'/'),
398    'L_ERR_COPY'=>$lang['step1_err_copy'],
399    'L_END_TITLE'=>$lang['install_end_title'],
400    'L_END_MESSAGE'=>$lang['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.