Project

General

Profile

1
<?php
2
/*
3
*
4
*                       About WebsiteBaker
5
*
6
* Website Baker is a PHP-based Content Management System (CMS)
7
* designed with one goal in mind: to enable its users to produce websites
8
* with ease.
9
*
10
*                       LICENSE INFORMATION
11
*
12
* WebsiteBaker is free software; you can redistribute it and/or
13
* modify it under the terms of the GNU General Public License
14
* as published by the Free Software Foundation; either version 2
15
* of the License, or (at your option) any later version.
16
*
17
* WebsiteBaker is distributed in the hope that it will be useful,
18
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20
* See the GNU General Public License for more details.
21
*
22
* You should have received a copy of the GNU General Public License
23
* along with this program; if not, write to the Free Software
24
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
*
26
*                   WebsiteBaker Extra Information
27
*
28
*
29
*/
30
/**
31
 *
32
 * @category        frontend
33
 * @package         framework
34
 * @author          WebsiteBaker Project
35
 * @copyright       2004-2009, Ryan Djurovich
36
 * @copyright       2009-2010, Website Baker Org. e.V.
37
 * @link			http://www.websitebaker2.org/
38
 * @license         http://www.gnu.org/licenses/gpl.html
39
 * @platform        WebsiteBaker 2.8.x
40
 * @requirements    PHP 4.3.4 and higher
41
 * @version         $Id: functions.php 1270 2010-01-22 22:56:36Z Luisehahne $
42
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/functions.php $
43
 * @lastmodified    $Date: 2010-01-22 23:56:36 +0100 (Fri, 22 Jan 2010) $
44
 *
45
*/
46

    
47
// Stop this file from being accessed directly
48
if(!defined('WB_URL')) {
49
	header('Location: ../index.php');
50
	exit(0);
51
}
52

    
53
// Define that this file has been loaded
54
define('FUNCTIONS_FILE_LOADED', true);
55

    
56
// Function to remove a non-empty directory
57
function rm_full_dir($directory)
58
{
59
    // If suplied dirname is a file then unlink it
60
    if (is_file($directory)) {
61
        return unlink($directory);
62
    }
63

    
64
    // Empty the folder
65
	if (is_dir($directory))
66
    {
67
        $dir = dir($directory);
68
        while (false !== $entry = $dir->read())
69
        {
70
            // Skip pointers
71
            if ($entry == '.' || $entry == '..') {
72
                continue;
73
            }
74

    
75
            // Deep delete directories
76
            if (is_dir("$directory/$entry")) {
77
                rm_full_dir("$directory/$entry");
78
            }
79
            else
80
            {
81
                unlink("$directory/$entry");
82
            }
83
        }
84

    
85
        // Now delete the folder
86
        $dir->close();
87
        return rmdir($directory);
88
	}
89
}
90

    
91
// Function to open a directory and add to a dir list
92
function directory_list($directory)
93
{
94
	$list = array();
95

    
96
	if (is_dir($directory))
97
    {
98
    	// Open the directory then loop through its contents
99
    	$dir = dir($directory);
100
    	while (false !== $entry = $dir->read()) {
101
    		// Skip pointers
102
    		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
103
    			continue;
104
    		}
105
    		// Add dir and contents to list
106
    		if (is_dir("$directory/$entry")) {
107
    			$list = array_merge($list, directory_list("$directory/$entry"));
108
    			$list[] = "$directory/$entry";
109
    		}
110
    	}
111

    
112
        $dir->close();
113
    }
114
    // Now return the list
115
	return $list;
116
}
117

    
118
// Function to open a directory and add to a dir list
119
function chmod_directory_contents($directory, $file_mode)
120
{
121
	if (is_dir($directory))
122
    {
123
    	// Set the umask to 0
124
    	$umask = umask(0);
125

    
126
    	// Open the directory then loop through its contents
127
    	$dir = dir($directory);
128
    	while (false !== $entry = $dir->read()) {
129
    		// Skip pointers
130
    		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
131
    			continue;
132
    		}
133
    		// Chmod the sub-dirs contents
134
    		if(is_dir("$directory/$entry")) {
135
    			chmod_directory_contents("$directory/$entry", $file_mode);
136
    		}
137
    		change_mode($directory.'/'.$entry);
138
    	}
139
        $dir->close();
140
    	// Restore the umask
141
    	umask($umask);
142
    }
