Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php 116 2005-09-16 21:20:22Z stefan $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2005, 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
}
37

    
38
// Define that this file has been loaded
39
define('FUNCTIONS_FILE_LOADED', true);
40

    
41
// Function to remove a non-empty directory
42
function rm_full_dir($directory)
43
{
44
    // If suplied dirname is a file then unlink it
45
    if (is_file($directory)) {
46
        return unlink($directory);
47
    }
48

    
49
    // Empty the folder
50
    $dir = dir($directory);
51
    while (false !== $entry = $dir->read()) {
52
        // Skip pointers
53
        if ($entry == '.' || $entry == '..') {
54
            continue;
55
        }
56

    
57
        // Deep delete directories      
58
        if (is_dir("$directory/$entry")) {
59
            rm_full_dir("$directory/$entry");
60
        } else {
61
            unlink("$directory/$entry");
62
        }
63
    }
64

    
65
    // Now delete the folder
66
    $dir->close();
67
    return rmdir($directory);
68
}
69

    
70
// Function to open a directory and add to a dir list
71
function directory_list($directory) {
72
	
73
	$list = array();
74

    
75
	// Open the directory then loop through its contents
76
	$dir = dir($directory);
77
	while (false !== $entry = $dir->read()) {
78
		// Skip pointers
79
		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
80
			continue;
81
		}
82
		// Add dir and contents to list
83
		if (is_dir("$directory/$entry")) {
84
			$list = array_merge($list, directory_list("$directory/$entry"));
85
			$list[] = "$directory/$entry";
86
		}
87
	}
88

    
89
	// Now return the list
90
	return $list;
91
}
92

    
93
// Function to open a directory and add to a dir list
94
function chmod_directory_contents($directory, $file_mode) {
95
	
96
	// Set the umask to 0
97
	$umask = umask(0);
98
	
99
	// Open the directory then loop through its contents
100
	$dir = dir($directory);
101
	while (false !== $entry = $dir->read()) {
102
		// Skip pointers
103
		if(substr($entry, 0, 1) == '.' || $entry == '.svn') {
104
			continue;
105
		}
106
		// Chmod the sub-dirs contents
107
		if(is_dir("$directory/$entry")) {
108
			chmod_directory_contents("$directory/$entry", $file_mode);
109
		}
110
		change_mode($directory.'/'.$entry, 'file');
111
	}
112
	
113
	// Restore the umask
114
	umask($umask);
115

    
116
}
117

    
118
// Function to open a directory and add to a file list
119
function file_list($directory, $skip = array()) {
120
	
121
	$list = array();
122
	$skip_file = false;
123
	
124
	// Open the directory then loop through its contents
125
	$dir = dir($directory);
126
	while (false !== $entry = $dir->read()) {
127
		// Skip pointers
128
		if($entry == '.' || $entry == '..') {
129
			$skip_file = true;
130
		}
131
		// Check if we to skip anything else
132
		if($skip != array()) {
133
			foreach($skip AS $skip_name) {
134
				if($entry == $skip_name) {
135
					$skip_file = true;
136
				}
137
			}
138
		}
139
		// Add dir and contents to list
140
		if($skip_file != true AND is_file("$directory/$entry")) {
141
			$list[] = "$directory/$entry";
142
		}
143
		
144
		// Reset the skip file var
145
		$skip_file = false;
146
	}
147

    
148
	// Now delete the folder
149
	return $list;
150
}
151

    
152
// Function to get a list of home folders not to show
153
function get_home_folders() {
154
	global $database, $admin;
155
	$home_folders = array();
156
	// Only return home folders is this feature is enabled
157
	if(HOME_FOLDERS) {
158
		$query_home_folders = $database->query("SELECT home_folder FROM ".TABLE_PREFIX."users WHERE home_folder != '".$admin->get_home_folder()."'");
159
		if($query_home_folders->numRows() > 0) {
160
			while($folder = $query_home_folders->fetchRow()) {
161
				$home_folders[$folder['home_folder']] = $folder['home_folder'];
162
			}
163
		}
164
		function remove_home_subs($directory = '/', $home_folders) {
165
			if($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) {
166
				// Loop through the dirs to check the home folders sub-dirs are not shown
167
			   while(false !== ($file = readdir($handle))) {
168
					if(substr($file, 0, 1) != '.' AND $file != '.svn' AND $file != 'index.php') {
169
						if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file)) {
170
							if($directory != '/') { $file = $directory.'/'.$file; } else { $file = '/'.$file; }
171
							foreach($home_folders AS $hf) {
172
								$hf_length = strlen($hf);
173
								if($hf_length > 0) {
174
									if(substr($file, 0, $hf_length+1) == $hf) {
175
										$home_folders[$file] = $file;
176
									}
177
								}
178
							}
179
							$home_folders = remove_home_subs($file, $home_folders);
180
						}
181
					}
182
				}
183
			}
184
			return $home_folders;
185
		}
