<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 5.1.1 - Licence Number VBCUP51ZOY
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2014 vBulletin Solutions Inc. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('CVS_REVISION', '$RCSfile$ - $Revision: 77380 $');

// #################### PRE-CACHE TEMPLATES AND DATA ######################
global $phrasegroups, $specialtemplates, $vbphrase, $DEVDEBUG;
$phrasegroups = array('style');
$specialtemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once(dirname(__FILE__) . '/global.php');
require_once(DIR . '/includes/adminfunctions_template.php');

// ######################## CHECK ADMIN PERMISSIONS #######################
if (!can_administer('canadminstyles'))
{
	print_cp_no_permission();
}

$vbulletin->input->clean_array_gpc('r', array(
	'templateid' => vB_Cleaner::TYPE_INT,
	'dostyleid'  => vB_Cleaner::TYPE_INT,
));

// ############################# LOG ACTION ###############################
log_admin_action(iif($vbulletin->GPC['templateid'] != 0, "template id = " . $vbulletin->GPC['templateid'], iif($vbulletin->GPC['dostyleid'] != 0, "style id = " . $vbulletin->GPC['dostyleid'])));

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

$vb5_config =& vB::getConfig();
$assertor = vB::getDbAssertor();
print_cp_header($vbphrase['replacement_variable_manager_gstyle'], '', '<style type="text/css">.ldi li, .lsq a { font: 11px tahoma; list-style-type:disc; }</style>');

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'modify';
}

// *********************** kill *********************
if ($_POST['do'] == 'kill')
{
	vB_Api::instanceInternal('template')->delete($vbulletin->GPC['templateid']);
	print_rebuild_style($vbulletin->GPC['dostyleid'], '', 0, 0, 1, 0);

	$args = array();
	parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
	$args['do'] = 'modify';

	print_cp_redirect2('replacement', $args, 1);
}

// *********************** remove *********************
if ($_REQUEST['do'] == 'remove')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'group' => vB_Cleaner::TYPE_STR
	));

	$hidden = array();
	$hidden['dostyleid'] =& $vbulletin->GPC['dostyleid'];
	$hidden['group'] = $vbulletin->GPC['group'];
	print_delete_confirmation('template', $vbulletin->GPC['templateid'], 'replacement', 'kill', 'replacement_variable', $hidden, $vbphrase['please_be_aware_replacement_variable_is_inherited']);

}

