Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 851 2008-07-01 18:38:36Z thorn $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
/*
27

    
28
Website Baker functions file
29
This file contains general functions used in Website Baker
30

    
31
*/
32

    
33
// Stop this file from being accessed directly
34
if(!defined('WB_URL')) {
35
	header('Location: ../index.php');
36
	exit(0);
37
}
38

    
39
// Define that this file has been loaded
40
define('FUNCTIONS_FILE_LOADED', true);
41

    
42
// Function to remove a non-empty directory
43
function rm_full_dir($directory)
44
{
45
    // If suplied dirname is a file then unlink it
46
    if (is_file($directory)) {
47
        return unlink($directory);
48
    }
49

    
50
    // Empty the folder
51
    $dir = dir($directory);
52
    while (false !== $entry = $dir->read()) {
53
        // Skip pointers
54
        if ($entry == '.' || $entry == '..') {
55
            continue;
56
        }
57

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

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

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

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

    
90
	// Now return the list
91
	return $list;
92
}
93

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

    
117
}
118

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

    
149
	// Now delete the folder
150
	return $list;
151
}
152

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
352
// init utf8-functions -- workaround to prevent functions-utf8.php and charsets_table.php (~140kB) to be loaded more than once
353
// functions and arrays from functions-utf8.php and charsets_table.php will be in global name-space
354
function init_utf8funcs() {
355
	static $utf8_ok=0;
356
	if($utf8_ok == 0) {
357
		++$utf8_ok;
358
		require_once(WB_PATH.'/framework/functions-utf8.php');
359
	}
360
}
361

    
362
// Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
363
// Will replace all numeric and named entities except &gt; &lt; &apos; &quot; &#39; &nbsp;
364
// In case of error the returned string is unchanged, and a message is emitted.
365
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET) {
366
	//init utf8-functions
367
	init_utf8funcs();
368
	return entities_to_umlauts2($string, $charset_out);
369
}
370

    
371
// Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
372
// In case of error the returned string is unchanged, and a message is emitted.
373
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET) {
374
	//init utf8-functions
375
	init_utf8funcs();
376
	return umlauts_to_entities2($string, $charset_in);
377
}
378

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

    
407
// Function to convert a desired media filename to a clean filename
408
function media_filename($string) {
409
	//init utf8-functions
410
	init_utf8funcs();
411
	$string = entities_to_7bit($string);
412
	// Now remove all bad characters
413
	$bad = array(
414
	'\'', // '
415
	'"', // "
416
	'`', // `
417
	'!', // !
418
	'@', // @
419
	'#', // #
420
	'$', // $
421
	'%', // %
422
	'^', // ^
423
	'&', // &
424
	'*', // *
425
	'=', // =
426
	'+', // +
427
	'|', // |
428
	'/', // /
429
	'\\', // \
430
	';', // ;
431
	':', // :
432
	',', // ,
433
	'?' // ?
434
	);
435
	$string = str_replace($bad, '', $string);
436
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
437
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
438
	// Clean any page spacers at the end of string
439
	$string = trim($string);
440
	// Finally, return the cleaned string
441
	return $string;
442
}
443

    
444
// Function to work out a page link
445
if(!function_exists('page_link')) {
446
	function page_link($link) {
447
		global $admin;
448
		return $admin->page_link($link);
449
	}
450
}
451

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

    
491
// Function for working out a file mime type (if the in-built PHP one is not enabled)
492
if(!function_exists('mime_content_type')) {
493
   function mime_content_type($file) {
494
       $file = escapeshellarg($file);
495
       return trim(`file -bi $file`);
496
   }
497
}
498

    
499
// Generate a thumbnail from an image
500
function make_thumb($source, $destination, $size) {
501
	// Check if GD is installed
502
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
503
		// First figure out the size of the thumbnail
504
		list($original_x, $original_y) = getimagesize($source);
505
		if ($original_x > $original_y) {
506
			$thumb_w = $size;
507
			$thumb_h = $original_y*($size/$original_x);
508
		}
509
		if ($original_x < $original_y) {
510
			$thumb_w = $original_x*($size/$original_y);
511
			$thumb_h = $size;
512
		}
513
		if ($original_x == $original_y) {
514
			$thumb_w = $size;
515
			$thumb_h = $size;	
516
		}
517
		// Now make the thumbnail
518
		$source = imageCreateFromJpeg($source);
519
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
520
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
521
		imagejpeg($dst_img, $destination);
522
		// Clear memory
523
		imagedestroy($dst_img);
524
	   imagedestroy($source);
525
	   // Return true
526
	   return true;
527
   } else {
528
   	return false;
529
   }
