Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 1220 2009-12-21 14:10:33Z LordDarkman $
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
	if (is_dir($directory)) {
52
    $dir = dir($directory);
53
    while (false !== $entry = $dir->read()) {
54
        // Skip pointers
55
        if ($entry == '.' || $entry == '..') {
56
            continue;
57
        }
58

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

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

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

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

    
92
	// Now return the list
93
	return $list;
94
}
95

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

    
119
}
120

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

    
151
	// Now delete the folder
152
	return $list;
153
}
154

    
155
// Function to get a list of home folders not to show
156
function get_home_folders() {
157
	global $database, $admin;
158
	$home_folders = array();
159
	// Only return home folders is this feature is enabled
160
	// and user is not admin
161
//	if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
162
	if(HOME_FOLDERS AND (!in_array('1',explode(",", $_SESSION['GROUPS_ID'])))) {
163

    
164
		$query_home_folders = $database->query("SELECT home_folder FROM ".TABLE_PREFIX."users WHERE home_folder != '".$admin->get_home_folder()."'");
165
		if($query_home_folders->numRows() > 0) {
166
			while($folder = $query_home_folders->fetchRow()) {
167
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
168
			}
169
		}
170
		function remove_home_subs($directory = '/', $home_folders) {
171
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) {
172
				// Loop through the dirs to check the home folders sub-dirs are not shown
173
			   while(false !== ($file = readdir($handle))) {
174
					if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
175
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
176
							if($directory != '/') { $file = $directory.'/'.$file; } else { $file = '/'.$file; }
177
							foreach($home_folders AS $hf) {
178
								$hf_length = strlen($hf);
179
								if($hf_length > 0) {
180
									if(substr($file, 0, $hf_length+1) == $hf) {
181
										$home_folders[$file] = $file;
182
									}
183
								}
184
							}
185
							$home_folders = remove_home_subs($file, $home_folders);
186
						}
187
					}
188
				}
189
			}
190
			return $home_folders;
191
		}
192
		$home_folders = remove_home_subs('/', $home_folders);
193
	}
194
	return $home_folders;
195
}
196

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

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

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

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

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

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

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

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

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

    
323
// Function to genereate page trail
324
function get_page_trail($page_id) {
325
	return implode(',', array_reverse(get_parent_ids($page_id)));
326
}
327

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

    
345
// Function as replacement for php's htmlspecialchars()
346
// Will not mangle HTML-entities
347
function my_htmlspecialchars($string) {
348
	$string = preg_replace('/&(?=[#a-z0-9]+;)/i', '__amp;_', $string);
349
	$string = strtr($string, array('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;', '\''=>'&#39;'));
350
	$string = preg_replace('/__amp;_(?=[#a-z0-9]+;)/i', '&', $string);
351
	return($string);
352
}
353

    
354
// Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
355
// Will replace all numeric and named entities except &gt; &lt; &apos; &quot; &#039; &nbsp;
356
// In case of error the returned string is unchanged, and a message is emitted.
357
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET) {
358
	require_once(WB_PATH.'/framework/functions-utf8.php');
359
	return entities_to_umlauts2($string, $charset_out);
360
}
361

    
362
// Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
363
// In case of error the returned string is unchanged, and a message is emitted.
364
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET) {
365
	require_once(WB_PATH.'/framework/functions-utf8.php');
366
	return umlauts_to_entities2($string, $charset_in);
367
}
368

    
369
// Function to convert a page title to a page filename
370
function page_filename($string) {
371
	require_once(WB_PATH.'/framework/functions-utf8.php');
372
	$string = entities_to_7bit($string);
373
	// Now remove all bad characters
374
	$bad = array(
375
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
376
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
377
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
378
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
379
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
380
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
381
	);
382
	$string = str_replace($bad, '', $string);
383
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
384
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
385
	// Now replace spaces with page spcacer
386
	$string = trim($string);
387
	$string = preg_replace('/(\s)+/', PAGE_SPACER, $string);
388
	// Now convert to lower-case
389
	$string = strtolower($string);
390
	// If there are any weird language characters, this will protect us against possible problems they could cause
391
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
392
	// Finally, return the cleaned string
393
	return $string;
394
}
395

    
396
// Function to convert a desired media filename to a clean filename
397
function media_filename($string) {
398
	require_once(WB_PATH.'/framework/functions-utf8.php');
399
	$string = entities_to_7bit($string);
400
	// Now remove all bad characters
401
	$bad = array(
402
	'\'', // '
403
	'"', // "
404
	'`', // `
405
	'!', // !
406
	'@', // @
407
	'#', // #
408
	'$', // $
409
	'%', // %
410
	'^', // ^
411
	'&', // &
412
	'*', // *
413
	'=', // =
414
	'+', // +
415
	'|', // |
416
	'/', // /
417
	'\\', // \
418
	';', // ;
419
	':', // :
420
	',', // ,
421
	'?' // ?
422
	);
423
	$string = str_replace($bad, '', $string);
424
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
425
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
426
	// Clean any page spacers at the end of string
427
	$string = trim($string);
428
	// Finally, return the cleaned string
429
	return $string;
430
}
431

    
432
// Function to work out a page link
433
if(!function_exists('page_link')) {
434
	function page_link($link) {
435
		global $admin;
436
		return $admin->page_link($link);
437
	}
438
}
439

    
440
// Create a new file in the pages directory
441
function create_access_file($filename,$page_id,$level) {
442
	global $admin, $MESSAGE;
443
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
444
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
445
	} else {
446
		// First make sure parent folder exists
447
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
448
		$parents = '';
449
		foreach($parent_folders AS $parent_folder) {
450
			if($parent_folder != '/' AND $parent_folder != '') {
451
				$parents .= '/'.$parent_folder;
452
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
453
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
454
				}
455
			}	
456
		}
