<?php
// +-------------------------------------------------------------+
// | HiveMail version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: skin.php,v $ - $Revision: 1.47 $
// | $Date: 2004/08/04 06:04:39 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
if ($_POST['cmd'] != 'serve') {
	cp_header(' &raquo; Skins');
}

// ############################################################################
// Set the default cmd
default_var($cmd, 'modify');
if ($cmd == 'add') {
	$cmd = 'edit';
}

// ############################################################################
// Download skin
if ($_POST['cmd'] == 'serve') {
	$askin = getinfo('skin', $askinid, false, false);
	if ($askin === false) {
		adminlog($askinid, false);
		cp_header();
		cp_nav('skindownup');
		cp_error('Invalid skin specified.');
	}
	eval('$askin[\'vars\'] = '.$askin['vars'].';');

	adminlog($askinid, true);
	header('Content-disposition: attachment; filename=hivemail_skin_'.date('m_d_Y').'.skin');
	header('Content-type: unknown/unknown');
	while ($template = $DB_site->fetch_array($gettemplates, "SELECT * FROM hive_template WHERE templatesetid = $askin[templatesetid] ORDER BY title")) {
		$title = $template['title'];
		unset($template['title'], $template['templatesetid'], $template['templateid'], $template[0], $template[1], $template[2], $template[3], $template[4], $template[5], $template[6], $template[7], $template[8]);
		$askin['templates']["$title"] = $template;
	}
	$askin = array('vars' => $askin['vars'], 'templates' => $askin['templates']);
	echo preg_replace("#\r?\n#", "\n", export_array($askin));
	exit;
}