530
}
531

    
532
// Function to work-out a single part of an octal permission value
533
function extract_permission($octal_value, $who, $action) {
534
	// Make sure the octal value is 4 chars long
535
	if(strlen($octal_value) == 0) {
536
		$octal_value = '0000';
537
	} elseif(strlen($octal_value) == 1) {
538
		$octal_value = '000'.$octal_value;
539
	} elseif(strlen($octal_value) == 2) {
540
		$octal_value = '00'.$octal_value;
541
	} elseif(strlen($octal_value) == 3) {
542
		$octal_value = '0'.$octal_value;
543
	} elseif(strlen($octal_value) == 4) {
544
		$octal_value = ''.$octal_value;
545
	} else {
546
		$octal_value = '0000';
547
	}
548
	// Work-out what position of the octal value to look at
549
	switch($who) {
550
	case 'u':
551
		$position = '1';
552
		break;
553
	case 'user':
554
		$position = '1';
555
		break;
556
	case 'g':
557
		$position = '2';
558
		break;
559
	case 'group':
560
		$position = '2';
561
		break;
562
	case 'o':
563
		$position = '3';
564
		break;
565
	case 'others':
566
		$position = '3';
567
		break;
568
	}
569
	// Work-out how long the octal value is and ajust acording
570
	if(strlen($octal_value) == 4) {
571
		$position = $position+1;
572
	} elseif(strlen($octal_value) != 3) {
573
		exit('Error');
574
	}
575
	// Now work-out what action the script is trying to look-up
576
	switch($action) {
577
	case 'r':
578
		$action = 'r';
579
		break;
580
	case 'read':
581
		$action = 'r';
582
		break;
583
	case 'w':
584
		$action = 'w';
585
		break;
586
	case 'write':
587
		$action = 'w';
588
		break;
589
	case 'e':
590
		$action = 'e';
591
		break;
592
	case 'execute':
593
		$action = 'e';
594
		break;
595
	}
596
	// Get the value for "who"
597
	$value = substr($octal_value, $position-1, 1);
598
	// Now work-out the details of the value
599
	switch($value) {
600
	case '0':
601
		$r = false;
602
		$w = false;
603
		$e = false;
604
		break;
605
	case '1':
606
		$r = false;
607
		$w = false;
608
		$e = true;
609
		break;
610
	case '2':
611
		$r = false;
612
		$w = true;
613
		$e = false;
614
		break;
615
	case '3':
616
		$r = false;
617
		$w = true;
618
		$e = true;
619
		break;
620
	case '4':
621
		$r = true;
622
		$w = false;
623
		$e = false;
624
		break;
625
	case '5':
626
		$r = true;
627
		$w = false;
628
		$e = true;
629
		break;
630
	case '6':
631
		$r = true;
632
		$w = true;
633
		$e = false;
634
		break;
635
	case '7':
636
		$r = true;
637
		$w = true;
638
		$e = true;
639
		break;
640
	default:
641
		$r = false;
642
		$w = false;
643
		$e = false;
644
	}
645
	// And finally, return either true or false
646
	return $$action;
647
}
648

    
649
// Function to delete a page
650
function delete_page($page_id) {
651
	
652
	global $admin, $database, $MESSAGE;
653
	
654
	// Find out more about the page
655
	$database = new database();
656
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
657
	$results = $database->query($query);
658
	if($database->is_error()) {
659
		$admin->print_error($database->get_error());
660
	}
661
	if($results->numRows() == 0) {
662
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
663
	}
664
	$results_array = $results->fetchRow();
665
	$parent = $results_array['parent'];
666
	$level = $results_array['level'];
667
	$link = $results_array['link'];
668
	$page_title = ($results_array['page_title']);
669
	$menu_title = ($results_array['menu_title']);
670
	
671
	// Get the sections that belong to the page
672
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
673
	if($query_sections->numRows() > 0) {
674
		while($section = $query_sections->fetchRow()) {
675
			// Set section id
676
			$section_id = $section['section_id'];
677
			// Include the modules delete file if it exists
678
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
679
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
680
			}
681
		}
682
	}
683
	
684
	// Update the pages table
685
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
686
	$database->query($query);
687
	if($database->is_error()) {
688
		$admin->print_error($database->get_error());
689
	}
690
	
691
	// Update the sections table
692
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
693
	$database->query($query);
694
	if($database->is_error()) {
695
		$admin->print_error($database->get_error());
696
	}
697
	
698
	// Include the ordering class or clean-up ordering
699
	require_once(WB_PATH.'/framework/class.order.php');
700
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
701
	$order->clean($parent);
702
	
703
	// Unlink the page access file and directory
