source: trunk/install.php @ 354

Last change on this file since 354 was 354, checked in by gweltas, 20 years ago

Migration of common.php in the include directory to fit the new coding rules

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