Project

General

Profile

1
<?php
2

    
3
// $Id: save.php 1194 2009-11-27 23:51:46Z Luisehahne $
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
 * @category   install
25
 * @package    create
26
 * @author(s)  Dietmar W?llbrink <Luisehahne>, Dietrich Roland Pehlke <Aldus>
27
 * @platform   WB 2.8.x
28
 * @require    PHP 5.2.11
29
 * @license    http://www.gnu.org/licenses/gpl.html
30
 * @link       http://project.websitebaker2.org/browser/branches/2.8.x/wb/pages
31
 * @changeset  2009/11/28 fix deprecated eregi
32

    
33
*/
34
$debug = true;
35

    
36
if (true === $debug) {
37
	ini_set('display_errors', 1);
38
	error_reporting(E_ALL);
39
}
40
// Start a session
41
if(!defined('SESSION_STARTED')) {
42
	session_name('wb_session_id');
43
	session_start();
44
	define('SESSION_STARTED', true);
45
}
46
// get random-part for session_name()
47
list($usec,$sec) = explode(' ',microtime());
48
srand((float)$sec+((float)$usec*100000));
49
$session_rand = rand(1000,9999);
50

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

    
100
// Dummy class to allow modules' install scripts to call $admin->print_error
101
class admin_dummy
102
{
103
	var $error='';
104
	function print_error($message)
105
	{
106
		$this->error=$message;
107
	}
108
}
109

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

    
127
// Function to workout what the default permissions are for directories created by the webserver
128
function default_dir_mode($temp_dir) {
129
	$v = explode(".",PHP_VERSION);
130
	$v = $v[0].$v[1];
131
	if($v > 41 AND is_writable($temp_dir)) {
132
		$dirname = $temp_dir.'/test_permissions/';
133
		mkdir($dirname);
134
		$default_dir_mode = '0'.substr(sprintf('%o', fileperms($dirname)), -3);
135
		rmdir($dirname);
136
	} else {
137
		$default_dir_mode = '0777';
138
	}
139
	return $default_dir_mode;
140
}
141

    
142
function add_slashes($input) {
143
	if ( get_magic_quotes_gpc() || ( !is_string($input) ) ) {
144
		return $input;
145
	}
146
	$output = addslashes($input);
147
	return $output;
148
}
149

    
150
// Begin check to see if form was even submitted
151
// Set error if no post vars found
152
if(!isset($_POST['website_title'])) {
153
	set_error('Please fill-in the form below');
154
}
155
// End check to see if form was even submitted
156

    
157
// Begin path and timezone details code
158

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

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

    
199
// Begin operating system specific code
200
// Get operating system
201
if(!isset($_POST['operating_system']) OR $_POST['operating_system'] != 'linux' AND $_POST['operating_system'] != 'windows') {
202
	set_error('Please select a valid operating system');
203
} else {
204
	$operating_system = $_POST['operating_system'];
205
}
206
// Work-out file permissions
207
if($operating_system == 'windows') {
208
	$file_mode = '0777';
209
	$dir_mode = '0777';
210
} elseif(isset($_POST['world_writeable']) AND $_POST['world_writeable'] == 'true') {
211
	$file_mode = '0777';
212
	$dir_mode = '0777';
213
} else {
214
	$file_mode = default_file_mode('../temp');
215
	$dir_mode = default_dir_mode('../temp');
216
}
217
// End operating system specific code
218

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

    
257
// Find out if the user wants to install tables and data
258
if(isset($_POST['install_tables']) AND $_POST['install_tables'] == 'true') {
259
	$install_tables = true;
260
} else {
261
	$install_tables = false;
262
}
263
// End database details code
264

    
265
// Begin website title code
266
// Get website title
267
if(!isset($_POST['website_title']) OR $_POST['website_title'] == '') {
268
	set_error('Please enter a website title', 'website_title');
269
} else {
270
	$website_title = add_slashes($_POST['website_title']);
271
}
272
// End website title code
273

    
274
// Begin admin user details code
275
// Get admin username
276
if(!isset($_POST['admin_username']) OR $_POST['admin_username'] == '') {
277
	set_error('Please enter a username for the Administrator account','admin_username');
278
} else {
279
	$admin_username = $_POST['admin_username'];
280
}
281
// Get admin email and validate it
282
if(!isset($_POST['admin_email']) OR $_POST['admin_email'] == '') {
283
	set_error('Please enter an email for the Administrator account','admin_email');
284
} else {
285
	if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $_POST['admin_email'])) {
286
		$admin_email = $_POST['admin_email'];
287
	} else {
288
		set_error('Please enter a valid email address for the Administrator account','admin_email');
289
	}
