Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 455 2007-05-08 20:14:10Z 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
function mb_convert_encoding_wrapper($string, $charset_out, $charset_in) {
368
	if (function_exists('mb_convert_encoding')) {
369
		$string=mb_convert_encoding($string, $charset_out, $charset_in);
370
	} else {
371
		if ($charset_in == 'ISO-8859-1') { $mysqlcharset_from = 'latin1'; }
372
		elseif ($charset_in == 'ISO-8859-2') { $mysqlcharset_from = 'latin2'; }
373
		elseif ($charset_in == 'ISO-8859-3') { $mysqlcharset_from = 'latin1'; } //?
374
		elseif ($charset_in == 'ISO-8859-4') { $mysqlcharset_from = 'latin7'; }
375
		elseif ($charset_in == 'ISO-8859-5') { $string = convert_cyr_string ($string, "iso8859-5", "windows-1251" ); $mysqlcharset_from = 'cp1251'; }
376
		elseif ($charset_in == 'ISO-8859-6') { $mysqlcharset_from = 'latin1'; } //? BROKEN
377
		elseif ($charset_in == 'ISO-8859-7') { $mysqlcharset_from = 'greek'; }
378
		elseif ($charset_in == 'ISO-8859-8') { $mysqlcharset_from = 'hebrew'; }
379
		elseif ($charset_in == 'ISO-8859-9') { $mysqlcharset_from = 'latin5'; }
380
		elseif ($charset_in == 'ISO-8859-10') { $mysqlcharset_from = 'latin1'; } //?
381
		elseif ($charset_in == 'BIG5') { $mysqlcharset_from = 'big5'; }
382
		elseif ($charset_in == 'ISO-2022-JP') { $mysqlcharset_from = 'latin1'; } //? BROKEN
383
		elseif ($charset_in == 'ISO-2022-KR') { $mysqlcharset_from = 'latin1'; } //? BROKEN
384
		elseif ($charset_in == 'GB2312') { $mysqlcharset_from = 'gb2312'; }
385
		elseif ($charset_in == 'ISO-8859-11') { $mysqlcharset_from = 'tis620'; }
386
		elseif ($charset_in == 'UTF-8') { $mysqlcharset_from = 'utf8'; }
387
		else { $mysqlcharset_from = 'latin1'; }
388

    
389
		if ($charset_out == 'ISO-8859-1') { $mysqlcharset_to = 'latin1'; }
390
		elseif ($charset_out == 'ISO-8859-2') { $mysqlcharset_to = 'latin2'; }
391
		elseif ($charset_out == 'ISO-8859-3') { $mysqlcharset_to = 'latin1'; } //?
392
		elseif ($charset_out == 'ISO-8859-4') { $mysqlcharset_to = 'latin7'; }
393
		elseif ($charset_out == 'ISO-8859-5') { $mysqlcharset_to = 'cp1251'; } // use convert_cyr_string afterwards
394
		elseif ($charset_out == 'ISO-8859-6') { $mysqlcharset_to = 'latin1'; } //? BROKEN
395
		elseif ($charset_out == 'ISO-8859-7') { $mysqlcharset_to = 'greek'; }
396
		elseif ($charset_out == 'ISO-8859-8') { $mysqlcharset_to = 'hebrew'; }
397
		elseif ($charset_out == 'ISO-8859-9') { $mysqlcharset_to = 'latin5'; }
398
		elseif ($charset_out == 'ISO-8859-10') { $mysqlcharset_to = 'latin1'; } //?
399
		elseif ($charset_out == 'BIG5') { $mysqlcharset_to = 'big5'; }
400
		elseif ($charset_out == 'ISO-2022-JP') { $mysqlcharset_to = 'latin1'; } //? BROKEN
401
		elseif ($charset_out == 'ISO-2022-KR') { $mysqlcharset_to = 'latin1'; } //? BROKEN
402
		elseif ($charset_out == 'GB2312') { $mysqlcharset_to = 'gb2312'; }
403
		elseif ($charset_out == 'ISO-8859-11') { $mysqlcharset_to = 'tis620'; }
404
		elseif ($charset_out == 'UTF-8') { $mysqlcharset_to = 'utf8'; }
405
		else { $mysqlcharset_to = 'latin1'; }
406
        	
407
		if ($charset_in == 'HTML-ENTITIES') { $mysqlcharset_from = 'html'; } // special-case
408
		if ($charset_out == 'HTML-ENTITIES') { $mysqlcharset_to = 'html'; } // special-case
409

    
410
		// use mysql to convert the string
411
		if ($mysqlcharset_from!="html" && $mysqlcharset_to!="html" && $mysqlcharset_from!="" && $mysqlcharset_to!="" && $mysqlcharset_from!=$mysqlcharset_to) {
412
			$string=my_mysql_iconv($string, $mysqlcharset_from, $mysqlcharset_to);
413
			if ($mysqlcharset_to == 'cp1251') { 
414
				$string = convert_cyr_string ($string, "windows-1251", "iso-8859-5" );
415
			}
416
		}
417
		// do the utf8->htmlentities or htmlentities->utf8 translation
418
		if (($mysqlcharset_from=='html' && $mysqlcharset_to=='utf8') || ($mysqlcharset_from=='utf8' && $mysqlcharset_to=='html')) {
419
			if ($mysqlcharset_from == 'html') {
420
				$named_to_numbered_entities=array('&Aacute;'=>'&#193;','&aacute;'=>'&#225;','&Acirc;'=>'&#194;',
421
				'&acirc;'=>'&#226;','&AElig;'=>'&#198;','&aelig;'=>'&#230;','&Agrave;'=>'&#192;','&agrave;'=>'&#224;',
422
				'&Aring;'=>'&#197;','&aring;'=>'&#229;','&Atilde;'=>'&#195;','&atilde;'=>'&#227;','&Auml;'=>'&#196;',
423
				'&auml;'=>'&#228;','&Ccedil;'=>'&#199;','&ccedil;'=>'&#231;','&Eacute;'=>'&#201;','&eacute;'=>'&#233;',
424
				'&Ecirc;'=>'&#202;','&ecirc;'=>'&#234;','&Egrave;'=>'&#200;','&egrave;'=>'&#232;','&Euml;'=>'&#203;',
425
				'&euml;'=>'&#235;','&Iacute;'=>'&#205;','&iacute;'=>'&#237;','&Icirc;'=>'&#206;','&icirc;'=>'&#238;',
426
				'&Igrave;'=>'&#204;','&igrave;'=>'&#236;','&Iuml;'=>'&#207;','&iuml;'=>'&#239;','&Ntilde;'=>'&#209;',
427
				'&ntilde;'=>'&#241;','&Oacute;'=>'&#211;','&oacute;'=>'&#243;','&Ocirc;'=>'&#212;','&ocirc;'=>'&#244;',
428
				'&OElig;'=>'&#338;','&oelig;'=>'&#339;','&Ograve;'=>'&#210;','&ograve;'=>'&#242;','&Otilde;'=>'&#213;',
429
				'&otilde;'=>'&#245;','&Ouml;'=>'&#214;','&ouml;'=>'&#246;','&Scaron;'=>'&#352;','&scaron;'=>'&#353;',
430
				'&szlig;'=>'&#223;','&Uacute;'=>'&#218;','&uacute;'=>'&#250;','&Ucirc;'=>'&#219;','&ucirc;'=>'&#251;',
431
				'&Ugrave;'=>'&#217;','&ugrave;'=>'&#249;','&Uuml;'=>'&#220;','&uuml;'=>'&#252;','&Yacute;'=>'&#221;',
432
				'&yacute;'=>'&#253;','&Yuml;'=>'&#376;','&yuml;'=>'&#255;','&copy;'=>'&#169;','&reg;'=>'&#174;',
433
				'&ETH;'=>'&#208;','&times;'=>'&#215;','&Oslash;'=>'&#216;','&THORN;'=>'&#222;','&eth;'=>'&#240;',
434
				'&oslash;'=>'&#248;','&thorn;'=>'&#254;');
435
				$string = strtr($string, $named_to_numbered_entities);
436
				$string = preg_replace("/&#([0-9]+);/e", "code_to_utf8($1)", $string);
437
			}
438
			elseif ($mysqlcharset_to == 'html') {
439
				$string = preg_replace("/&#([0-9]+);/e", "code_to_utf8($1)", $string);
440
				$char = "";
441
				while (strlen($string) > 0) {
442
					preg_match("/^(.)(.*)$/su", $string, $match);
443
					if (strlen($match[1]) > 1) {
444
						$char .= "&#".uniord($match[1]).";";
445
					} else $char .= $match[1];
446
					$string = $match[2];
447
				}
448
				$string = $char;
449
				$string_htmlspecialchars_decode=array("&lt;"=>"<", "&gt;"=>">", "&amp;"=>"&", "&quot;"=>"\"", "&#039;'"=>"\'");
450
				$string = strtr($string, $string_htmlspecialchars_decode);
451
				$numbered_to_named_entities=array('&#193;'=>'&Aacute;','&#225;'=>'&aacute;','&#194;'=>'&Acirc;',
452
				'&#226;'=>'&acirc;','&#198;'=>'&AElig;','&#230;'=>'&aelig;','&#192;'=>'&Agrave;','&#224;'=>'&agrave;',
453
				'&#197;'=>'&Aring;','&#229;'=>'&aring;','&#195;'=>'&Atilde;','&#227;'=>'&atilde;','&#196;'=>'&Auml;',
454
				'&#228;'=>'&auml;','&#199;'=>'&Ccedil;','&#231;'=>'&ccedil;','&#201;'=>'&Eacute;','&#233;'=>'&eacute;',
455
				'&#202;'=>'&Ecirc;','&#234;'=>'&ecirc;','&#200;'=>'&Egrave;','&#232;'=>'&egrave;','&#203;'=>'&Euml;',
456
				'&#235;'=>'&euml;','&#205;'=>'&Iacute;','&#237;'=>'&iacute;','&#206;'=>'&Icirc;','&#238;'=>'&icirc;',
457
				'&#204;'=>'&Igrave;','&#236;'=>'&igrave;','&#207;'=>'&Iuml;','&#239;'=>'&iuml;','&#209;'=>'&Ntilde;',
458
				'&#241;'=>'&ntilde;','&#211;'=>'&Oacute;','&#243;'=>'&oacute;','&#212;'=>'&Ocirc;','&#244;'=>'&ocirc;',
459
				'&#338;'=>'&OElig;','&#339;'=>'&oelig;','&#210;'=>'&Ograve;','&#242;'=>'&ograve;','&#213;'=>'&Otilde;',
460
				'&#245;'=>'&otilde;','&#214;'=>'&Ouml;','&#246;'=>'&ouml;','&#352;'=>'&Scaron;','&#353;'=>'&scaron;',
461
				'&#223;'=>'&szlig;','&#218;'=>'&Uacute;','&#250;'=>'&uacute;','&#219;'=>'&Ucirc;','&#251;'=>'&ucirc;',
462
				'&#217;'=>'&Ugrave;','&#249;'=>'&ugrave;','&#220;'=>'&Uuml;','&#252;'=>'&uuml;','&#221;'=>'&Yacute;',
463
				'&#253;'=>'&yacute;','&#376;'=>'&Yuml;','&#255;'=>'&yuml;','&#169;'=>'&copy;','&#174;'=>'&reg;',
464
				'&#208;'=>'&ETH;','&#215;'=>'&times;','&#216;'=>'&Oslash;','&#222;'=>'&THORN;','&#240;'=>'&eth;',
465
				'&#248;'=>'&oslash;','&#254;'=>'&thorn;');
466
				$string = strtr($string, $numbered_to_named_entities);
467
			}
468
		}
469
	}
470
	return($string);
471
}
472
// support-function for mb_convert_encoding_wrapper()
473
function uniord($c) {
474
        $ud = 0;
475
        if (ord($c{0}) >= 0 && ord($c{0}) <= 127) $ud = ord($c{0});
476
        if (ord($c{0}) >= 192 && ord($c{0}) <= 223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
477
        if (ord($c{0}) >= 224 && ord($c{0}) <= 239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
478
        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);
479
        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);
480
        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);
