<?php
/**
 * This file implements evoSkins support functions.
 *
 * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.
 * See also {@link http://sourceforge.net/projects/evocms/}.
 *
 * @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}.
 * Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.
 *
 * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)
 *
 * {@internal Open Source relicensing agreement:
 * Daniel HAHLER grants Francois PLANQUE the right to license
 * Daniel HAHLER's contributions to this file and the b2evolution project
 * under any OSI approved OSS license (http://www.opensource.org/licenses/).
 * }}
 *
 * @package evocore
 *
 * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
 * @author blueyed: Daniel HAHLER.
 * @author fplanque: Francois PLANQUE.
 *
 * @version $Id: _skin.funcs.php,v 1.7.2.7 2006/11/13 17:00:38 fplanque Exp $
 */
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );


/**
 * Template function: output HTML base tag to current skin
 */
function skin_base_tag()
{
	global $skins_url, $skin, $Blog;

	if( ! empty( $skin ) )
	{
		$base_href = $skins_url.$skin.'/';
	}
	else
	{ // No skin used:
		if( ! empty( $Blog ) )
		{
			$base_href = $Blog->get( 'baseurl' );
		}
		else
		{
			$base_href = $baseurl;
		}
	}

	base_tag( $base_href );
}

/**
 * Output content-type header
 * 
 * We use this method when we are NOT generating a static page
 *
 * @see skin_content_meta()
 * 
 * @param string content-type; override for RSS feeds
 */
function skin_content_header( $type = 'text/html' )
{
	global $generating_static, $io_charset;

	if( empty($generating_static) )
	{	// We use this method when we are NOT generating a static page
		header( 'Content-type: '.$type.'; charset='.$io_charset );
	}
}

/**
 * Output content-type http_equiv meta tag
 * 
 * We use this method when we ARE generating a static page
 *
 * @see skin_content_header()
 *
 * @param string content-type; override for RSS feeds
 */
function skin_content_meta( $type = 'text/html' )
{
	global $generating_static, $io_charset;

	if( ! empty($generating_static) )
	{	// We use this method when we ARE generating a static page
		echo '<meta http-equiv="Content-Type" content="'.$type.'; charset='.$io_charset.'" />';
	}
}

/**
 * Checks if a skin is provided by a plugin.
 *
 * @uses Plugin::GetProvidedSkins()
 * @return false|integer False in case no plugin provides the skin or ID of the first plugin that provides it.
 */
function skin_provided_by_plugin( $name )
{
	static $plugin_skins;
	if( ! isset($plugin_skins) || ! isset($plugin_skins[$name]) )
	{
		global $Plugins;

		$plugin_r = $Plugins->trigger_event_first_return('GetProvidedSkins', NULL, array('in_array'=>$name));
		if( $plugin_r )
		{
			$plugin_skins[$name] = $plugin_r['plugin_ID'];
		}
		else
		{
			$plugin_skins[$name] = false;
		}
	}

	return $plugin_skins[$name];
}

/**
 * Checks if a skin exists. This can either be a regular skin directory
 * or can be in the list {@link Plugin::GetProvidedSkins()}.
 *
 * @param skin name (directory name)
 * @return boolean true is exists, false if not
 */
function skin_exists( $name, $filename = '_main.php' )
{
	global $skins_path;

	if( is_readable( $skins_path.$name.'/'.$filename ) )
	{
		return true;
	}

	// Check list provided by plugins:
	if( skin_provided_by_plugin($name) )
	{
		return true;
	}

	return false;
}


/**
 * Outputs an <option> set with default skin selected
 *
 * skin_options(-)
 *
 */
function skin_options( $default = '' )
{
	echo skin_options_return( $default );
}


/**
 * Returns an <option> set with default skin selected
 *
 * @return string
 */
function skin_options_return( $default = '' )
{
	$r = '';

	for( skin_list_start(); skin_list_next(); )
	{
		$r .= '<option value="';
		$r .= skin_list_iteminfo( 'name', false );
		$r .=  '"';
		if( skin_list_iteminfo( 'name',false ) == $default )
		{
			$r .= ' selected="selected" ';
		}
		$r .=  '>';
		$r .= skin_list_iteminfo( 'name', false );
		$r .=  "</option>\n";
	}

	return $r;
}


/**
 * Initializes skin list iterator
 *
 * lists all folders in skin directory
 */
function skin_list_start()
{
	global $skins_path, $skin_dir;

	if( empty( $skins_path ) )
	{	// Check if conf has been properly for version 1.9 (remove in approx 12 months)
		debug_die( '$skins_path is not properly set in /conf/_advanced.php' );
	}

	$skin_dir = dir( $skins_path );
}


/**
 * Get next skin
 *
 * Lists all folders in skin directory,
 * except the ones starting with a . (UNIX style) or a _ (FrontPage style)
 *
 * @return string skin name
 */
function skin_list_next()
{
	global $skins_path, $skin_dir, $skin_name;

	do
	{ // Find next subfolder:
		if( !($skin_name = $skin_dir->read()) )
		{
			return false;		// No more subfolder
		}
	} while( ( ! is_dir($skins_path.$skin_name) )	// skip regular files
						|| ($skin_name[0] == '.')								// skip UNIX hidden files/dirs
						|| ($skin_name[0] == '_')								// skip FRONTPAGE hidden files/dirs
						|| ($skin_name == 'CVS' ) );						// Skip CVS directory
	// echo 'ret=',  $skin_name;
	return $skin_name;
}


/**
 * skin_list_iteminfo(-)
 *
 * Display info about item
 *
 * fplanque: created
 */
function skin_list_iteminfo( $what='', $display = true )
{
	global $skins_path, $skins_url, $skin_name;

	switch( $what )
	{
		case 'path':
			$info = $skins_path.$skin_name;
			break;

		case 'url':
			$info = $skins_url.$skin_name;
			break;

		case 'name':
		default:
			$info = $skin_name;
	}

	if( $display ) echo $info;

	return $info;
}


/**
 * @param boolean display (true) or return?
 */
function skin_change_url( $display = true )
{
	$r = url_add_param( get_bloginfo('blogurl'), 'skin='.rawurlencode(skin_list_iteminfo('name',false)) );
	if( $display )
	{
		echo $r;
	}
	else
	{
		return $r;
	}
}


/*
 nolog */
?>