checkForVisibilityProfiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 24
rs 9.9332
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * @package SimplePortal ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
global $db_prefix, $db_package_log;
13
14
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('ELK'))
15
{
16
	$_GET['debug'] = 'Blue Dream!';
17
	require_once(dirname(__FILE__) . '/SSI.php');
18
}
19
elseif (!defined('ELK'))
20
{
21
	die('<b>Error:</b> Cannot install - please verify you put this in the same place as ElkArte\'s index.php.');
22
}
23
24
$db = database();
25
$db_table = db_table();
26
27
// Define and create (if needed) all the portal tables
28
$sp_tables = defineTables();
29
30
// See if any blocks are already defined, like would be found during an upgrade
31
$has_blocks = checkForBlocks();
32
if ($has_blocks)
33
{
34
	// Rename the "old" (pre-classes) to the "new" format introduced in B2
35
	updateBlocks();
36
}
37
else
38
{
39
	// No blocks yet, since this is a new install, lets set some defaults
40
	addDefaultBlocks();
41
}
42
43
// Have any permission profiles been added yet, or is this an upgrade
44
$has_permission_profiles = checkForPermissionProfiles();
45
if (!$has_permission_profiles)
46
{
47
	addDefaultPermissions();
48
}
49
50
// Have any style profiles been added yet, or is this an upgrade
51
$has_style_profiles = checkForStyleProfiles();
52
if (!$has_style_profiles)
53
{
54
	addDefaultStyles();
55
}
56
57
// Have any visibility profiles been added yet, or is this an upgrade
58
$has_visibility_profiles = checkForVisibilityProfiles();
59
if (!$has_visibility_profiles)
60
{
61
	addDefaultVisibility();
62
}
63
64
// Make any upgrade changes as required
65
updateTableStructures();
66
updateStyleProfiles($has_style_profiles);
67
updateVisibilityProfiles($has_visibility_profiles);
68
69
$db_package_log = $db_table->package_log();
70
71
if (ELK === 'SSI')
0 ignored issues
show
introduced by
The condition ELK === 'SSI' is always true.
Loading history...
72
{
73
	echo 'Database changes were carried out successfully.';
74
}
75
76
/**
77
 * Adds a few default visibility profiles for use
78
 */
79
function addDefaultVisibility()
80
{
81
	$db = database();
82
83
	$db->insert('replace',
84
		'{db_prefix}sp_profiles',
85
		array('id_profile' => 'int', 'type' => 'int', 'name' => 'text', 'value' => 'text'),
86
		array(
87
			array(14, 3, '$_show_on_portal', 'portal|'),
88
			array(15, 3, '$_show_on_board_index', 'forum|'),
89
			array(16, 3, '$_show_on_all_actions', '|allaction'),
90
			array(17, 3, '$_show_on_all_boards', '|allboard'),
91
			array(18, 3, '$_show_on_all_pages', '|allpage'),
92
			array(19, 3, '$_show_on_all_categories', '|allcategory'),
93
			array(20, 3, '$_show_on_all_articles', '|allarticle'),
94
			array(21, 3, '$_show_everywhere', '|all|1'),
95
		),
96
		array('id_profile')
97
	);
98
}
99
100
/**
101
 * Adds a few default style profiles for use
102
 */