481
        if (ord($c{0}) >= 254 && ord($c{0}) <= 255) $ud = false; // error
482
        return $ud;
483
}
484
// support-function for mb_convert_encoding_wrapper()
485
function code_to_utf8($num) {
486
	if ($num <= 0x7F) {
487
		return chr($num);
488
	} elseif ($num <= 0x7FF) {
489
		return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
490
	} elseif ($num <= 0xFFFF) {
491
		 return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
492
	} elseif ($num <= 0x1FFFFF) {
493
		return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
494
	}
495
	return " ";
496
}
497

    
498
// Function to convert a string from mixed html-entities/umlauts to pure utf-8-umlauts
499
function string_to_utf8($string, $charset=DEFAULT_CHARSET) {
500
	$charset = strtoupper($charset);
501
	if ($charset == '') { $charset = 'ISO-8859-1'; }
502

    
503
	// there's no GB2312 or ISO-8859-11 encoding in php's mb_* functions
504
	if ($charset == "GB2312") {
505
		$string=my_mysql_iconv($string, 'gb2312', 'utf8');
506
	} elseif ($charset == "ISO-8859-11") {
507
		$string=my_mysql_iconv($string, 'tis620', 'utf8');
508
	} elseif ($charset != "UTF-8") {
509
		$string=mb_convert_encoding_wrapper($string, 'UTF-8', $charset);
510
	}
511
	$string=mb_convert_encoding_wrapper($string, 'HTML-ENTITIES', 'UTF-8');
512
	$string=mb_convert_encoding_wrapper($string, 'UTF-8', 'HTML-ENTITIES');
513
	return($string);
514
}
515

    
516
// Function to convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
517
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET, $convert_htmlspecialchars=0) {
518
	$charset_out = strtoupper($charset_out);
519
	if ($charset_out == '') {
520
		$charset_out = 'ISO-8859-1';
521
	}
522
	$string = string_to_utf8($string);
523
	if($charset_out != 'UTF-8') {
524
		if ($charset_out == "GB2312") {
525
			$string=my_mysql_iconv($string, 'utf8', 'gb2312');
526
		} elseif ($charset_out == "ISO-8859-11") {
527
			$string=my_mysql_iconv($string, 'utf8', 'tis620');
528
		} else {
529
			$string=mb_convert_encoding_wrapper($string, $charset_out, 'UTF-8');
530
		}
531
	}
532
	if($convert_htmlspecialchars == 1) {
533
		$string=htmlspecialchars($string);
534
	}
535
	return($string);
536
}
537

    
538
// Function to convert a string from mixed html-entitites/$charset_in-umlauts to pure html-entities
539
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET, $convert_htmlspecialchars=1) {
540
	$charset_in = strtoupper($charset_in);
541
	if ($charset_in == "") {
542
		$charset_in = 'ISO-8859-1';
543
	}
544
	$string = string_to_utf8($string, $charset_in);
545
	if($convert_htmlspecialchars == 1) {
546
		$string=htmlspecialchars($string,ENT_QUOTES);
547
	}
548
	$string=mb_convert_encoding_wrapper($string,'HTML-ENTITIES','UTF-8');
549
	return($string);
550
}
551

    
552
// translate any latin/greek/cyrillic html-entities to their plain 7bit equivalents
553
function entities_to_7bit($string) {
554
	require(WB_PATH.'/framework/convert.php');
555
	$string = strtr($string, $conversion_array);
556
	return($string);
557
}
558

    
559
// Function to convert a page title to a page filename
560
function page_filename($string) {
561
	$string = entities_to_7bit(umlauts_to_entities($string));
562
	// Now replace spaces with page spcacer
563
	$string = str_replace(' ', PAGE_SPACER, $string);
564
	// Now remove all bad characters
565
	$bad = array(
566
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
567
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
568
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
569
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
570
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
571
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
572
	);
573
	$string = str_replace($bad, '', $string);
574
	// Now convert to lower-case
575
	$string = strtolower($string);
576
	// Now remove multiple page spacers
577
	$string = str_replace(PAGE_SPACER.PAGE_SPACER, PAGE_SPACER, $string);
578
	// Clean any page spacers at the end of string
579
	$string = str_replace(PAGE_SPACER, ' ', $string);
580
	$string = trim($string);
581
	$string = str_replace(' ', PAGE_SPACER, $string);
582
	// If there are any weird language characters, this will protect us against possible problems they could cause
583
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
584
	// Finally, return the cleaned string
585
	return $string;
586
}
587

    
588
// Function to convert a desired media filename to a clean filename
589
function media_filename($string) {
590
	$string = entities_to_7bit(umlauts_to_entities($string));
591
	// Now remove all bad characters
592
	$bad = array(
593
	'\'', // '
594
	'"', // "
595
	'`', // `
596
	'!', // !
597
	'@', // @
598
	'#', // #
599
	'$', // $
600
	'%', // %
601
	'^', // ^
602
	'&', // &
603
	'*', // *
604
	'=', // =
605
	'+', // +
606
	'|', // |
607
	'/', // /
608
	'\\', // \
609
	';', // ;
610
	':', // :
611
	',', // ,
612
	'?' // ?
613
	);
614
	$string = str_replace($bad, '', $string);
615
	// Clean any page spacers at the end of string
616
	$string = trim($string);
617
	// Finally, return the cleaned string
618
	return $string;
619
}
620

    
621
// Function to work out a page link
622
if(!function_exists('page_link')) {
623
	function page_link($link) {
624
		global $admin;
625
		return $admin->page_link($link);
626
	}
627
}
628

    
629
// Create a new file in the pages directory
630
function create_access_file($filename,$page_id,$level) {
631
	global $admin;
632
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
633
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
634
	} else {
635
		// First make sure parent folder exists
636
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
637
		$parents = '';
638
		foreach($parent_folders AS $parent_folder) {
639
			if($parent_folder != '/' AND $parent_folder != '') {
640
				$parents .= '/'.$parent_folder;
641
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
642
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
643
				}
644
			}	
645
		}
