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-2010, 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 4.3.4 and higher
13
 * @version         $Id: functions.php 1291 2010-02-19 02:07:14Z Luisehahne $
14
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/functions.php $
15
 * @lastmodified    $Date: 2010-02-19 03:07:14 +0100 (Fri, 19 Feb 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
// Function to open a directory and add to a dir list
61
function directory_list($directory)
62
{
63
	$list = array();
64
	if (is_dir($directory))
65
    {
66
    	// Open the directory then loop through its contents
67
    	$dir = dir($directory);
68
    	while (false !== $entry = $dir->read())
69
		{
70
    		// Skip pointers
71
    		if($entry[0] == '.') { continue; }
72
    		// Add dir and contents to list
73
    		if (is_dir("$directory/$entry"))
74
			{
75
    			$list = array_merge($list, directory_list("$directory/$entry"));
76
    			$list[] = "$directory/$entry";
77
    		}
78
    	}
79
        $dir->close();
80
    }
81
    // Now return the list
82
	return $list;
83
}
84

    
85
// Function to open a directory and add to a dir list
86
function chmod_directory_contents($directory, $file_mode)
87
{
88
	if (is_dir($directory))
89
    {
90
    	// Set the umask to 0
91
    	$umask = umask(0);
92
    	// Open the directory then loop through its contents
93
    	$dir = dir($directory);
94
    	while (false !== $entry = $dir->read())
95
		{
96
    		// Skip pointers
97
    		if($entry[0] == '.') { continue; }
98
    		// Chmod the sub-dirs contents
99
    		if(is_dir("$directory/$entry"))
100
			{
101
    			chmod_directory_contents($directory.'/'.$entry, $file_mode);
102
    		}
103
    		change_mode($directory.'/'.$entry);
104
    	}
105
        $dir->close();
106
    	// Restore the umask
107
    	umask($umask);
108
    }
109
}
110

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

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

    
211
// Function to create directories
212
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE)
213
{
214
	if(!is_dir($dir_name))
215
    {
216
		$umask = umask(0);
217
		mkdir($dir_name, $dir_mode);
218
		umask($umask);
219
		return true;
220
	} else {
221
		return false;	
222
	}
223
}
224

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

    
240
		if(file_exists($name))
241
        {
242
			$umask = umask(0);
243
			chmod($name, $mode);
244
			umask($umask);
245
			return true;
246
		}
247
        else
248
        {
249
			return false;	
250
		}
251
	}
252
    else
253
    {
254
		return true;
255
	}
256
}
257

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

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

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

    
320
// Function to get page title
321
function get_page_title($id)
322
{
323
	global $database;
324
	// Get title
325
	$sql = 'SELECT `page_title` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$id;
326
	$page_title = $database->get_one($sql);
327
	return $page_title;
328
}
329

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

    
340
// Function to get all parent page titles
341
function get_parent_titles($parent_id)
342
{
343
	$titles[] = get_menu_title($parent_id);
344
	if(is_parent($parent_id) != false)
345
	{
346
		$parent_titles = get_parent_titles(is_parent($parent_id));
347
		$titles = array_merge($titles, $parent_titles);
348
	}
349
	return $titles;
350
}
351

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

    
364
// Function to genereate page trail
365
function get_page_trail($page_id) {
366
	return implode(',', array_reverse(get_parent_ids($page_id)));
367
}
368

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

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

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

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

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

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

    
482
// Function to work out a page link
483
if(!function_exists('page_link'))
484
{
485
	function page_link($link)
486
	{
487
		global $admin;
488
		return $admin->page_link($link);
489
	}
490
}
491

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

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

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

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

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

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

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

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

    
600
        $temp = explode('.',$filename);
601
        $ext = strtolower(array_pop($temp));
602

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

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

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

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

    
790
// Load module into DB
791
function load_module($directory, $install = false)
792
{
793
	global $database,$admin,$MESSAGE;
794

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

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

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

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

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

    
967
?>
(12-12/15)