<commit_before><?php

/**
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This software is a derived product, based on:
 *
 * Simple Machines Forum (SMF)
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.0 Alpha
 *
 * Moderation helper functions.
 *
 */

if (!defined('ELK'))
	die('No access...');

/**
 * How many open reports do we have?
 *  - if flush is true will clear the moderator menu count
 *  - returns the number of open reports
 *  - sets $context['open_mod_reports'] for template use
 *
 * @param boolean $flush = true if moderator menu count will be cleared
 */
function recountOpenReports($flush = true)
{
	global $user_info, $context;

	$db = database();

	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}log_reported
		WHERE ' . $user_info['mod_cache']['bq'] . '
			AND closed = {int:not_closed}
			AND ignore_all = {int:not_ignored}',
		array(
			'not_closed' => 0,
			'not_ignored' => 0,
		)
	);
	list ($open_reports) = $db->fetch_row($request);
	$db->free_result($request);

	$_SESSION['rc'] = array(
		'id' => $user_info['id'],
		'time' => time(),
		'reports' => $open_reports,
	);

	$context['open_mod_reports'] = $open_reports;
	if ($flush)
		cache_put_data('num_menu_errors', null, 900);
	return $open_reports;
}

/**
 * How many unapproved posts and topics do we have?
 * 	- Sets $context['total_unapproved_topics']
 *  - Sets $context['total_unapproved_posts']
 *  - approve_query is set to list of boards they can see
 *
 * @param string $approve_query
 * @return array of values
 */
function recountUnapprovedPosts($approve_query = null)
{
	global $context;

	$db = database();

	if ($approve_query === null)
		return array('posts' => 0, 'topics' => 0);

	// Any unapproved posts?
	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}messages AS m
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_first_msg != m.id_msg)
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
		WHERE m.approved = {int:not_approved}
			AND {query_see_board}
			' . $approve_query,
		array(
			'not_approved' => 0,
		)
	);
	list ($unapproved_posts) = $db->fetch_row($request);
	$db->free_result($request);

	// What about topics?
	$request = $db->query('', '
		SELECT COUNT(m.id_topic)
		FROM {db_prefix}topics AS m
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
		WHERE m.approved = {int:not_approved}
			AND {query_see_board}
			' . $approve_query,
		array(
			'not_approved' => 0,
		)
	);
	list ($unapproved_topics) = $db->fetch_row($request);
	$db->free_result($request);

	$context['total_unapproved_topics'] = $unapproved_topics;
	$context['total_unapproved_posts'] = $unapproved_posts;
	return array('posts' => $unapproved_posts, 'topics' => $unapproved_topics);
}

/**
 * How many falied emails (that they can see) do we have?
 *
 * @param string $approve_query
 */
function recountFailedEmails($approve_query = null)
{
	global $context;

	$db = database();

	if ($approve_query === null)
		return 0;

	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}postby_emails_error AS m
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
		WHERE {query_see_board}
			' . $approve_query . '
			OR m.id_board = -1',
		array(
		)
	);
	list ($failed_emails) = $db->fetch_row($request);
	$db->free_result($request);

	$context['failed_emails'] = $failed_emails;
	return $failed_emails;
}

/**
 * How many entries are we viewing?
 */
function totalReports($status = 0)
{
	global $user_info;

	$db = database();

	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}log_reported AS lr
		WHERE lr.closed = {int:view_closed}
			AND ' . ($user_info['mod_cache']['bq'] == '1=1' || $user_info['mod_cache']['bq'] == '0=1' ? $user_info['mod_cache']['bq'] : 'lr.' . $user_info['mod_cache']['bq']),
		array(
			'view_closed' => $status,
		)
	);
	list ($total_reports) = $db->fetch_row($request);
	$db->free_result($request);

	return $total_reports;
}

/**
 * Changes a property of all the reports passed (and the user can see)
 *
 * @param array an array of report IDs
 * @param string the property to update ('close' or 'ignore')
 * @param int the status of the property (mainly: 0 or 1)
 */
function updateReportsStatus($reports_id, $property = 'close', $status = 0)
{
	global $user_info;

	if (empty($reports_id))
		return;

	$db = database();

	$reports_id = is_array($reports_id) ? $reports_id : array($reports_id);

	$db->query('', '
		UPDATE {db_prefix}log_reported
		SET ignore_all = {int:ignore_all}
		WHERE id_report IN ({array_int:report_list})
			AND ' . $user_info['mod_cache']['bq'],
		array(
			'report_list' => $reports_id,
			'is_closed' => 1,
		)
	);

	return $db->affected_rows();
}

/**
 * Loads the number of items awaiting moderation attention
 *  - Only loads the value a given permission level can see
 *  - If supplied a board number will load the values only for that board
 *  - Unapproved posts
 *  - Unapproved topics
 *  - Unapproved attachments
 *  - Failed emails
 *  - Reported posts
 *  - Members awaiting approval (activation, deletion, group requests)
 *
 * @param int $brd
 */
