1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\message;
15
16/**
17* Class topic_form
18* Form used to send topics as notification emails
19*/
20class topic_form extends form
21{
22	/** @var int */
23	protected $topic_id;
24	/** @var array */
25	protected $topic_row;
26	/** @var string */
27	protected $recipient_address;
28	/** @var string */
29	protected $recipient_name;
30	/** @var string */
31	protected $recipient_lang;
32
33	/**
34	* Get the data of the topic
35	*
36	* @param int $topic_id
37	* @return	false|array		false if the topic does not exist, array otherwise
38	*/
39	protected function get_topic_row($topic_id)
40	{
41		$sql = 'SELECT forum_id, topic_title
42			FROM ' . TOPICS_TABLE . '
43			WHERE topic_id = ' . (int) $topic_id;
44		$result = $this->db->sql_query($sql);
45		$row = $this->db->sql_fetchrow($result);
46		$this->db->sql_freeresult($result);
47
48		return $row;
49	}
50
51	/**
52	* {inheritDoc}
53	*/
54	public function check_allow()
55	{
56		$error = parent::check_allow();
57		if ($error)
58		{
59			return $error;
60		}
61
62		if (!$this->auth->acl_get('u_sendemail'))
63		{
64			return 'NO_EMAIL';
65		}
66
67		if (!$this->topic_row)
68		{
69			return 'NO_TOPIC';
70		}
71
72		if (!$this->auth->acl_get('f_read', $this->topic_row['forum_id']))
73		{
74			if ($this->user->data['user_id'] != ANONYMOUS)
75			{
76				send_status_line(403, 'Forbidden');
77			}
78			else
79			{
80				send_status_line(401, 'Unauthorized');
81			}
82			return 'SORRY_AUTH_READ';
83		}
84
85		if (!$this->auth->acl_get('f_email', $this->topic_row['forum_id']))
86		{
87			return 'NO_EMAIL';
88		}
89
90		return false;
91	}
92
93	/**
94	* {inheritDoc}
95	*/
96	public function bind(\phpbb\request\request_interface $request)
97	{
98		parent::bind($request);
99
100		$this->topic_id = $request->variable('t', 0);
101		$this->recipient_address = $request->variable('email', '');
102		$this->recipient_name = $request->variable('name', '', true);
103		$this->recipient_lang = $request->variable('lang', $this->config['default_lang']);
104
105		$this->topic_row = $this->get_topic_row($this->topic_id);
106	}
107
108	/**
109	* {inheritDoc}
110	*/
111	public function submit(\messenger $messenger)
112	{
113		if (!$this->recipient_address || !preg_match('/^' . get_preg_expression('email') . '$/i', $this->recipient_address))
114		{
115			$this->errors[] = $this->user->lang['EMPTY_ADDRESS_EMAIL'];
116		}
117
118		if (!$this->recipient_name)
119		{
120			$this->errors[] = $this->user->lang['EMPTY_NAME_EMAIL'];
121		}
122
123		$this->message->set_template('email_notify');
124		$this->message->set_template_vars(array(
125			'TOPIC_NAME'	=> htmlspecialchars_decode($this->topic_row['topic_title'], ENT_COMPAT),
126			'U_TOPIC'		=> generate_board_url() . '/viewtopic.' . $this->phpEx . '?f=' . $this->topic_row['forum_id'] . '&t=' . $this->topic_id,
127		));
128		$this->message->set_body($this->body);
129		$this->message->add_recipient(
130			$this->recipient_name,
131			$this->recipient_address,
132			$this->recipient_lang,
133			NOTIFY_EMAIL
134		);
135		$this->message->set_sender_notify_type(NOTIFY_EMAIL);
136
137		parent::submit($messenger);
138	}
139
140	/**
141	* {inheritDoc}
142	*/
143	public function get_return_message()
144	{
145		return sprintf($this->user->lang['RETURN_TOPIC'],  '<a href="' . append_sid($this->phpbb_root_path . 'viewtopic.' . $this->phpEx, 'f=' . $this->topic_row['forum_id'] . '&amp;t=' . $this->topic_id) . '">', '</a>');
146	}
147
148	/**
149	* {inheritDoc}
150	*/
151	public function render(\phpbb\template\template $template)
152	{
153		parent::render($template);
154
155		$this->user->add_lang('viewtopic');
156		$template->assign_vars(array(
157			'EMAIL'				=> $this->recipient_address,
158			'NAME'				=> $this->recipient_name,
159			'S_LANG_OPTIONS'	=> language_select($this->recipient_lang),
160			'MESSAGE'			=> $this->body,
161
162			'L_EMAIL_BODY_EXPLAIN'	=> $this->user->lang['EMAIL_TOPIC_EXPLAIN'],
163			'S_POST_ACTION'			=> append_sid($this->phpbb_root_path . 'memberlist.' . $this->phpEx, 'mode=email&amp;t=' . $this->topic_id))
164		);
165	}
166}
167