Project

General

Profile

1
<?php
2

    
3
// $Id: save.php 1149 2009-09-22 14:50:47Z aldus $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25
$debug = true;
26

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

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

    
91
// Dummy class to allow modules' install scripts to call $admin->print_error
92
class admin_dummy
93
{
94
	public  $error='';
95
	
96
	public function print_error($message)
97
	{
98
		$this->error=$message;
99
	}
100
}
101

    
102
// Function to workout what the default permissions are for files created by the webserver
103
function default_file_mode($temp_dir) {
104
	$v = explode(".",PHP_VERSION);
105
	$v = $v[0].$v[1];
106
	if($v > 41 AND is_writable($temp_dir)) {
107
		$filename = $temp_dir.'/test_permissions.txt';
108
		$handle = fopen($filename, 'w');
109
		fwrite($handle, 'This file is to get the default file permissions');
110
		fclose($handle);
111
		$default_file_mode = '0'.substr(sprintf('%o', fileperms($filename)), -3);
112
		unlink($filename);
113
	} else {
114
		$default_file_mode = '0777';
115
	}
116
	return $default_file_mode;
117
}
118

    
119
// Function to workout what the default permissions are for directories created by the webserver
120
function default_dir_mode($temp_dir) {
121
	$v = explode(".",PHP_VERSION);
122
	$v = $v[0].$v[1];
123
	if($v > 41 AND is_writable($temp_dir)) {
124
		$dirname = $temp_dir.'/test_permissions/';
125
		mkdir($dirname);
126
		$default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
127
		rmdir($dirname);
128
	} else {
129
		$default_dir_mode = '0777';
130
	}
131
	return $default_dir_mode;
132
}
133

    
134
function add_slashes($input) {
135
	if ( get_magic_quotes_gpc() || ( !is_string($input) ) ) {
136
		return $input;
137
	}
138
	$output = addslashes($input);
139
	return $output;
140
}
141

    
142
// Begin check to see if form was even submitted
143
// Set error if no post vars found
144
if(!isset($_POST['website_title'])) {
145
	set_error('Please fill-in the form below');
146
}
147
// End check to see if form was even submitted
148

    
149
// Begin path and timezone details code
150

    
151
// Check if user has entered the installation url
152
if(!isset($_POST['wb_url']) OR $_POST['wb_url'] == '') {
153
	set_error('Please enter an absolute URL', 'wb_url');
154
} else {
155
	$wb_url = $_POST['wb_url'];
156
}
157
// Remove any slashes at the end of the URL
158
if(substr($wb_url, strlen($wb_url)-1, 1) == "/") {
159
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
160
}
161
if(substr($wb_url, strlen($wb_url)-1, 1) == "\\") {
162
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
163
}
164
if(substr($wb_url, strlen($wb_url)-1, 1) == "/") {
165
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
166
}
167
if(substr($wb_url, strlen($wb_url)-1, 1) == "\\") {
168
	$wb_url = substr($wb_url, 0, strlen($wb_url)-1);
169
}
170
// Get the default time zone
171
if(!isset($_POST['default_timezone']) OR !is_numeric($_POST['default_timezone'])) {
172
	set_error('Please select a valid default timezone', 'default_timezone');
173
} else {
174
	$default_timezone = $_POST['default_timezone']*60*60;
175
}
176
// End path and timezone details code
177

    
178
// Get the default language
179
$allowed_languages = array('BG','CA', 'CS', 'DA', 'DE', 'EN', 'ES', 'ET', 'FI', 'FR', 'HR', 'HU', 'IT', 'LV', 'NL', 'NO', 'PL', 'PT', 'RU','SE', 'TR');
180
if(!isset($_POST['default_language']) OR !in_array($_POST['default_language'], $allowed_languages)) {
181
	set_error('Please select a valid default backend language','default_language');
182
} else {
183
	$default_language = $_POST['default_language'];
184
	// make sure the selected language file exists in the language folder
185
	if(!file_exists('../languages/' .$default_language .'.php')) {
186
		set_error('The language file: \'' .$default_language .'.php\' is missing. Upload file to language folder or choose another language','default_language');
187
	}
188
}
189
// End default language details code
190

    
191
// Begin operating system specific code
192
// Get operating system
193
if(!isset($_POST['operating_system']) OR $_POST['operating_system'] != 'linux' AND $_POST['operating_system'] != 'windows') {
194
	set_error('Please select a valid operating system');
195
} else {
196
	$operating_system = $_POST['operating_system'];
197
}
198
// Work-out file permissions
199
if($operating_system == 'windows') {
200
	$file_mode = '0777';
201
	$dir_mode = '0777';
202
} elseif(isset($_POST['world_writeable']) AND $_POST['world_writeable'] == 'true') {
203
	$file_mode = '0777';
204
	$dir_mode = '0777';
205
} else {
206
	$file_mode = default_file_mode('../temp');
207
	$dir_mode = default_dir_mode('../temp');
208
}
209
// End operating system specific code
210

    
211
// Begin database details code
212
// Check if user has entered a database host
213
if(!isset($_POST['database_host']) OR $_POST['database_host'] == '') {
214
	set_error('Please enter a database host name', 'database_host');
215
} else {
216
	$database_host = $_POST['database_host'];
217
}
218
// Check if user has entered a database username
219
if(!isset($_POST['database_username']) OR $_POST['database_username'] == '') {
220
	set_error('Please enter a database username','database_username');
221
} else {
222
	$database_username = $_POST['database_username'];
223
}
224
// Check if user has entered a database password
225
if(!isset($_POST['database_password'])) {
226
	set_error('Please enter a database password', 'database_password');
227
} else {
228
	$database_password = $_POST['database_password'];
229
}
230
// Check if user has entered a database name
231
if(!isset($_POST['database_name']) OR $_POST['database_name'] == '') {
232
	set_error('Please enter a database name', 'database_name');
233
} else {
234
	// make sure only allowed characters are specified
235
	if(preg_match('/[^a-z0-9_-]+/i', $_POST['database_name'])) {
236
		// contains invalid characters (only a-z, A-Z, 0-9 and _ allowed to avoid problems with table/field names)
237
		set_error('Only characters a-z, A-Z, 0-9, - and _ allowed in database name.', 'database_name');
238
	}
239
	$database_name = $_POST['database_name'];
240
}
241
// Get table prefix
242
if(preg_match('/[^a-z0-9_]+/i', $_POST['table_prefix'])) {
243
	// contains invalid characters (only a-z, A-Z, 0-9 and _ allowed to avoid problems with table/field names)
244
	set_error('Only characters a-z, A-Z, 0-9 and _ allowed in table_prefix.', 'table_prefix');
245
} else {
246
	$table_prefix = $_POST['table_prefix'];
247
}
248

    
249
// Find out if the user wants to install tables and data
250
if(isset($_POST['install_tables']) AND $_POST['install_tables'] == 'true') {
251
	$install_tables = true;
252
} else {
253
	$install_tables = false;
254
}
255
// End database details code
256

    
257
// Begin website title code
258
// Get website title
259
if(!isset($_POST['website_title']) OR $_POST['website_title'] == '') {
260
	set_error('Please enter a website title', 'website_title');
261
} else {
262
	$website_title = add_slashes($_POST['website_title']);
263
}
264
// End website title code
265

    
266
// Begin admin user details code
267
// Get admin username
268
if(!isset($_POST['admin_username']) OR $_POST['admin_username'] == '') {
269
	set_error('Please enter a username for the Administrator account','admin_username');
270
} else {
271
	$admin_username = $_POST['admin_username'];
272
}
273
// Get admin email and validate it
274
if(!isset($_POST['admin_email']) OR $_POST['admin_email'] == '') {
275
	set_error('Please enter an email for the Administrator account','admin_email');
276
} else {
277
	if(eregi("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $_POST['admin_email'])) {
278
		$admin_email = $_POST['admin_email'];
279
	} else {
280
		set_error('Please enter a valid email address for the Administrator account','admin_email');
281
	}
282
}
283
// Get the two admin passwords entered, and check that they match
284
if(!isset($_POST['admin_password']) OR $_POST['admin_password'] == '') {
285
	set_error('Please enter a password for the Administrator account','admin_password');
286
} else {
287
	$admin_password = $_POST['admin_password'];
288
}
289
if(!isset($_POST['admin_repassword']) OR $_POST['admin_repassword'] == '') {
290
	set_error('Please make sure you re-enter the password for the Administrator account','admin_repassword');
291
} else {
292
	$admin_repassword = $_POST['admin_repassword'];
293
}
294
if($admin_password != $admin_repassword) {
295
	set_error('Sorry, the two Administrator account passwords you entered do not match','admin_repassword');
296
}
297
// End admin user details code
298

    
299
// Try and write settings to config file
300
$config_content = "" .
301
"<?php\n".
302
"\n".
303
"define('DB_TYPE', 'mysql');\n".
304
"define('DB_HOST', '$database_host');\n".
305
"define('DB_USERNAME', '$database_username');\n".
306
"define('DB_PASSWORD', '$database_password');\n".
307
"define('DB_NAME', '$database_name');\n".
308
"define('TABLE_PREFIX', '$table_prefix');\n".
309
"\n".
310
"define('WB_PATH', dirname(__FILE__));\n".
311
"define('WB_URL', '$wb_url');\n".
312
"define('ADMIN_PATH', WB_PATH.'/admin');\n".
313
"define('ADMIN_URL', '$wb_url/admin');\n".
314
"\n".
315
"require_once(WB_PATH.'/framework/initialize.php');\n".
316
"\n".
317
"?>";
318

    
319
$config_filename = '../config.php';
320

    
321
/**
322
 *	Looks a littel bit confuse, but in some circumstances
323
 *	the config.php file could be corrupted - so the installer
324
 *	is called even if the config.php file exists!
325
 */
326
if (file_exists($config_filename)) unlink($config_filename);
327

    
328
/**
329
 * Try to write the config file
330
 */
331
$fp = fopen($config_filename, 'w');
332
if (!$fp ) {
333
	set_error ("Can't create the config file.");
334
} else {
335
	if (fwrite($fp, $config_content, strlen($config_content) ) === FALSE) {
336
		/**
337
		 *	We have to close the file-pointer first, as
338
		 *	the following function "die's" and keep the 
339
		 *	file-handle //open//
340
		 */
341
		fclose( $fp );
342
		set_error("Can't write to the configuration file [b]".$config_filename."[/b]!<br />Please check the permissions.");
343
	
344
	} else {
345
		fclose($fp);
346
	}
347
}
348

    
349
// Define configuration vars
350
define('DB_TYPE', 'mysql');
351
define('DB_HOST', $database_host);
352
define('DB_USERNAME', $database_username);
353
define('DB_PASSWORD', $database_password);
354
define('DB_NAME', $database_name);
355
define('TABLE_PREFIX', $table_prefix);
356
define('WB_PATH', str_replace(array('/install','\install'), '',dirname(__FILE__)));
357
define('WB_URL', $wb_url);
358
define('ADMIN_PATH', WB_PATH.'/admin');
359
define('ADMIN_URL', $wb_url.'/admin');
360

    
361
// Check if the user has entered a correct path
362
if(!file_exists(WB_PATH.'/framework/class.admin.php')) {
363
	set_error('It appears the Absolute path that you entered is incorrect');
364
}
365

    
366
// Try connecting to database	
367
if(!@mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD)) {
368
	set_error('Database host name, username and/or password incorrect. MySQL Error:<br />'.mysql_error());
369
}
370

    
371
// Try to create the database
372
mysql_query('CREATE DATABASE `'.$database_name.'`');
373

    
374
// Close the mysql connection
375
mysql_close();
376

    
377
// Include WB functions file
378
require_once(WB_PATH.'/framework/functions.php');
379

    
380
// Re-connect to the database, this time using in-build database class
381
require_once(WB_PATH.'/framework/class.login.php');
382
$database=new database();
383

    
384
// Check if we should install tables
385
if($install_tables == true) {
386
	if (!defined('WB_INSTALL_PROCESS')) define ('WB_INSTALL_PROCESS', true);
387
	// Remove tables if they exist
388

    
389
	// Pages table
390
	$pages = "DROP TABLE IF EXISTS `".TABLE_PREFIX."pages`";
391
	$database->query($pages);
392
	// Sections table
393
	$sections = "DROP TABLE IF EXISTS `".TABLE_PREFIX."sections`";
394
	$database->query($sections);
395
	// Settings table
396
	$settings = "DROP TABLE IF EXISTS `".TABLE_PREFIX."settings`";
397
	$database->query($settings);
398
	// Users table
399
	$users = "DROP TABLE IF EXISTS `".TABLE_PREFIX."users`";
400
	$database->query($users);
401
	// Groups table
402
	$groups = "DROP TABLE IF EXISTS `".TABLE_PREFIX."groups`";
403
	$database->query($groups);
404
	// Search table
405
	$search = "DROP TABLE IF EXISTS `".TABLE_PREFIX."search`";
406
	$database->query($search);
407
	// Addons table
408
	$addons = "DROP TABLE IF EXISTS `".TABLE_PREFIX."addons`";
409
	$database->query($addons);
410
				
411
	// Try installing tables
412
	
413
	// Pages table
414
	$pages = 'CREATE TABLE `'.TABLE_PREFIX.'pages` ( `page_id` INT NOT NULL auto_increment,'
415
	       . ' `parent` INT NOT NULL DEFAULT \'0\','
416
	       . ' `root_parent` INT NOT NULL DEFAULT \'0\','
417
	       . ' `level` INT NOT NULL DEFAULT \'0\','
418
	       . ' `link` TEXT NOT NULL,'
419
	       . ' `target` VARCHAR( 7 ) NOT NULL DEFAULT \'\' ,'
420
	       . ' `page_title` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
421
	       . ' `menu_title` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
422
	       . ' `description` TEXT NOT NULL ,'
423
	       . ' `keywords` TEXT NOT NULL ,'
424
	       . ' `page_trail` TEXT NOT NULL  ,'
425
	       . ' `template` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
426
	       . ' `visibility` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
427
	       . ' `position` INT NOT NULL DEFAULT \'0\','
428
	       . ' `menu` INT NOT NULL DEFAULT \'0\','
429
	       . ' `language` VARCHAR( 5 ) NOT NULL DEFAULT \'\' ,'
430
	       . ' `searching` INT NOT NULL DEFAULT \'0\','
431
	       . ' `admin_groups` TEXT NOT NULL ,'
432
	       . ' `admin_users` TEXT NOT NULL ,'
433
	       . ' `viewing_groups` TEXT NOT NULL ,'
434
	       . ' `viewing_users` TEXT NOT NULL ,'
435
	       . ' `modified_when` INT NOT NULL DEFAULT \'0\','
436
	       . ' `modified_by` INT NOT NULL  DEFAULT \'0\','
437
	       . ' PRIMARY KEY ( `page_id` ) '
438
	       . ' )';
439
	$database->query($pages);
440
	
441
	// Sections table
442
	$pages = 'CREATE TABLE `'.TABLE_PREFIX.'sections` ( `section_id` INT NOT NULL auto_increment,'
443
	       . ' `page_id` INT NOT NULL DEFAULT \'0\','
444
	       . ' `position` INT NOT NULL DEFAULT \'0\','
445
	       . ' `module` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
446
	       . ' `block` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
447
	       . ' `publ_start` VARCHAR( 255 ) NOT NULL DEFAULT \'0\' ,'
448
	       . ' `publ_end` VARCHAR( 255 ) NOT NULL DEFAULT \'0\' ,' 
449
	       . ' PRIMARY KEY ( `section_id` ) '
450
	       . ' )';
451
	$database->query($pages);
452

    
453
	require(ADMIN_PATH.'/interface/version.php');
454
	
455
	// Settings table
456
	$settings='CREATE TABLE `'.TABLE_PREFIX.'settings` ( `setting_id` INT NOT NULL auto_increment,'
457
		. ' `name` VARCHAR( 255 ) NOT NULL DEFAULT \'\' ,'
458
		. ' `value` TEXT NOT NULL ,'
459
		. ' PRIMARY KEY ( `setting_id` ) '
460
		. ' )';
461
	$database->query($settings);
462

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

    
572
	// Insert default data
573
	
574
	// Admin group
575
	$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';
576
	$insert_admin_group = "INSERT INTO `".TABLE_PREFIX."groups` VALUES ('1', 'Administrators', '$full_system_permissions', '', '')";
577
	$database->query($insert_admin_group);
578
	// Admin user
579
	$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')";
580
	$database->query($insert_admin_user);
581
	
582
	// Search header
583
	$search_header = addslashes('
584
<h1>[TEXT_SEARCH]</h1>
585

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

    
609
</form>
610

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

    
634
	$insert_search_results_loop = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'results_loop', '$search_results_loop', '')";
635
	$database->query($insert_search_results_loop);
636
	// Search results footer
637
	$search_results_footer = addslashes("</table>");
638
	$insert_search_results_footer = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'results_footer', '$search_results_footer', '')";