186
		$home_folders = remove_home_subs('/', $home_folders);
187
	}
188
	return $home_folders;
189
}
190

    
191
// Function to create directories
192
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE) {
193
	if(!file_exists($dir_name)) {
194
		$umask = umask(0);
195
		mkdir($dir_name, $dir_mode);
196
		umask($umask);
197
		return true;
198
	} else {
199
		return false;	
200
	}
201
}
202

    
203
// Function to chmod files and directories
204
function change_mode($name) {
205
	if(OPERATING_SYSTEM != 'windows') {
206
		// Only chmod if os is not windows
207
		if(is_dir($name)) {
208
			$mode = OCTAL_DIR_MODE;
209
		} else {
210
			$mode = OCTAL_FILE_MODE;
211
		}
212
		if(file_exists($name)) {
213
			$umask = umask(0);
214
			chmod($name, $mode);
215
			umask($umask);
216
			return true;
217
		} else {
218
			return false;	
219
		}
220
	} else {
221
		return true;
222
	}
223
}
224

    
225
// Function to figure out if a parent exists
226
function is_parent($page_id) {
227
	global $database;
228
	// Get parent
229
	$query = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'");
230
	$fetch = $query->fetchRow();
231
	// If parent isnt 0 return its ID
232
	if($fetch['parent'] == '0') {
233
		return false;
234
	} else {
235
		return $fetch['parent'];
236
	}
237
}
238

    
239
// Function to work out level
240
function level_count($page_id) {
241
	global $database;
242
	// Get page parent
243
	$query_page = $database->query("SELECT parent FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
244
	$fetch_page = $query_page->fetchRow();
245
	$parent = $fetch_page['parent'];
246
	if($parent > 0) {
247
		// Get the level of the parent
248
		$query_parent = $database->query("SELECT level FROM ".TABLE_PREFIX."pages WHERE page_id = '$parent' LIMIT 1");
249
		$fetch_parent = $query_parent->fetchRow();
250
		$level = $fetch_parent['level'];
251
		return $level+1;
252
	} else {
253
		return 0;
254
	}
255
}
256

    
257
// Function to work out root parent
258
function root_parent($page_id) {
259
	global $database;
260
	// Get page details
261
	$query_page = $database->query("SELECT parent,level FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id' LIMIT 1");
262
	$fetch_page = $query_page->fetchRow();
263
	$parent = $fetch_page['parent'];
264
	$level = $fetch_page['level'];	
265
	if($level == 1) {
266
		return $parent;
267
	} elseif($parent == 0) {
268
		return 0;
269
	} else {
270
		// Figure out what the root parents id is
271
		$parent_ids = array_reverse(get_parent_ids($page_id));
272
		return $parent_ids[0];
273
	}
274
}
275

    
276
// Function to get page title
277
function get_page_title($id) {
278
	global $database;
279
	// Get title
280
	$query = $database->query("SELECT page_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
281
	$fetch = $query->fetchRow();
282
	// Return title
283
	return $fetch['page_title'];
284
}
285

    
286
// Function to get a pages menu title
287
function get_menu_title($id) {
288
	// Connect to the database
289
	$database = new database();
290
	// Get title
291
	$query = $database->query("SELECT menu_title FROM ".TABLE_PREFIX."pages WHERE page_id = '$id'");
292
	$fetch = $query->fetchRow();
293
	// Return title
294
	return $fetch['menu_title'];
295
}
296

    
297
// Function to get all parent page titles
298
function get_parent_titles($parent_id) {
299
	$titles[] = get_menu_title($parent_id);
300
	if(is_parent($parent_id) != false) {
301
		$parent_titles = get_parent_titles(is_parent($parent_id));
302
		$titles = array_merge($titles, $parent_titles);
303
	}
304
	return $titles;
305
}
306

    
307
// Function to get all parent page id's
308
function get_parent_ids($parent_id) {
309
	$ids[] = $parent_id;
310
	if(is_parent($parent_id) != false) {
311
		$parent_ids = get_parent_ids(is_parent($parent_id));
312
		$ids = array_merge($ids, $parent_ids);
313
	}
314
	return $ids;
315
}
316

    
317
// Function to genereate page trail
318
function get_page_trail($page_id) {
319
	return implode(',', array_reverse(get_parent_ids($page_id)));
320
}
321

    
322
// Function to get all sub pages id's
323
function get_subs($parent, $subs) {
324
	// Connect to the database
325
	$database = new database();
326
	// Get id's
327
	$query = $database->query("SELECT page_id FROM ".TABLE_PREFIX."pages WHERE parent = '$parent'");
328
	if($query->numRows() > 0) {
329
		while($fetch = $query->fetchRow()) {
330
			$subs[] = $fetch['page_id'];
331
			// Get subs of this sub
332
			$subs = get_subs($fetch['page_id'], $subs);
333
		}
334
	}
335
	// Return subs array
336
	return $subs;
337
}
338

    
339
// Function to convert a page title to a page filename
340
function page_filename($string) {
341
	// First, translate any non-english characters to their english equivalents
342
	require(WB_PATH.'/framework/convert.php');
343
   $string = strtr($string, $conversion_array);
344
	// Now replace spaces with page spcacer
345
	$string = str_replace(' ', PAGE_SPACER, $string);
346
	// Now remove all bad characters
347
	$bad = array(
348
	'\'', /* /  */ '"', /* " */	'<', /* < */	'>', /* > */
349
	'{', /* { */	'}', /* } */	'[', /* [ */	']', /* ] */	'`', /* ` */
350
	'!', /* ! */	'@', /* @ */	'#', /* # */	'$', /* $ */	'%', /* % */
351
	'^', /* ^ */	'&', /* & */	'*', /* * */	'(', /* ( */	')', /* ) */
352
	'=', /* = */	'+', /* + */	'|', /* | */	'/', /* / */	'\\', /* \ */
353
	';', /* ; */	':', /* : */	',', /* , */	'?' /* ? */
354
	);
355
	$string = str_replace($bad, '', $string);
356
	// Now convert to lower-case
357
	$string = strtolower($string);
358
	// Now remove multiple page spacers
359
	$string = str_replace(PAGE_SPACER.PAGE_SPACER, PAGE_SPACER, $string);
360
	// Clean any page spacers at the end of string
361
	$string = str_replace(PAGE_SPACER, ' ', $string);
362
	$string = trim($string);
363
	$string = str_replace(' ', PAGE_SPACER, $string);
364
	// If there are any weird language characters, this will protect us against possible problems they could cause
365
	$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
366
	// Finally, return the cleaned string
367
	return $string;
368
}
369

    
370
// Function to convert a desired media filename to a clean filename
371
function media_filename($string) {
372
	// First, translate any non-english characters to their english equivalents
373
	require(WB_PATH.'/framework/convert.php');
374
   $string = strtr($string, $conversion_array);
375
	// Now remove all bad characters
376
	$bad = array(
377
	'\'', // '
378
	'"', // "
379
	'`', // `
380
	'!', // !
381
	'@', // @
382
	'#', // #
383
	'$', // $
384
	'%', // %
385
	'^', // ^
386
	'&', // &
387
	'*', // *
388
	'=', // =
389
	'+', // +
390
	'|', // |
391
	'/', // /
392
	'\\', // \
393
	';', // ;
394
	':', // :
395
	',', // ,
396
	'?' // ?
397
	);
398
	$string = str_replace($bad, '', $string);
399
	// Clean any page spacers at the end of string
400
	$string = trim($string);
401
	// Finally, return the cleaned string
402
	return $string;
403
}
404

    
405
// Function to work out a page link
406
if(!function_exists('page_link')) {
407
	function page_link($link) {
408
		// Check for :// in the link (used in URL's)
409
		if(strstr($link, '://') == '') {
410
			return WB_URL.PAGES_DIRECTORY.$link.PAGE_EXTENSION;
411
		} else {
412
			return $link;
413
		}
414
	}
415
}
416

    
417
// Create a new file in the pages directory
418
function create_access_file($filename,$page_id,$level) {
419
	global $admin;
420
	if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
421
		$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
422
	} else {
423
		// First make sure parent folder exists
424
		$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
425
		$parents = '';
426
		foreach($parent_folders AS $parent_folder) {
427
			if($parent_folder != '/' AND $parent_folder != '') {
428
				$parents .= '/'.$parent_folder;
429
				if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents)) {
430
					make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
431
				}
432
			}	
433
		}
434
		// The depth of the page directory in the directory hierarchy
435
		// '/pages' is at depth 1
436
		$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
437
		// Work-out how many ../'s we need to get to the index page
438
		$index_location = '';
439
		for($i = 0; $i < $level + $pages_dir_depth; $i++) {
440
			$index_location .= '../';
441
		}
442
		$content = ''.
443
'<?php
444
$page_id = '.$page_id.';
445
require("'.$index_location.'config.php");
446
require(WB_PATH."/index.php");
447
?>';
448
		$handle = fopen($filename, 'w');
449
		fwrite($handle, $content);
450
		fclose($handle);
451
		// Chmod the file
452
		change_mode($filename, 'file');
453
	}
