Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        backend
5
 * @package         install
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version      	$Id: save.php 1671 2012-04-18 06:48:57Z Luisehahne $
14
 * @filesource		$HeadURL:  $
15
 * @lastmodified    $Date: $
16
 *
17
 */
18

    
19
$debug = true;
20

    
21
if (true === $debug) {
22
	ini_set('display_errors', 1);
23
	error_reporting(E_ALL);
24
}
25
// Start a session
26
if(!defined('SESSION_STARTED')) {
27
	session_name('wb_session_id');
28
	session_start();
29
	define('SESSION_STARTED', true);
30
}
31
// get random-part for session_name()
32
list($usec,$sec) = explode(' ',microtime());
33
srand((float)$sec+((float)$usec*100000));
34
$session_rand = rand(1000,9999);
35

    
36
// Function to set error
37
function set_error($message, $field_name = '') {
38
	global $_POST;
39
	if(isset($message) AND $message != '') {
40
		// Copy values entered into session so user doesn't have to re-enter everything
41
		if(isset($_POST['website_title'])) {
42
			$_SESSION['wb_url'] = $_POST['wb_url'];
43
			$_SESSION['default_timezone'] = $_POST['default_timezone'];
44
			$_SESSION['default_language'] = $_POST['default_language'];
45
			if(!isset($_POST['operating_system'])) {
46
				$_SESSION['operating_system'] = 'linux';
47
			} else {
48
				$_SESSION['operating_system'] = $_POST['operating_system'];
49
			}
50
			if(!isset($_POST['world_writeable'])) {
51
				$_SESSION['world_writeable'] = false;
52
			} else {
53
				$_SESSION['world_writeable'] = true;
54
			}
55
			$_SESSION['database_host'] = $_POST['database_host'];
56
			$_SESSION['database_username'] = $_POST['database_username'];
57
			$_SESSION['database_password'] = $_POST['database_password'];
58
			$_SESSION['database_name'] = $_POST['database_name'];
59
			$_SESSION['table_prefix'] = $_POST['table_prefix'];
60
			if(!isset($_POST['install_tables'])) {
61
				$_SESSION['install_tables'] = false;
62
			} else {
63
				$_SESSION['install_tables'] = true;
64
			}
65
			$_SESSION['website_title'] = $_POST['website_title'];
66
			$_SESSION['admin_username'] = $_POST['admin_username'];
67
			$_SESSION['admin_email'] = $_POST['admin_email'];
68
			$_SESSION['admin_password'] = $_POST['admin_password'];
69
			$_SESSION['admin_repassword'] = $_POST['admin_repassword'];
70
		}
71
		// Set the message
72
		$_SESSION['message'] = $message;
73
		// Set the element(s) to highlight
74
		if($field_name != '') {
75
			$_SESSION['ERROR_FIELD'] = $field_name;
76
		}
77
		// Specify that session support is enabled
78
		$_SESSION['session_support'] = '<font class="good">Enabled</font>';
79
		// Redirect to first page again and exit
80
		header('Location: index.php?sessions_checked=true');
81
		exit();
82
	}
83
}
84
/* */
85

    
86
// Function to workout what the default permissions are for files created by the webserver
87
function default_file_mode($temp_dir) {
88
	$v = explode(".",PHP_VERSION);
89
	$v = $v[0].$v[1];
90
	if($v > 41 AND is_writable($temp_dir)) {
91
		$filename = $temp_dir.'/test_permissions.txt';
92
		$handle = fopen($filename, 'w');
93
		fwrite($handle, 'This file is to get the default file permissions');
94
		fclose($handle);
95
		$default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
96
		unlink($filename);
97
	} else {
98
		$default_file_mode = '0777';
99
	}
100
	return $default_file_mode;
101
}
102

    
103
// Function to workout what the default permissions are for directories created by the webserver
104
function default_dir_mode($temp_dir) {
105
	$v = explode(".",PHP_VERSION);
106
	$v = $v[0].$v[1];
107
	if($v > 41 AND is_writable($temp_dir)) {
108
		$dirname = $temp_dir.'/test_permissions/';
109
		mkdir($dirname);
110
		$default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
111
		rmdir($dirname);
112
	} else {
113
		$default_dir_mode = '0777';
114
	}
115
	return $default_dir_mode;
116
}
117

    
118
function add_slashes($input) {
119
	if ( get_magic_quotes_gpc() || ( !is_string($input) ) ) {
120
		return $input;
121
	}
122
	$output = addslashes($input);
123
	return $output;
124
}
125

    
126
// Begin check to see if form was even submitted
127
// Set error if no post vars found
128
if(!isset($_POST['website_title'])) {
129
	set_error('Please fill-in the form below');
130
}
131
// End check to see if form was even submitted
132

    
133
// Begin path and timezone details code
134

    
135
// Check if user has entered the installation url
136
if(!isset($_POST['wb_url']) OR $_POST['wb_url'] == '') {
137
	set_error('Please enter an absolute URL', 'wb_url');
138
} else {
139
	$wb_url = $_POST['wb_url'];
140
}
141
// Remove any slashes at the end of the URL
142
if(substr($wb_url, strlen($wb_url)-1, 1) == "/") {
143
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
144
}
145
if(substr($wb_url, strlen($wb_url)-1, 1) == "\\") {
146
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
147
}
148
if(substr($wb_url, strlen($wb_url)-1, 1) == "/") {
149
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
150
}
151
if(substr($wb_url, strlen($wb_url)-1, 1) == "\\") {
152
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
153
}
154
// Get the default time zone
155
if(!isset($_POST['default_timezone']) OR !is_numeric($_POST['default_timezone'])) {
156
	set_error('Please select a valid default timezone', 'default_timezone');
157
} else {
158
	$default_timezone = $_POST['default_timezone']*60*60;
159
}
160
// End path and timezone details code
161

    
162
// Get the default language
163
$allowed_languages = array('BG','CA', 'CS', 'DA', 'DE', 'EN', 'ES', 'ET', 'FI', 'FR', 'HR', 'HU', 'IT', 'LV', 'NL', 'NO', 'PL', 'PT', 'RU','SE','SK','TR');
164
if(!isset($_POST['default_language']) OR !in_array($_POST['default_language'], $allowed_languages)) {
165
	set_error('Please select a valid default backend language','default_language');
166
} else {
167
	$default_language = $_POST['default_language'];
168
	// make sure the selected language file exists in the language folder
169
	if(!file_exists('../languages/' .$default_language .'.php')) {
170
		set_error('The language file: \'' .$default_language .'.php\' is missing. Upload file to language folder or choose another language','default_language');
171
	}
172
}
173
// End default language details code
174

    
175
// Begin operating system specific code
176
// Get operating system
177
if(!isset($_POST['operating_system']) OR $_POST['operating_system'] != 'linux' AND $_POST['operating_system'] != 'windows') {
178
	set_error('Please select a valid operating system');
179
} else {
180
	$operating_system = $_POST['operating_system'];
181
}
182
// Work-out file permissions
183
if($operating_system == 'windows') {
184
	$file_mode = '0777';
185
	$dir_mode = '0777';
186
} elseif(isset($_POST['world_writeable']) AND $_POST['world_writeable'] == 'true') {
187
	$file_mode = '0777';
188
	$dir_mode = '0777';
189
} else {
190
	$file_mode = default_file_mode('../temp');
191
	$dir_mode = default_dir_mode('../temp');
192
}
193
// End operating system specific code
194

    
195
// Begin database details code
196
// Check if user has entered a database host
197
if(!isset($_POST['database_host']) OR $_POST['database_host'] == '') {
198
	set_error('Please enter a database host name', 'database_host');
199
} else {
200
	$database_host = $_POST['database_host'];
201
}
202
// Check if user has entered a database username
203
if(!isset($_POST['database_username']) OR $_POST['database_username'] == '') {
204
	set_error('Please enter a database username','database_username');
205
} else {
206
	$database_username = $_POST['database_username'];
207
}
208
// Check if user has entered a database password
209
if(!isset($_POST['database_password'])) {
210
	set_error('Please enter a database password', 'database_password');
211
} else {
212
	$database_password = $_POST['database_password'];
213
}
214
// Check if user has entered a database name
215
if(!isset($_POST['database_name']) OR $_POST['database_name'] == '') {
216
	set_error('Please enter a database name', 'database_name');
217
} else {
218
	// make sure only allowed characters are specified
219
	if(preg_match('/[^a-z0-9_-]+/i', $_POST['database_name'])) {
220
		// contains invalid characters (only a-z, A-Z, 0-9 and _ allowed to avoid problems with table/field names)
221
		set_error('Only characters a-z, A-Z, 0-9, - and _ allowed in database name.', 'database_name');
222
	}
223
	$database_name = $_POST['database_name'];
224
}
225
// Get table prefix
226
if(preg_match('/[^a-z0-9_]+/i', $_POST['table_prefix'])) {
227
	// contains invalid characters (only a-z, A-Z, 0-9 and _ allowed to avoid problems with table/field names)
228
	set_error('Only characters a-z, A-Z, 0-9 and _ allowed in table_prefix.', 'table_prefix');
229
} else {
230
	$table_prefix = $_POST['table_prefix'];
231
}
232

    
233
// Find out if the user wants to install tables and data
234
if(isset($_POST['install_tables']) AND $_POST['install_tables'] == 'true') {
235
	$install_tables = true;
236
} else {
237
	$install_tables = false;
238
}
239
// End database details code
240

    
241
// Begin website title code
242
// Get website title
243
if(!isset($_POST['website_title']) OR $_POST['website_title'] == '') {
244
	set_error('Please enter a website title', 'website_title');
245
} else {
246
	$website_title = add_slashes($_POST['website_title']);
247
}
248
// End website title code
249

    
250
// Begin admin user details code
251
// Get admin username
252
if(!isset($_POST['admin_username']) OR $_POST['admin_username'] == '') {
253
	set_error('Please enter a username for the Administrator account','admin_username');
254
} else {
255
	$admin_username = $_POST['admin_username'];
256
}
257
// Get admin email and validate it
258
if(!isset($_POST['admin_email']) OR $_POST['admin_email'] == '') {
259
	set_error('Please enter an email for the Administrator account','admin_email');
260
} else {
261
	if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i', $_POST['admin_email'])) {
262
		$admin_email = $_POST['admin_email'];
263
	} else {
264
		set_error('Please enter a valid email address for the Administrator account','admin_email');
265
	}