103
function addDefaultStyles()
104
{
105
	$db = database();
106
107
	$db->insert('replace',
108
		'{db_prefix}sp_profiles',
109
		array('id_profile' => 'int', 'type' => 'int', 'name' => 'text', 'value' => 'text'),
110
		array(
111
			array(4, 2, '$_default_title_default_body', 'title_default_class~category_header|title_custom_class~|title_custom_style~|body_default_class~portalbg|body_custom_class~|body_custom_style~|no_title~|no_body~'),
112
			array(5, 2, '$_default_title_alternate_body', 'title_default_class~category_header|title_custom_class~|title_custom_style~|body_default_class~portalbg2|body_custom_class~|body_custom_style~|no_title~|no_body~'),
113
			array(6, 2, '$_alternate_title_default_body', 'title_default_class~secondary_header|title_custom_class~|title_custom_style~|body_default_class~portalbg|body_custom_class~|body_custom_style~|no_title~|no_body~'),
114
			array(7, 2, '$_alternate_title_alternate_body', 'title_default_class~secondary_header|title_custom_class~|title_custom_style~|body_default_class~portalbg2|body_custom_class~|body_custom_style~|no_title~|no_body~'),
115
			array(8, 2, '$_no_title_default_body', 'title_default_class~|title_custom_class~|title_custom_style~|body_default_class~portalbg|body_custom_class~|body_custom_style~|no_title~1|no_body~'),
116
			array(9, 2, '$_no_title_alternate_body', 'title_default_class~|title_custom_class~|title_custom_style~|body_default_class~portalbg2|body_custom_class~|body_custom_style~|no_title~1|no_body~'),
117
			array(10, 2, '$_default_title_roundframe', 'title_default_class~category_header|title_custom_class~|title_custom_style~|body_default_class~roundframe|body_custom_class~|body_custom_style~|no_title~|no_body~'),
118
			array(11, 2, '$_alternate_title_roundframe', 'title_default_class~secondary_header|title_custom_class~|title_custom_style~|body_default_class~roundframe|body_custom_class~|body_custom_style~|no_title~|no_body~'),
119
			array(12, 2, '$_no_title_roundframe', 'title_default_class~|title_custom_class~|title_custom_style~|body_default_class~roundframe|body_custom_class~|body_custom_style~|no_title~1|no_body~'),
120
			array(13, 2, '$_no_title_information', 'title_default_class~|title_custom_class~|title_custom_style~|body_default_class~information|body_custom_class~|body_custom_style~|no_title~1|no_body~'),
121
		),
122
		array('id_profile')
123
	);
124
}
125
126
/**
127
 * Adds guest only, member ony and everyone permission profiles
128
 */
129
function addDefaultPermissions()
130
{
131
	$db = database();
132
133
	$request = $db->query('', '
134
		SELECT
135
			id_group
136
		FROM {db_prefix}membergroups
137
		WHERE min_posts != {int:min_posts}',
138
		array(
139
			'min_posts' => -1,
140
		)
141
	);
142
	$post_groups = array();
143
	while ($row = $db->fetch_assoc($request))
144
	{
145
		$post_groups[] = $row['id_group'];
146
	}
147
	$db->free_result($request);
148
149
	$db->insert('replace',
150
		'{db_prefix}sp_profiles',
151
		array('id_profile' => 'int', 'type' => 'int', 'name' => 'text', 'value' => 'text'),
152
		array(
153
			array(1, 1, '$_guests', '-1|'),
154
			array(2, 1, '$_members', implode(',', $post_groups) . ',0|'),
155
			array(3, 1, '$_everyone', implode(',', $post_groups) . ',0,-1|'),
156
		),
157
		array('id_profile')
158
	);
159
}
160
161
/**
162
 * Makes any table structure updates required from previous releases
163
 */
164
function updateTableStructures()
165
{
166
	$db_table = db_table();
167
168
	// Update the page table to accept more data
169
	$page_cols = $db_table->db_list_columns('{db_prefix}sp_pages', true);
170
	if (isset($page_cols['body']) && $page_cols['body']['type'] === 'text')
171
	{
172
		$db_table->db_change_column('{db_prefix}sp_pages', 'body', array('type' => 'mediumtext'));
173
	}
174
}
175
176
/**
177
 * Update sp_blocks to use visibility profiles in place of display and mobile view values
178
 *
179
 * @param bool $has_visibility_profiles;
180
 */
181
function updateVisibilityProfiles($has_visibility_profiles)
0 ignored issues
show
Unused Code introduced by
The parameter $has_visibility_profiles is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

