Project

General

Profile

1 1155 Luisehahne
<?php
2
3
// $Id$
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, Ryan Djurovich
9
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
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);
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
	if(HOME_FOLDERS AND (!in_array('1',split(",",$_SESSION['GROUPS_ID'])))) {
161
162
		$query_home_folders = $database->query("SELECT home_folder FROM ".TABLE_PREFIX."users WHERE home_folder != '".$admin->get_home_folder()."'");
163
		if($query_home_folders->numRows() > 0) {
164
			while($folder = $query_home_folders->fetchRow()) {
165
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
166
			}
167
		}
168
		function remove_home_subs($directory = '/', $home_folders) {
169
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) {
170
				// Loop through the dirs to check the home folders sub-dirs are not shown
171
			   while(false !== ($file = readdir($handle))) {
172
					if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
173
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
174
							if($directory != '/') { $file = $directory.'/'.$file; } else { $file = '/'.$file; }
175
							foreach($home_folders AS $hf) {
176
								$hf_length = strlen($hf);
177
								if($hf_length > 0) {
178
									if(substr($file, 0, $hf_length+1) == $hf) {
179
										$home_folders[$file] = $file;
180
									}
181
								}
182
							}
183
							$home_folders = remove_home_subs($file, $home_folders);
184
						}
185
					}
186
				}
187
			}
188
			return $home_folders;
189
		}
190
		$home_folders = remove_home_subs('/', $home_folders);
191
	}
192
	return $home_folders;