266
}
267
// Get the two admin passwords entered, and check that they match
268
if(!isset($_POST['admin_password']) OR $_POST['admin_password'] == '') {
269
	set_error('Please enter a password for the Administrator account','admin_password');
270
} else {
271
	$admin_password = $_POST['admin_password'];
272
}
273
if(!isset($_POST['admin_repassword']) OR $_POST['admin_repassword'] == '') {
274
	set_error('Please make sure you re-enter the password for the Administrator account','admin_repassword');
275
} else {
276
	$admin_repassword = $_POST['admin_repassword'];
277
}
278
if($admin_password != $admin_repassword) {
279
	set_error('Sorry, the two Administrator account passwords you entered do not match','admin_repassword');
280
}
281
// End admin user details code
282

    
283
// Try and write settings to config file
284
$config_content = "" .
285
"<?php\n".
286
"\n".
287
"define('DEBUG', false);\n".
288
"define('DB_TYPE', 'mysql');\n".
289
"define('DB_HOST', '$database_host');\n".
290
"define('DB_NAME', '$database_name');\n".
291
"define('DB_USERNAME', '$database_username');\n".
292
"define('DB_PASSWORD', '$database_password');\n".
293
"define('TABLE_PREFIX', '$table_prefix');\n".
294
"\n".
295
"define('WB_URL', '$wb_url');\n".
296
"define('ADMIN_DIRECTORY', 'admin'); // no leading/trailing slash or backslash!! A simple directory only!!\n".
297
"\n".
298
"require_once(dirname(__FILE__).'/framework/initialize.php');\n".
299
"\n";
300

    
301
$config_filename = '../config.php';
302
// Check if the file exists and is writable first.
303
if(file_exists($config_filename) AND is_writable($config_filename)) {
304
	if(!$handle = fopen($config_filename, 'w')) {
305
		set_error("Cannot open the configuration file ($config_filename)");
306
	} else {
307
		if (fwrite($handle, $config_content) === FALSE) {
308
			set_error("Cannot write to the configuration file ($config_filename)");
309
		}
310
		// Close file
311
		fclose($handle);
312
	}
313
} else {
314
	set_error("The configuration file $config_filename is not writable. Change its permissions so it is, then re-run step 4.");
315
}
316

    
317
// Define configuration vars
318
define('DEBUG', false);
319
define('DB_TYPE', 'mysql');
320
define('DB_HOST', $database_host);
321
define('DB_NAME', $database_name);
322
define('DB_USERNAME', $database_username);
323
define('DB_PASSWORD', $database_password);
324
define('TABLE_PREFIX', $table_prefix);
325
define('WB_PATH', str_replace(array('/install','\install'), '',dirname(__FILE__)));
326
define('WB_URL', $wb_url);
327
define('ADMIN_PATH', WB_PATH.'/admin');
328
define('ADMIN_URL', $wb_url.'/admin');
329

    
330
// Check if the user has entered a correct path
331
if(!file_exists(WB_PATH.'/framework/class.admin.php')) {
332
	set_error('It appears the Absolute path that you entered is incorrect');
333
}
334

    
335
// Try connecting to database	
336
if(!@mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD)) {
337
	set_error('Database host name, username and/or password incorrect. MySQL Error:<br />'.mysql_error());
338
}
339

    
340
// Try to create the database
341
mysql_query('CREATE DATABASE `'.$database_name.'`');
342

    
343
// Close the mysql connection
344
mysql_close();
345

    
346
$sSecMod = (defined('SECURE_FORM_MODULE') && SECURE_FORM_MODULE != '') ? '.'.SECURE_FORM_MODULE : '';
347
$sSecMod = WB_PATH.'/framework/SecureForm'.$sSecMod.'.php';
348
require_once($sSecMod);
349

    
350
require_once(WB_PATH.'/framework/class.admin.php');
351

    
352
// Dummy class to allow modules' install scripts to call $admin->print_error
353
class admin_dummy extends admin
354
{
355
	var $error='';
356
	function print_error($message, $link = 'index.php', $auto_footer = true)
357
	{
358
		$this->error=$message;
359
	}
360
}
361

    
362
// Include WB functions file
363
require_once(WB_PATH.'/framework/functions.php');
364

    
365
// Re-connect to the database, this time using in-build database class
366
require_once(WB_PATH.'/framework/class.login.php');
367

    
368
$database=new database();
369

    
370
// Check if we should install tables
371
if($install_tables == true) {
372
	if (!defined('WB_INSTALL_PROCESS')) define ('WB_INSTALL_PROCESS', true);
373
	// Remove tables if they exist
374

    
375
	// Pages table
376
	$pages = "DROP TABLE IF EXISTS `".TABLE_PREFIX."pages`";
377
	$database->query($pages);
378
	// Sections table
379
	$sections = "DROP TABLE IF EXISTS `".TABLE_PREFIX."sections`";
380
	$database->query($sections);
381
	// Settings table
382
	$settings = "DROP TABLE IF EXISTS `".TABLE_PREFIX."settings`";
383
	$database->query($settings);
384
	// Users table
385
	$users = "DROP TABLE IF EXISTS `".TABLE_PREFIX."users`";
386
	$database->query($users);
387
	// Groups table
388
	$groups = "DROP TABLE IF EXISTS `".TABLE_PREFIX."groups`";
389
	$database->query($groups);
390
	// Search table
391
	$search = "DROP TABLE IF EXISTS `".TABLE_PREFIX."search`";
392
	$database->query($search);
393
	// Addons table
394
	$addons = "DROP TABLE IF EXISTS `".TABLE_PREFIX."addons`";
395
	$database->query($addons);
396
				
397
	// Try installing tables
398
	
399
	// Pages table
400
	$pages = 'CREATE TABLE `'.TABLE_PREFIX.'pages` ( `page_id` INT NOT NULL auto_increment,'
401
	       . ' `parent` INT NOT NULL DEFAULT \'0\','
402
	       . ' `root_parent` INT NOT NULL DEFAULT \'0\','
403
	       . ' `level` INT NOT NULL DEFAULT \'0\','
404
	       . ' `link` VARCHAR( 255 ) NOT NULL,'
405
	       . ' `target` VARCHAR( 7 ) NOT NULL DEFAULT \'\' ,'
406
	       . ' `page_title` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
407
		   . ' `page_icon` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
408
	       . ' `menu_title` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
409
		   . ' `menu_icon_0` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
410
		   . ' `menu_icon_1` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
411
	       . ' `description` TEXT NOT NULL ,'
412
	       . ' `keywords` TEXT NOT NULL ,'
413
	       . ' `page_trail` VARCHAR( 255 ) NOT NULL  ,'
414
	       . ' `template` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
415
	       . ' `visibility` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
416
	       . ' `position` INT NOT NULL DEFAULT \'0\','
417
	       . ' `menu` INT NOT NULL DEFAULT \'0\','
418
	       . ' `language` VARCHAR( 5 ) NOT NULL DEFAULT \'\' ,'
419
	       . ' `page_code` INT NOT NULL DEFAULT \'0\','
420
	       . ' `searching` INT NOT NULL DEFAULT \'0\','
421
	       . ' `admin_groups` VARCHAR( 512 ) NOT NULL DEFAULT \'1\' ,'
422
	       . ' `admin_users` VARCHAR( 512 ) NOT NULL ,'
423
	       . ' `viewing_groups` VARCHAR( 512 ) NOT NULL DEFAULT \'1\' ,'
424
	       . ' `viewing_users` VARCHAR( 512 ) NOT NULL ,'
425
	       . ' `modified_when` INT NOT NULL DEFAULT \'0\','
426
	       . ' `modified_by` INT NOT NULL  DEFAULT \'0\','
427
	       . ' PRIMARY KEY ( `page_id` ) '
428
	       . ' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
429
	if(!$database->query($pages)) {
430
	}
431
	
432
	// Sections table
433
	$pages = 'CREATE TABLE `'.TABLE_PREFIX.'sections` ( `section_id` INT NOT NULL auto_increment,'
434
	       . ' `page_id` INT NOT NULL DEFAULT \'0\','
435
	       . ' `position` INT NOT NULL DEFAULT \'0\','
436
	       . ' `module` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
437
	       . ' `block` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
438
	       . ' `publ_start` VARCHAR( 255 ) NOT NULL DEFAULT \'0\' ,'
439
	       . ' `publ_end` VARCHAR( 255 ) NOT NULL DEFAULT \'0\' ,' 
440
	       . ' PRIMARY KEY ( `section_id` ) '
441
	       . ' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
442
	$database->query($pages);
443

    
444
	require(ADMIN_PATH.'/interface/version.php');
445
	
446
	// Settings table
447
	$settings='CREATE TABLE `'.TABLE_PREFIX.'settings` ( `setting_id` INT NOT NULL auto_increment,'
448
		. ' `name` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
449
		. ' `value` TEXT NOT NULL ,'
450
		. ' PRIMARY KEY ( `setting_id` ) '
451
		. ' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
452
	$database->query($settings);
453

    
454
	$settings_rows=	"INSERT INTO `".TABLE_PREFIX."settings` "
455
	." (name, value) VALUES "
456
	." ('wb_version', '".VERSION."'),"
457
	." ('wb_revision', '".REVISION."'),"
458
 	." ('wb_sp', '".SP."'),"
459
	." ('website_title', '$website_title'),"
460
	." ('website_description', ''),"
461
	." ('website_keywords', ''),"
462
	." ('website_header', ''),"
463
	." ('website_footer', ''),"
464
	." ('wysiwyg_style', 'font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px;'),"
465
	." ('er_level', ''),"
466
	." ('default_language', '$default_language'),"
467
	." ('app_name', 'wb_$session_rand'),"
468
	." ('sec_anchor', 'wb_'),"
469
	." ('default_timezone', '$default_timezone'),"
470
	." ('default_date_format', 'M d Y'),"
471
	." ('default_time_format', 'g:i A'),"
472
	." ('redirect_timer', '1500'),"
473
	." ('home_folders', 'true'),"
474
	." ('warn_page_leave', '1'),"
475
	." ('default_template', 'round'),"
476
	." ('default_theme', 'wb_theme'),"
477
	." ('default_charset', 'utf-8'),"
478
	." ('multiple_menus', 'true'),"
479
	." ('page_level_limit', '4'),"
480
	." ('intro_page', 'false'),"
481
	." ('page_trash', 'inline'),"
482
	." ('homepage_redirection', 'false'),"
483
	." ('page_languages', 'true'),"
484
	." ('wysiwyg_editor', 'fckeditor'),"
485
	." ('manage_sections', 'true'),"
486
	." ('section_blocks', 'true'),"
487
	." ('smart_login', 'false'),"
488
	." ('frontend_login', 'false'),"
489
	." ('frontend_signup', 'false'),"
490
	." ('search', 'public'),"
491
	." ('page_extension', '.php'),"
492
	." ('page_spacer', '-'),"
493
	." ('dev_infos', 'true'),"
494
	." ('pages_directory', '/pages'),"
495
	." ('page_icon_dir', '/templates/*/title_images'),"
496
	." ('rename_files_on_upload', 'ph.*?,cgi,pl,pm,exe,com,bat,pif,cmd,src,asp,aspx'),"
497
	." ('media_directory', '/media'),"
498
	." ('operating_system', '$operating_system'),"
499
	." ('string_file_mode', '$file_mode'),"
500
	." ('string_dir_mode', '$dir_mode'),"
501
	." ('wbmailer_routine', 'phpmail'),"
502
	." ('server_email', '$admin_email'),"		// avoid that mail provider (e.g. mail.com) reject mails like yourname@mail.com
503
	." ('wbmailer_default_sendername', 'WB Mailer'),"
504
	." ('wbmailer_smtp_host', ''),"
505
	." ('wbmailer_smtp_auth', ''),"
506
	." ('wbmailer_smtp_username', ''),"
507
	." ('wbmailer_smtp_password', ''),"
508
	." ('fingerprint_with_ip_octets', '2'),"
509
	." ('secure_form_module', ''),"
510
	." ('mediasettings', '')";
511
	$database->query($settings_rows);
512
	
513
	// Users table
514
	$users = 'CREATE TABLE `'.TABLE_PREFIX.'users` ( `user_id` INT NOT NULL auto_increment,'
515
	       . ' `group_id` INT NOT NULL DEFAULT \'0\','
516
	       . ' `groups_id` VARCHAR( 255 ) NOT NULL DEFAULT \'0\','
517
	       . ' `active` INT NOT NULL DEFAULT \'0\','
518
	       . ' `username` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
519
	       . ' `password` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
520
	       . ' `remember_key` VARCHAR( 255 ) NOT NULL DEFAULT \'\','
521
	       . ' `last_reset` INT NOT NULL DEFAULT \'0\','
522
	       . ' `display_name` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
523
	       . ' `email` TEXT NOT NULL ,'
524
	       . ' `timezone` INT NOT NULL DEFAULT \'0\','
525
	       . ' `date_format` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
526
	       . ' `time_format` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
527
	       . ' `language` VARCHAR( 5 ) NOT NULL DEFAULT \'' .$default_language .'\' ,'
528
	       . ' `home_folder` TEXT NOT NULL ,'
529
	       . ' `login_when` INT NOT NULL  DEFAULT \'0\','
530
	       . ' `login_ip` VARCHAR( 15 ) NOT NULL DEFAULT \'\' ,'
531
	       . ' PRIMARY KEY ( `user_id` ) '
532
	       . ' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
533
	$database->query($users);
534
	
535
	// Groups table
536
	$groups = 'CREATE TABLE `'.TABLE_PREFIX.'groups` ( `group_id` INT NOT NULL auto_increment,'
537
	        . ' `name` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
538
	        . ' `system_permissions` TEXT NOT NULL ,'
539
	        . ' `module_permissions` TEXT NOT NULL ,'
540
	        . ' `template_permissions` TEXT NOT NULL ,'
541
	        . ' PRIMARY KEY ( `group_id` ) '
542
	        . ' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
543
	$database->query($groups);
544
	
545
	// Search settings table
546
	$search = 'CREATE TABLE `'.TABLE_PREFIX.'search` ( `search_id` INT NOT NULL auto_increment,'
547
	        . ' `name` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
548
	        . ' `value` TEXT NOT NULL ,'
549
	        . ' `extra` TEXT NOT NULL ,'
550
	        . ' PRIMARY KEY ( `search_id` ) '
551
	        . ' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
552
	$database->query($search);
553
	
554
	// Addons table
555
	$addons = 'CREATE TABLE `'.TABLE_PREFIX.'addons` ( '
556
			.'`addon_id` INT NOT NULL auto_increment ,'
557
			.'`type` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
558
			.'`directory` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
559
			.'`name` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
560
			.'`description` TEXT NOT NULL ,'
561
			.'`function` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
562
			.'`version` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
563
			.'`platform` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
564
			.'`author` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
565
			.'`license` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
566
			.' PRIMARY KEY ( `addon_id` ) '
567
			.' ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
568
	$database->query($addons);
569

    
570
	// Insert default data
571
	
572
	// Admin group
573
	$full_system_permissions = 'pages,pages_view,pages_add,pages_add_l0,pages_settings,pages_modify,pages_intro,pages_delete,media,media_view,media_upload,media_rename,media_delete,media_create,addons,modules,modules_view,modules_install,modules_uninstall,templates,templates_view,templates_install,templates_uninstall,languages,languages_view,languages_install,languages_uninstall,settings,settings_basic,settings_advanced,access,users,users_view,users_add,users_modify,users_delete,groups,groups_view,groups_add,groups_modify,groups_delete,admintools';
574
	$insert_admin_group = "INSERT INTO `".TABLE_PREFIX."groups` VALUES ('1', 'Administrators', '$full_system_permissions', '', '')";
575
	$database->query($insert_admin_group);
576
	// Admin user
577
	$insert_admin_user = "INSERT INTO `".TABLE_PREFIX."users` (user_id,group_id,groups_id,active,username,password,email,display_name) VALUES ('1','1','1','1','$admin_username','".md5($admin_password)."','$admin_email','Administrator')";
578
	$database->query($insert_admin_user);
579
	
580
	// Search header
581
	$search_header = addslashes('
582
<h1>[TEXT_SEARCH]</h1>
583

    
584
<form name="searchpage" action="[WB_URL]/search/index.php" method="get">
585
<table cellpadding="3" cellspacing="0" border="0" width="500">
586
<tr>
587
<td>
588
<input type="hidden" name="search_path" value="[SEARCH_PATH]" />
589
<input type="text" name="string" value="[SEARCH_STRING]" style="width: 100%;" />
590
</td>
591
<td width="150">
592
<input type="submit" value="[TEXT_SEARCH]" style="width: 100%;" />
593
</td>
594
</tr>
595
<tr>
596
<td colspan="2">
597
<input type="radio" name="match" id="match_all" value="all"[ALL_CHECKED] />
598
<label for="match_all">[TEXT_ALL_WORDS]</label>
599
<input type="radio" name="match" id="match_any" value="any"[ANY_CHECKED] />
600
<label for="match_any">[TEXT_ANY_WORDS]</label>
601
<input type="radio" name="match" id="match_exact" value="exact"[EXACT_CHECKED] />
602
<label for="match_exact">[TEXT_EXACT_MATCH]</label>
603
</td>
604
</tr>
605
</table>
606

    
607
</form>
608

    
609
<hr />
610
	');
611
	$insert_search_header = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'header', '$search_header', '')";
612
	$database->query($insert_search_header);
613
	// Search footer
614
	$search_footer = addslashes('');
615
	$insert_search_footer = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'footer', '$search_footer', '')";
