source: trunk/install.php @ 3197

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

merge r3196 from branch 2.0 to trunk

bug 926 fixed: change links to piwigo.org so that they go to existing URLs.

new: if the current language is french, the links go to fr.piwigo.org instead.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 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
214if ('fr_FR' == $language) {
215  define('PHPWG_DOMAIN', 'fr.piwigo.org');
216}
217else {
218  define('PHPWG_DOMAIN', 'piwigo.org');
219}
220define('PHPWG_URL', 'http://'.PHPWG_DOMAIN);
221
222load_language( 'common.lang', '', array('language'=>$language, 'target_charset'=>'utf-8') );
223load_language( 'admin.lang', '', array('language'=>$language, 'target_charset'=>'utf-8') );
224load_language( 'install.lang', '', array('language'=>$language, 'target_charset'=>'utf-8') );
225
226//----------------------------------------------------- template initialization
227$template=new Template(PHPWG_ROOT_PATH.'admin/template/goto', 'roma');
228$template->set_filenames( array('install'=>'install.tpl') );
229$step = 1;
230//---------------------------------------------------------------- form analyze
231if ( isset( $_POST['install'] ))
232{
233  if ( @mysql_connect( $_POST['dbhost'],
234                       $_POST['dbuser'],
235                       $_POST['dbpasswd'] ) )
236  {
237    if ( @mysql_select_db($_POST['dbname'] ) )
238    {
239      array_push( $infos, l10n('step1_confirmation') );
240    }
241    else
242    {
243      array_push( $errors, l10n('step1_err_db') );
244    }
245    if ( version_compare(mysql_get_server_info(), '4.1.0', '>=') )
246    {
247      $pwg_charset='utf-8';
248      $pwg_db_charset='utf8';
249      $install_charset_collate = "DEFAULT CHARACTER SET $pwg_db_charset";
250    }
251    else
252    {
253      $pwg_charset='iso-8859-1';
254      $pwg_db_charset='latin1';
255      $install_charset_collate = '';
256      if ( !array_key_exists($language, get_languages($pwg_charset) ) )
257      {
258        $language='en_UK';
259      }
260    }
261  }
262  else
263  {
264    array_push( $errors, l10n('step1_err_server') );
265  }
266
267  $webmaster = trim(preg_replace( '/\s{2,}/', ' ', $admin_name ));
268  if ( empty($webmaster))
269    array_push( $errors, l10n('step2_err_login1') );
270  else if ( preg_match( '/[\'"]/', $webmaster ) )
271    array_push( $errors, l10n('step2_err_login3') );
272  if ( $admin_pass1 != $admin_pass2 || empty($admin_pass1) )
273    array_push( $errors, l10n('step2_err_pass') );
274  if ( empty($admin_mail))
275    array_push( $errors, l10n('reg_err_mail_address') );
276  else
277  {
278    $error_mail_address = validate_mail_address(null, $admin_mail);
279    if (!empty($error_mail_address))
280      array_push( $errors, $error_mail_address );
281  }
282
283  if ( count( $errors ) == 0 )
284  {
285    $step = 2;
286    $file_content = '<?php
287$cfgBase = \''.$dbname.'\';
288$cfgUser = \''.$dbuser.'\';
289$cfgPassword = \''.$dbpasswd.'\';
290$cfgHote = \''.$dbhost.'\';
291
292$prefixeTable = \''.$table_prefix.'\';
293
294define(\'PHPWG_INSTALLED\', true);
295define(\'PWG_CHARSET\', \''.$pwg_charset.'\');
296define(\'DB_CHARSET\', \''.$pwg_db_charset.'\');
297define(\'DB_COLLATE\', \'\');
298
299?'.'>';
300
301    @umask(0111);
302    // writing the configuration file
303    if ( !($fp = @fopen( $config_file, 'w' )))
304    {
305      $html_content = htmlentities( $file_content, ENT_QUOTES );
306      $html_content = nl2br( $html_content );
307      $error_copy = l10n('step1_err_copy');
308      $error_copy .= '<br>--------------------------------------------------------------------<br>';
309      $error_copy .= '<span class="sql_content">' . $html_content . '</span>';
310      $error_copy .= '<br>--------------------------------------------------------------------<br>';
311    }
312    @fputs($fp, $file_content, strlen($file_content));
313    @fclose($fp);
314
315    // Create empty local files to avoid log errors
316    create_empty_local_files();
317
318    // tables creation, based on piwigo_structure.sql
319    execute_sqlfile(
320      PHPWG_ROOT_PATH.'install/piwigo_structure.sql',
321      DEFAULT_PREFIX_TABLE,
322      $table_prefix
323      );
324    // We fill the tables with basic informations
325    execute_sqlfile(
326      PHPWG_ROOT_PATH.'install/config.sql',
327      DEFAULT_PREFIX_TABLE,
328      $table_prefix
329      );
330
331    // fill $conf global array
332    load_conf_from_db();
333
334    $insert = array(
335      'id' => 1,
336      'galleries_url' => PHPWG_ROOT_PATH.'galleries/',
337      );
338    mass_inserts(SITES_TABLE, array_keys($insert), array($insert));
339
340    // webmaster admin user
341    $inserts = array(
342      array(
343        'id'           => 1,
344        'username'     => $admin_name,
345        'password'     => md5($admin_pass1),
346        'mail_address' => $admin_mail,
347        ),
348      array(
349        'id'           => 2,
350        'username'     => 'guest',
351        ),
352      );
353    mass_inserts(USERS_TABLE, array_keys($inserts[0]), $inserts);
354
355    create_user_infos(array(1,2), array('language' => $language));
356
357    // Available upgrades must be ignored after a fresh installation. To
358    // make PWG avoid upgrading, we must tell it upgrades have already been
359    // made.
360    list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
361    define('CURRENT_DATE', $dbnow);
362    $datas = array();
363    foreach (get_available_upgrade_ids() as $upgrade_id)
364    {
365      array_push(
366        $datas,
367        array(
368          'id'          => $upgrade_id,
369          'applied'     => CURRENT_DATE,
370          'description' => 'upgrade included in installation',
371          )
372        );
373    }
374    mass_inserts(
375      UPGRADE_TABLE,
376      array_keys($datas[0]),
377      $datas
378      );
379  }
380}
381
382//------------------------------------------------------ start template output
383foreach (get_languages('utf-8') as $language_code => $language_name)
384{
385  if ($language == $language_code)
386  {
387    $template->assign('language_selection', $language_code);
388  }
389  $languages_options[$language_code] = $language_name;
390}
391$template->assign('language_options', $languages_options);
392
393$template->assign(
394  array(
395    'T_CONTENT_ENCODING' => 'utf-8',
396    'RELEASE'=>PHPWG_VERSION,
397    'F_ACTION' => 'install.php?language=' . $language,
398    'F_DB_HOST'=>$dbhost,
399    'F_DB_USER'=>$dbuser,
400    'F_DB_NAME'=>$dbname,
401    'F_DB_PREFIX' => $table_prefix,
402    'F_ADMIN'=>$admin_name,
403    'F_ADMIN_EMAIL'=>$admin_mail,
404    'L_INSTALL_HELP'=>sprintf(l10n('install_help'), PHPWG_URL.'/forum'),
405    ));
406
407//------------------------------------------------------ errors & infos display
408if ($step == 1)
409{
410  $template->assign('install', true);
411}
412else
413{
414  array_push($infos, l10n('install_end_message'));
415
416  if (isset($error_copy))
417  {
418    array_push($errors, $error_copy);
419  }
420}
421if (count($errors) != 0)
422{
423  $template->assign('errors', $errors);
424}
425
426if (count($infos) != 0 )
427{
428  $template->assign('infos', $infos);
429}
430
431//----------------------------------------------------------- html code display
432$template->pparse('install');
433?>
Note: See TracBrowser for help on using the repository browser.