// *********************** update *********************
if ($_POST['do'] == 'update')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'findtext'    => vB_Cleaner::TYPE_STR,
		'replacetext' => vB_Cleaner::TYPE_STR
	));

	$vbulletin->GPC['findtext'] = strtolower($vbulletin->GPC['findtext']);

	if ($vbulletin->GPC['findtext'] === '')
	{
		print_stop_message2('please_complete_required_fields');
	}

	if ($vbulletin->GPC['dostyleid'] != -1)
	{
		$style = vB_Library::instance('Style')->fetchStyleByID($vbulletin->GPC['dostyleid'], false);

		$templateids = unserialize($style['templatelist']);
		$conditions[] = array('field' => 'templateid','value' => $templateids, 'operator' => vB_dB_Query::OPERATOR_EQ);

	}
	else
	{
		$conditions[] = array('field' => 'styleid','value' => -1, 'operator' => vB_dB_Query::OPERATOR_EQ);
	}

	$templates = $assertor->assertQuery('template',
		array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT,
			vB_dB_Query::CONDITIONS_KEY => $conditions
		)
	);

	if ($templates AND $templates->valid())
	{
		foreach ($templates AS $template)
		{
			$template_cache[strtolower($template['title'])] = $template;
		}
	}

	$changed = false;
	if (isset($template_cache["{$vbulletin->GPC['findtext']}"]))
	{
		$existing =& $template_cache["{$vbulletin->GPC['findtext']}"];
		// prevent duplicates
		if ($vbulletin->GPC['templateid'] == 0 AND $existing['styleid'] == $vbulletin->GPC['dostyleid'])
		{
			print_stop_message2(array(
				'replacement_already_exists',
				htmlspecialchars($existing['title']),
				htmlspecialchars($existing['template']),
				"replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;dostyleid=$existing[styleid]&amp;templateid=$existing[templateid]"
			));
		}

		if ($existing['template'] != $vbulletin->GPC['replacetext'])
		{
			$changed = true;
			if ($existing['styleid'] != $vbulletin->GPC['dostyleid'])
			{
				$assertor->assertQuery('template',
					array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_INSERT,
						'styleid' => $vbulletin->GPC['dostyleid'],
						'templatetype' => 'replacement',
						'title' => $vbulletin->GPC['findtext'],
						'template' => $vbulletin->GPC['replacetext'],
						'username' => $vbulletin->userinfo['username'],
						'version' => $vbulletin->options['templateversion'],
						'product' => 'vbulletin',
						'dateline' => TIMENOW,
					)
				);
			}
			else
			{
				$assertor->assertQuery('template',
					array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_UPDATE,
						'template' => $vbulletin->GPC['replacetext'],
						'username' => $vbulletin->userinfo['username'],
						'version' => $vbulletin->options['templateversion'],
						'product' => 'vbulletin',
						'dateline' => TIMENOW,
						 vB_dB_Query::CONDITIONS_KEY => array(
							'templateid' => $vbulletin->GPC['templateid']
						 )
					)
				);
			}
		}
	}
	else
	{
		$changed = true;
		/*insert query*/
		$assertor->assertQuery('template',
			array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_INSERT,
				'styleid' => $vbulletin->GPC['dostyleid'],
				'templatetype' => 'replacement',
				'title' => $vbulletin->GPC['findtext'],
				'template' => $vbulletin->GPC['replacetext'],
				'username' => $vbulletin->userinfo['username'],
				'version' => $vbulletin->options['templateversion'],
				'product' => 'vbulletin',
				'dateline' => TIMENOW,
			)
		);
	}
	$file = 'replacement';
	$args = array('do' => 'modify');
	if ($changed)
	{
		print_rebuild_style($vbulletin->GPC['dostyleid'], iif($vbulletin->GPC['dostyleid'] == -1, MASTERSTYLE, $style['title']), 0, 0, 1, 0);
		print_cp_redirect2($file, $args, 1);
	}
	else
	{
		print_stop_message2('nothing_to_do',$file, $args);
	}
}

// *********************** edit *********************
if ($_REQUEST['do'] == 'edit')
{
	$style = vB_Library::instance('Style')->fetchStyleByID($vbulletin->GPC['dostyleid'], false);
	$replacement = vB_Api::instance('template')->fetchByID($vbulletin->GPC['templateid']);

	print_form_header('replacement', 'update');
	construct_hidden_code('templateid', $vbulletin->GPC['templateid']);
	construct_hidden_code('dostyleid', $vbulletin->GPC['dostyleid']);
	construct_hidden_code('findtext', $replacement['title']);
	if ($replacement['styleid'] == $vbulletin->GPC['dostyleid'])
	{
		print_table_header(construct_phrase($vbphrase['x_y_id_z'], $vbphrase['replacement_variable'], htmlspecialchars_uni($replacement['title']), $replacement['templateid']));
	}
	else
	{
		print_table_header(construct_phrase($vbphrase['customize_replacement_variable_x'], htmlspecialchars_uni($replacement['title'])));
	}
	print_label_row($vbphrase['style'], iif($vbulletin->GPC['dostyleid'] == -1, MASTERSTYLE, $style['title']));
	print_label_row("$vbphrase[search_for_text] <dfn>($vbphrase[case_insensitive])</dfn>", htmlspecialchars_uni($replacement['title']));
	print_textarea_row($vbphrase['replace_with_text'], 'replacetext', $replacement['template'], 5, 50);
	print_submit_row($vbphrase['save']);

}

