Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        frontend
5
 * @package         framework
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link			http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: functions.php 1349 2010-12-19 19:04:59Z Luisehahne $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/functions.php $
15
 * @lastmodified    $Date: 2010-12-19 20:04:59 +0100 (Sun, 19 Dec 2010) $
16
 *
17
*/
18

    
19
// Stop this file from being accessed directly
20
if(!defined('WB_URL')) {
21
	header('Location: ../index.php');
22
	exit;
23
}
24

    
25
// Define that this file has been loaded
26
define('FUNCTIONS_FILE_LOADED', true);
27

    
28
// Function to remove a non-empty directory
29
function rm_full_dir($directory)
30
{
31
    // If suplied dirname is a file then unlink it
32
    if (is_file($directory))
33
	{
34
        return unlink($directory);
35
    }
36
    // Empty the folder
37
	if (is_dir($directory))
38
    {
39
        $dir = dir($directory);
40
        while (false !== $entry = $dir->read())
41
        {
42
            // Skip pointers
43
            if ($entry == '.' || $entry == '..') { continue; }
44
            // Deep delete directories
45
            if (is_dir($directory.'/'.$entry))
46
			{
47
				rm_full_dir($directory.'/'.$entry);
48
            }
49
            else
50
            {
51
                unlink($directory.'/'.$entry);
52
            }
53
        }
54
        // Now delete the folder
55
        $dir->close();
56
        return rmdir($directory);
57
	}
58
}
59

    
60
/*
61
 * returns a recursive list of all subdirectories from a given directory
62
 * @access  public
63
 * @param   string  $directory: from this dir the recursion will start
64
 * @param   bool    $show_hidden:  if set to TRUE also hidden dirs (.dir) will be shown
65
 * @return  array
66
 * example:
67
 *  /srv/www/httpdocs/wb/media/a/b/c/
68
 *  /srv/www/httpdocs/wb/media/a/b/d/
69
 * directory_list('/srv/www/httpdocs/wb/media/') will return:
70
 *  /a
71
 *  /a/b
72
 *  /a/b/c
73
 *  /a/b/d
74
 */
75
 function directory_list($directory, $show_hidden = false)