454
}
455

    
456
// Function for working out a file mime type (if the in-built PHP one is not enabled)
457
if(!function_exists('mime_content_type')) {
458
   function mime_content_type($file) {
459
       $file = escapeshellarg($file);
460
       return trim(`file -bi $file`);
461
   }
462
}
463

    
464
// Generate a thumbnail from an image
465
function make_thumb($source, $destination, $size) {
466
	// Check if GD is installed
467
	if(extension_loaded('gd') AND function_exists('imageCreateFromJpeg')) {
468
		// First figure out the size of the thumbnail
469
		list($original_x, $original_y) = getimagesize($source);
470
		if ($original_x > $original_y) {
471
			$thumb_w = $size;
472
			$thumb_h = $original_y*($size/$original_x);
473
		}
474
		if ($original_x < $original_y) {
475
			$thumb_w = $original_x*($size/$original_y);
476
			$thumb_h = $size;
477
		}
478
		if ($original_x == $original_y) {
479
			$thumb_w = $size;
480
			$thumb_h = $size;	
481
		}
482
		// Now make the thumbnail
483
		$source = imageCreateFromJpeg($source);
484
		$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
485
		imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
486
		imagejpeg($dst_img, $destination);
487
		// Clear memory
488
		imagedestroy($dst_img);
489
	   imagedestroy($source);
490
	   // Return true
491
	   return true;
492
   } else {
493
   	return false;
494
   }
495
}
496

    
497
// Function to work-out a single part of an octal permission value
498
function extract_permission($octal_value, $who, $action) {
499
	// Make sure the octal value is 4 chars long
500
	if(strlen($octal_value) == 0) {
501
		$octal_value = '0000';
502
	} elseif(strlen($octal_value) == 1) {
503
		$octal_value = '000'.$octal_value;
504
	} elseif(strlen($octal_value) == 2) {
505
		$octal_value = '00'.$octal_value;
506
	} elseif(strlen($octal_value) == 3) {
507
		$octal_value = '0'.$octal_value;
508
	} elseif(strlen($octal_value) == 4) {
509
		$octal_value = ''.$octal_value;
510
	} else {
511
		$octal_value = '0000';
512
	}
513
	// Work-out what position of the octal value to look at
514
	switch($who) {
515
	case 'u':
516
		$position = '1';
517
		break;
518
	case 'user':
519
		$position = '1';
520
		break;
521
	case 'g':
522
		$position = '2';
523
		break;
524
	case 'group':
525
		$position = '2';
526
		break;
527
	case 'o':
528
		$position = '3';
529
		break;
530
	case 'others':
531
		$position = '3';
532
		break;
533
	}
534
	// Work-out how long the octal value is and ajust acording
535
	if(strlen($octal_value) == 4) {
536
		$position = $position+1;
537
	} elseif(strlen($octal_value) != 3) {
538
		exit('Error');
539
	}
540
	// Now work-out what action the script is trying to look-up
541
	switch($action) {
542
	case 'r':
543
		$action = 'r';
544
		break;
545
	case 'read':
546
		$action = 'r';
547
		break;
548
	case 'w':
549
		$action = 'w';
550
		break;
551
	case 'write':
552
		$action = 'w';
553
		break;
554
	case 'e':
555
		$action = 'e';
556
		break;
557
	case 'execute':
558
		$action = 'e';
559
		break;
560
	}
561
	// Get the value for "who"
562
	$value = substr($octal_value, $position-1, 1);
563
	// Now work-out the details of the value
564
	switch($value) {
565
	case '0':
566
		$r = false;
567
		$w = false;
568
		$e = false;
569
		break;
570
	case '1':
571
		$r = false;
572
		$w = false;
573
		$e = true;
574
		break;
575
	case '2':
576
		$r = false;
577
		$w = true;
578
		$e = false;
579
		break;
580
	case '3':
581
		$r = false;
582
		$w = true;
583
		$e = true;
584
		break;
585
	case '4':
586
		$r = true;
587
		$w = false;
588
		$e = false;
589
		break;
590
	case '5':
591
		$r = true;
592
		$w = false;
593
		$e = true;
594
		break;
595
	case '6':
596
		$r = true;
597
		$w = true;
598
		$e = false;
599
		break;
600
	case '7':
601
		$r = true;
602
		$w = true;
603
		$e = true;
604
		break;
605
	default:
606
		$r = false;
607
		$w = false;
608
		$e = false;
609
	}
610
	// And finally, return either true or false
611
	return $$action;
612
}
613

    
614
// Function to delete a page
615
function delete_page($page_id) {
616
	
617
	global $admin, $database;
618
	
619
	// Find out more about the page
620
	$database = new database();
621
	$query = "SELECT page_id,menu_title,page_title,level,link,parent,modified_by,modified_when FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
622
	$results = $database->query($query);
623
	if($database->is_error()) {
624
		$admin->print_error($database->get_error());
625
	}
626
	if($results->numRows() == 0) {
627
		$admin->print_error($MESSAGE['PAGES']['NOT_FOUND']);
628
	}
629
	$results_array = $results->fetchRow();
630
	$parent = $results_array['parent'];
631
	$level = $results_array['level'];
632
	$link = $results_array['link'];
633
	$page_title = ($results_array['page_title']);
634
	$menu_title = ($results_array['menu_title']);
635
	
636
	// Get the sections that belong to the page
637
	$query_sections = $database->query("SELECT section_id,module FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'");
638
	if($query_sections->numRows() > 0) {
639
		while($section = $query_sections->fetchRow()) {
640
			// Set section id
641
			$section_id = $section['section_id'];
642
			// Include the modules delete file if it exists
643
			if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php')) {
644
				require(WB_PATH.'/modules/'.$section['module'].'/delete.php');
645
			}
646
		}
647
	}
648
	
649
	// Update the pages table
650
	$query = "DELETE FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'";
651
	$database->query($query);
652
	if($database->is_error()) {
653
		$admin->print_error($database->get_error());
654
	}
655
	
656
	// Update the sections table
657
	$query = "DELETE FROM ".TABLE_PREFIX."sections WHERE page_id = '$page_id'";
658
	$database->query($query);
659
	if($database->is_error()) {
660
		$admin->print_error($database->get_error());
661
	}
662
	
663
	// Include the ordering class or clean-up ordering
664
	require_once(WB_PATH.'/framework/class.order.php');
665
	$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
666
	$order->clean($parent);
667
	
668
	// Unlink the page access file and directory
669
	$directory = WB_PATH.PAGES_DIRECTORY.$link;
670
	$filename = $directory.'.php';
671
	$directory .= '/';
672
	if(file_exists($filename)) {
673
		if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/')) {
674
			$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
675
		} else {
676
			unlink($filename);
677
			if(file_exists($directory)) {
678
				rm_full_dir($directory);
679
			}
680
		}
681
	}
682
	
683
}
684

    
685
?>
(9-9/11)