Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 442 2007-04-01 13:09:35Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2007, 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

    
26
/*
27

    
28
Website Baker functions file
29
This file contains general functions used in Website Baker
30

    
31
*/
32

    
33
// Stop this file from being accessed directly
34
if(!defined('WB_URL')) {
35
	header('Location: ../index.php');
36
	exit(0);
37
}
38

    
39
// Define that this file has been loaded
40
define('FUNCTIONS_FILE_LOADED', true);
41

    
42
// Function to remove a non-empty directory
43
function rm_full_dir($directory)
44
{
45
    // If suplied dirname is a file then unlink it
46
    if (is_file($directory)) {
47
        return unlink($directory);
48
    }
49

    
50
    // Empty the folder
51
    $dir = dir($directory);
52
    while (false !== $entry = $dir->read()) {
53
        // Skip pointers
54
        if ($entry == '.' || $entry == '..') {
55
            continue;
56
        }
57

    
58
        // Deep delete directories      
59
        if (is_dir("$directory/$entry")) {
60
            rm_full_dir("$directory/$entry");
61
        } else {
62
            unlink("$directory/$entry");
63
        }
64
    }
65

    
66
    // Now delete the folder
67
    $dir->close();
68
    return rmdir($directory);
69
}
70

    
71
// Function to open a directory and add to a dir list
72
function directory_list($directory) {
73
	
74
	$list = array();
75

    
76
	// Open the directory then loop through its contents
77
	$dir = dir($directory);
78
	while (false !== $entry = $dir->read()) {
79
		// Skip pointers
80
		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
81
			continue;
82
		}
83
		// Add dir and contents to list
84
		if (is_dir("$directory/$entry")) {
85
			$list = array_merge($list, directory_list("$directory/$entry"));
86
			$list[] = "$directory/$entry";
87
		}
88
	}
89

    
90
	// Now return the list
91
	return $list;
92
}
93

    
94
// Function to open a directory and add to a dir list
95
function chmod_directory_contents($directory, $file_mode) {
96
	
97
	// Set the umask to 0
98
	$umask = umask(0);
99
	
100
	// Open the directory then loop through its contents
101
	$dir = dir($directory);
102
	while (false !== $entry = $dir->read()) {
103
		// Skip pointers
104
		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
105
			continue;
106
		}
107
		// Chmod the sub-dirs contents
108
		if(is_dir("$directory/$entry")) {
109
			chmod_directory_contents("$directory/$entry", $file_mode);
110
		}
111
		change_mode($directory.'/'.$entry, 'file');
112
	}
113
	
114
	// Restore the umask
115
	umask($umask);
116

    
117
}
118

    
119
// Function to open a directory and add to a file list
120
function file_list($directory, $skip = array()) {
121
	
122
	$list = array();
123
	$skip_file = false;
124
	
125
	// Open the directory then loop through its contents
126
	$dir = dir($directory);
127
	while (false !== $entry = $dir->read()) {
128
		// Skip pointers
129
		if($entry == '.' || $entry == '..') {
130
			$skip_file = true;
131
		}
132
		// Check if we to skip anything else
133
		if($skip != array()) {
134
			foreach($skip AS $skip_name) {
135
				if($entry == $skip_name) {
136
					$skip_file = true;
137
				}
138
			}
139
		}
140
		// Add dir and contents to list
141
		if($skip_file != true AND is_file("$directory/$entry")) {
142
			$list[] = "$directory/$entry";
143
		}
144
		
145
		// Reset the skip file var
146
		$skip_file = false;
147
	}
148

    
149
	// Now delete the folder
150
	return $list;
151
}
152

    
153
// Function to get a list of home folders not to show
154
function get_home_folders() {
155
	global $database, $admin;
156
	$home_folders = array();
157
	// Only return home folders is this feature is enabled
158
	// and user is not admin
159
	if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
160
		$query_home_folders = $database->query("SELECT home_folder FROM ".TABLE_PREFIX."users WHERE home_folder != '".$admin->get_home_folder()."'");
161
		if($query_home_folders->numRows() > 0) {
162
			while($folder = $query_home_folders->fetchRow()) {
163
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
164
			}
165
		}
166
		function remove_home_subs($directory = '/', $home_folders) {
167
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) {
168
				// Loop through the dirs to check the home folders sub-dirs are not shown
169
			   while(false !== ($file = readdir($handle))) {
170
					if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
171
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
172
							if($directory != '/') { $file = $directory.'/'.$file; } else { $file = '/'.$file; }
173
							foreach($home_folders AS $hf) {
174
								$hf_length = strlen($hf);
175
								if($hf_length > 0) {
176
									if(substr($file, 0, $hf_length+1) == $hf) {
177
										$home_folders[$file] = $file;
178
									}
179
								}
180
							}
181
							$home_folders = remove_home_subs($file, $home_folders);
182
						}
183
					}
184
				}
