Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 465 2007-05-19 08:35:08Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2007, 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, '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
	// and user is not admin
159
	if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
160
		$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
		return $page_id;
271
	} 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 as replecement for php's htmlspecialchars()
342
function my_htmlspecialchars($string) {
343
	$string = umlauts_to_entities($string);
344
	$string = entities_to_umlauts($string, DEFAULT_CHARSET, 1);
345
	return($string);
346
}
347

    
348
// Function to convert a string from $from- to $to-encoding, using mysql
349
function my_mysql_iconv($string, $from, $to) {
350
	// keep current character set values:
351
	$character_set_database = mysql_result(mysql_query("SELECT @@character_set_client"),0,0);
352
	$character_set_results = mysql_result(mysql_query("SELECT @@character_set_results"),0,0);
353
	$collation_results = mysql_result(mysql_query("SELECT @@collation_connection"),0,0);
354
	mysql_query("SET character_set_client=$from");
355
	mysql_query("SET character_set_results=$to");
356
	mysql_query("SET collation_connection=utf8_unicode_ci");
357
	$string_escaped = mysql_real_escape_string($string);
358
	$converted_string = mysql_result(mysql_query("SELECT '$string_escaped'"),0,0);
359
	// restore previous character set values:
360
	mysql_query("SET character_set_client=$character_set_database");
361
	mysql_query("SET character_set_results=$character_set_results");
362
	mysql_query("SET collation_connection=$collation_results");
363
	return $converted_string;
364
}
365

    
366
// Function as wrapper for mb_convert_encoding
367
// converts $charset_in to $charset_out or 
368
// UTF-8 to HTML-ENTITIES or HTML-ENTITIES to UTF-8
369
function mb_convert_encoding_wrapper($string, $charset_out, $charset_in) {
370
	if ($charset_out == $charset_in) {
371
		return $string;
372
	}
373
	// try mb_convert_encoding(). This can handle to or from HTML-ENTITIES, too
374
	if (function_exists('mb_convert_encoding')) {
375
		// there's no GB2312 or ISO-8859-11 encoding in php's mb_* functions
376
		if ($charset_in=='ISO-8859-11' || $charset_in=='GB2312') {
377
			if (function_exists('iconv')) {
378
				$string = iconv($charset_in, 'UTF-8', $string);
379
			}
380
			else {
381
				if ($charset_in == 'GB2312') {
382
					$string=my_mysql_iconv($string, 'gb2312', 'utf8');
383
				} else {
384
					$string=my_mysql_iconv($string, 'tis620', 'utf8');
385
				}
386
			}
387
			$charset_in='UTF-8';
388
			if ($charset_out == 'UTF-8') {
389
				return $string;
390
			}
391
		}
392
		if ($charset_out=='ISO-8859-11' || $charset_out=='GB2312') {
393
			$string=mb_convert_encoding($string, 'UTF-8', $charset_in);
394
			if (function_exists('iconv')) {
395
				$string = iconv('UTF-8', $charset_out, $string);
396
			}
397
			else {
398
				if ($charset_out == 'GB2312') {
399
					$string=my_mysql_iconv($string, 'utf8', 'gb2312');
400
				} else {
401
					$string=my_mysql_iconv($string, 'utf8', 'tis620');
402
				}
403
			}
404
		} else {
405
			$string=mb_convert_encoding($string, $charset_out, $charset_in);
406
		}
407
		return $string;
408
	}
409

    
410
	// try iconv(). This can't handle to or from HTML-ENTITIES.
411
	if (function_exists('iconv') && $charset_out!='HTML-ENTITIES' && $charset_in!='HTML-ENTITIES' ) {
412
		$string = iconv($charset_in, $charset_out, $string);
413
		return $string;
414
	}
415

    
416
	// do the UTF-8->HTML-ENTITIES or HTML-ENTITIES->UTF-8 translation
417
	if (($charset_in=='HTML-ENTITIES' && $charset_out=='UTF-8') || ($charset_in=='UTF-8' && $charset_out=='HTML-ENTITIES')) {
418
		$named_to_numbered_entities=array(
419
			'&nbsp;'=>'&#160;','&iexcl;'=>'&#161;','&cent;'=>'&#162;','&pound;'=>'&#163;','&curren;'=>'&#164;',
420
			'&yen;'=>'&#165;','&brvbar;'=>'&#166;','&sect;'=>'&#167;','&uml;'=>'&#168;','&ordf;'=>'&#170;',
421
			'&laquo;'=>'&#171;','&not;'=>'&#172;','&shy;'=>'&#173;','&reg;'=>'&#174;','&macr;'=>'&#175;',
422
			'&deg;'=>'&#176;','&plusmn;'=>'&#177;','&sup2;'=>'&#178;','&sup3;'=>'&#179;','&acute;'=>'&#180;',
423
			'&micro;'=>'&#181;','&para;'=>'&#182;','&middot;'=>'&#183;','&cedil;'=>'&#184;','&sup1;'=>'&#185;',
424
			'&ordm;'=>'&#186;','&raquo;'=>'&#187;','&frac14;'=>'&#188;','&frac12;'=>'&#189;','&frac34;'=>'&#190;',
425
			'&iquest;'=>'&#191;','&divide;'=>'&#247;','&empty;'=>'&#8709;','&euro;'=>'&#8364;',
426
			'&Aacute;'=>'&#193;','&aacute;'=>'&#225;','&Acirc;'=>'&#194;',
427
			'&acirc;'=>'&#226;','&AElig;'=>'&#198;','&aelig;'=>'&#230;','&Agrave;'=>'&#192;','&agrave;'=>'&#224;',
428
			'&Aring;'=>'&#197;','&aring;'=>'&#229;','&Atilde;'=>'&#195;','&atilde;'=>'&#227;','&Auml;'=>'&#196;',
429
			'&auml;'=>'&#228;','&Ccedil;'=>'&#199;','&ccedil;'=>'&#231;','&Eacute;'=>'&#201;','&eacute;'=>'&#233;',
430
			'&Ecirc;'=>'&#202;','&ecirc;'=>'&#234;','&Egrave;'=>'&#200;','&egrave;'=>'&#232;','&Euml;'=>'&#203;',
431
			'&euml;'=>'&#235;','&Iacute;'=>'&#205;','&iacute;'=>'&#237;','&Icirc;'=>'&#206;','&icirc;'=>'&#238;',
432
			'&Igrave;'=>'&#204;','&igrave;'=>'&#236;','&Iuml;'=>'&#207;','&iuml;'=>'&#239;','&Ntilde;'=>'&#209;',
433
			'&ntilde;'=>'&#241;','&Oacute;'=>'&#211;','&oacute;'=>'&#243;','&Ocirc;'=>'&#212;','&ocirc;'=>'&#244;',
434
			'&OElig;'=>'&#338;','&oelig;'=>'&#339;','&Ograve;'=>'&#210;','&ograve;'=>'&#242;','&Otilde;'=>'&#213;',
435
			'&otilde;'=>'&#245;','&Ouml;'=>'&#214;','&ouml;'=>'&#246;','&Scaron;'=>'&#352;','&scaron;'=>'&#353;',
436
			'&szlig;'=>'&#223;','&Uacute;'=>'&#218;','&uacute;'=>'&#250;','&Ucirc;'=>'&#219;','&ucirc;'=>'&#251;',
437
			'&Ugrave;'=>'&#217;','&ugrave;'=>'&#249;','&Uuml;'=>'&#220;','&uuml;'=>'&#252;','&Yacute;'=>'&#221;',
438
			'&yacute;'=>'&#253;','&Yuml;'=>'&#376;','&yuml;'=>'&#255;','&copy;'=>'&#169;','&reg;'=>'&#174;',
439
			'&ETH;'=>'&#208;','&times;'=>'&#215;','&Oslash;'=>'&#216;','&THORN;'=>'&#222;','&eth;'=>'&#240;',
440
			'&oslash;'=>'&#248;','&thorn;'=>'&#254;');
441
		$numbered_to_named_entities=array('&#193;'=>'&Aacute;','&#225;'=>'&aacute;','&#194;'=>'&Acirc;',
442
			'&#160;'=>'&nbsp;','&#161;'=>'&iexcl;','&#162;'=>'&cent;','&#163;'=>'&pound;','&#164;'=>'&curren;',
443
			'&#165;'=>'&yen;','&#166;'=>'&brvbar;','&#167;'=>'&sect;','&#168;'=>'&uml;','&#170;'=>'&ordf;',
444
			'&#171;'=>'&laquo;','&#172;'=>'&not;','&#173;'=>'&shy;','&#174;'=>'&reg;','&#175;'=>'&macr;',
445
			'&#176;'=>'&deg;','&#177;'=>'&plusmn;','&#178;'=>'&sup2;','&#179;'=>'&sup3;','&#180;'=>'&acute;',
446
			'&#181;'=>'&micro;','&#182;'=>'&para;','&#183;'=>'&middot;','&#184;'=>'&cedil;','&#185;'=>'&sup1;',
447
			'&#186;'=>'&ordm;','&#187;'=>'&raquo;','&#188;'=>'&frac14;','&#189;'=>'&frac12;','&#190;'=>'&frac34;',
448
			'&#191;'=>'&iquest;','&#247;'=>'&divide;','&#8709;'=>'&empty;','&#8364;'=>'&euro;',
449
			'&#226;'=>'&acirc;','&#198;'=>'&AElig;','&#230;'=>'&aelig;','&#192;'=>'&Agrave;','&#224;'=>'&agrave;',
450
			'&#197;'=>'&Aring;','&#229;'=>'&aring;','&#195;'=>'&Atilde;','&#227;'=>'&atilde;','&#196;'=>'&Auml;',
451
			'&#228;'=>'&auml;','&#199;'=>'&Ccedil;','&#231;'=>'&ccedil;','&#201;'=>'&Eacute;','&#233;'=>'&eacute;',
452
			'&#202;'=>'&Ecirc;','&#234;'=>'&ecirc;','&#200;'=>'&Egrave;','&#232;'=>'&egrave;','&#203;'=>'&Euml;',
453
			'&#235;'=>'&euml;','&#205;'=>'&Iacute;','&#237;'=>'&iacute;','&#206;'=>'&Icirc;','&#238;'=>'&icirc;',
454
			'&#204;'=>'&Igrave;','&#236;'=>'&igrave;','&#207;'=>'&Iuml;','&#239;'=>'&iuml;','&#209;'=>'&Ntilde;',
455
			'&#241;'=>'&ntilde;','&#211;'=>'&Oacute;','&#243;'=>'&oacute;','&#212;'=>'&Ocirc;','&#244;'=>'&ocirc;',
456
			'&#338;'=>'&OElig;','&#339;'=>'&oelig;','&#210;'=>'&Ograve;','&#242;'=>'&ograve;','&#213;'=>'&Otilde;',
457
			'&#245;'=>'&otilde;','&#214;'=>'&Ouml;','&#246;'=>'&ouml;','&#352;'=>'&Scaron;','&#353;'=>'&scaron;',
458
			'&#223;'=>'&szlig;','&#218;'=>'&Uacute;','&#250;'=>'&uacute;','&#219;'=>'&Ucirc;','&#251;'=>'&ucirc;',
459
			'&#217;'=>'&Ugrave;','&#249;'=>'&ugrave;','&#220;'=>'&Uuml;','&#252;'=>'&uuml;','&#221;'=>'&Yacute;',
460
			'&#253;'=>'&yacute;','&#376;'=>'&Yuml;','&#255;'=>'&yuml;','&#169;'=>'&copy;','&#174;'=>'&reg;',
461
			'&#208;'=>'&ETH;','&#215;'=>'&times;','&#216;'=>'&Oslash;','&#222;'=>'&THORN;','&#240;'=>'&eth;',
462
			'&#248;'=>'&oslash;','&#254;'=>'&thorn;');
463
		if ($charset_in == 'HTML-ENTITIES') {
464
			$string = strtr($string, $named_to_numbered_entities);
465
			$string = preg_replace("/&#([0-9]+);/e", "code_to_utf8($1)", $string);
466
		}
467
		elseif ($charset_out == 'HTML-ENTITIES') {
468
			$string = preg_replace("/&#([0-9]+);/e", "code_to_utf8($1)", $string);
469
			$char = "";
470
			while (strlen($string) > 0) {
471
				preg_match("/^(.)(.*)$/su", $string, $match);
472
				if (strlen($match[1]) > 1) {
473
					$char .= "&#".uniord($match[1]).";";
474
				} else $char .= $match[1];
475
				$string = $match[2];
476
			}
477
			$string = $char;
478
			$string_htmlspecialchars_decode=array("&lt;"=>"<", "&gt;"=>">", "&amp;"=>"&", "&quot;"=>"\"", "&#039;"=>"\'");
479
			$string = strtr($string, $string_htmlspecialchars_decode);
480
			$string = strtr($string, $numbered_to_named_entities);
481
		}
482
		return $string;
483
	}
484

    
485
	// mb_convert_encoding() and iconv() aren't available, so use my_mysql_iconv()
486
	if ($charset_in == 'ISO-8859-1') { $mysqlcharset_from = 'latin1'; }
487
	elseif ($charset_in == 'ISO-8859-2') { $mysqlcharset_from = 'latin2'; }
488
	elseif ($charset_in == 'ISO-8859-3') { $mysqlcharset_from = 'latin1'; }
489
	elseif ($charset_in == 'ISO-8859-4') { $mysqlcharset_from = 'latin7'; }
490
	elseif ($charset_in == 'ISO-8859-5') { $string = convert_cyr_string ($string, "iso8859-5", "windows-1251" ); $mysqlcharset_from = 'cp1251'; }
491
	elseif ($charset_in == 'ISO-8859-6') { $mysqlcharset_from = ''; } //?
492
	elseif ($charset_in == 'ISO-8859-7') { $mysqlcharset_from = 'greek'; }
493
	elseif ($charset_in == 'ISO-8859-8') { $mysqlcharset_from = 'hebrew'; }
494
	elseif ($charset_in == 'ISO-8859-9') { $mysqlcharset_from = 'latin5'; }
495
	elseif ($charset_in == 'ISO-8859-10') { $mysqlcharset_from = 'latin1'; }
496
	elseif ($charset_in == 'BIG5') { $mysqlcharset_from = 'big5'; }
497
	elseif ($charset_in == 'ISO-2022-JP') { $mysqlcharset_from = ''; } //?
498
	elseif ($charset_in == 'ISO-2022-KR') { $mysqlcharset_from = ''; } //?
499
	elseif ($charset_in == 'GB2312') { $mysqlcharset_from = 'gb2312'; }
500
	elseif ($charset_in == 'ISO-8859-11') { $mysqlcharset_from = 'tis620'; }
501
	elseif ($charset_in == 'UTF-8') { $mysqlcharset_from = 'utf8'; }
502
	else { $mysqlcharset_from = 'latin1'; }
503

    
504
	if ($charset_out == 'ISO-8859-1') { $mysqlcharset_to = 'latin1'; }
505
	elseif ($charset_out == 'ISO-8859-2') { $mysqlcharset_to = 'latin2'; }
506
	elseif ($charset_out == 'ISO-8859-3') { $mysqlcharset_to = 'latin1'; }
507
	elseif ($charset_out == 'ISO-8859-4') { $mysqlcharset_to = 'latin7'; }
508
	elseif ($charset_out == 'ISO-8859-5') { $mysqlcharset_to = 'cp1251'; } // use convert_cyr_string afterwards
509
	elseif ($charset_out == 'ISO-8859-6') { $mysqlcharset_to = ''; } //?
510
	elseif ($charset_out == 'ISO-8859-7') { $mysqlcharset_to = 'greek'; }
511
	elseif ($charset_out == 'ISO-8859-8') { $mysqlcharset_to = 'hebrew'; }
512
	elseif ($charset_out == 'ISO-8859-9') { $mysqlcharset_to = 'latin5'; }
513
	elseif ($charset_out == 'ISO-8859-10') { $mysqlcharset_to = 'latin1'; }
514
	elseif ($charset_out == 'BIG5') { $mysqlcharset_to = 'big5'; }
515
	elseif ($charset_out == 'ISO-2022-JP') { $mysqlcharset_to = ''; } //?
516
	elseif ($charset_out == 'ISO-2022-KR') { $mysqlcharset_to = ''; } //?
517
	elseif ($charset_out == 'GB2312') { $mysqlcharset_to = 'gb2312'; }
518
	elseif ($charset_out == 'ISO-8859-11') { $mysqlcharset_to = 'tis620'; }
519
	elseif ($charset_out == 'UTF-8') { $mysqlcharset_to = 'utf8'; }
520
	else { $mysqlcharset_to = 'latin1'; }
521

    
522
	if ($mysqlcharset_from!="" && $mysqlcharset_to!="" && $mysqlcharset_from!=$mysqlcharset_to) {
523
		$string=my_mysql_iconv($string, $mysqlcharset_from, $mysqlcharset_to);
524
		if ($mysqlcharset_to == 'cp1251') { 
525
			$string = convert_cyr_string ($string, "windows-1251", "iso-8859-5" );
526
		}
527
		return($string);
528
	}
529

    
530
	// $string is unchanged. This will happen if we have to deal with ISO-8859-6 or ISO-2022-JP or -KR
531
	// and mbstring _and_ iconv aren't available.
532
	return $string;
533
}
534
// support-function for mb_convert_encoding_wrapper()
535
function uniord($c) {
536
        $ud = 0;
537
        if (ord($c{0}) >= 0 && ord($c{0}) <= 127) $ud = ord($c{0});
538
        if (ord($c{0}) >= 192 && ord($c{0}) <= 223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
539
        if (ord($c{0}) >= 224 && ord($c{0}) <= 239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
540
        if (ord($c{0}) >= 240 && ord($c{0}) <= 247) $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
541
        if (ord($c{0}) >= 248 && ord($c{0}) <= 251) $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
542
        if (ord($c{0}) >= 252 && ord($c{0}) <= 253) $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
543
        if (ord($c{0}) >= 254 && ord($c{0}) <= 255) $ud = false; // error
544
        return $ud;
545
}
546
// support-function for mb_convert_encoding_wrapper()
547
function code_to_utf8($num) {
548
	if ($num <= 0x7F) {
549
		return chr($num);
550
	} elseif ($num <= 0x7FF) {
551
		return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
552
	} elseif ($num <= 0xFFFF) {
553
		 return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
554
	} elseif ($num <= 0x1FFFFF) {
555
		return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
556
	}
557
	return " ";
558
}
559

    
560
// Function to convert a string from mixed html-entities/umlauts to pure utf-8-umlauts
561
function string_to_utf8($string, $charset=DEFAULT_CHARSET) {
562
	$charset = strtoupper($charset);
563
	if ($charset == '') { $charset = 'ISO-8859-1'; }
564

    
565
	if (!is_UTF8($string)) {
566
		$string=mb_convert_encoding_wrapper($string, 'UTF-8', $charset);
567
	}
568
	// check if we really get UTF-8. We don't get UTF-8 if charset is ISO-8859-6 or ISO-2022-JP/KR
569
	// and mb_string AND iconv aren't available.
570
	if (is_UTF8($string)) {
571
		$string=mb_convert_encoding_wrapper($string, 'HTML-ENTITIES', 'UTF-8');
572
		$string=mb_convert_encoding_wrapper($string, 'UTF-8', 'HTML-ENTITIES');
573
	} else {
574
		// nothing we can do here :-(
575
	}
576
	return($string);
577
}
578

    
579
// function to check if a string is UTF-8
580
function is_UTF8 ($str) {
581
	if (strlen($str) < 4000) {
582
		// see http://bugs.php.net/bug.php?id=24460 and http://bugs.php.net/bug.php?id=27070 and http://ilia.ws/archives/5-Top-10-ways-to-crash-PHP.html for this.
583
		// 4000 works for me ...
584
		return preg_match('/^(?:[\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*$/s', $str);
585
	}	else {
586
		$isUTF8 = true;
587
		while($str{0}) {
588
			if (preg_match("/^[\x09\x0A\x0D\x20-\x7E]/", $str)) { $str = substr($str, 1); continue; }
589
			if (preg_match("/^[\xC2-\xDF][\x80-\xBF]/", $str)) { $str = substr($str, 2); continue; }
590
			if (preg_match("/^\xE0[\xA0-\xBF][\x80-\xBF]/", $str)) { $str = substr($str, 3); continue; }
591
			if (preg_match("/^[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}/", $str)) { $str = substr($str, 3); continue; }
592
			if (preg_match("/^\xED[\x80-\x9F][\x80-\xBF]/", $str)) { $str = substr($str, 3); continue; }
593
			if (preg_match("/^\xF0[\x90-\xBF][\x80-\xBF]{2}/", $str)) { $str = substr($str, 4); continue; }
594
			if (preg_match("/^[\xF1-\xF3][\x80-\xBF]{3}/", $str)) { $str = substr($str, 4); continue; }
595
			if (preg_match("/^\xF4[\x80-\x8F][\x80-\xBF]{2}/", $str)) { $str = substr($str, 4); continue; }
596
			if (preg_match("/^$/", $str)) { break; }
597
			$isUTF8 = false;
598
			break;
599
		}
600
		return ($isUTF8);
601
	}
602
}
603

    
604
// Function to convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
605
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET, $convert_htmlspecialchars=0) {
606
	$charset_out = strtoupper($charset_out);
607
	if ($charset_out == '') { $charset_out = 'ISO-8859-1'; }
608
	$string = string_to_utf8($string);
609
	if($convert_htmlspecialchars == 1) {
610
		$string=htmlspecialchars($string);
611
	}
612
	if($charset_out!='UTF-8' && is_UTF8($string)) {
613
		$string=mb_convert_encoding_wrapper($string, $charset_out, 'UTF-8');
614
	}
615
	return($string);
616
}
617

    
618
// Function to convert a string from mixed html-entitites/$charset_in-umlauts to pure html-entities
619
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET, $convert_htmlspecialchars=1) {
620
	$charset_in = strtoupper($charset_in);
621
	if ($charset_in == "") { $charset_in = 'ISO-8859-1'; }
622
	$string = string_to_utf8($string, $charset_in);
623
	if($convert_htmlspecialchars == 1) {
624
		$string=htmlspecialchars($string,ENT_QUOTES);
625
	}
626
	if (is_UTF8($string)) {
627
		$string=mb_convert_encoding_wrapper($string,'HTML-ENTITIES','UTF-8');
628
	}
629
	return($string);
630
}
631

    
632
// translate any latin/greek/cyrillic html-entities to their plain 7bit equivalents
633
// and numbered-entities into hex
634
function entities_to_7bit($string) {
635
	require(WB_PATH.'/framework/convert.php');
636
	$string = strtr($string, $conversion_array);
637
	$string = preg_replace('/&#([0-9]+);/e', "dechex('$1')",  $string);
638
	return($string);
639
}
640

    
641
// Function to convert a page title to a page filename
642
function page_filename($string) {
643
	$string = entities_to_7bit(umlauts_to_entities($string));
644
	// Now replace spaces with page spcacer
645
	$string = str_replace(' ', PAGE_SPACER, $string);
646
	// Now remove all bad characters
647
	$bad = array(
648
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
649
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
650
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
651
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
652
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
653
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
654
	);
655
	$string = str_replace($bad, '', $string);
656
	// Now convert to lower-case
657
	$string = strtolower($string);
658
	// Now remove multiple page spacers
659
	$string = str_replace(PAGE_SPACER.PAGE_SPACER, PAGE_SPACER, $string);
660
	// Clean any page spacers at the end of string
661
	$string = str_replace(PAGE_SPACER, ' ', $string);
662
	$string = trim($string);
663
	$string = str_replace(' ', PAGE_SPACER, $string);
664
	// If there are any weird language characters, this will protect us against possible problems they could cause
665
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
666
	// Finally, return the cleaned string
667
	return $string;
668
}
669

    
670
// Function to convert a desired media filename to a clean filename
671
function media_filename($string) {
672
	$string = entities_to_7bit(umlauts_to_entities($string));
673
	// Now remove all bad characters
674
	$bad = array(
675
	'\'', // '
676
	'"', // "
677
	'`', // `
678
	'!', // !
679
	'@', // @
680
	'#', // #
681
	'$', // $
682
	'%', // %
683
	'^', // ^
684
	'&', // &
685
	'*', // *
686
	'=', // =
687
	'+', // +
688
	'|', // |
689
	'/', // /
690
	'\\', // \
691
	';', // ;
692
	':', // :
693
	',', // ,
694
	'?' // ?
695
	);
696
	$string = str_replace($bad, '', $string);
697
	// Clean any page spacers at the end of string
698
	$string = trim($string);
699
	// Finally, return the cleaned string
700
	return $string;
701
}
702

    
703
// Function to work out a page link
704
if(!function_exists('page_link')) {
705
	function page_link($link) {
706
		global $admin;
707
		return $admin->page_link($link);
708
	}
709
}
710

    
711
// Create a new file in the pages directory
712
function create_access_file($filename,$page_id,$level) {
713
	global $admin;
714
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
715
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
716
	} else {
717
		// First make sure parent folder exists
718
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
719
		$parents = '';
720
		foreach($parent_folders AS $parent_folder) {
721
			if($parent_folder != '/' AND $parent_folder != '') {
722
				$parents .= '/'.$parent_folder;
723
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
724
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
725
				}
726
			}	
727
		}