76
{
77
	$result_list = array();
78
	if (is_dir($directory))
79
    {
80
    	$dir = dir($directory); // Open the directory
81
    	while (false !== $entry = $dir->read()) // loop through the directory
82
		{
83
			if($entry == '.' || $entry == '..') { continue; } // Skip pointers
84
			if($entry[0] == '.' && $show_hidden == false) { continue; } // Skip hidden files
85
    		if (is_dir("$directory/$entry")) // Add dir and contents to list
86
			{
87
    			$result_list = array_merge($result_list, directory_list("$directory/$entry"));
88
    			$result_list[] = "$directory/$entry";
89
    		}
90
    	}
91
        $dir->close();
92
    }
93
	return $result_list; // Now return the 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
	if (is_dir($directory))
100
    {
101
    	// Set the umask to 0
102
    	$umask = umask(0);
103
    	// Open the directory then loop through its contents
104
    	$dir = dir($directory);
105
    	while (false !== $entry = $dir->read())
106
		{
107
    		// Skip pointers
108
    		if($entry[0] == '.') { continue; }
109
    		// Chmod the sub-dirs contents
110
    		if(is_dir("$directory/$entry"))
111
			{
112
    			chmod_directory_contents($directory.'/'.$entry, $file_mode);
113
    		}
114
    		change_mode($directory.'/'.$entry);
115
    	}
116
        $dir->close();
117
    	// Restore the umask
118
    	umask($umask);
119
    }
120
}
121

    
122
// Function to open a directory and add to a file list
123
function file_list($directory, $skip = array(), $show_hidden = false)
124
{
125
	$result_list = array();
126
	if (is_dir($directory))
127
    {
128
    	$dir = dir($directory); // Open the directory
129
		while (false !== ($entry = $dir->read())) // loop through the directory
130
		{
131
			if($entry == '.' || $entry == '..') { continue; } // Skip pointers
132
			if($entry[0] == '.' && $show_hidden == false) { continue; } // Skip hidden files
133
			if( sizeof($skip) > 0 && in_array($entry, $skip) ) { continue; } // Check if we to skip anything else
134
			if(is_file( $directory.'/'.$entry)) // Add files to list
135
			{
136
				$result_list[] = $directory.'/'.$entry;
137
			}
138
		}
139
		$dir->close(); // Now close the folder object
140
	}
141
	natsort($result_list); // make the list nice. Not all OS do this itself
142
	return $result_list;
143
}
144

    
145
// Function to get a list of home folders not to show
146
function get_home_folders()
147
{
148
	global $database, $admin;
149
	$home_folders = array();
150
	// Only return home folders is this feature is enabled
151
	// and user is not admin
152
//	if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
153
	if(HOME_FOLDERS AND (!in_array('1',explode(',', $_SESSION['GROUPS_ID']))))
154
	{
155
		$sql = 'SELECT `home_folder` FROM `'.TABLE_PREFIX.'users` WHERE `home_folder` != "'.$admin->get_home_folder().'"';
156
		$query_home_folders = $database->query($sql);
157
		if($query_home_folders->numRows() > 0)
158
		{
159
			while($folder = $query_home_folders->fetchRow())
160
			{
161
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
162
			}
163
		}
164
		function remove_home_subs($directory = '/', $home_folders = '')
165
		{
166
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory))
167
			{
168
				// Loop through the dirs to check the home folders sub-dirs are not shown
169
				while(false !== ($file = readdir($handle)))
170
				{
171
					if($file[0] != '.' AND $file != 'index.php')
172
					{
173
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file))
174
						{
175
							if($directory != '/')
176
							{
177
								$file = $directory.'/'.$file;
178
							}
179
							else
180
							{
181
								$file = '/'.$file;
182
							}
183
							foreach($home_folders AS $hf)
184
							{
185
								$hf_length = strlen($hf);
186
								if($hf_length > 0)
187
								{
188
									if(substr($file, 0, $hf_length+1) == $hf)
189
									{
190
										$home_folders[$file] = $file;
191
									}
192
								}
193
							}
194
							$home_folders = remove_home_subs($file, $home_folders);
195
						}
196
					}
197
				}
198
			}
199
			return $home_folders;
200
		}
201
		$home_folders = remove_home_subs('/', $home_folders);
202
	}
203
	return $home_folders;