// *********************** insert *********************
if ($_POST['do'] == 'insert')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'findtext'    => vB_Cleaner::TYPE_STR,
		'replacetext' => vB_Cleaner::TYPE_STR,
	));

	$vbulletin->GPC['findtext'] = strtolower($vbulletin->GPC['findtext']);

	if ($vbulletin->GPC['findtext'] === '')
	{
		print_stop_message2('please_complete_required_fields');
	}

	$existing = $assertor->getRow('template',
		array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT,
				vB_dB_Query::CONDITIONS_KEY => array(
					'styleid' => $vbulletin->GPC['dostyleid'],
					'templatetype' => 'replacement',
					'title' => $vbulletin->GPC['findtext'],
				)
		)
	);
	if ($existing)
	{
		print_stop_message2(array(
			'replacement_already_exists',
			htmlspecialchars($existing['title']),
			htmlspecialchars($existing['template']),
			"replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;dostyleid=$existing[styleid]&amp;templateid=$existing[templateid]"
		));
	}
	else
	{
		/*insert query*/
		$assertor->assertQuery('template',
			array(vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_INSERT,
				'styleid' => $vbulletin->GPC['dostyleid'],
				'templatetype' => 'replacement',
				'title' => $vbulletin->GPC['findtext'],
				'template' => $vbulletin->GPC['replacetext'],
				'username' => $vbulletin->userinfo['username'],
				'version' => $vbulletin->options['templateversion'],
				'product' => 'vbulletin',
				'dateline' => TIMENOW,
			)
		);
		$style = vB_Library::instance('Style')->fetchStyleByID($vbulletin->GPC['dostyleid'], false);
		print_rebuild_style($vbulletin->GPC['dostyleid'], iif($vbulletin->GPC['dostyleid'] == -1, MASTERSTYLE, $style['title']), 0, 0, 1, 0);
		$args = array();
		parse_str(vB::getCurrentSession()->get('sessionurl'),$args);
		$args['do'] = 'modify';

		print_cp_redirect2('replacement', $args, 1);
	}
}

// *********************** add *********************
if ($_REQUEST['do'] == 'add')
{
	print_form_header('replacement', 'insert');
	print_table_header($vbphrase['add_new_replacement_variable']);
	print_style_chooser_row('dostyleid', $vbulletin->GPC['dostyleid'], MASTERSTYLE, $vbphrase['style'], iif($vb5_config['Misc']['debug'] == 1, 1, 0));
	print_input_row("$vbphrase[search_for_text] <dfn>($vbphrase[case_insensitive])</dfn>", 'findtext', '');
	print_textarea_row($vbphrase['replace_with_text'], 'replacetext', '', 5, 50);
	print_submit_row($vbphrase['save']);

}