185
			}
186
			return $home_folders;
187
		}
188
		$home_folders = remove_home_subs('/', $home_folders);
189
	}
190
	return $home_folders;
191
}
192

    
193
// Function to create directories
194
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE) {
195
	if(!file_exists($dir_name)) {
196
		$umask = umask(0);
197
		mkdir($dir_name, $dir_mode);
198
		umask($umask);
199
		return true;
200
	} else {
201
		return false;	
202
	}
203
}
204

    
205
// Function to chmod files and directories
206
function change_mode($name) {
207
	if(OPERATING_SYSTEM != 'windows') {
208
		// Only chmod if os is not windows
209
		if(is_dir($name)) {
210
			$mode = OCTAL_DIR_MODE;
211
		} else {
212
			$mode = OCTAL_FILE_MODE;
213
		}
214
		if(file_exists($name)) {
215
			$umask = umask(0);
216
			chmod($name, $mode);
217
			umask($umask);
218
			return true;
219
		} else {
220
			return false;	
221
		}
222
	} else {
223
		return true;
224
	}
225
}
226

    
227
// Function to figure out if a parent exists
228
function is_parent($page_id) {
229
	global $database;
230
	// Get parent
231
	$query = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
232
	$fetch = $query->fetchRow();
233
	// If parent isnt 0 return its ID
234
	if($fetch['parent'] == '0') {
235
		return false;
236
	} else {
237
		return $fetch['parent'];
238
	}
239
}
240

    
241
// Function to work out level
242
function level_count($page_id) {
243
	global $database;
244
	// Get page parent
245
	$query_page = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
246
	$fetch_page = $query_page->fetchRow();
247
	$parent = $fetch_page['parent'];
248
	if($parent > 0) {
249
		// Get the level of the parent
250
		$query_parent = $database->query("SELECT level FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent' LIMIT 1");
251
		$fetch_parent = $query_parent->fetchRow();
252
		$level = $fetch_parent['level'];
253
		return $level+1;
254
	} else {
255
		return 0;
256
	}
257
}
258

    
259
// Function to work out root parent
260
function root_parent($page_id) {
261
	global $database;
262
	// Get page details
263
	$query_page = $database->query("SELECT parent,level FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
264
	$fetch_page = $query_page->fetchRow();
265
	$parent = $fetch_page['parent'];
266
	$level = $fetch_page['level'];	
267
	if($level == 1) {
268
		return $parent;
269
	} elseif($parent == 0) {
270
		return $page_id;
271
	} else {
272
		// Figure out what the root parents id is
273
		$parent_ids = array_reverse(get_parent_ids($page_id));
274
		return $parent_ids[0];
275
	}
276
}
277

    
278
// Function to get page title
279
function get_page_title($id) {
280
	global $database;
281
	// Get title
282
	$query = $database->query("SELECT page_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
283
	$fetch = $query->fetchRow();
284
	// Return title
285
	return $fetch['page_title'];
286
}
287

    
288
// Function to get a pages menu title
289
function get_menu_title($id) {
290
	// Connect to the database
291
	$database = new database();
292
	// Get title
293
	$query = $database->query("SELECT menu_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
294
	$fetch = $query->fetchRow();
295
	// Return title
296
	return $fetch['menu_title'];
297
}
298

    
299
// Function to get all parent page titles
300
function get_parent_titles($parent_id) {
301
	$titles[] = get_menu_title($parent_id);
302
	if(is_parent($parent_id) != false) {
303
		$parent_titles = get_parent_titles(is_parent($parent_id));
304
		$titles = array_merge($titles, $parent_titles);
305
	}
306
	return $titles;
307
}
308

    
309
// Function to get all parent page id's
310
function get_parent_ids($parent_id) {
311
	$ids[] = $parent_id;
312
	if(is_parent($parent_id) != false) {
313
		$parent_ids = get_parent_ids(is_parent($parent_id));
314
		$ids = array_merge($ids, $parent_ids);
315
	}
316
	return $ids;
317
}
318

    
319
// Function to genereate page trail
320
function get_page_trail($page_id) {
321
	return implode(',', array_reverse(get_parent_ids($page_id)));
322
}
323

    
324
// Function to get all sub pages id's
325
function get_subs($parent, $subs) {
326
	// Connect to the database
327
	$database = new database();
328
	// Get id's
329
	$query = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'");
330
	if($query->numRows() > 0) {
331
		while($fetch = $query->fetchRow()) {
332
			$subs[] = $fetch['page_id'];
333
			// Get subs of this sub
334
			$subs = get_subs($fetch['page_id'], $subs);
335
		}
336
	}
337
	// Return subs array
338
	return $subs;
339
}
340

    
341
// Function as replecement for php's htmlspecialchars()
342
function my_htmlspecialchars($string) {
343
	$string = umlauts_to_entities($string);
344
	$string = entities_to_umlauts($string);
345
	return($string);
346
}
347

    
348
// Function to get the DEFAULT_CHARSET
349
function get_wbcharset() {
350
	$charset=strtoupper(DEFAULT_CHARSET);
351
	if(strcmp($charset,"BIG5") == 0) {
352
		$charset="BIG-5";
353
	}
354
	return($charset);
355
}
356

    
357
// Function to convert a string from $from- to $to-encoding, using mysql
358
function my_mysql_iconv($string, $from, $to) {
359
	// keep current character set values:
360
	$character_set_database = mysql_result(mysql_query("SELECT @@character_set_client"),0,0);
361
	$character_set_results = mysql_result(mysql_query("SELECT @@character_set_results"),0,0);
362
	$collation_results = mysql_result(mysql_query("SELECT @@collation_connection"),0,0);
363
	mysql_query("SET character_set_client=$from");
364
	mysql_query("SET character_set_results=$to");
365
	mysql_query("SET collation_connection=utf8_unicode_ci");
366
	$string_escaped = mysql_real_escape_string($string);
367
	$converted_string = mysql_result(mysql_query("SELECT '$string_escaped'"),0,0);
368
	// restore previous character set values:
369
	mysql_query("SET character_set_client=$character_set_database");
370
	mysql_query("SET character_set_results=$character_set_results");
371
	mysql_query("SET collation_connection=$collation_results");
372
	return $converted_string;
373
}
374

    
375
// Function to convert a string from html-entities to umlauts
376
// and encode htmlspecialchars
377
function entities_to_umlauts($string) {
378
	$charset = get_wbcharset();
379
	// there's no GB2312 or ISO-8859-11 encoding in php's mb_* functions
380
	if (strcmp($charset,"GB2312") == 0) {
381
		if(function_exists('iconv')) {
382
			$string=mb_convert_encoding($string,'UTF-8','HTML-ENTITIES');
383
			$string=iconv("UTF-8","GB2312",$string);
384
		} else {
385
			$string=mb_convert_encoding($string,'UTF-8','HTML-ENTITIES');
386
			$string=my_mysql_iconv($string, 'utf8', 'gb2312');
387
		}
388
	} elseif (strcmp($charset,"ISO-8859-11") == 0) {
389
		if(function_exists('iconv')) {
390
			$string=mb_convert_encoding($string,'UTF-8','HTML-ENTITIES');
391
			$string=iconv("UTF-8","ISO-8859-11",$string);
392
		} else {
393
			$string=mb_convert_encoding($string,'UTF-8','HTML-ENTITIES');
394
			$string=my_mysql_iconv($string, 'utf8', 'tis620');
395
		}
396
	} else {
397
		$string=mb_convert_encoding($string,$charset,'HTML-ENTITIES');
398
	}
399
	$string=htmlspecialchars($string);
400
	return($string);
401
}
402

    
403
// Function to convert a string from umlauts to html-entities
404
// and encode htmlspecialchars
405
function umlauts_to_entities($string) {
406
	$charset=get_wbcharset();
407
	// there's no GB2312 or ISO-8859-11 encoding in php's mb_* functions
408
	if (strcmp($charset,"GB2312") == 0) {
409
		if(function_exists('iconv')) {
410
			$string=iconv("GB2312","UTF-8",$string);
411
			$charset="UTF-8";
412
		} else {
413
			$string=my_mysql_iconv($string, 'gb2312', 'utf8');
414
			$charset="UTF-8";
415
		}
416
	} elseif (strcmp($charset,"ISO-8859-11") == 0) {
417
		if(function_exists('iconv')) {
418
			$string=iconv("ISO-8859-11","UTF-8",$string);
419
			$charset="UTF-8";
420
		} else {
421
			$string=my_mysql_iconv($string, 'tis620', 'utf8');
422
			$charset="UTF-8";
423
		}
424
	}
425
	$string=mb_convert_encoding($string,'HTML-ENTITIES',$charset);
426
	$string=mb_convert_encoding($string,'UTF-8','HTML-ENTITIES');
427
	$string=htmlspecialchars($string,ENT_QUOTES);
428
	$string=mb_convert_encoding($string,'HTML-ENTITIES','UTF-8');
429
	return($string);
430
}
431

    
432
// translate any "latin" html-entities to their plain 7bit equivalents
433
function entities_to_7bit($string) {
434
	require(WB_PATH.'/framework/convert.php');
435
	$string = strtr($string, $conversion_array);
436
	return($string);
437
}
438

    
439
// Function to convert a page title to a page filename
440
function page_filename($string) {
441
	$string = entities_to_7bit(umlauts_to_entities($string));
442
	// Now replace spaces with page spcacer
443
	$string = str_replace(' ', PAGE_SPACER, $string);
444
	// Now remove all bad characters
445
	$bad = array(
446
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
447
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
448
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
449
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
450
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
451
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
452
	);
453
	$string = str_replace($bad, '', $string);
454
	// Now convert to lower-case
455
	$string = strtolower($string);
456
	// Now remove multiple page spacers
457
	$string = str_replace(PAGE_SPACER.PAGE_SPACER, PAGE_SPACER, $string);
458
	// Clean any page spacers at the end of string
459
	$string = str_replace(PAGE_SPACER, ' ', $string);
460
	$string = trim($string);
461
	$string = str_replace(' ', PAGE_SPACER, $string);
462
	// If there are any weird language characters, this will protect us against possible problems they could cause
463
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
464
	// Finally, return the cleaned string
465
	return $string;
466
}
467

    
468
// Function to convert a desired media filename to a clean filename
469
function media_filename($string) {
470
	$string = entities_to_7bit(umlauts_to_entities($string));
471
	// Now remove all bad characters
472
	$bad = array(
473
	'\'', // '
474
	'"', // "
475
	'`', // `
476
	'!', // !
477
	'@', // @
478
	'#', // #
479
	'$', // $
480
	'%', // %
481
	'^', // ^
482
	'&', // &
483
	'*', // *
484
	'=', // =
485
	'+', // +
486
	'|', // |
487
	'/', // /
488
	'\\', // \
489
	';', // ;
490
	':', // :
491
	',', // ,
492
	'?' // ?
493
	);
494
	$string = str_replace($bad, '', $string);
495
	// Clean any page spacers at the end of string
496
	$string = trim($string);
497
	// Finally, return the cleaned string
498
	return $string;
499
}
500

    
501
// Function to work out a page link
502
if(!function_exists('page_link')) {
503
	function page_link($link) {
504
		global $admin;
505
		return $admin->page_link($link);
506
	}
507
}
508

    
509
// Create a new file in the pages directory
510
function create_access_file($filename,$page_id,$level) {
511
	global $admin;
512
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
513
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
514
	} else {
515
		// First make sure parent folder exists
516
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
517
		$parents = '';
518
		foreach($parent_folders AS $parent_folder) {
519
			if($parent_folder != '/' AND $parent_folder != '') {
520
				$parents .= '/'.$parent_folder;
521
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
522
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
523
				}
524
			}	
525
		}
526
		// The depth of the page directory in the directory hierarchy
527
		// '/pages' is at depth 1
528
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
529
		// Work-out how many ../'s we need to get to the index page
530
		$index_location = '';
531
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
532
			$index_location .= '../';
533
		}
534
		$content = ''.
535
'<?php
536
$page_id = '.$page_id.';
537
require("'.$index_location.'config.php");
538
require(WB_PATH."/index.php");
539
?>';
540
		$handle = fopen($filename, 'w');
541
		fwrite($handle, $content);
542
		fclose($handle);
543
		// Chmod the file
544
		change_mode($filename, 'file');
545
	}
546
}
547

    
548
// Function for working out a file mime type (if the in-built PHP one is not enabled)
549
if(!function_exists('mime_content_type')) {
550
   function mime_content_type($file) {
551
       $file = escapeshellarg($file);
552
       return trim(`file -bi $file`);
553
   }
554
}
555

    
556
// Generate a thumbnail from an image
557
function make_thumb($source, $destination, $size) {
558
	// Check if GD is installed
559
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
560
		// First figure out the size of the thumbnail
561
		list($original_x, $original_y) = getimagesize($source);
562
		if ($original_x > $original_y) {
563
			$thumb_w = $size;
564
			$thumb_h = $original_y*($size/$original_x);
565
		}
566
		if ($original_x < $original_y) {
567
			$thumb_w = $original_x*($size/$original_y);
568
			$thumb_h = $size;
569
		}
570
		if ($original_x == $original_y) {
571
			$thumb_w = $size;
572
			$thumb_h = $size;	
573
		}
574
		// Now make the thumbnail
575
		$source = imageCreateFromJpeg($source);
576
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
577
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
578
		imagejpeg($dst_img, $destination);
579
		// Clear memory
580
		imagedestroy($dst_img);
581
	   imagedestroy($source);
582
	   // Return true
583
	   return true;
584
   } else {
585
   	return false;
586
   }
