source: branches/release-1_3/admin/install.php @ 317

Last change on this file since 317 was 317, checked in by z0rglub, 20 years ago
  • Php Warnings correction (file not found)
  • feature 5 : using same mail address for webmaster and initial admin
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 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 317 2004-01-24 22:55:02Z 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','install_help');
44  templatize_array( $tpl, 'lang', $handle );
45  $vtp->setGlobalVar( $handle, 'language', $_GET['language'] );
46}
47//---------------------- Step 1 : connection informations, write of config file
48if ( isset($_GET['step']) && $_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    $config_file = '../include/mysql.inc.php';
78   
79    if ( count( $errors ) == 0 )
80    {
81      $file_content = "<?php";
82      $file_content.= "\n\$cfgBase = '".     $_POST['cfgBase']."';";
83      $file_content.= "\n\$cfgUser = '".     $_POST['cfgUser']."';";
84      $file_content.= "\n\$cfgPassword = '". $_POST['cfgPassword']."';";
85      $file_content.= "\n\$cfgHote = '".     $_POST['cfgHote']."';";
86      $file_content.= "\n\$prefixeTable = '".$_POST['prefixeTable']."';";
87      $file_content.= "\n?>";
88      // writting the configuration file
89      if ( $fp = @fopen( $config_file, 'a+' ) )
90      {
91        fwrite( $fp, $file_content ); 
92        fclose( $fp );
93      }
94      $cfgHote     = '';
95      $cfgUser     = '';
96      $cfgPassword = '';
97      $cfgBase     = '';
98      if ( is_file( $config_file ) ) include( $config_file );
99      $file_OK = false;
100      if ( @mysql_connect( $cfgHote, $cfgUser, $cfgPassword ) )
101      {
102        if ( @mysql_select_db( $cfgBase ) ) $file_OK = true;
103      }
104      if ( !$file_OK )
105      {
106        $vtp->addSession( $handle, 'error_copy' );
107        $html_content = htmlentities( $file_content, ENT_QUOTES );
108        $html_content = nl2br( $html_content );
109        $vtp->setVar( $handle, 'error_copy.file_content', $html_content );
110        $vtp->closeSession( $handle, 'error_copy' );
111      }
112      else
113      {
114        $url = 'install.php?step=2&language='.$_GET['language'];
115        header( 'Request-URI: '.$url ); 
116        header( 'Content-Location: '.$url); 
117        header( 'Location: '.$url );
118        exit();
119      }
120    }
121  }
122  // errors display
123  if ( sizeof( $errors ) != 0 )
124  {
125    $vtp->addSession( $handle, 'errors' );
126    foreach ( $errors as $error ) {
127      $vtp->addSession( $handle, 'error' );
128      $vtp->setVar( $handle, 'error.content', $error );
129      $vtp->closeSession( $handle, 'error' );
130    }
131    $vtp->closeSession( $handle, 'errors' );
132  }
133  // infos display
134  if ( sizeof( $infos ) != 0 )
135  {
136    $vtp->addSession( $handle, 'infos' );
137    foreach ( $infos as $info ) {
138      $vtp->addSession( $handle, 'info' );
139      $vtp->setVar( $handle, 'info.content', $info );
140      $vtp->closeSession( $handle, 'info' );
141    }
142    $vtp->closeSession( $handle, 'infos' );
143  }
144  // form display (if necessary)
145  if ( !isset( $_POST['submit'] ) or sizeof( $errors ) > 0 )
146  {
147    $vtp->addSession( $handle, 'step1' );
148
149    // host
150    if ( !isset( $_POST['cfgHote'] ) )
151      $vtp->setVar( $handle, 'step1.f_host', 'localhost' );
152    else
153      $vtp->setVar( $handle, 'step1.f_host', $_POST['cfgHote'] );
154    // user
155        if ( isset( $_POST['cfgUser'] ) )
156            $vtp->setVar( $handle, 'step1.f_user', $_POST['cfgUser'] );
157    // base
158        if ( isset( $_POST['cfgBase'] ) )
159            $vtp->setVar( $handle, 'step1.f_base', $_POST['cfgBase'] );
160    // prefixeTable
161    if ( !isset( $_POST['prefixeTable'] ) )
162      $vtp->setVar( $handle, 'step1.f_prefixeTable', 'phpwebgallery_' );
163    else
164      $vtp->setVar( $handle, 'step1.f_prefixeTable', $_POST['prefixeTable'] );
165
166    $vtp->closeSession( $handle, 'step1' );
167  }
168}
169//------------------------------------- Step 2 : creation of tables in database
170else if (  isset($_GET['step']) && $_GET['step'] == 2 )
171{
172  $errors = array();
173  $infos  = array();
174
175  include( '../include/mysql.inc.php' );
176  mysql_connect( $cfgHote, $cfgUser, $cfgPassword )
177    or die ( "Can't connect to database host" );
178  mysql_select_db( $cfgBase )
179    or die ( "Connection to host succeeded, but database selection failed" );
180               
181  if ( !isset( $_POST['submit'] ) )
182  {
183    // tables creation, based on phpwebgallery_structure.sql
184    $sql_lines = file( './phpwebgallery_structure.sql' );
185    $query = '';
186    foreach ( $sql_lines as $sql_line ) {
187      $sql_line = trim( $sql_line );
188      if ( preg_match( '/(^--|^$)/', $sql_line ) ) continue;
189      $query.= ' '.$sql_line;
190      // if we reached the end of query, we execute it and reinitialize the
191      // variable "query"
192      if ( preg_match( '/;$/', $sql_line ) )
193      {
194        $query = trim( $query );
195        $query = str_replace( 'phpwebgallery_', $prefixeTable, $query );
196        // we don't execute "DROP TABLE" queries
197        if ( !preg_match( '/^DROP TABLE/i', $query ) )
198          mysql_query( $query );
199        $query = '';
200      }
201    }
202  }
203
204  if ( isset( $_POST['submit'] ) )
205  {
206    // webmaster login must be
207    // 1. non empty
208    // 2. without characters ' or "
209    $webmaster = preg_replace( '/\s{2,}/', ' ', $_POST['webmaster'] );
210    $webmaster = trim( $webmaster );
211    if ( $webmaster == '' )
212      array_push( $errors, $lang['step2_err_login1'] );
213    if ( preg_match( '/[\'"]/', $webmaster ) )
214      array_push( $errors, $lang['step2_err_login3'] );
215    // the webmaster string must be the same as its confirmation
216    if ( $_POST['pwdWebmaster'] != $_POST['pwdWebmasterConf'] )
217      array_push( $errors, $lang['step2_err_pass'] );
218    // mail address must have this format : name@server.com
219    $error_mail_address = validate_mail_address( $_POST['mail_webmaster'] );
220    if ( $error_mail_address != '' )
221      array_push( $errors, $error_mail_address );
222    if ( $_POST['mail_webmaster'] == '' )
223      array_push( $errors, $lang['reg_err_mail_address'] );
224
225    // if no error found till here : insertion of data in tables
226    if ( count( $errors ) == 0 )
227    {
228      $query = 'DELETE FROM '.$prefixeTable.'config';
229      mysql_query( $query );
230
231      $query = 'INSERT INTO '.$prefixeTable.'config';
232      $query.= ' (webmaster,mail_webmaster) VALUES ';
233      $query.= " ('".$webmaster."','".$_POST['mail_webmaster']."')";
234      $query.= ';';
235      mysql_query( $query );
236
237      $query = 'INSERT INTO '.$prefixeTable.'sites';
238      $query.= " (id,galleries_url) VALUES (1, './galleries/')";
239      $query.= ';';
240      mysql_query( $query );
241
242      // webmaster admin user
243      $query = 'INSERT INTO '.$prefixeTable.'users';
244      $query.= ' (id,username,password,status,language,mail_address) VALUES ';
245      $query.= "(1,'".$webmaster."','".md5( $_POST['pwdWebmaster'] )."'";
246      $query.= ",'admin','".$_GET['language']."'";
247      $query.= ",'".$_POST['mail_webmaster']."')";
248      $query.= ';';
249      mysql_query($query);
250
251      // guest user
252      $query = 'INSERT INTO '.$prefixeTable.'users';
253      $query.= '(id,username,password,status,language) VALUES ';
254      $query.= "(2,'guest','','guest','".$_GET['language']."')";
255      $query.= ';';
256      mysql_query( $query );
257    }
258  }
259
260  // errors display
261  if ( sizeof( $errors ) != 0 )
262  {
263    $vtp->addSession( $handle, 'errors' );
264    foreach ( $errors as $error ) {
265      $vtp->addSession( $handle, 'error' );
266      $vtp->setVar( $handle, 'error.content', $error );
267      $vtp->closeSession( $handle, 'error' );
268      }
269    $vtp->closeSession( $handle, 'errors' );
270  }
271
272  if ( !isset( $_POST['submit'] ) or sizeof( $errors ) > 0 )
273  {
274    $vtp->addSession( $handle, 'step2' );
275        if ( isset( $_POST['webmaster'] ))
276    $vtp->setVar( $handle, 'step2.f_webmaster', $_POST['webmaster'] );
277        if ( isset( $_POST['mail_webmaster'] ))
278    $vtp->setVar( $handle, 'step2.f_mail_webmaster', $_POST['mail_webmaster']);
279    $vtp->closeSession( $handle, 'step2' );
280  }
281
282  // end of installation message
283  if ( isset( $_POST['submit'] ) and count( $errors ) == 0 )
284  {
285    $vtp->addSession( $handle, 'install_end' );
286    $vtp->closeSession( $handle, 'install_end' );
287  }
288}
289//---------------------------------------------------- Step 0 : language choice
290else
291{
292  $vtp->addSession( $handle, 'step0' );
293  $languages = get_languages( '../language/' );
294  foreach ( $languages as $language ) {
295    $vtp->addSession( $handle, 'language' );
296    $vtp->setVar( $handle, 'language.name', $language );
297    $vtp->closeSession( $handle, 'language' );
298  }
299  $vtp->closeSession( $handle, 'step0' );
300}
301//----------------------------------------------------------- html code display
302$code = $vtp->Display( $handle, 0 );
303echo $code;
304?>
Note: See TracBrowser for help on using the repository browser.