181
function updateVisibilityProfiles(/** @scrutinizer ignore-unused */ $has_visibility_profiles)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
{
183
	$db_table = db_table();
184
	$db = database();
185
186
	$block_cols = $db_table->db_list_columns('{db_prefix}sp_blocks', true);
187
	if (!isset($block_cols['visibility']))
188
	{
189
		// Add visibility profile col to sp_blocks
190
		$db_table->db_add_column('{db_prefix}sp_blocks', array('name' => 'visibility', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true));
191
192
		// Read in the "old" data
193
		$result = $db->query('', '
194
			SELECT
195
				id_block, ' . (isset($block_cols['mobile_view']) ? 'mobile_view, ' : '') . ' display, display_custom
196
			FROM {db_prefix}sp_blocks',
197
			array()
198
		);
199
		$updates = array();
200
		$all_action = array('allaction' => 'allaction', 'allboard' => 'allboard', 'allpage' => 'allpages', 'all' => 'all');
201
		while ($row = $db->fetch_assoc($result))
202
		{
203
			$selections = array();
204
			$query = array();
205
206
			// Old Mobile and Custom to new is easy
207
			$mobile_view = !empty($row['mobile_view']) ? 1 : 0;
208
			if (!empty($row['display_custom']))
209
				$query[] = $row['display_custom'];
210
211
			// The rest depends on if its a selection or query
212
			$rest = explode(',', $row['display']);
213
			foreach ($rest as $sel_or_query)
214
			{
215
				if ($sel_or_query === 'sportal')
216
				{
217
					$selections[] = 'portal';
218
				}
219
				elseif ($sel_or_query === 'sforum')
220
				{
221
					$selections[] = 'forum';
222
				}
223
				elseif (in_array($sel_or_query, $all_action))
224
				{
225
					$query[] = $all_action[$sel_or_query];
226
				}
227
				else
228
				{
229
					$selections[] = $sel_or_query;
230
				}
231
			}
232
233
			// Build the visibility value that represents this row
234
			$updates[$row['id_block']] = (!empty($selections) ? implode(',', $selections) : '') . '|' . (!empty($query) ? implode(',', $query) : '') . (!empty($mobile_view) ? '|' . $mobile_view : '');
235
			if (empty($updates[$row['id_block']]) || $updates[$row['id_block']] === '|')
236
			{
237
				$updates[$row['id_block']] = 'portal|';
238
			}
239
		}
240
		$db->free_result($result);
241
242
		// Now check if each update already exists as a profile, if not, then add it and then update the block that used it !
243
		foreach ($updates as $id => $visibility)
244
		{
245
			// Find the old visibility value in the profile table
246
			$result = $db->query('', '
247
				SELECT
248
					id_profile
249
				FROM {db_prefix}sp_profiles
250
				WHERE value = {string:value}
251
					AND type = {int:type}
252
				LIMIT 1',
253
				array(
254
					'value' => $visibility,
255
					'type' => 3
256
				)
257
			);
258
			$visibility_profile = '';
259
			if ($db->num_rows($result) !== 0)
260
			{
261
				list ($visibility_profile) = $db->fetch_row($result);
262
				$db->free_result($result);
263
			}
264
265
			if (empty($visibility_profile))
266
			{
267
				// Not existing, so we add it and get the new id.
268
				$db->insert('ignore',
269
					'{db_prefix}sp_profiles',
270
					array('type' => 'int', 'name' => 'text', 'value' => 'text'),
271
					array(
272
						array(3, '$_converted_' . $id, $visibility),
273
					),
274
					array('id_profile')
275
				);
276
				$visibility_profile = $db->insert_id('{db_prefix}sp_profiles', 'id_profile');
277
			}
278
279
			// Update the block to use this visibility profile
280
			{
281
				$db->query('', '
282
					UPDATE {db_prefix}sp_blocks
283
					SET visibility = {string:visibility}
284
					WHERE id_block = {int:id}',
285
					array(
286
						'visibility' => $visibility_profile,
287
						'id' => $id
288
					)
289
				);
290
			}
291
		}
292
293
		// No need for the old columns now
294
		$db_table->db_remove_column('{db_prefix}sp_blocks', 'display');
295
		$db_table->db_remove_column('{db_prefix}sp_blocks', 'display_custom');
296
		$db_table->db_remove_column('{db_prefix}sp_blocks', 'mobile_view');
297
	}
298
}
299
300
/**
301
 * Update tables to use styles profiles id's in place of old style text
302
 *
303
 * @param bool $has_style_profiles
304
 */
305
function updateStyleProfiles($has_style_profiles)
306
{
307
	$db_table = db_table();
308
	$db = database();
309
310
	// Update tables to use styles (profiles) in place of style
311
	foreach (array('id_article' => 'sp_articles', 'id_block' => 'sp_blocks', 'id_page' => 'sp_pages') as $key => $sp_table)
312
	{
313
		$block_cols = $db_table->db_list_columns('{db_prefix}' . $sp_table, true);
314
		if (!isset($block_cols['styles']))
315
		{
316
			// Add the new styles column, it will replace the old style column
317
			$db_table->db_add_column('{db_prefix}' . $sp_table, array('name' => 'styles', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true));
318
319
			// Find the old style value in the profile table that matches
320
			$result = $db->query('', '
321
				SELECT
322
					sp.id_profile, old.' . $key . '
323
				FROM {db_prefix}sp_profiles AS sp
324
					INNER JOIN {db_prefix}' . $sp_table . ' AS old ON (sp.value = old.style)
325
				WHERE sp.type = {int:type}',
326
				array(
327
					'type' => 2,
328
					'db_error_skip' => true,
329
				)
330
			);
331
			$updates = array();
332
			if ($result)
333
			{
334
				while ($row = $db->fetch_assoc($result))
335
				{
336
					$updates[$row[$key]] = $row['id_profile'];
337
				}
338
				$db->free_result($result);
339
			}
340
341
			// Add the styles profile id for any old style that matched new ones
342
			foreach ($updates as $id => $style)
343
			{
344
				$db->query('', '
345
					UPDATE {db_prefix}' . $sp_table . '
346
					SET styles = {int:style}
347
					WHERE ' . $key . '={int:id}',
348
					array(
349
						'style' => $style,
350
						'id' => $id
351
					)
352
				);
353
			}
354
355
			// Set any blank ones to the default.
356
			$db->query('', '
357
				UPDATE {db_prefix}' . $sp_table . '
358
				SET styles = {int:style}
359
				WHERE styles = {int:id}',
360
				array(
361
					'style' => !empty($has_style_profiles) ? $has_style_profiles : 4,
362
					'id' => 0
363
				)
364
			);
365
366
			// No need for the old column now
367
			$db_table->db_remove_column('{db_prefix}' . $sp_table, 'style');
368
		}
369
	}
370
}
371
372
/**
373
 * Checks if any Visibility profiles already exist in the system
374
 *
375
 * @return bool
376
 */
