Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 672 2008-02-08 17:41:40Z 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 replace spaces with page spcacer
385
	$string = trim($string);
386
	$string = preg_replace('/(\s)+/', PAGE_SPACER, $string);
387
	// Now remove all bad characters
388
	$bad = array(
389
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
390
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
391
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
392
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
393
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
394
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
395
	);
396
	$string = str_replace($bad, '', $string);
397
	// Now convert to lower-case
398
	$string = strtolower($string);
399
	// If there are any weird language characters, this will protect us against possible problems they could cause
400
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
401
	// Finally, return the cleaned string
402
	return $string;
403
}
404

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

    
440
// Function to work out a page link
441
if(!function_exists('page_link')) {
442
	function page_link($link) {
443
		global $admin;
444
		return $admin->page_link($link);
445
	}
446
}
447

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

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

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

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

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

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

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

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

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

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

    
843
?>
(11-11/13)