587
}
588

    
589
// Function to work-out a single part of an octal permission value
590
function extract_permission($octal_value, $who, $action) {
591
	// Make sure the octal value is 4 chars long
592
	if(strlen($octal_value) == 0) {
593
		$octal_value = '0000';
594
	} elseif(strlen($octal_value) == 1) {
595
		$octal_value = '000'.$octal_value;
596
	} elseif(strlen($octal_value) == 2) {
597
		$octal_value = '00'.$octal_value;
598
	} elseif(strlen($octal_value) == 3) {
599
		$octal_value = '0'.$octal_value;
600
	} elseif(strlen($octal_value) == 4) {
601
		$octal_value = ''.$octal_value;
602
	} else {
603
		$octal_value = '0000';
604
	}
605
	// Work-out what position of the octal value to look at
606
	switch($who) {
607
	case 'u':
608
		$position = '1';
609
		break;
610
	case 'user':
611
		$position = '1';
612
		break;
613
	case 'g':
614
		$position = '2';
615
		break;
616
	case 'group':
617
		$position = '2';
618
		break;
619
	case 'o':
620
		$position = '3';
621
		break;
622
	case 'others':
623
		$position = '3';
624
		break;
625
	}
626
	// Work-out how long the octal value is and ajust acording
627
	if(strlen($octal_value) == 4) {
628
		$position = $position+1;
629
	} elseif(strlen($octal_value) != 3) {
630
		exit('Error');
631
	}
632
	// Now work-out what action the script is trying to look-up
633
	switch($action) {
634
	case 'r':
635
		$action = 'r';
636
		break;
637
	case 'read':
638
		$action = 'r';
639
		break;
640
	case 'w':
641
		$action = 'w';
642
		break;
643
	case 'write':
644
		$action = 'w';
645
		break;
646
	case 'e':
647
		$action = 'e';
648
		break;
649
	case 'execute':
650
		$action = 'e';
651
		break;
652
	}
653
	// Get the value for "who"
654
	$value = substr($octal_value, $position-1, 1);
655
	// Now work-out the details of the value
656
	switch($value) {
657
	case '0':
658
		$r = false;
659
		$w = false;
660
		$e = false;
661
		break;
662
	case '1':
663
		$r = false;
664
		$w = false;
665
		$e = true;
666
		break;
667
	case '2':
668
		$r = false;
669
		$w = true;
670
		$e = false;
671
		break;
672
	case '3':
673
		$r = false;
674
		$w = true;
675
		$e = true;
676
		break;
677
	case '4':
678
		$r = true;
679
		$w = false;
680
		$e = false;
681
		break;
682
	case '5':
683
		$r = true;
684
		$w = false;
685
		$e = true;
686
		break;
687
	case '6':
688
		$r = true;
689
		$w = true;
690
		$e = false;
691
		break;
692
	case '7':
693
		$r = true;
694
		$w = true;
695
		$e = true;
696
		break;
697
	default:
698
		$r = false;
699
		$w = false;
700
		$e = false;
701
	}
702
	// And finally, return either true or false
703
	return $$action;
704
}
705

    
706
// Function to delete a page
707
function delete_page($page_id) {
708
	
709
	global $admin, $database;
710
	
711
	// Find out more about the page
712
	$database = new database();
713
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
714
	$results = $database->query($query);
715
	if($database->is_error()) {
716
		$admin->print_error($database->get_error());
717
	}
718
	if($results->numRows() == 0) {
719
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
720
	}
721
	$results_array = $results->fetchRow();
722
	$parent = $results_array['parent'];
723
	$level = $results_array['level'];
724
	$link = $results_array['link'];
725
	$page_title = ($results_array['page_title']);
726
	$menu_title = ($results_array['menu_title']);
727
	
728
	// Get the sections that belong to the page
729
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
730
	if($query_sections->numRows() > 0) {
731
		while($section = $query_sections->fetchRow()) {
732
			// Set section id
733
			$section_id = $section['section_id'];
734
			// Include the modules delete file if it exists
735
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
736
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
737
			}
738
		}
739
	}
