Location: PHPKode > projects > Charray's CMS > ccms/ccms/file.php
<?php

// Charray's CMS (CCMS) Version 0.9.1.1
//
// Copyright (c) 2007, Kinson Chan
// charray[at]gmail.com / kchan[at]cs.hku.hk
// All rights reserved.
//
// Please refer to LICENSE.txt coming with this package for
// terms and conditions for redistribution and reuse.

if (!defined("CCMS_IN")) {
	die();
}

function ccms_locate_file($path, &$decoder) {
	global $ccms_mime;
	global $ccms_decoders;
	global $ccms_data_path;
	global $ccms_error_decoder;
	global $ccms_not_found_file;
	global $ccms_not_permitted_file;

	$decoder = false;
	$result = false;
	$path = trim($path);

	// If it is a directory, make sure there is a / behind.
	if (is_dir("$ccms_data_path$path") && !preg_match('/\/$/', $path)) {
		$path .= '/';
	}

	// If it looks like a directory, let us add something to point
	// to a default file.
	if (preg_match('/\/$/', $path)) {
		$path .= 'index';
	}

	// Try to see if we see any files that matched the name extension.
	foreach ($ccms_decoders as $proposed_decoder) {
		$proposed_file = "$ccms_data_path$path.$proposed_decoder";
		if (file_exists($proposed_file)) {
			$decoder = $proposed_decoder;
			$result = $proposed_file;
		}
	}

	// We are out of luck here. Let's see if the original file exists.
	// However, make sure it is not a file readable by decoder.
	// Files readable by decoder should be kept secret.
	$proposed_file = "$ccms_data_path/$path";
	$proposed_extension = ccms_identify_extension($proposed_file);
	if (file_exists($proposed_file) &&
	    !in_array($proposed_extension, $ccms_decoders) &&
	    isset($ccms_mime[$proposed_extension]))
	{
		$decoder = false;
		$result = $proposed_file;
	}

	// We are very out of luck here. None of above works - File Not Found.
	if (!$result) {
		$decoder = $ccms_error_decoder;
		$result = $ccms_not_found_file;
	}

	// Check if the file is readable.
	if (!is_readable($result)) {
		$decoder = $ccms_error_decoder;
		$result = $ccms_not_permitted_file;
	}

	return $result;
}

function ccms_identify_extension($file) {
	if (preg_match('/\.([^\/\.]+)$/', $file, $matched)) {
		return strtolower($matched[1]);
	}
	
	return false;
}

function ccms_identify_mime($file) {
	global $ccms_mime;

	if ($extension = ccms_identify_extension($file)) {
		if (isset($ccms_mime[$extension])) {
			return $ccms_mime[$extension];
		}
	}

	return 'unknown';
}

function ccms_serve_file($decoder, $file) {
	global $ccms_decoder_path;
	global $ccms_template_path;
	global $ccms_module_path;
	global $ccms_library_path;
	global $ccms_images_url;
	global $ccms_controller_url;
	global $ccms_title;
	global $ccms_content;
	global $ccms_template;
	global $ccms_left_modules;
	global $ccms_right_modules;
	global $ccms_top_modules;
	global $ccms_bottom_modules;
	global $ccms_left_content;
	global $ccms_right_content;
	global $ccms_top_content;
	global $ccms_bottom_content;

	if ($decoder) {
		$decoder_path = "$ccms_decoder_path/$decoder.php";
		$decoder_name = "ccms_decoder_$decoder";

		$ccms_title = ccms_file_title($file);
		$ccms_content = '';
		$ccms_template = 'default';
		require_once ($decoder_path);
		$decoder_name($file);

		if (!$ccms_template) {
			echo $ccms_content;
			return;
		}

		$ccms_left_content = ccms_execute_modules($ccms_left_modules, $file);
		$ccms_right_content = ccms_execute_modules($ccms_right_modules, $file);
		$ccms_top_content = ccms_execute_modules($ccms_top_modules, $file);
		$ccms_bottom_content = ccms_execute_modules($ccms_bottom_modules, $file);
		$template_path = "$ccms_template_path/$ccms_template.php";
		require_once ($template_path);
	} else {
		ccms_serve_raw_file($file);
	}
}

function ccms_serve_raw_file($file) {
	header('Content-Type: ' . ccms_identify_mime($file));
	readfile($file);
}

function ccms_execute_modules($modules, $file) {
	global $ccms_module_path;
	$result = array();

	foreach ($modules as $module) {
		$module_file = "$ccms_module_path/$module.php";
		require_once ($module_file);
		$module_function = "ccms_module_$module";
		$result[] = $module_function($file);
	}

	return $result;
}

function ccms_list_directory($dir) {
	$result = array();
	$handle = @opendir($dir);

	if (!$handle) {
		return $result;
	}

	if (!preg_match('/\/$/', $dir)) {
		$dir .= '/';
	}

	while ($filename = readdir($handle)) {
		if ($filename == '.' || $filename == '..') {
			continue;
		}

		$result[] = "$dir$filename";
	}

	closedir($handle);
	return $result;
}

function ccms_file_title($file) {
	global $ccms_site_name;
	global $ccms_data_path;
	global $ccms_decoders;

	if (substr($file, 0, strlen($ccms_data_path)) == $ccms_data_path) {
		$file = substr($file, strlen($ccms_data_path));
	} else {
		$pos = strrpos($file, '.');
		$pos2 = strrpos($file, '/');

		if ($pos > $pos2) {
			return substr($file, $pos2 + 1, $pos - $pos2 - 1);
		} else {
			return substr($file, $pos2 + 1);
		}
	}

	// If it is an index file, get the title from the parent directory.
	// However, if it is on the top already, give the default name.
	if (preg_match('/index(\.[^\.\/]+)?$/', $file)) {
		if (!preg_match('/\/([^\/]+)\/index(\.[^\.\/]+)?$/', $file, $matched)) {
			return $ccms_site_name;
		} else {
			return $matched[1];
		}
	// Otherwise, get it from the file name.
	} else {
		if (!preg_match('/([^\/]+)\.([^\.\/]+)$/', $file, $matched)) {
			return false;
		}

		// At this point, $matched[1] is the title and $matched[2] is
		// the extension. Check we have decoder for this extension.
		// If not, we should skip it, too.
		if (in_array(strtolower($matched[2]), $ccms_decoders) &&
		    !preg_match('/\.ccms_[a-zA-Z]+\.[a-zA-Z]+$/', $file)) {
			return $matched[1];
		} else {
			return false;
		}
	}

	return false;
}

?>
Return current item: Charray's CMS