290
}
291
// Get the two admin passwords entered, and check that they match
292
if(!isset($_POST['admin_password']) OR $_POST['admin_password'] == '') {
293
	set_error('Please enter a password for the Administrator account','admin_password');
294
} else {
295
	$admin_password = $_POST['admin_password'];
296
}
297
if(!isset($_POST['admin_repassword']) OR $_POST['admin_repassword'] == '') {
298
	set_error('Please make sure you re-enter the password for the Administrator account','admin_repassword');
299
} else {
300
	$admin_repassword = $_POST['admin_repassword'];
301
}
302
if($admin_password != $admin_repassword) {
303
	set_error('Sorry, the two Administrator account passwords you entered do not match','admin_repassword');
304
}
305
// End admin user details code
306

    
307
// Try and write settings to config file
308
$config_content = "" .
309
"<?php\n".
310
"\n".
311
"define('DB_TYPE', 'mysql');\n".
312
"define('DB_HOST', '$database_host');\n".
313
"define('DB_USERNAME', '$database_username');\n".
314
"define('DB_PASSWORD', '$database_password');\n".
315
"define('DB_NAME', '$database_name');\n".
316
"define('TABLE_PREFIX', '$table_prefix');\n".
317
"\n".
318
"define('WB_PATH', dirname(__FILE__));\n".
319
"define('WB_URL', '$wb_url');\n".
320
"define('ADMIN_PATH', WB_PATH.'/admin');\n".
321
"define('ADMIN_URL', '$wb_url/admin');\n".
322
"\n".
323
"require_once(WB_PATH.'/framework/initialize.php');\n".
324
"\n".
325
"?>";
326

    
327
$config_filename = '../config.php';
328

    
329
// Check if the file exists and is writable first.
330
if(file_exists($config_filename) AND is_writable($config_filename)) {
331
	if(!$handle = fopen($config_filename, 'w')) {
332
		set_error("Cannot open the configuration file ($config_filename)");
333
	} else {
334
		if (fwrite($handle, $config_content) === FALSE) {
335
			set_error("Cannot write to the configuration file ($config_filename)");
336
		}
337
		// Close file
338
		fclose($handle);
339
	}
340
} else {
341
	set_error("The configuration file $config_filename is not writable. Change its permissions so it is, then re-run step 4.");
342
}
343

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

    
356
// Check if the user has entered a correct path
357
if(!file_exists(WB_PATH.'/framework/class.admin.php')) {
358
	set_error('It appears the Absolute path that you entered is incorrect');
359
}
360

    
361
// Try connecting to database	
362
if(!@mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD)) {
363
	set_error('Database host name, username and/or password incorrect. MySQL Error:<br />'.mysql_error());
364
}
365

    
366
// Try to create the database
367
mysql_query('CREATE DATABASE `'.$database_name.'`');
368

    
369
// Close the mysql connection
370
mysql_close();
371

    
372
// Include WB functions file
373
require_once(WB_PATH.'/framework/functions.php');
374

    
375
// Re-connect to the database, this time using in-build database class
376
require_once(WB_PATH.'/framework/class.login.php');
377
$database=new database();
378

    
379
// Check if we should install tables
380
if($install_tables == true) {
381
	if (!defined('WB_INSTALL_PROCESS')) define ('WB_INSTALL_PROCESS', true);
382
	// Remove tables if they exist
383

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

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

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

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

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

    
604
</form>
605

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

    
629
	$insert_search_results_loop = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'results_loop', '$search_results_loop', '')";
630
	$database->query($insert_search_results_loop);
631
	// Search results footer
632
	$search_results_footer = addslashes("</table>");
633
	$insert_search_results_footer = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'results_footer', '$search_results_footer', '')";
634
	$database->query($insert_search_results_footer);
635
	// Search no results
636
	$search_no_results = addslashes('<tr><td><p>[TEXT_NO_RESULTS]</p></td></tr>');
637
	$insert_search_no_results = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'no_results', '$search_no_results', '')";
638
	$database->query($insert_search_no_results);
639
	// Search module-order
640
	$search_module_order = addslashes('faqbaker,manual,wysiwyg');
641
	$insert_search_module_order = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'module_order', '$search_module_order', '')";
642
	$database->query($insert_search_module_order);
643
	// Search max lines of excerpt
644
	$search_max_excerpt = addslashes('15');
645
	$insert_search_max_excerpt = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'max_excerpt', '$search_max_excerpt', '')";
646
	$database->query($insert_search_max_excerpt);
647
	// max time to search per module
648
	$search_time_limit = addslashes('0');
649
	$insert_search_time_limit = "INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'time_limit', '$search_time_limit', '')";
650
	$database->query($insert_search_time_limit);
651
	// some config-elements
652
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_enable_old_search', 'true', '')");
653
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_search_keywords', 'true', '')");
654
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_search_description', 'true', '')");
655
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_show_description', 'true', '')");
656
	$database->query("INSERT INTO `".TABLE_PREFIX."search` VALUES ('', 'cfg_enable_flush', 'false', '')");
657
	// Search template
658
	$database->query("INSERT INTO `".TABLE_PREFIX."search` (name) VALUES ('template')");
659
		
660
	require_once(WB_PATH.'/framework/initialize.php');
661
	
662
	// Include the PclZip class file (thanks to 
663
	require_once(WB_PATH.'/include/pclzip/pclzip.lib.php');
664
			
665
	// Install add-ons
666
	if(file_exists(WB_PATH.'/install/modules')) {
667
		// Unpack pre-packaged modules
668
			
669
	}
670
	if(file_exists(WB_PATH.'/install/templates')) {
671
		// Unpack pre-packaged templates
672
		
673
	}