193
}
194
195
// Function to create directories
196
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE) {
197
	if(!file_exists($dir_name)) {
198
		$umask = umask(0);
199
		mkdir($dir_name, $dir_mode);
200
		umask($umask);
201
		return true;
202
	} else {
203
		return false;
204
	}
205
}
206
207
// Function to chmod files and directories
208
function change_mode($name) {
209
	if(OPERATING_SYSTEM != 'windows') {
210
		// Only chmod if os is not windows
211
		if(is_dir($name)) {
212
			$mode = OCTAL_DIR_MODE;
213
		} else {
214
			$mode = OCTAL_FILE_MODE;
215
		}
216
		if(file_exists($name)) {
217
			$umask = umask(0);
218
			chmod($name, $mode);
219
			umask($umask);
220
			return true;
221
		} else {
222
			return false;
223
		}
224
	} else {
225
		return true;
226
	}
227
}
228
229
// Function to figure out if a parent exists
230
function is_parent($page_id) {
231
	global $database;
232
	// Get parent
233
	$query = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
234
	$fetch = $query->fetchRow();
235
	// If parent isnt 0 return its ID
236
	if($fetch['parent'] == '0') {
237
		return false;
238
	} else {
239
		return $fetch['parent'];
240
	}
241
}
242
243
// Function to work out level
244
function level_count($page_id) {
245
	global $database;
246
	// Get page parent
247
	$query_page = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
248
	$fetch_page = $query_page->fetchRow();
249
	$parent = $fetch_page['parent'];
250
	if($parent > 0) {
251
		// Get the level of the parent
252
		$query_parent = $database->query("SELECT level FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent' LIMIT 1");
253
		$fetch_parent = $query_parent->fetchRow();
254
		$level = $fetch_parent['level'];
255
		return $level+1;
256
	} else {
257
		return 0;
258
	}
259
}
260
261
// Function to work out root parent
262
function root_parent($page_id) {
263
	global $database;
264
	// Get page details
265
	$query_page = $database->query("SELECT parent,level FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
266
	$fetch_page = $query_page->fetchRow();
267
	$parent = $fetch_page['parent'];
268
	$level = $fetch_page['level'];
269
	if($level == 1) {
270
		return $parent;
271
	} elseif($parent == 0) {
272
		return $page_id;
273
	} else {
274
		// Figure out what the root parents id is
275
		$parent_ids = array_reverse(get_parent_ids($page_id));
276
		return $parent_ids[0];
277
	}
278
}
279
280
// Function to get page title
281
function get_page_title($id) {
282
	global $database;
283
	// Get title
284
	$query = $database->query("SELECT page_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
285
	$fetch = $query->fetchRow();
286
	// Return title
287
	return $fetch['page_title'];
288
}
289
290
// Function to get a pages menu title
291
function get_menu_title($id) {
292
	// Connect to the database
293
	$database = new database();
294
	// Get title
295
	$query = $database->query("SELECT menu_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
296
	$fetch = $query->fetchRow();
297
	// Return title
298
	return $fetch['menu_title'];
299
}
300
301
// Function to get all parent page titles
302
function get_parent_titles($parent_id) {
303
	$titles[] = get_menu_title($parent_id);
304
	if(is_parent($parent_id) != false) {
305
		$parent_titles = get_parent_titles(is_parent($parent_id));
306
		$titles = array_merge($titles, $parent_titles);
307
	}
308
	return $titles;
309
}
310
311
// Function to get all parent page id's
312
function get_parent_ids($parent_id) {
313
	$ids[] = $parent_id;
314
	if(is_parent($parent_id) != false) {
315
		$parent_ids = get_parent_ids(is_parent($parent_id));
316
		$ids = array_merge($ids, $parent_ids);
317
	}
318
	return $ids;
319
}
320
321
// Function to genereate page trail
322
function get_page_trail($page_id) {
323
	return implode(',', array_reverse(get_parent_ids($page_id)));
324
}
325
326
// Function to get all sub pages id's
327
function get_subs($parent, $subs) {
328
	// Connect to the database
329
	$database = new database();
330
	// Get id's
331
	$query = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'");
332
	if($query->numRows() > 0) {
333
		while($fetch = $query->fetchRow()) {
334
			$subs[] = $fetch['page_id'];
335
			// Get subs of this sub
336
			$subs = get_subs($fetch['page_id'], $subs);
337
		}
338
	}
339
	// Return subs array
340
	return $subs;
341
}
342
343
// Function as replacement for php's htmlspecialchars()
344
// Will not mangle HTML-entities
345
function my_htmlspecialchars($string) {
346
	$string = preg_replace('/&(?=[#a-z0-9]+;)/i', '__amp;_', $string);
347
	$string = strtr($string, array('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;', '\''=>'&#39;'));
348
	$string = preg_replace('/__amp;_(?=[#a-z0-9]+;)/i', '&', $string);
349
	return($string);
350
}
351
352
// Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
353
// Will replace all numeric and named entities except &gt; &lt; &apos; &quot; &#039; &nbsp;
354
// In case of error the returned string is unchanged, and a message is emitted.
355
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET) {
356
	require_once(WB_PATH.'/framework/functions-utf8.php');
357
	return entities_to_umlauts2($string, $charset_out);
358
}
359
360
// Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
361
// In case of error the returned string is unchanged, and a message is emitted.
362
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET) {
363
	require_once(WB_PATH.'/framework/functions-utf8.php');
364
	return umlauts_to_entities2($string, $charset_in);
365
}
366
367
// Function to convert a page title to a page filename
368
function page_filename($string) {
369
	require_once(WB_PATH.'/framework/functions-utf8.php');
370
	$string = entities_to_7bit($string);
371
	// Now remove all bad characters
372
	$bad = array(
373
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
374
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
375
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
376
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
377
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
378
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
379
	);
380
	$string = str_replace($bad, '', $string);
381
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
382
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
383
	// Now replace spaces with page spcacer
384
	$string = trim($string);
385
	$string = preg_replace('/(\s)+/', PAGE_SPACER, $string);
386
	// Now convert to lower-case
387
	$string = strtolower($string);
388
	// If there are any weird language characters, this will protect us against possible problems they could cause
389
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
390
	// Finally, return the cleaned string
391
	return $string;
392
}
393
394
// Function to convert a desired media filename to a clean filename
395
function media_filename($string) {
396
	require_once(WB_PATH.'/framework/functions-utf8.php');
397
	$string = entities_to_7bit($string);
398
	// Now remove all bad characters
399
	$bad = array(
400
	'\'', // '
401
	'"', // "
402
	'`', // `
403
	'!', // !
404
	'@', // @
405
	'#', // #
406
	'$', // $
407
	'%', // %
408
	'^', // ^
409
	'&', // &
410
	'*', // *
411
	'=', // =
412
	'+', // +
413
	'|', // |
414
	'/', // /
415
	'\\', // \
416
	';', // ;
417
	':', // :
418
	',', // ,
419
	'?' // ?
420
	);
421
	$string = str_replace($bad, '', $string);
422
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
423
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
424
	// Clean any page spacers at the end of string
425
	$string = trim($string);
426
	// Finally, return the cleaned string
427
	return $string;
428
}
429
430
// Function to work out a page link
431
if(!function_exists('page_link')) {
432
	function page_link($link) {
433
		global $admin;
434
		return $admin->page_link($link);
435
	}
436
}
437
438
// Create a new file in the pages directory
439
function create_access_file($filename,$page_id,$level) {
440
	global $admin, $MESSAGE;
441
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
442
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
443
	} else {
444
		// First make sure parent folder exists
445
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
446
		$parents = '';
447
		foreach($parent_folders AS $parent_folder) {
448
			if($parent_folder != '/' AND $parent_folder != '') {
449
				$parents .= '/'.$parent_folder;
450
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
451
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
452
				}
453
			}
454
		}
455
		// The depth of the page directory in the directory hierarchy
456
		// '/pages' is at depth 1
457
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
458
		// Work-out how many ../'s we need to get to the index page
459
		$index_location = '';
460
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
461
			$index_location .= '../';
462
		}