704
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
705
	$filename = $directory.PAGE_EXTENSION;
706
	$directory .= '/';
707
	if(file_exists($filename)) {
708
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
709
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
710
		} else {
711
			unlink($filename);
712
			if(file_exists($directory) && rtrim($directory,'/')!=WB_PATH.PAGES_DIRECTORY && substr($link, 0, 1) != '.') {
713
				rm_full_dir($directory);
714
			}
715
		}
716
	}
717
	
718
}
719

    
720
// Load module into DB
721
function load_module($directory, $install = false) {
722
	global $database,$admin,$MESSAGE;
723
	if(file_exists($directory.'/info.php')) {
724
		require($directory.'/info.php');
725
		if(isset($module_name)) {
726
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
727
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
728
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
729
			$module_function = strtolower($module_function);
730
			// Check that it doesn't already exist
731
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
732
			if($result->numRows() == 0) {
733
				// Load into DB
734
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
735
				"(directory,name,description,type,function,version,platform,author,license) ".
736
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
737
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
738
				$database->query($query);
739
				// Run installation script
740
				if($install == true) {
741
					if(file_exists($directory.'/install.php')) {
742
						require($directory.'/install.php');
743
					}
744
				}
745
			}
746
		}
747
	}
748
}
749

    
750
// Load template into DB
751
function load_template($directory) {
752
	global $database;
753
	if(file_exists($directory.'/info.php')) {
754
		require($directory.'/info.php');
755
		if(isset($template_name)) {
756
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
757
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
758
			// Check that it doesn't already exist
759
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
760
			if($result->numRows() == 0) {
761
				// Load into DB
762
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
763
				"(directory,name,description,type,version,platform,author,license) ".
764
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
765
				"'$template_version','$template_platform','$template_author','$template_license')";
766
				$database->query($query);
767
			}
768
		}
769
	}
770
}
771

    
772
// Load language into DB
773
function load_language($file) {
774
	global $database;
775
	if(file_exists($file)) {
776
		require($file);
777
		if(isset($language_name)) {
778
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
779
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
780
			// Check that it doesn't already exist
781
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
782
			if($result->numRows() == 0) {
783
				// Load into DB
784
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
785
				"(directory,name,type,version,platform,author,license) ".
786
				"VALUES ('$language_code','$language_name','language',".
787
				"'$language_version','$language_platform','$language_author','$language_license')";
788
	 		$database->query($query);
789
			}
790
		}
791
	}
792
}
793

    
794
// Upgrade module info in DB, optionally start upgrade script
795
function upgrade_module($directory, $upgrade = false) {
796
	global $database, $admin, $MESSAGE;
797
	$directory = WB_PATH . "/modules/$directory";
798
	if(file_exists($directory.'/info.php')) {
799
		require($directory.'/info.php');
800
		if(isset($module_name)) {
801
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
802
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
803
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
804
			$module_function = strtolower($module_function);
805
			// Check that it does already exist
806
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
807
			if($result->numRows() > 0) {
808
				// Update in DB
809
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
810
					"version = '$module_version', " .
811
					"description = '" . addslashes($module_description) . "', " .
812
					"platform = '$module_platform', " .
813
					"author = '$module_author', " .
814
					"license = '$module_license'" .
815
					"WHERE directory = '$module_directory'";
816
				$database->query($query);
817
				// Run upgrade script
818
				if($upgrade == true) {
819
					if(file_exists($directory.'/upgrade.php')) {
820
						require($directory.'/upgrade.php');
821
					}
822
				}
823
			}
824
		}
825
	}
826
}
827

    
828
// extracts the content of a string variable from a string (save alternative to including files)
829
if(!function_exists('get_variable_content')) {
830
	function get_variable_content($search, $data, $striptags=true, $convert_to_entities=true) {
831
		$match = '';
832
		// search for $variable followed by 0-n whitespace then by = then by 0-n whitespace
833
		// then either " or ' then 0-n characters then either " or ' followed by 0-n whitespace and ;
834
		// the variable name is returned in $match[1], the content in $match[3]
835
		if (preg_match('/(\$' .$search .')\s*=\s*("|\')(.*)\2\s*;/', $data, $match)) {
836
			if(strip_tags(trim($match[1])) == '$' .$search) {
837
				// variable name matches, return it's value
838
				$match[3] = ($striptags == true) ? strip_tags($match[3]) : $match[3];
839
				$match[3] = ($convert_to_entities == true) ? htmlentities($match[3]) : $match[3];
840
				return $match[3];
841
			}
842
		}
843
		return false;
844
	}
845
}
846

    
847
?>
(11-11/14)