Project

General

Profile

1 238 stefan
<?php
2
3
// $Id$
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8 399 Ruebenwurz
 Copyright (C) 2004-2007, Ryan Djurovich
9 238 stefan
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 286 stefan
	exit(0);
37 238 stefan
}
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, 'file');
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 325 stefan
	// and user is not admin
159
	if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
160 238 stefan
		$query_home_folders = $database->query("SELECT home_folder FROM ".TABLE_PREFIX."users WHERE home_folder != '".$admin->get_home_folder()."'");
161
		if($query_home_folders->numRows() > 0) {
162
			while($folder = $query_home_folders->fetchRow()) {
163
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
164
			}
165
		}
166
		function remove_home_subs($directory = '/', $home_folders) {
167
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) {
168
				// Loop through the dirs to check the home folders sub-dirs are not shown
169
			   while(false !== ($file = readdir($handle))) {
170
					if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
171
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
172
							if($directory != '/') { $file = $directory.'/'.$file; } else { $file = '/'.$file; }
173
							foreach($home_folders AS $hf) {
174
								$hf_length = strlen($hf);
175
								if($hf_length > 0) {
176
									if(substr($file, 0, $hf_length+1) == $hf) {
177
										$home_folders[$file] = $file;
178
									}
179
								}
180
							}
181
							$home_folders = remove_home_subs($file, $home_folders);
182
						}
183
					}
184
				}
185
			}
186
			return $home_folders;
187
		}
188
		$home_folders = remove_home_subs('/', $home_folders);
189
	}
190
	return $home_folders;