463
		$content = ''.
464
'<?php
465
$page_id = '.$page_id.';
466
require("'.$index_location.'config.php");
467
require(WB_PATH."/index.php");
468
?>';
469
		$handle = fopen($filename, 'w');
470
		fwrite($handle, $content);
471
		fclose($handle);
472
		// Chmod the file
473
		change_mode($filename);
474
	}
475
}
476
477
// Function for working out a file mime type (if the in-built PHP one is not enabled)
478
if(!function_exists('mime_content_type')) {
479
    function mime_content_type($filename) {
480
481
    $mime_types = array(
482
            'txt'	=> 'text/plain',
483
            'htm'	=> 'text/html',
484
            'html'	=> 'text/html',
485
            'php'	=> 'text/html',
486
            'css'	=> 'text/css',
487
            'js'	=> 'application/javascript',
488
            'json'	=> 'application/json',
489
            'xml'	=> 'application/xml',
490
            'swf'	=> 'application/x-shockwave-flash',
491
            'flv'	=> 'video/x-flv',
492
493
            // images
494
            'png'	=> 'image/png',
495
            'jpe'	=> 'image/jpeg',
496
            'jpeg'	=> 'image/jpeg',
497
            'jpg'	=> 'image/jpeg',
498
            'gif'	=> 'image/gif',
499
            'bmp'	=> 'image/bmp',
500
            'ico'	=> 'image/vnd.microsoft.icon',
501
            'tiff'	=> 'image/tiff',
502
            'tif'	=> 'image/tiff',
503
            'svg'	=> 'image/svg+xml',
504
            'svgz'	=> 'image/svg+xml',
505
506
            // archives
507
            'zip'	=> 'application/zip',
508
            'rar'	=> 'application/x-rar-compressed',
509
            'exe'	=> 'application/x-msdownload',
510
            'msi'	=> 'application/x-msdownload',
511
            'cab'	=> 'application/vnd.ms-cab-compressed',
512
513
            // audio/video
514
            'mp3'	=> 'audio/mpeg',
515
            'mp4'	=> 'audio/mpeg',
516
            'qt'	=> 'video/quicktime',
517
            'mov'	=> 'video/quicktime',
518
519
            // adobe
520
            'pdf'	=> 'application/pdf',
521
            'psd'	=> 'image/vnd.adobe.photoshop',
522
            'ai'	=> 'application/postscript',
523
            'eps'	=> 'application/postscript',
524
            'ps'	=> 'application/postscript',
525
526
            // ms office
527
            'doc'	=> 'application/msword',
528
            'rtf'	=> 'application/rtf',
529
            'xls'	=> 'application/vnd.ms-excel',
530
            'ppt'	=> 'application/vnd.ms-powerpoint',
531
532
            // open office
533
            'odt'	=> 'application/vnd.oasis.opendocument.text',
534
            'ods'	=> 'application/vnd.oasis.opendocument.spreadsheet',
535
        );
536
537
        $temp = explode('.',$filename);
538
        $ext = strtolower(array_pop($temp));
539
540
        if (array_key_exists($ext, $mime_types)) {
541
            return $mime_types[$ext];
542
        }
543
        elseif (function_exists('finfo_open')) {
544
            $finfo = finfo_open(FILEINFO_MIME);
545
            $mimetype = finfo_file($finfo, $filename);
546
            finfo_close($finfo);
547
            return $mimetype;
548
        }
549
        else {
550
            return 'application/octet-stream';
551
        }
552
    }
553
}
554
555
// Generate a thumbnail from an image
556
function make_thumb($source, $destination, $size) {
557
	// Check if GD is installed
558
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
559
		// First figure out the size of the thumbnail
560
		list($original_x, $original_y) = getimagesize($source);
561
		if ($original_x > $original_y) {
562
			$thumb_w = $size;
563
			$thumb_h = $original_y*($size/$original_x);
564
		}
565
		if ($original_x < $original_y) {
566
			$thumb_w = $original_x*($size/$original_y);
567
			$thumb_h = $size;
568
		}
569
		if ($original_x == $original_y) {
570
			$thumb_w = $size;
571
			$thumb_h = $size;
572
		}
573
		// Now make the thumbnail
574
		$source = imageCreateFromJpeg($source);
575
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
576
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
577
		imagejpeg($dst_img, $destination);
578
		// Clear memory
579
		imagedestroy($dst_img);
580
	   imagedestroy($source);
581
	   // Return true
582
	   return true;
583
   } else {
584
   	return false;
585
   }
