source: trunk/install.php @ 2248

Last change on this file since 2248 was 2248, checked in by patdenice, 16 years ago

Install goes smarty.
Get browser language directly.

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