Project

General

Profile

1
<?php
2

    
3
// $Id: functions.php,v 1.20 2005/06/23 05:47:22 rdjurovich Exp $
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_PATH')) { exit('Direct access to this file is not allowed'); }
35

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

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

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

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

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

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

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

    
87
	// Now return the list
88
	return $list;
89
}
90

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

    
114
}
115

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

    
146
	// Now delete the folder
147
	return $list;
148
}
149

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
682
?>
(6-6/7)