##
##
##        Mod title:  Style Creator
##
##      Mod version:  0.1-beta
##  Works on FluxBB:  1.4.*
##     Release date:  10-01-2010
##           Author:  M@x (maraumax@maraumax.fr)
##
##      Description:  Cette modification vous permet de crer depuis votre interface
##                    administrateur des styles personnaliss.
##
##   Affected files:  Oui
##
##       Affects DB:  Oui
##
##            Notes:  Avant toutes procdures, faites un backup ! (Bases et fichiers modifis)
##
##       DISCLAIMER:  Veuillez noter que les modifications ne sont pas officielement
##      			  supports par FluxBB...
##
##


#
#---------[ 1. Uploader ]-------------------------------------------------------
#

./include/stylecreator/ (dossier complet)
./admin_styles.php
./install_mod.php

#
#---------[ 2. Executer et supprimer ]---------------------------------------------------
#

install_mod.php

#
#---------[ 3. Ouvrir ]---------------------------------------------------
#

./include/common_admin.php

#
#---------[ 4. Rechercher ]---------------------------------------------------
#

<?php endif; ?>					<li<?php if ($page == 'reports') echo ' class="isactive"'; ?>><a href="admin_reports.php">

#
#---------[ 5. Avant, ajouter ]---------------------------------------------------
#

<?php endif; ?><?php if ($is_admin): ?>					<li<?php if ($page == 'styles') echo ' class="isactive"'; ?>><a href="admin_styles.php">Styles</a></li>

#
#---------[ 6. Ouvrir ]---------------------------------------------------
#

./header.php

#
#---------[ 7. Rechercher ]---------------------------------------------------
#

if (defined('PUN_ADMIN_CONSOLE'))
	echo '<link rel="stylesheet" type="text/css" href="style/imports/base_admin.css" />'."\n";

#
#---------[ 8. Aprs, ajouter ]---------------------------------------------------
#

if(defined('LOAD_STYLETOOLS'))
	echo '<script type="text/javascript" src="include/stylecreator/jquery.js"></script>'."\n".'<script type="text/javascript" src="include/stylecreator/jquery.qtip.js"></script>'."\n".'<script type="text/javascript" src="include/stylecreator/jquery.ajaxupload.js"></script>'."\n".'<script type="text/javascript" src="include/stylecreator/jquery.ui.js"></script>'."\n".'<script type="text/javascript" src="include/stylecreator/jquery.colorpicker.js"></script>'."\n".'<script type="text/javascript" src="include/stylecreator/scripts.js"></script>'."\n".'<link rel="stylesheet" type="text/css" href="include/stylecreator/css/colorpicker.css" />'."\n".'<link rel="stylesheet" type="text/css" href="include/stylecreator/css/jquery.ui.css" />'."\n";

#
#---------[ 9. Ouvrir ]---------------------------------------------------
#

./include/functions.php

#
#---------[ 10. Ajoutez  la fin ]---------------------------------------------------
#

//
// Check if color is valid
//
function check_color($inputs, $fields)
{
	foreach($inputs AS $key => $input)
	{
		if(in_array($key, $fields) && !ereg('#[0-9a-f]{6}', utf8_strtolower($input)))
			return false;
	}

	return true;
}

//
// Check if size is valide
//
function check_size($inputs, $fields)
{
	foreach($inputs AS $key => $input)
	{
		if(in_array($key, $fields) && !ereg('[0-9](px|%)', utf8_strtolower($input)))
			return false;
	}

	return true;
}

//
// Get the repeat value of backgrounds
//
function get_background_repeat($repeat)
{
	switch($repeat)
	{
		case 'none':
			$choice = 'background-repeat: no-repeat; background-position: top center;';
		break;
		case 'center':
			$choice = 'background-position: center;';
		break;
		case 'repeat-x':
			$choice = 'background-repeat: repeat-x; background-position: top center;';
		break;
		case 'repeat-y':
			$choice = 'background-repeat: repeat-y; background-position: top center;';
		break;
		case 'repeat':
			$choice = 'background-repeat: repeat; background-position: top center;';
		break;
		default: 
			$choice = 'background-repeat: no-repeat; background-position: top center;';
		break;
	}

	return $choice;
}

//
// Get the effect of links
//
function get_link_effect($effect)
{
	switch($effect)
	{
		case 'none':
			$choice = 'text-decoration: none;';
		break;
		case 'bold':
			$choice = 'font-weight: bold;';
		break;
		case 'italic':
			$choice = 'font-style: italic;';
		break;
		case 'underline':
			$choice = 'text-decoration: underline;';
		break;
		default:
			$choice = 'text-decoration: none;';
		break;
	}

	return $choice;
}

