<?php if (!defined('VB_ENTRY')) die('Access denied.');
/*========================================================================*\
|| ###################################################################### ||
|| # vBulletin 6.2.3 - Licence Number L3138846
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2026 MH Sub I, LLC dba vBulletin. 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   # ||
|| ###################################################################### ||
\*========================================================================*/

// ###################### Start getHelpPhraseName #######################
// return the correct short name for a help topic
function fetch_help_phrase_short_name($item, $suffix = '')
{
	$parts = [$item['script']];
	if ($item['action'])
	{
		$parts[] = str_replace(',', '_', $item['action']);
	}

	if ($item['optionname'])
	{
		$parts[] = $item['optionname'];
	}

	return implode('_', $parts) . $suffix;
}

function get_help_export_xml($product)
{
	$assertor = vB::getDbAssertor();

	$products = [$product];
	if ($product == 'vbulletin')
	{
		$products[] = '';
	}

	$conditions = [
		'volatile' => 1,
		'product' => $products,
	];
	$topics = $assertor->select('vBForum:adminhelp', $conditions, ['script', 'action', 'optionname']);
	$helptopics = [];
	$phrase_names = [];
	foreach ($topics AS $topic)
	{
		$topic['phrase_name'] = fetch_help_phrase_short_name($topic);
		$phrase_names[] = $topic['phrase_name'] . '_title';
		$phrase_names[] = $topic['phrase_name'] . '_text';

		$helptopics[$topic['script']][] = $topic;
	}
	unset($topic);

	$conditions = [
		'languageid' => -1,
		'varname' => $phrase_names,
	];
	$phrases = $assertor->getRows('phrase', $conditions, false, 'varname');
	$xml = new vB_XML_Builder();

	$xml->add_group('helptopics', [
		'vbversion' => vB::getDatastore()->getOption('templateversion'),
		'product' => $product,
		'hasphrases' => 1,
	]);

	ksort($helptopics);
	foreach ($helptopics AS $script => $scripttopics)
	{
		$xml->add_group('helpscript', ['name' => $script]);
		foreach ($scripttopics AS $topic)
		{
			$attr = ['disp' => $topic['displayorder']];
			if ($topic['action'])
			{
				$attr['act'] = $topic['action'];
			}
			if ($topic['optionname'])
			{
				$attr['opt'] = $topic['optionname'];
			}

			$title = $phrases[$topic['phrase_name'] . '_title'];
			$text = $phrases[$topic['phrase_name'] . '_text'];

			if (!empty($title) OR !empty($text))
			{
				$xml->add_group('helptopic', $attr);

				$title_attributes = [
					'date' => $title['dateline'],
					'username' => $title['username'],
					'version' => htmlspecialchars_uni($title['version'])
				];
				$xml->add_tag('title', $title['text'], $title_attributes);

				$text_attributes = [
					'date' => $text['dateline'],
					'username' => $text['username'],
					'version' => htmlspecialchars_uni($text['version'])
				];
				$xml->add_tag('text', $text['text'], $text_attributes);

				$xml->close_group();
			}
			else
			{
				$xml->add_tag('helptopic', '', $attr);
			}
		}
		$xml->close_group();
	}

	$xml->close_group();

	$doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n";

	$doc .= $xml->output();
	return $doc;
}

