Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 445 2007-04-10 18:04:09Z 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 convert a string from $from- to $to-encoding, using mysql
349
function my_mysql_iconv($string, $from, $to) {
350
	// keep current character set values:
351
	$character_set_database = mysql_result(mysql_query("SELECT @@character_set_client"),0,0);
352
	$character_set_results = mysql_result(mysql_query("SELECT @@character_set_results"),0,0);
353
	$collation_results = mysql_result(mysql_query("SELECT @@collation_connection"),0,0);
354
	mysql_query("SET character_set_client=$from");
355
	mysql_query("SET character_set_results=$to");
356
	mysql_query("SET collation_connection=utf8_unicode_ci");
357
	$string_escaped = mysql_real_escape_string($string);
358
	$converted_string = mysql_result(mysql_query("SELECT '$string_escaped'"),0,0);
359
	// restore previous character set values:
360
	mysql_query("SET character_set_client=$character_set_database");
361
	mysql_query("SET character_set_results=$character_set_results");
362
	mysql_query("SET collation_connection=$collation_results");
363
	return $converted_string;
364
}
365

    
366
// Function to convert a string from mixed html-entities/umlauts to pure utf-8-umlauts
367
function string_to_utf8($string, $charset=DEFAULT_CHARSET) {
368
	$charset = strtoupper($charset);
369
	if ($charset == '') { $charset = 'ISO-8859-1'; }
370

    
371
	// there's no GB2312 or ISO-8859-11 encoding in php's mb_* functions
372
	if ($charset == "GB2312") {
373
		$string=my_mysql_iconv($string, 'gb2312', 'utf8');
374
	} elseif ($charset == "ISO-8859-11") {
375
		$string=my_mysql_iconv($string, 'tis620', 'utf8');
376
	} else {
377
		$string=mb_convert_encoding($string, 'UTF-8', $charset);
378
	}
379
	$string=mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8');
380
	$string=mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');
381
	return($string);
382
}
383

    
384
// Function to convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
385
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET, $convert_htmlspecialchars=0) {
386
	$charset_out = strtoupper($charset_out);
387
	if ($charset_out == '') {
388
		$charset_out = 'ISO-8859-1';
389
	}
390
	$string = string_to_utf8($string);
391
	if($charset_out != 'UTF-8') {
392
		if ($charset_out == "GB2312") {
393
			$string=my_mysql_iconv($string, 'utf8', 'gb2312');
394
		} elseif ($charset_out == "ISO-8859-11") {
395
			$string=my_mysql_iconv($string, 'utf8', 'tis620');
396
		} else {
397
			$string=mb_convert_encoding($string, $charset_out, 'UTF-8');
398
		}
399
	}
400
	if($convert_htmlspecialchars == 1) {
401
		$string=htmlspecialchars($string);
402
	}
403
	return($string);
404
}
405

    
406
// Function to convert a string from mixed html-entitites/$charset_in-umlauts to pure html-entities
407
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET, $convert_htmlspecialchars=1) {
408
	$charset_in = strtoupper($charset_in);
409
	if ($charset_in == "") {
410
		$charset_in = 'ISO-8859-1';
411
	}
412
	$string = string_to_utf8($string, $charset_in);
413
	if($convert_htmlspecialchars == 1) {
414
		$string=htmlspecialchars($string,ENT_QUOTES);
415
	}
416
	$string=mb_convert_encoding($string,'HTML-ENTITIES','UTF-8');
417
	return($string);
418
}
419

    
420
// translate any latin/greek/cyrillic html-entities to their plain 7bit equivalents
421
function entities_to_7bit($string) {
422
	require(WB_PATH.'/framework/convert.php');
423
	$string = strtr($string, $conversion_array);
424
	return($string);
425
}
426

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

    
456
// Function to convert a desired media filename to a clean filename
457
function media_filename($string) {
458
	$string = entities_to_7bit(umlauts_to_entities($string));
459
	// Now remove all bad characters
460
	$bad = array(
461
	'\'', // '
462
	'"', // "
463
	'`', // `
464
	'!', // !
465
	'@', // @
466
	'#', // #
467
	'$', // $
468
	'%', // %
469
	'^', // ^
470
	'&', // &
471
	'*', // *
472
	'=', // =
473
	'+', // +
474
	'|', // |
475
	'/', // /
476
	'\\', // \
477
	';', // ;
478
	':', // :
479
	',', // ,
480
	'?' // ?
481
	);
482
	$string = str_replace($bad, '', $string);
483
	// Clean any page spacers at the end of string
484
	$string = trim($string);
485
	// Finally, return the cleaned string
486
	return $string;
487
}
488

    
489
// Function to work out a page link
490
if(!function_exists('page_link')) {
491
	function page_link($link) {
492
		global $admin;
493
		return $admin->page_link($link);
494
	}
495
}
496

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

    
536
// Function for working out a file mime type (if the in-built PHP one is not enabled)
537
if(!function_exists('mime_content_type')) {
538
   function mime_content_type($file) {
539
       $file = escapeshellarg($file);
540
       return trim(`file -bi $file`);
541
   }
542
}
543

    
544
// Generate a thumbnail from an image
545
function make_thumb($source, $destination, $size) {
546
	// Check if GD is installed
547
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
548
		// First figure out the size of the thumbnail
549
		list($original_x, $original_y) = getimagesize($source);
550
		if ($original_x > $original_y) {
551
			$thumb_w = $size;
552
			$thumb_h = $original_y*($size/$original_x);
553
		}
554
		if ($original_x < $original_y) {
555
			$thumb_w = $original_x*($size/$original_y);
556
			$thumb_h = $size;
557
		}
558
		if ($original_x == $original_y) {
559
			$thumb_w = $size;
560
			$thumb_h = $size;	
561
		}
562
		// Now make the thumbnail
563
		$source = imageCreateFromJpeg($source);
564
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
565
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
566
		imagejpeg($dst_img, $destination);
567
		// Clear memory
568
		imagedestroy($dst_img);
569
	   imagedestroy($source);
570
	   // Return true
571
	   return true;
572
   } else {
573
   	return false;
574
   }
575
}
576

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

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

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

    
795
// Load template into DB
796
function load_template($directory) {
797
	global $database;
798
	if(file_exists($directory.'/info.php')) {
799
		require($directory.'/info.php');
800
		if(isset($template_name)) {
801
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
802
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
803
			// Check that it doesn't already exist
804
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
805
			if($result->numRows() == 0) {
806
				// Load into DB
807
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
808
				"(directory,name,description,type,version,platform,author,license) ".
809
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
810
				"'$template_version','$template_platform','$template_author','$template_license')";
811
				$database->query($query);
812
			}
813
		}
814
	}
815
}
816

    
817
// Load language into DB
818
function load_language($file) {
819
	global $database;
820
	if(file_exists($file)) {
821
		require($file);
822
		if(isset($language_name)) {
823
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
824
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
825
			// Check that it doesn't already exist
826
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
827
			if($result->numRows() == 0) {
828
				// Load into DB
829
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
830
				"(directory,name,type,version,platform,author,license) ".
831
				"VALUES ('$language_code','$language_name','language',".
832
				"'$language_version','$language_platform','$language_author','$language_license')";
833
	 		$database->query($query);
834
			}
835
		}
836
	}
837
}
838

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

    
873
?>
(10-10/12)