586
}
587
588
// Function to work-out a single part of an octal permission value
589
function extract_permission($octal_value, $who, $action) {
590
	// Make sure the octal value is 4 chars long
591
	if(strlen($octal_value) == 0) {
592
		$octal_value = '0000';
593
	} elseif(strlen($octal_value) == 1) {
594
		$octal_value = '000'.$octal_value;
595
	} elseif(strlen($octal_value) == 2) {
596
		$octal_value = '00'.$octal_value;
597
	} elseif(strlen($octal_value) == 3) {
598
		$octal_value = '0'.$octal_value;
599
	} elseif(strlen($octal_value) == 4) {
600
		$octal_value = ''.$octal_value;
601
	} else {
602
		$octal_value = '0000';
603
	}
604
	// Work-out what position of the octal value to look at
605
	switch($who) {
606
	case 'u':
607
		$position = '1';
608
		break;
609
	case 'user':
610
		$position = '1';
611
		break;
612
	case 'g':
613
		$position = '2';
614
		break;
615
	case 'group':
616
		$position = '2';
617
		break;
618
	case 'o':
619
		$position = '3';
620
		break;
621
	case 'others':
622
		$position = '3';
623
		break;
624
	}
625
	// Work-out how long the octal value is and ajust acording
626
	if(strlen($octal_value) == 4) {
627
		$position = $position+1;
628
	} elseif(strlen($octal_value) != 3) {
629
		exit('Error');
630
	}
631
	// Now work-out what action the script is trying to look-up
632
	switch($action) {
633
	case 'r':
634
		$action = 'r';
635
		break;
636
	case 'read':
637
		$action = 'r';
638
		break;
639
	case 'w':
640
		$action = 'w';
641
		break;
642
	case 'write':
643
		$action = 'w';
644
		break;
645
	case 'e':
646
		$action = 'e';
647
		break;
648
	case 'execute':
649
		$action = 'e';
650
		break;
651
	}
652
	// Get the value for "who"
653
	$value = substr($octal_value, $position-1, 1);
654
	// Now work-out the details of the value
655
	switch($value) {
656
	case '0':
657
		$r = false;
658
		$w = false;
659
		$e = false;
660
		break;
661
	case '1':
662
		$r = false;
663
		$w = false;
664
		$e = true;
665
		break;
666
	case '2':
667
		$r = false;
668
		$w = true;
669
		$e = false;
670
		break;
671
	case '3':
672
		$r = false;
673
		$w = true;
674
		$e = true;
675
		break;
676
	case '4':
677
		$r = true;
678
		$w = false;
679
		$e = false;
680
		break;
681
	case '5':
682
		$r = true;
683
		$w = false;
684
		$e = true;
685
		break;
686
	case '6':
687
		$r = true;
688
		$w = true;
689
		$e = false;
690
		break;
691
	case '7':
692
		$r = true;
693
		$w = true;
694
		$e = true;
695
		break;
696
	default:
697
		$r = false;
698
		$w = false;
699
		$e = false;
700
	}
701
	// And finally, return either true or false
702
	return $$action;
703
}
704
705
// Function to delete a page
706
function delete_page($page_id) {
707
708
	global $admin, $database, $MESSAGE;
709
710
	// Find out more about the page
711
	$database = new database();
712
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
713
	$results = $database->query($query);
714
	if($database->is_error()) {
715
		$admin->print_error($database->get_error());
716
	}
717
	if($results->numRows() == 0) {
718
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
719
	}
720
	$results_array = $results->fetchRow();
721
	$parent = $results_array['parent'];
722
	$level = $results_array['level'];
723
	$link = $results_array['link'];
724
	$page_title = ($results_array['page_title']);
725
	$menu_title = ($results_array['menu_title']);
726
727
	// Get the sections that belong to the page
728
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
729
	if($query_sections->numRows() > 0) {
730
		while($section = $query_sections->fetchRow()) {
731
			// Set section id
732
			$section_id = $section['section_id'];
733
			// Include the modules delete file if it exists
734
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
735
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
736
			}
737
		}
738
	}