// *********************** modify *********************
if ($_REQUEST['do'] == 'modify')
{
	print_form_header('', '');
	print_table_header($vbphrase['color_key']);
	print_description_row('
	<div class="darkbg" style="border: 2px inset;"><ul class="darkbg">
		<li class="col-g">' . $vbphrase['replacement_variable_is_unchanged_from_the_default_style'] . '</li>
		<li class="col-i">' . $vbphrase['replacement_variable_is_inherited_from_a_parent_style'] . '</li>
		<li class="col-c">' . $vbphrase['replacement_variable_is_customized_in_this_style'] . '</li>
	</ul></div>
	');
	print_table_footer();

	echo "<center>\n";
	echo "<div class=\"tborder\" style=\"width: 100%\">";
	echo "<div class=\"tcat\" style=\"padding:4px\" align=\"center\"><b>$vbphrase[replacement_variables]</b></div>\n";
	echo "<div class=\"alt1\" style=\"padding: 8px\">";
	echo "<div class=\"darkbg\" style=\"padding: 4px; border: 2px inset; text-align: " . vB_Template_Runtime::fetchStyleVar('left') . "\">\n";

	print_replacements();

	echo "</div></div></div>\n</center>\n";
}

// Display Function //
function print_replacements($parentid = -1, $indent = "\t")
{
	global $vbulletin, $vbphrase;
	static $stylecache, $donecache = array();

	$vb5_config =& vB::getConfig();
	$assertor = vB::getDbAssertor();

	if ($parentid == -1 AND $vb5_config['Misc']['debug'])
	{
		echo "<ul class=\"lsq\">\n";
		echo "\t<li><b>" . MASTERSTYLE . "</b>" . construct_link_code($vbphrase['add_new_replacement_variable'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=add&amp;dostyleid=-1") . "\n";
		echo "\t\t<ul class=\"ldi\">\n";
		$templates = $assertor->getRows('template',	array('templatetype' => 'replacement', 'styleid' => $parentid));

		if ($templates)
		{
			foreach ($templates AS $template)
			{
				echo "\t\t<li>" . htmlspecialchars_uni($template['title']) . construct_link_code($vbphrase['edit'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;dostyleid=-1&amp;templateid=$template[templateid]").construct_link_code($vbphrase['delete'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=remove&amp;dostyleid=-1&amp;templateid=$template[templateid]") . "\n";
			}
		}
		else
		{
			echo "\t\t\t<li>" . $vbphrase['no_replacements_defined'] . "</li>\n";
		}
		echo "\t\t</ul><br />\n\t</li>\n</ul>\n<hr size=\"1\" />\n";
	}

	if (empty($stylecache))
	{
		$styles = vB_Library::instance('Style')->fetchStyles(false, true);

		foreach ($styles AS $style)
		{
			$stylecache[$style[parentid]][$style[displayorder]][$style[styleid]] = $style;
		}
	}

	// Check style actually exists / has children
	if (!isset($stylecache[$parentid]))
	{
		return;
	}

	foreach ($stylecache[$parentid] AS $holder)
	{
		echo "$indent<ul class=\"lsq\">\n";
		foreach ($holder AS $styleid => $style)
		{
			echo "$indent<li><b>$style[title]</b>" . construct_link_code($vbphrase['add_new_replacement_variable'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=add&amp;dostyleid=$styleid") . "\n";
			echo "\t$indent<ul class=\"ldi\">\n";
			$templateids = unserialize($style['templatelist']);

			$templates = $assertor->getRows('getReplacementTemplates',	array('templateids' => $templateids));

			if($templates)
			{
				foreach ($templates AS $template)
				{
					if (in_array($template['templateid'], $donecache))
					{
						echo "\t\t$indent<li class=\"col-i\">" . htmlspecialchars_uni($template['title']) . construct_link_code($vbphrase['customize_gstyle'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;dostyleid=$styleid&amp;templateid=$template[templateid]") . "</li>\n";
					}
					else if ($template['styleid'] != -1)
					{
						echo "\t\t$indent<li class=\"col-c\">" . htmlspecialchars_uni($template['title']) . construct_link_code($vbphrase['edit'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;dostyleid=$styleid&amp;templateid=$template[templateid]") . construct_link_code($vbphrase['delete'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=remove&amp;dostyleid=$styleid&amp;templateid=$template[templateid]") . "</li>\n";
						$donecache[] = $template['templateid'];
					}
					else
					{
						echo "\t\t$indent<li class=\"col-g\">" . htmlspecialchars_uni($template['title']) . construct_link_code($vbphrase['customize_gstyle'], "replacement.php?" . vB::getCurrentSession()->get('sessionurl') . "do=edit&amp;dostyleid=$styleid&amp;templateid=$template[templateid]") . "</li>\n";
					}
				}
			}
			else
			{
				echo "\t\t$indent<li>" . $vbphrase['no_replacements_defined'] . "</li>\n";
			}

			echo "$indent\t</ul><br />\n";
			print_replacements($styleid, "$indent\t");
			echo "$indent</li>\n";
		}

		echo "$indent</ul>\n";
		if ($style['parentid'] == -1)
		{
			echo "<hr size=\"1\" />\n";
		}
	}
}

unset($DEVDEBUG);
print_cp_footer();

/*======================================================================*\
|| ####################################################################
|| # Downloaded: 07:00, Thu May 15th 2014
|| # CVS: $RCSfile$ - $Revision: 77380 $
|| ####################################################################
\*======================================================================*/
?>
