Shoutbox_Controller::action_index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package SimplePortal ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
use BBC\ParserWrapper;
13
use BBC\PreparseCode;
14
15
16
/**
17
 * Shoutbox controller.
18
 *
19
 * - This class handles requests for Shoutbox Functionality
20
 */
21
class Shoutbox_Controller extends Action_Controller
22
{
23
	/**
24
	 * Need the template for the shoutbox
25
	 */
26
	public function pre_dispatch()
27
	{
28
		loadTemplate('PortalShoutbox');
29
		require_once(SUBSDIR . '/PortalShoutbox.subs.php');
30
	}
31
32
	/**
33
	 * Default method
34
	 */
35
	public function action_index()
36
	{
37
		// We really only have one choice :P
38
		$this->action_sportal_shoutbox();
39
	}
40
41
	/**
42
	 * Override default method, just say no for xml
43
	 */
44
	public function trackStats($action = '')
45
	{
46
		if (isset($this->_req->xml))
47
		{
48
			return false;
49
		}
50
51
		return parent::trackStats($action);
52
	}
53
54
	/**
55
	 * The Shoutbox ... allows for the adding, editing, deleting and viewing of shouts
56
	 */
57
	public function action_sportal_shoutbox()
58
	{
59
		global $context, $scripturl, $user_info;
60
61
		// ID of the shoutbox we are working on and timestamp
62
		$shoutbox_id = !empty($_REQUEST['shoutbox_id']) ? (int) $_REQUEST['shoutbox_id'] : 0;
63
		$request_time = !empty($_REQUEST['time']) ? (int) $_REQUEST['time'] : 0;
64
65
		// We need to know which shoutbox this is for/from
66
		$context['SPortal']['shoutbox'] = sportal_get_shoutbox($shoutbox_id, true, true);
67
68
		// Shouting but no one is there to here you
69
		if (empty($context['SPortal']['shoutbox']))
70
		{
71
			if (isset($_REQUEST['xml']))
72
			{
73
				obExit(false, false);
74
			}
75
			else
76
			{
77
				throw new Elk_Exception('error_sp_shoutbox_not_exist', false);
78
			}
79
		}
80
81
		// Any warning title for the shoutbox, like Not For Support ;P
82
		$parser = ParserWrapper::instance();
83
		$context['SPortal']['shoutbox']['warning'] = $parser->parseMessage($context['SPortal']['shoutbox']['warning'], true);
84
85
		$can_moderate = allowedTo('sp_admin') || allowedTo('sp_manage_shoutbox');
86
		if (!$can_moderate && !empty($context['SPortal']['shoutbox']['moderator_groups']))
87
		{
88
			$can_moderate = count(array_intersect($user_info['groups'], $context['SPortal']['shoutbox']['moderator_groups'])) > 0;
89
		}
90
91
		// Adding a shout
92
		if (!empty($_REQUEST['shout']))
93
		{
94
			// Pretty basic
95
			is_not_guest();
96
			checkSession('request');
97
98
			// If you are not flooding the system, add the shout to the box
99
			if (!($flood = sp_prevent_flood('spsbp', false)))
100
			{
101
				require_once(SUBSDIR . '/Post.subs.php');
102
103
				$_REQUEST['shout'] = Util::htmlspecialchars(trim($_REQUEST['shout']));
104
				$preparse = PreparseCode::instance();
105
				$preparse->preparsecode($_REQUEST['shout'], false);
106
107
				if (!empty($_REQUEST['shout']))
108
				{
109
					sportal_create_shout($context['SPortal']['shoutbox'], $_REQUEST['shout']);
110
				}
111
			}
112
			else
113
			{
114
				$context['SPortal']['shoutbox']['warning'] = $flood;
115
			}
116
		}
117
118
		// Removing a shout, regret saying that do you :P
119
		if (!empty($_REQUEST['delete']))
120
		{
121
			checkSession('request');
122
123
			if (!$can_moderate)
124
			{
125
				throw new Elk_Exception('error_sp_cannot_shoutbox_moderate', false);
126
			}
127
128
			$delete = (int) $_REQUEST['delete'];
129
130
			if (!empty($delete))
131
			{
132
				sportal_delete_shout($shoutbox_id, $delete);
133
			}
134
		}
135
136
		// Responding to an ajax request
137
		if (isset($_REQUEST['xml']))
138
		{
139
			$shout_parameters = array(
140
				'limit' => $context['SPortal']['shoutbox']['num_show'],
141
				'bbc' => $context['SPortal']['shoutbox']['allowed_bbc'],
142
				'reverse' => $context['SPortal']['shoutbox']['reverse'],
143
				'cache' => $context['SPortal']['shoutbox']['caching'],
144
				'can_moderate' => $can_moderate,
145
			);
146
147
			// Get all the shouts for this box
148
			$context['SPortal']['shouts'] = sportal_get_shouts($shoutbox_id, $shout_parameters);
149
150
			// Return a clean xml response
151
			Template_Layers::instance()->removeAll();
152
			$context['sub_template'] = 'shoutbox_xml';
153
			$context['SPortal']['updated'] = empty($context['SPortal']['shoutbox']['last_update']) || $context['SPortal']['shoutbox']['last_update'] > $request_time;
154
155
			return;
156
		}
157
158
		// Show all the shouts in this box
159
		$total_shouts = sportal_get_shoutbox_count($shoutbox_id);
160
161
		$context['per_page'] = $context['SPortal']['shoutbox']['num_show'];
162
		$context['start'] = !empty($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
163
		$context['page_index'] = constructPageIndex($scripturl . '?action=shoutbox;shoutbox_id=' . $shoutbox_id, $context['start'], $total_shouts, $context['per_page']);
164
165
		$shout_parameters = array(
166
			'start' => $context['start'],
167
			'limit' => $context['per_page'],
168
			'bbc' => $context['SPortal']['shoutbox']['allowed_bbc'],
169
			'cache' => $context['SPortal']['shoutbox']['caching'],
170
			'can_moderate' => $can_moderate,
171
		);
172
173
		$context['SPortal']['shouts_history'] = sportal_get_shouts($shoutbox_id, $shout_parameters);
174
		$context['SPortal']['shoutbox_id'] = $shoutbox_id;
175
		$context['sub_template'] = 'shoutbox_all';
176
		$context['page_title'] = $context['SPortal']['shoutbox']['name'];
177
	}
178
}
179