457
		// The depth of the page directory in the directory hierarchy
458
		// '/pages' is at depth 1
459
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
460
		// Work-out how many ../'s we need to get to the index page
461
		$index_location = '';
462
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
463
			$index_location .= '../';
464
		}
465
		$content = ''.
466
'<?php
467
$page_id = '.$page_id.';
468
require("'.$index_location.'config.php");
469
require(WB_PATH."/index.php");
470
?>';
471
		$handle = fopen($filename, 'w');
472
		fwrite($handle, $content);
473
		fclose($handle);
474
		// Chmod the file
475
		change_mode($filename);
476
	}
477
}
478

    
479
// Function for working out a file mime type (if the in-built PHP one is not enabled)
480
if(!function_exists('mime_content_type')) {
481
    function mime_content_type($filename) {
482

    
483
    $mime_types = array(
484
            'txt'	=> 'text/plain',
485
            'htm'	=> 'text/html',
486
            'html'	=> 'text/html',
487
            'php'	=> 'text/html',
488
            'css'	=> 'text/css',
489
            'js'	=> 'application/javascript',
490
            'json'	=> 'application/json',
491
            'xml'	=> 'application/xml',
492
            'swf'	=> 'application/x-shockwave-flash',
493
            'flv'	=> 'video/x-flv',
494

    
495
            // images
496
            'png'	=> 'image/png',
497
            'jpe'	=> 'image/jpeg',
498
            'jpeg'	=> 'image/jpeg',
499
            'jpg'	=> 'image/jpeg',
500
            'gif'	=> 'image/gif',
501
            'bmp'	=> 'image/bmp',
502
            'ico'	=> 'image/vnd.microsoft.icon',
503
            'tiff'	=> 'image/tiff',
504
            'tif'	=> 'image/tiff',
505
            'svg'	=> 'image/svg+xml',
506
            'svgz'	=> 'image/svg+xml',
507

    
508
            // archives
509
            'zip'	=> 'application/zip',
510
            'rar'	=> 'application/x-rar-compressed',
511
            'exe'	=> 'application/x-msdownload',
512
            'msi'	=> 'application/x-msdownload',
513
            'cab'	=> 'application/vnd.ms-cab-compressed',
514

    
515
            // audio/video
516
            'mp3'	=> 'audio/mpeg',
517
            'mp4'	=> 'audio/mpeg',
518
            'qt'	=> 'video/quicktime',
519
            'mov'	=> 'video/quicktime',
520

    
521
            // adobe
522
            'pdf'	=> 'application/pdf',
523
            'psd'	=> 'image/vnd.adobe.photoshop',
524
            'ai'	=> 'application/postscript',
525
            'eps'	=> 'application/postscript',
526
            'ps'	=> 'application/postscript',
527

    
528
            // ms office
529
            'doc'	=> 'application/msword',
530
            'rtf'	=> 'application/rtf',
531
            'xls'	=> 'application/vnd.ms-excel',
532
            'ppt'	=> 'application/vnd.ms-powerpoint',
533

    
534
            // open office
535
            'odt'	=> 'application/vnd.oasis.opendocument.text',
536
            'ods'	=> 'application/vnd.oasis.opendocument.spreadsheet',
537
        );
538

    
539
        $temp = explode('.',$filename);
540
        $ext = strtolower(array_pop($temp));
541

    
542
        if (array_key_exists($ext, $mime_types)) {
543
            return $mime_types[$ext];
544
        }
545
        elseif (function_exists('finfo_open')) {
546
            $finfo = finfo_open(FILEINFO_MIME);
547
            $mimetype = finfo_file($finfo, $filename);
548
            finfo_close($finfo);
549
            return $mimetype;
550
        }
551
        else {
552
            return 'application/octet-stream';
553
        }
554
    }
555
}
556

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

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

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

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

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

    
835
// Load language into DB
836
function load_language($file) {
837
	global $database;
838
	if (file_exists($file) && preg_match('#^([A-Z]{2}.php)#', basename($file))) {
839
		require($file);
840
		if(isset($language_name)) {
841
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
842
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
843
			// Check that it doesn't already exist
844
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
845
			if($result->numRows() == 0) {
846
				// Load into DB
847
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
848
				"(directory,name,type,version,platform,author,license) ".
849
				"VALUES ('$language_code','$language_name','language',".
850
				"'$language_version','$language_platform','$language_author','$language_license')";
851
	 		$database->query($query);
852
			}
853
		}
854
	}
855
}
856

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

    
891
// extracts the content of a string variable from a string (save alternative to including files)
892
if(!function_exists('get_variable_content')) {
893
	function get_variable_content($search, $data, $striptags=true, $convert_to_entities=true) {
894
		$match = '';
895
		// search for $variable followed by 0-n whitespace then by = then by 0-n whitespace
896
		// then either " or ' then 0-n characters then either " or ' followed by 0-n whitespace and ;
897
		// the variable name is returned in $match[1], the content in $match[3]
898
		if (preg_match('/(\$' .$search .')\s*=\s*("|\')(.*)\2\s*;/', $data, $match)) {
899
			if(strip_tags(trim($match[1])) == '$' .$search) {
900
				// variable name matches, return it's value
901
				$match[3] = ($striptags == true) ? strip_tags($match[3]) : $match[3];
902
				$match[3] = ($convert_to_entities == true) ? htmlentities($match[3]) : $match[3];
903
				return $match[3];
904
			}
905
		}
906
		return false;
907
	}
908
}
909

    
910
?>
(12-12/15)