source: trunk/admin/install.php @ 94

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

Step2 OK

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