377
function checkForVisibilityProfiles()
378
{
379
	$db = database();
380
381
	// Do visibility profiles exist?
382
	$result = $db->query('', '
383
		SELECT
384
			id_profile
385
		FROM {db_prefix}sp_profiles
386
		WHERE type = {int:type}
387
		LIMIT {int:limit}',
388
		array(
389
			'type' => 3,
390
			'limit' => 1,
391
		)
392
	);
393
	$has_visibility_profiles = '';
394
	if ($db->num_rows($result) !== 0)
395
	{
396
		list ($has_visibility_profiles) = $db->fetch_row($result);
397
		$db->free_result($result);
398
	}
399
400
	return !empty($has_visibility_profiles);
401
}
402
403
/**
404
 * Checks if any Style profiles already exist in the system
405
 *
406
 * @return bool
407
 */
408
function checkForStyleProfiles()
409
{
410
	$db = database();
411
412
	// Do style profiles exist already?
413
	$result = $db->query('', '
414
		SELECT
415
			id_profile
416
		FROM {db_prefix}sp_profiles
417
		WHERE type = {int:type}
418
		LIMIT {int:limit}',
419
		array(
420
			'type' => 2,
421
			'limit' => 1,
422
		)
423
	);
424
	$has_style_profiles = '';
425
	if ($db->num_rows($result) !== 0)
426
	{
427
		list ($has_style_profiles) = $db->fetch_row($result);
428
		$db->free_result($result);
429
	}
430
431
	return !empty($has_style_profiles);
432
}
433
434
/**
435
 * Checks if any Permission profiles already exist in the system
436
 *
437
 * @return bool
438
 */
439
function checkForPermissionProfiles()
440
{
441
	$db = database();
442
443
	$result = $db->query('', '
444
		SELECT 
445
			id_profile
446
		FROM {db_prefix}sp_profiles
447
		WHERE type = {int:type}
448
		LIMIT {int:limit}',
449
		array(
450
			'type' => 1,
451
			'limit' => 1,
452
		)
453
	);
454
	$has_permission_profiles = '';
455
	if ($db->num_rows($result) !== 0)
456
	{
457
		list ($has_permission_profiles) = $db->fetch_row($result);
458
		$db->free_result($result);
459
	}
460
461
	return !empty($has_permission_profiles);
462
}
463
464
/**
465
 * Checks if there are any blocks in the system
466
 *
467
 * @return bool
468
 */
469
function checkForBlocks()
470
{
471
	$db = database();
472
473
	$result = $db->query('', '
474
		SELECT 
475
			id_block
476
		FROM {db_prefix}sp_blocks
477
		LIMIT 1',
478
		array()
479
	);
480
	$has_blocks = '';
481
	if ($db->num_rows($result) !== 0)
482
	{
483
		list ($has_blocks) = $db->fetch_row($result);
484
		$db->free_result($result);
485
	}
486
487
	return !empty($has_blocks);
488
}
489
490
/**
491
 * Beta 2 introduced blocks as classes, so a little renaming is needed
492
 * From the "old" (pre-classes) to the "new" (blocks as classes) format
493
 */