616
	$database->query($insert_search_footer);
617
	// Search results header
618
	$search_results_header = addslashes(''.
619
'[TEXT_RESULTS_FOR] \'<b>[SEARCH_STRING]</b>\':
620
<table cellpadding="2" cellspacing="0" border="0" width="100%" style="padding-top: 10px;">');
621
	$insert_search_results_header = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'results_header', '$search_results_header', '')";
622
	$database->query($insert_search_results_header);
623
	// Search results loop
624
	$search_results_loop = addslashes(''.
625
'<tr style="background-color: #F0F0F0;">
626
<td><a href="[LINK]">[TITLE]</a></td>
627
<td align="right">[TEXT_LAST_UPDATED_BY] [DISPLAY_NAME] ([USERNAME]) [TEXT_ON] [DATE]</td>
628
</tr>
629
<tr><td colspan="2" style="text-align: justify; padding-bottom: 5px;">[DESCRIPTION]</td></tr>
630
<tr><td colspan="2" style="text-align: justify; padding-bottom: 10px;">[EXCERPT]</td></tr>');
631

    
632
	$insert_search_results_loop = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'results_loop', '$search_results_loop', '')";
633
	$database->query($insert_search_results_loop);
634
	// Search results footer
635
	$search_results_footer = addslashes("</table>");
