<commit_before><?php

/**
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.0 Alpha
 *
 * This file is mainly concerned with minor tasks relating to boards, such as
 * marking them read, collapsing categories, or quick moderation.
 *
 */

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

/**
 * This class handles a part of the actions to mak boards, topics, or replies, as read/unread.
 */
class MarkRead_Controller
{
	/**
	 * This is the main function for markasread file.
	 */
	public function action_index()
	{
		// These checks have been moved here.
		// Do NOT call the specific handlers directly.

		// Guests can't mark things.
		is_not_guest();

		checkSession('get');

		$redir = $this->_dispatch();

		redirectexit($redir);
	}

	/**
	 * This function forwards the request to the appropriate function.
	 */
	private function _dispatch()
	{
		// sa=all action_markboards()
		if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'all')
			$sa = 'action_markboards';
		elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'unreadreplies')
			// mark topics from unread
			$sa = 'action_markreplies';
		elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'topic')
			// mark a single topic as read
			$sa = 'action_marktopic';
		else
			// the rest, for now...
			$sa = 'action_markasread';

		return $this->{$sa}();
	}

	/**
	 * This is the main method for markasread controller when using APIs.
	 */
	public function action_index_api()
	{
		global $context, $txt;

		// Guests can't mark things.
		is_not_guest('', false);

		checkSession('get');

		$this->_dispatch();

		// For the time being this is a special case
		if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'all')
		{
			loadTemplate('Xml');

			Template_Layers::getInstance()->removeAll();
			$context['sub_template'] = 'generic_xml_buttons';

			$context['xml_data'] = array(
				'text' => $txt['unread_topics_visit_none'],
			);
		}
		else
			// No need to do anything, just die
			obExit(false);
	}

	/**
	 * Marks boards as read (or unread).
	 * Accessed by action=markasread;sa=all
	 */
	public function action_markboards()
	{
		global $modSettings;

		require_once(SUBSDIR . '/Boards.subs.php');

		// Find all the boards this user can see.
		$boards = accessibleBoards();

		if (!empty($boards))
			// Mark boards as read
			markBoardsRead($boards, isset($_REQUEST['unread']), true);

		$_SESSION['id_msg_last_visit'] = $modSettings['maxMsgID'];
		if (!empty($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'action=unread') !== false)
			return 'action=unread';

		if (isset($_SESSION['topicseen_cache']))
			$_SESSION['topicseen_cache'] = array();

		return '';
	}

	/**
	 * Marks the selected topics as read.
	 * Accessed by action=markasread;sa=unreadreplies
	 */
	public function action_markreplies()
	{
		global $user_info, $modSettings;

		$db = database();

		// Make sure all the topics are integers!
		$topics = array_map('intval', explode('-', $_REQUEST['topics']));

		require_once(SUBSDIR . '/Topic.subs.php');
		$logged_topics = getLoggedTopics($user_info['id'], $topics);

		$markRead = array();
		foreach ($topics as $id_topic)
			$markRead[] = array($user_info['id'], (int) $id_topic, $modSettings['maxMsgID'], $logged_topics[$id_topic]);

		markTopicsRead($markRead, true);

		if (isset($_SESSION['topicseen_cache']))
			$_SESSION['topicseen_cache'] = array();

		return 'action=unreadreplies';
	}

	/**
	 * Mark a single topic as unread.
	 * Accessed by action=markasread;sa=topic
	 */
	public function action_marktopic()
	{
		global $board, $topic, $user_info;

		$db = database();

		require_once(SUBSDIR . '/Topic.subs.php');
		require_once(SUBSDIR . '/Messages.subs.php');

		// Mark a topic unread.
		// First, let's figure out what the latest message is.
		$topicinfo = getTopicInfo($topic, 'all');
		$topic_msg_id = (int) $_GET['t'];
<commit_msg>db init wasn't needed.. Signed-off-by:<NAME> <<EMAIL>><commit_after><?php

/**
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.0 Alpha
 *
 * This file is mainly concerned with minor tasks relating to boards, such as
 * marking them read, collapsing categories, or quick moderation.
 *
 */

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

/**
 * This class handles a part of the actions to mak boards, topics, or replies, as read/unread.
 */
class MarkRead_Controller
{
	/**
	 * This is the main function for markasread file.
	 */
	public function action_index()
	{
		// These checks have been moved here.
		// Do NOT call the specific handlers directly.

		// Guests can't mark things.
		is_not_guest();

		checkSession('get');

		$redir = $this->_dispatch();

		redirectexit($redir);
	}

	/**
	 * This function forwards the request to the appropriate function.
	 */
	private function _dispatch()
	{
		// sa=all action_markboards()
		if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'all')
			$sa = 'action_markboards';
		elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'unreadreplies')
			// mark topics from unread
			$sa = 'action_markreplies';
		elseif (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'topic')
			// mark a single topic as read
			$sa = 'action_marktopic';
		else
			// the rest, for now...
			$sa = 'action_markasread';

		return $this->{$sa}();
	}

	/**
	 * This is the main method for markasread controller when using APIs.
	 */
	public function action_index_api()
	{
		global $context, $txt;

		// Guests can't mark things.
		is_not_guest('', false);

		checkSession('get');

		$this->_dispatch();

		// For the time being this is a special case
		if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'all')
		{
			loadTemplate('Xml');

			Template_Layers::getInstance()->removeAll();
			$context['sub_template'] = 'generic_xml_buttons';

			$context['xml_data'] = array(
				'text' => $txt['unread_topics_visit_none'],
			);
		}
		else
			// No need to do anything, just die
			obExit(false);
	}

	/**
	 * Marks boards as read (or unread).
	 * Accessed by action=markasread;sa=all
	 */
	public function action_markboards()
	{
		global $modSettings;

		require_once(SUBSDIR . '/Boards.subs.php');

		// Find all the boards this user can see.
		$boards = accessibleBoards();

		if (!empty($boards))
			// Mark boards as read
			markBoardsRead($boards, isset($_REQUEST['unread']), true);

		$_SESSION['id_msg_last_visit'] = $modSettings['maxMsgID'];
		if (!empty($_SESSION['old_url']) && strpos($_SESSION['old_url'], 'action=unread') !== false)
			return 'action=unread';

		if (isset($_SESSION['topicseen_cache']))
			$_SESSION['topicseen_cache'] = array();

		return '';
	}

	/**
	 * Marks the selected topics as read.
	 * Accessed by action=markasread;sa=unreadreplies
	 */
	public function action_markreplies()
	{
		global $user_info, $modSettings;

		// Make sure all the topics are integers!
		$topics = array_map('intval', explode('-', $_REQUEST['topics']));

		require_once(SUBSDIR . '/Topic.subs.php');
		$logged_topics = getLoggedTopics($user_info['id'], $topics);

		$markRead = array();
		foreach ($topics as $id_topic)
			$markRead[] = array($user_info['id'], (int) $id_topic, $modSettings['maxMsgID'], $logged_topics[$id_topic]);

		markTopicsRead($markRead, true);

		if (isset($_SESSION['topicseen_cache']))
			$_SESSION['topicseen_cache'] = array();

		return 'action=unreadreplies';
	}

	/**
	 * Mark a single topic as unread.
	 * Accessed by action=markasread;sa=topic
	 */
	public function action_marktopic()
	{
		global $board, $topic, $user_info;

		require_once(SUBSDIR . '/Topic.subs.php');
		require_once(SUBSDIR . '/Messages.subs.php');

		// Mark a topic unread.
		// First, let's figure out what the latest message is.
		$topicinfo = getTopicInfo($topic, 'all');
		$topic_msg_id = (int) $_GET['t'];

{'fraction_non_alphanumeric': 0.16411295428643788, 'fraction_numerical': 0.0007597821957705458, 'mean_word_length': 3.8813349814585907, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2485323', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2583, 'n_words': 679}