Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 881 2008-11-22 15:51:23Z 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
// Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
353
// Will replace all numeric and named entities except &gt; &lt; &apos; &quot; &#039; &nbsp;
354
// In case of error the returned string is unchanged, and a message is emitted.
355
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET) {
356
	require_once(WB_PATH.'/framework/functions-utf8.php');
357
	return entities_to_umlauts2($string, $charset_out);
358
}
359

    
360
// Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
361
// In case of error the returned string is unchanged, and a message is emitted.
362
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET) {
363
	require_once(WB_PATH.'/framework/functions-utf8.php');
364
	return umlauts_to_entities2($string, $charset_in);
365
}
366

    
367
// Function to convert a page title to a page filename
368
function page_filename($string) {
369
	require_once(WB_PATH.'/framework/functions-utf8.php');
370
	$string = entities_to_7bit($string);
371
	// Now remove all bad characters
372
	$bad = array(
373
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
374
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
375
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
376
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
377
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
378
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
379
	);
380
	$string = str_replace($bad, '', $string);
381
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
382
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
383
	// Now replace spaces with page spcacer
384
	$string = trim($string);
385
	$string = preg_replace('/(\s)+/', PAGE_SPACER, $string);
386
	// Now convert to lower-case
387
	$string = strtolower($string);
388
	// If there are any weird language characters, this will protect us against possible problems they could cause
389
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
390
	// Finally, return the cleaned string
391
	return $string;
392
}
393

    
394
// Function to convert a desired media filename to a clean filename
395
function media_filename($string) {
396
	require_once(WB_PATH.'/framework/functions-utf8.php');
397
	$string = entities_to_7bit($string);
398
	// Now remove all bad characters
399
	$bad = array(
400
	'\'', // '
401
	'"', // "
402
	'`', // `
403
	'!', // !
404
	'@', // @
405
	'#', // #
406
	'$', // $
407
	'%', // %
408
	'^', // ^
409
	'&', // &
410
	'*', // *
411
	'=', // =
412
	'+', // +
413
	'|', // |
414
	'/', // /
415
	'\\', // \
416
	';', // ;
417
	':', // :
418
	',', // ,
419
	'?' // ?
420
	);
421
	$string = str_replace($bad, '', $string);
422
	// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
423
	$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
424
	// Clean any page spacers at the end of string
425
	$string = trim($string);
426
	// Finally, return the cleaned string
427
	return $string;
428
}
429

    
430
// Function to work out a page link
431
if(!function_exists('page_link')) {
432
	function page_link($link) {
433
		global $admin;
434
		return $admin->page_link($link);
435
	}
436
}
437

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

    
477
// Function for working out a file mime type (if the in-built PHP one is not enabled)
478
if(!function_exists('mime_content_type')) {
479
   function mime_content_type($file) {
480
       $file = escapeshellarg($file);
481
       return trim(`file -bi $file`);
482
   }
483
}
484

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

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

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

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

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

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

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

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

    
833
?>
(11-11/14)