Project

General

Profile

1
<?php
2
/****************************************************************************
3
* SVN Version information:
4
*
5
* $Id: functions.php 1237 2010-01-11 13:13:28Z Luisehahne $
6
*
7
*
8
*
9
*****************************************************************************
10
*                          WebsiteBaker
11
*
12
* WebsiteBaker Project <http://www.websitebaker2.org/>
13
* Copyright (C) 2009, Website Baker Org. e.V.
14
*         http://start.websitebaker2.org/impressum-datenschutz.php
15
* Copyright (C) 2004-2009, Ryan Djurovich
16
*
17
*                        About WebsiteBaker
18
*
19
* Website Baker is a PHP-based Content Management System (CMS)
20
* designed with one goal in mind: to enable its users to produce websites
21
* with ease.
22
*
23
*****************************************************************************
24
*
25
*****************************************************************************
26
*                        LICENSE INFORMATION
27
*
28
* WebsiteBaker is free software; you can redistribute it and/or
29
* modify it under the terms of the GNU General Public License
30
* as published by the Free Software Foundation; either version 2
31
* of the License, or (at your option) any later version.
32
*
33
* WebsiteBaker is distributed in the hope that it will be useful,
34
* but WITHOUT ANY WARRANTY; without even the implied warranty of
35
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36
* See the GNU General Public License for more details.
37
*
38
* You should have received a copy of the GNU General Public License
39
* along with this program; if not, write to the Free Software
40
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
41
****************************************************************************
42
*
43
*                   WebsiteBaker Extra Information
44
*
45
* Website Baker functions file
46
* This file contains general functions used in Website Baker
47
*
48
*
49
*
50
*
51
*****************************************************************************/
52
/**
53
 *
54
 * @category     frontend
55
 * @package      framework
56
 * @author       Ryan Djurovich
57
 * @copyright    2004-2009, Ryan Djurovich
58
 * @copyright    2009-2010, Website Baker Org. e.V.
59
 * @version      $Id: functions.php 1237 2010-01-11 13:13:28Z Luisehahne $
60
 * @platform     WebsiteBaker 2.8.x
61
 * @requirements >= PHP 4.3.4
62
 * @license      http://www.gnu.org/licenses/gpl.html
63
 *
64
 */
65

    
66
// Stop this file from being accessed directly
67
if(!defined('WB_URL')) {
68
	header('Location: ../index.php');
69
	exit(0);
70
}
71

    
72
// Define that this file has been loaded
73
define('FUNCTIONS_FILE_LOADED', true);
74

    
75
// Function to remove a non-empty directory
76
function rm_full_dir($directory)
77
{
78
    // If suplied dirname is a file then unlink it
79
    if (is_file($directory)) {
80
        return unlink($directory);
81
    }
82

    
83
    // Empty the folder
84
	if (is_dir($directory))
85
    {
86
        $dir = dir($directory);
87
        while (false !== $entry = $dir->read())
88
        {
89
            // Skip pointers
90
            if ($entry == '.' || $entry == '..') {
91
                continue;
92
            }
93

    
94
            // Deep delete directories
95
            if (is_dir("$directory/$entry")) {
96
                rm_full_dir("$directory/$entry");
97
            }
98
            else
99
            {
100
                unlink("$directory/$entry");
101
            }
102
        }
103

    
104
        // Now delete the folder
105
        $dir->close();
106
        return rmdir($directory);
107
	}
108
}
109

    
110
// Function to open a directory and add to a dir list
111
function directory_list($directory)
112
{
113
	$list = array();
114

    
115
	if (is_dir($directory))
116
    {
117
    	// Open the directory then loop through its contents
118
    	$dir = dir($directory);
119
    	while (false !== $entry = $dir->read()) {
120
    		// Skip pointers
121
    		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
122
    			continue;
123
    		}
124
    		// Add dir and contents to list
125
    		if (is_dir("$directory/$entry")) {
126
    			$list = array_merge($list, directory_list("$directory/$entry"));
127
    			$list[] = "$directory/$entry";
128
    		}
129
    	}
130

    
131
        $dir->close();
132
    }
