<commit_before><?php

/**
 * Integration system for attachments into Post controller
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This file contains code covered by:
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.1 beta 1
 *
 */

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

class Attachments_Post_Module implements ElkArte\sources\modules\Module_Interface
{
	/**
	 * The mode of attachments (disabled/enabled/show only).
	 * @var int
	 */
	protected static $_attach_level = 0;

	/**
	 * The objects that keeps track of errors.
	 * @var Attachment_Error_Context
	 */
	protected $_attach_errors = null;

	/**
	 * List of attachments ID already saved.
	 * @var int[]
	 */
	protected $_saved_attach_id = array();

	/**
	 * If it is a new message or if it is an existing one edited.
	 * @var bool
	 */
	protected $_is_new_message = false;

	/**
	 * {@inheritdoc }
	 */
	public static function hooks(\Event_Manager $eventsManager)
	{
		global $modSettings;

		if (!empty($modSettings['attachmentEnable']))
		{
			self::$_attach_level = $modSettings['attachmentEnable'];

			return array(
				array('prepare_post', array('Attachments_Post_Module', 'prepare_post'), array()),
				array('prepare_context', array('Attachments_Post_Module', 'prepare_context'), array('post_errors')),
				array('finalize_post_form', array('Attachments_Post_Module', 'finalize_post_form'), array('show_additional_options', 'board', 'topic')),

				array('prepare_save_post', array('Attachments_Post_Module', 'prepare_save_post'), array('post_errors')),
				array('pre_save_post', array('Attachments_Post_Module', 'pre_save_post'), array('msgOptions')),
				array('after_save_post', array('Attachments_Post_Module', 'after_save_post'), array('msgOptions')),

				array('before_save_draft', array('Attachments_Post_Module', 'before_save_draft'), array('draft')),
			);
		}
		else
			return array();
	}

	public function before_save_draft($draft)
	{
		$this->saveAttachments(isset($_REQUEST['id_draft']) ? (int) $_REQUEST['id_draft'] : 0);
	}

	public function prepare_post()
	{
		$this->_attach_errors = Attachment_Error_Context::context();

		$this->_attach_errors->activate();
	}

	public function prepare_context($post_errors)
	{
		global $context;

		// An array to hold all the attachments for this topic.
		$context['attachments']['current'] = array();

		if ($this->_attach_errors->hasErrors())
			$post_errors->add(array('attachments_errors' => $this->_attach_errors));
	}