// ############################################################################
// Upload skin
if ($_POST['cmd'] == 'upload') {
	cp_nav('skindownup');
	if (!upload_file($filename, $filedata)) {
		adminlog(0, false);
		cp_error('There has been error while trying to upload the file.<br />Please go back and try again.');
	}

	$filedata = str_replace('\\\\\\\'', '\\\\\'', $filedata);
	$filedata = str_replace('\\\\\'', '\\\\\\\'', $filedata);
	eval('$askin = '.$filedata.';');
	
	$DB_site->query("
		INSERT INTO hive_templateset
		(templatesetid, title)
		VALUES (NULL, '".addslashes($newname)."')
	");
	$templatesetid = $DB_site->insert_id();
	$values = '';
	if (is_array($askin['templates'])) {
		foreach ($askin['templates'] as $title => $tempinfo) {
			$values .= ",(NULL, $templatesetid, $tempinfo[templategroupid], '".addslashes($title)."', '".addslashes($tempinfo['user_data'])."', '".addslashes($tempinfo['parsed_data'])."', '".addslashes($tempinfo['backup_data'])."')";
		}
		$values = substr($values, 1);
		$DB_site->query("
			INSERT INTO hive_template
			(templateid, templatesetid, templategroupid, title, user_data, parsed_data, backup_data)
			VALUES
			$values
		");
	}
	
	// Create new skin
	$DB_site->query("
		INSERT INTO hive_skin
		(skinid, templatesetid, title, vars)
		VALUES (NULL, $templatesetid, '".addslashes($newname)."', '".addslashes(iif(is_array($askin['vars']), export_array($askin['vars']), $askin['vars']))."')
	");
	$askinid = $DB_site->insert_id();

	// Make it available for all users by default
	$DB_site->query("
		UPDATE hive_usergroup
		SET allowedskins = CONCAT(allowedskins, ',$askinid')
	");

	adminlog($askinid, true);
	cp_redirect('The skin has been created.', "skin.php");
}

// ############################################################################
// Import/export skin
if ($cmd == 'downup') {
	cp_nav('skindownup');
	adminlog();

	startform('skin.php', 'serve');
	starttable('Export Skin');
	textrow('Use this to download a file of the select skin and save it to your computer.<br />This file can be later used to import the skin back into the system.');
	tableselect('Skin to download:', 'askinid', 'skin');
	endform('Export Skin');
	endtable();

	startform('skin.php', 'upload', '', array(), true);
	starttable('Import Skin');
	textrow('Use this form to upload a skin file and create a new skin based on it.');
	filefield('Skin file:<br /><span class="cp_small">Click Browse and select the backup file from your computer. When you are done, click Open.</span>', 'file');
	inputfield('Name of new skin:<br /><span class="cp_small">The new skin that will be created from this file will have this name.</span>', 'newname');
	endform('Import Skin');
	endtable();
}

// ############################################################################
// Remove skin
if ($_POST['cmd'] == 'kill') {
	cp_nav('skinmodify');
	$askin = getinfo('skin', $askinid);
	if ($askinid == 1) {
		adminlog($askinid, false);
		cp_error('You cannot remove the original skin.');
	}

	$DB_site->query("
		DELETE FROM hive_skin
		WHERE skinid = $askinid
	");
	$DB_site->query("
		UPDATE hive_user
		SET skinid = $newskinid
		WHERE skinid = $askinid
	");

	adminlog($askinid, true, 'kill', "Users will now be using skin $newskinid");
	cp_redirect('The skin has been removed.', 'skin.php');
}

// ############################################################################
// Remove skin
if ($cmd == 'remove') {
	cp_nav('skinmodify');
	$askin = getinfo('skin', $askinid);
	if ($askinid == 1) {
		adminlog($askinid, false);
		cp_error('You cannot remove the original skin.');
	}

	adminlog($askinid);
	startform('skin.php', 'kill', 'Are you sure you want to remove this skin?');
	starttable('Remove skin "'.$askin['title'].'" (ID: '.$askinid.')');
	textrow('Are you <b>sure</b> you want to remove this skin? This procedure <b>cannot</b> be reversed!');
	tableselect('New skin for users that are using this skin:', 'newskinid', 'skin', -1, "skinid <> $askinid");
	hiddenfield('askinid', $askinid);
	endform('Remove Skin', '', 'Go Back');
	endtable();
}

// ############################################################################
// Create a new skin or update an existing one
if ($_POST['cmd'] == 'update') {
	cp_nav('skinmodify');
	if (empty($askin['title'])) {
		adminlog($askinid, false);
		cp_error('The skin must have a title.');
	} else {
		$askin['vars'] = export_array($askin['vars']);

		if ($askinid == 0) {
			// Create new template set
			if ($askin['templatesetid'] == -1) {
				$DB_site->auto_query('templateset', array('title' => $askin['title']));
				$askin['templatesetid'] = $DB_site->insert_id();
			}

			// Create the skin
			$DB_site->auto_query('skin', $askin);
			$askinid = $DB_site->insert_id();

			// Make it available for all users by default
			$DB_site->query("
				UPDATE hive_usergroup
				SET allowedskins = CONCAT(allowedskins, ',$askinid')
			");

			adminlog($askinid, true);
			cp_redirect('The skin has been created.', 'skin.php');
		} else {
			$DB_site->auto_query('skin', $askin, "skinid = $askinid");
			adminlog($askinid, true);
			cp_redirect('The skin has been updated.', 'skin.php');
		}
	}
}

// ############################################################################
// Create a new skin or update an existing one
if ($cmd == 'edit') {
	cp_nav('skinmodify');
	startform('skin.php', 'update', '', array('askin_title' => 'title'));

	$askin = getinfo('skin', $askinid, false, false);
	adminlog($askinid);
	if ($askin === false) {
		$askin = array(
			'title' => '',
			'templatesetid' => 'huzaah',
			'vars' => $_defskin,
		);
		$askinid = 0;
		starttable('Create New Skin');
	} else {
		eval('$askin["vars"] = '.$askin['vars'].';');
		starttable('Update skin "'.$askin['title'].'" (ID: '.$askinid.')');
	}

	$vars = array(
		'Settings' => array(
			'doctype' => array('HTML document type:<br /><span class="cp_small">The document type declaration that will be used on all pages.</span>', false),
			'images' => array('Images folder:<br /><span class="cp_small">(Without preceding or following slashes.)</span>', false),
			'body' => array('Extra attributes for the &lt;body&gt; tag:', false),
		),
		'Text and Fonts' => array(
			'note1' => 'This section controls the font, sizes and colors of text that are used in all pages.',
			'fontface' => array('Font face of text everywhere:', false),
			'note2' => 'Sizes:',
			'normalsize' => array('Size of regular text:', false),
			'smallsize' => array('Size of small text:', false),
			'note3' => 'Colors:',
			'tableheadfontcolor' => array('Color of text in table headers:', true),
			'timecolor' => array('Color of times-stamps:', true),
			'linkhovercolor' => array('Color of links when being hovered:', true),
			'highcolor' => array('Color of highlighted text:', true),
			'linkcolor' => array('Color of links:', true),
		),
		'Table Border Options' => array(
			'note1' => 'Normal table border (surrounds normal cells inside the table):',
			'border_normal_horizonal_width' => array('Width of horizontal borders (between rows):', false),
			'border_normal_vertical_width' => array('Width of vertical borders (between cells):', false),
			'border_normal_edges_width' => array('Width of edge borders (leftmost and rightmost):', false),
			'border_normal_color' => array('Color of borders:', true),
			'border_normal_style' => array('Borders style (solid, dotted, etc.):', false),
			'note2' => 'Table header border (surrounds header cells with table titles):',
			'border_header_horizonal_width' => array('Width of horizontal borders (between rows):', false),
			'border_header_vertical_width' => array('Width of vertical borders (between cells):', false),
			'border_header_edges_width' => array('Width of edge borders (leftmost and rightmost):', false),
			'border_header_color' => array('Color of borders:', true),
			'border_header_style' => array('Borders style (solid, dotted, etc.):', false),
		),
		'Table Background Colors' => array(
			'note1' => 'This section controls the background colors of table cells:',
			'tableheadbgcolor' => array('Background color of the table headers:', true),
			'firstalt' => array('First alternating color:', true),
			'secondalt' => array('Second alternating color:', true),
			'note2' => '</b>To use an image in the background, use this syntax: url(\'images/bg.gif\')<br />For more information on this subject please see <a href="http://www.w3schools.com/css/pr_background.asp" target="_blank">this page</a>.<b>',
		),
		'Other Colors' => array(
			'formbackground' => array('Background color of form elements:', true),
			'pagebgcolor' => array('Background color of the page:', true),
			'note2' => '</b>To use an image in the background, use this syntax: url(\'images/bg.gif\')<br />For more information on this subject please see <a href="http://www.w3schools.com/css/pr_background.asp" target="_blank">this page</a>.<b>',
		),
		'Custom Values' => array(
			'note1' => 'Use the values below in any template using the codes that appears next to them.',
		),
	);

	// Add extra custom fields
	foreach ($askin['vars'] as $varname => $varvalue) {
		if (substr($varname, 0, strlen('custom')) == 'custom' and !empty($varvalue)) {
			$vars['Custom Values']["$varname"] = array('$skin['.$varname.']:', false, false);
		}
	}
	for ($i = 0; $i < 5; $i++) {
		$vars['Custom Values']['custom'.count($vars['Custom Values'])] = array('$skin[custom'.count($vars['Custom Values']).']:', false, false);
	}

	inputfield('Title:', 'askin[title]', $askin['title']);
	hiddenfield('askinid', $askinid);
	if ($askinid == 0) {
		selectbox('Template set:', 'askin[templatesetid]', array('-1' => 'Create New Set') + table_to_array('templateset', 'templatesetid', '1 = 1', 'title'), -1);
	} else {
		tableselect('Template set: '.makelink('create new', 'templateset.php?cmd=edit', true), 'askin[templatesetid]', 'templateset', $askin['templatesetid']);
	}

	echo "<script language=\"JavaScript\">
<!--
var defskin = new Array();
var skin = new Array();

function changeInput(num) {
	if (eval('document.forms.form.skin'+num+'.value') != defskin[num]) {
		eval('document.forms.form.skin'+num).style.color = '#085858';
		if (eval('document.forms.form.color'+num)) {
			eval('document.forms.form.color'+num).style.background = defskin[num];
		}
		eval('document.forms.form.change'+num).value = 'Redo';
		return defskin[num];
	} else {
		eval('document.forms.form.skin'+num).style.color = '#cc3300';
		if (eval('document.forms.form.color'+num)) {
			eval('document.forms.form.color'+num).style.background = skin[num];
		}
		eval('document.forms.form.change'+num).value = 'Undo';
		return skin[num];
	}
}
// -->
</script>
";
	$i = 1;
	foreach ($vars as $catname => $itsvars) {
		if ($askinid == 0) {
			emptyrow(2);
			endtable();
		} else {
			tablehead(array('<input class="button" type="submit" value="  Update Skin  " />&nbsp;<input class="button" type="reset" value="  Reset Fields  " />'), 2);
			endtable();
		}
		starttable($catname);
		//tablehead(array($catname), 2);
		foreach ($itsvars as $varname => $varinfo) {
			if (substr($varname, 0, 4) == 'note') {
				textrow("<b>$varinfo</b>", 2);
				continue;
			}

			$description = $varinfo[0];
			$dobox = $varinfo[1];
			$dorevert = iif(isset($varinfo[2]), $varinfo[2], true);
			if ($dorevert) {
				echo "
				<script language=\"JavaScript\">
				<!--
				defskin[$i] = '".addslashes($_defskin["$varname"])."';
				skin[$i] = '".addslashes($askin['vars']["$varname"])."';
				// -->
				</script>";
			}
			inputfield($description, "askin[vars][$varname]\" onFocus=\"this.select();\" tabindex=\"$i\"".iif($dobox, " onChange=\"this.form.color$i.style.background = this.value;\"")." style=\"color: ".iif($askin['vars']["$varname"] != $_defskin["$varname"], '#cc3300', '#085858').";\" id=\"skin$i", $askin['vars']["$varname"], iif($dobox, 27, 35) - iif($dorevert and $askin['vars']["$varname"] != $_defskin["$varname"], 10, 0), iif($dorevert and $askin['vars']["$varname"] != $_defskin["$varname"], ' <input class="button" type="button" value="Undo" style="width: 56px; height: 17px; padding: 0px;" id="change'.$i.'" onClick="this.form.skin'.$i.'.value = changeInput('.$i.');">').iif($dobox, ' <input class="bginput" style="background: '.$askin['vars']["$varname"].'" type="button" value="       " disabled="disabled" style="width: 45px; height: 17px; padding: 0px;" id="color'.$i.'">'), true, false);
			$i++;
		}
	}

	if ($askinid == 0) {
		endform('Create Skin');
	} else {
		endform('Update Skin', 'Reset Fields');
	}
	endtable();
}

// ############################################################################
// List the skins
if ($cmd == 'modify') {
	cp_nav('skinmodify');
	adminlog();
	$cells = array(
		'ID',
		'Title',
		'Used By',
		'Options'
	);
	$askins = $DB_site->query('
		SELECT skin.*, COUNT(user.userid) AS users
		FROM hive_skin AS skin
		LEFT JOIN hive_user AS user USING (skinid)
		GROUP BY skin.skinid
	');
	if ($DB_site->num_rows($askins) < 1) {
		starttable('Error', '450');
		tablehead($cells);
		textrow('No skins found', count($cells), 1);
	} else {
		starttable('Existing Skins', '450');
		tablehead($cells);
		while ($askin = $DB_site->fetch_array($askins)) {
			$cells = array(
				$askin['skinid'],
				"<a href=\"skin.php?cmd=edit&askinid=$askin[skinid]\">$askin[title]</a>",
				iif($askin['users'] > 0, "<a href=\"user.php?cmd=results&find[eskinid]=$askin[skinid]\">$askin[users] user".iif($askin['users'] != 1, 's').'</a>', "$askin[users] users"),
				makelink('view', "../".INDEX_FILE."?skinid=$askin[skinid]", 1) . '-' . makelink('edit', "skin.php?cmd=edit&askinid=$askin[skinid]") . iif($askin['skinid'] != 1, '-' . makelink('remove', "skin.php?cmd=remove&askinid=$askin[skinid]"))
			);
			tablerow($cells);
		}
	}
	emptyrow(count($cells));
	endtable();

	button('', 'Create New Skin', "window.location='skin.php?cmd=edit'", true);

	starttable('Help', '450');
	textrow('A <b>skin</b> for HiveMail&trade; consists of two parts: variables and templates.<br /><br />
	Each skin has a set of variables you can change. These variables control text colors, font face, table borders and other options. To edit these variables, click the <i>[edit]</i> link next to the appropriate skin.<br /><br />
	Each skin also has a set of templates that are used to display content. That set can be defined in the skin options, and it points to a <b><a href="templateset.php">template set</a></b> you have created already.<br /><br />
	This way, two or more skins can use the same template sets, but different variables. All skins using a particular template set would have the same basic look, but colors, fonts, etc. could be different per skin.<br /><br />
	(The original skin (ID: 1) cannot be removed.)');
	endtable();
}

cp_footer();
?>