494
function updateBlocks()
495
{
496
	$db = database();
497
498
	$replace_array = array(
499
		'sp_userInfo' => 'User_Info',
500
		'sp_latestMember' => 'Latest_Member',
501
		'sp_whosOnline' => 'Whos_Online',
502
		'sp_boardStats' => 'Board_Stats',
503
		'sp_topPoster' => 'Top_Poster',
504
		'sp_topStatsMember' => 'Top_Stats_Member',
505
		'sp_recent' => 'Recent',
506
		'sp_topTopics' => 'Top_Topics',
507
		'sp_topBoards' => 'Top_Boards',
508
		'sp_showPoll' => 'Show_Poll',
509
		'sp_boardNews' => 'Board_News',
510
		'sp_quickSearch' => 'Quick_Search',
511
		'sp_news' => 'News',
512
		'sp_attachmentImage' => 'Attachment_Image',
513
		'sp_attachmentRecent' => 'Attachment_Recent',
514
		'sp_calendar' => 'Calendar',
515
		'sp_calendarInformation' => 'Calendar_Information',
516
		'sp_rssFeed' => 'Rss_Feed',
517
		'sp_theme_select' => 'Theme_Select',
518
		'sp_staff' => 'Staff',
519
		'sp_articles' => 'Articles',
520
		'sp_shoutbox' => 'Shoutbox',
521
		'sp_gallery' => 'Gallery',
522
		'sp_menu' => 'Menu',
523
		'sp_bbc' => 'Bbc',
524
		'sp_html' => 'Html',
525
		'sp_php' => 'Php',
526
	);
527
528
	foreach ($replace_array as $from => $to)
529
	{
530
		$db->query('', '
531
			UPDATE {db_prefix}sp_blocks
532
			SET type = REPLACE(type, {string:from}, {string:to})
533
			WHERE type LIKE {string:from}',
534
			array(
535
				'from' => $from,
536
				'to' => $to,
537
			)
538
		);
539
	}
540
}
541
542
/**
543
 * Defines and creates all of the tables that make the portal tick
544
 */