	public function finalize_post_form(&$show_additional_options, $board, $topic)
	{
		global $txt, $context, $modSettings, $user_info, $scripturl;

		$context['attachments']['can']['post'] = self::$_attach_level == 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments')));
		if ($context['attachments']['can']['post'])
		{
			// If there are attachments, calculate the total size and how many.
			$attachments = array();
			$attachments['total_size'] = 0;
			$attachments['quantity'] = 0;

			// If this isn't a new post, check the current attachments.
			if (isset($_REQUEST['msg']))
			{
				$attachments['quantity'] = count($context['attachments']['current']);
				foreach ($context['attachments']['current'] as $attachment)
					$attachments['total_size'] += $attachment['size'];
			}

			// A bit of house keeping first.
			if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1)
				unset($_SESSION['temp_attachments']);

			if (!empty($_SESSION['temp_attachments']))
			{
				// Is this a request to delete them?
				if (isset($_GET['delete_temp']))
				{
					foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
					{
						if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
							@unlink($attachment['tmp_name']);
					}
					$this->_attach_errors->addError('temp_attachments_gone');
					$_SESSION['temp_attachments'] = array();
				}
				// Hmm, coming in fresh and there are files in session.
				elseif ($context['current_action'] != 'post2' || !empty($_POST['from_qr']))
				{
					// Let's be nice and see if they belong here first.
					if ((empty($_REQUEST['msg']) && empty($_SESSION['temp_attachments']['post']['msg']) && $_SESSION['temp_attachments']['post']['board'] == $board) || (!empty($_REQUEST['msg']) && $_SESSION['temp_attachments']['post']['msg'] == $_REQUEST['msg']))
					{
						// See if any files still exist before showing the warning message and the files attached.
						foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
						{
							if (strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
								continue;

							if (file_exists($attachment['tmp_name']))
							{
								$this->_attach_errors->addError('temp_attachments_new');
								$context['files_in_session_warning'] = $txt['attached_files_in_session'];
								unset($_SESSION['temp_attachments']['post']['files']);
								break;
							}
						}
					}
					else
					{
						// Since, they don't belong here. Let's inform the user that they exist..
						if (!empty($topic))
							$delete_url = $scripturl . '?action=post' . (!empty($_REQUEST['msg']) ? (';msg=' . $_REQUEST['msg']) : '') . (!empty($_REQUEST['last_msg']) ? (';last_msg=' . $_REQUEST['last_msg']) : '') . ';topic=' . $topic . ';delete_temp';
						else
							$delete_url = $scripturl . '?action=post;board=' . $board . ';delete_temp';

						// Compile a list of the files to show the user.
						$file_list = array();
						foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
							if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
								$file_list[] = $attachment['name'];

						$_SESSION['temp_attachments']['post']['files'] = $file_list;
						$file_list = '<div class="attachments">' . implode('<br />', $file_list) . '</div>';

						if (!empty($_SESSION['temp_attachments']['post']['msg']))
						{
							// We have a message id, so we can link back to the old topic they were trying to edit..
							$goback_link = '<a href="' . $scripturl . '?action=post' . (!empty($_SESSION['temp_attachments']['post']['msg']) ? (';msg=' . $_SESSION['temp_attachments']['post']['msg']) : '') . (!empty($_SESSION['temp_attachments']['post']['last_msg']) ? (';last_msg=' . $_SESSION['temp_attachments']['post']['last_msg']) : '') . ';topic=' . $_SESSION['temp_attachments']['post']['topic'] . ';additionalOptions">' . $txt['here'] . '</a>';

							$this->_attach_errors->addError(array('temp_attachments_found', array($delete_url, $goback_link, $file_list)));
							$context['ignore_temp_attachments'] = true;
						}
						else
						{
							$this->_attach_errors->addError(array('temp_attachments_lost', array($delete_url, $file_list)));
							$context['ignore_temp_attachments'] = true;
						}
					}
				}

				foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
				{
					// Skipping over these
					if (isset($context['ignore_temp_attachments']) || isset($_SESSION['temp_attachments']['post']['files']))
						break;<commit_msg>Fix "attachment found go back" link<commit_after><?php

/**
 * Integration system for attachments into Post controller
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This file contains code covered by:
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.1 beta 1
 *
 */

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

class Attachments_Post_Module implements ElkArte\sources\modules\Module_Interface
{
	/**
	 * The mode of attachments (disabled/enabled/show only).
	 * @var int
	 */
	protected static $_attach_level = 0;

	/**
	 * The objects that keeps track of errors.
	 * @var Attachment_Error_Context
	 */
	protected $_attach_errors = null;

	/**
	 * List of attachments ID already saved.
	 * @var int[]
	 */
	protected $_saved_attach_id = array();

	/**
	 * If it is a new message or if it is an existing one edited.
	 * @var bool
	 */
	protected $_is_new_message = false;

	/**
	 * {@inheritdoc }
	 */
	public static function hooks(\Event_Manager $eventsManager)
	{
		global $modSettings;

		if (!empty($modSettings['attachmentEnable']))
		{
			self::$_attach_level = $modSettings['attachmentEnable'];

			return array(
				array('prepare_post', array('Attachments_Post_Module', 'prepare_post'), array()),
				array('prepare_context', array('Attachments_Post_Module', 'prepare_context'), array('post_errors')),
				array('finalize_post_form', array('Attachments_Post_Module', 'finalize_post_form'), array('show_additional_options', 'board', 'topic')),

				array('prepare_save_post', array('Attachments_Post_Module', 'prepare_save_post'), array('post_errors')),
				array('pre_save_post', array('Attachments_Post_Module', 'pre_save_post'), array('msgOptions')),
				array('after_save_post', array('Attachments_Post_Module', 'after_save_post'), array('msgOptions')),

				array('before_save_draft', array('Attachments_Post_Module', 'before_save_draft'), array('draft')),
			);
		}
		else
			return array();
	}

	public function before_save_draft($draft)
	{
		$this->saveAttachments(isset($_REQUEST['id_draft']) ? (int) $_REQUEST['id_draft'] : 0);
	}

	public function prepare_post()
	{
		$this->_attach_errors = Attachment_Error_Context::context();

		$this->_attach_errors->activate();
	}

	public function prepare_context($post_errors)
	{
		global $context;

		// An array to hold all the attachments for this topic.
		$context['attachments']['current'] = array();

		if ($this->_attach_errors->hasErrors())
			$post_errors->add(array('attachments_errors' => $this->_attach_errors));
	}

	public function finalize_post_form(&$show_additional_options, $board, $topic)
	{
		global $txt, $context, $modSettings, $user_info, $scripturl;

		$context['attachments']['can']['post'] = self::$_attach_level == 1 && (allowedTo('post_attachment') || ($modSettings['postmod_active'] && allowedTo('post_unapproved_attachments')));
		if ($context['attachments']['can']['post'])
		{
			// If there are attachments, calculate the total size and how many.
			$attachments = array();
			$attachments['total_size'] = 0;
			$attachments['quantity'] = 0;

			// If this isn't a new post, check the current attachments.
			if (isset($_REQUEST['msg']))
			{
				$attachments['quantity'] = count($context['attachments']['current']);
				foreach ($context['attachments']['current'] as $attachment)
					$attachments['total_size'] += $attachment['size'];
			}

			// A bit of house keeping first.
			if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1)
				unset($_SESSION['temp_attachments']);

			if (!empty($_SESSION['temp_attachments']))
			{
				// Is this a request to delete them?
				if (isset($_GET['delete_temp']))
				{
					foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
					{
						if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
							@unlink($attachment['tmp_name']);
					}
					$this->_attach_errors->addError('temp_attachments_gone');
					$_SESSION['temp_attachments'] = array();
				}
				// Hmm, coming in fresh and there are files in session.
				elseif ($context['current_action'] != 'post2' || !empty($_POST['from_qr']))
				{
					// Let's be nice and see if they belong here first.
					if ((empty($_REQUEST['msg']) && empty($_SESSION['temp_attachments']['post']['msg']) && $_SESSION['temp_attachments']['post']['board'] == $board) || (!empty($_REQUEST['msg']) && $_SESSION['temp_attachments']['post']['msg'] == $_REQUEST['msg']))
					{
						// See if any files still exist before showing the warning message and the files attached.
						foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
						{
							if (strpos($attachID, 'post_tmp_' . $user_info['id']) === false)
								continue;

							if (file_exists($attachment['tmp_name']))
							{
								$this->_attach_errors->addError('temp_attachments_new');
								$context['files_in_session_warning'] = $txt['attached_files_in_session'];
								unset($_SESSION['temp_attachments']['post']['files']);
								break;
							}
						}
					}
					else
					{
						// Since, they don't belong here. Let's inform the user that they exist..
						if (!empty($topic))
							$delete_url = $scripturl . '?action=post' . (!empty($_REQUEST['msg']) ? (';msg=' . $_REQUEST['msg']) : '') . (!empty($_REQUEST['last_msg']) ? (';last_msg=' . $_REQUEST['last_msg']) : '') . ';topic=' . $topic . ';delete_temp';
						else
							$delete_url = $scripturl . '?action=post;board=' . $board . ';delete_temp';

						// Compile a list of the files to show the user.
						$file_list = array();
						foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
							if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false)
								$file_list[] = $attachment['name'];

						$_SESSION['temp_attachments']['post']['files'] = $file_list;
						$file_list = '<div class="attachments">' . implode('<br />', $file_list) . '</div>';

						if (!empty($_SESSION['temp_attachments']['post']['msg']))
						{
							// We have a message id, so we can link back to the old topic they were trying to edit..
							$goback_url = $scripturl . '?action=post' . (!empty($_SESSION['temp_attachments']['post']['msg']) ? (';msg=' . $_SESSION['temp_attachments']['post']['msg']) : '') . (!empty($_SESSION['temp_attachments']['post']['last_msg']) ? (';last_msg=' . $_SESSION['temp_attachments']['post']['last_msg']) : '') . ';topic=' . $_SESSION['temp_attachments']['post']['topic'] . ';additionalOptions';

							$this->_attach_errors->addError(array('temp_attachments_found', array($delete_url, $goback_url, $file_list)));
							$context['ignore_temp_attachments'] = true;
						}
						else
						{
							$this->_attach_errors->addError(array('temp_attachments_lost', array($delete_url, $file_list)));
							$context['ignore_temp_attachments'] = true;
						}
					}
				}

				foreach ($_SESSION['temp_attachments'] as $attachID => $attachment)
				{
					// Skipping over these
					if (isset($context['ignore_temp_attachments']) || isset($_SESSION['temp_attachments']['post']['files']))
						break;
{'fraction_non_alphanumeric': 0.18070127001656544, 'fraction_numerical': 0.0020706791827719492, 'mean_word_length': 4.313164649798313, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 53, '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': '415112', 'n_tokens_mistral': 5299, 'n_tokens_neox': 4374, 'n_words': 936}