133
    // Now return the list
134
	return $list;
135
}
136

    
137
// Function to open a directory and add to a dir list
138
function chmod_directory_contents($directory, $file_mode)
139
{
140
	if (is_dir($directory))
141
    {
142
    	// Set the umask to 0
143
    	$umask = umask(0);
144

    
145
    	// Open the directory then loop through its contents
146
    	$dir = dir($directory);
147
    	while (false !== $entry = $dir->read()) {
148
    		// Skip pointers
149
    		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
150
    			continue;
151
    		}
152
    		// Chmod the sub-dirs contents
153
    		if(is_dir("$directory/$entry")) {
154
    			chmod_directory_contents("$directory/$entry", $file_mode);
155
    		}
156
    		change_mode($directory.'/'.$entry);
157
    	}
158
        $dir->close();
159
    	// Restore the umask
160
    	umask($umask);
161
    }
162
}
163

    
164
// Function to open a directory and add to a file list
165
function file_list($directory, $skip = array()) {
166
	
167
	$list = array();
168
	$skip_file = false;
169
	
170
	if (is_dir($directory))
171
    {
172
    	// Open the directory then loop through its contents
173
    	$dir = dir($directory);
174
    }
175
	while (false !== $entry = $dir->read())
176
    {
177
		// Skip pointers
178
		if($entry == '.' || $entry == '..')
179
        {
180
			$skip_file = true;
181
		}
182
		// Check if we to skip anything else
183
		if($skip != array()) {
184
			foreach($skip AS $skip_name)
185
            {
186
				if($entry == $skip_name)
187
                {
188
					$skip_file = true;
189
				}
190
			}
191
		}
192
		// Add dir and contents to list
193
		if($skip_file != true AND is_file("$directory/$entry"))
194
        {
195
			$list[] = "$directory/$entry";
196
		}
197
		
198
		// Reset the skip file var
199
		$skip_file = false;
200
	}
201
    $dir->close();
202
	// Now delete the folder
203
	return $list;
204
}
205

    
206
// Function to get a list of home folders not to show
207
function get_home_folders() {
208
	global $database, $admin;
209
	$home_folders = array();
210
	// Only return home folders is this feature is enabled
211
	// and user is not admin
212
//	if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
213
	if(HOME_FOLDERS AND (!in_array('1',explode(",", $_SESSION['GROUPS_ID'])))) {
214

    
215
		$query_home_folders = $database->query("SELECT home_folder FROM ".TABLE_PREFIX."users WHERE home_folder != '".$admin->get_home_folder()."'");
216
		if($query_home_folders->numRows() > 0) {
217
			while($folder = $query_home_folders->fetchRow()) {
218
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
219
			}
220
		}
221
		function remove_home_subs($directory = '/', $home_folders) {
222
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) {
223
				// Loop through the dirs to check the home folders sub-dirs are not shown
224
			   while(false !== ($file = readdir($handle))) {
225
					if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
226
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
227
							if($directory != '/') { $file = $directory.'/'.$file; } else { $file = '/'.$file; }
228
							foreach($home_folders AS $hf) {
229
								$hf_length = strlen($hf);
230
								if($hf_length > 0) {
231
									if(substr($file, 0, $hf_length+1) == $hf) {
232
										$home_folders[$file] = $file;
233
									}
234
								}
235
							}
236
							$home_folders = remove_home_subs($file, $home_folders);
237
						}
238
					}
239
				}
240
			}
241
			return $home_folders;
242
		}
243
		$home_folders = remove_home_subs('/', $home_folders);
244
	}
245
	return $home_folders;