function loadModeratorMenuCounts($brd = null)
{
	global $modSettings, $user_info;

	static $menu_errors = array();

	// Work out what boards they can work in!
	$approve_boards = !empty($user_info['mod_cache']['ap']) ? $user_info['mod_cache']['ap'] : boardsAllowedTo('approve_posts');

	// Supplied a specific board to check?
	if (!empty($brd))<commit_msg>fixes #993 - db value error ignore_all Signed-off-by:<NAME> <<EMAIL>><commit_after><?php

/**
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This software is a derived product, based on:
 *
 * Simple Machines Forum (SMF)
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.0 Alpha
 *
 * Moderation helper functions.
 *
 */

if (!defined('ELK'))
	die('No access...');

/**
 * How many open reports do we have?
 *  - if flush is true will clear the moderator menu count
 *  - returns the number of open reports
 *  - sets $context['open_mod_reports'] for template use
 *
 * @param boolean $flush = true if moderator menu count will be cleared
 */
function recountOpenReports($flush = true)
{
	global $user_info, $context;

	$db = database();

	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}log_reported
		WHERE ' . $user_info['mod_cache']['bq'] . '
			AND closed = {int:not_closed}
			AND ignore_all = {int:not_ignored}',
		array(
			'not_closed' => 0,
			'not_ignored' => 0,
		)
	);
	list ($open_reports) = $db->fetch_row($request);
	$db->free_result($request);

	$_SESSION['rc'] = array(
		'id' => $user_info['id'],
		'time' => time(),
		'reports' => $open_reports,
	);

	$context['open_mod_reports'] = $open_reports;
	if ($flush)
		cache_put_data('num_menu_errors', null, 900);
	return $open_reports;
}

/**
 * How many unapproved posts and topics do we have?
 * 	- Sets $context['total_unapproved_topics']
 *  - Sets $context['total_unapproved_posts']
 *  - approve_query is set to list of boards they can see
 *
 * @param string $approve_query
 * @return array of values
 */
function recountUnapprovedPosts($approve_query = null)
{
	global $context;

	$db = database();

	if ($approve_query === null)
		return array('posts' => 0, 'topics' => 0);

	// Any unapproved posts?
	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}messages AS m
			INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_first_msg != m.id_msg)
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)
		WHERE m.approved = {int:not_approved}
			AND {query_see_board}
			' . $approve_query,
		array(
			'not_approved' => 0,
		)
	);
	list ($unapproved_posts) = $db->fetch_row($request);
	$db->free_result($request);

	// What about topics?
	$request = $db->query('', '
		SELECT COUNT(m.id_topic)
		FROM {db_prefix}topics AS m
			INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
		WHERE m.approved = {int:not_approved}
			AND {query_see_board}
			' . $approve_query,
		array(
			'not_approved' => 0,
		)
	);
	list ($unapproved_topics) = $db->fetch_row($request);
	$db->free_result($request);

	$context['total_unapproved_topics'] = $unapproved_topics;
	$context['total_unapproved_posts'] = $unapproved_posts;
	return array('posts' => $unapproved_posts, 'topics' => $unapproved_topics);
}

/**
 * How many falied emails (that they can see) do we have?
 *
 * @param string $approve_query
 */
function recountFailedEmails($approve_query = null)
{
	global $context;

	$db = database();

	if ($approve_query === null)
		return 0;

	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}postby_emails_error AS m
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
		WHERE {query_see_board}
			' . $approve_query . '
			OR m.id_board = -1',
		array(
		)
	);
	list ($failed_emails) = $db->fetch_row($request);
	$db->free_result($request);

	$context['failed_emails'] = $failed_emails;
	return $failed_emails;
}

/**
 * How many entries are we viewing?
 */
function totalReports($status = 0)
{
	global $user_info;

	$db = database();

	$request = $db->query('', '
		SELECT COUNT(*)
		FROM {db_prefix}log_reported AS lr
		WHERE lr.closed = {int:view_closed}
			AND ' . ($user_info['mod_cache']['bq'] == '1=1' || $user_info['mod_cache']['bq'] == '0=1' ? $user_info['mod_cache']['bq'] : 'lr.' . $user_info['mod_cache']['bq']),
		array(
			'view_closed' => $status,
		)
	);
	list ($total_reports) = $db->fetch_row($request);
	$db->free_result($request);

	return $total_reports;
}

/**
 * Changes a property of all the reports passed (and the user can see)
 *
 * @param array an array of report IDs
 * @param string the property to update ('close' or 'ignore')
 * @param int the status of the property (mainly: 0 or 1)
 */
function updateReportsStatus($reports_id, $property = 'close', $status = 0)
{
	global $user_info;

	if (empty($reports_id))
		return;

	$db = database();

	$reports_id = is_array($reports_id) ? $reports_id : array($reports_id);

	$db->query('', '
		UPDATE {db_prefix}log_reported
		SET ' . ($property == 'close' ? 'closed' : 'ignore_all') . '= {int:closed}
		WHERE id_report IN ({array_int:report_list})
			AND ' . $user_info['mod_cache']['bq'],
		array(
			'report_list' => $reports_id,
			'status' => $status,
		)
	);

	return $db->affected_rows();
}

/**
 * Loads the number of items awaiting moderation attention
 *  - Only loads the value a given permission level can see
 *  - If supplied a board number will load the values only for that board
 *  - Unapproved posts
 *  - Unapproved topics
 *  - Unapproved attachments
 *  - Failed emails
 *  - Reported posts
 *  - Members awaiting approval (activation, deletion, group requests)
 *
 * @param int $brd
 */
function loadModeratorMenuCounts($brd = null)
{
	global $modSettings, $user_info;

	static $menu_errors = array();

	// Work out what boards they can work in!
	$approve_boards = !empty($user_info['mod_cache']['ap']) ? $user_info['mod_cache']['ap'] : boardsAllowedTo('approve_posts');

	// Supplied a specific board to check?
	if (!empty($brd))
{'fraction_non_alphanumeric': 0.15686103351955308, 'fraction_numerical': 0.004888268156424581, 'mean_word_length': 4.107891217119929, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5915583', 'n_tokens_mistral': 4416, 'n_tokens_neox': 4013, 'n_words': 1043}