source: trunk/install.php @ 382

Last change on this file since 382 was 382, checked in by z0rglub, 20 years ago
  • creation of function execute_sqlfile
  • code refactoring
  • use install/config.sql to import initial configuration parameters
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                              install.php                              |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-03-04 23:55:40 +0000 (Thu, 04 Mar 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 382 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28//----------------------------------------------------------- include
29define('PHPWG_ROOT_PATH','./');
30
31// Guess an initial language ...
32function guess_lang()
33{
34  global $_SERVER;
35  $languages = array();
36  $i = 0;
37  if ( $opendir = opendir ( PHPWG_ROOT_PATH.'language/' ) )
38  {
39    while ( $file = readdir ( $opendir ) )
40    {
41      if ( is_dir ( PHPWG_ROOT_PATH.'language/'.$file )&& !substr_count($file,'.'))
42      {
43        $languages[$i++] =$file;
44      }
45    }
46  }
47
48  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
49  {
50        $accept_lang_ary = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
51        for ($i = 0; $i < sizeof($accept_lang_ary); $i++)
52        {
53          for ($j=0; $j<sizeof($languages); $j++)
54          {
55                if (preg_match('#' . substr($languages[$j],0,2) . '#i', substr(trim($accept_lang_ary[$i]),0,2)))
56                {
57                  if (file_exists(PHPWG_ROOT_PATH . 'language/' . $languages[$j].'/install.lang.php'))
58                  {
59                        return $languages[$j];
60                  }
61                }
62          }
63        }
64  }
65  return 'en_EN';
66}
67
68function execute_sqlfile( $filepath, $replaced, $replacing )
69{
70  $sql_lines = file( $filepath );
71  $query = '';
72  foreach ( $sql_lines as $sql_line ) {
73    $sql_line = trim( $sql_line );
74    if ( preg_match( '/(^--|^$)/', $sql_line ) ) continue;
75    $query.= ' '.$sql_line;
76    // if we reached the end of query, we execute it and reinitialize the
77    // variable "query"
78    if ( preg_match( '/;$/', $sql_line ) )
79    {
80      $query = trim( $query );
81      $query = str_replace( $replaced, $replacing, $query );
82      // we don't execute "DROP TABLE" queries
83      if ( !preg_match( '/^DROP TABLE/i', $query ) ) mysql_query( $query );
84      $query = '';
85    }
86  }
87}
88
89set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
90//
91// addslashes to vars if magic_quotes_gpc is off this is a security
92// precaution to prevent someone trying to break out of a SQL statement.
93//
94if( !get_magic_quotes_gpc() )
95{
96  if( is_array($_POST) )
97  {
98    while( list($k, $v) = each($_POST) )
99    {
100      if( is_array($_POST[$k]) )
101      {
102        while( list($k2, $v2) = each($_POST[$k]) )
103        {
104          $_POST[$k][$k2] = addslashes($v2);
105        }
106        @reset($_POST[$k]);
107      }
108      else
109      {
110        $_POST[$k] = addslashes($v);
111      }
112    }
113    @reset($_POST);
114  }
115
116  if( is_array($_COOKIE) )
117  {
118    while( list($k, $v) = each($_COOKIE) )
119    {
120      if( is_array($_COOKIE[$k]) )
121      {
122        while( list($k2, $v2) = each($_COOKIE[$k]) )
123        {
124          $_COOKIE[$k][$k2] = addslashes($v2);
125        }
126        @reset($_COOKIE[$k]);
127      }
128      else
129      {
130        $_COOKIE[$k] = addslashes($v);
131      }
132    }
133    @reset($_COOKIE);
134  }
135}
136
137//----------------------------------------------------- variable initialization
138$install_style = 'default';
139$release_version = '1.4';
140if ( isset( $_POST['language'] ))
141{
142  $language = strip_tags($_POST['language']);
143}
144else 
145{
146  $language = guess_lang();
147}
148
149if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $language . '/install.lang.php')) )
150{
151  $language = 'en_EN';
152}
153include( './language/'.$language.'/install.lang.php' );
154include( './language/'.$language.'/admin.lang.php' );
155$mapping_lang = $language;
156if ( !file_exists(@realpath(PHPWG_ROOT_PATH . 'language/' . $language . '/lang.lang.php')) )
157{
158  $mapping_lang = 'en_EN';
159}
160include_once(PHPWG_ROOT_PATH . 'language/' . $mapping_lang . '/lang.lang.php');
161
162// Obtain various vars
163$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
164$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
165$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
166$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
167
168$table_prefix = (!empty($_POST['prefix'])) ? $_POST['prefix'] : 'phpwg_';
169
170$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
171$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
172$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
173$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
174
175$lang_options = $lang['lang'];
176@asort($lang_options);
177@reset($lang_options);
178
179$infos = array();
180$errors = array();
181
182// Open config.php ... if it exists
183$config_file = PHPWG_ROOT_PATH.'include/mysql.inc.php';
184if (@file_exists($config_file))
185{
186        include($config_file);
187}
188
189// Is phpBB already installed? Yes? Redirect to the index
190if (defined("PHPWG_INSTALLED"))
191{ 
192  header( 'Location: index.php' );
193  exit;
194}
195
196include(PHPWG_ROOT_PATH . 'include/constants.php');
197include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
198include(PHPWG_ROOT_PATH . 'include/template.php');
199//----------------------------------------------------- template initialization
200$template=setup_style($install_style);
201$template->set_filenames( array('install'=>'install.tpl') );
202$step = 1;
203//----------------------------------------------------- form analyze
204if ( isset( $_POST['install'] ))
205{
206  if ( @mysql_connect( $_POST['dbhost'],
207                       $_POST['dbuser'],
208                       $_POST['dbpasswd'] ) )
209  {
210    if ( @mysql_select_db($_POST['dbname'] ) )
211    {
212      array_push( $infos, $lang['step1_confirmation'] );
213    }
214    else
215    {
216      array_push( $errors, $lang['step1_err_db'] );
217    }
218  }
219  else
220  {
221    array_push( $errors, $lang['step1_err_server'] );
222  }
223 
224  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
225  if ( empty($webmaster))
226    array_push( $errors, $lang['step2_err_login1'] );
227  else if ( preg_match( '/[\'"]/', $webmaster ) )
228    array_push( $errors, $lang['step2_err_login3'] );
229  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
230    array_push( $errors, $lang['step2_err_pass'] );
231  if ( empty($admin_mail))
232    array_push( $errors, $lang['reg_err_mail_address'] );
233  else 
234  {
235    $error_mail_address = validate_mail_address($admin_mail);
236    if (!empty($error_mail_address))
237      array_push( $errors, $error_mail_address );
238  }
239 
240  if ( count( $errors ) == 0 )
241  {
242    $step = 2;
243    $file_content = "<?php";
244    $file_content.= "\n\$dbname = '".     $dbname."';";
245    $file_content.= "\n\$dbuser = '".     $dbuser."';";
246    $file_content.= "\n\$dbpasswd = '". $dbpasswd."';";
247    $file_content.= "\n\$dbhost = '".     $dbhost."';";
248    $file_content.= "\n";
249    $file_content.= "\n\$table_prefix = '".$table_prefix."';";
250    $file_content.= "\n";
251    $file_content.= "\ndefine('PHPWG_INSTALLED', true);";
252    $file_content.= "\n?".">";
253   
254    @umask(0111);
255    // writing the configuration file
256    if ( !($fp = @fopen( $config_file, 'w' )))
257    {
258      $html_content = htmlentities( $file_content, ENT_QUOTES );
259      $html_content = nl2br( $html_content );
260      $template->assign_block_vars('error_copy',
261                                   array('FILE_CONTENT'=>$html_content));
262    }
263    @fputs($fp, $file_content, strlen($file_content));
264    @fclose($fp);
265   
266    // tables creation, based on phpwebgallery_structure.sql
267    execute_sqlfile( './install/phpwebgallery_structure.sql'
268                     , 'phpwebgallery_'
269                     , $table_prefix );
270    // We fill the tables with basic informations
271    execute_sqlfile( './install/config.sql'
272                     , 'phpwebgallery_'
273                     , $table_prefix );
274
275    $query = 'UPDATE '.CONFIG_TABLE;
276    $query.= " SET value = '".$admin_name."'";
277    $query.= " WHERE param = 'webmaster'";
278    $query.= ';';
279    mysql_query( $query );
280
281    $query = 'UPDATE '.CONFIG_TABLE;
282    $query.= " SET value = '".$admin_mail."'";
283    $query.= " WHERE param = 'mail_webmaster'";
284    $query.= ';';
285    mysql_query( $query );
286   
287    $query = 'INSERT INTO '.SITES_TABLE;
288    $query.= " (id,galleries_url) VALUES (1, './galleries/');";
289    mysql_query( $query );
290   
291    // webmaster admin user
292    $query = 'INSERT INTO '.USERS_TABLE;
293    $query.= ' (id,username,password,status,language,mail_address) VALUES ';
294    $query.= "(1,'".$admin_name."','".md5( $admin_pass1 )."'";
295    $query.= ",'admin','".$language."'";
296    $query.= ",'".$admin_mail."');";
297    mysql_query($query);
298   
299    // guest user
300    $query = 'INSERT INTO '.USERS_TABLE;
301    $query.= '(id,username,password,status,language) VALUES ';
302    $query.= "(2,'guest','','guest','".$language."')";
303    $query.= ';';
304    mysql_query( $query );
305  }
306}
307
308$template->assign_vars(array(
309  'RELEASE'=>$release_version,
310 
311  'L_BASE_TITLE'=>$lang['Initial_config'],
312  'L_LANG_TITLE'=>$lang['Default_lang'],
313  'L_DB_TITLE'=>$lang['step1_title'],
314  'L_DB_HOST'=>$lang['step1_host'],
315  'L_DB_HOST_INFO'=>$lang['step1_host_info'],
316  'L_DB_USER'=>$lang['step1_user'],
317  'L_DB_USER_INFO'=>$lang['step1_user_info'],
318  'L_DB_PASS'=>$lang['step1_pass'],
319  'L_DB_PASS_INFO'=>$lang['step1_pass_info'],
320  'L_DB_NAME'=>$lang['step1_database'],
321  'L_DB_NAME_INFO'=>$lang['step1_database_info'],
322  'L_DB_PREFIX'=>$lang['step1_prefix'],
323  'L_DB_PREFIX_INFO'=>$lang['step1_prefix_info'],
324  'L_ADMIN_TITLE'=>$lang['step2_title'],
325  'L_ADMIN'=>$lang['conf_general_webmaster'],
326  'L_ADMIN_INFO'=>$lang['conf_general_webmaster_info'],
327  'L_ADMIN_PASSWORD'=>$lang['step2_pwd'],
328  'L_ADMIN_PASSWORD_INFO'=>$lang['step2_pwd_info'],
329  'L_ADMIN_CONFIRM_PASSWORD'=>$lang['step2_pwd_conf'],
330  'L_ADMIN_CONFIRM_PASSWORD_INFO'=>$lang['step2_pwd_conf_info'],
331  'L_ADMIN_EMAIL'=>$lang['conf_general_mail'],
332  'L_ADMIN_EMAIL_INFO'=>$lang['conf_general_mail_info'],
333  'L_SUBMIT'=>$lang['Start_Install'],
334  'L_HELP'=>$lang['install_help'],
335  'L_ERR_COPY'=>$lang['step1_err_copy'],
336  'L_END_TITLE'=>$lang['install_end_title'],
337  'L_END_MESSAGE'=>$lang['install_end_message'],
338 
339  'F_ACTION'=>add_session_id( 'install.php' ),
340  'F_DB_HOST'=>$dbhost,
341  'F_DB_USER'=>$dbuser,
342  'F_DB_NAME'=>$dbname,
343  'F_DB_PREFIX'=>$table_prefix,
344  'F_ADMIN'=>$admin_name,
345  'F_ADMIN_EMAIL'=>$admin_mail,
346  'F_LANG_SELECT'=>make_jumpbox($lang_options, $language, true),
347 
348  'T_CONTENT_ENCODING' => $lang['charset'],
349  'T_STYLE' =>  './template/'.$install_style.'/'.$install_style.'-admin.css')
350        );
351       
352//-------------------------------------------------------- errors & infos display
353if ( sizeof( $errors ) != 0 )
354{
355  $template->assign_block_vars('errors',array());
356  for ( $i = 0; $i < sizeof( $errors ); $i++ )
357  {
358    $template->assign_block_vars('errors.error',array('ERROR'=>$errors[$i]));
359  }
360}
361
362if ( sizeof( $infos ) != 0 )
363{
364  $template->assign_block_vars('infos',array());
365  for ( $i = 0; $i < sizeof( $infos ); $i++ )
366  {
367    $template->assign_block_vars('infos.info',array('INFO'=>$infos[$i]));
368  }
369}
370
371if ($step ==1)
372{
373  $template->assign_block_vars('install',array());
374}
375else
376{
377  $template->assign_block_vars('install_end',array());
378}
379
380//----------------------------------------------------------- html code display
381$template->pparse('install');
382?>
Note: See TracBrowser for help on using the repository browser.