191
}
192
193
// Function to create directories
194
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE) {
195
	if(!file_exists($dir_name)) {
196
		$umask = umask(0);
197
		mkdir($dir_name, $dir_mode);
198
		umask($umask);
199
		return true;
200
	} else {
201
		return false;
202
	}
203
}
204
205
// Function to chmod files and directories
206
function change_mode($name) {
207
	if(OPERATING_SYSTEM != 'windows') {
208
		// Only chmod if os is not windows
209
		if(is_dir($name)) {
210
			$mode = OCTAL_DIR_MODE;
211
		} else {
212
			$mode = OCTAL_FILE_MODE;
213
		}
214
		if(file_exists($name)) {
215
			$umask = umask(0);
216
			chmod($name, $mode);
217
			umask($umask);
218
			return true;
219
		} else {
220
			return false;
221
		}
222
	} else {
223
		return true;
224
	}
225
}
226
227
// Function to figure out if a parent exists
228
function is_parent($page_id) {
229
	global $database;
230
	// Get parent
231
	$query = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
232
	$fetch = $query->fetchRow();
233
	// If parent isnt 0 return its ID
234
	if($fetch['parent'] == '0') {
235
		return false;
236
	} else {
237
		return $fetch['parent'];
238
	}
239
}
240
241
// Function to work out level
242
function level_count($page_id) {
243
	global $database;
244
	// Get page parent
245
	$query_page = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
246
	$fetch_page = $query_page->fetchRow();
247
	$parent = $fetch_page['parent'];
248
	if($parent > 0) {
249
		// Get the level of the parent
250
		$query_parent = $database->query("SELECT level FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent' LIMIT 1");
251
		$fetch_parent = $query_parent->fetchRow();
252
		$level = $fetch_parent['level'];
253
		return $level+1;
254
	} else {
255
		return 0;
256
	}
257
}
258
259
// Function to work out root parent
260
function root_parent($page_id) {
261
	global $database;
262
	// Get page details
263
	$query_page = $database->query("SELECT parent,level FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
264
	$fetch_page = $query_page->fetchRow();
265
	$parent = $fetch_page['parent'];
266
	$level = $fetch_page['level'];
267
	if($level == 1) {
268
		return $parent;
269
	} elseif($parent == 0) {
270 400 Ruebenwurz
		return $page_id;
271 238 stefan
	} else {
272
		// Figure out what the root parents id is
273
		$parent_ids = array_reverse(get_parent_ids($page_id));
274
		return $parent_ids[0];
275
	}
276
}
277
278
// Function to get page title
279
function get_page_title($id) {
280
	global $database;
281
	// Get title
282
	$query = $database->query("SELECT page_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
283
	$fetch = $query->fetchRow();
284
	// Return title
285
	return $fetch['page_title'];
286
}
287
288
// Function to get a pages menu title
289
function get_menu_title($id) {
290
	// Connect to the database
291
	$database = new database();
292
	// Get title
293
	$query = $database->query("SELECT menu_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
294
	$fetch = $query->fetchRow();
295
	// Return title
296
	return $fetch['menu_title'];
297
}
298
299
// Function to get all parent page titles
300
function get_parent_titles($parent_id) {
301
	$titles[] = get_menu_title($parent_id);
302
	if(is_parent($parent_id) != false) {
303
		$parent_titles = get_parent_titles(is_parent($parent_id));
304
		$titles = array_merge($titles, $parent_titles);
305
	}
306
	return $titles;
307
}
308
309
// Function to get all parent page id's
310
function get_parent_ids($parent_id) {
311
	$ids[] = $parent_id;
312
	if(is_parent($parent_id) != false) {
313
		$parent_ids = get_parent_ids(is_parent($parent_id));
314
		$ids = array_merge($ids, $parent_ids);
315
	}
316
	return $ids;
317
}
318
319
// Function to genereate page trail
320
function get_page_trail($page_id) {
321
	return implode(',', array_reverse(get_parent_ids($page_id)));
322
}
323
324
// Function to get all sub pages id's
325
function get_subs($parent, $subs) {
326
	// Connect to the database
327
	$database = new database();
328
	// Get id's
329
	$query = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'");
330
	if($query->numRows() > 0) {
331
		while($fetch = $query->fetchRow()) {
332
			$subs[] = $fetch['page_id'];
333
			// Get subs of this sub
334
			$subs = get_subs($fetch['page_id'], $subs);
335
		}
336
	}
337
	// Return subs array
338
	return $subs;
339
}
340
341
// Function to convert a page title to a page filename
342
function page_filename($string) {
343
	// First, translate any non-english characters to their english equivalents
344
	require(WB_PATH.'/framework/convert.php');
345
   $string = strtr($string, $conversion_array);
346
	// Now replace spaces with page spcacer
347
	$string = str_replace(' ', PAGE_SPACER, $string);
348
	// Now remove all bad characters
349
	$bad = array(
350
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
351
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
352
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
353
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
354
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
355
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
356
	);
357
	$string = str_replace($bad, '', $string);
358
	// Now convert to lower-case
359
	$string = strtolower($string);
360
	// Now remove multiple page spacers
361
	$string = str_replace(PAGE_SPACER.PAGE_SPACER, PAGE_SPACER, $string);
362
	// Clean any page spacers at the end of string
363
	$string = str_replace(PAGE_SPACER, ' ', $string);
364
	$string = trim($string);
365
	$string = str_replace(' ', PAGE_SPACER, $string);
366
	// If there are any weird language characters, this will protect us against possible problems they could cause
367
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
368
	// Finally, return the cleaned string
369
	return $string;
370
}
371
372
// Function to convert a desired media filename to a clean filename
373
function media_filename($string) {
374
	// First, translate any non-english characters to their english equivalents
375
	require(WB_PATH.'/framework/convert.php');
376
   $string = strtr($string, $conversion_array);
377
	// Now remove all bad characters
378
	$bad = array(
379
	'\'', // '
380
	'"', // "
381
	'`', // `
382
	'!', // !
383
	'@', // @
384
	'#', // #
385
	'$', // $
386
	'%', // %
387
	'^', // ^
388
	'&', // &
389
	'*', // *
390
	'=', // =
391
	'+', // +
392
	'|', // |
393
	'/', // /
394
	'\\', // \
395
	';', // ;
396
	':', // :
397
	',', // ,
398
	'?' // ?
399
	);
400
	$string = str_replace($bad, '', $string);
401
	// Clean any page spacers at the end of string
402
	$string = trim($string);
403
	// Finally, return the cleaned string
404
	return $string;
405
}
406
407
// Function to work out a page link
408
if(!function_exists('page_link')) {
409
	function page_link($link) {
410 242 stefan
		global $admin;
411
		return $admin->page_link($link);
412 238 stefan
	}
413
}
414
415
// Create a new file in the pages directory
416
function create_access_file($filename,$page_id,$level) {
417
	global $admin;
418
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
419
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
420
	} else {
421
		// First make sure parent folder exists
422
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
423
		$parents = '';
424
		foreach($parent_folders AS $parent_folder) {
425
			if($parent_folder != '/' AND $parent_folder != '') {
426
				$parents .= '/'.$parent_folder;
427
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
428
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
429
				}
430
			}
431
		}
432
		// The depth of the page directory in the directory hierarchy
433
		// '/pages' is at depth 1
434
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
435
		// Work-out how many ../'s we need to get to the index page
436
		$index_location = '';
437
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
438
			$index_location .= '../';
439
		}
440
		$content = ''.
441
'<?php
442
$page_id = '.$page_id.';
443
require("'.$index_location.'config.php");
444
require(WB_PATH."/index.php");
445
?>';
446
		$handle = fopen($filename, 'w');
447
		fwrite($handle, $content);
448
		fclose($handle);
449
		// Chmod the file
450
		change_mode($filename, 'file');
451
	}
452
}
453
454
// Function for working out a file mime type (if the in-built PHP one is not enabled)
455
if(!function_exists('mime_content_type')) {
456
   function mime_content_type($file) {
457
       $file = escapeshellarg($file);
458
       return trim(`file -bi $file`);
459
   }
460
}
461
462
// Generate a thumbnail from an image
463
function make_thumb($source, $destination, $size) {
464
	// Check if GD is installed
465
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
466
		// First figure out the size of the thumbnail
467
		list($original_x, $original_y) = getimagesize($source);
468
		if ($original_x > $original_y) {
469
			$thumb_w = $size;
470
			$thumb_h = $original_y*($size/$original_x);
471
		}
472
		if ($original_x < $original_y) {
473
			$thumb_w = $original_x*($size/$original_y);
474
			$thumb_h = $size;
475
		}
476
		if ($original_x == $original_y) {
477
			$thumb_w = $size;
478
			$thumb_h = $size;
479
		}
480
		// Now make the thumbnail
481
		$source = imageCreateFromJpeg($source);
482
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
483
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
484
		imagejpeg($dst_img, $destination);
485
		// Clear memory
486
		imagedestroy($dst_img);
487
	   imagedestroy($source);
488
	   // Return true
489
	   return true;
490
   } else {
491
   	return false;
492
   }