639
	$database->query($insert_search_results_footer);
640
	// Search no results
641
	$search_no_results = addslashes('<tr><td><p>[TEXT_NO_RESULTS]</p></td></tr>');
642
	$insert_search_no_results = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'no_results', '$search_no_results', '')";
643
	$database->query($insert_search_no_results);
644
	// Search module-order
645
	$search_module_order = addslashes('faqbaker,manual,wysiwyg');
646
	$insert_search_module_order = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'module_order', '$search_module_order', '')";
647
	$database->query($insert_search_module_order);
648
	// Search max lines of excerpt
649
	$search_max_excerpt = addslashes('15');
650
	$insert_search_max_excerpt = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'max_excerpt', '$search_max_excerpt', '')";
651
	$database->query($insert_search_max_excerpt);
652
	// max time to search per module
653
	$search_time_limit = addslashes('0');
654
	$insert_search_time_limit = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'time_limit', '$search_time_limit', '')";
655
	$database->query($insert_search_time_limit);
656
	// some config-elements
657
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_enable_old_search', 'true', '')");
658
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_search_keywords', 'true', '')");
659
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_search_description', 'true', '')");
660
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_show_description', 'true', '')");
661
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_enable_flush', 'false', '')");
662
	// Search template
663
	$database->query("INSERT INTO `".TABLE_PREFIX."search` (name) VALUES ('template')");
