source: trunk/install.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: install.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48//----------------------------------------------------------- include
49define('PHPWG_ROOT_PATH','./');
50
51/**
52 * loads an sql file and executes all queries
53 *
54 * Before executing a query, $replaced is... replaced by $replacing. This is
55 * useful when the SQL file contains generic words. Drop table queries are
56 * not executed.
57 *
58 * @param string filepath
59 * @param string replaced
60 * @param string replacing
61 * @return void
62 */
63function execute_sqlfile($filepath, $replaced, $replacing)
64{
65  $sql_lines = file($filepath);
66  $query = '';
67  foreach ($sql_lines as $sql_line)
68  {
69    $sql_line = trim($sql_line);
70    if (preg_match('/(^--|^$)/', $sql_line))
71    {
72      continue;
73    }
74    $query.= ' '.$sql_line;
75    // if we reached the end of query, we execute it and reinitialize the
76    // variable "query"
77    if (preg_match('/;$/', $sql_line))
78    {
79      $query = trim($query);
80      $query = str_replace($replaced, $replacing, $query);
81      // we don't execute "DROP TABLE" queries
82      if (!preg_match('/^DROP TABLE/i', $query))
83      {
84        global $install_charset_collate;
85        if ( !empty($install_charset_collate) )
86        {
87          if ( preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches) )
88          {
89            $query = $matches[1].' '.$install_charset_collate.';';
90          }
91        }
92        mysql_query($query);
93      }
94      $query = '';
95    }
96  }
97}
98
99set_magic_quotes_runtime(0); // Disable magic_quotes_runtime
100//
101// addslashes to vars if magic_quotes_gpc is off this is a security
102// precaution to prevent someone trying to break out of a SQL statement.
103//
104if( !get_magic_quotes_gpc() )
105{
106  if( is_array($_POST) )
107  {
108    while( list($k, $v) = each($_POST) )
109    {
110      if( is_array($_POST[$k]) )
111      {
112        while( list($k2, $v2) = each($_POST[$k]) )
113        {
114          $_POST[$k][$k2] = addslashes($v2);
115        }
116        @reset($_POST[$k]);
117      }
118      else
119      {
120        $_POST[$k] = addslashes($v);
121      }
122    }
123    @reset($_POST);
124  }
125
126  if( is_array($_GET) )
127  {
128    while( list($k, $v) = each($_GET) )
129    {
130      if( is_array($_GET[$k]) )
131      {
132        while( list($k2, $v2) = each($_GET[$k]) )
133        {
134          $_GET[$k][$k2] = addslashes($v2);
135        }
136        @reset($_GET[$k]);
137      }
138      else
139      {
140        $_GET[$k] = addslashes($v);
141      }
142    }
143    @reset($_GET);
144  }
145
146  if( is_array($_COOKIE) )
147  {
148    while( list($k, $v) = each($_COOKIE) )
149    {
150      if( is_array($_COOKIE[$k]) )
151      {
152        while( list($k2, $v2) = each($_COOKIE[$k]) )
153        {
154          $_COOKIE[$k][$k2] = addslashes($v2);
155        }
156        @reset($_COOKIE[$k]);
157      }
158      else
159      {
160        $_COOKIE[$k] = addslashes($v);
161      }
162    }
163    @reset($_COOKIE);
164  }
165}
166
167//----------------------------------------------------- variable initialization
168
169define('DEFAULT_PREFIX_TABLE', 'phpwebgallery_');
170
171// Obtain various vars
172$dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
173$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
174$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
175$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
176
177if (isset($_POST['install']))
178{
179  $table_prefix = $_POST['prefix'];
180}
181else
182{
183  $table_prefix = DEFAULT_PREFIX_TABLE;
184}
185
186$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
187$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
188$admin_pass2 = (!empty($_POST['admin_pass2'])) ? $_POST['admin_pass2'] : '';
189$admin_mail = (!empty($_POST['admin_mail'])) ? $_POST['admin_mail'] : '';
190
191$infos = array();
192$errors = array();
193
194// Open config.php ... if it exists
195$config_file = PHPWG_ROOT_PATH.'include/mysql.inc.php';
196if (@file_exists($config_file))
197{
198  include($config_file);
199  // Is PhpWebGallery already installed ?
200  if (defined("PHPWG_INSTALLED"))
201  {
202    die('PhpWebGallery is already installed');
203  }
204}
205
206$prefixeTable = $table_prefix;
207include(PHPWG_ROOT_PATH . 'include/config_default.inc.php');
208@include(PHPWG_ROOT_PATH. 'include/config_local.inc.php');
209include(PHPWG_ROOT_PATH . 'include/constants.php');
210include(PHPWG_ROOT_PATH . 'include/functions.inc.php');
211include(PHPWG_ROOT_PATH . 'admin/include/functions.php');
212include(PHPWG_ROOT_PATH . 'admin/include/functions_upgrade.php');
213include(PHPWG_ROOT_PATH . 'include/template.class.php');
214
215if (isset($_GET['language']))
216{
217  $language = strip_tags($_GET['language']);
218}
219else
220{
221  $language = 'en_UK';
222  // Try to get browser language
223  foreach (get_languages('utf-8') as $language_code => $language_name)
224  {
225    if (substr($language_code,0,2) == @substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2))
226    {
227      $language = $language_code;
228      break;
229    }
230  }
231}
232
233load_language( 'common.lang', '', $language, false, 'utf-8' );
234load_language( 'admin.lang', '', $language, false, 'utf-8' );
235load_language( 'install.lang', '', $language, false, 'utf-8' );
236
237//----------------------------------------------------- template initialization
238$template=new Template(PHPWG_ROOT_PATH.'template/yoga', 'clear');
239$template->set_filenames( array('install'=>'install.tpl') );
240$step = 1;
241//---------------------------------------------------------------- form analyze
242if ( isset( $_POST['install'] ))
243{
244  if ( @mysql_connect( $_POST['dbhost'],
245                       $_POST['dbuser'],
246                       $_POST['dbpasswd'] ) )
247  {
248    if ( @mysql_select_db($_POST['dbname'] ) )
249    {
250      array_push( $infos, l10n('step1_confirmation') );
251    }
252    else
253    {
254      array_push( $errors, l10n('step1_err_db') );
255    }
256    if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
257    {
258      $pwg_charset='utf-8';
259      $pwg_db_charset='utf8';
260      $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
261    }
262    else
263    {
264      $pwg_charset='iso-8859-1';
265      $pwg_db_charset='latin1';
266      $install_charset_collate = '';
267      if ( !array_key_exists($language, get_languages($pwg_charset) ) )
268      {
269        $language='en_UK';
270      }
271    }
272  }
273  else
274  {
275    array_push( $errors, l10n('step1_err_server') );
276  }
277
278  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
279  if ( empty($webmaster))
280    array_push( $errors, l10n('step2_err_login1') );
281  else if ( preg_match( '/[\'"]/', $webmaster ) )
282    array_push( $errors, l10n('step2_err_login3') );
283  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
284    array_push( $errors, l10n('step2_err_pass') );
285  if ( empty($admin_mail))
286    array_push( $errors, l10n('reg_err_mail_address') );
287  else
288  {
289    $error_mail_address = validate_mail_address(null, $admin_mail);
290    if (!empty($error_mail_address))
291      array_push( $errors, $error_mail_address );
292  }
293
294  if ( count( $errors ) == 0 )
295  {
296    $step = 2;
297    $file_content = '<?php
298$cfgBase = \''.$dbname.'\';
299$cfgUser = \''.$dbuser.'\';
300$cfgPassword = \''.$dbpasswd.'\';
301$cfgHote = \''.$dbhost.'\';
302
303$prefixeTable = \''.$table_prefix.'\';
304
305define(\'PHPWG_INSTALLED\', true);
306define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
307define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
308define(\'DB_COLLATE\', \'\');
309
310?'.'>';
311
312    @umask(0111);
313    // writing the configuration file
314    if ( !($fp = @fopen( $config_file, 'w' )))
315    {
316      $html_content = htmlentities( $file_content, ENT_QUOTES );
317      $html_content = nl2br( $html_content );
318      $template->assign('error_copy', $html_content);
319    }
320    @fputs($fp, $file_content, strlen($file_content));
321    @fclose($fp);
322
323    // Create empty local files to avoid log errors
324    create_empty_local_files();
325
326    // tables creation, based on phpwebgallery_structure.sql
327    execute_sqlfile(
328      PHPWG_ROOT_PATH.'install/phpwebgallery_structure.sql',
329      DEFAULT_PREFIX_TABLE,
330      $table_prefix
331      );
332    // We fill the tables with basic informations
333    execute_sqlfile(
334      PHPWG_ROOT_PATH.'install/config.sql',
335      DEFAULT_PREFIX_TABLE,
336      $table_prefix
337      );
338
339    // fill $conf global array
340    load_conf_from_db();
341
342    $insert = array(
343      'id' => 1,
344      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
345      );
346    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
347
348    // webmaster admin user
349    $inserts = array(
350      array(
351        'id'           => 1,
352        'username'     => $admin_name,
353        'password'     => md5($admin_pass1),
354        'mail_address' => $admin_mail,
355        ),
356      array(
357        'id'           => 2,
358        'username'     => 'guest',
359        ),
360      );
361    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
362
363    create_user_infos(array(1,2), array('language' => $language));
364
365    // Available upgrades must be ignored after a fresh installation. To
366    // make PWG avoid upgrading, we must tell it upgrades have already been
367    // made.
368    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
369    define('CURRENT_DATE', $dbnow);
370    $datas = array();
371    foreach (get_available_upgrade_ids() as $upgrade_id)
372    {
373      array_push(
374        $datas,
375        array(
376          'id'          => $upgrade_id,
377          'applied'     => CURRENT_DATE,
378          'description' => 'upgrade included in installation',
379          )
380        );
381    }
382    mass_inserts(
383      UPGRADE_TABLE,
384      array_keys($datas[0]),
385      $datas
386      );
387  }
388}
389
390//------------------------------------------------------ start template output
391foreach (get_languages('utf-8') as $language_code => $language_name)
392{
393  if ($language == $language_code)
394  {
395    $template->assign('language_selection', $language_code);
396  }
397  $languages_options[$language_code] = $language_name;
398}
399$template->assign('language_options', $languages_options);
400
401$template->assign(
402  array(
403    'T_CONTENT_ENCODING' => 'utf-8',
404    'RELEASE'=>PHPWG_VERSION,
405    'F_ACTION' => 'install.php?language=' . $language,
406    'F_DB_HOST'=>$dbhost,
407    'F_DB_USER'=>$dbuser,
408    'F_DB_NAME'=>$dbname,
409    'F_DB_PREFIX' => $table_prefix,
410    'F_ADMIN'=>$admin_name,
411    'F_ADMIN_EMAIL'=>$admin_mail,
412    'L_INSTALL_HELP'=>sprintf(l10n('install_help'), 'http://forum.'.PHPWG_DOMAIN.'/'),
413    ));
414
415//------------------------------------------------------ errors & infos display
416if (count($errors) != 0)
417{
418  $template->assign('errors', $errors);
419}
420
421if (count($infos) != 0 )
422{
423  $template->assign('infos', $infos);
424}
425
426if ($step == 1)
427{
428  $template->assign('install', true);
429}
430
431//----------------------------------------------------------- html code display
432$template->pparse('install');
433?>
Note: See TracBrowser for help on using the repository browser.