493
}
494
495
// Function to work-out a single part of an octal permission value
496
function extract_permission($octal_value, $who, $action) {
497
	// Make sure the octal value is 4 chars long
498
	if(strlen($octal_value) == 0) {
499
		$octal_value = '0000';
500
	} elseif(strlen($octal_value) == 1) {
501
		$octal_value = '000'.$octal_value;
502
	} elseif(strlen($octal_value) == 2) {
503
		$octal_value = '00'.$octal_value;
504
	} elseif(strlen($octal_value) == 3) {
505
		$octal_value = '0'.$octal_value;
506
	} elseif(strlen($octal_value) == 4) {
507
		$octal_value = ''.$octal_value;
508
	} else {
509
		$octal_value = '0000';
510
	}
511
	// Work-out what position of the octal value to look at
512
	switch($who) {
513
	case 'u':
514
		$position = '1';
515
		break;
516
	case 'user':
517
		$position = '1';
518
		break;
519
	case 'g':
520
		$position = '2';
521
		break;
522
	case 'group':
523
		$position = '2';
524
		break;
525
	case 'o':
526
		$position = '3';
527
		break;
528
	case 'others':
529
		$position = '3';
530
		break;
531
	}
532
	// Work-out how long the octal value is and ajust acording
533
	if(strlen($octal_value) == 4) {
534
		$position = $position+1;
535
	} elseif(strlen($octal_value) != 3) {
536
		exit('Error');
537
	}
538
	// Now work-out what action the script is trying to look-up
539
	switch($action) {
540
	case 'r':
541
		$action = 'r';
542
		break;
543
	case 'read':
544
		$action = 'r';
545
		break;
546
	case 'w':
547
		$action = 'w';
548
		break;
549
	case 'write':
550
		$action = 'w';
551
		break;
552
	case 'e':
553
		$action = 'e';
554
		break;
555
	case 'execute':
556
		$action = 'e';
557
		break;
558
	}
559
	// Get the value for "who"
560
	$value = substr($octal_value, $position-1, 1);
561
	// Now work-out the details of the value
562
	switch($value) {
563
	case '0':
564
		$r = false;
565
		$w = false;
566
		$e = false;
567
		break;
568
	case '1':
569
		$r = false;
570
		$w = false;
571
		$e = true;
572
		break;
573
	case '2':
574
		$r = false;
575
		$w = true;
576
		$e = false;
577
		break;
578
	case '3':
579
		$r = false;
580
		$w = true;
581
		$e = true;
582
		break;
583
	case '4':
584
		$r = true;
585
		$w = false;
586
		$e = false;
587
		break;
588
	case '5':
589
		$r = true;
590
		$w = false;
591
		$e = true;
592
		break;
593
	case '6':
594
		$r = true;
595
		$w = true;
596
		$e = false;
597
		break;
598
	case '7':
599
		$r = true;
600
		$w = true;
601
		$e = true;
602
		break;
603
	default:
604
		$r = false;
605
		$w = false;
606
		$e = false;
607
	}
608
	// And finally, return either true or false
609
	return $$action;
610
}
611
612
// Function to delete a page
613
function delete_page($page_id) {
614
615
	global $admin, $database;
616
617
	// Find out more about the page
618
	$database = new database();
619
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
620
	$results = $database->query($query);
621
	if($database->is_error()) {
622
		$admin->print_error($database->get_error());
623
	}
624
	if($results->numRows() == 0) {
625
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
626
	}
627
	$results_array = $results->fetchRow();
628
	$parent = $results_array['parent'];
629
	$level = $results_array['level'];
630
	$link = $results_array['link'];
631
	$page_title = ($results_array['page_title']);
632
	$menu_title = ($results_array['menu_title']);
633
634
	// Get the sections that belong to the page
635
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
636
	if($query_sections->numRows() > 0) {
637
		while($section = $query_sections->fetchRow()) {
638
			// Set section id
639
			$section_id = $section['section_id'];
640
			// Include the modules delete file if it exists
641
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
642
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
643
			}
644
		}
645
	}