728
		// The depth of the page directory in the directory hierarchy
729
		// '/pages' is at depth 1
730
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
731
		// Work-out how many ../'s we need to get to the index page
732
		$index_location = '';
733
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
734
			$index_location .= '../';
735
		}
736
		$content = ''.
737
'<?php
738
$page_id = '.$page_id.';
739
require("'.$index_location.'config.php");
740
require(WB_PATH."/index.php");
741
?>';
742
		$handle = fopen($filename, 'w');
743
		fwrite($handle, $content);
744
		fclose($handle);
745
		// Chmod the file
746
		change_mode($filename, 'file');
747
	}
748
}
749

    
750
// Function for working out a file mime type (if the in-built PHP one is not enabled)
751
if(!function_exists('mime_content_type')) {
752
   function mime_content_type($file) {
753
       $file = escapeshellarg($file);
754
       return trim(`file -bi $file`);
755
   }
756
}
757

    
758
// Generate a thumbnail from an image
759
function make_thumb($source, $destination, $size) {
760
	// Check if GD is installed
761
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
762
		// First figure out the size of the thumbnail
763
		list($original_x, $original_y) = getimagesize($source);
764
		if ($original_x > $original_y) {
765
			$thumb_w = $size;
766
			$thumb_h = $original_y*($size/$original_x);
767
		}
768
		if ($original_x < $original_y) {
769
			$thumb_w = $original_x*($size/$original_y);
770
			$thumb_h = $size;
771
		}
772
		if ($original_x == $original_y) {
773
			$thumb_w = $size;
774
			$thumb_h = $size;	
775
		}
776
		// Now make the thumbnail
777
		$source = imageCreateFromJpeg($source);
778
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
779
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
780
		imagejpeg($dst_img, $destination);
781
		// Clear memory
782
		imagedestroy($dst_img);
783
	   imagedestroy($source);
784
	   // Return true
785
	   return true;
786
   } else {
787
   	return false;
788
   }