204
}
205

    
206
// Function to create directories
207
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE)
208
{
209
	if(!is_dir($dir_name))
210
    {
211
		$umask = umask(0);
212
		mkdir($dir_name, $dir_mode);
213
		umask($umask);
214
		return true;
215
	} else {
216
		return false;	
217
	}
218
}
219

    
220
// Function to chmod files and directories
221
function change_mode($name)
222
{
223
	if(OPERATING_SYSTEM != 'windows')
224
    {
225
		// Only chmod if os is not windows
226
		if(is_dir($name))
227
        {
228
			$mode = OCTAL_DIR_MODE;
229
		}
230
        else
231
        {
232
			$mode = OCTAL_FILE_MODE;
233
		}
234

    
235
		if(file_exists($name))
236
        {
237
			$umask = umask(0);
238
			chmod($name, $mode);
239
			umask($umask);
240
			return true;
241
		}
242
        else
243
        {
244
			return false;	
245
		}
246
	}
247
    else
248
    {
249
		return true;
250
	}
251
}
252

    
253
// Function to figure out if a parent exists
254
function is_parent($page_id)
255
{
256
	global $database;
257
	// Get parent
258
	$sql = 'SELECT `parent` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
259
	$parent = $database->get_one($sql);
260
	// If parent isnt 0 return its ID
261
	if(is_null($parent))
262
	{
263
		return false;
264
	}
265
	else
266
	{
267
		return $parent;
268
	}
269
}
270

    
271
// Function to work out level
272
function level_count($page_id)
273
{
274
	global $database;
275
	// Get page parent
276
	$sql = 'SELECT `parent` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
277
	$parent = $database->get_one($sql);
278
	if($parent > 0) 
279
	{	// Get the level of the parent
280
		$sql = 'SELECT `level` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$parent;
281
		$level = $database->get_one($sql);
282
		return $level+1;
283
	}
284
	else
285
	{
286
		return 0;
287
	}
288
}
289

    
290
// Function to work out root parent
291
function root_parent($page_id)
292
{
293
	global $database;
294
	// Get page details
295
	$sql = 'SELECT `parent`, `level` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
296
	$query_page = $database->query($sql);
297
	$fetch_page = $query_page->fetchRow();
298
	$parent = $fetch_page['parent'];
299
	$level = $fetch_page['level'];	
300
	if($level == 1)
301
	{
302
		return $parent;
303
	}
304
	elseif($parent == 0)
305
	{
306
		return $page_id;
307
	}
308
	else
309
	{	// Figure out what the root parents id is
310
		$parent_ids = array_reverse(get_parent_ids($page_id));
311
		return $parent_ids[0];
312
	}
313
}
314

    
315
// Function to get page title
316
function get_page_title($id)
317
{
318
	global $database;
319
	// Get title
320
	$sql = 'SELECT `page_title` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$id;
321
	$page_title = $database->get_one($sql);
322
	return $page_title;
323
}
324

    
325
// Function to get a pages menu title
326
function get_menu_title($id)
327
{
328
	global $database;
329
	// Get title
330
	$sql = 'SELECT `menu_title` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$id;
331
	$menu_title = $database->get_one($sql);
332
	return $menu_title;
333
}
334

    
335
// Function to get all parent page titles
336
function get_parent_titles($parent_id)
337
{
338
	$titles[] = get_menu_title($parent_id);
339
	if(is_parent($parent_id) != false)
340
	{
341
		$parent_titles = get_parent_titles(is_parent($parent_id));
342
		$titles = array_merge($titles, $parent_titles);
343
	}
344
	return $titles;
345
}
346

    
347
// Function to get all parent page id's
348
function get_parent_ids($parent_id)
349
{
350
	$ids[] = $parent_id;
351
	if(is_parent($parent_id) != false)
352
	{
353
		$parent_ids = get_parent_ids(is_parent($parent_id));
354
		$ids = array_merge($ids, $parent_ids);
355
	}
356
	return $ids;
357
}
358

    
359
// Function to genereate page trail
360
function get_page_trail($page_id) {
361
	return implode(',', array_reverse(get_parent_ids($page_id)));
362
}
363

    
364
// Function to get all sub pages id's
365
function get_subs($parent, $subs)
366
{
367
	// Connect to the database
368
	global $database;
369
	// Get id's
370
	$sql = 'SELECT `page_id` FROM `'.TABLE_PREFIX.'pages` WHERE `parent` = '.$parent;
371
	$query = $database->query($sql);
372
	if($query->numRows() > 0)
373
	{
374
		while($fetch = $query->fetchRow())
375
		{
376
			$subs[] = $fetch['page_id'];
377
			// Get subs of this sub
378
			$subs = get_subs($fetch['page_id'], $subs);
379
		}
380
	}
381
	// Return subs array
382
	return $subs;
383
}
384

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

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

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

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

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

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

    
534
// Function for working out a file mime type (if the in-built PHP one is not enabled)
535
if(!function_exists('mime_content_type'))
536
{
537
    function mime_content_type($filename) 
538
	{
539
	    $mime_types = array(
540
            'txt'	=> 'text/plain',
541
            'htm'	=> 'text/html',
542
            'html'	=> 'text/html',
543
            'php'	=> 'text/html',
544
            'css'	=> 'text/css',
545
            'js'	=> 'application/javascript',
546
            'json'	=> 'application/json',
547
            'xml'	=> 'application/xml',
548
            'swf'	=> 'application/x-shockwave-flash',
549
            'flv'	=> 'video/x-flv',
550

    
551
            // images
552
            'png'	=> 'image/png',
553
            'jpe'	=> 'image/jpeg',
554
            'jpeg'	=> 'image/jpeg',
555
            'jpg'	=> 'image/jpeg',
556
            'gif'	=> 'image/gif',
557
            'bmp'	=> 'image/bmp',
558
            'ico'	=> 'image/vnd.microsoft.icon',
559
            'tiff'	=> 'image/tiff',
560
            'tif'	=> 'image/tiff',
561
            'svg'	=> 'image/svg+xml',
562
            'svgz'	=> 'image/svg+xml',
563

    
564
            // archives
565
            'zip'	=> 'application/zip',
566
            'rar'	=> 'application/x-rar-compressed',
567
            'exe'	=> 'application/x-msdownload',
568
            'msi'	=> 'application/x-msdownload',
569
            'cab'	=> 'application/vnd.ms-cab-compressed',
570

    
571
            // audio/video
572
            'mp3'	=> 'audio/mpeg',
573
            'mp4'	=> 'audio/mpeg',
574
            'qt'	=> 'video/quicktime',
575
            'mov'	=> 'video/quicktime',
576

    
577
            // adobe
578
            'pdf'	=> 'application/pdf',
579
            'psd'	=> 'image/vnd.adobe.photoshop',
580
            'ai'	=> 'application/postscript',
581
            'eps'	=> 'application/postscript',
582
            'ps'	=> 'application/postscript',
583

    
584
            // ms office
585
            'doc'	=> 'application/msword',
586
            'rtf'	=> 'application/rtf',
587
            'xls'	=> 'application/vnd.ms-excel',
588
            'ppt'	=> 'application/vnd.ms-powerpoint',
589

    
590
            // open office
591
            'odt'	=> 'application/vnd.oasis.opendocument.text',
592
            'ods'	=> 'application/vnd.oasis.opendocument.spreadsheet',
593
        );
594

    
595
        $temp = explode('.',$filename);
596
        $ext = strtolower(array_pop($temp));
597

    
598
        if (array_key_exists($ext, $mime_types))
599
		{
600
            return $mime_types[$ext];
601
        }
602
        elseif (function_exists('finfo_open'))
603
		{
604
            $finfo = finfo_open(FILEINFO_MIME);
605
            $mimetype = finfo_file($finfo, $filename);
606
            finfo_close($finfo);
607
            return $mimetype;
608
        }
609
        else
610
		{
611
            return 'application/octet-stream';
612
        }
613
    }
614
}
615

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

    
654
/*
655
 * Function to work-out a single part of an octal permission value
656
 *
657
 * @param mixed $octal_value: an octal value as string (i.e. '0777') or real octal integer (i.e. 0777 | 777)
658
 * @param string $who: char or string for whom the permission is asked( U[ser] / G[roup] / O[thers] )
659
 * @param string $action: char or string with the requested action( r[ead..] / w[rite..] / e|x[ecute..] )
660
 * @return boolean
661
 */