664
		
665
	require_once(WB_PATH.'/framework/initialize.php');
666
	
667
	// Include the PclZip class file (thanks to 
668
	require_once(WB_PATH.'/include/pclzip/pclzip.lib.php');
669
			
670
	// Install add-ons
671
	if(file_exists(WB_PATH.'/install/modules')) {
672
		// Unpack pre-packaged modules
673
			
674
	}
675
	if(file_exists(WB_PATH.'/install/templates')) {
676
		// Unpack pre-packaged templates
677
		
678
	}
679
	if(file_exists(WB_PATH.'/install/languages')) {
680
		// Unpack pre-packaged languages
681
		
682
	}
683
	
684
	$admin=new admin_dummy();
685
	// Load addons into DB
686
	$dirs['modules'] = WB_PATH.'/modules/';
687
	$dirs['templates'] = WB_PATH.'/templates/';
688
	$dirs['languages'] = WB_PATH.'/languages/';
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
// Log the user in and go to Website Baker Administration
790
$thisApp = new Login(
791
		array(
792
				"MAX_ATTEMPS" => "50",
793
				"WARNING_URL" => ADMIN_URL."/login/warning.html",
794
				"USERNAME_FIELDNAME" => 'admin_username',
795
				"PASSWORD_FIELDNAME" => 'admin_password',
796
				"REMEMBER_ME_OPTION" => SMART_LOGIN,
797
				"MIN_USERNAME_LEN" => "2",
798
				"MIN_PASSWORD_LEN" => "2",
799
				"MAX_USERNAME_LEN" => "30",
800
				"MAX_PASSWORD_LEN" => "30",
801
				'LOGIN_URL' => ADMIN_URL."/login/index.php",
802
				'DEFAULT_URL' => ADMIN_URL."/start/index.php",
803
				'TEMPLATE_DIR' => ADMIN_PATH."/login",
804
				'TEMPLATE_FILE' => "template.html",
805
				'FRONTEND' => false,
806
				'FORGOTTEN_DETAILS_APP' => ADMIN_URL."/login/forgot/index.php",
807
				'USERS_TABLE' => TABLE_PREFIX."users",
808
				'GROUPS_TABLE' => TABLE_PREFIX."groups",
809
		)
810
);
811
?>
(2-2/3)