Passed
Pull Request — release-2.1 (#6100)
by John
10:55
created

prepareAgreementContext()   C

Complexity

Conditions 13
Paths 190

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 25
nc 190
nop 0
dl 0
loc 46
rs 5.8666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file handles the user and privacy policy agreements.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2020 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
if (!defined('SMF'))
17
	die('Hacking attempt...');
18
19
/*	The purpose of this file is to show the user an updated registration
20
	agreement, and get them to agree to it.
21
22
	bool prepareAgreementContext()
23
		// !!!
24
25
	bool canRequireAgreement()
26
		// !!!
27
28
	bool canRequirePrivacyPolicy()
29
		// !!!
30
31
	void Agreement()
32
		- Show the new registration agreement
33
34
	void AcceptAgreement()
35
		- Called when they actually accept the agreement
36
		- Save the date of the current agreement to the members database table
37
		- Redirect back to wherever they came from
38
*/
39
40
function prepareAgreementContext()
41
{
42
	global $boarddir, $context, $modSettings, $user_info, $language;
43
44
	if (!empty($context['show_agreement']))
45
	{
46
		// Grab the agreement.
47
		// Have we got a localized one?
48
		if (file_exists($boarddir . '/agreement.' . $user_info['language'] . '.txt'))
49
			$context['agreement_file'] = $boarddir . '/agreement.' . $user_info['language'] . '.txt';
50
		elseif (file_exists($boarddir . '/agreement.txt'))
51
			$context['agreement_file'] = $boarddir . '/agreement.txt';
52
53
		if (!empty($context['agreement_file']))
54
		{
55
			$cache_id = strtr($context['agreement_file'], array($boarddir => '', '.txt' => '', '.' => '_'));
56
			$context['agreement'] = parse_bbc(file_get_contents($context['agreement_file']), true, $cache_id);
57
		}
58
59
		// An agreement file must be present and its text cannot be empty.
60
		if (empty($context['agreement']))
61
		{
62
			if ($_REQUEST['sa'] == 'both')
63
				$context['show_agreement'] = false;
64
			else
65
				fatal_lang_error('no_agreement', false);
66
		}
67
	}
68
69
	if (!empty($context['show_privacy_policy']))
70
	{
71
		// Have we got a localized policy?
72
		if (!empty($modSettings['policy_' . $user_info['language']]))
73
			$context['policy'] = parse_bbc($modSettings['policy_' . $user_info['language']]);
74
		elseif (!empty($modSettings['policy_' . $language]))
75
			$context['policy'] = parse_bbc($modSettings['policy_' . $language]);
76
		// Then I guess we've got nothing
77
		elseif ($_REQUEST['sa'] == 'both')
78
			$context['show_privacy_policy'] = false;
79
		else
80
			fatal_lang_error('no_privacy_policy', false);
81
	}
82
83
	// Nothing to show? That's no good.
84
	if (empty($context['show_agreement']) && empty($context['show_privacy_policy']))
85
		fatal_lang_error('no_agreement', false);
86
}
87
88
function canRequireAgreement()
89
{
90
	global $modSettings, $options, $user_info, $context;
91
92
	// Guests can't agree
93
	if (!empty($user_info['is_guest']) || empty($modSettings['requireAgreement']))
94
		return false;
95
96
	$agreement_lang = strpos($context['agreement_file'], 'agreement.' . $user_info['language'] . '.txt') !== false ? $user_info['language'] : 'default';
97
98
	if (empty($modSettings['agreement_updated_' . $agreement_lang]))
99
		return false;
100
101
	$context['agreement_accepted_date'] = empty($options['agreement_accepted']) ? 0 : $options['agreement_accepted'];
102
103
	// A new timestamp means that there are new changes to the registration agreement and must therefore be shown.
104
	return empty($options['agreement_accepted']) || $modSettings['agreement_updated_' . $agreement_lang] > $options['agreement_accepted'];
105
}
106
107
function canRequirePrivacyPolicy()
108
{
109
	global $modSettings, $options, $user_info, $language, $context;
110
111
	if (!empty($user_info['is_guest']) || empty($modSettings['requirePolicyAgreement']))
112
		return false;
113
114
	$policy_lang = !empty($modSettings['policy_' . $user_info['language']]) ? $user_info['language'] : $language;
115
116
	if (empty($modSettings['policy_updated_' . $policy_lang]))
117
		return false;
118
119
	$context['privacy_policy_accepted_date'] = empty($options['policy_accepted']) ? 0 : $options['policy_accepted'];
120
121
	return empty($options['policy_accepted']) || $modSettings['policy_updated_' . $policy_lang] > $options['policy_accepted'];
122
}
123
124
// Let's tell them there's a new agreement
125
function Agreement()
126
{
127
	global $context, $scripturl, $txt;
128
129
	// Keep it sanitary
130
	if (!isset($_REQUEST['sa']) || !in_array($_REQUEST['sa'], array('agreement', 'policy')))
131
		$_REQUEST['sa'] = 'both';
132
133
	// Now, what do we actually want to show?
134
	$context['show_agreement'] = in_array($_REQUEST['sa'], array('agreement', 'both'));
135
	$context['show_privacy_policy'] = in_array($_REQUEST['sa'], array('policy', 'both'));
136
137
	prepareAgreementContext();
138
139
	// What, if anything, do they need to accept?
140
	$context['can_accept_agreement'] = !empty($context['show_agreement']) && canRequireAgreement();
141
	$context['can_accept_privacy_policy'] = !empty($context['show_privacy_policy']) && canRequirePrivacyPolicy();
142
143
	// The form will need to tell us what they are accepting
144
	if (!empty($context['can_accept_agreement']) && !empty($context['can_accept_privacy_policy']))
145
		$context['accept_doc'] = 'both';
146
	elseif (!empty($context['can_accept_agreement']))
147
		$context['accept_doc'] = 'agreement';
148
	elseif (!empty($context['can_accept_privacy_policy']))
149
		$context['accept_doc'] = 'policy';
150
151
	loadLanguage('Agreement');
152
	loadTemplate('Agreement');
153
154
	if (!empty($context['show_agreement']) && !empty($context['show_privacy_policy']))
155
		$page_title = $txt['agreement_and_privacy_policy'];
156
	elseif (!empty($context['show_agreement']))
157
		$page_title = $txt['agreement'];
158
	elseif (!empty($context['show_privacy_policy']))
159
		$page_title = $txt['privacy_policy'];
160
161
	if (isset($_SESSION['old_url']))
162
		$_SESSION['redirect_url'] = $_SESSION['old_url'];
163
	$context['page_title'] = $page_title;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $page_title does not seem to be defined for all execution paths leading up to this point.
Loading history...
164
	$context['linktree'][] = array(
165
		'url' => $scripturl . '?action=agreement',
166
		'name' => $txt['agreement_and_privacy_policy'],
167
	);
168
169
	if ($_REQUEST['sa'] !== 'both')
170
		$context['linktree'][] = array(
171
			'url' => $scripturl . '?action=agreement;sa=' . $_REQUEST['sa'],
172
			'name' => $context['page_title'],
173
		);
174
}
175
176
// I solemly swear to no longer chase squirrels.
177
function AcceptAgreement()
178
{
179
	global $smcFunc, $user_info, $context;
180
181
	// No funny business allowed
182
	if (!isset($_REQUEST['doc']) || !in_array($_REQUEST['doc'], array('agreement', 'policy', 'both')))
183
		redirectexit('action=agreement');
184
185
	// Zero in on the agreement for this...
186
	$context['show_agreement'] = in_array($_REQUEST['doc'], array('agreement', 'both'));
187
	$context['show_privacy_policy'] = in_array($_REQUEST['doc'], array('policy', 'both'));
188
189
	prepareAgreementContext();
190
191
	// Can they agree to what they are trying to agree to?
192
	if (!empty($context['show_agreement']) && !canRequireAgreement())
193
		redirectexit('action=agreement');
194
	if (!empty($context['show_privacy_policy']) && !canRequirePrivacyPolicy())
195
		redirectexit('action=agreement');
196
197
	checkSession();
198
199
	if (in_array($_REQUEST['doc'], array('agreement', 'both')))
200
	{
201
		$smcFunc['db_insert']('replace',
202
			'{db_prefix}themes',
203
			array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string', 'value' => 'string'),
204
			array($user_info['id'], 1, 'agreement_accepted', time()),
205
			array('id_member', 'id_theme', 'variable')
206
		);
207
		logAction('agreement_accepted', array('applicator' => $user_info['id']), 'user');
208
	}
209
210
	if (in_array($_REQUEST['doc'], array('policy', 'both')))
211
	{
212
		$smcFunc['db_insert']('replace',
213
			'{db_prefix}themes',
214
			array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string', 'value' => 'string'),
215
			array($user_info['id'], 1, 'policy_accepted', time()),
216
			array('id_member', 'id_theme', 'variable')
217
		);
218
		logAction('policy_accepted', array('applicator' => $user_info['id']), 'user');
219
	}
220
221
	// Redirect back to chasing those squirrels, er, viewing those memes.
222
	redirectexit(!empty($_SESSION['redirect_url']) ? $_SESSION['redirect_url'] : '');
223
}
224
225
?>