source: trunk/install.php @ 3049

Last change on this file since 3049 was 3049, checked in by plg, 15 years ago

Administration: happy new year 2009, all PHP headers updated.

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