246
}
247

    
248
// Function to create directories
249
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE)
250
{
251
	if(!is_dir($dir_name))
252
    {
253
		$umask = umask(0);
254
		mkdir($dir_name, $dir_mode);
255
		umask($umask);
256
		return true;
257
	} else {
258
		return false;	
259
	}
260
}
261

    
262
// Function to chmod files and directories
263
function change_mode($name) {
264
	if(OPERATING_SYSTEM != 'windows')
265
    {
266
		// Only chmod if os is not windows
267
		if(is_dir($name))
268
        {
269
			$mode = OCTAL_DIR_MODE;
270
		}
271
        else
272
        {
273
			$mode = OCTAL_FILE_MODE;
274
		}
275

    
276
		if(file_exists($name))
277
        {
278
			$umask = umask(0);
279
			chmod($name, $mode);
280
			umask($umask);
281
			return true;
282
		}
283
        else
284
        {
285
			return false;	
286
		}
287
	}
288
    else
289
    {
290
		return true;
291
	}
292
}
293

    
294
// Function to figure out if a parent exists
295
function is_parent($page_id) {
296
	global $database;
297
	// Get parent
298
	$query = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
299
	$fetch = $query->fetchRow();
300
	// If parent isnt 0 return its ID
301
	if($fetch['parent'] == '0') {
302
		return false;
303
	} else {
304
		return $fetch['parent'];
305
	}
306
}
307

    
308
// Function to work out level
309
function level_count($page_id) {
310
	global $database;
311
	// Get page parent
312
	$query_page = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
313
	$fetch_page = $query_page->fetchRow();
314
	$parent = $fetch_page['parent'];
315
	if($parent > 0) {
316
		// Get the level of the parent
317
		$query_parent = $database->query("SELECT level FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent' LIMIT 1");
318
		$fetch_parent = $query_parent->fetchRow();
319
		$level = $fetch_parent['level'];
320
		return $level+1;
321
	} else {
322
		return 0;
323
	}
324
}
325

    
326
// Function to work out root parent
327
function root_parent($page_id) {
328
	global $database;
329
	// Get page details
330
	$query_page = $database->query("SELECT parent,level FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
331
	$fetch_page = $query_page->fetchRow();
332
	$parent = $fetch_page['parent'];
333
	$level = $fetch_page['level'];	
334
	if($level == 1) {
335
		return $parent;
336
	} elseif($parent == 0) {
337
		return $page_id;
338
	} else {
339
		// Figure out what the root parents id is
340
		$parent_ids = array_reverse(get_parent_ids($page_id));
341
		return $parent_ids[0];
342
	}
343
}
344

    
345
// Function to get page title
346
function get_page_title($id) {
347
	global $database;
348
	// Get title
349
	$query = $database->query("SELECT page_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
350
	$fetch = $query->fetchRow();
351
	// Return title
352
	return $fetch['page_title'];
353
}
354

    
355
// Function to get a pages menu title
356
function get_menu_title($id) {
357
	// Connect to the database
358
	$database = new database();
359
	// Get title
360
	$query = $database->query("SELECT menu_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
361
	$fetch = $query->fetchRow();
362
	// Return title
363
	return $fetch['menu_title'];
364
}
365

    
366
// Function to get all parent page titles
367
function get_parent_titles($parent_id) {
368
	$titles[] = get_menu_title($parent_id);
369
	if(is_parent($parent_id) != false) {
370
		$parent_titles = get_parent_titles(is_parent($parent_id));
371
		$titles = array_merge($titles, $parent_titles);
372
	}
373
	return $titles;
374
}
375

    
376
// Function to get all parent page id's
377
function get_parent_ids($parent_id) {
378
	$ids[] = $parent_id;
379
	if(is_parent($parent_id) != false) {
380
		$parent_ids = get_parent_ids(is_parent($parent_id));
381
		$ids = array_merge($ids, $parent_ids);
382
	}
383
	return $ids;
384
}
385

    
386
// Function to genereate page trail
387
function get_page_trail($page_id) {
388
	return implode(',', array_reverse(get_parent_ids($page_id)));
389
}
390

    
391
// Function to get all sub pages id's
392
function get_subs($parent, $subs) {
393
	// Connect to the database
394
	$database = new database();
395
	// Get id's
396
	$query = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'");
397
	if($query->numRows() > 0) {
398
		while($fetch = $query->fetchRow()) {
399
			$subs[] = $fetch['page_id'];
400
			// Get subs of this sub
401
			$subs = get_subs($fetch['page_id'], $subs);
402
		}
403
	}
404
	// Return subs array
405
	return $subs;
406
}
407

    
408
// Function as replacement for php's htmlspecialchars()
409
// Will not mangle HTML-entities
410
function my_htmlspecialchars($string) {
411
	$string = preg_replace('/&(?=[#a-z0-9]+;)/i', '__amp;_', $string);
412
	$string = strtr($string, array('<'=>'&lt;', '>'=>'&gt;', '&'=>'&amp;', '"'=>'&quot;', '\''=>'&#39;'));
413
	$string = preg_replace('/__amp;_(?=[#a-z0-9]+;)/i', '&', $string);
414
	return($string);
415
}
416

    
417
// Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
418
// Will replace all numeric and named entities except &gt; &lt; &apos; &quot; &#039; &nbsp;
419
// In case of error the returned string is unchanged, and a message is emitted.
420
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET) {
421
	require_once(WB_PATH.'/framework/functions-utf8.php');
422
	return entities_to_umlauts2($string, $charset_out);
423
}
424

    
425
// Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
426
// In case of error the returned string is unchanged, and a message is emitted.
427
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET) {
428
	require_once(WB_PATH.'/framework/functions-utf8.php');
429
	return umlauts_to_entities2($string, $charset_in);
430
}
431

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

    
459
// Function to convert a desired media filename to a clean filename
460
function media_filename($string) {
461
	require_once(WB_PATH.'/framework/functions-utf8.php');
462
	$string = entities_to_7bit($string);
463
	// Now remove all bad characters
464
	$bad = array(
465
	'\'', // '
466
	'"', // "
467
	'`', // `
468
	'!', // !
469
	'@', // @
470
	'#', // #
471
	'$', // $
472
	'%', // %
473
	'^', // ^
474
	'&', // &
475
	'*', // *
476
	'=', // =
477
	'+', // +
478
	'|', // |
479
	'/', // /
480
	'\\', // \
481
	';', // ;
482
	':', // :
483
	',', // ,
484
	'?' // ?
485
	);
486
	$string = str_replace($bad, '', $string);
487
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
488
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
489
	// Clean any page spacers at the end of string
490
	$string = trim($string);
491
	// Finally, return the cleaned string
492
	return $string;
493
}
494

    
495
// Function to work out a page link
496
if(!function_exists('page_link')) {
497
	function page_link($link) {
498
		global $admin;
499
		return $admin->page_link($link);
500
	}
501
}
502

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

    
542
// Function for working out a file mime type (if the in-built PHP one is not enabled)
543
if(!function_exists('mime_content_type')) {
544
    function mime_content_type($filename) {
545

    
546
    $mime_types = array(
547
            'txt'	=> 'text/plain',
548
            'htm'	=> 'text/html',
549
            'html'	=> 'text/html',
550
            'php'	=> 'text/html',
551
            'css'	=> 'text/css',
552
            'js'	=> 'application/javascript',
553
            'json'	=> 'application/json',
554
            'xml'	=> 'application/xml',
555
            'swf'	=> 'application/x-shockwave-flash',
556
            'flv'	=> 'video/x-flv',
557

    
558
            // images
559
            'png'	=> 'image/png',
560
            'jpe'	=> 'image/jpeg',
561
            'jpeg'	=> 'image/jpeg',
562
            'jpg'	=> 'image/jpeg',
563
            'gif'	=> 'image/gif',
564
            'bmp'	=> 'image/bmp',
565
            'ico'	=> 'image/vnd.microsoft.icon',
566
            'tiff'	=> 'image/tiff',
567
            'tif'	=> 'image/tiff',
568
            'svg'	=> 'image/svg+xml',
569
            'svgz'	=> 'image/svg+xml',
570

    
571
            // archives
572
            'zip'	=> 'application/zip',
573
            'rar'	=> 'application/x-rar-compressed',
574
            'exe'	=> 'application/x-msdownload',
575
            'msi'	=> 'application/x-msdownload',
576
            'cab'	=> 'application/vnd.ms-cab-compressed',
577

    
578
            // audio/video
579
            'mp3'	=> 'audio/mpeg',
580
            'mp4'	=> 'audio/mpeg',
581
            'qt'	=> 'video/quicktime',
582
            'mov'	=> 'video/quicktime',
583

    
584
            // adobe
585
            'pdf'	=> 'application/pdf',
586
            'psd'	=> 'image/vnd.adobe.photoshop',
587
            'ai'	=> 'application/postscript',
588
            'eps'	=> 'application/postscript',
589
            'ps'	=> 'application/postscript',
590

    
591
            // ms office
592
            'doc'	=> 'application/msword',
593
            'rtf'	=> 'application/rtf',
594
            'xls'	=> 'application/vnd.ms-excel',
595
            'ppt'	=> 'application/vnd.ms-powerpoint',
596

    
597
            // open office
598
            'odt'	=> 'application/vnd.oasis.opendocument.text',
599
            'ods'	=> 'application/vnd.oasis.opendocument.spreadsheet',
600
        );
601

    
602
        $temp = explode('.',$filename);
603
        $ext = strtolower(array_pop($temp));
604

    
605
        if (array_key_exists($ext, $mime_types)) {
606
            return $mime_types[$ext];
607
        }
608
        elseif (function_exists('finfo_open')) {
609
            $finfo = finfo_open(FILEINFO_MIME);
610
            $mimetype = finfo_file($finfo, $filename);
611
            finfo_close($finfo);
612
            return $mimetype;
613
        }
614
        else {
615
            return 'application/octet-stream';
616
        }
617
    }
618
}
619

    
620
// Generate a thumbnail from an image
621
function make_thumb($source, $destination, $size) {
622
	// Check if GD is installed
623
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
624
		// First figure out the size of the thumbnail
625
		list($original_x, $original_y) = getimagesize($source);
626
		if ($original_x > $original_y) {
627
			$thumb_w = $size;
628
			$thumb_h = $original_y*($size/$original_x);
629
		}
630
		if ($original_x < $original_y) {
631
			$thumb_w = $original_x*($size/$original_y);
632
			$thumb_h = $size;
633
		}
634
		if ($original_x == $original_y) {
635
			$thumb_w = $size;
636
			$thumb_h = $size;	
637
		}
638
		// Now make the thumbnail
639
		$source = imageCreateFromJpeg($source);
640
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
641
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
642
		imagejpeg($dst_img, $destination);
643
		// Clear memory
644
		imagedestroy($dst_img);
645
	   imagedestroy($source);
646
	   // Return true
647
	   return true;
648
   } else {
649
   	return false;
650
   }
651
}
652

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

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

    
841
// Load module into DB
842
function load_module($directory, $install = false) {
843
	global $database,$admin,$MESSAGE;
844
	if(is_dir($directory) AND file_exists($directory.'/info.php'))
845
	{
846
		require($directory.'/info.php');
847
		if(isset($module_name))
848
	{
849
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
850
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
851
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
852
			$module_function = strtolower($module_function);
853
			// Check that it doesn't already exist
854
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
855
			if($result->numRows() == 0)
856
			{
857
				// Load into DB
858
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
859
				"(directory,name,description,type,function,version,platform,author,license) ".
860
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
861
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
862
				$database->query($query);
863
				// Run installation script
864
				if($install == true)
865
				{
866
					if(file_exists($directory.'/install.php')) {
867
						require($directory.'/install.php');
868
					}
869
				}
870
			}
871
		}
872
	}
873
}
874

    
875
// Load template into DB
876
function load_template($directory) {
877
	global $database;
878
	if(is_dir($directory) AND file_exists($directory.'/info.php')) {
879
		require($directory.'/info.php');
880
		if(isset($template_name)) {
881
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
882
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
883
			if(!isset($template_function)) { $template_function = 'template'; }
884
			// Check that it doesn't already exist
885
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE type = 'template' AND directory = '".$template_directory."' LIMIT 0,1");
886
			if($result->numRows() == 0) {
887
				// Load into DB
888
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
889
				"(directory,name,description,type,function,version,platform,author,license) ".
890
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
891
				"'$template_function','$template_version','$template_platform','$template_author','$template_license')";
892
				$database->query($query);
893
			}
894
		}
895
	}
896
}
897

    
898
// Load language into DB
899
function load_language($file) {
900
	global $database;
901
	if (file_exists($file) && preg_match('#^([A-Z]{2}.php)#', basename($file))) {
902
		require($file);
903
		if(isset($language_name)) {
904
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
905
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
906
			// Check that it doesn't already exist
907
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
908
			if($result->numRows() == 0) {
909
				// Load into DB
910
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
911
				"(directory,name,type,version,platform,author,license) ".
912
				"VALUES ('$language_code','$language_name','language',".
913
				"'$language_version','$language_platform','$language_author','$language_license')";
914
	 		$database->query($query);
915
			}
916
		}
917
	}
918
}
919

    
920
// Upgrade module info in DB, optionally start upgrade script
921
function upgrade_module($directory, $upgrade = false) {
922
	global $database, $admin, $MESSAGE;
923
	$directory = WB_PATH . "/modules/$directory";
924
	if(file_exists($directory.'/info.php')) {
925
		require($directory.'/info.php');
926
		if(isset($module_name)) {
927
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
928
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
929
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
930
			$module_function = strtolower($module_function);
931
			// Check that it does already exist
932
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
933
			if($result->numRows() > 0) {
934
				// Update in DB
935
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
936
					"version = '$module_version', " .
937
					"description = '" . addslashes($module_description) . "', " .
938
					"platform = '$module_platform', " .
939
					"author = '$module_author', " .
940
					"license = '$module_license'" .
941
					"WHERE directory = '$module_directory'";
942
				$database->query($query);
943
				// Run upgrade script
944
				if($upgrade == true) {
945
					if(file_exists($directory.'/upgrade.php')) {
946
						require($directory.'/upgrade.php');
947
					}
948
				}
949
			}
950
		}
951
	}
952
}
953

    
954
// extracts the content of a string variable from a string (save alternative to including files)
955
if(!function_exists('get_variable_content')) {
956
	function get_variable_content($search, $data, $striptags=true, $convert_to_entities=true) {
957
		$match = '';
958
		// search for $variable followed by 0-n whitespace then by = then by 0-n whitespace
959
		// then either " or ' then 0-n characters then either " or ' followed by 0-n whitespace and ;
960
		// the variable name is returned in $match[1], the content in $match[3]
961
		if (preg_match('/(\$' .$search .')\s*=\s*("|\')(.*)\2\s*;/', $data, $match)) {
962
			if(strip_tags(trim($match[1])) == '$' .$search) {
963
				// variable name matches, return it's value
964
				$match[3] = ($striptags == true) ? strip_tags($match[3]) : $match[3];
965
				$match[3] = ($convert_to_entities == true) ? htmlentities($match[3]) : $match[3];
966
				return $match[3];
967
			}
968
		}
969
		return false;
970
	}
971
}
972

    
973
?>
(12-12/15)