646
		// The depth of the page directory in the directory hierarchy
647
		// '/pages' is at depth 1
648
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
649
		// Work-out how many ../'s we need to get to the index page
650
		$index_location = '';
651
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
652
			$index_location .= '../';
653
		}
654
		$content = ''.
655
'<?php
656
$page_id = '.$page_id.';
657
require("'.$index_location.'config.php");
658
require(WB_PATH."/index.php");
659
?>';
660
		$handle = fopen($filename, 'w');
661
		fwrite($handle, $content);
662
		fclose($handle);
663
		// Chmod the file
664
		change_mode($filename, 'file');
665
	}
666
}
667

    
668
// Function for working out a file mime type (if the in-built PHP one is not enabled)
669
if(!function_exists('mime_content_type')) {
670
   function mime_content_type($file) {
671
       $file = escapeshellarg($file);
672
       return trim(`file -bi $file`);
673
   }
674
}
675

    
676
// Generate a thumbnail from an image
677
function make_thumb($source, $destination, $size) {
678
	// Check if GD is installed
679
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
680
		// First figure out the size of the thumbnail
681
		list($original_x, $original_y) = getimagesize($source);
682
		if ($original_x > $original_y) {
683
			$thumb_w = $size;
684
			$thumb_h = $original_y*($size/$original_x);
685
		}
686
		if ($original_x < $original_y) {
687
			$thumb_w = $original_x*($size/$original_y);
688
			$thumb_h = $size;
689
		}
690
		if ($original_x == $original_y) {
691
			$thumb_w = $size;
692
			$thumb_h = $size;	
693
		}
694
		// Now make the thumbnail
695
		$source = imageCreateFromJpeg($source);
696
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
697
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
698
		imagejpeg($dst_img, $destination);
699
		// Clear memory
700
		imagedestroy($dst_img);
701
	   imagedestroy($source);
702
	   // Return true
703
	   return true;
704
   } else {
705
   	return false;
706
   }