//
// Create a style (.css)
//
function create_style($params, $uploads, $name)
{
	$tpl_style = trim(file_get_contents(PUN_ROOT.'include/template/style.tpl'));

	foreach($params AS $param => $value)
		$tpl_style = str_replace('<'.$param.'>', $value, $tpl_style);

	// Links effect
	if(isset($params['links_beffect']))
		$links_beffect = get_link_effect($params['links_beffect']);
	else
		$links_beffect = 'text-decoration: none;';
	$tpl_style = str_replace('<links_bhcolor_more>', $links_beffect, $tpl_style);

	// Navlinks effect
	if(isset($params['links_neffect']))
		$links_beffect = get_link_effect($params['links_neffect']);
	else
			$links_neffect = 'text-decoration: none;';
	$tpl_style = str_replace('<links_nh_more>', $links_neffect, $tpl_style);

	// Options
	$options = array();
	if(isset($params['show_description']) && $params['show_description'] == 0)
		$options[] = '.pun #brddesc { display: none; }';
	if(isset($params['show_moderators']) && $params['show_moderators'] == 0)
		$options[] = '.pun div.blocktable p.modlist { display: none; }';

	/* Status icons */
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status']))
	{
		list($img_width, $img_height, ) = getimagesize(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status']);
		$options[] = 'div.icon { background-image: url(../img/'.$name.'/'.$uploads['msg-status'].'); width: '.$img_width.'px; height: '.$img_height.'px; }';
	}
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-new']))
	{
		list($img_width, $img_height, ) = getimagesize(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-new']);
		$options[] = 'div.inew { background-image: url(../img/'.$name.'/'.$uploads['msg-status-new'].'); width: '.$img_width.'px; height: '.$img_height.'px; }';
	}
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-sticky']))
	{
		list($img_width, $img_height, ) = getimagesize(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-sticky']);
		$options[] = 'tr.isticky div.icon, tr.isticky div.inew { background-image: url(../img/'.$name.'/'.$uploads['msg-status-sticky'].'); width: '.$img_width.'px; height: '.$img_height.'px; }';
	}
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-close']))
	{
		list($img_width, $img_height, ) = getimagesize(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-close']);
		$options[] = 'tr.iclosed div.icon { background-image: url(../img/'.$name.'/'.$uploads['msg-status-close'].'); width: '.$img_width.'px; height: '.$img_height.'px; }';
	}
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-redirect']))
	{
		list($img_width, $img_height, ) = getimagesize(PUN_ROOT.'img/'.$name.'/'.$uploads['msg-status-redirect']);
		$options[] = 'tr.iredirect div.icon { background-image: url(../img/'.$name.'/'.$uploads['msg-status-redirect'].'); width: '.$img_width.'px; height: '.$img_height.'px; }';
	}
	/* Background images */
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['background']))
		$options[] = 'body { background-image: url(../img/'.$name.'/'.$uploads['background'].'); '.get_background_repeat($params['imgs_bgpos']).' }';
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['background-navlinks']))
		$options[] = '#brdmenu { background-image: url(../img/'.$name.'/'.$uploads['background-navlinks'].'); '.get_background_repeat($params['imgs_npos']).' }';
	if(file_exists(PUN_ROOT.'img/'.$name.'/'.$uploads['background-title']))
	{
		list($img_width, $img_height, ) = getimagesize(PUN_ROOT.'img/'.$name.'/'.$uploads['background-title']);
		$options[] = '.pun h2 { background-image: url(../img/'.$name.'/'.$uploads['background-title'].'); background-position: left center; background-repeat: no-repeat; padding-left: '.($img_width+4).'px; }';
	}

	$tpl_style = str_replace('<options>', implode("\n", $options), $tpl_style);

	$fh = @fopen(PUN_ROOT.'style/'.$name.'.css', 'wb');
	if ($fh)
	{
		fwrite($fh, $tpl_style);
		fclose($fh);
	}
}

function generate_style_files()
{
	global $db;

	$result = $db->query('SELECT s.style_name, s.style_options, s.style_uploads, s.usable FROM '.$db->prefix.'styles AS s');
	while($cur_style = $db->fetch_assoc($result))
	{
		$style_name = str_replace(' ', '_', preg_replace('/([^a-z0-9]+)/i', '', $cur_style['style_name']));
		$style_uploads = unserialize($cur_style['style_uploads']);
		$style_options = unserialize($cur_style['style_options']);

		if($cur_style['usable'] == 0 OR empty($cur_style['style_options']))
		{
			if(file_exists(PUN_ROOT.'style/'.$style_name.'.css'))
				unlink(PUN_ROOT.'style/'.$style_name.'.css');
		}
		else
			create_style($style_options, $style_uploads, $style_name);
	}
}


#
#---------[ 11. Appliquer ]---------------------------------------------------
#

Appliquez un chmod de 777 sur les dossiers suivants :
./style/
./img/


#
#---------[ 12. Installation termine ]---------------------------------------------------
#

L'installation est dsormais termine. Vous pouvez dsormais vous rendre dans 
votre paneau d'adminiatration dans le menu "Styles" pour crer vos styles.