674
	if(file_exists(WB_PATH.'/install/languages')) {
675
		// Unpack pre-packaged languages
676
		
677
	}
678
	
679
	$admin=new admin_dummy();
680
	// Load addons into DB
681
	$dirs['modules'] = WB_PATH.'/modules/';
682
	$dirs['templates'] = WB_PATH.'/templates/';
683
	$dirs['languages'] = WB_PATH.'/languages/';
684
	foreach($dirs AS $type => $dir) {
685
		if($handle = opendir($dir)) {
686
			while(false !== ($file = readdir($handle))) {
687
				if($file != '' AND substr($file, 0, 1) != '.' AND $file != 'admin.php' AND $file != 'index.php') {
688
					// Get addon type
689
					if($type == 'modules') {
690
						load_module($dir.'/'.$file, true);
691
						// Pretty ugly hack to let modules run $admin->set_error
692
						// See dummy class definition admin_dummy above
693
						if ($admin->error!='') {
694
							set_error($admin->error);
695
						}
696
					} elseif($type == 'templates') {
697
						load_template($dir.'/'.$file);
698
					} elseif($type == 'languages') {
699
						load_language($dir.'/'.$file);
700
					}
701
				}
702
			}
703
		closedir($handle);
704
		}
705
	}
706
	
707
	// Check if there was a database error
708
	if($database->is_error()) {
709
		set_error($database->get_error());
710
	}
711

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

    
726
	$missing_tables = array();
727
	foreach($requested_tables as $temp_table) {
728
		if (!in_array($temp_table, $all_tables)) {
729
			$missing_tables[] = $temp_table;
730
		}
731
	}
732
	
733
	/**
734
	 *	If one or more needed tables are missing, so 
735
	 *	we can't go on and have to display an error
736
	 */
737
	if ( count($missing_tables) > 0 ) {
738
		$error_message  = "One or more tables are missing in the selected database <b><font color='#990000'>".DB_NAME."</font></b>.<br />";
739
		$error_message .= "Please install the missing tables or choose 'install tables' as recommend.<br />";
740
		$error_message .= "Missing tables are: <b>".implode(", ", $missing_tables)."</b>";
741
		
742
		set_error( $error_message );
743
	}
744
	
745
	/**
746
	 *	Try to get some default settings ...
747
	 */
748
	$vars = array(
749
		'DEFAULT_THEME'	=> "wb_theme",
750
		'THEME_URL'		=> WB_URL."/templates/wb_theme",
751
		'THEME_PATH'	=> WB_PATH."/templates/wb_theme",
752
		'LANGUAGE'		=> $_POST['default_language'],
753
		'SERVER_EMAIL'	=> "admin@yourdomain.com",
754
		'SMART_LOGIN'	=> false
755
	);
756
	foreach($vars as $k => $v) if (!defined($k)) define($k, $v);
757
	
758
	if (!isset($MESSAGE)) include (WB_PATH."/languages/".LANGUAGE.".php");
759
	
760
	/**
761
	 *	The important part ...
762
	 *	Is there an valid user?
763
	 */
764
	$result = $database->query("SELECT * from ".$table_prefix."users where username='".$_POST['admin_username']."'");
765
	if ( $database->is_error() ) {
766
		set_error ($database->get_error() );
767
	}
768
	if ($result->numRows() == 0) {
769
		/**
770
		 *	No matches found ... user properly unknown
771
	 	 */
772
	 	set_error ("Unkown user. Please use a valid username.");
773
	} else {
774
	 	
775
		$data = $result->fetchRow();
776
	 	/**
777
	 	 *	Does the password match
778
	 	 */
779
	 	if ( md5($_POST['admin_password']) != $data['password']) {
780
	 		set_error ("Password didn't match");
781
	 	}
782
	}
783
}
784
// Log the user in and go to Website Baker Administration
785
$thisApp = new Login(
786
		array(
787
				"MAX_ATTEMPS" => "50",
788
				"WARNING_URL" => ADMIN_URL."/login/warning.html",
789
				"USERNAME_FIELDNAME" => 'admin_username',
790
				"PASSWORD_FIELDNAME" => 'admin_password',
791
				"REMEMBER_ME_OPTION" => SMART_LOGIN,
792
				"MIN_USERNAME_LEN" => "2",
793
				"MIN_PASSWORD_LEN" => "2",
794
				"MAX_USERNAME_LEN" => "30",
795
				"MAX_PASSWORD_LEN" => "30",
796
				'LOGIN_URL' => ADMIN_URL."/login/index.php",
797
				'DEFAULT_URL' => ADMIN_URL."/start/index.php",
798
				'TEMPLATE_DIR' => ADMIN_PATH."/login",
799
				'TEMPLATE_FILE' => "template.html",
800
				'FRONTEND' => false,
801
				'FORGOTTEN_DETAILS_APP' => ADMIN_URL."/login/forgot/index.php",
802
				'USERS_TABLE' => TABLE_PREFIX."users",
803
				'GROUPS_TABLE' => TABLE_PREFIX."groups",
804
		)
805
);
806
?>
(2-2/3)