707
}
708

    
709
// Function to work-out a single part of an octal permission value
710
function extract_permission($octal_value, $who, $action) {
711
	// Make sure the octal value is 4 chars long
712
	if(strlen($octal_value) == 0) {
713
		$octal_value = '0000';
714
	} elseif(strlen($octal_value) == 1) {
715
		$octal_value = '000'.$octal_value;
716
	} elseif(strlen($octal_value) == 2) {
717
		$octal_value = '00'.$octal_value;
718
	} elseif(strlen($octal_value) == 3) {
719
		$octal_value = '0'.$octal_value;
720
	} elseif(strlen($octal_value) == 4) {
721
		$octal_value = ''.$octal_value;
722
	} else {
723
		$octal_value = '0000';
724
	}
725
	// Work-out what position of the octal value to look at
726
	switch($who) {
727
	case 'u':
728
		$position = '1';
729
		break;
730
	case 'user':
731
		$position = '1';
732
		break;
733
	case 'g':
734
		$position = '2';
735
		break;
736
	case 'group':
737
		$position = '2';
738
		break;
739
	case 'o':
740
		$position = '3';
741
		break;
742
	case 'others':
743
		$position = '3';
744
		break;
745
	}
746
	// Work-out how long the octal value is and ajust acording
747
	if(strlen($octal_value) == 4) {
748
		$position = $position+1;
749
	} elseif(strlen($octal_value) != 3) {
750
		exit('Error');
751
	}
752
	// Now work-out what action the script is trying to look-up
753
	switch($action) {
754
	case 'r':
755
		$action = 'r';
756
		break;
757
	case 'read':
758
		$action = 'r';
759
		break;
760
	case 'w':
761
		$action = 'w';
762
		break;
763
	case 'write':
764
		$action = 'w';
765
		break;
766
	case 'e':
767
		$action = 'e';
768
		break;
769
	case 'execute':
770
		$action = 'e';
771
		break;
772
	}
773
	// Get the value for "who"
774
	$value = substr($octal_value, $position-1, 1);
775
	// Now work-out the details of the value
776
	switch($value) {
777
	case '0':
778
		$r = false;
779
		$w = false;
780
		$e = false;
781
		break;
782
	case '1':
783
		$r = false;
784
		$w = false;
785
		$e = true;
786
		break;
787
	case '2':
788
		$r = false;
789
		$w = true;
790
		$e = false;
791
		break;
792
	case '3':
793
		$r = false;
794
		$w = true;
795
		$e = true;
796
		break;
797
	case '4':
798
		$r = true;
799
		$w = false;
800
		$e = false;
801
		break;
802
	case '5':
803
		$r = true;
804
		$w = false;
805
		$e = true;
806
		break;
807
	case '6':
808
		$r = true;
809
		$w = true;
810
		$e = false;
811
		break;
812
	case '7':
813
		$r = true;
814
		$w = true;
815
		$e = true;
816
		break;
817
	default:
818
		$r = false;
819
		$w = false;
820
		$e = false;
821
	}
822
	// And finally, return either true or false
823
	return $$action;
824
}
825

    
826
// Function to delete a page
827
function delete_page($page_id) {
828
	
829
	global $admin, $database;
830
	
831
	// Find out more about the page
832
	$database = new database();
833
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
834
	$results = $database->query($query);
835
	if($database->is_error()) {
836
		$admin->print_error($database->get_error());
837
	}
838
	if($results->numRows() == 0) {
839
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
840
	}
841
	$results_array = $results->fetchRow();
842
	$parent = $results_array['parent'];
843
	$level = $results_array['level'];
844
	$link = $results_array['link'];
845
	$page_title = ($results_array['page_title']);
846
	$menu_title = ($results_array['menu_title']);
847
	
848
	// Get the sections that belong to the page
849
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
850
	if($query_sections->numRows() > 0) {
851
		while($section = $query_sections->fetchRow()) {
852
			// Set section id
853
			$section_id = $section['section_id'];
854
			// Include the modules delete file if it exists
855
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
856
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
857
			}
858
		}
859
	}