740
	
741
	// Update the pages table
742
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
743
	$database->query($query);
744
	if($database->is_error()) {
745
		$admin->print_error($database->get_error());
746
	}
747
	
748
	// Update the sections table
749
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
750
	$database->query($query);
751
	if($database->is_error()) {
752
		$admin->print_error($database->get_error());
753
	}
754
	
755
	// Include the ordering class or clean-up ordering
756
	require_once(WB_PATH.'/framework/class.order.php');
757
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
758
	$order->clean($parent);
759
	
760
	// Unlink the page access file and directory
761
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
762
	$filename = $directory.'.php';
763
	$directory .= '/';
764
	if(file_exists($filename) && substr($filename,0,1<>'.')) {
765
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
766
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
767
		} else {
768
			unlink($filename);
769
			if(file_exists($directory)) {
770
				rm_full_dir($directory);
771
			}
772
		}
773
	}
774
	
775
}
776

    
777
// Load module into DB
778
function load_module($directory, $install = false) {
779
	global $database,$admin,$MESSAGE;
780
	if(file_exists($directory.'/info.php')) {
781
		require($directory.'/info.php');
782
		if(isset($module_name)) {
783
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
784
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
785
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
786
			$module_function = strtolower($module_function);
787
			// Check that it doesn't already exist
788
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
789
			if($result->numRows() == 0) {
790
				// Load into DB
791
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
792
				"(directory,name,description,type,function,version,platform,author,license) ".
793
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
794
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
795
				$database->query($query);
796
				// Run installation script
797
				if($install == true) {
798
					if(file_exists($directory.'/install.php')) {
799
						require($directory.'/install.php');
800
					}
801
				}
802
			}
803
		}
804
	}
805
}
806

    
807
// Load template into DB
808
function load_template($directory) {
809
	global $database;
810
	if(file_exists($directory.'/info.php')) {
811
		require($directory.'/info.php');
812
		if(isset($template_name)) {
813
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
814
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
815
			// Check that it doesn't already exist
816
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
817
			if($result->numRows() == 0) {
818
				// Load into DB
819
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
820
				"(directory,name,description,type,version,platform,author,license) ".
821
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
822
				"'$template_version','$template_platform','$template_author','$template_license')";
823
				$database->query($query);
824
			}
825
		}
826
	}
827
}
828

    
829
// Load language into DB
830
function load_language($file) {
831
	global $database;
832
	if(file_exists($file)) {
833
		require($file);
834
		if(isset($language_name)) {
835
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
836
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
837
			// Check that it doesn't already exist
838
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
839
			if($result->numRows() == 0) {
840
				// Load into DB
841
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
842
				"(directory,name,type,version,platform,author,license) ".
843
				"VALUES ('$language_code','$language_name','language',".
844
				"'$language_version','$language_platform','$language_author','$language_license')";
845
	 		$database->query($query);
846
			}
847
		}
848
	}
849
}
850

    
851
// Upgrade module info in DB, optionally start upgrade script
852
function upgrade_module($directory, $upgrade = false) {
853
	global $database, $admin, $MESSAGE;
854
	$directory = WB_PATH . "/modules/$directory";
855
	if(file_exists($directory.'/info.php')) {
856
		require($directory.'/info.php');
857
		if(isset($module_name)) {
858
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
859
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
860
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
861
			$module_function = strtolower($module_function);
862
			// Check that it does already exist
863
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
864
			if($result->numRows() > 0) {
865
				// Update in DB
866
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
867
					"version = '$module_version', " .
868
					"description = '" . addslashes($module_description) . "', " .
869
					"platform = '$module_platform', " .
870
					"author = '$module_author', " .
871
					"license = '$module_license'" .
872
					"WHERE directory = '$module_directory'";
873
				$database->query($query);
874
				// Run upgrade script
875
				if($upgrade == true) {
876
					if(file_exists($directory.'/upgrade.php')) {
877
						require($directory.'/upgrade.php');
878
					}
879
				}
880
			}
881
		}
882
	}
883
}
884

    
885
?>
(10-10/12)