789
}
790

    
791
// Function to work-out a single part of an octal permission value
792
function extract_permission($octal_value, $who, $action) {
793
	// Make sure the octal value is 4 chars long
794
	if(strlen($octal_value) == 0) {
795
		$octal_value = '0000';
796
	} elseif(strlen($octal_value) == 1) {
797
		$octal_value = '000'.$octal_value;
798
	} elseif(strlen($octal_value) == 2) {
799
		$octal_value = '00'.$octal_value;
800
	} elseif(strlen($octal_value) == 3) {
801
		$octal_value = '0'.$octal_value;
802
	} elseif(strlen($octal_value) == 4) {
803
		$octal_value = ''.$octal_value;
804
	} else {
805
		$octal_value = '0000';
806
	}
807
	// Work-out what position of the octal value to look at
808
	switch($who) {
809
	case 'u':
810
		$position = '1';
811
		break;
812
	case 'user':
813
		$position = '1';
814
		break;
815
	case 'g':
816
		$position = '2';
817
		break;
818
	case 'group':
819
		$position = '2';
820
		break;
821
	case 'o':
822
		$position = '3';
823
		break;
824
	case 'others':
825
		$position = '3';
826
		break;
827
	}
828
	// Work-out how long the octal value is and ajust acording
829
	if(strlen($octal_value) == 4) {
830
		$position = $position+1;
831
	} elseif(strlen($octal_value) != 3) {
832
		exit('Error');
833
	}
834
	// Now work-out what action the script is trying to look-up
835
	switch($action) {
836
	case 'r':
837
		$action = 'r';
838
		break;
839
	case 'read':
840
		$action = 'r';
841
		break;
842
	case 'w':
843
		$action = 'w';
844
		break;
845
	case 'write':
846
		$action = 'w';
847
		break;
848
	case 'e':
849
		$action = 'e';
850
		break;
851
	case 'execute':
852
		$action = 'e';
853
		break;
854
	}
855
	// Get the value for "who"
856
	$value = substr($octal_value, $position-1, 1);
857
	// Now work-out the details of the value
858
	switch($value) {
859
	case '0':
860
		$r = false;
861
		$w = false;
862
		$e = false;
863
		break;
864
	case '1':
865
		$r = false;
866
		$w = false;
867
		$e = true;
868
		break;
869
	case '2':
870
		$r = false;
871
		$w = true;
872
		$e = false;
873
		break;
874
	case '3':
875
		$r = false;
876
		$w = true;
877
		$e = true;
878
		break;
879
	case '4':
880
		$r = true;
881
		$w = false;
882
		$e = false;
883
		break;
884
	case '5':
885
		$r = true;
886
		$w = false;
887
		$e = true;
888
		break;
889
	case '6':
890
		$r = true;
891
		$w = true;
892
		$e = false;
893
		break;
894
	case '7':
895
		$r = true;
896
		$w = true;
897
		$e = true;
898
		break;
899
	default:
900
		$r = false;
901
		$w = false;
902
		$e = false;
903
	}
904
	// And finally, return either true or false
905
	return $$action;
906
}
907

    
908
// Function to delete a page
909
function delete_page($page_id) {
910
	
911
	global $admin, $database;
912
	
913
	// Find out more about the page
914
	$database = new database();
915
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
916
	$results = $database->query($query);
917
	if($database->is_error()) {
918
		$admin->print_error($database->get_error());
919
	}
920
	if($results->numRows() == 0) {
921
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
922
	}
923
	$results_array = $results->fetchRow();
924
	$parent = $results_array['parent'];
925
	$level = $results_array['level'];
926
	$link = $results_array['link'];
927
	$page_title = ($results_array['page_title']);
928
	$menu_title = ($results_array['menu_title']);
929
	
930
	// Get the sections that belong to the page
931
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
932
	if($query_sections->numRows() > 0) {
933
		while($section = $query_sections->fetchRow()) {
934
			// Set section id
935
			$section_id = $section['section_id'];
936
			// Include the modules delete file if it exists
937
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
938
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
939
			}
940
		}
941
	}