860
	
861
	// Update the pages table
862
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
863
	$database->query($query);
864
	if($database->is_error()) {
865
		$admin->print_error($database->get_error());
866
	}
867
	
868
	// Update the sections table
869
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
870
	$database->query($query);
871
	if($database->is_error()) {
872
		$admin->print_error($database->get_error());
873
	}
874
	
875
	// Include the ordering class or clean-up ordering
876
	require_once(WB_PATH.'/framework/class.order.php');
877
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
878
	$order->clean($parent);
879
	
880
	// Unlink the page access file and directory
881
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
882
	$filename = $directory.'.php';
883
	$directory .= '/';
884
	if(file_exists($filename) && substr($filename,0,1<>'.')) {
885
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
886
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
887
		} else {
888
			unlink($filename);
889
			if(file_exists($directory)) {
890
				rm_full_dir($directory);
891
			}
892
		}
893
	}
894
	
895
}
896

    
897
// Load module into DB
898
function load_module($directory, $install = false) {
899
	global $database,$admin,$MESSAGE;
900
	if(file_exists($directory.'/info.php')) {
901
		require($directory.'/info.php');
902
		if(isset($module_name)) {
903
			if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
904
			if(!isset($module_platform) AND isset($module_designed_for)) { $module_platform = $module_designed_for; }
905
			if(!isset($module_function) AND isset($module_type)) { $module_function = $module_type; }
906
			$module_function = strtolower($module_function);
907
			// Check that it doesn't already exist
908
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$module_directory."' LIMIT 0,1");
909
			if($result->numRows() == 0) {
910
				// Load into DB
911
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
912
				"(directory,name,description,type,function,version,platform,author,license) ".
913
				"VALUES ('$module_directory','$module_name','".addslashes($module_description)."','module',".
914
				"'$module_function','$module_version','$module_platform','$module_author','$module_license')";
915
				$database->query($query);
916
				// Run installation script
917
				if($install == true) {
918
					if(file_exists($directory.'/install.php')) {
919
						require($directory.'/install.php');
920
					}
921
				}
922
			}
923
		}
924
	}