646
647
	// Update the pages table
648
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
649
	$database->query($query);
650
	if($database->is_error()) {
651
		$admin->print_error($database->get_error());
652
	}
653
654
	// Update the sections table
655
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
656
	$database->query($query);
657
	if($database->is_error()) {
658
		$admin->print_error($database->get_error());
659
	}
660
661
	// Include the ordering class or clean-up ordering
662
	require_once(WB_PATH.'/framework/class.order.php');
663
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
664
	$order->clean($parent);
665
666
	// Unlink the page access file and directory
667
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
668
	$filename = $directory.'.php';
669
	$directory .= '/';
670
	if(file_exists($filename)) {
671
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
672
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
673
		} else {
674
			unlink($filename);
675
			if(file_exists($directory)) {
676
				rm_full_dir($directory);
677
			}
678
		}
679
	}
680
681
}
682
683
// Load module into DB
684
function load_module($directory, $install = false) {
685 249 stefan
	global $database,$admin,$MESSAGE;
686 238 stefan
	if(file_exists($directory.'/info.php')) {
687
		require($directory.'/info.php');
688
		if(isset($module_name)) {
689
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
690
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
691
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
692
			$module_function = strtolower($module_function);
693
			// Check that it doesn't already exist
694
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
695
			if($result->numRows() == 0) {
696
				// Load into DB
697
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
698
				"(directory,name,description,type,function,version,platform,author,license) ".
699
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
700
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
701
				$database->query($query);
702
				// Run installation script
703
				if($install == true) {
704
					if(file_exists($directory.'/install.php')) {
705
						require($directory.'/install.php');
706
					}
707
				}
708
			}
709
		}
710
	}
711
}
712
713
// Load template into DB
714
function load_template($directory) {
715
	global $database;
716
	if(file_exists($directory.'/info.php')) {
717
		require($directory.'/info.php');
718
		if(isset($template_name)) {
719
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
720
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
721
			// Check that it doesn't already exist
722
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
723
			if($result->numRows() == 0) {
724
				// Load into DB
725
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
726
				"(directory,name,description,type,version,platform,author,license) ".
727
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
728
				"'$template_version','$template_platform','$template_author','$template_license')";
729
				$database->query($query);
730
			}
731
		}
732
	}
733
}
734
735
// Load language into DB
736
function load_language($file) {
737
	global $database;
738
	if(file_exists($file)) {
739
		require($file);
740
		if(isset($language_name)) {
741
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
742
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
743
			// Check that it doesn't already exist
744
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
745
			if($result->numRows() == 0) {
746
				// Load into DB
747
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
748
				"(directory,name,type,version,platform,author,license) ".
749
				"VALUES ('$language_code','$language_name','language',".
750
				"'$language_version','$language_platform','$language_author','$language_license')";
751
	 		$database->query($query);
752
			}
753
		}
754
	}
755
}
756
757 373 Ruebenwurz
// Upgrade module info in DB, optionally start upgrade script
758
function upgrade_module($directory, $upgrade = false) {
759
	global $database, $admin, $MESSAGE;
760
	$directory = WB_PATH . "/modules/$directory";
761
	if(file_exists($directory.'/info.php')) {
762
		require($directory.'/info.php');
763
		if(isset($module_name)) {
764
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
765
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
766
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
767
			$module_function = strtolower($module_function);
768
			// Check that it does already exist
769
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
770
			if($result->numRows() > 0) {
771
				// Update in DB
772
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
773
					"version = '$module_version', " .
774
					"description = '" . addslashes($module_description) . "', " .
775
					"platform = '$module_platform', " .
776
					"author = '$module_author', " .
777
					"license = '$module_license'" .
778
					"WHERE directory = '$module_directory'";
779
				$database->query($query);
780
				// Run upgrade script
781
				if($upgrade == true) {
782
					if(file_exists($directory.'/upgrade.php')) {
783
						require($directory.'/upgrade.php');
784
					}
785
				}
786
			}
787
		}
788
	}
789
}
790
791 399 Ruebenwurz
?>