Location: PHPKode > projects > Charray's CMS > ccms/ccms/path.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_determine_path() {
	global $ccms_controller_url;

	$default_path = '/';
	$result = '';

	// Obtain the path name from _SERVER global.
	// If it is absent, use the default value.
	if (isset($_SERVER['PATH_INFO'])) {
		$result = $_SERVER['PATH_INFO'];
		$result = urldecode($result);
	} else {
		$result = $default_path;
	}

	$orig_result = $result;
	$result = ccms_normalize_path($result);
	if ($orig_result != $result || !isset($_SERVER['PATH_INFO'])) {
		header("Location: $ccms_controller_url$result");
		exit;
	}

	// If the user given path is not valid, return it to normal.
	if (!ccms_valid_path($result)) {
		$result = $default_path;
	}

	return $result;
}

function ccms_valid_path($path) {
	return (!strstr('/..', $path) && !strstr('/.', $path));
}

function ccms_normalize_path($path) {
	global $ccms_data_path;

	$path = trim(trim($path), '/');
	$path = preg_replace('/\/index$/', '', $path);
	$path = "/$path";
	$path = preg_replace('/\/{2,}/', '/', $path);

	if (is_dir("$ccms_data_path$path") && $path != '/') {
		$path .= '/';
	}

	return $path;
}

function ccms_path_title($path) {
	global $ccms_site_name;

	$path = '/' . trim($path, '/');
	if ($path != '/') {
		return substr($path, strrpos($path, '/') + 1);
	} else {
		return $ccms_site_name;
	}
}

function ccms_obtain_path($file, $raw = false) {
	global $ccms_data_path;
	global $ccms_decoders;
	global $ccms_mime;

	$result = substr($file, strlen($ccms_data_path));

	// It it ends with /index..., 
	if (preg_match('/^(.*)\/index\.([^\/]+)$/', $result, $matched) &&
	    in_array($matched[2], $ccms_decoders))
	{
		return "$matched[1]/";
	}

	// It is not index file. Try to cut up to the extension point.
	if (preg_match('/^(.*)\.([^\/]+)$/', $result, $matched)) {
		if (in_array(strtolower($matched[2]), $ccms_decoders)) {
			return ccms_normalize_path($matched[1]);
		} else if (isset($ccms_mime[strtolower($matched[2])]) && $raw) {
			return ccms_normalize_path($matched[0]);
		}
	}

	return false;
}

?>
Return current item: Charray's CMS