739
740
	// Update the pages table
741
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
742
	$database->query($query);
743
	if($database->is_error()) {
744
		$admin->print_error($database->get_error());
745
	}
746
747
	// Update the sections table
748
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
749
	$database->query($query);
750
	if($database->is_error()) {
751
		$admin->print_error($database->get_error());
752
	}
753
754
	// Include the ordering class or clean-up ordering
755
	require_once(WB_PATH.'/framework/class.order.php');
756
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
757
	$order->clean($parent);
758
759
	// Unlink the page access file and directory
760
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
761
	$filename = $directory.PAGE_EXTENSION;
762
	$directory .= '/';
763
	if(file_exists($filename)) {
764
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
765
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
766
		} else {
767
			unlink($filename);
768
			if(file_exists($directory) && rtrim($directory,'/')!=WB_PATH.PAGES_DIRECTORY && substr($link, 0, 1) != '.') {
769
				rm_full_dir($directory);
770
			}
771
		}
772
	}
773
774
}
775
776
// Load module into DB
777
function load_module($directory, $install = false) {
778
	global $database,$admin,$MESSAGE;
779
	if(is_dir($directory) AND file_exists($directory.'/info.php'))
780
	{
781
		require($directory.'/info.php');
782
		if(isset($module_name))
783
	{
784
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
785
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
786
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
787
			$module_function = strtolower($module_function);
788
			// Check that it doesn't already exist
789
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
790
			if($result->numRows() == 0)
791
			{
792
				// Load into DB
793
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
794
				"(directory,name,description,type,function,version,platform,author,license) ".
795
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
796
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
797
				$database->query($query);
798
				// Run installation script
799
				if($install == true)
800
				{
801
					if(file_exists($directory.'/install.php')) {
802
						require($directory.'/install.php');
803
					}
804
				}
805
			}
806
		}
807
	}
808
}
809
810
// Load template into DB
811
function load_template($directory) {
812
	global $database;
813
	if(file_exists($directory.'/info.php')) {
814
		require($directory.'/info.php');
815
		if(isset($template_name)) {
816
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
817
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
818
			if(!isset($template_function)) { $template_function = 'template'; }
819
			// Check that it doesn't already exist
820
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
821
			if($result->numRows() == 0) {
822
				// Load into DB
823
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
824
				"(directory,name,description,type,function,version,platform,author,license) ".
825
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
826
				"'$template_function','$template_version','$template_platform','$template_author','$template_license')";
827
				$database->query($query);
828
			}
829
		}
830
	}
831
}
832
833
// Load language into DB
834
function load_language($file) {
835
	global $database;
836
	if (file_exists($file) && preg_match('#^([A-Z]{2}.php)#', basename($file))) {
837
		require($file);
838
		if(isset($language_name)) {
839
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
840
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
841
			// Check that it doesn't already exist
842
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
843
			if($result->numRows() == 0) {
844
				// Load into DB
845
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
846
				"(directory,name,type,version,platform,author,license) ".
847
				"VALUES ('$language_code','$language_name','language',".
848
				"'$language_version','$language_platform','$language_author','$language_license')";
849
	 		$database->query($query);
850
			}
851
		}
852
	}
853
}
854
855
// Upgrade module info in DB, optionally start upgrade script
856
function upgrade_module($directory, $upgrade = false) {
857
	global $database, $admin, $MESSAGE;
858
	$directory = WB_PATH . "/modules/$directory";
859
	if(file_exists($directory.'/info.php')) {
860
		require($directory.'/info.php');
861
		if(isset($module_name)) {
862
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
863
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
864
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
865
			$module_function = strtolower($module_function);
866
			// Check that it does already exist
867
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
868
			if($result->numRows() > 0) {
869
				// Update in DB
870
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
871
					"version = '$module_version', " .
872
					"description = '" . addslashes($module_description) . "', " .
873
					"platform = '$module_platform', " .
874
					"author = '$module_author', " .
875
					"license = '$module_license'" .
876
					"WHERE directory = '$module_directory'";
877
				$database->query($query);
878
				// Run upgrade script
879
				if($upgrade == true) {
880
					if(file_exists($directory.'/upgrade.php')) {
881
						require($directory.'/upgrade.php');
882
					}
883
				}
884
			}
885
		}
886
	}
887
}
888
889
// extracts the content of a string variable from a string (save alternative to including files)
890
if(!function_exists('get_variable_content')) {
891
	function get_variable_content($search, $data, $striptags=true, $convert_to_entities=true) {
892
		$match = '';
893
		// search for $variable followed by 0-n whitespace then by = then by 0-n whitespace
894
		// then either " or ' then 0-n characters then either " or ' followed by 0-n whitespace and ;
895
		// the variable name is returned in $match[1], the content in $match[3]
896
		if (preg_match('/(\$' .$search .')\s*=\s*("|\')(.*)\2\s*;/', $data, $match)) {
897
			if(strip_tags(trim($match[1])) == '$' .$search) {
898
				// variable name matches, return it's value
899
				$match[3] = ($striptags == true) ? strip_tags($match[3]) : $match[3];
900
				$match[3] = ($convert_to_entities == true) ? htmlentities($match[3]) : $match[3];
901
				return $match[3];
902
			}
903
		}
904
		return false;
905
	}
906
}
907
908
?>