636
	$insert_search_results_footer = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'results_footer', '$search_results_footer', '')";
637
	$database->query($insert_search_results_footer);
638
	// Search no results
639
	$search_no_results = addslashes('<tr><td><p>[TEXT_NO_RESULTS]</p></td></tr>');
640
	$insert_search_no_results = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'no_results', '$search_no_results', '')";
641
	$database->query($insert_search_no_results);
642
	// Search module-order
643
	$search_module_order = addslashes('faqbaker,manual,wysiwyg');
644
	$insert_search_module_order = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'module_order', '$search_module_order', '')";
645
	$database->query($insert_search_module_order);
646
	// Search max lines of excerpt
647
	$search_max_excerpt = addslashes('15');
648
	$insert_search_max_excerpt = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'max_excerpt', '$search_max_excerpt', '')";
649
	$database->query($insert_search_max_excerpt);
650
	// max time to search per module
651
	$search_time_limit = addslashes('0');
652
	$insert_search_time_limit = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'time_limit', '$search_time_limit', '')";
653
	$database->query($insert_search_time_limit);
654
	// some config-elements
655
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_enable_old_search', 'true', '')");
656
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_search_keywords', 'true', '')");
657
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_search_description', 'true', '')");
658
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_show_description', 'true', '')");
659
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_enable_flush', 'false', '')");
660
	// Search template
