Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 468 2007-06-04 22:55:31Z 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 = strtr($string, array("<"=>"&lt;", ">"=>"&gt;", "\""=>"&quot;", "\'"=>"&#039;"));
344
	return($string);
345
}
346

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

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

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

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

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

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

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

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

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

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

    
578
// function to check if a string is UTF-8
579
function is_UTF8 ($str) {
580
	if (strlen($str) < 4000) {
581
		// 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.
582
		// 4000 works for me ...
583
		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);
584
	}	else {
585
		$isUTF8 = true;
586
		while($str{0}) {
587
			if (preg_match("/^[\x09\x0A\x0D\x20-\x7E]/", $str)) { $str = substr($str, 1); continue; }
588
			if (preg_match("/^[\xC2-\xDF][\x80-\xBF]/", $str)) { $str = substr($str, 2); continue; }
589
			if (preg_match("/^\xE0[\xA0-\xBF][\x80-\xBF]/", $str)) { $str = substr($str, 3); continue; }
590
			if (preg_match("/^[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}/", $str)) { $str = substr($str, 3); continue; }
591
			if (preg_match("/^\xED[\x80-\x9F][\x80-\xBF]/", $str)) { $str = substr($str, 3); continue; }
592
			if (preg_match("/^\xF0[\x90-\xBF][\x80-\xBF]{2}/", $str)) { $str = substr($str, 4); continue; }
593
			if (preg_match("/^[\xF1-\xF3][\x80-\xBF]{3}/", $str)) { $str = substr($str, 4); continue; }
594
			if (preg_match("/^\xF4[\x80-\x8F][\x80-\xBF]{2}/", $str)) { $str = substr($str, 4); continue; }
595
			if (preg_match("/^$/", $str)) { break; }
596
			$isUTF8 = false;
597
			break;
598
		}
599
		return ($isUTF8);
600
	}
601
}
602

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

    
614
// Function to convert a string from mixed html-entitites/$charset_in-umlauts to pure html-entities
615
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET, $convert_htmlspecialchars=0) {
616
	$charset_in = strtoupper($charset_in);
617
	if ($charset_in == "") { $charset_in = 'ISO-8859-1'; }
618
	$string = string_to_utf8($string, $charset_in);
619
	if (is_UTF8($string)) {
620
		$string=mb_convert_encoding_wrapper($string,'HTML-ENTITIES','UTF-8');
621
	}
622
	return($string);
623
}
624

    
625
// translate any latin/greek/cyrillic html-entities to their plain 7bit equivalents
626
// and numbered-entities into hex
627
function entities_to_7bit($string) {
628
	require(WB_PATH.'/framework/convert.php');
629
	$string = strtr($string, $conversion_array);
630
	$string = preg_replace('/&#([0-9]+);/e', "dechex('$1')",  $string);
631
	return($string);
632
}
633

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

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

    
696
// Function to work out a page link
697
if(!function_exists('page_link')) {
698
	function page_link($link) {
699
		global $admin;
700
		return $admin->page_link($link);
701
	}
702
}
703

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

    
743
// Function for working out a file mime type (if the in-built PHP one is not enabled)
744
if(!function_exists('mime_content_type')) {
745
   function mime_content_type($file) {
746
       $file = escapeshellarg($file);
747
       return trim(`file -bi $file`);
748
   }
749
}
750

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

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

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

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

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

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

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

    
1080
?>
(10-10/12)