// ###################### Start xml_import_helptopics #######################
// import XML help topics - call this function like this:
function xml_import_help_topics($xml = false)
{
	global $vbulletin;

	$assertor = vB::getDbAssertor();

	$vbphrase = vB_Api::instanceInternal('phrase')->renderPhrasesNoShortcode([
		'please_wait' => 'please_wait',
		'importing_admin_help' => 'importing_admin_help',
	]);
	$vbphrase = $vbphrase['phrases'];
	print_dots_start('<b>' . $vbphrase['importing_admin_help'] . "</b>, $vbphrase[please_wait]", ':', 'dspan');

	try
	{
		$xmlobj = new vB_XML_Parser($xml);
		if ($xmlobj->error_no())
		{
			throw new vB_Exception_AdminStopMessage('no_xml_and_no_path');
		}

		if (!$arr = $xmlobj->parse())
		{
			throw new vB_Exception_AdminStopMessage(['xml_error_x_at_line_y', $xmlobj->error_string(), $xmlobj->error_line()]);
		}

		if (empty($arr['helpscript']))
		{
			throw new vB_Exception_AdminStopMessage('invalid_file_specified');
		}

		$product = (empty($arr['product']) ? 'vbulletin' : $arr['product']);
		$has_phrases = (!empty($arr['hasphrases']));

		$products = [$product];
		if ($product == 'vbulletin')
		{
			$products[] = '';
		}

		$assertor->delete('vBForum:adminhelp', [
			'volatile' => 1,
			'product' => $products,
		]);

		if ($has_phrases)
		{
			$assertor->delete('phrase', [
				'product' => $products,
				'fieldname' => 'cphelptext',
				'languageid' => -1,
			]);
		}

		foreach (vB_XML_Parser::getList($arr, 'helpscript') AS $helpscript)
		{
			$help_sql = [];
			$phrase_sql = [];
			$help_sql_len = 0;
			$phrase_sql_len = 0;

			foreach (vB_XML_Parser::getList($helpscript, 'helptopic') AS $topic)
			{
				$topic['opt'] = $topic['opt'] ?? '';
				$topic['act'] = $topic['act'] ?? '';

				$help_sql[] = "
					('" . $vbulletin->db->escape_string($helpscript['name']) . "',
					'" . $vbulletin->db->escape_string($topic['act']) . "',
					'" . $vbulletin->db->escape_string($topic['opt']) . "',
					" . intval($topic['disp']) . ",
					1,
					'" . $vbulletin->db->escape_string($product) . "')
				";
				$help_sql_len += strlen(end($help_sql));

				if ($has_phrases)
				{
					$phrase_name = fetch_help_phrase_short_name([
						'script' => $helpscript['name'],
						'action' => $topic['act'],
						'optionname' => $topic['opt']
					]);

					if (isset($topic['text']['value']))
					{
						$phrase_sql[] = "
							(-1,
							'cphelptext',
							'{$phrase_name}_text',
							'" . $vbulletin->db->escape_string($topic['text']['value']) . "',
							'" . $vbulletin->db->escape_string($product) . "',
							'" . $vbulletin->db->escape_string($topic['text']['username']) . "',
							" . intval($topic['text']['date']) . ",
							'" . $vbulletin->db->escape_string($topic['text']['version']) . "')
						";

						$phrase_sql_len += strlen(end($phrase_sql));

					}

					if (isset($topic['title']['value']))
					{
						$phrase_sql[] = "
							(-1,
							'cphelptext',
							'{$phrase_name}_title',
							'" . $vbulletin->db->escape_string($topic['title']['value']) . "',
							'" . $vbulletin->db->escape_string($product) . "',
							'" . $vbulletin->db->escape_string($topic['title']['username']) . "',
							" . intval($topic['title']['date']) . ",
							'" . $vbulletin->db->escape_string($topic['title']['version']) . "')
						";
						$phrase_sql_len += strlen(end($phrase_sql));
					}
				}

				if ($phrase_sql_len > 102400)
				{
					// insert max of 100k of phrases at a time
					/*insert query*/
					$vbulletin->db->query_write("
						REPLACE INTO " . TABLE_PREFIX . "phrase
							(languageid, fieldname, varname, text, product, username, dateline, version)
						VALUES
							" . implode(",\n", $phrase_sql)
					);

					$phrase_sql = [];
					$phrase_sql_len = 0;
				}

				if ($help_sql_len > 102400)
				{
					// insert max of 100k of phrases at a time
					/*insert query*/
					$vbulletin->db->query_write("
						REPLACE INTO " . TABLE_PREFIX . "adminhelp
							(script, action, optionname, displayorder, volatile, product)
						VALUES
							" . implode(",\n\t", $help_sql)
					);

					$help_sql = [];
					$help_sql_len = 0;
				}
			}

			if ($help_sql)
			{
				/*insert query*/
				$vbulletin->db->query_write("
					REPLACE INTO " . TABLE_PREFIX . "adminhelp
						(script, action, optionname, displayorder, volatile, product)
					VALUES
						" . implode(",\n\t", $help_sql)
				);
			}

			if ($phrase_sql)
			{
				/*insert query*/
				$vbulletin->db->query_write("
					REPLACE INTO " . TABLE_PREFIX . "phrase
						(languageid, fieldname, varname, text, product, username, dateline, version)
					VALUES
						" . implode(",\n", $phrase_sql)
				);
			}
		}
	}
	catch (vB_Exception_AdminStopMessage $e)
	{
		print_stop_message_array($e->getErrors());
	}
	finally
	{
		print_dots_stop();
	}

	require_once(DIR . '/includes/adminfunctions_language.php');
	build_language();
}

/*=========================================================================*\
|| #######################################################################
|| # Downloaded: 13:27, Sun Aug 30th 2026
|| # CVS: $RCSfile$ - $Revision: 121869 $
|| #######################################################################
\*=========================================================================*/