545
function defineTables()
546
{
547
	$sp_tables = array(
548
		'sp_articles' => array(
549
			'columns' => array(
550
				array('name' => 'id_article', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
551
				array('name' => 'id_category', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
552
				array('name' => 'id_member', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
553
				array('name' => 'member_name', 'type' => 'varchar', 'size' => 80, 'default' => ''),
554
				array('name' => 'namespace', 'type' => 'varchar', 'size' => 255, 'default' => ''),
555
				array('name' => 'title', 'type' => 'varchar', 'size' => 255, 'default' => ''),
556
				array('name' => 'body', 'type' => 'text'),
557
				array('name' => 'type', 'type' => 'varchar', 'size' => 40, 'default' => ''),
558
				array('name' => 'date', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
559
				array('name' => 'permissions', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
560
				array('name' => 'styles', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
561
				array('name' => 'views', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
562
				array('name' => 'comments', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
563
				array('name' => 'status', 'type' => 'tinyint', 'size' => 4, 'default' => 1),
564
			),
565
			'indexes' => array(
566
				array('type' => 'primary', 'columns' => array('id_article')),
567
			),
568
		),
569
		'sp_blocks' => array(
570
			'columns' => array(
571
				array('name' => 'id_block', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
572
				array('name' => 'label', 'type' => 'varchar', 'size' => 255, 'default' => ''),
573
				array('name' => 'type', 'type' => 'varchar', 'size' => 40, 'default' => ''),
574
				array('name' => 'col', 'type' => 'tinyint', 'size' => 4, 'default' => 0),
575
				array('name' => 'row', 'type' => 'tinyint', 'size' => 4, 'default' => 0),
576
				array('name' => 'permissions', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
577
				array('name' => 'styles', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
578
				array('name' => 'visibility', 'type' => 'mediumint', 'size' => 8, 'default' => 0),
579
				array('name' => 'state', 'type' => 'tinyint', 'size' => 4, 'default' => 1),
580
				array('name' => 'force_view', 'type' => 'tinyint', 'size' => 2, 'default' => 0),
581
			),
582
			'indexes' => array(
583
				array('type' => 'primary', 'columns' => array('id_block')),
584
				array('type' => 'index', 'columns' => array('state')),
585
			),
586
		),
587
		'sp_categories' => array(
588
			'columns' => array(
589
				array('name' => 'id_category', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
590
				array('name' => 'namespace', 'type' => 'varchar', 'size' => 255, 'default' => ''),
591
				array('name' => 'name', 'type' => 'varchar', 'size' => 255, 'default' => ''),
592
				array('name' => 'description', 'type' => 'text'),
593
				array('name' => 'permissions', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
594
				array('name' => 'articles', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
595
				array('name' => 'status', 'type' => 'tinyint', 'size' => 4, 'default' => 1),
596
			),
597
			'indexes' => array(
598
				array('type' => 'primary', 'columns' => array('id_category')),
599
			),
600
		),
601
		'sp_comments' => array(
602
			'columns' => array(
603
				array('name' => 'id_comment', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
604
				array('name' => 'id_article', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
605
				array('name' => 'id_member', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
606
				array('name' => 'member_name', 'type' => 'varchar', 'size' => 80, 'default' => ''),
607
				array('name' => 'body', 'type' => 'text'),
608
				array('name' => 'log_time', 'type' => 'int', 'size' => 10, 'default' => 0),
609
			),
610
			'indexes' => array(
611
				array('type' => 'primary', 'columns' => array('id_comment')),
612
			),
613
		),
614
		'sp_custom_menus' => array(
615
			'columns' => array(
616
				array('name' => 'id_menu', 'type' => 'mediumint', 'size' => 8, 'auto' => true),
617
				array('name' => 'name', 'type' => 'tinytext'),
618
			),
619
			'indexes' => array(
620
				array('type' => 'primary', 'columns' => array('id_menu')),
621
			),
622
		),
623
		'sp_functions' => array(
624
			'columns' => array(
625
				array('name' => 'id_function', 'type' => 'tinyint', 'size' => 4, 'auto' => true, 'unsigned' => true),
626
				array('name' => 'function_order', 'type' => 'tinyint', 'size' => 4, 'default' => 0),
627
				array('name' => 'name', 'type' => 'varchar', 'size' => 40, 'default' => ''),
628
			),
629
			'indexes' => array(
630
				array('type' => 'primary', 'columns' => array('id_function')),
631
			),
632
		),
633
		'sp_menu_items' => array(
634
			'columns' => array(
635
				array('name' => 'id_item', 'type' => 'mediumint', 'size' => 8, 'auto' => true),
636
				array('name' => 'id_menu', 'type' => 'mediumint', 'size' => 8, 'default' => 0),
637
				array('name' => 'namespace', 'type' => 'tinytext'),
638
				array('name' => 'title', 'type' => 'tinytext'),
639
				array('name' => 'href', 'type' => 'tinytext'),
640
				array('name' => 'target', 'type' => 'tinyint', 'size' => 4, 'default' => 0),
641
			),
642
			'indexes' => array(
643
				array('type' => 'primary', 'columns' => array('id_item')),
644
			),
645
		),
646
		'sp_pages' => array(
647
			'columns' => array(
648
				array('name' => 'id_page', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
649
				array('name' => 'namespace', 'type' => 'varchar', 'size' => 255, 'default' => ''),
650
				array('name' => 'title', 'type' => 'varchar', 'size' => 255, 'default' => ''),
651
				array('name' => 'body', 'type' => 'mediumtext'),
652
				array('name' => 'type', 'type' => 'varchar', 'size' => 40, 'default' => ''),
653
				array('name' => 'permissions', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
654
				array('name' => 'styles', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
655
				array('name' => 'views', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
656
				array('name' => 'status', 'type' => 'tinyint', 'size' => 4, 'default' => 1),
657
			),
658
			'indexes' => array(
659
				array('type' => 'primary', 'columns' => array('id_page')),
660
			),
661
		),
662
		'sp_parameters' => array(
663
			'columns' => array(
664
				array('name' => 'id_block', 'type' => 'mediumint', 'size' => 9, 'default' => 0, 'unsigned' => true),
665
				array('name' => 'variable', 'type' => 'varchar', 'size' => 255, 'default' => ''),
666
				array('name' => 'value', 'type' => 'text'),
667
			),
668
			'indexes' => array(
669
				array('type' => 'primary', 'columns' => array('id_block', 'variable')),
670
				array('type' => 'key', 'columns' => array('variable')),
671
			),
672
		),
673
		'sp_profiles' => array(
674
			'columns' => array(
675
				array('name' => 'id_profile', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
676
				array('name' => 'type', 'type' => 'tinyint', 'size' => 4, 'default' => 0),
677
				array('name' => 'name', 'type' => 'varchar', 'size' => 255, 'default' => ''),
678
				array('name' => 'value', 'type' => 'text'),
679
			),
680
			'indexes' => array(
681
				array('type' => 'primary', 'columns' => array('id_profile')),
682
			),
683
		),
684
		'sp_shoutboxes' => array(
685
			'columns' => array(
686
				array('name' => 'id_shoutbox', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
687
				array('name' => 'name', 'type' => 'varchar', 'size' => 255, 'default' => ''),
688
				array('name' => 'permissions', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
689
				array('name' => 'moderator_groups', 'type' => 'text'),
690
				array('name' => 'warning', 'type' => 'text'),
691
				array('name' => 'allowed_bbc', 'type' => 'text'),
692
				array('name' => 'height', 'type' => 'smallint', 'size' => 5, 'default' => 200, 'unsigned' => true),
693
				array('name' => 'num_show', 'type' => 'smallint', 'size' => 5, 'default' => 20, 'unsigned' => true),
694
				array('name' => 'num_max', 'type' => 'mediumint', 'size' => 8, 'default' => 1000, 'unsigned' => true),
695
				array('name' => 'refresh', 'type' => 'tinyint', 'size' => 4, 'default' => 1),
696
				array('name' => 'reverse', 'type' => 'tinyint', 'size' => 4, 'default' => 0),
697
				array('name' => 'caching', 'type' => 'tinyint', 'size' => 4, 'default' => 1),
698
				array('name' => 'status', 'type' => 'tinyint', 'size' => 4, 'default' => 1),
699
				array('name' => 'num_shouts', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
700
				array('name' => 'last_update', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
701
			),
702
			'indexes' => array(
703
				array('type' => 'primary', 'columns' => array('id_shoutbox')),
704
			),
705
		),
706
		'sp_shouts' => array(
707
			'columns' => array(
708
				array('name' => 'id_shout', 'type' => 'mediumint', 'size' => 8, 'auto' => true, 'unsigned' => true),
709
				array('name' => 'id_shoutbox', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
710
				array('name' => 'id_member', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
711
				array('name' => 'member_name', 'type' => 'varchar', 'size' => 80, 'default' => ''),
712
				array('name' => 'log_time', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
713
				array('name' => 'body', 'type' => 'text'),
714
			),
715
			'indexes' => array(
716
				array('type' => 'primary', 'columns' => array('id_shout')),
717
			),
718
		),
719
		'sp_attachments' => array(
720
			'columns' => array(
721
				array('name' => 'id_attach', 'type' => 'int', 'size' => 10, 'auto' => true, 'unsigned' => true),
722
				array('name' => 'id_thumb', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
723
				array('name' => 'id_article', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
724
				array('name' => 'id_member', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
725
				array('name' => 'attachment_type', 'type' => 'tinyint', 'size' => 3, 'default' => 0, 'unsigned' => true),
726
				array('name' => 'filename', 'type' => 'varchar', 'size' => 255, 'default' => ''),
727
				array('name' => 'file_hash', 'type' => 'varchar', 'size' => 40, 'default' => ''),
728
				array('name' => 'fileext', 'type' => 'varchar', 'size' => 8, 'default' => ''),
729
				array('name' => 'size', 'type' => 'int', 'size' => 10, 'default' => 0, 'unsigned' => true),
730
				array('name' => 'width', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
731
				array('name' => 'height', 'type' => 'mediumint', 'size' => 8, 'default' => 0, 'unsigned' => true),
732
				array('name' => 'mime_type', 'type' => 'varchar', 'size' => 20, 'default' => ''),
733
			),
734
			'indexes' => array(
735
				array('type' => 'primary', 'columns' => array('id_attach')),
736
				array('type' => 'index', 'columns' => array('id_member', 'id_attach')),
737
				array('type' => 'key', 'columns' => array('id_article')),
738
				array('type' => 'key', 'columns' => array('attachment_type')),
739
				array('type' => 'key', 'columns' => array('id_thumb')),
740
			),
741
		),
742
	);
743
744
	$db_table = db_table();
745
746
	// Create the tables, if they don't already exist
747
	foreach ($sp_tables as $sp_table => $data)
748
	{
749
		$db_table->db_create_table('{db_prefix}' . $sp_table, $data['columns'], $data['indexes'], array(), 'ignore');
750
	}
751
752
	return $sp_tables;
753
}
754
755
/**
756
 * Adds several default blocks to a new installation
757
 */
758
function addDefaultBlocks()
759
{
760
	$db = database();
761
762
	$welcome_text = '<h2 style="text-align: center;">Welcome to SimplePortal!</h2>
763
	<p>Although always developing, SimplePortal is produced with the user in mind first. User feedback is the number one method of 
764
	growth, and our users are always finding ways for SimplePortal to improve and grow. It has added numerous user-requested 
765
	features such as articles, block types and the ability to completely customize the portal page.</p>
766
	<p>All this and SimplePortal has remained Simple! SimplePortal is built for simplicity and ease of use; ensuring the average 
767
	forum administrator can install SimplePortal, configure a few settings, and show off the brand new portal to the users in 
768
	minutes. Confusing menus, undesired pre-loaded blocks and settings that cannot be found are all avoided as much as possible. 
769
	Because when it comes down to it, SimplePortal is YOUR portal, and should reflect your taste as much as possible.</p>';
770
771
	$default_blocks = array(
772
		'user_info' => array('label' => 'User Info', 'type' => 'User_Info', 'col' => 1, 'row' => 1, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
773
		'whos_online' => array('label' => 'Who&#039;s Online', 'type' => 'Whos_Online', 'col' => 1, 'row' => 2, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
774
		'board_stats' => array('label' => 'Board Stats', 'type' => 'Board_Stats', 'col' => 1, 'row' => 3, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
775
		'theme_select' => array('label' => 'Theme Select', 'type' => 'Theme_Select', 'col' => 1, 'row' => 4, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
776
		'search' => array('label' => 'Search', 'type' => 'Quick_Search', 'col' => 1, 'row' => 5, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
777
		'news' => array('label' => 'News', 'type' => 'News', 'col' => 2, 'row' => 1, 'permissions' => 3, 'styles' => 8, 'visibility' => 14),
778
		'welcome' => array('label' => 'Welcome', 'type' => 'Html', 'col' => 2, 'row' => 2, 'permissions' => 3, 'styles' => 8, 'visibility' => 14),
779
		'board_news' => array('label' => 'Board News', 'type' => 'Board_News', 'col' => 2, 'row' => 3, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
780
		'recent_topics' => array('label' => 'Recent Topics', 'type' => 'Recent', 'col' => 3, 'row' => 1, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
781
		'top_poster' => array('label' => 'Top Poster', 'type' => 'Top_Poster', 'col' => 4, 'row' => 1, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
782
		'recent_posts' => array('label' => 'Recent Posts', 'type' => 'Recent', 'col' => 4, 'row' => 2, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
783
		'staff' => array('label' => 'Forum Staff', 'type' => 'Staff', 'col' => 4, 'row' => 3, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
784
		'calendar' => array('label' => 'Calendar', 'type' => 'Calendar', 'col' => 4, 'row' => 4, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
785
		'top_boards' => array('label' => 'Top Boards', 'type' => 'Top_Boards', 'col' => 4, 'row' => 5, 'permissions' => 3, 'styles' => 4, 'visibility' => 14),
786
	);
787
788
	// Add the default blocks to the system
789
	$db->insert('ignore',
790
		'{db_prefix}sp_blocks',
791
		array('label' => 'text', 'type' => 'text', 'col' => 'int', 'row' => 'int', 'permissions' => 'int', 'styles' => 'int', 'visibility' => 'int'),
792
		$default_blocks,
793
		array('id_block', 'state')
794
	);
795
796
	// Set a few optional block parameters
797
	$request = $db->query('', '
798
		SELECT 
799
			MIN(id_block) AS id, type
800
		FROM {db_prefix}sp_blocks
801
		WHERE type IN ({array_string:types})
802
		GROUP BY type
803
		LIMIT 4',
804
		array(
805
			'types' => array('Html', 'Board_News', 'Calendar', 'Recent'),
806
		)
807
	);
808
	$block_ids = array();
809
	while ($row = $db->fetch_assoc($request))
810
	{
811
		$block_ids[$row['type']] = $row['id'];
812
	}
813
	$db->free_result($request);
814
815
	$default_parameters = array(
816
		array('id_block' => $block_ids['Html'], 'variable' => 'content', 'value' => htmlspecialchars($welcome_text)),
817
		array('id_block' => $block_ids['Board_News'], 'variable' => 'avatar', 'value' => 1),
818
		array('id_block' => $block_ids['Board_News'], 'variable' => 'per_page', 'value' => 3),
819
		array('id_block' => $block_ids['Calendar'], 'variable' => 'events', 'value' => 1),
820
		array('id_block' => $block_ids['Calendar'], 'variable' => 'birthdays', 'value' => 1),
821
		array('id_block' => $block_ids['Calendar'], 'variable' => 'holidays', 'value' => 1),
822
		array('id_block' => $block_ids['Recent'], 'variable' => 'type', 'value' => 1),
823
		array('id_block' => $block_ids['Recent'], 'variable' => 'display', 'value' => 1),
824
	);
825
826
	$db->insert('replace',
827
		'{db_prefix}sp_parameters',
828
		array('id_block' => 'int', 'variable' => 'text', 'value' => 'text'),
829
		$default_parameters,
830
		array('id_block', 'variable')
831
	);
832
}
833