Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 461 2007-05-14 20:17:49Z 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
	// try mb_convert_encoding(). This can handle to or from HTML-ENTITIES, too
371
	if (function_exists('mb_convert_encoding')) {
372
		// there's no GB2312 or ISO-8859-11 encoding in php's mb_* functions
373
		if ($charset_in=='ISO-8859-11' || $charset_in=='GB2312') {
374
			if (function_exists('iconv')) {
375
				$string = iconv($charset_in, 'UTF-8', $string);
376
			}
377
			else {
378
				if ($charset_in == 'GB2312') {
379
					$string=my_mysql_iconv($string, 'gb2312', 'utf8');
380
				} else {
381
					$string=my_mysql_iconv($string, 'tis620', 'utf8');
382
				}
383
			}
384
			$charset_in='UTF-8';
385
			if ($charset_out == 'UTF-8') {
386
				return $string;
387
			}
388
		}
389
		if ($charset_out=='ISO-8859-11' || $charset_out=='GB2312') {
390
			$string=mb_convert_encoding($string, 'UTF-8', $charset_in);
391
			if (function_exists('iconv')) {
392
				$string = iconv('UTF-8', $charset_out, $string);
393
			}
394
			else {
395
				if ($charset_out == 'GB2312') {
396
					$string=my_mysql_iconv($string, 'utf8', 'gb2312');
397
				} else {
398
					$string=my_mysql_iconv($string, 'utf8', 'tis620');
399
				}
400
			}
401
		} else {
402
			$string=mb_convert_encoding($string, $charset_out, $charset_in);
403
		}
404
		return $string;
405
	}
406

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

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

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

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

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

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

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

    
562
	if (!is_UTF8($string)) {
563
		$string=mb_convert_encoding_wrapper($string, 'UTF-8', $charset);
564
	} else {
565
	}
566

    
567
	// check if we really get UTF-8. We don't get UTF-8 if charset is ISO-8859-11 or GB2312 and mb_string AND iconv aren't available.
568
	if (is_UTF8($string)) {
569
		$string=mb_convert_encoding_wrapper($string, 'HTML-ENTITIES', 'UTF-8');
570
		$string=mb_convert_encoding_wrapper($string, 'UTF-8', 'HTML-ENTITIES');
571
	} else {
572
	}
573
	return($string);
574
}
575

    
576
// function to check if a string is UTF-8
577
function is_UTF8 ($string) {
578
	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})*$%xs', $string);
