source: trunk/admin/install.php @ 156

Last change on this file since 156 was 156, checked in by z0rglub, 21 years ago

Use prefixeTable instead of prefix_table to be compliant with previous
releases

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1<?php
2/***************************************************************************
3 *                                install.php                              *
4 *                            -------------------                          *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: install.php 156 2003-09-21 18:25:01Z z0rglub $
9 *                                                                         *
10 ***************************************************************************/
11
12/***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19
20//-------------------------------------------------------------------- includes
21define( PREFIX_INCLUDE, '.' );
22include( '../include/vtemplate.class.php' );
23include( '../include/functions.inc.php' );
24//----------------------------------------------------- template initialization
25$vtp = new VTemplate;
26$handle = $vtp->Open( '../template/default/admin/install.vtp' );
27$vtp->setGlobalVar( $handle, 'release', '1.3' );
28//-------------------------------------------------------------------- language
29if ( isset( $_GET['language'] ) )
30{
31  $isadmin = true;
32  $lang = array();
33  include( '../language/'.$_GET['language'].'.php' );
34  $tpl = array( 'step1_err_copy', 'step1_err_copy_2', 'step1_err_copy_next',
35                'errors_title', 'step1_title','step1_host','step1_host_info',
36                'step1_user','step1_user_info','step1_pass','step1_pass_info',
37                'step1_database','step1_database_info','step1_prefix',
38                'step1_prefix_info','submit','infos_title','step2_title',
39                'conf_general_webmaster','conf_general_webmaster_info',
40                'step2_pwd','step2_pwd_info','step2_pwd_conf',
41                'step2_pwd_conf_info','conf_general_mail',
42                'conf_general_mail_info','install_end_title',
43                'install_end_message');
44  templatize_array( $tpl, 'lang', $handle );
45  $vtp->setGlobalVar( $handle, 'language', $_GET['language'] );
46}
47//---------------------- Step 1 : connection informations, write of config file
48if ( $_GET['step'] == 1 )
49{
50  $errors = array();
51  $infos  = array();
52  // creation of ./include/mysql.inc.php : file containing database
53  // connection informations
54  if ( isset( $_POST['cfgBase'] )
55       and isset( $_POST['cfgUser'] )
56       and isset( $_POST['cfgPassword'] )
57       and isset( $_POST['cfgHote'] ) )
58  {
59    if ( @mysql_connect( $_POST['cfgHote'],
60                         $_POST['cfgUser'],
61                         $_POST['cfgPassword'] ) )
62    {
63      if ( @mysql_select_db($_POST['cfgBase'] ) )
64      {
65        array_push( $infos, $lang['step1_confirmation'] );
66      }
67      else
68      {
69        array_push( $errors, $lang['step1_err_db'] );
70      }
71    }
72    else
73    {
74      array_push( $errors, $lang['step1_err_server'] );
75    }
76                       
77    if ( count( $errors ) == 0 )
78    {
79      $file_content = "<?php";
80      $file_content.= "\n\$cfgBase = '".     $_POST['cfgBase']."';";
81      $file_content.= "\n\$cfgUser = '".     $_POST['cfgUser']."';";
82      $file_content.= "\n\$cfgPassword = '". $_POST['cfgPassword']."';";
83      $file_content.= "\n\$cfgHote = '".     $_POST['cfgHote']."';";
84      $file_content.= "\n\$prefixeTable = '".$_POST['prefixeTable']."';";
85      $file_content.= "\n?>";
86      // writting the configuration file
87      if ( $fp = @fopen( '../include/mysql.inc.php', 'a+' ) )
88      {
89        fwrite( $fp, $file_content ); 
90        fclose( $fp );
91      }
92      $cfgHote     = '';
93      $cfgUser     = '';
94      $cfgPassword = '';
95      $cfgBase     = '';
96      include( '../include/mysql.inc.php' );
97      $file_OK = false;
98      if ( @mysql_connect( $cfgHote, $cfgUser, $cfgPassword ) )
99      {
100        if ( @mysql_select_db( $cfgBase ) ) $file_OK = true;
101      }
102      if ( !$file_OK )
103      {
104        $vtp->addSession( $handle, 'error_copy' );
105        $html_content = htmlentities( $file_content, ENT_QUOTES );
106        $html_content = nl2br( $html_content );
107        $vtp->setVar( $handle, 'error_copy.file_content', $html_content );
108        $vtp->closeSession( $handle, 'error_copy' );
109      }
110      else
111      {
112        $url = 'install.php?step=2&language='.$_GET['language'];
113        header( 'Request-URI: '.$url ); 
114        header( 'Content-Location: '.$url); 
115        header( 'Location: '.$url );
116        exit();
117      }
118    }
119  }
120  // errors display
121  if ( sizeof( $errors ) != 0 )
122  {
123    $vtp->addSession( $handle, 'errors' );
124    foreach ( $errors as $error ) {
125      $vtp->addSession( $handle, 'error' );
126      $vtp->setVar( $handle, 'error.content', $error );
127      $vtp->closeSession( $handle, 'error' );
128    }
129    $vtp->closeSession( $handle, 'errors' );
130  }
131  // infos display
132  if ( sizeof( $infos ) != 0 )
133  {
134    $vtp->addSession( $handle, 'infos' );
135    foreach ( $infos as $info ) {
136      $vtp->addSession( $handle, 'info' );
137      $vtp->setVar( $handle, 'info.content', $info );
138      $vtp->closeSession( $handle, 'info' );
139    }
140    $vtp->closeSession( $handle, 'infos' );
141  }
142  // form display (if necessary)
143  if ( !isset( $_POST['submit'] ) or sizeof( $errors ) > 0 )
144  {
145    $vtp->addSession( $handle, 'step1' );
146
147    // host
148    if ( !isset( $_POST['cfgHote'] ) )
149      $vtp->setVar( $handle, 'step1.f_host', 'localhost' );
150    else
151      $vtp->setVar( $handle, 'step1.f_host', $_POST['cfgHote'] );
152    // user
153    $vtp->setVar( $handle, 'step1.f_user', $_POST['cfgUser'] );
154    // base
155    $vtp->setVar( $handle, 'step1.f_base', $_POST['cfgBase'] );
156    // prefixeTable
157    if ( !isset( $_POST['prefixeTable'] ) )
158      $vtp->setVar( $handle, 'step1.f_prefixeTable', 'phpwebgallery_' );
159    else
160      $vtp->setVar( $handle, 'step1.f_prefixeTable', $_POST['prefixeTable'] );
161
162    $vtp->closeSession( $handle, 'step1' );
163  }
164}
165//------------------------------------- Step 2 : creation of tables in database
166else if ( $_GET['step'] == 2 )
167{
168  $errors = array();
169  $infos  = array();
170
171  include( '../include/mysql.inc.php' );
172  mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
173    or die ( "Can't connect to database host" );
174  mysql_select_db( $cfgBase )
175    or die ( "Connection to host succeeded, but database selection failed" );
176               
177  if ( !isset( $_POST['submit'] ) )
178  {
179    // tables creation, based on phpwebgallery_structure.sql
180    $sql_lines = file( './phpwebgallery_structure.sql' );
181    $query = '';
182    foreach ( $sql_lines as $sql_line ) {
183      $sql_line = trim( $sql_line );
184      if ( preg_match( '/(^--|^$)/', $sql_line ) ) continue;
185      $query.= ' '.$sql_line;
186      // if we reached the end of query, we execute it and reinitialize the
187      // variable "query"
188      if ( preg_match( '/;$/', $sql_line ) )
189      {
190        $query = trim( $query );
191        $query = str_replace( 'phpwebgallery_', $prefixeTable, $query );
192        // we don't execute "DROP TABLE" queries
193        if ( !preg_match( '/^DROP TABLE/i', $query ) )
194          mysql_query( $query );
195        $query = '';
196      }
197    }
198  }
199
200  if ( isset( $_POST['submit'] ) )
201  {
202    // webmaster login must be
203    // 1. non empty
204    // 2. without characters ' or "
205    $webmaster = preg_replace( '/\s{2,}/', ' ', $_POST['webmaster'] );
206    $webmaster = trim( $webmaster );
207    if ( $webmaster == '' )
208      array_push( $errors, $lang['step2_err_login1'] );
209    if ( preg_match( '/[\'"]/', $webmaster ) )
210      array_push( $errors, $lang['step2_err_login3'] );
211    // the webmaster string must be the same as its confirmation
212    if ( $_POST['pwdWebmaster'] != $_POST['pwdWebmasterConf'] )
213      array_push( $errors, $lang['step2_err_pass'] );
214    // mail address must have this format : name@server.com
215    $error_mail_address = validate_mail_address( $_POST['mail_webmaster'] );
216    if ( $error_mail_address != '' )
217      array_push( $errors, $error_mail_address );
218    if ( $_POST['mail_webmaster'] == '' )
219      array_push( $errors, $lang['reg_err_mail_address'] );
220
221    // if no error found till here : insertion of data in tables
222    if ( count( $errors ) == 0 )
223    {
224      $query = 'DELETE FROM '.$prefixeTable.'config';
225      mysql_query( $query );
226
227      $query = 'INSERT INTO '.$prefixeTable.'config';
228      $query.= ' (webmaster,mail_webmaster) VALUES ';
229      $query.= " ('".$webmaster."','".$_POST['mail_webmaster']."')";
230      $query.= ';';
231      mysql_query( $query );
232
233      $query = 'INSERT INTO '.$prefixeTable.'sites';
234      $query.= " (id,galleries_url) VALUES (1, './galleries/')";
235      $query.= ';';
236      mysql_query( $query );
237
238      // webmaster admin user
239      $query = 'INSERT INTO '.$prefixeTable.'users';
240      $query.= ' (id,username,password,status,language) VALUES ';
241      $query.= "(1,'".$webmaster."','".md5( $_POST['pwdWebmaster'] )."'";
242      $query.= ",'admin','".$_GET['language']."')";
243      $query.= ';';
244      mysql_query($query);
245
246      // guest user
247      $query = 'INSERT INTO '.$prefixeTable.'users';
248      $query.= '(id,username,password,status,language) VALUES ';
249      $query.= "(2,'guest','','guest','francais')";
250      $query.= ';';
251      mysql_query( $query );
252    }
253  }
254
255  // errors display
256  if ( sizeof( $errors ) != 0 )
257  {
258    $vtp->addSession( $handle, 'errors' );
259    foreach ( $errors as $error ) {
260      $vtp->addSession( $handle, 'error' );
261      $vtp->setVar( $handle, 'error.content', $error );
262      $vtp->closeSession( $handle, 'error' );
263      }
264    $vtp->closeSession( $handle, 'errors' );
265  }
266
267  if ( !isset( $_POST['submit'] ) or sizeof( $errors ) > 0 )
268  {
269    $vtp->addSession( $handle, 'step2' );
270    $vtp->setVar( $handle, 'step2.f_webmaster', $_POST['webmaster'] );
271    $vtp->setVar( $handle, 'step2.f_mail_webmaster', $_POST['mail_webmaster']);
272    $vtp->closeSession( $handle, 'step2' );
273  }
274
275  // end of installation message
276  if ( isset( $_POST['submit'] ) and count( $errors ) == 0 )
277  {
278    $vtp->addSession( $handle, 'install_end' );
279    $vtp->closeSession( $handle, 'install_end' );
280  }
281}
282//---------------------------------------------------- Step 0 : language choice
283else
284{
285  $vtp->addSession( $handle, 'step0' );
286  $languages = get_languages( '../language/' );
287  foreach ( $languages as $language ) {
288    $vtp->addSession( $handle, 'language' );
289    $vtp->setVar( $handle, 'language.name', $language );
290    $vtp->closeSession( $handle, 'language' );
291  }
292  $vtp->closeSession( $handle, 'step0' );
293}
294//----------------------------------------------------------- html code display
295$code = $vtp->Display( $handle, 0 );
296echo $code;
297?>
Note: See TracBrowser for help on using the repository browser.