661
	$database->query("INSERT INTO `".TABLE_PREFIX."search` (name) VALUES ('template')");
662

    
663
	require_once(WB_PATH.'/framework/initialize.php');
664

    
665
	// Include the PclZip class file (thanks to
666
	require_once(WB_PATH.'/include/pclzip/pclzip.lib.php');
667

    
668
	// Install add-ons
669
	if(file_exists(WB_PATH.'/install/modules')) {
670
		// Unpack pre-packaged modules
671
			
672
	}
673
	if(file_exists(WB_PATH.'/install/templates')) {
674
		// Unpack pre-packaged templates
675
		
676
	}
677
	if(file_exists(WB_PATH.'/install/languages')) {
678
		// Unpack pre-packaged languages
679
		
680
	}
681

    
682
	$admin=new admin_dummy('Start','',false,false);
683

    
684
	// Load addons into DB
685
	$dirs['modules'] = WB_PATH.'/modules/';
686
	$dirs['templates'] = WB_PATH.'/templates/';
687
	$dirs['languages'] = WB_PATH.'/languages/';
688

    
689
	foreach($dirs AS $type => $dir) {
690
		if($handle = opendir($dir)) {
691
			while(false !== ($file = readdir($handle))) {
692
				if($file != '' AND substr($file, 0, 1) != '.' AND $file != 'admin.php' AND $file != 'index.php') {
693
					// Get addon type
694
					if($type == 'modules') {
695
						load_module($dir.'/'.$file, true);
696
						// Pretty ugly hack to let modules run $admin->set_error
697
						// See dummy class definition admin_dummy above
698
						if ($admin->error!='') {
699
							set_error($admin->error);
700
						}
701
					} elseif($type == 'templates') {
702
						load_template($dir.'/'.$file);
703
					} elseif($type == 'languages') {
704
						load_language($dir.'/'.$file);
705
					}
706
				}
707
			}
708
		closedir($handle);
709
		}
710
	}