579
}
580

    
581
// Function to convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
582
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET, $convert_htmlspecialchars=0) {
583
	$charset_out = strtoupper($charset_out);
584
	if ($charset_out == '') { $charset_out = 'ISO-8859-1'; }
585
	$string = string_to_utf8($string);
586
	if($convert_htmlspecialchars == 1) {
587
		$string=htmlspecialchars($string);
588
	}
589
	if($charset_out!='UTF-8' && is_UTF8($string)) {
590
		$string=mb_convert_encoding_wrapper($string, $charset_out, 'UTF-8');
591
	}
592
	return($string);
593
}
594

    
595
// Function to convert a string from mixed html-entitites/$charset_in-umlauts to pure html-entities
596
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET, $convert_htmlspecialchars=1) {
597
	$charset_in = strtoupper($charset_in);
598
	if ($charset_in == "") { $charset_in = 'ISO-8859-1'; }
599
	$string = string_to_utf8($string, $charset_in);
600
	if($convert_htmlspecialchars == 1) {
601
		$string=htmlspecialchars($string,ENT_QUOTES);
602
	}
603
	if (is_UTF8($string)) {
604
		$string=mb_convert_encoding_wrapper($string,'HTML-ENTITIES','UTF-8');
605
	}
606
	return($string);
607
}
608

    
609
// translate any latin/greek/cyrillic html-entities to their plain 7bit equivalents
610
// and numbered-entities into hex
611
function entities_to_7bit($string) {
612
	require(WB_PATH.'/framework/convert.php');
613
	$string = strtr($string, $conversion_array);
614
	$string = preg_replace('/&#([0-9]+);/e', "dechex('$1')",  $string);
615
	return($string);
616
}
617

    
618
// Function to convert a page title to a page filename
619
function page_filename($string) {
620
	$string = entities_to_7bit(umlauts_to_entities($string));
621
	// Now replace spaces with page spcacer
622
	$string = str_replace(' ', PAGE_SPACER, $string);
623
	// Now remove all bad characters
624
	$bad = array(
625
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
626
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
627
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
628
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
629
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
630
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
631
	);
632
	$string = str_replace($bad, '', $string);
633
	// Now convert to lower-case
634
	$string = strtolower($string);
635
	// Now remove multiple page spacers
636
	$string = str_replace(PAGE_SPACER.PAGE_SPACER, PAGE_SPACER, $string);
637
	// Clean any page spacers at the end of string
638
	$string = str_replace(PAGE_SPACER, ' ', $string);
639
	$string = trim($string);
640
	$string = str_replace(' ', PAGE_SPACER, $string);
641
	// If there are any weird language characters, this will protect us against possible problems they could cause
642
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
643
	// Finally, return the cleaned string
644
	return $string;
645
}
646

    
647
// Function to convert a desired media filename to a clean filename
648
function media_filename($string) {
649
	$string = entities_to_7bit(umlauts_to_entities($string));
650
	// Now remove all bad characters
651
	$bad = array(
652
	'\'', // '
653
	'"', // "
654
	'`', // `
655
	'!', // !
656
	'@', // @
657
	'#', // #
658
	'$', // $
659
	'%', // %
660
	'^', // ^
661
	'&', // &
662
	'*', // *
663
	'=', // =
664
	'+', // +
665
	'|', // |
666
	'/', // /
667
	'\\', // \
668
	';', // ;
669
	':', // :
670
	',', // ,
671
	'?' // ?
672
	);
673
	$string = str_replace($bad, '', $string);
674
	// Clean any page spacers at the end of string
675
	$string = trim($string);
676
	// Finally, return the cleaned string
677
	return $string;
678
}
679

    
680
// Function to work out a page link
681
if(!function_exists('page_link')) {
682
	function page_link($link) {
683
		global $admin;
684
		return $admin->page_link($link);
685
	}
686
}
687

    
688
// Create a new file in the pages directory
689
function create_access_file($filename,$page_id,$level) {
690
	global $admin;
691
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
692
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
693
	} else {
694
		// First make sure parent folder exists
695
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
696
		$parents = '';
697
		foreach($parent_folders AS $parent_folder) {
698
			if($parent_folder != '/' AND $parent_folder != '') {
699
				$parents .= '/'.$parent_folder;
700
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
701
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
702
				}
703
			}	
704
		}
705
		// The depth of the page directory in the directory hierarchy
706
		// '/pages' is at depth 1
707
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
708
		// Work-out how many ../'s we need to get to the index page
709
		$index_location = '';
710
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
711
			$index_location .= '../';
712
		}
713
		$content = ''.
714
'<?php
715
$page_id = '.$page_id.';
716
require("'.$index_location.'config.php");
717
require(WB_PATH."/index.php");
718
?>';
719
		$handle = fopen($filename, 'w');
720
		fwrite($handle, $content);
721
		fclose($handle);
722
		// Chmod the file
723
		change_mode($filename, 'file');
724
	}
