<commit_before><?php

/**
 * The main purpose of this file is to show a list of all errors that were
 * logged on the forum, and allow filtering and deleting them.
 *
 * Simple Machines Forum (SMF)
 *
 * @package SMF
 * @author Simple Machines http://www.simplemachines.org
 * @copyright 2019 Simple Machines and individual contributors
 * @license http://www.simplemachines.org/about/smf/license.php BSD
 *
 * @version 2.1 RC2
 */

if (!defined('SMF'))
	die('No direct access...');

/**
 * View the forum's error log.
 * This function sets all the context up to show the error log for maintenance.
 * It requires the maintain_forum permission.
 * It is accessed from ?action=admin;area=logs;sa=errorlog.
 *
 * @uses the Errors template and error_log sub template.
 */
function ViewErrorLog()
{
	global $scripturl, $txt, $context, $modSettings, $user_profile, $filter, $smcFunc;

	// Viewing contents of a file?
	if (isset($_GET['file']))
		return ViewFile();

	// Viewing contents of a backtrace?
	if (isset($_GET['backtrace']))
		return ViewBacktrace();

	// Check for the administrative permission to do this.
	isAllowedTo('admin_forum');

	// Templates, etc...
	loadLanguage('ManageMaintenance');
	loadTemplate('Errors');

	// You can filter by any of the following columns:
	$filters = array(
		'id_member' => array(
			'txt' => $txt['username'],
			'operator' => '=',
			'datatype' => 'int',
		),
		'ip' => array(
			'txt' => $txt['ip_address'],
			'operator' => '=',
			'datatype' => 'inet',
		),
		'session' => array(
			'txt' => $txt['session'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'url' => array(
			'txt' => $txt['error_url'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'message' => array(
			'txt' => $txt['error_message'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'error_type' => array(
			'txt' => $txt['error_type'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'file' => array(
			'txt' => $txt['file'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'line' => array(
			'txt' => $txt['line'],
			'operator' => '=',
			'datatype' => 'int',
		),
	);

	// Set up the filtering...
	if (isset($_GET['value'], $_GET['filter']) && isset($filters[$_GET['filter']]))
		$filter = array(
			'variable' => $_GET['filter'],
			'value' => array(
				'sql' => in_array($_GET['filter'], array('message', 'url', 'file')) ? base64_decode(strtr($_GET['value'], array(' ' => '+'))) : $smcFunc['db_escape_wildcard_string']($_GET['value']),
			),
			'href' => ';filter=' . $_GET['filter'] . ';value=' . $_GET['value'],
			'entity' => $filters[$_GET['filter']]['txt']
		);

	// Deleting, are we?
	if (isset($_POST['delall']) || isset($_POST['delete']))
		deleteErrors();

	// Just how many errors are there?
	$result = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}log_errors' . (isset($filter) ? '
		WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : ''),
		array(
			'filter' => isset($filter) ? $filter['value']['sql'] : '',
		)
	);
	list ($num_errors) = $smcFunc['db_fetch_row']($result);
	$smcFunc['db_free_result']($result);

	// If this filter is empty...
	if ($num_errors == 0 && isset($filter))
		redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : ''));

	// Clean up start.
	if (!isset($_GET['start']) || $_GET['start'] < 0)
		$_GET['start'] = 0;

	// Do we want to reverse error listing?
	$context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up';

	// Set the page listing up.
	$context['page_index'] = constructPageIndex($scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : '') . (isset($filter) ? $filter['href'] : ''), $_GET['start'], $num_errors, $modSettings['defaultMaxListItems']);
	$context['start'] = $_GET['start'];

	// Update the error count
	if (!isset($filter))
		$context['num_errors'] = $num_errors;
	else
	{
		// We want all errors, not just the number of filtered messages...
		$query = $smcFunc['db_query']('', '
			SELECT COUNT(id_error)
			FROM {db_prefix}log_errors',
			array()
		);

		list($context['num_errors']) = $smcFunc['db_fetch_row']($query);
		$smcFunc['db_free_result']($query);
	}

	// Find and sort out the errors.
	$request = $smcFunc['db_query']('', '
		SELECT id_error, id_member, ip, url, log_time, message, session, error_type, file, line
		FROM {db_prefix}log_errors' . (isset($filter) ? '
		WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : '') . '
		ORDER BY id_error ' . ($context['sort_direction'] == 'down' ? 'DESC' : '') . '
		LIMIT {int:start}, {int:max}',
		array(
			'filter' => isset($filter) ? $filter['value']['sql'] : '',
			'start' => $_GET['start'],
			'max' => $modSettings['defaultMaxListItems'],
		)
	);
	$context['errors'] = array();
	$members = array();

	for ($i = 0; $row = $smcFunc['db_fetch_assoc']($request); $i++)
	{
		$search_message = preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '%', $smcFunc['db_escape_wildcard_string']($row['message']));
		if ($search_message == $filter['value']['sql'])
			$search_message = $smcFunc['db_escape_wildcard_string']($row['message']);
		$show_message = strtr(strtr(preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '$1', $row['message']), array("\r" => '', '<br>' => "\n", '<' => '&lt;', '>' => '&gt;', '"' => '&quot;')), array("\n" => '<br>'));

		$context['errors'][$row['id_error']] = array(
			'member' => array(
				'id' => $row['id_member'],
				'ip' => inet_dtop($row['ip']),
				'session' => $row['session']
			),
			'time' => timeformat($row['log_time']),
			'timestamp' => $row['log_time'],
			'url' => array(
				'html' => $smcFunc['htmlspecialchars'](strpos($row['url'], 'cron.php') === false ? (substr($row['url'], 0, 1) == '?' ? $scripturl : '') . $row['url'] : $row['url']),
				'href' => base64_encode($smcFunc['db_escape_wildcard_string']($row['url']))<commit_msg>Error log generates a error<commit_after><?php

/**
 * The main purpose of this file is to show a list of all errors that were
 * logged on the forum, and allow filtering and deleting them.
 *
 * Simple Machines Forum (SMF)
 *
 * @package SMF
 * @author Simple Machines http://www.simplemachines.org
 * @copyright 2019 Simple Machines and individual contributors
 * @license http://www.simplemachines.org/about/smf/license.php BSD
 *
 * @version 2.1 RC2
 */

if (!defined('SMF'))
	die('No direct access...');

/**
 * View the forum's error log.
 * This function sets all the context up to show the error log for maintenance.
 * It requires the maintain_forum permission.
 * It is accessed from ?action=admin;area=logs;sa=errorlog.
 *
 * @uses the Errors template and error_log sub template.
 */
function ViewErrorLog()
{
	global $scripturl, $txt, $context, $modSettings, $user_profile, $filter, $smcFunc;

	// Viewing contents of a file?
	if (isset($_GET['file']))
		return ViewFile();

	// Viewing contents of a backtrace?
	if (isset($_GET['backtrace']))
		return ViewBacktrace();

	// Check for the administrative permission to do this.
	isAllowedTo('admin_forum');

	// Templates, etc...
	loadLanguage('ManageMaintenance');
	loadTemplate('Errors');

	// You can filter by any of the following columns:
	$filters = array(
		'id_member' => array(
			'txt' => $txt['username'],
			'operator' => '=',
			'datatype' => 'int',
		),
		'ip' => array(
			'txt' => $txt['ip_address'],
			'operator' => '=',
			'datatype' => 'inet',
		),
		'session' => array(
			'txt' => $txt['session'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'url' => array(
			'txt' => $txt['error_url'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'message' => array(
			'txt' => $txt['error_message'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'error_type' => array(
			'txt' => $txt['error_type'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'file' => array(
			'txt' => $txt['file'],
			'operator' => 'LIKE',
			'datatype' => 'string',
		),
		'line' => array(
			'txt' => $txt['line'],
			'operator' => '=',
			'datatype' => 'int',
		),
	);

	// Set up the filtering...
	if (isset($_GET['value'], $_GET['filter']) && isset($filters[$_GET['filter']]))
		$filter = array(
			'variable' => $_GET['filter'],
			'value' => array(
				'sql' => in_array($_GET['filter'], array('message', 'url', 'file')) ? base64_decode(strtr($_GET['value'], array(' ' => '+'))) : $smcFunc['db_escape_wildcard_string']($_GET['value']),
			),
			'href' => ';filter=' . $_GET['filter'] . ';value=' . $_GET['value'],
			'entity' => $filters[$_GET['filter']]['txt']
		);

	// Deleting, are we?
	if (isset($_POST['delall']) || isset($_POST['delete']))
		deleteErrors();

	// Just how many errors are there?
	$result = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {db_prefix}log_errors' . (isset($filter) ? '
		WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : ''),
		array(
			'filter' => isset($filter) ? $filter['value']['sql'] : '',
		)
	);
	list ($num_errors) = $smcFunc['db_fetch_row']($result);
	$smcFunc['db_free_result']($result);

	// If this filter is empty...
	if ($num_errors == 0 && isset($filter))
		redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : ''));

	// Clean up start.
	if (!isset($_GET['start']) || $_GET['start'] < 0)
		$_GET['start'] = 0;

	// Do we want to reverse error listing?
	$context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up';

	// Set the page listing up.
	$context['page_index'] = constructPageIndex($scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : '') . (isset($filter) ? $filter['href'] : ''), $_GET['start'], $num_errors, $modSettings['defaultMaxListItems']);
	$context['start'] = $_GET['start'];

	// Update the error count
	if (!isset($filter))
		$context['num_errors'] = $num_errors;
	else
	{
		// We want all errors, not just the number of filtered messages...
		$query = $smcFunc['db_query']('', '
			SELECT COUNT(id_error)
			FROM {db_prefix}log_errors',
			array()
		);

		list($context['num_errors']) = $smcFunc['db_fetch_row']($query);
		$smcFunc['db_free_result']($query);
	}

	// Find and sort out the errors.
	$request = $smcFunc['db_query']('', '
		SELECT id_error, id_member, ip, url, log_time, message, session, error_type, file, line
		FROM {db_prefix}log_errors' . (isset($filter) ? '
		WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : '') . '
		ORDER BY id_error ' . ($context['sort_direction'] == 'down' ? 'DESC' : '') . '
		LIMIT {int:start}, {int:max}',
		array(
			'filter' => isset($filter) ? $filter['value']['sql'] : '',
			'start' => $_GET['start'],
			'max' => $modSettings['defaultMaxListItems'],
		)
	);
	$context['errors'] = array();
	$members = array();

	for ($i = 0; $row = $smcFunc['db_fetch_assoc']($request); $i++)
	{
		$search_message = preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '%', $smcFunc['db_escape_wildcard_string']($row['message']));

		if (isset($filter) && $search_message == $filter['value']['sql'])
			$search_message = $smcFunc['db_escape_wildcard_string']($row['message']);

		$show_message = strtr(strtr(preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '$1', $row['message']), array("\r" => '', '<br>' => "\n", '<' => '&lt;', '>' => '&gt;', '"' => '&quot;')), array("\n" => '<br>'));

		$context['errors'][$row['id_error']] = array(
			'member' => array(
				'id' => $row['id_member'],
				'ip' => inet_dtop($row['ip']),
				'session' => $row['session']
			),
			'time' => timeformat($row['log_time']),
			'timestamp' => $row['log_time'],
			'url' => array(
				'html' => $smcFunc['htmlspecialchars'](strpos($row['url'], 'cron.php') === false ? (substr($row['url'], 0, 1) == '?' ? $scripturl : '') . $row['url'] : $row['url']),
				'href' => base64_encode($smcFunc['db_escape_wildcard_string']($row['url']))
{'fraction_non_alphanumeric': 0.2435625814863103, 'fraction_numerical': 0.0029335071707953064, 'mean_word_length': 4.5060565275908475, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6166328', 'n_tokens_mistral': 4731, 'n_tokens_neox': 4378, 'n_words': 906}