662
function extract_permission($octal_value, $who, $action)
663
{
664
	// Make sure that all arguments are set and $octal_value is a real octal-integer
665
	if( ($who == '') or ($action == '') or (preg_match( '/[^0-7]/', (string)$octal_value )) )
666
	{
667
		return false; // invalid argument, so return false
668
	}
669
	// convert $octal_value into a decimal-integer to be sure having a valid value
670
	$right_mask = octdec($octal_value);
671
	$action_mask = 0;
672
	// set the $action related bit in $action_mask
673
	switch($action[0]) // get action from first char of $action
674
	{
675
		case 'r':
676
		case 'R':
677
			$action_mask = 4; // set read-bit only (2^2)
678
			break;
679
		case 'w':
680
		case 'W':
681
			$action_mask = 2; // set write-bit only (2^1)
682
			break;
683
		case 'e':
684
		case 'E':
685
		case 'x':
686
		case 'X':
687
			$action_mask = 1; // set execute-bit only (2^0)
688
			break;
689
		default:
690
			return false; // undefined action name, so return false
691
	}
692
	// shift action-mask into the right position
693
	switch($who[0]) // get who from first char of $who
694
	{
695
		case 'u':
696
		case 'U':
697
			$action_mask <<= 3; // shift left 3 bits
698
		case 'g':
699
		case 'G':
700
			$action_mask <<= 3; // shift left 3 bits
701
		case 'o':
702
		case 'O':
703
			/* NOP */
704
			break;
705
		default:
706
			return false; // undefined who, so return false
707
	}
708
	return( ($right_mask & $action_mask) != 0 ); // return result of binary-AND
709
}
710

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

    
785
// Load module into DB
786
function load_module($directory, $install = false)
787
{
788
	global $database,$admin,$MESSAGE;
789

    
790
	if(is_dir($directory) AND file_exists($directory.'/info.php'))
791
	{
792
		require($directory.'/info.php');
793
		if(isset($module_name))
794
		{
795
			if(!isset($module_license))                                  { $module_license = 'GNU General Public License'; }
796
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
797
			if(!isset($module_function) AND isset($module_type))         { $module_function = $module_type; }
798
			$module_function = strtolower($module_function);
799
			// Check that it doesn't already exist
800
			$sql  = 'SELECT `addon_id` FROM `'.TABLE_PREFIX.'addons` ';
801
			$sql .= 'WHERE `type` = "module" AND `directory` = "'.$module_directory.'" LIMIT 0,1';
802
			$result = $database->query($sql);
803
			if($result->numRows() == 0)
804
			{
805
				// Load into DB
806
				$sql  = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
807
				$sql .= '`directory` = "'.$module_directory.'", ';
808
				$sql .= '`name` = "'.$module_name.'", ';
809
				$sql .= '`description`= "'.addslashes($module_description).'", ';
810
				$sql .= '`type`= "module", ';
811
				$sql .= '`function` = "'.$module_function.'", ';
812
				$sql .= '`version` = "'.$module_version.'", ';
813
				$sql .= '`platform` = "'.$module_platform.'", ';
814
				$sql .= '`author` = "'.addslashes($module_author).'", ';
815
				$sql .= '`license` = "'.addslashes($module_license).'"';
816
				$database->query($sql);
817
				// Run installation script
818
				if($install == true)
819
				{
820
					if(file_exists($directory.'/install.php'))
821
					{
822
						require($directory.'/install.php');
823
					}
824
				}
825
			}
826
		}
827
	}
828
}
829

    
830
// Load template into DB
831
function load_template($directory)
832
{
833
	global $database, $admin;
834
	if(is_dir($directory) AND file_exists($directory.'/info.php'))
835
	{
836
		require($directory.'/info.php');
837
		if(isset($template_name))
838
		{
839
			if(!isset($template_license))
840
            {
841
              $template_license = 'GNU General Public License';
842
            }
843
			if(!isset($template_platform) AND isset($template_designed_for))
844
            {
845
              $template_platform = $template_designed_for;
846
            }
847
			if(!isset($template_function))
848
            {
849
              $template_function = 'template';
850
            }
851
			// Check that it doesn't already exist
852
			$sql  = 'SELECT `addon_id` FROM `'.TABLE_PREFIX.'addons` ';
853
			$sql .= 'WHERE `type` = "template" AND `directory` = "'.$template_directory.'" LIMIT 0,1';
854
			$result = $database->query($sql);
855
			if($result->numRows() == 0)
856
			{
857
				// Load into DB
858
				$sql  = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
859
				$sql .= '`directory` = "'.$template_directory.'", ';
860
				$sql .= '`name` = "'.$template_name.'", ';
861
				$sql .= '`description`= "'.addslashes($template_description).'", ';
862
				$sql .= '`type`= "template", ';
863
				$sql .= '`function` = "'.$template_function.'", ';
864
				$sql .= '`version` = "'.$template_version.'", ';
865
				$sql .= '`platform` = "'.$template_platform.'", ';
866
				$sql .= '`author` = "'.addslashes($template_author).'", ';
867
				$sql .= '`license` = "'.addslashes($template_license).'" ';
868
				$database->query($sql);
869
			}
870
		}
871
	}
872
}
873

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

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

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

    
971
?>
(13-13/16)