925
}
926

    
927
// Load template into DB
928
function load_template($directory) {
929
	global $database;
930
	if(file_exists($directory.'/info.php')) {
931
		require($directory.'/info.php');
932
		if(isset($template_name)) {
933
			if(!isset($template_license)) { $template_license = 'GNU General Public License'; }
934
			if(!isset($template_platform) AND isset($template_designed_for)) { $template_platform = $template_designed_for; }
935
			// Check that it doesn't already exist
936
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$template_directory."' LIMIT 0,1");
937
			if($result->numRows() == 0) {
938
				// Load into DB
939
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
940
				"(directory,name,description,type,version,platform,author,license) ".
941
				"VALUES ('$template_directory','$template_name','".addslashes($template_description)."','template',".
942
				"'$template_version','$template_platform','$template_author','$template_license')";
943
				$database->query($query);
944
			}
945
		}
946
	}
947
}
948

    
949
// Load language into DB
950
function load_language($file) {
951
	global $database;
952
	if(file_exists($file)) {
953
		require($file);
954
		if(isset($language_name)) {
955
			if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
956
			if(!isset($language_platform) AND isset($language_designed_for)) { $language_platform = $language_designed_for; }
957
			// Check that it doesn't already exist
958
			$result = $database->query("SELECT addon_id FROM ".TABLE_PREFIX."addons WHERE directory = '".$language_code."' LIMIT 0,1");
959
			if($result->numRows() == 0) {
960
				// Load into DB
961
				$query = "INSERT INTO ".TABLE_PREFIX."addons ".
962
				"(directory,name,type,version,platform,author,license) ".
963
				"VALUES ('$language_code','$language_name','language',".
964
				"'$language_version','$language_platform','$language_author','$language_license')";
965
	 		$database->query($query);
966
			}
967
		}
968
	}
969
}
970

    
971
// Upgrade module info in DB, optionally start upgrade script
972
function upgrade_module($directory, $upgrade = false) {
973
	global $database, $admin, $MESSAGE;
974
	$directory = WB_PATH . "/modules/$directory";
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 does 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
				// Update in DB
986
				$query = "UPDATE " . TABLE_PREFIX . "addons SET " .
987
					"version = '$module_version', " .
988
					"description = '" . addslashes($module_description) . "', " .
989
					"platform = '$module_platform', " .
990
					"author = '$module_author', " .
991
					"license = '$module_license'" .
992
					"WHERE directory = '$module_directory'";
993
				$database->query($query);
994
				// Run upgrade script
995
				if($upgrade == true) {
996
					if(file_exists($directory.'/upgrade.php')) {
997
						require($directory.'/upgrade.php');
998
					}
999
				}
1000
			}
1001
		}
1002
	}
1003
}
1004

    
1005
?>
(10-10/12)