725
}
726

    
727
// Function for working out a file mime type (if the in-built PHP one is not enabled)
728
if(!function_exists('mime_content_type')) {
729
   function mime_content_type($file) {
730
       $file = escapeshellarg($file);
731
       return trim(`file -bi $file`);
732
   }
733
}
734

    
735
// Generate a thumbnail from an image
736
function make_thumb($source, $destination, $size) {
737
	// Check if GD is installed
738
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
739
		// First figure out the size of the thumbnail
740
		list($original_x, $original_y) = getimagesize($source);
741
		if ($original_x > $original_y) {
742
			$thumb_w = $size;
743
			$thumb_h = $original_y*($size/$original_x);
744
		}
745
		if ($original_x < $original_y) {
746
			$thumb_w = $original_x*($size/$original_y);
747
			$thumb_h = $size;
748
		}
749
		if ($original_x == $original_y) {
750
			$thumb_w = $size;
751
			$thumb_h = $size;	
752
		}
753
		// Now make the thumbnail
754
		$source = imageCreateFromJpeg($source);
755
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
756
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
757
		imagejpeg($dst_img, $destination);
758
		// Clear memory
759
		imagedestroy($dst_img);
760
	   imagedestroy($source);
761
	   // Return true
762
	   return true;
763
   } else {
764
   	return false;
765
   }
766
}
767

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

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

    
956
// Load module into DB
957
function load_module($directory, $install = false) {
958
	global $database,$admin,$MESSAGE;
959
	if(file_exists($directory.'/info.php')) {
960
		require($directory.'/info.php');
961
		if(isset($module_name)) {
962
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
963
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
964
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
965
			$module_function = strtolower($module_function);
966
			// Check that it doesn't already exist
967
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
968
			if($result->numRows() == 0) {
969
				// Load into DB
970
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
971
				"(directory,name,description,type,function,version,platform,author,license) ".
972
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
973
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
974
				$database->query($query);
975
				// Run installation script
976
				if($install == true) {
977
					if(file_exists($directory.'/install.php')) {
978
						require($directory.'/install.php');
979
					}
980
				}
981
			}
982
		}
983
	}
984
}
985

    
986
// Load template into DB
987
function load_template($directory) {
988
	global $database;
989
	if(file_exists($directory.'/info.php')) {
990
		require($directory.'/info.php');
991
		if(isset($template_name)) {
992
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
993
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
994
			// Check that it doesn't already exist
995
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
996
			if($result->numRows() == 0) {
997
				// Load into DB
998
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
999
				"(directory,name,description,type,version,platform,author,license) ".
1000
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
1001
				"'$template_version','$template_platform','$template_author','$template_license')";
1002
				$database->query($query);
1003
			}
1004
		}
1005
	}
1006
}
1007

    
1008
// Load language into DB
1009
function load_language($file) {
1010
	global $database;
1011
	if(file_exists($file)) {
1012
		require($file);
1013
		if(isset($language_name)) {
1014
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
1015
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
1016
			// Check that it doesn't already exist
1017
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
1018
			if($result->numRows() == 0) {
1019
				// Load into DB
1020
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
1021
				"(directory,name,type,version,platform,author,license) ".
1022
				"VALUES ('$language_code','$language_name','language',".
1023
				"'$language_version','$language_platform','$language_author','$language_license')";
1024
	 		$database->query($query);
1025
			}
1026
		}
1027
	}
1028
}
1029

    
1030
// Upgrade module info in DB, optionally start upgrade script
1031
function upgrade_module($directory, $upgrade = false) {
1032
	global $database, $admin, $MESSAGE;
1033
	$directory = WB_PATH . "/modules/$directory";
1034
	if(file_exists($directory.'/info.php')) {
1035
		require($directory.'/info.php');
1036
		if(isset($module_name)) {
1037
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
1038
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
1039
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
1040
			$module_function = strtolower($module_function);
1041
			// Check that it does already exist
1042
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
1043
			if($result->numRows() > 0) {
1044
				// Update in DB
1045
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
1046
					"version = '$module_version', " .
1047
					"description = '" . addslashes($module_description) . "', " .
1048
					"platform = '$module_platform', " .
1049
					"author = '$module_author', " .
1050
					"license = '$module_license'" .
1051
					"WHERE directory = '$module_directory'";
1052
				$database->query($query);
1053
				// Run upgrade script
1054
				if($upgrade == true) {
1055
					if(file_exists($directory.'/upgrade.php')) {
1056
						require($directory.'/upgrade.php');
1057
					}
1058
				}
1059
			}
1060
		}
1061
	}
1062
}
1063

    
1064
?>
(10-10/12)