143
}
144

    
145
// Function to open a directory and add to a file list
146
function file_list($directory, $skip = array()) {
147
	
148
	$list = array();
149
	$skip_file = false;
150
	
151
	if (is_dir($directory))
152
    {
153
    	// Open the directory then loop through its contents
154
    	$dir = dir($directory);
155
    }
156
	while (false !== $entry = $dir->read())
157
    {
158
		// Skip pointers
159
		if($entry == '.' || $entry == '..')
160
        {
161
			$skip_file = true;
162
		}
163
		// Check if we to skip anything else
164
		if($skip != array()) {
165
			foreach($skip AS $skip_name)
166
            {
167
				if($entry == $skip_name)
168
                {
169
					$skip_file = true;
170
				}
171
			}
172
		}
173
		// Add dir and contents to list
174
		if($skip_file != true AND is_file("$directory/$entry"))
175
        {
176
			$list[] = "$directory/$entry";
177
		}
178
		
179
		// Reset the skip file var
180
		$skip_file = false;
181
	}
182
    $dir->close();
183
	// Now delete the folder
184
	return $list;
185
}
186

    
187
// Function to get a list of home folders not to show
188
function get_home_folders() {
189
	global $database, $admin;
190
	$home_folders = array();
191
	// Only return home folders is this feature is enabled
192
	// and user is not admin
193
//	if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
194
	if(HOME_FOLDERS AND (!in_array('1',explode(",", $_SESSION['GROUPS_ID'])))) {
195

    
196
		$query_home_folders = $database->query("SELECT home_folder FROM ".TABLE_PREFIX."users WHERE home_folder != '".$admin->get_home_folder()."'");
197
		if($query_home_folders->numRows() > 0) {
198
			while($folder = $query_home_folders->fetchRow()) {
199
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
200
			}
201
		}
202
		function remove_home_subs($directory = '/', $home_folders) {
203
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) {
204
				// Loop through the dirs to check the home folders sub-dirs are not shown
205
			   while(false !== ($file = readdir($handle))) {
206
					if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
207
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
208
							if($directory != '/') { $file = $directory.'/'.$file; } else { $file = '/'.$file; }
209
							foreach($home_folders AS $hf) {
210
								$hf_length = strlen($hf);
211
								if($hf_length > 0) {
212
									if(substr($file, 0, $hf_length+1) == $hf) {
213
										$home_folders[$file] = $file;
214
									}
215
								}
216
							}
217
							$home_folders = remove_home_subs($file, $home_folders);
218
						}
219
					}
220
				}
221
			}
222
			return $home_folders;
223
		}
224
		$home_folders = remove_home_subs('/', $home_folders);
225
	}
226
	return $home_folders;