711
	
712
	// Check if there was a database error
713
	if($database->is_error()) {
714
		set_error($database->get_error());
715
	}
716

    
717
// end of if install_tables	
718
} else {
719
	/**
720
	 *	DB - Exists
721
	 *	Tables also?
722
	 *
723
	 */
724
	$requested_tables = array("pages","sections","settings","users","groups","search","addons");
725
	for($i=0;$i<count($requested_tables);$i++) $requested_tables[$i] = $table_prefix.$requested_tables[$i];
726
	
727
	$result = mysql_list_tables( DB_NAME );
728
	$all_tables = array();
729
	for($i=0; $i < mysql_num_rows($result); $i++) $all_tables[] = mysql_table_name($result, $i);
730

    
731
	$missing_tables = array();
732
	foreach($requested_tables as $temp_table) {
733
		if (!in_array($temp_table, $all_tables)) {
734
			$missing_tables[] = $temp_table;
735
		}
736
	}
737
	
738
	/**
739
	 *	If one or more needed tables are missing, so 
740
	 *	we can't go on and have to display an error
741
	 */
742
	if ( count($missing_tables) > 0 ) {
743
		$error_message  = "One or more tables are missing in the selected database <b><font color='#990000'>".DB_NAME."</font></b>.<br />";
744
		$error_message .= "Please install the missing tables or choose 'install tables' as recommend.<br />";
745
		$error_message .= "Missing tables are: <b>".implode(", ", $missing_tables)."</b>";
746
		
747
		set_error( $error_message );
748
	}
749
	
750
	/**
751
	 *	Try to get some default settings ...
752
	 */
753
	$vars = array(
754
		'DEFAULT_THEME'	=> "wb_theme",
755
		'THEME_URL'		=> WB_URL."/templates/wb_theme",
756
		'THEME_PATH'	=> WB_PATH."/templates/wb_theme",
757
		'LANGUAGE'		=> $_POST['default_language'],
758
		'SERVER_EMAIL'	=> "admin@yourdomain.com",
759
		'SMART_LOGIN'	=> false
760
	);
761
	foreach($vars as $k => $v) if (!defined($k)) define($k, $v);
762
	
763
	if (!isset($MESSAGE)) include (WB_PATH."/languages/".LANGUAGE.".php");
764
	
765
	/**
766
	 *	The important part ...
767
	 *	Is there an valid user?
768
	 */
769
	$result = $database->query("SELECT * from ".$table_prefix."users where username='".$_POST['admin_username']."'");
770
	if ( $database->is_error() ) {
771
		set_error ($database->get_error() );
772
	}
773
	if ($result->numRows() == 0) {
774
		/**
775
		 *	No matches found ... user properly unknown
776
	 	 */
777
	 	set_error ("Unkown user. Please use a valid username.");
778
	} else {
779
	 	
780
		$data = $result->fetchRow();
781
	 	/**
782
	 	 *	Does the password match
783
	 	 */
784
	 	if ( md5($_POST['admin_password']) != $data['password']) {
785
	 		set_error ("Password didn't match");
786
	 	}
787
	}
788
}
789

    
790
$ThemeUrl = WB_URL.$admin->correct_theme_source('warning.html');
791
// Setup template object, parse vars to it, then parse it
792
$ThemePath = realpath(WB_PATH.$admin->correct_theme_source('login.htt'));
793

    
794
// Log the user in and go to Website Baker Administration
795
$thisApp = new Login(
796
		array(
797
				"MAX_ATTEMPS" => "50",
798
				"WARNING_URL" => $ThemeUrl."/warning.html",
799
				"USERNAME_FIELDNAME" => 'admin_username',
800
				"PASSWORD_FIELDNAME" => 'admin_password',
801
				"REMEMBER_ME_OPTION" => SMART_LOGIN,
802
				"MIN_USERNAME_LEN" => "2",
803
				"MIN_PASSWORD_LEN" => "3",
804
				"MAX_USERNAME_LEN" => "30",
805
				"MAX_PASSWORD_LEN" => "30",
806
				'LOGIN_URL' => ADMIN_URL."/login/index.php",
807
				'DEFAULT_URL' => ADMIN_URL."/start/index.php",
808
				'TEMPLATE_DIR' => $ThemePath,
809
				'TEMPLATE_FILE' => 'login.htt',
810
				'FRONTEND' => false,
811
				'FORGOTTEN_DETAILS_APP' => ADMIN_URL."/login/forgot/index.php",
812
				'USERS_TABLE' => TABLE_PREFIX."users",
813
				'GROUPS_TABLE' => TABLE_PREFIX."groups",
814
		)
815
);
(4-4/5)