942
	
943
	// Update the pages table
944
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
945
	$database->query($query);
946
	if($database->is_error()) {
947
		$admin->print_error($database->get_error());
948
	}
949
	
950
	// Update the sections table
951
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
952
	$database->query($query);
953
	if($database->is_error()) {
954
		$admin->print_error($database->get_error());
955
	}
956
	
957
	// Include the ordering class or clean-up ordering
958
	require_once(WB_PATH.'/framework/class.order.php');
959
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
960
	$order->clean($parent);
961
	
962
	// Unlink the page access file and directory
963
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
964
	$filename = $directory.'.php';
965
	$directory .= '/';
966
	if(file_exists($filename) && substr($filename,0,1<>'.')) {
967
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
968
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
969
		} else {
970
			unlink($filename);
971
			if(file_exists($directory)) {
972
				rm_full_dir($directory);
973
			}
974
		}
975
	}
976
	
977
}
978

    
979
// Load module into DB
980
function load_module($directory, $install = false) {
981
	global $database,$admin,$MESSAGE;
982
	if(file_exists($directory.'/info.php')) {
983
		require($directory.'/info.php');
984
		if(isset($module_name)) {
985
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
986
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
987
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
988
			$module_function = strtolower($module_function);
989
			// Check that it doesn't already exist
990
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
991
			if($result->numRows() == 0) {
992
				// Load into DB
993
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
994
				"(directory,name,description,type,function,version,platform,author,license) ".
995
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
996
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
997
				$database->query($query);
998
				// Run installation script
999
				if($install == true) {
1000
					if(file_exists($directory.'/install.php')) {
1001
						require($directory.'/install.php');
1002
					}
1003
				}
1004
			}
1005
		}
1006
	}
1007
}
1008

    
1009
// Load template into DB
1010
function load_template($directory) {
1011
	global $database;
1012
	if(file_exists($directory.'/info.php')) {
1013
		require($directory.'/info.php');
1014
		if(isset($template_name)) {
1015
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
1016
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
1017
			// Check that it doesn't already exist
1018
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
1019
			if($result->numRows() == 0) {
1020
				// Load into DB
1021
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
1022
				"(directory,name,description,type,version,platform,author,license) ".
1023
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
1024
				"'$template_version','$template_platform','$template_author','$template_license')";
1025
				$database->query($query);
1026
			}
1027
		}
1028
	}
1029
}
1030

    
1031
// Load language into DB
1032
function load_language($file) {
1033
	global $database;
1034
	if(file_exists($file)) {
1035
		require($file);
1036
		if(isset($language_name)) {
1037
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
1038
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
1039
			// Check that it doesn't already exist
1040
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
1041
			if($result->numRows() == 0) {
1042
				// Load into DB
1043
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
1044
				"(directory,name,type,version,platform,author,license) ".
1045
				"VALUES ('$language_code','$language_name','language',".
1046
				"'$language_version','$language_platform','$language_author','$language_license')";
1047
	 		$database->query($query);
1048
			}
1049
		}
1050
	}
1051
}
1052

    
1053
// Upgrade module info in DB, optionally start upgrade script
1054
function upgrade_module($directory, $upgrade = false) {
1055
	global $database, $admin, $MESSAGE;
1056
	$directory = WB_PATH . "/modules/$directory";
1057
	if(file_exists($directory.'/info.php')) {
1058
		require($directory.'/info.php');
1059
		if(isset($module_name)) {
1060
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
1061
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
1062
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
1063
			$module_function = strtolower($module_function);
1064
			// Check that it does already exist
1065
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
1066
			if($result->numRows() > 0) {
1067
				// Update in DB
1068
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
1069
					"version = '$module_version', " .
1070
					"description = '" . addslashes($module_description) . "', " .
1071
					"platform = '$module_platform', " .
1072
					"author = '$module_author', " .
1073
					"license = '$module_license'" .
1074
					"WHERE directory = '$module_directory'";
1075
				$database->query($query);
1076
				// Run upgrade script
1077
				if($upgrade == true) {
1078
					if(file_exists($directory.'/upgrade.php')) {
1079
						require($directory.'/upgrade.php');
1080
					}
1081
				}
1082
			}
1083
		}
1084
	}
1085
}
1086

    
1087
?>
(10-10/12)