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

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

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

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

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

    
683
?>
(9-9/11)