227
}
228

    
229
// Function to create directories
230
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE)
231
{
232
	if(!is_dir($dir_name))
233
    {
234
		$umask = umask(0);
235
		mkdir($dir_name, $dir_mode);
236
		umask($umask);
237
		return true;
238
	} else {
239
		return false;	
240
	}
241
}
242

    
243
// Function to chmod files and directories
244
function change_mode($name) {
245
	if(OPERATING_SYSTEM != 'windows')
246
    {
247
		// Only chmod if os is not windows
248
		if(is_dir($name))
249
        {
250
			$mode = OCTAL_DIR_MODE;
251
		}
252
        else
253
        {
254
			$mode = OCTAL_FILE_MODE;
255
		}
256

    
257
		if(file_exists($name))
258
        {
259
			$umask = umask(0);
260
			chmod($name, $mode);
261
			umask($umask);
262
			return true;
263
		}
264
        else
265
        {
266
			return false;	
267
		}
268
	}
269
    else
270
    {
271
		return true;
272
	}
273
}
274

    
275
// Function to figure out if a parent exists
276
function is_parent($page_id) {
277
	global $database;
278
	// Get parent
279
	$query = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
280
	$fetch = $query->fetchRow();
281
	// If parent isnt 0 return its ID
282
	if($fetch['parent'] == '0') {
283
		return false;
284
	} else {
285
		return $fetch['parent'];
286
	}
287
}
288

    
289
// Function to work out level
290
function level_count($page_id) {
291
	global $database;
292
	// Get page parent
293
	$query_page = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
294
	$fetch_page = $query_page->fetchRow();
295
	$parent = $fetch_page['parent'];
296
	if($parent > 0) {
297
		// Get the level of the parent
298
		$query_parent = $database->query("SELECT level FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent' LIMIT 1");
299
		$fetch_parent = $query_parent->fetchRow();
300
		$level = $fetch_parent['level'];
301
		return $level+1;
302
	} else {
303
		return 0;
304
	}
305
}
306

    
307
// Function to work out root parent
308
function root_parent($page_id) {
309
	global $database;
310
	// Get page details
311
	$query_page = $database->query("SELECT parent,level FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
312
	$fetch_page = $query_page->fetchRow();
313
	$parent = $fetch_page['parent'];
314
	$level = $fetch_page['level'];	
315
	if($level == 1) {
316
		return $parent;
317
	} elseif($parent == 0) {
318
		return $page_id;
319
	} else {
320
		// Figure out what the root parents id is
321
		$parent_ids = array_reverse(get_parent_ids($page_id));
322
		return $parent_ids[0];
323
	}
324
}
325

    
326
// Function to get page title
327
function get_page_title($id) {
328
	global $database;
329
	// Get title
330
	$query = $database->query("SELECT page_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
331
	$fetch = $query->fetchRow();
332
	// Return title
333
	return $fetch['page_title'];
334
}
335

    
336
// Function to get a pages menu title
337
function get_menu_title($id) {
338
	// Connect to the database
339
	$database = new database();
340
	// Get title
341
	$query = $database->query("SELECT menu_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
342
	$fetch = $query->fetchRow();
343
	// Return title
344
	return $fetch['menu_title'];
345
}
346

    
347
// Function to get all parent page titles
348
function get_parent_titles($parent_id) {
349
	$titles[] = get_menu_title($parent_id);
350
	if(is_parent($parent_id) != false) {
351
		$parent_titles = get_parent_titles(is_parent($parent_id));
352
		$titles = array_merge($titles, $parent_titles);
353
	}
354
	return $titles;
355
}
356

    
357
// Function to get all parent page id's
358
function get_parent_ids($parent_id) {
359
	$ids[] = $parent_id;
360
	if(is_parent($parent_id) != false) {
361
		$parent_ids = get_parent_ids(is_parent($parent_id));
362
		$ids = array_merge($ids, $parent_ids);
363
	}
364
	return $ids;
365
}
366

    
367
// Function to genereate page trail
368
function get_page_trail($page_id) {
369
	return implode(',', array_reverse(get_parent_ids($page_id)));
370
}
371

    
372
// Function to get all sub pages id's
373
function get_subs($parent, $subs) {
374
	// Connect to the database
375
	$database = new database();
376
	// Get id's
377
	$query = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'");
378
	if($query->numRows() > 0) {
379
		while($fetch = $query->fetchRow()) {
380
			$subs[] = $fetch['page_id'];
381
			// Get subs of this sub
382
			$subs = get_subs($fetch['page_id'], $subs);
383
		}
384
	}
385
	// Return subs array
386
	return $subs;
387
}
388

    
389
// Function as replacement for php's htmlspecialchars()
390
// Will not mangle HTML-entities
391
function my_htmlspecialchars($string) {
392
	$string = preg_replace('/&(?=[#a-z0-9]+;)/i', '__amp;_', $string);
393
	$string = strtr($string, array('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;', '\''=>'&#39;'));
394
	$string = preg_replace('/__amp;_(?=[#a-z0-9]+;)/i', '&', $string);
395
	return($string);
396
}
397

    
398
// Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
399
// Will replace all numeric and named entities except &gt; &lt; &apos; &quot; &#039; &nbsp;
400
// In case of error the returned string is unchanged, and a message is emitted.
401
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET) {
402
	require_once(WB_PATH.'/framework/functions-utf8.php');
403
	return entities_to_umlauts2($string, $charset_out);
404
}
405

    
406
// Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
407
// In case of error the returned string is unchanged, and a message is emitted.
408
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET) {
409
	require_once(WB_PATH.'/framework/functions-utf8.php');
410
	return umlauts_to_entities2($string, $charset_in);
411
}
412

    
413
// Function to convert a page title to a page filename
414
function page_filename($string) {
415
	require_once(WB_PATH.'/framework/functions-utf8.php');
416
	$string = entities_to_7bit($string);
417
	// Now remove all bad characters
418
	$bad = array(
419
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
420
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
421
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
422
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
423
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
424
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
425
	);
426
	$string = str_replace($bad, '', $string);
427
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
428
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
429
	// Now replace spaces with page spcacer
430
	$string = trim($string);
431
	$string = preg_replace('/(\s)+/', PAGE_SPACER, $string);
432
	// Now convert to lower-case
433
	$string = strtolower($string);
434
	// If there are any weird language characters, this will protect us against possible problems they could cause
435
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
436
	// Finally, return the cleaned string
437
	return $string;
438
}
439

    
440
// Function to convert a desired media filename to a clean filename
441
function media_filename($string) {
442
	require_once(WB_PATH.'/framework/functions-utf8.php');
443
	$string = entities_to_7bit($string);
444
	// Now remove all bad characters
445
	$bad = array(
446
	'\'', // '
447
	'"', // "
448
	'`', // `
449
	'!', // !
450
	'@', // @
451
	'#', // #
452
	'$', // $
453
	'%', // %
454
	'^', // ^
455
	'&', // &
456
	'*', // *
457
	'=', // =
458
	'+', // +
459
	'|', // |
460
	'/', // /
461
	'\\', // \
462
	';', // ;
463
	':', // :
464
	',', // ,
465
	'?' // ?
466
	);
467
	$string = str_replace($bad, '', $string);
468
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
469
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
470
	// Clean any page spacers at the end of string
471
	$string = trim($string);
472
	// Finally, return the cleaned string
473
	return $string;
474
}
475

    
476
// Function to work out a page link
477
if(!function_exists('page_link')) {
478
	function page_link($link) {
479
		global $admin;
480
		return $admin->page_link($link);
481
	}
482
}
483

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

    
523
// Function for working out a file mime type (if the in-built PHP one is not enabled)
524
if(!function_exists('mime_content_type')) {
525
    function mime_content_type($filename) {
526

    
527
    $mime_types = array(
528
            'txt'	=> 'text/plain',
529
            'htm'	=> 'text/html',
530
            'html'	=> 'text/html',
531
            'php'	=> 'text/html',
532
            'css'	=> 'text/css',
533
            'js'	=> 'application/javascript',
534
            'json'	=> 'application/json',
535
            'xml'	=> 'application/xml',
536
            'swf'	=> 'application/x-shockwave-flash',
537
            'flv'	=> 'video/x-flv',
538

    
539
            // images
540
            'png'	=> 'image/png',
541
            'jpe'	=> 'image/jpeg',
542
            'jpeg'	=> 'image/jpeg',
543
            'jpg'	=> 'image/jpeg',
544
            'gif'	=> 'image/gif',
545
            'bmp'	=> 'image/bmp',
546
            'ico'	=> 'image/vnd.microsoft.icon',
547
            'tiff'	=> 'image/tiff',
548
            'tif'	=> 'image/tiff',
549
            'svg'	=> 'image/svg+xml',
550
            'svgz'	=> 'image/svg+xml',
551

    
552
            // archives
553
            'zip'	=> 'application/zip',
554
            'rar'	=> 'application/x-rar-compressed',
555
            'exe'	=> 'application/x-msdownload',
556
            'msi'	=> 'application/x-msdownload',
557
            'cab'	=> 'application/vnd.ms-cab-compressed',
558

    
559
            // audio/video
560
            'mp3'	=> 'audio/mpeg',
561
            'mp4'	=> 'audio/mpeg',
562
            'qt'	=> 'video/quicktime',
563
            'mov'	=> 'video/quicktime',
564

    
565
            // adobe
566
            'pdf'	=> 'application/pdf',
567
            'psd'	=> 'image/vnd.adobe.photoshop',
568
            'ai'	=> 'application/postscript',
569
            'eps'	=> 'application/postscript',
570
            'ps'	=> 'application/postscript',
571

    
572
            // ms office
573
            'doc'	=> 'application/msword',
574
            'rtf'	=> 'application/rtf',
575
            'xls'	=> 'application/vnd.ms-excel',
576
            'ppt'	=> 'application/vnd.ms-powerpoint',
577

    
578
            // open office
579
            'odt'	=> 'application/vnd.oasis.opendocument.text',
580
            'ods'	=> 'application/vnd.oasis.opendocument.spreadsheet',
581
        );
582

    
583
        $temp = explode('.',$filename);
584
        $ext = strtolower(array_pop($temp));
585

    
586
        if (array_key_exists($ext, $mime_types)) {
587
            return $mime_types[$ext];
588
        }
589
        elseif (function_exists('finfo_open')) {
590
            $finfo = finfo_open(FILEINFO_MIME);
591
            $mimetype = finfo_file($finfo, $filename);
592
            finfo_close($finfo);
593
            return $mimetype;
594
        }
595
        else {
596
            return 'application/octet-stream';
597
        }
598
    }
599
}
600

    
601
// Generate a thumbnail from an image
602
function make_thumb($source, $destination, $size) {
603
	// Check if GD is installed
604
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
605
		// First figure out the size of the thumbnail
606
		list($original_x, $original_y) = getimagesize($source);
607
		if ($original_x > $original_y) {
608
			$thumb_w = $size;
609
			$thumb_h = $original_y*($size/$original_x);
610
		}
611
		if ($original_x < $original_y) {
612
			$thumb_w = $original_x*($size/$original_y);
613
			$thumb_h = $size;
614
		}
615
		if ($original_x == $original_y) {
616
			$thumb_w = $size;
617
			$thumb_h = $size;	
618
		}
619
		// Now make the thumbnail
620
		$source = imageCreateFromJpeg($source);
621
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
622
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
623
		imagejpeg($dst_img, $destination);
624
		// Clear memory
625
		imagedestroy($dst_img);
626
	   imagedestroy($source);
627
	   // Return true
628
	   return true;
629
   } else {
630
   	return false;
631
   }
632
}
633

    
634
// Function to work-out a single part of an octal permission value
635
function extract_permission($octal_value, $who, $action) {
636
	// Make sure the octal value is 4 chars long
637
	if(strlen($octal_value) == 0) {
638
		$octal_value = '0000';
639
	} elseif(strlen($octal_value) == 1) {
640
		$octal_value = '000'.$octal_value;
641
	} elseif(strlen($octal_value) == 2) {
642
		$octal_value = '00'.$octal_value;
643
	} elseif(strlen($octal_value) == 3) {
644
		$octal_value = '0'.$octal_value;
645
	} elseif(strlen($octal_value) == 4) {
646
		$octal_value = ''.$octal_value;
647
	} else {
648
		$octal_value = '0000';
649
	}
650
	// Work-out what position of the octal value to look at
651
	switch($who) {
652
	case 'u':
653
		$position = '1';
654
		break;
655
	case 'user':
656
		$position = '1';
657
		break;
658
	case 'g':
659
		$position = '2';
660
		break;
661
	case 'group':
662
		$position = '2';
663
		break;
664
	case 'o':
665
		$position = '3';
666
		break;
667
	case 'others':
668
		$position = '3';
669
		break;
670
	}
671
	// Work-out how long the octal value is and ajust acording
672
	if(strlen($octal_value) == 4) {
673
		$position = $position+1;
674
	} elseif(strlen($octal_value) != 3) {
675
		exit('Error');
676
	}
677
	// Now work-out what action the script is trying to look-up
678
	switch($action) {
679
	case 'r':
680
		$action = 'r';
681
		break;
682
	case 'read':
683
		$action = 'r';
684
		break;
685
	case 'w':
686
		$action = 'w';
687
		break;
688
	case 'write':
689
		$action = 'w';
690
		break;
691
	case 'e':
692
		$action = 'e';
693
		break;
694
	case 'execute':
695
		$action = 'e';
696
		break;
697
	}
698
	// Get the value for "who"
699
	$value = substr($octal_value, $position-1, 1);
700
	// Now work-out the details of the value
701
	switch($value) {
702
	case '0':
703
		$r = false;
704
		$w = false;
705
		$e = false;
706
		break;
707
	case '1':
708
		$r = false;
709
		$w = false;
710
		$e = true;
711
		break;
712
	case '2':
713
		$r = false;
714
		$w = true;
715
		$e = false;
716
		break;
717
	case '3':
718
		$r = false;
719
		$w = true;
720
		$e = true;
721
		break;
722
	case '4':
723
		$r = true;
724
		$w = false;
725
		$e = false;
726
		break;
727
	case '5':
728
		$r = true;
729
		$w = false;
730
		$e = true;
731
		break;
732
	case '6':
733
		$r = true;
734
		$w = true;
735
		$e = false;
736
		break;
737
	case '7':
738
		$r = true;
739
		$w = true;
740
		$e = true;
741
		break;
742
	default:
743
		$r = false;
744
		$w = false;
745
		$e = false;
746
	}
747
	// And finally, return either true or false
748
	return $$action;
749
}
750

    
751
// Function to delete a page
752
function delete_page($page_id) {
753
	
754
	global $admin, $database, $MESSAGE;
755
	
756
	// Find out more about the page
757
	$database = new database();
758
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
759
	$results = $database->query($query);
760
	if($database->is_error()) {
761
		$admin->print_error($database->get_error());
762
	}
763
	if($results->numRows() == 0) {
764
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
765
	}
766
	$results_array = $results->fetchRow();
767
	$parent = $results_array['parent'];
768
	$level = $results_array['level'];
769
	$link = $results_array['link'];
770
	$page_title = ($results_array['page_title']);
771
	$menu_title = ($results_array['menu_title']);
772
	
773
	// Get the sections that belong to the page
774
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
775
	if($query_sections->numRows() > 0) {
776
		while($section = $query_sections->fetchRow()) {
777
			// Set section id
778
			$section_id = $section['section_id'];
779
			// Include the modules delete file if it exists
780
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
781
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
782
			}
783
		}
784
	}
