<?php
// VERSION 4.0.8
/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2008+ King Kovifor. All Rights Reserved.			  # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| #################################################################### ||
\*======================================================================*/
ini_set("display_errors", "1");
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

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

// #################### PRE-CACHE TEMPLATES AND DATA ######################
$phrasegroups = array('cpglobal');
$specialtemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

// ############################# CLEAN VARIABLES ###############################
$vbulletin->input->clean_array_gpc('g', array('do' => TYPE_NOHTML, 'mid' => TYPE_UINT, 'id' => TYPE_UINT));

if($vbulletin->GPC['do'] != 'download' OR $_POST['do'] != 'download')
{
	print_cp_header($vbphrase['eg_tfh_impex']);
}

if($vbulletin->GPC['do'] == '')
{
	print_form_header('eg_tfh_impex', 'doimport', 1, 1, 'uploadform"');
	print_table_header($vbphrase['eg_tfh_import']);
	print_upload_row($vbphrase['upload_xml_file'], 'modulefile', 999999999);
	print_input_row($vbphrase['import_xml_file'], 'serverfile', './includes/xml/eg_tfh-module.xml');
	print_submit_row($vbphrase['import'], 0);
	print_table_footer();
	
	print_form_header('eg_tfh_impex', 'download', 0, 1, 'downloadform" target="download');
	print_table_header($vbphrase['download']);
	print_input_row($vbphrase['filename'], 'filename', 'eg_tfh-module.xml');
	print_description_row($vbphrase['eg_tfh_m_modules'], 0, 2, 'tfoot');
	$modules = $db->query("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE `iden` = 'm'");
	while ($module = $db->fetch_array($modules))
	{
		print_label_row("<label for=\"cb$module[moduleid]\">
			<input type=\"checkbox\" id=\"cb$module[moduleid]\" name=\"download[]\" value=\"$module[tabid]\" />$module[name]</label>
		");
	}
	print_submit_row($vbphrase['download']);
	print_table_footer();
}

if($vbulletin->GPC['do'] == 'doimport' OR $_POST['do'] == 'doimport')
{
	$vbulletin->input->clean_array_gpc('p', array('serverfile' => TYPE_STR));
	$vbulletin->input->clean_array_gpc('f', array('modulefile' => TYPE_FILE));

	if (file_exists($vbulletin->GPC['modulefile']['tmp_name']))
	{
		$xml = file_read($vbulletin->GPC['modulefile']['tmp_name']);
	}
	else if (file_exists($vbulletin->GPC['serverfile']))
	{
		$xml = file_read($vbulletin->GPC['serverfile']);
	}
	else
	{
		print_stop_message('no_file_uploaded_and_no_local_file_found');
	}

	print_dots_start('<b>Importing Modules</b>, please wait.', ':', 'dspan');
	require_once(DIR . '/includes/class_xml.php');

	$vbxml = new vB_XML_Parser($xml);
	if ($vbxml->error_no == 1)
	{
			print_dots_stop();
			print_stop_message('no_xml_and_no_path');
	}

	if(!$arr = $vbxml->parse())
	{
		print_dots_stop();
		print_stop_message('xml_error_x_at_line_y', $vbxml->error_string(), $vbxml->error_line());
	}

	if (!is_array($arr['module'][0]))
	{
		$arr['module'] = array($arr['module']);
	}

	$maxid = $db->query_first("SELECT MAX(tabid) AS max FROM `" . TABLE_PREFIX . "eg_tfh_tabs`");

	foreach ($arr['module'] AS $module)
	{
		$db->query_write(fetch_query_sql($module, 'tabs'));
	}

	vBulletinHook::build_datastore($db);
	print_dots_stop();
	print_cp_redirect("eg_tfh_impex.php?" . $vbulletin->session->vars['sessionurl'], 0);
}

if($vbulletin->GPC['do'] == 'download' OR $_POST['do'] == 'download')
{
	$vbulletin->input->clean_array_gpc('p', array('filename' => TYPE_STR, 'download' => TYPE_ARRAY_UINT));

	if (empty($vbulletin->GPC['download']) OR empty($vbulletin->GPC['filename']))
	{
		print_stop_message('please_complete_required_fields');
	}

	require_once(DIR . '/includes/class_eg_tfh_yui.php');
	$vbxml = new vB_XML_Builder($vbulletin);
	$vbxml->add_group('modules');

	$modules = $db->query_read("SELECT * FROM `" . TABLE_PREFIX . "eg_tfh_tabs` WHERE tabid IN (" . implode(', ', $vbulletin->GPC['download']) . ")");
	while ($module = $db->fetch_array($modules))
	{
		$params = array();
		$vbxml->add_group('module', $params);

		$vbxml->add_tag('name', $module['name']);
		$vbxml->add_tag('file', $module['file']);
		$vbxml->add_tag('cache', $module['cache']);
		$vbxml->add_tag('iden', $module['iden']);
		$vbxml->add_tag('varname', $module['varname']);
		
		$vbxml->close_group();
	}

	$vbxml->close_group();

	$doctype = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n\r\n";

	$doctype .= $vbxml->output();
	$vbxml = null;

	require_once(DIR . '/includes/functions_file.php');
	file_download($doctype, $vbulletin->GPC['filename'], 'text/xml');
}

print_cp_footer();

?>