785
	
786
	// Update the pages table
787
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
788
	$database->query($query);
789
	if($database->is_error()) {
790
		$admin->print_error($database->get_error());
791
	}
792
	
793
	// Update the sections table
794
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
795
	$database->query($query);
796
	if($database->is_error()) {
797
		$admin->print_error($database->get_error());
798
	}
799
	
800
	// Include the ordering class or clean-up ordering
801
	require_once(WB_PATH.'/framework/class.order.php');
802
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
803
	$order->clean($parent);
804
	
805
	// Unlink the page access file and directory
806
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
807
	$filename = $directory.PAGE_EXTENSION;
808
	$directory .= '/';
809
	if(file_exists($filename)) {
810
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
811
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
812
		} else {
813
			unlink($filename);
814
			if(file_exists($directory) && rtrim($directory,'/')!=WB_PATH.PAGES_DIRECTORY && substr($link, 0, 1) != '.') {
815
				rm_full_dir($directory);
816
			}
817
		}
818
	}
819
	
820
}
821

    
822
// Load module into DB
823
function load_module($directory, $install = false) {
824
	global $database,$admin,$MESSAGE;
825
	if(is_dir($directory) AND file_exists($directory.'/info.php'))
826
	{
827
		require($directory.'/info.php');
828
		if(isset($module_name))
829
	{
830
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
831
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
832
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
833
			$module_function = strtolower($module_function);
834
			// Check that it doesn't already exist
835
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE type = 'module' AND directory = '".$module_directory."' LIMIT 0,1");
836
			if($result->numRows() == 0)
837
			{
838
				// Load into DB
839
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
840
				"(directory,name,description,type,function,version,platform,author,license) ".
841
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
842
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
843
				$database->query($query);
844
				// Run installation script
845
				if($install == true)
846
				{
847
					if(file_exists($directory.'/install.php')) {
848
						require($directory.'/install.php');
849
					}
850
				}
851
			}
852
		}
853
	}
854
}
855

    
856
// Load template into DB
857
function load_template($directory) {
858
	global $database;
859
	if(is_dir($directory) AND file_exists($directory.'/info.php')) {
860
		require($directory.'/info.php');
861
		if(isset($template_name)) {
862
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
863
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
864
			if(!isset($template_function)) { $template_function = 'template'; }
865
			// Check that it doesn't already exist
866
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE type = 'template' AND directory = '".$template_directory."' LIMIT 0,1");
867
			if($result->numRows() == 0) {
868
				// Load into DB
869
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
870
				"(directory,name,description,type,function,version,platform,author,license) ".
871
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
872
				"'$template_function','$template_version','$template_platform','$template_author','$template_license')";
873
				$database->query($query);
874
			}
875
		}
876
	}
877
}
878

    
879
// Load language into DB
880
function load_language($file) {
881
	global $database;
882
	if (file_exists($file) && preg_match('#^([A-Z]{2}.php)#', basename($file))) {
883
		require($file);
884
		if(isset($language_name)) {
885
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
886
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
887
			// Check that it doesn't already exist
888
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE type = 'language' AND directory = '".$language_code."' LIMIT 0,1");
889
			if($result->numRows() == 0) {
890
				// Load into DB
891
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
892
				"(directory,name,type,version,platform,author,license) ".
893
				"VALUES ('$language_code','$language_name','language',".
894
				"'$language_version','$language_platform','$language_author','$language_license')";
895
	 		$database->query($query);
896
			}
897
		}
898
	}
899
}
900

    
901
// Upgrade module info in DB, optionally start upgrade script
902
function upgrade_module($directory, $upgrade = false) {
903
	global $database, $admin, $MESSAGE;
904
	$directory = WB_PATH . "/modules/$directory";
905
	if(file_exists($directory.'/info.php')) {
906
		require($directory.'/info.php');
907
		if(isset($module_name)) {
908
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
909
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
910
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
911
			$module_function = strtolower($module_function);
912
			// Check that it does already exist
913
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
914
			if($result->numRows() > 0) {
915
				// Update in DB
916
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
917
					"version = '$module_version', " .
918
					"description = '" . addslashes($module_description) . "', " .
919
					"platform = '$module_platform', " .
920
					"author = '$module_author', " .
921
					"license = '$module_license'" .
922
					"WHERE directory = '$module_directory'";
923
				$database->query($query);
924
				// Run upgrade script
925
				if($upgrade == true) {
926
					if(file_exists($directory.'/upgrade.php')) {
927
						require($directory.'/upgrade.php');
928
					}
929
				}
930
			}
931
		}
932
	}
933
}
934

    
935
// extracts the content of a string variable from a string (save alternative to including files)
936
if(!function_exists('get_variable_content')) {
937
	function get_variable_content($search, $data, $striptags=true, $convert_to_entities=true) {
938
		$match = '';
939
		// search for $variable followed by 0-n whitespace then by = then by 0-n whitespace
940
		// then either " or ' then 0-n characters then either " or ' followed by 0-n whitespace and ;
941
		// the variable name is returned in $match[1], the content in $match[3]
942
		if (preg_match('/(\$' .$search .')\s*=\s*("|\')(.*)\2\s*;/', $data, $match)) {
943
			if(strip_tags(trim($match[1])) == '$' .$search) {
944
				// variable name matches, return it's value
945
				$match[3] = ($striptags == true) ? strip_tags($match[3]) : $match[3];
946
				$match[3] = ($convert_to_entities == true) ? htmlentities($match[3]) : $match[3];
947
				return $match[3];
948
			}
949
		}
950
		return false;
951
	}
952
}
953

    
954
?>
(12-12/15)